Skip to content

Commit

Permalink
WIP on cardgame refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 19, 2023
1 parent 7042bc6 commit eee4be5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 24 deletions.
23 changes: 21 additions & 2 deletions yarn-project/aztec-nr/aztec/src/note/lifecycle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 @@ -30,6 +36,19 @@ pub fn create_note<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 Expand Up @@ -65,7 +84,7 @@ pub fn destroy_note<Note, N>(
// 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);
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
8 changes: 7 additions & 1 deletion yarn-project/aztec-nr/aztec/src/state_vars/set.nr
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ impl<Note, N> Set<Note, N> {
// 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
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
@@ -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,
};
Expand Down Expand Up @@ -133,20 +129,12 @@ impl Deck {
}

pub fn add_cards<N>(&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);
}

Expand Down

0 comments on commit eee4be5

Please sign in to comment.