Skip to content

Commit

Permalink
Check note is read before removing from a set.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeilaWang committed Sep 7, 2023
1 parent 2c2d72b commit 730bc93
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 19 deletions.
26 changes: 20 additions & 6 deletions yarn-project/noir-libs/noir-aztec/src/state_vars/set.nr
Original file line number Diff line number Diff line change
@@ -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<Note, N> {
private_context: Option<&mut PrivateContext>,
Expand Down Expand Up @@ -62,7 +62,12 @@ impl<Note, N> Set<Note, N> {
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):
Expand All @@ -88,11 +93,20 @@ impl<Note, N> Set<Note, N> {
// 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,
Expand Down
54 changes: 41 additions & 13 deletions yarn-project/noir-libs/noir-aztec/src/types/vec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,46 @@ impl<T, MaxLen> BoundedVec<T, MaxLen> {
self.len -= 1;
elem
}

fn any<Env>(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<Field, 3> = 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<Field, 1> = BoundedVec::new(0);
vec.push(1);
vec.push(2);
}

// #[test]
// fn test_vec() {
// let vec: BoundedVec<Field, 2> = 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);
// }
#[test]
fn test_vec_any() {
let mut vec: BoundedVec<Field, 3> = 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);
}

0 comments on commit 730bc93

Please sign in to comment.