Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 19, 2023
1 parent 5816e56 commit 4dec60b
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 40 deletions.
35 changes: 16 additions & 19 deletions yarn-project/aztec-nr/aztec/src/note/lifecycle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::note::{
use crate::oracle::{
notes::{notify_created_note, notify_nullified_note},
get_public_key::get_public_key,
}
};
use crate::constants_gen::EMPTY_NULLIFIED_COMMITMENT;
use crate::log::emit_encrypted_log;

Expand All @@ -21,24 +21,8 @@ pub fn create_note<Note, N>(
storage_slot: Field,
note: &mut Note,
note_interface: NoteInterface<Note, N>,
owner: Field, // TODO: add owner() to NoteInterface and remove this arg?
) {
create_note_no_broadcast(context, storage_slot, note, note_interface);
let owner_key = get_public_key(owner);
emit_encrypted_log(
context,
(*context).this_address(),
storage_slot,
owner_key,
note.serialize(),
);
}

pub fn create_note_no_broadcast<Note, N>(
context: &mut PrivateContext,
storage_slot: Field,
note: &mut Note,
note_interface: NoteInterface<Note, N>,
owner: Field, // TODO(benesjan): add owner() to NoteInterface and remove this arg?
broadcast: bool,
) {
let contract_address = (*context).this_address();

Expand All @@ -52,6 +36,19 @@ pub fn create_note_no_broadcast<Note, N>(
assert(notify_created_note(storage_slot, preimage, inner_note_hash) == 0);

context.push_new_note_hash(inner_note_hash);

let serialize = note_interface.serialize;

if broadcast {
let owner_key = get_public_key(owner);
emit_encrypted_log(
context,
(*context).this_address(),
storage_slot,
owner_key,
serialize(*note),
);
}
}

pub fn create_note_hash_from_public<Note, N>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ impl<Note, N> ImmutableSingleton<Note, N> {
// docs:end:is_initialized

// docs:start:initialize
pub fn initialize(self, note: &mut Note, owner: Option<Field>) {
pub fn initialize(
self,
note: &mut Note,
owner: Option<Field>,
owner2: Field, // TODO(benesjan): add owner() to NoteInterface and remove this arg?
broadcast: bool,
) {
let context = self.context.unwrap();

// Nullify the storage slot.
Expand All @@ -58,6 +64,8 @@ impl<Note, N> ImmutableSingleton<Note, N> {
self.storage_slot,
note,
self.note_interface,
owner2,
broadcast,
);
}
// docs:end:initialize
Expand Down
15 changes: 4 additions & 11 deletions yarn-project/aztec-nr/aztec/src/state_vars/set.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::abi::PublicContextInputs;
use crate::constants_gen::{MAX_NOTES_PER_PAGE, MAX_READ_REQUESTS_PER_CALL};
use crate::context::{PrivateContext, PublicContext, Context};
use crate::note::{
lifecycle::{create_note, create_note_no_broadcast, create_note_hash_from_public, destroy_note},
lifecycle::{create_note, create_note_hash_from_public, destroy_note},
note_getter::{get_notes, view_notes},
note_getter_options::NoteGetterOptions,
note_header::NoteHeader,
Expand Down Expand Up @@ -39,27 +39,20 @@ impl<Note, N> Set<Note, N> {
// docs:start:insert
pub fn insert(self,
note: &mut Note,
owner: Field, // TODO: add owner() to NoteInterface and remove this arg?
owner: Field, // TODO(benesjan): add owner() to NoteInterface and remove this arg?
broadcast: bool,
) {
create_note(
self.context.private.unwrap(),
self.storage_slot,
note,
self.note_interface,
owner,
broadcast,
);
}
// docs:end:insert

pub fn insert_no_broadcast(self, note: &mut Note) {
create_note_no_broadcast(
self.context.private.unwrap(),
self.storage_slot,
note,
self.note_interface,
);
}

// docs:start:insert_from_public
pub fn insert_from_public(self, note: &mut Note) {
create_note_hash_from_public(
Expand Down
22 changes: 17 additions & 5 deletions yarn-project/aztec-nr/aztec/src/state_vars/singleton.nr
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,39 @@ impl<Note, N> Singleton<Note, N> {
// docs:end:is_initialized

// docs:start:initialize
pub fn initialize(self, note: &mut Note, owner: Option<Field>) {
pub fn initialize(
self,
note: &mut Note,
owner: Option<Field>,
owner2: Field, // TODO(benesjan): add owner() to NoteInterface and remove this arg?
broadcast: bool,
) {
let context = self.context.unwrap();

// Nullify the storage slot.
let compute_initialization_nullifier = self.compute_initialization_nullifier;
let nullifier = compute_initialization_nullifier(self.storage_slot, owner);
context.push_new_nullifier(nullifier, EMPTY_NULLIFIED_COMMITMENT);

create_note(context, self.storage_slot, note, self.note_interface);
create_note(context, self.storage_slot, note, self.note_interface, owner2, broadcast);
}
// docs:end:initialize

// docs:start:replace
pub fn replace(self, new_note: &mut Note) {
pub fn replace(
self,
new_note: &mut Note,
owner: Field, // TODO(benesjan): add owner() to NoteInterface and remove this arg?
broadcast: bool,
) {
let context = self.context.unwrap();
let prev_note = get_note(context, self.storage_slot, self.note_interface);

// Nullify previous note.
destroy_note(context, prev_note, self.note_interface);

// Add replacement note.
create_note(context, self.storage_slot, new_note, self.note_interface);
create_note(context, self.storage_slot, new_note, self.note_interface, owner, broadcast);
}
// docs:end:replace

Expand All @@ -98,7 +109,8 @@ impl<Note, N> Singleton<Note, N> {

// Add the same note again.
// Because a nonce is added to every note in the kernel, its nullifier will be different.
create_note(context, self.storage_slot, &mut note, self.note_interface);
// TODO(benesjan): add owner() to NoteInterface and remove the 0? Note: why don't we need to emit
create_note(context, self.storage_slot, &mut note, self.note_interface, 0, false);

note
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/value-note/src/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn create_note(
note: &mut ValueNote,
) {
// Insert the new note to the owner's set of notes.
balance.insert(note);
balance.insert(note, owner, false);

// Remove this if statement if we can wrap this create_note function in an if statement.
if note.value != 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl BalanceSet {
let mut addend_note = TokenNote::new(addend, self.owner);

// docs:start:insert
self.set.insert(&mut addend_note, self.owner);
self.set.insert(&mut addend_note, self.owner, true);
// docs:end:insert
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Deck {
let mut inserted_cards = [];
for card in cards {
let mut card_note = CardNote::from_card(card, owner);
self.set.insert(&mut card_note.note, owner);
self.set.insert(&mut card_note.note, owner, true);
inserted_cards = inserted_cards.push_back(card_note);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl BalanceSet {
let mut addend_note = TokenNote::new(addend, self.owner);

// docs:start:insert
self.set.insert(&mut addend_note, self.owner);
self.set.insert(&mut addend_note, self.owner, true);
// docs:end:insert
}

Expand Down

0 comments on commit 4dec60b

Please sign in to comment.