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 7042bc6 commit 5816e56
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 104 deletions.
4 changes: 3 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@
"mktemp",
"unshielding",
"workdir",
"leveldb"
"leveldb",
"persistable",
"siloes"
],
"ignorePaths": [
"node_modules/",
Expand Down
26 changes: 24 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,36 @@ 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: 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>,
) {
let contract_address = (*context).this_address();

Expand Down Expand Up @@ -65,7 +87,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
17 changes: 15 additions & 2 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_hash_from_public, destroy_note},
lifecycle::{create_note, create_note_no_broadcast, create_note_hash_from_public, destroy_note},
note_getter::{get_notes, view_notes},
note_getter_options::NoteGetterOptions,
note_header::NoteHeader,
Expand Down Expand Up @@ -37,16 +37,29 @@ 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: add owner() to NoteInterface and remove this arg?
) {
create_note(
self.context.private.unwrap(),
self.storage_slot,
note,
self.note_interface,
owner,
);
}
// 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
18 changes: 1 addition & 17 deletions yarn-project/boxes/token/src/contracts/src/types/balance_set.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,13 @@ use dep::aztec::{
context::Context,
constants_gen::MAX_READ_REQUESTS_PER_CALL,
state_vars::set::Set,
log::emit_encrypted_log,
types::address::AztecAddress,
};
use dep::aztec::note::{
note_getter::view_notes,
note_getter_options::{NoteGetterOptions, SortOrder},
note_viewer_options::NoteViewerOptions
};
use dep::aztec::note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
};
use dep::aztec::oracle::{
rand::rand,
get_secret_key::get_secret_key,
get_public_key::get_public_key,
};

use crate::types::token_note::{TokenNote, TOKEN_NOTE_LEN, TokenNoteMethods};

Expand Down Expand Up @@ -77,13 +66,8 @@ impl BalanceSet {
let mut addend_note = TokenNote::new(addend, self.owner);

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

addend_note.emit_encrypted(
self.context.private.unwrap(),
self.set.storage_slot
);
}

pub fn sub(self: Self, subtrahend: SafeU120) {
Expand Down
25 changes: 0 additions & 25 deletions yarn-project/boxes/token/src/contracts/src/types/token_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,6 @@ impl TokenNote {
self.header = header;
}


pub fn emit_encrypted(
self: &mut Self,
context: &mut PrivateContext,
storage_slot: Field,
) {
// We only bother inserting the note if non-empty to save funds on gas.
if !self.amount.is_zero() {
// docs:start:encrypted
let application_contract_address = (*context).this_address();
let encryption_pub_key = get_public_key(self.owner.address);
let encrypted_data = (*self).serialize();

emit_encrypted_log(
context,
application_contract_address,
storage_slot,
encryption_pub_key,
encrypted_data,
);
// docs:end:encrypted
}
}
}

fn deserialize(preimage: [Field; TOKEN_NOTE_LEN]) -> TokenNote {
TokenNote::deserialize(preimage)
}
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);
inserted_cards = inserted_cards.push_back(card_note);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,13 @@ use dep::aztec::{
context::Context,
constants_gen::MAX_READ_REQUESTS_PER_CALL,
state_vars::set::Set,
log::emit_encrypted_log,
types::address::AztecAddress,
};
use dep::aztec::note::{
note_getter::view_notes,
note_getter_options::{NoteGetterOptions, SortOrder},
note_viewer_options::NoteViewerOptions
};
use dep::aztec::note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
};
use dep::aztec::oracle::{
rand::rand,
get_secret_key::get_secret_key,
get_public_key::get_public_key,
};

use crate::types::token_note::{TokenNote, TOKEN_NOTE_LEN, TokenNoteMethods};

Expand Down Expand Up @@ -77,13 +66,8 @@ impl BalanceSet {
let mut addend_note = TokenNote::new(addend, self.owner);

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

addend_note.emit_encrypted(
self.context.private.unwrap(),
self.set.storage_slot
);
}

pub fn sub(self: Self, subtrahend: SafeU120) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ use dep::aztec::{
context::PrivateContext,
constants_gen::MAX_READ_REQUESTS_PER_CALL,
state_vars::set::Set,
log::emit_encrypted_log
};
use dep::aztec::types::address::AztecAddress;
use dep::aztec::oracle::{
rand::rand,
get_secret_key::get_secret_key,
get_public_key::get_public_key,
};

use dep::safe_math::SafeU120;
Expand Down Expand Up @@ -83,30 +81,6 @@ impl TokenNote {
pub fn set_header(&mut self, header: NoteHeader) {
self.header = header;
}


pub fn emit_encrypted(
self: &mut Self,
context: &mut PrivateContext,
storage_slot: Field,
) {
// We only bother inserting the note if non-empty to save funds on gas.
if !self.amount.is_zero() {
// docs:start:encrypted
let application_contract_address = (*context).this_address();
let encryption_pub_key = get_public_key(self.owner.address);
let encrypted_data = (*self).serialize();

emit_encrypted_log(
context,
application_contract_address,
storage_slot,
encryption_pub_key,
encrypted_data,
);
// docs:end:encrypted
}
}
}

fn deserialize(preimage: [Field; TOKEN_NOTE_LEN]) -> TokenNote {
Expand Down

0 comments on commit 5816e56

Please sign in to comment.