Skip to content

Commit

Permalink
feat: Add BoundedVec::from_parts and `BoundedVec::from_parts_unchec…
Browse files Browse the repository at this point in the history
…ked` (#6691)

Co-authored-by: Maxim Vezenov <mvezenov@gmail.com>
  • Loading branch information
jfecher and vezenovm authored Dec 3, 2024
1 parent 3992d79 commit 768aa7c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
36 changes: 36 additions & 0 deletions docs/docs/noir/standard_library/containers/boundedvec.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,42 @@ Example:
let bounded_vec: BoundedVec<Field, 10> = BoundedVec::from_array([1, 2, 3])
```

### from_parts

```rust
pub fn from_parts(mut array: [T; MaxLen], len: u32) -> Self
```

Creates a new BoundedVec from the given array and length.
The given length must be less than or equal to the length of the array.

This function will zero out any elements at or past index `len` of `array`.
This incurs an extra runtime cost of O(MaxLen). If you are sure your array is
zeroed after that index, you can use `from_parts_unchecked` to remove the extra loop.

Example:

#include_code from-parts noir_stdlib/src/collections/bounded_vec.nr rust

### from_parts_unchecked

```rust
pub fn from_parts_unchecked(array: [T; MaxLen], len: u32) -> Self
```

Creates a new BoundedVec from the given array and length.
The given length must be less than or equal to the length of the array.

This function is unsafe because it expects all elements past the `len` index
of `array` to be zeroed, but does not check for this internally. Use `from_parts`
for a safe version of this function which does zero out any indices past the
given length. Invalidating this assumption can notably cause `BoundedVec::eq`
to give incorrect results since it will check even elements past `len`.

Example:

#include_code from-parts-unchecked noir_stdlib/src/collections/bounded_vec.nr rust

### map

```rust
Expand Down
92 changes: 91 additions & 1 deletion noir_stdlib/src/collections/bounded_vec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,58 @@ impl<T, let MaxLen: u32> BoundedVec<T, MaxLen> {
}
ret
}

/// Creates a new BoundedVec from the given array and length.
/// The given length must be less than or equal to the length of the array.
///
/// This function will zero out any elements at or past index `len` of `array`.
/// This incurs an extra runtime cost of O(MaxLen). If you are sure your array is
/// zeroed after that index, you can use `from_parts_unchecked` to remove the extra loop.
///
/// Example:
///
/// ```noir
/// let vec: BoundedVec<u32, 4> = BoundedVec::from_parts([1, 2, 3, 0], 3);
/// assert_eq(vec.len(), 3);
/// ```
pub fn from_parts(mut array: [T; MaxLen], len: u32) -> Self {
assert(len <= MaxLen);
let zeroed = crate::mem::zeroed();
for i in 0..MaxLen {
if i >= len {
array[i] = zeroed;
}
}
BoundedVec { storage: array, len }
}

/// Creates a new BoundedVec from the given array and length.
/// The given length must be less than or equal to the length of the array.
///
/// This function is unsafe because it expects all elements past the `len` index
/// of `array` to be zeroed, but does not check for this internally. Use `from_parts`
/// for a safe version of this function which does zero out any indices past the
/// given length. Invalidating this assumption can notably cause `BoundedVec::eq`
/// to give incorrect results since it will check even elements past `len`.
///
/// Example:
///
/// ```noir
/// let vec: BoundedVec<u32, 4> = BoundedVec::from_parts_unchecked([1, 2, 3, 0], 3);
/// assert_eq(vec.len(), 3);
///
/// // invalid use!
/// let vec1: BoundedVec<u32, 4> = BoundedVec::from_parts_unchecked([1, 2, 3, 1], 3);
/// let vec2: BoundedVec<u32, 4> = BoundedVec::from_parts_unchecked([1, 2, 3, 2], 3);
///
/// // both vecs have length 3 so we'd expect them to be equal, but this
/// // fails because elements past the length are still checked in eq
/// assert_eq(vec1, vec2); // fails
/// ```
pub fn from_parts_unchecked(array: [T; MaxLen], len: u32) -> Self {
assert(len <= MaxLen);
BoundedVec { storage: array, len }
}
}

impl<T, let MaxLen: u32> Eq for BoundedVec<T, MaxLen>
Expand All @@ -431,7 +483,11 @@ where
//
// 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)
if self.len == other.len {
self.storage == other.storage
} else {
false
}
}
}

Expand Down Expand Up @@ -598,4 +654,38 @@ mod bounded_vec_tests {
assert(bounded_vec1 != bounded_vec2);
}
}

mod from_parts {
use crate::collections::bounded_vec::BoundedVec;

#[test]
fn from_parts() {
// docs:start:from-parts
let vec: BoundedVec<u32, 4> = BoundedVec::from_parts([1, 2, 3, 0], 3);
assert_eq(vec.len(), 3);

// Any elements past the given length are zeroed out, so these
// two BoundedVecs will be completely equal
let vec1: BoundedVec<u32, 4> = BoundedVec::from_parts([1, 2, 3, 1], 3);
let vec2: BoundedVec<u32, 4> = BoundedVec::from_parts([1, 2, 3, 2], 3);
assert_eq(vec1, vec2);
// docs:end:from-parts
}

#[test]
fn from_parts_unchecked() {
// docs:start:from-parts-unchecked
let vec: BoundedVec<u32, 4> = BoundedVec::from_parts_unchecked([1, 2, 3, 0], 3);
assert_eq(vec.len(), 3);

// invalid use!
let vec1: BoundedVec<u32, 4> = BoundedVec::from_parts_unchecked([1, 2, 3, 1], 3);
let vec2: BoundedVec<u32, 4> = BoundedVec::from_parts_unchecked([1, 2, 3, 2], 3);

// both vecs have length 3 so we'd expect them to be equal, but this
// fails because elements past the length are still checked in eq
assert(vec1 != vec2);
// docs:end:from-parts-unchecked
}
}
}

0 comments on commit 768aa7c

Please sign in to comment.