From 3d33a33e74c3e7d0fc511059b07f0ef9ddd9b667 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:17:41 +0100 Subject: [PATCH] feat: simplify `BoundedVec::eq` (#4838) # Description ## Problem\* Resolves ## Summary\* This PR follows up on #4830 to simplify the implementation based on discussion in that PR. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --- noir_stdlib/src/collections/bounded_vec.nr | 37 ++++------------------ 1 file changed, 6 insertions(+), 31 deletions(-) diff --git a/noir_stdlib/src/collections/bounded_vec.nr b/noir_stdlib/src/collections/bounded_vec.nr index b18a9e9ec5c..c6a3365a979 100644 --- a/noir_stdlib/src/collections/bounded_vec.nr +++ b/noir_stdlib/src/collections/bounded_vec.nr @@ -98,15 +98,12 @@ impl BoundedVec { impl Eq for BoundedVec where T: Eq { fn eq(self, other: BoundedVec) -> bool { - let mut ret = self.len == other.len; - let mut exceeded_len = false; - for i in 0..MaxLen { - exceeded_len |= i == self.len; - if !exceeded_len { - ret &= self.storage[i] == other.storage[i]; - } - } - ret + // TODO: https://github.com/noir-lang/noir/issues/4837 + // + // We make the assumption that the user has used the proper interface for working with `BoundedVec`s + // rather than directly manipulating the internal fields as this can result in an inconsistent internal state. + + (self.len == other.len) & (self.storage == other.storage) } } @@ -131,26 +128,4 @@ mod bounded_vec_tests { assert(bounded_vec1 != bounded_vec2); } - - #[test] - fn equality_respects_specified_length() { - let mut bounded_vec1: BoundedVec = BoundedVec::new(); - bounded_vec1.push(1); - - // This BoundedVec has an extra value past the end of its specified length, - // this should be ignored when checking equality so they are considered equal. - let mut bounded_vec2: BoundedVec = BoundedVec { storage: [1, 2, 0], len: 1 }; - - assert_eq(bounded_vec1, bounded_vec2); - - // Pushing another entry onto `bounded_vec1` to make the underlying arrays equal should - // result in the `BoundedVec`s being unequal as their lengths are different. - bounded_vec1.push(2); - - assert(bounded_vec1 != bounded_vec2); - - bounded_vec2.push(2); - - assert_eq(bounded_vec1, bounded_vec2); - } }