diff --git a/yarn-project/noir-libs/noir-aztec/src/state_vars/set.nr b/yarn-project/noir-libs/noir-aztec/src/state_vars/set.nr index b8813c989a31..5012ab86e7eb 100644 --- a/yarn-project/noir-libs/noir-aztec/src/state_vars/set.nr +++ b/yarn-project/noir-libs/noir-aztec/src/state_vars/set.nr @@ -1,15 +1,15 @@ +use dep::std::option::Option; use crate::abi::PublicContextInputs; use crate::constants_gen::{MAX_NOTES_PER_PAGE, MAX_READ_REQUESTS_PER_CALL}; use crate::context::{PrivateContext, PublicContext}; -use crate::note::lifecycle::{create_note, create_note_hash_from_public, destroy_note}; use crate::note::{ + lifecycle::{create_note, create_note_hash_from_public, destroy_note}, note_getter::{ensure_note_exists, ensure_note_hash_exists, get_notes, view_notes}, note_getter_options::NoteGetterOptions, note_interface::NoteInterface, note_viewer_options::NoteViewerOptions, - utils::compute_inner_note_hash, + utils::compute_note_hash_for_read_or_nullify, }; -use dep::std::option::Option; struct Set { private_context: Option<&mut PrivateContext>, @@ -62,7 +62,12 @@ impl Set { self.note_interface, &mut note_with_header, ); - self.remove(note_with_header); + destroy_note( + self.private_context.unwrap(), + self.storage_slot, + note_with_header, + self.note_interface, + ); } // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386): @@ -88,11 +93,20 @@ impl Set { // this hack once public kernel injects nonces. header.nonce = 1; set_header(&mut note_with_header, header); - - self.remove(note_with_header); + destroy_note( + self.private_context.unwrap(), + self.storage_slot, + note_with_header, + self.note_interface, + ); } fn remove(self, note: Note) { + let note_hash = compute_note_hash_for_read_or_nullify(self.note_interface, note); + let read_requests = self.private_context.unwrap_unchecked().read_requests; + let has_been_read = read_requests.any(|r| r == note_hash); + assert(has_been_read, "Can only remove a note that has been read from the set."); + destroy_note( self.private_context.unwrap(), self.storage_slot, diff --git a/yarn-project/noir-libs/noir-aztec/src/types/vec.nr b/yarn-project/noir-libs/noir-aztec/src/types/vec.nr index 4f0e1c95701b..9c2961b91b69 100644 --- a/yarn-project/noir-libs/noir-aztec/src/types/vec.nr +++ b/yarn-project/noir-libs/noir-aztec/src/types/vec.nr @@ -37,18 +37,46 @@ impl BoundedVec { self.len -= 1; elem } + + fn any(self, predicate: fn[Env](T) -> bool) -> bool { + let mut ret = false; + for i in 0..MaxLen { + if (i < self.len as u64) { + ret |= predicate(self.storage[i]); + } + } + ret + } +} + +#[test] +fn test_vec_push_pop() { + let mut vec: BoundedVec = BoundedVec::new(0); + assert(vec.len == 0); + vec.push(2); + assert(vec.len == 1); + vec.push(4); + assert(vec.len == 2); + vec.push(6); + assert(vec.len == 3); + let x = vec.pop(); + assert(vec.len == 2); + assert(x == 6); +} + +#[test(should_fail)] +fn test_vec_push_overflow() { + let mut vec: BoundedVec = BoundedVec::new(0); + vec.push(1); + vec.push(2); } -// #[test] -// fn test_vec() { -// let vec: BoundedVec = BoundedVec::new(0); -// assert(vec.len == 0); -// let vec1 = vec.push(1); -// assert(vec1.len == 1); -// let vec2 = vec1.push(1); -// assert(vec2.len == 2); -// let vec3 = vec2.push(1); -// assert(vec3.len == 3); -// let x = vec3.pop(); -// assert(x == 1); -// } \ No newline at end of file +#[test] +fn test_vec_any() { + let mut vec: BoundedVec = BoundedVec::new(0); + vec.push(0); + vec.push(1); + vec.push(2); + assert(vec.any(|v| v == 2) == true); + assert(vec.any(|v| v == 3) == false); +} \ No newline at end of file