|
| 1 | +use crate::cmp::Eq; |
| 2 | + |
| 3 | +unconstrained fn __get_shuffle_indices<T, let N: u32>(lhs: [T; N], rhs: [T; N]) -> [Field; N] where T: Eq { |
| 4 | + let mut shuffle_indices: [Field;N ] = [0; N]; |
| 5 | + |
| 6 | + let mut shuffle_mask: [bool; N] = [false; N]; |
| 7 | + for i in 0..N { |
| 8 | + let mut found = false; |
| 9 | + for j in 0..N { |
| 10 | + if ((shuffle_mask[j] == false) & (!found)) { |
| 11 | + if (lhs[i] == rhs[j]) { |
| 12 | + found = true; |
| 13 | + shuffle_indices[i] = j as Field; |
| 14 | + shuffle_mask[j] = true; |
| 15 | + } |
| 16 | + } |
| 17 | + if (found) { |
| 18 | + continue; |
| 19 | + } |
| 20 | + } |
| 21 | + assert(found == true, "check_shuffle, lhs and rhs arrays do not contain equivalent values"); |
| 22 | + } |
| 23 | + |
| 24 | + shuffle_indices |
| 25 | +} |
| 26 | + |
| 27 | +unconstrained fn __get_index<let N: u32>(indices: [Field; N], idx: Field) -> Field { |
| 28 | + let mut result = 0; |
| 29 | + for i in 0..N { |
| 30 | + if (indices[i] == idx) { |
| 31 | + result = i as Field; |
| 32 | + break; |
| 33 | + } |
| 34 | + } |
| 35 | + result |
| 36 | +} |
| 37 | + |
| 38 | +pub(crate) fn check_shuffle<T, let N: u32>(lhs: [T; N], rhs: [T; N]) where T: Eq { |
| 39 | + unsafe { |
| 40 | + let shuffle_indices = __get_shuffle_indices(lhs, rhs); |
| 41 | + |
| 42 | + for i in 0..N { |
| 43 | + let idx = __get_index(shuffle_indices, i as Field); |
| 44 | + assert_eq(shuffle_indices[idx], i as Field); |
| 45 | + } |
| 46 | + for i in 0..N { |
| 47 | + let idx = shuffle_indices[i]; |
| 48 | + let expected = rhs[idx]; |
| 49 | + let result = lhs[i]; |
| 50 | + assert_eq(expected, result); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +mod test { |
| 56 | + use super::check_shuffle; |
| 57 | + use crate::cmp::Eq; |
| 58 | + |
| 59 | + struct CompoundStruct { |
| 60 | + a: bool, |
| 61 | + b: Field, |
| 62 | + c: u64 |
| 63 | + } |
| 64 | + impl Eq for CompoundStruct { |
| 65 | + fn eq(self, other: Self) -> bool { |
| 66 | + (self.a == other.a) & (self.b == other.b) & (self.c == other.c) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn test_shuffle() { |
| 72 | + let lhs: [Field; 5] = [0, 1, 2, 3, 4]; |
| 73 | + let rhs: [Field; 5] = [2, 0, 3, 1, 4]; |
| 74 | + check_shuffle(lhs, rhs); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn test_shuffle_identity() { |
| 79 | + let lhs: [Field; 5] = [0, 1, 2, 3, 4]; |
| 80 | + let rhs: [Field; 5] = [0, 1, 2, 3, 4]; |
| 81 | + check_shuffle(lhs, rhs); |
| 82 | + } |
| 83 | + |
| 84 | + #[test(should_fail_with = "check_shuffle, lhs and rhs arrays do not contain equivalent values")] |
| 85 | + fn test_shuffle_fail() { |
| 86 | + let lhs: [Field; 5] = [0, 1, 2, 3, 4]; |
| 87 | + let rhs: [Field; 5] = [0, 1, 2, 3, 5]; |
| 88 | + check_shuffle(lhs, rhs); |
| 89 | + } |
| 90 | + |
| 91 | + #[test(should_fail_with = "check_shuffle, lhs and rhs arrays do not contain equivalent values")] |
| 92 | + fn test_shuffle_duplicates() { |
| 93 | + let lhs: [Field; 5] = [0, 1, 2, 3, 4]; |
| 94 | + let rhs: [Field; 5] = [0, 1, 2, 3, 3]; |
| 95 | + check_shuffle(lhs, rhs); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_shuffle_compound_struct() { |
| 100 | + let lhs: [CompoundStruct; 5] = [ |
| 101 | + CompoundStruct { a: false, b: 0, c: 12345 }, |
| 102 | + CompoundStruct { a: false, b: -100, c: 54321 }, |
| 103 | + CompoundStruct { a: true, b: 5, c: 0xffffffffffffffff }, |
| 104 | + CompoundStruct { a: true, b: 9814, c: 0xeeffee0011001133 }, |
| 105 | + CompoundStruct { a: false, b: 0x155, c: 0 } |
| 106 | + ]; |
| 107 | + let rhs: [CompoundStruct; 5] = [ |
| 108 | + CompoundStruct { a: false, b: 0x155, c: 0 }, |
| 109 | + CompoundStruct { a: false, b: 0, c: 12345 }, |
| 110 | + CompoundStruct { a: false, b: -100, c: 54321 }, |
| 111 | + CompoundStruct { a: true, b: 9814, c: 0xeeffee0011001133 }, |
| 112 | + CompoundStruct { a: true, b: 5, c: 0xffffffffffffffff } |
| 113 | + ]; |
| 114 | + check_shuffle(lhs, rhs); |
| 115 | + } |
| 116 | +} |
0 commit comments