From eee4be58632d8873697750328dd6820d66abb643 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 19 Oct 2023 09:53:30 +0000 Subject: [PATCH] WIP on cardgame refactor --- .../aztec-nr/aztec/src/note/lifecycle.nr | 23 +++++++++++++++++-- .../src/state_vars/immutable_singleton.nr | 10 +++++++- .../aztec-nr/aztec/src/state_vars/set.nr | 8 ++++++- .../aztec/src/state_vars/singleton.nr | 22 ++++++++++++++---- yarn-project/aztec-nr/value-note/src/utils.nr | 2 +- .../contracts/card_game_contract/src/cards.nr | 16 ++----------- 6 files changed, 57 insertions(+), 24 deletions(-) diff --git a/yarn-project/aztec-nr/aztec/src/note/lifecycle.nr b/yarn-project/aztec-nr/aztec/src/note/lifecycle.nr index bc15b4dec4a3..572398c138f3 100644 --- a/yarn-project/aztec-nr/aztec/src/note/lifecycle.nr +++ b/yarn-project/aztec-nr/aztec/src/note/lifecycle.nr @@ -9,14 +9,20 @@ use crate::note::{ note_interface::NoteInterface, utils::compute_inner_note_hash, }; -use crate::oracle::notes::{notify_created_note, notify_nullified_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; pub fn create_note( 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(); @@ -30,6 +36,19 @@ pub fn create_note( 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( @@ -65,7 +84,7 @@ pub fn destroy_note( // the nullifier corresponds to so they can be matched and both squashed/deleted. // nonzero nonce implies "persistable" nullifier (nullifies a persistent/in-tree // commitment) in which case `nullified_commitment` is not used since the kernel - // just siloes and forwards the nullier to its output. + // just siloes and forwards the nullifier to its output. if (header.is_transient) { // TODO(1718): Can we reuse the note commitment computed in `compute_nullifier`? nullified_commitment = compute_inner_note_hash(note_interface, note); 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 68fe16dd3b6f..0223d39c0abd 100644 --- a/yarn-project/aztec-nr/aztec/src/state_vars/set.nr +++ b/yarn-project/aztec-nr/aztec/src/state_vars/set.nr @@ -37,12 +37,18 @@ impl Set { // docs:end:new // docs:start:insert - pub fn insert(self, note: &mut Note) { + pub fn insert(self, + note: &mut Note, + 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 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/noir-contracts/src/contracts/card_game_contract/src/cards.nr b/yarn-project/noir-contracts/src/contracts/card_game_contract/src/cards.nr index 58005615eca2..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 @@ -1,16 +1,12 @@ use dep::aztec::{ context::{PrivateContext, PublicContext, Context}, constants_gen::{MAX_NOTES_PER_PAGE, MAX_READ_REQUESTS_PER_CALL}, - log::emit_encrypted_log, note::{ note_getter_options::NoteGetterOptions, note_viewer_options::NoteViewerOptions, note_getter::view_notes, }, - oracle::{ - get_public_key::get_public_key, - get_secret_key::get_secret_key, - }, + oracle::get_secret_key::get_secret_key, state_vars::set::Set, types::point::Point, }; @@ -133,20 +129,12 @@ impl Deck { } pub fn add_cards(&mut self, cards: [Card; N], owner: Field) -> [CardNote]{ - let owner_key = get_public_key(owner); let context = self.set.context.private.unwrap(); let mut inserted_cards = []; for card in cards { let mut card_note = CardNote::from_card(card, owner); - self.set.insert(&mut card_note.note); - emit_encrypted_log( - context, - (*context).this_address(), - self.set.storage_slot, - owner_key, - card_note.note.serialize(), - ); + self.set.insert(&mut card_note.note, owner, true); inserted_cards = inserted_cards.push_back(card_note); }