Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simplify BoundedVec::eq #4838

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 6 additions & 31 deletions noir_stdlib/src/collections/bounded_vec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,12 @@ impl<T, MaxLen> BoundedVec<T, MaxLen> {

impl<T, MaxLen> Eq for BoundedVec<T, MaxLen> where T: Eq {
fn eq(self, other: BoundedVec<T, MaxLen>) -> 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)
}
}

Expand All @@ -131,26 +128,4 @@ mod bounded_vec_tests {

assert(bounded_vec1 != bounded_vec2);
}

#[test]
fn equality_respects_specified_length() {
let mut bounded_vec1: BoundedVec<Field, 3> = 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<Field, 3> = 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);
}
}
Loading