Skip to content

Commit

Permalink
full refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 19, 2023
1 parent 0ca4271 commit b41e1dc
Show file tree
Hide file tree
Showing 18 changed files with 150 additions and 154 deletions.
35 changes: 31 additions & 4 deletions yarn-project/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
use dep::aztec::note::note_interface::NoteInterface;
use dep::aztec::note::note_header::NoteHeader;
use dep::aztec::note::utils::compute_siloed_note_hash;
use dep::aztec::oracle::get_secret_key::get_secret_key;
use dep::aztec::{
note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_siloed_note_hash,
},
oracle::{
get_secret_key::get_secret_key,
get_public_key::get_public_key,
},
log::emit_encrypted_log,
context::PrivateContext,
};

global ADDRESS_NOTE_LEN: Field = 2;

Expand Down Expand Up @@ -39,6 +48,18 @@ impl AddressNote {
pub fn set_header(&mut self, header: NoteHeader) {
self.header = header;
}

// Broadcasts the note as an encrypted log on L1.
pub fn broadcast(self, context: &mut PrivateContext, slot: Field) {
let encryption_pub_key = get_public_key(self.owner);
emit_encrypted_log(
context,
(*context).this_address(),
slot,
encryption_pub_key,
self.serialize(),
);
}
}

fn deserialize(preimage: [Field; ADDRESS_NOTE_LEN]) -> AddressNote {
Expand Down Expand Up @@ -70,11 +91,17 @@ fn set_header(note: &mut AddressNote, header: NoteHeader) {
note.set_header(header);
}

// Broadcasts the note as an encrypted log on L1.
fn broadcast(context: &mut PrivateContext, slot: Field, note: AddressNote) {
note.broadcast(context, slot);
}

global AddressNoteMethods = NoteInterface {
deserialize,
serialize,
compute_note_hash,
compute_nullifier,
get_header,
set_header,
broadcast,
};
40 changes: 5 additions & 35 deletions yarn-project/aztec-nr/easy-private-state/src/easy_private_state.nr
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use dep::aztec::{
context::{PrivateContext, PublicContext, Context},
log::emit_encrypted_log,
context::Context,
note::note_getter_options::NoteGetterOptions,
oracle::get_public_key::get_public_key,
state_vars::set::Set,
types::point::Point,
};
use dep::std::option::Option;
use dep::value_note::{
filter::filter_notes_min_sum,
value_note::{ValueNote, ValueNoteMethods, VALUE_NOTE_LEN},
Expand Down Expand Up @@ -43,19 +39,8 @@ impl EasyPrivateUint {

// Insert the new note to the owner's set of notes.
// docs:start:insert
self.set.insert(&mut addend_note);
self.set.insert(&mut addend_note, true);
// docs:end:insert

// Emit the newly created encrypted note preimages via oracle calls.
let owner_key = get_public_key(owner);
let context = self.context.private.unwrap();
emit_encrypted_log(
context,
(*context).this_address(),
self.set.storage_slot,
owner_key,
addend_note.serialize(),
);
}

// Very similar to `value_note::utils::decrement`.
Expand Down Expand Up @@ -88,23 +73,8 @@ impl EasyPrivateUint {
// Creates change note for the owner.
let result_value = minuend - subtrahend;
let mut result_note = ValueNote::new(result_value as Field, owner);
self.set.insert(&mut result_note);

// Emit the newly created encrypted note preimages via oracle calls.
let mut encrypted_data = [0; VALUE_NOTE_LEN];
if result_value != 0 {
encrypted_data = result_note.serialize();
};

let owner_key = get_public_key(owner);

let context = self.context.private.unwrap();
emit_encrypted_log(
context,
(*context).this_address(),
self.set.storage_slot,
owner_key,
encrypted_data,
);
let broadcast = result_value != 0;
// We broadcast the note if it's value is non-zero.
self.set.insert(&mut result_note, broadcast);
}
}
5 changes: 0 additions & 5 deletions yarn-project/aztec-nr/value-note/src/utils.nr
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use dep::std::option::Option;
use dep::aztec::context::PrivateContext;
// docs:start:encrypted_import

use dep::aztec::log::emit_encrypted_log;

// docs:end:encrypted_import
use dep::aztec::note::note_getter_options::{NoteGetterOptions, SortOrder};
use dep::aztec::oracle::get_public_key::get_public_key;
use dep::aztec::state_vars::set::Set;
Expand Down
10 changes: 5 additions & 5 deletions yarn-project/aztec.js/src/artifacts/ecdsa_account_contract.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions yarn-project/boxes/blank-react/src/artifacts/Blank.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions yarn-project/boxes/blank/src/artifacts/Blank.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
use dep::aztec::note::note_interface::NoteInterface;
use dep::aztec::note::note_header::NoteHeader;
use dep::aztec::note::utils::compute_unique_siloed_note_hash;
use dep::aztec::oracle::get_secret_key::get_secret_key;
use dep::aztec::{
note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_unique_siloed_note_hash,
},
oracle::{
get_secret_key::get_secret_key,
get_public_key::get_public_key,
},
log::emit_encrypted_log,
context::PrivateContext,
};

global ECDSA_PUBLIC_KEY_NOTE_LEN: Field = 5;

Expand Down Expand Up @@ -64,6 +73,18 @@ impl EcdsaPublicKeyNote {
pub fn set_header(&mut self, header: NoteHeader) {
self.header = header;
}

// Broadcasts the note as an encrypted log on L1.
pub fn broadcast(self, context: &mut PrivateContext, slot: Field) {
let encryption_pub_key = get_public_key(self.owner);
emit_encrypted_log(
context,
(*context).this_address(),
slot,
encryption_pub_key,
self.serialize(),
);
}
}

fn deserialize(preimage: [Field; ECDSA_PUBLIC_KEY_NOTE_LEN]) -> EcdsaPublicKeyNote {
Expand Down Expand Up @@ -107,11 +128,17 @@ fn set_header(note: &mut EcdsaPublicKeyNote, header: NoteHeader) {
note.set_header(header);
}

// Broadcasts the note as an encrypted log on L1.
fn broadcast(context: &mut PrivateContext, slot: Field, note: EcdsaPublicKeyNote) {
note.broadcast(context, slot);
}

global EcdsaPublicKeyNoteInterface = NoteInterface {
deserialize,
serialize,
compute_note_hash,
compute_nullifier,
get_header,
set_header,
broadcast,
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ contract EcdsaAccount {
use dep::aztec::{
abi::CallContext,
context::{PrivateContext, PublicContext, Context},
log::emit_encrypted_log,
note::{
note_header::NoteHeader,
utils as note_utils,
Expand Down Expand Up @@ -48,15 +47,7 @@ contract EcdsaAccount {
) {
let this = context.this_address();
let mut pub_key_note = EcdsaPublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this);
storage.public_key.initialize(&mut pub_key_note, Option::none());

emit_encrypted_log(
&mut context,
this,
storage.public_key.storage_slot,
get_public_key(this),
pub_key_note.serialize(),
);
storage.public_key.initialize(&mut pub_key_note, Option::none(), true);
}

#[aztec(private)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ contract Escrow {

use dep::aztec::{
context::{PrivateContext, PublicContext, Context},
log::emit_encrypted_log,
note::{
note_getter_options::NoteGetterOptions,
note_header::NoteHeader,
Expand Down Expand Up @@ -45,14 +44,7 @@ contract Escrow {
let mut note = AddressNote::new(owner, this);

// Insert the owner into storage
storage.owners.insert(&mut note);
emit_encrypted_log(
&mut context,
this,
storage.owners.storage_slot,
get_public_key(this),
note.serialize(),
);
storage.owners.insert(&mut note, true);
}
// docs:end:constructor

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ contract PendingCommitments {
};
use dep::aztec::{
constants_gen::ARGS_LENGTH,
context::{PrivateContext, PublicContext, Context},
log::emit_encrypted_log,
context::Context,
note::{
note_getter::NoteGetterOptions,
note_header::NoteHeader,
utils as note_utils,
},
oracle::get_public_key::get_public_key,
state_vars::{map::Map, set::Set},
};

Expand Down Expand Up @@ -61,14 +59,7 @@ contract PendingCommitments {
let mut note = ValueNote::new(amount, owner);

// Insert note and emit encrypted note preimage via oracle call
owner_balance.insert(&mut note);
emit_encrypted_log(
&mut context,
context.inputs.call_context.storage_contract_address,
owner_balance.storage_slot,
get_public_key(owner),
note.serialize(),
);
owner_balance.insert(&mut note, true);

let options = NoteGetterOptions::with_filter(filter_notes_min_sum, amount);
// get note inserted above
Expand Down Expand Up @@ -102,14 +93,7 @@ contract PendingCommitments {

// Insert note and emit encrypted note preimage via oracle call
let mut note = ValueNote::new(amount, owner);
owner_balance.insert(&mut note);
emit_encrypted_log(
&mut context,
context.inputs.call_context.storage_contract_address,
owner_balance.storage_slot,
get_public_key(owner),
note.serialize(),
);
owner_balance.insert(&mut note, true);

0
}
Expand All @@ -133,14 +117,7 @@ contract PendingCommitments {
let mut note = ValueNote::new(amount, owner);

// Insert note and emit encrypted note preimage via oracle call
owner_balance.insert(&mut note);
emit_encrypted_log(
&mut context,
context.inputs.call_context.storage_contract_address,
owner_balance.storage_slot,
get_public_key(owner),
note.serialize(),
);
owner_balance.insert(&mut note, true);
}

// Nested/inner function to get a note and confirm it matches the expected value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ contract PokeableToken {
};
use dep::aztec::{
context::{PrivateContext, PublicContext, Context},
log::emit_encrypted_log,
note::{
note_getter::NoteGetterOptions,
note_header::{NoteHeader},
Expand Down Expand Up @@ -55,8 +54,8 @@ contract PokeableToken {
let mut sender_note = AddressNote::new(sender, sender);
let mut recipient_note = AddressNote::new(recipient, recipient);

storage.sender.initialize(&mut sender_note, Option::none());
storage.recipient.initialize(&mut recipient_note, Option::none());
storage.sender.initialize(&mut sender_note, Option::none(), false);
storage.recipient.initialize(&mut recipient_note, Option::none(), false);

// Insert new note to a set of user notes and emit the newly created encrypted note preimage via oracle call.
let sender_balance = storage.balances.at(sender);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ contract SchnorrAccount {
use dep::std::option::Option;

use dep::aztec::{
context::{PrivateContext, PublicContext, Context},
log::emit_encrypted_log,
context::{PrivateContext, Context},
note::{ note_header::NoteHeader, utils as note_utils },
oracle::get_public_key::get_public_key,
state_vars::immutable_singleton::ImmutableSingleton,
Expand Down Expand Up @@ -48,16 +47,8 @@ contract SchnorrAccount {
let this = context.this_address();
// docs:start:initialize
let mut pub_key_note = PublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this);
storage.signing_public_key.initialize(&mut pub_key_note, Option::none());
storage.signing_public_key.initialize(&mut pub_key_note, Option::none(), true);
// docs:end:initialize

emit_encrypted_log(
&mut context,
this,
storage.signing_public_key.storage_slot,
get_public_key(this),
pub_key_note.serialize(),
);
}

#[aztec(private)]
Expand Down
Loading

0 comments on commit b41e1dc

Please sign in to comment.