Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ pub fn encode_and_encrypt_event<Event, NB, MB, OB>(
iv: AztecAddress
) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
| e: Event | {
let header = context.get_header();
let ovpk = header.get_ovpk_m(context, ov);
let ivpk = header.get_ivpk_m(context, iv);
let ovpk = context.get_ovpk_m(ov);
let ivpk = context.get_ivpk_m(iv);
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, compute);
}
Expand All @@ -63,9 +62,8 @@ pub fn encode_and_encrypt_event_unconstrained<Event, NB, MB, OB>(
iv: AztecAddress
) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
| e: Event | {
let header = context.get_header();
let ovpk = header.get_ovpk_m(context, ov);
let ivpk = header.get_ivpk_m(context, iv);
let ovpk = context.get_ovpk_m(ov);
let ivpk = context.get_ivpk_m(iv);
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, compute_unconstrained);
}
Expand All @@ -78,9 +76,8 @@ pub fn encode_and_encrypt_event_with_randomness<Event, NB, MB, OB>(
iv: AztecAddress
) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress, Field)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
| e: Event | {
let header = context.get_header();
let ovpk = header.get_ovpk_m(context, ov);
let ivpk = header.get_ivpk_m(context, iv);
let ovpk = context.get_ovpk_m(ov);
let ivpk = context.get_ivpk_m(iv);
emit_with_keys(context, randomness, e, ovpk, ivpk, compute);
}
}
Expand All @@ -92,9 +89,8 @@ pub fn encode_and_encrypt_event_with_randomness_unconstrained<Event, NB, MB, OB>
iv: AztecAddress
) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress, Field)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
| e: Event | {
let header = context.get_header();
let ovpk = header.get_ovpk_m(context, ov);
let ivpk = header.get_ivpk_m(context, iv);
let ovpk = context.get_ovpk_m(ov);
let ivpk = context.get_ivpk_m(iv);
emit_with_keys(context, randomness, e, ovpk, ivpk, compute_unconstrained);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ pub fn encode_and_encrypt_note<Note, N, NB, M>(
iv: AztecAddress
) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
| e: NoteEmission<Note> | {
let header = context.get_header();
let ovpk = header.get_ovpk_m(context, ov);
let ivpk = header.get_ivpk_m(context, iv);
let ovpk = context.get_ovpk_m(ov);
let ivpk = context.get_ivpk_m(iv);
emit_with_keys(context, e.note, ovpk, ivpk, compute);
}
}
Expand All @@ -77,9 +76,8 @@ pub fn encode_and_encrypt_note_unconstrained<Note, N, NB, M>(
iv: AztecAddress
) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
| e: NoteEmission<Note> | {
let header = context.get_header();
let ovpk = header.get_ovpk_m(context, ov);
let ivpk = header.get_ivpk_m(context, iv);
let ovpk = context.get_ovpk_m(ov);
let ivpk = context.get_ivpk_m(iv);
emit_with_keys(context, e.note, ovpk, ivpk, compute_unconstrained);
}
}
Expand Down
135 changes: 102 additions & 33 deletions noir-projects/aztec-nr/aztec/src/keys/getters.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,66 @@ global DELAY = 5;

// docs:start:key-getters
trait KeyGettersPrivate {
fn get_npk_m(header: Header, context: &mut PrivateContext, address: AztecAddress) -> Point;
fn get_ivpk_m(header: Header, context: &mut PrivateContext, address: AztecAddress) -> Point;
fn get_ovpk_m(header: Header, context: &mut PrivateContext, address: AztecAddress) -> Point;
fn get_tpk_m(header: Header, context: &mut PrivateContext, address: AztecAddress) -> Point;
fn get_npk_m_hash(header: Header, context: &mut PrivateContext, address: AztecAddress) -> Field;
fn get_npk_m(context: &mut PrivateContext, address: AztecAddress) -> Point;
fn get_ivpk_m(context: &mut PrivateContext, address: AztecAddress) -> Point;
fn get_ovpk_m(context: &mut PrivateContext, address: AztecAddress) -> Point;
fn get_tpk_m(context: &mut PrivateContext, address: AztecAddress) -> Point;
fn get_npk_m_hash(context: &mut PrivateContext, address: AztecAddress) -> Field;
}

impl KeyGettersPrivate for Header {
fn get_npk_m(self, context: &mut PrivateContext, address: AztecAddress) -> Point {
get_master_key(context, address, NULLIFIER_INDEX, self)
impl KeyGettersPrivate for PrivateContext {
fn get_npk_m(context: &mut PrivateContext, address: AztecAddress) -> Point {
get_master_key(context, NULLIFIER_INDEX, address)
}

fn get_ivpk_m(self, context: &mut PrivateContext, address: AztecAddress) -> Point {
get_master_key(context, address, INCOMING_INDEX, self)
fn get_ivpk_m(context: &mut PrivateContext, address: AztecAddress) -> Point {
get_master_key(context, INCOMING_INDEX, address)
}

fn get_ovpk_m(self, context: &mut PrivateContext, address: AztecAddress) -> Point {
get_master_key(context, address, OUTGOING_INDEX, self)
fn get_ovpk_m(context: &mut PrivateContext, address: AztecAddress) -> Point {
get_master_key(context, OUTGOING_INDEX, address)
}

fn get_tpk_m(self, context: &mut PrivateContext, address: AztecAddress) -> Point {
get_master_key(context, address, TAGGING_INDEX, self)
fn get_tpk_m(context: &mut PrivateContext, address: AztecAddress) -> Point {
get_master_key(context, TAGGING_INDEX, address)
}

fn get_npk_m_hash(self, context: &mut PrivateContext, address: AztecAddress) -> Field {
get_master_key(context, address, NULLIFIER_INDEX, self).hash()
fn get_npk_m_hash(context: &mut PrivateContext, address: AztecAddress) -> Field {
get_master_key(context, NULLIFIER_INDEX, address).hash()
}
}
// docs:end:key-getters

trait KeyGettersPrivateHistorical {
fn get_npk_m_historical(header: Header, address: AztecAddress) -> Point;
fn get_ivpk_m_historical(header: Header, address: AztecAddress) -> Point;
fn get_ovpk_m_historical(header: Header, address: AztecAddress) -> Point;
fn get_tpk_m_historical(header: Header, address: AztecAddress) -> Point;
fn get_npk_m_hash_historical(header: Header, address: AztecAddress) -> Field;
}

impl KeyGettersPrivateHistorical for Header {
fn get_npk_m_historical(header: Header, address: AztecAddress) -> Point {
get_master_key_historical(header, NULLIFIER_INDEX, address)
}

fn get_ivpk_m_historical(header: Header, address: AztecAddress) -> Point {
get_master_key_historical(header, INCOMING_INDEX, address)
}

fn get_ovpk_m_historical(header: Header, address: AztecAddress) -> Point {
get_master_key_historical(header, OUTGOING_INDEX, address)
}

fn get_tpk_m_historical(header: Header, address: AztecAddress) -> Point {
get_master_key_historical(header, TAGGING_INDEX, address)
}

fn get_npk_m_hash_historical(header: Header, address: AztecAddress) -> Field {
get_master_key_historical(header, NULLIFIER_INDEX, address).hash()
}
}

trait KeyGettersUnconstrained {
fn get_npk_m(context: UnconstrainedContext, address: AztecAddress) -> Point;
fn get_ivpk_m(context: UnconstrainedContext, address: AztecAddress) -> Point;
Expand All @@ -53,31 +83,31 @@ trait KeyGettersUnconstrained {
}

impl KeyGettersUnconstrained for UnconstrainedContext {
fn get_npk_m(self, address: AztecAddress) -> Point {
get_master_key_unconstrained(self, address, NULLIFIER_INDEX)
fn get_npk_m(context: UnconstrainedContext, address: AztecAddress) -> Point {
get_master_key_unconstrained(context, NULLIFIER_INDEX, address)
}

fn get_ivpk_m(self, address: AztecAddress) -> Point {
get_master_key_unconstrained(self, address, INCOMING_INDEX)
fn get_ivpk_m(context: UnconstrainedContext, address: AztecAddress) -> Point {
get_master_key_unconstrained(context, INCOMING_INDEX, address)
}

fn get_ovpk_m(self, address: AztecAddress) -> Point {
get_master_key_unconstrained(self, address, OUTGOING_INDEX)
fn get_ovpk_m(context: UnconstrainedContext, address: AztecAddress) -> Point {
get_master_key_unconstrained(context, OUTGOING_INDEX, address)
}

fn get_tpk_m(self, address: AztecAddress) -> Point {
get_master_key_unconstrained(self, address, TAGGING_INDEX)
fn get_tpk_m(context: UnconstrainedContext, address: AztecAddress) -> Point {
get_master_key_unconstrained(context, TAGGING_INDEX, address)
}

fn get_npk_m_hash(self, address: AztecAddress) -> Field {
get_master_key_unconstrained(self, address, NULLIFIER_INDEX).hash()
fn get_npk_m_hash(context: UnconstrainedContext, address: AztecAddress) -> Field {
get_master_key_unconstrained(context, NULLIFIER_INDEX, address).hash()
}
}

fn get_master_key_unconstrained(
context: UnconstrainedContext,
address: AztecAddress,
key_index: Field,
address: AztecAddress
) -> Point {
let key = fetch_key_from_registry_unconstrained(context, key_index, address);
if is_empty(key) {
Expand All @@ -94,7 +124,7 @@ fn get_master_key_unconstrained(
fn fetch_key_from_registry_unconstrained(
context: UnconstrainedContext,
key_index: Field,
address: AztecAddress,
address: AztecAddress
) -> Point {
let x_coordinate_map_slot = key_index * 2 + 1;
let y_coordinate_map_slot = x_coordinate_map_slot + 1;
Expand All @@ -117,11 +147,27 @@ fn fetch_key_from_registry_unconstrained(

fn get_master_key(
context: &mut PrivateContext,
address: AztecAddress,
key_index: Field,
header: Header
address: AztecAddress
) -> Point {
let key = fetch_key_from_registry(context, key_index, address, header);
let key = fetch_key_from_registry(context, key_index, address);
if is_empty(key) {
// Keys were not registered in registry yet --> fetch key from PXE
let keys = fetch_and_constrain_keys(address);
// Return the corresponding to index
keys.get_key_by_index(key_index)
} else {
// Keys were registered --> return the key
key
}
}

fn get_master_key_historical(
header: Header,
key_index: Field,
address: AztecAddress
) -> Point {
let key = fetch_key_from_registry_historical(header, key_index, address);
if is_empty(key) {
// Keys were not registered in registry yet --> fetch key from PXE
let keys = fetch_and_constrain_keys(address);
Expand All @@ -136,8 +182,7 @@ fn get_master_key(
fn fetch_key_from_registry(
context: &mut PrivateContext,
key_index: Field,
address: AztecAddress,
header: Header
address: AztecAddress
) -> Point {
let x_coordinate_map_slot = key_index * 2 + 1;
let y_coordinate_map_slot = x_coordinate_map_slot + 1;
Expand All @@ -158,6 +203,30 @@ fn fetch_key_from_registry(
Point { x: x_coordinate, y: y_coordinate, is_infinite: false }
}

fn fetch_key_from_registry_historical(
header: Header,
key_index: Field,
address: AztecAddress
) -> Point {
let x_coordinate_map_slot = key_index * 2 + 1;
let y_coordinate_map_slot = x_coordinate_map_slot + 1;
let x_coordinate_derived_slot = derive_storage_slot_in_map(x_coordinate_map_slot, address);
let y_coordinate_derived_slot = derive_storage_slot_in_map(y_coordinate_map_slot, address);

let x_coordinate = SharedMutable::get_historical_value_in_private_other::<Field, 5>(
header,
AztecAddress::from_field(CANONICAL_KEY_REGISTRY_ADDRESS),
x_coordinate_derived_slot
);
let y_coordinate = SharedMutable::get_historical_value_in_private_other::<Field, 5>(
header,
AztecAddress::from_field(CANONICAL_KEY_REGISTRY_ADDRESS),
y_coordinate_derived_slot
);

Point { x: x_coordinate, y: y_coordinate, is_infinite: false }
}

// Passes only when keys were not rotated - is expected to be called only when keys were not registered yet
fn fetch_and_constrain_keys(address: AztecAddress) -> PublicKeys {
let (public_keys, partial_address) = get_public_keys_and_partial_address(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,22 @@ impl<T, INITIAL_DELAY> SharedMutable<T, INITIAL_DELAY, &mut PrivateContext> {
context.set_tx_max_block_number(block_horizon);
value_change.get_current_at(historical_block_number)
}

pub fn get_historical_value_in_private_other<T_OTHER, INITIAL_DELAY_OTHER>(
header: Header,
address: AztecAddress,
storage_slot: Field
) -> T_OTHER where T_OTHER: FromField + ToField + Eq {
// When reading the current value in private we construct a historical state proof for the public value.
// However, since this value might change, we must constrain the maximum transaction block number as this proof
// will only be valid for however many blocks we can ensure the value will not change, which will depend on the
// current delay and any scheduled delay changes.
let dummy: SharedMutable<T_OTHER, INITIAL_DELAY_OTHER, ()> = SharedMutable::new((), storage_slot);

let (value_change, _, historical_block_number): (ScheduledValueChange<T_OTHER>, ScheduledDelayChange<INITIAL_DELAY_OTHER>, u32) = dummy.historical_read_from_public_storage(header, address);

value_change.get_current_at(historical_block_number)
}
}

impl<T, INITIAL_DELAY> SharedMutable<T, INITIAL_DELAY, UnconstrainedContext> where T: ToField + FromField + Eq {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ impl<Context> EasyPrivateUint<Context> {
impl<Context> EasyPrivateUint<&mut PrivateContext> {
// Very similar to `value_note::utils::increment`.
pub fn add(self, addend: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
let header = self.context.get_header();
let owner_npk_m_hash = header.get_npk_m_hash(self.context, owner);
let owner_npk_m_hash = self.context.get_npk_m_hash(owner);
// Creates new note for the owner.
let mut addend_note = ValueNote::new(addend as Field, owner_npk_m_hash);

Expand All @@ -36,8 +35,7 @@ impl<Context> EasyPrivateUint<&mut PrivateContext> {

// Very similar to `value_note::utils::decrement`.
pub fn sub(self, subtrahend: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
let header = self.context.get_header();
let owner_npk_m_hash = header.get_npk_m_hash(self.context, owner);
let owner_npk_m_hash = self.context.get_npk_m_hash(owner);

// docs:start:get_notes
let options = NoteGetterOptions::with_filter(filter_notes_min_sum, subtrahend as Field);
Expand Down
3 changes: 1 addition & 2 deletions noir-projects/aztec-nr/value-note/src/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ pub fn increment(
recipient: AztecAddress,
outgoing_viewer: AztecAddress
) {
let header = balance.context.get_header();
let recipient_npk_m_hash = header.get_npk_m_hash(balance.context, recipient);
let recipient_npk_m_hash = balance.context.get_npk_m_hash(recipient);

let mut note = ValueNote::new(amount, recipient_npk_m_hash);
// Insert the new note to the owner's set of notes and emit the log if value is non-zero.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ contract AppSubscription {

// Assert that the given expiry_block_number < current_block_number + SUBSCRIPTION_DURATION_IN_BLOCKS.
AppSubscription::at(context.this_address()).assert_block_number(expiry_block_number).enqueue_view(&mut context);
let header = context.get_header();
let subscriber_npk_m_hash = header.get_npk_m_hash(&mut context, subscriber_address);
let subscriber_npk_m_hash = context.get_npk_m_hash(subscriber_address);

let mut subscription_note = SubscriptionNote::new(subscriber_npk_m_hash, expiry_block_number, tx_count);
storage.subscriptions.at(subscriber_address).initialize_or_replace(&mut subscription_note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), subscriber_address));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ impl<Context> Deck<Context> {

impl Deck<&mut PrivateContext> {
pub fn add_cards<N>(&mut self, cards: [Card; N], owner: AztecAddress) -> [CardNote] {
let header = self.set.context.get_header();
let owner_npk_m_hash = header.get_npk_m_hash(self.set.context, owner);
let msg_sender_ovpk_m = header.get_ovpk_m(self.set.context, self.set.context.msg_sender());
let owner_ivpk_m = header.get_ivpk_m(self.set.context, owner);
let context = self.set.context;
let owner_npk_m_hash = context.get_npk_m_hash(owner);
let msg_sender_ovpk_m = context.get_ovpk_m(context.msg_sender());
let owner_ivpk_m = context.get_ivpk_m(owner);

let mut inserted_cards = &[];
for card in cards {
Expand Down Expand Up @@ -182,8 +182,7 @@ pub fn get_pack_cards(
owner: AztecAddress,
context: &mut PrivateContext
) -> [Card; PACK_CARDS] {
let header = context.get_header();
let owner_npk_m_hash = header.get_npk_m_hash(context, owner);
let owner_npk_m_hash = context.get_npk_m_hash(owner);

// generate pseudo randomness deterministically from 'seed' and user secret
let secret = context.request_nsk_app(owner_npk_m_hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ contract Child {

#[aztec(private)]
fn private_set_value(new_value: Field, owner: AztecAddress) -> Field {
let header = context.get_header();
let owner_npk_m_hash = header.get_npk_m_hash(&mut context, owner);
let owner_npk_m_hash = context.get_npk_m_hash(owner);

let mut note = ValueNote::new(new_value, owner_npk_m_hash);
storage.a_map_with_private_values.at(owner).insert(&mut note).emit(encode_and_encrypt_note(&mut context, owner, owner));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ contract Crowdfunding {
Token::at(storage.donation_token.read_private()).transfer_from(donor, context.this_address(), amount as Field, 0).call(&mut context);
// docs:end:do-transfer

let header = context.get_header();
// 3) Create a value note for the donor so that he can later on claim a rewards token in the Claim
// contract by proving that the hash of this note exists in the note hash tree.
let donor_npk_m_hash = header.get_npk_m_hash(&mut context, donor);
let donor_npk_m_hash = context.get_npk_m_hash(donor);
let mut note = ValueNote::new(amount as Field, donor_npk_m_hash);
storage.donation_receipts.insert(&mut note).emit(encode_and_encrypt_note(&mut context, donor, donor));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ contract DelegatedOn {

#[aztec(private)]
fn private_set_value(new_value: Field, owner: AztecAddress) -> Field {
let header = context.get_header();
let owner_npk_m_hash = header.get_npk_m_hash(&mut context, owner);
let owner_npk_m_hash = context.get_npk_m_hash(owner);

let mut note = ValueNote::new(new_value, owner_npk_m_hash);
storage.a_map_with_private_values.at(owner).insert(&mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), owner));
Expand Down
Loading