From 4dec60bd1b23a5f5e44615586d19bfc5a2593702 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 19 Oct 2023 09:49:40 +0000 Subject: [PATCH] WIP --- .../aztec-nr/aztec/src/note/lifecycle.nr | 35 +++++++++---------- .../src/state_vars/immutable_singleton.nr | 10 +++++- .../aztec-nr/aztec/src/state_vars/set.nr | 15 +++----- .../aztec/src/state_vars/singleton.nr | 22 +++++++++--- yarn-project/aztec-nr/value-note/src/utils.nr | 2 +- .../src/contracts/src/types/balance_set.nr | 2 +- .../contracts/card_game_contract/src/cards.nr | 2 +- .../token_contract/src/types/balance_set.nr | 2 +- 8 files changed, 50 insertions(+), 40 deletions(-) diff --git a/yarn-project/aztec-nr/aztec/src/note/lifecycle.nr b/yarn-project/aztec-nr/aztec/src/note/lifecycle.nr index 47b0db923c15..572398c138f3 100644 --- a/yarn-project/aztec-nr/aztec/src/note/lifecycle.nr +++ b/yarn-project/aztec-nr/aztec/src/note/lifecycle.nr @@ -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; @@ -21,24 +21,8 @@ pub fn create_note( storage_slot: Field, note: &mut Note, note_interface: NoteInterface, - 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( - context: &mut PrivateContext, - storage_slot: Field, - note: &mut Note, - note_interface: NoteInterface, + owner: Field, // TODO(benesjan): add owner() to NoteInterface and remove this arg? + broadcast: bool, ) { let contract_address = (*context).this_address(); @@ -52,6 +36,19 @@ pub fn create_note_no_broadcast( 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( diff --git a/yarn-project/aztec-nr/aztec/src/state_vars/immutable_singleton.nr b/yarn-project/aztec-nr/aztec/src/state_vars/immutable_singleton.nr index 0dad78df1c66..90fde1d5db3e 100644 --- a/yarn-project/aztec-nr/aztec/src/state_vars/immutable_singleton.nr +++ b/yarn-project/aztec-nr/aztec/src/state_vars/immutable_singleton.nr @@ -45,7 +45,13 @@ impl ImmutableSingleton { // docs:end:is_initialized // docs:start:initialize - pub fn initialize(self, note: &mut Note, owner: Option) { + pub fn initialize( + self, + note: &mut Note, + owner: Option, + owner2: Field, // TODO(benesjan): add owner() to NoteInterface and remove this arg? + broadcast: bool, + ) { let context = self.context.unwrap(); // Nullify the storage slot. @@ -58,6 +64,8 @@ impl ImmutableSingleton { self.storage_slot, note, self.note_interface, + owner2, + broadcast, ); } // docs:end:initialize diff --git a/yarn-project/aztec-nr/aztec/src/state_vars/set.nr b/yarn-project/aztec-nr/aztec/src/state_vars/set.nr index 812367eee82f..0223d39c0abd 100644 --- a/yarn-project/aztec-nr/aztec/src/state_vars/set.nr +++ b/yarn-project/aztec-nr/aztec/src/state_vars/set.nr @@ -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, @@ -39,7 +39,8 @@ impl Set { // 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(), @@ -47,19 +48,11 @@ impl Set { 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( diff --git a/yarn-project/aztec-nr/aztec/src/state_vars/singleton.nr b/yarn-project/aztec-nr/aztec/src/state_vars/singleton.nr index d1ce9e44593e..ca7760e30634 100644 --- a/yarn-project/aztec-nr/aztec/src/state_vars/singleton.nr +++ b/yarn-project/aztec-nr/aztec/src/state_vars/singleton.nr @@ -63,7 +63,13 @@ impl Singleton { // docs:end:is_initialized // docs:start:initialize - pub fn initialize(self, note: &mut Note, owner: Option) { + pub fn initialize( + self, + note: &mut Note, + owner: Option, + owner2: Field, // TODO(benesjan): add owner() to NoteInterface and remove this arg? + broadcast: bool, + ) { let context = self.context.unwrap(); // Nullify the storage slot. @@ -71,12 +77,17 @@ impl Singleton { 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); @@ -84,7 +95,7 @@ impl Singleton { 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 @@ -98,7 +109,8 @@ impl Singleton { // 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 } diff --git a/yarn-project/aztec-nr/value-note/src/utils.nr b/yarn-project/aztec-nr/value-note/src/utils.nr index 0d54f9961a0c..b939ac99e826 100644 --- a/yarn-project/aztec-nr/value-note/src/utils.nr +++ b/yarn-project/aztec-nr/value-note/src/utils.nr @@ -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 { diff --git a/yarn-project/boxes/token/src/contracts/src/types/balance_set.nr b/yarn-project/boxes/token/src/contracts/src/types/balance_set.nr index 79d63aaf0424..e821edd9a7ce 100644 --- a/yarn-project/boxes/token/src/contracts/src/types/balance_set.nr +++ b/yarn-project/boxes/token/src/contracts/src/types/balance_set.nr @@ -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 } diff --git a/yarn-project/noir-contracts/src/contracts/card_game_contract/src/cards.nr b/yarn-project/noir-contracts/src/contracts/card_game_contract/src/cards.nr index ffe3177484bc..b5aee05ad3fb 100644 --- a/yarn-project/noir-contracts/src/contracts/card_game_contract/src/cards.nr +++ b/yarn-project/noir-contracts/src/contracts/card_game_contract/src/cards.nr @@ -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); } diff --git a/yarn-project/noir-contracts/src/contracts/token_contract/src/types/balance_set.nr b/yarn-project/noir-contracts/src/contracts/token_contract/src/types/balance_set.nr index 79d63aaf0424..e821edd9a7ce 100644 --- a/yarn-project/noir-contracts/src/contracts/token_contract/src/types/balance_set.nr +++ b/yarn-project/noir-contracts/src/contracts/token_contract/src/types/balance_set.nr @@ -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 }