From 041a671442b46ebd0301a299b5acbf13872e5d80 Mon Sep 17 00:00:00 2001 From: Nicolas Chamo Date: Wed, 18 Mar 2026 15:04:31 -0300 Subject: [PATCH] revert: remove assert_bounded_vec_trimmed (F-468) --- .../aztec/src/oracle/aes128_decrypt.nr | 7 +- .../aztec-nr/aztec/src/oracle/notes.nr | 6 +- .../aztec/src/utils/array/assert_trimmed.nr | 66 ------------------- .../aztec-nr/aztec/src/utils/array/mod.nr | 2 - 4 files changed, 2 insertions(+), 79 deletions(-) delete mode 100644 noir-projects/aztec-nr/aztec/src/utils/array/assert_trimmed.nr diff --git a/noir-projects/aztec-nr/aztec/src/oracle/aes128_decrypt.nr b/noir-projects/aztec-nr/aztec/src/oracle/aes128_decrypt.nr index b59ca4c0b0d5..29044c297770 100644 --- a/noir-projects/aztec-nr/aztec/src/oracle/aes128_decrypt.nr +++ b/noir-projects/aztec-nr/aztec/src/oracle/aes128_decrypt.nr @@ -1,5 +1,3 @@ -use crate::utils::array::assert_bounded_vec_trimmed; - #[oracle(aztec_utl_tryAes128Decrypt)] unconstrained fn try_aes128_decrypt_oracle( ciphertext: BoundedVec, @@ -21,10 +19,7 @@ pub unconstrained fn try_aes128_decrypt( iv: [u8; 16], sym_key: [u8; 16], ) -> Option> { - try_aes128_decrypt_oracle(ciphertext, iv, sym_key).map(|result: BoundedVec| { - assert_bounded_vec_trimmed(result); - result - }) + try_aes128_decrypt_oracle(ciphertext, iv, sym_key) } mod test { diff --git a/noir-projects/aztec-nr/aztec/src/oracle/notes.nr b/noir-projects/aztec-nr/aztec/src/oracle/notes.nr index bb5020cd5612..184ea85690bb 100644 --- a/noir-projects/aztec-nr/aztec/src/oracle/notes.nr +++ b/noir-projects/aztec-nr/aztec/src/oracle/notes.nr @@ -1,5 +1,4 @@ use crate::note::{HintedNote, note_interface::NoteType}; -use crate::utils::array::assert_bounded_vec_trimmed; use crate::protocol::{address::AztecAddress, traits::Packable}; @@ -143,7 +142,6 @@ where MaxNotes, as Packable>::N, ); - assert_bounded_vec_trimmed(packed_hinted_notes); let mut notes = BoundedVec::<_, MaxNotes>::new(); for i in 0..packed_hinted_notes.len() { @@ -170,9 +168,7 @@ where // both a preprocessor and a filter. let mut notes_array = [Option::none(); MaxNotes]; for i in 0..notes.len() { - if i < notes.len() { - notes_array[i] = Option::some(notes.get_unchecked(i)); - } + notes_array[i] = Option::some(notes.get_unchecked(i)); } notes_array diff --git a/noir-projects/aztec-nr/aztec/src/utils/array/assert_trimmed.nr b/noir-projects/aztec-nr/aztec/src/utils/array/assert_trimmed.nr deleted file mode 100644 index 784a51c4f3ba..000000000000 --- a/noir-projects/aztec-nr/aztec/src/utils/array/assert_trimmed.nr +++ /dev/null @@ -1,66 +0,0 @@ -/// Asserts that elements past `len()` in a `BoundedVec` are zeroed. -/// -/// Oracle functions may return `BoundedVec` values with dirty trailing storage (non-zero elements past `len()`). -/// This is problematic because `BoundedVec`'s `Eq` implementation and other operations assume trailing elements -/// are zeroed. -/// -/// This function should be called on any `BoundedVec` obtained from an oracle to guard against malformed data. -/// -/// TODO(https://github.com/noir-lang/noir/issues/4218): Remove once Noir natively validates `BoundedVec` returned -/// from unconstrained functions. -pub(crate) unconstrained fn assert_bounded_vec_trimmed(vec: BoundedVec) -where - T: Eq, -{ - let storage = vec.storage(); - let len = vec.len(); - for i in 0..MaxLen { - if i >= len { - assert_eq(storage[i], std::mem::zeroed(), "BoundedVec has non-zero trailing elements"); - } - } -} - -mod test { - use super::assert_bounded_vec_trimmed; - - #[test] - unconstrained fn trimmed_empty_vec() { - let vec: BoundedVec = BoundedVec::new(); - assert_bounded_vec_trimmed(vec); - } - - #[test] - unconstrained fn trimmed_full_vec() { - let vec = BoundedVec::::from_array([1, 2, 3]); - assert_bounded_vec_trimmed(vec); - } - - #[test] - unconstrained fn trimmed_partial_vec() { - let vec = BoundedVec::::from_array([1, 2, 3]); - assert_bounded_vec_trimmed(vec); - } - - #[test(should_fail_with = "BoundedVec has non-zero trailing elements")] - unconstrained fn dirty_trailing_element_fails() { - let mut vec = BoundedVec::::from_array([1]); - // We use the unchecked setter to write past the length, knowingly breaking the invariant. - vec.set_unchecked(1, 42); - assert_bounded_vec_trimmed(vec); - } - - #[test(should_fail_with = "BoundedVec has non-zero trailing elements")] - unconstrained fn dirty_last_element_fails() { - let mut vec = BoundedVec::::from_array([1, 2]); - vec.set_unchecked(2, 99); - assert_bounded_vec_trimmed(vec); - } - - #[test] - unconstrained fn trimmed_array_elements() { - // Test with array element type (like get_notes_oracle returns BoundedVec<[Field; N], MaxNotes>). - let vec = BoundedVec::<[Field; 2], 3>::from_array([[1, 2], [3, 4]]); - assert_bounded_vec_trimmed(vec); - } -} diff --git a/noir-projects/aztec-nr/aztec/src/utils/array/mod.nr b/noir-projects/aztec-nr/aztec/src/utils/array/mod.nr index 05a18cec68c7..52bf6c799cc0 100644 --- a/noir-projects/aztec-nr/aztec/src/utils/array/mod.nr +++ b/noir-projects/aztec-nr/aztec/src/utils/array/mod.nr @@ -1,11 +1,9 @@ pub mod append; -pub mod assert_trimmed; pub mod collapse; pub mod subarray; pub mod subbvec; pub use append::append; -pub(crate) use assert_trimmed::assert_bounded_vec_trimmed; pub use collapse::collapse; pub use subarray::subarray; pub use subbvec::subbvec;