|
| 1 | +global U64_SHIFT_MULTIPLIER: Field = 2.pow_32(64); |
| 2 | + |
| 3 | +pub fn pack_two_u64s(high: u64, low: u64) -> Field { |
| 4 | + let low_field = low as Field; |
| 5 | + // We use multiplication instead of bit shifting operations to shift the high bits as bit shift operations are |
| 6 | + // expensive in circuits. |
| 7 | + let high_field: Field = (high as Field) * U64_SHIFT_MULTIPLIER; |
| 8 | + high_field + low_field |
| 9 | +} |
| 10 | + |
| 11 | +pub fn unpack_two_u64s(input: Field) -> (u64, u64) { |
| 12 | + input.assert_max_bit_size::<128>(); |
| 13 | + let low = (input as u64); |
| 14 | + // Use division instead of bit shift since bit shifts are expensive in circuits |
| 15 | + let high = ((input - (low as Field)) / U64_SHIFT_MULTIPLIER) as u64; |
| 16 | + (high, low) |
| 17 | +} |
| 18 | + |
| 19 | +mod tests { |
| 20 | + use super::{pack_two_u64s, unpack_two_u64s}; |
| 21 | + |
| 22 | + global U64_MAX: Field = 2.pow_32(64) - 1; |
| 23 | + global U128_MAX: Field = 2.pow_32(128) - 1; |
| 24 | + |
| 25 | + #[test] |
| 26 | + fn packing_two_u64s() { |
| 27 | + // Test case 1: All bits set |
| 28 | + let packed = pack_two_u64s(U64_MAX as u64, U64_MAX as u64); |
| 29 | + let (upper, lower) = unpack_two_u64s(packed); |
| 30 | + assert(lower == U64_MAX as u64, "Lower 64 bits should be all 1s"); |
| 31 | + assert(upper == U64_MAX as u64, "Upper 64 bits should be all 1s"); |
| 32 | + |
| 33 | + // Test case 2: Only upper 64 bits set |
| 34 | + let packed = pack_two_u64s(U64_MAX as u64, 0); |
| 35 | + let (upper, lower) = unpack_two_u64s(packed); |
| 36 | + assert(lower == 0, "Lower 64 bits should be 0"); |
| 37 | + assert(upper == U64_MAX as u64, "Upper 64 bits should be all 1s"); |
| 38 | + |
| 39 | + // Test case 3: Only lower 64 bits set |
| 40 | + let packed = pack_two_u64s(0, U64_MAX as u64); |
| 41 | + let (upper, lower) = unpack_two_u64s(packed); |
| 42 | + assert(lower == U64_MAX as u64, "Lower 64 bits should be all 1s"); |
| 43 | + assert(upper == 0, "Upper 64 bits should be 0"); |
| 44 | + |
| 45 | + // Test case 4: Zero |
| 46 | + let packed = pack_two_u64s(0, 0); |
| 47 | + let (upper, lower) = unpack_two_u64s(packed); |
| 48 | + assert(lower == 0, "Lower 64 bits should be 0"); |
| 49 | + assert(upper == 0, "Upper 64 bits should be 0"); |
| 50 | + } |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn unpacking_two_u64s() { |
| 54 | + // Test case 1: All bits set |
| 55 | + let input = U128_MAX; |
| 56 | + let (upper, lower) = unpack_two_u64s(input); |
| 57 | + assert(lower == U64_MAX as u64, "Lower 64 bits should be all 1s"); |
| 58 | + assert(upper == U64_MAX as u64, "Upper 64 bits should be all 1s"); |
| 59 | + |
| 60 | + // Test case 2: Only upper 64 bits set |
| 61 | + let input = U128_MAX - U64_MAX; |
| 62 | + let (upper, lower) = unpack_two_u64s(input); |
| 63 | + assert(lower == 0, "Lower 64 bits should be 0"); |
| 64 | + assert(upper == U64_MAX as u64, "Upper 64 bits should be all 1s"); |
| 65 | + |
| 66 | + // Test case 3: Only lower 64 bits set |
| 67 | + let input = U64_MAX; |
| 68 | + let (upper, lower) = unpack_two_u64s(input); |
| 69 | + assert(lower == U64_MAX as u64, "Lower 64 bits should be all 1s"); |
| 70 | + assert(upper == 0, "Upper 64 bits should be 0"); |
| 71 | + |
| 72 | + // Test case 4: Zero |
| 73 | + let input = 0; |
| 74 | + let (upper, lower) = unpack_two_u64s(input); |
| 75 | + assert(lower == 0, "Lower 64 bits should be 0"); |
| 76 | + assert(upper == 0, "Upper 64 bits should be 0"); |
| 77 | + } |
| 78 | + |
| 79 | + #[test] |
| 80 | + fn roundtrip_two_u64s() { |
| 81 | + let original_upper = 12345; |
| 82 | + let original_lower = 67890; |
| 83 | + let packed = pack_two_u64s(original_upper, original_lower); |
| 84 | + let (unpacked_upper, unpacked_lower) = unpack_two_u64s(packed); |
| 85 | + assert(original_upper == unpacked_upper, "Upper 64 bits should match after roundtrip"); |
| 86 | + assert(original_lower == unpacked_lower, "Lower 64 bits should match after roundtrip"); |
| 87 | + } |
| 88 | +} |
0 commit comments