Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unconstrained context to key getters #7486

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
85 changes: 77 additions & 8 deletions noir-projects/aztec-nr/aztec/src/keys/getters.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ use dep::protocol_types::{
traits::is_empty
};
use crate::{
context::PrivateContext,
context::{PrivateContext, UnconstrainedContext},
oracle::{keys::get_public_keys_and_partial_address, key_validation_request::get_key_validation_request},
keys::{public_keys::PublicKeys, constants::{NULLIFIER_INDEX, INCOMING_INDEX, OUTGOING_INDEX, TAGGING_INDEX}},
state_vars::{shared_mutable::shared_mutable_private_getter::SharedMutablePrivateGetter}
state_vars::{shared_mutable::{shared_mutable::SharedMutable, shared_mutable_private_getter::SharedMutablePrivateGetter}}
};

global DELAY = 5;

// docs:start:key-getters
trait KeyGetters {
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;
}

impl KeyGetters for Header {
impl KeyGettersPrivate for Header {
fn get_npk_m(self, context: &mut PrivateContext, address: AztecAddress) -> Point {
get_master_key(context, address, NULLIFIER_INDEX, self)
}
Expand All @@ -44,6 +44,77 @@ impl KeyGetters for Header {
}
// docs:end:key-getters

trait KeyGettersUnconstrained {
fn get_npk_m(context: UnconstrainedContext, address: AztecAddress) -> Point;
fn get_ivpk_m(context: UnconstrainedContext, address: AztecAddress) -> Point;
fn get_ovpk_m(context: UnconstrainedContext, address: AztecAddress) -> Point;
fn get_tpk_m(context: UnconstrainedContext, address: AztecAddress) -> Point;
fn get_npk_m_hash(context: UnconstrainedContext, address: AztecAddress) -> Field;
}

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

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

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

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

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

fn get_master_key_unconstrained(
context: UnconstrainedContext,
address: AztecAddress,
key_index: Field,
) -> Point {
let key = fetch_key_from_registry_unconstrained(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 fetch_key_from_registry_unconstrained(
context: UnconstrainedContext,
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_current_value_in_unconstrained_other(
context,
AztecAddress::from_field(CANONICAL_KEY_REGISTRY_ADDRESS),
x_coordinate_derived_slot
);
let y_coordinate = SharedMutable::get_current_value_in_unconstrained_other(
context,
AztecAddress::from_field(CANONICAL_KEY_REGISTRY_ADDRESS),
y_coordinate_derived_slot
);

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

fn get_master_key(
context: &mut PrivateContext,
address: AztecAddress,
Expand Down Expand Up @@ -73,18 +144,16 @@ fn fetch_key_from_registry(
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_registry: SharedMutablePrivateGetter<Field, DELAY> = SharedMutablePrivateGetter::new(
let x_coordinate = SharedMutable::get_current_value_in_private_other::<Field, 5>(
context,
AztecAddress::from_field(CANONICAL_KEY_REGISTRY_ADDRESS),
x_coordinate_derived_slot
);
let y_coordinate_registry: SharedMutablePrivateGetter<Field, DELAY> = SharedMutablePrivateGetter::new(
let y_coordinate = SharedMutable::get_current_value_in_private_other::<Field, 5>(
context,
AztecAddress::from_field(CANONICAL_KEY_REGISTRY_ADDRESS),
y_coordinate_derived_slot
);
let x_coordinate = x_coordinate_registry.get_value_in_private(header);
let y_coordinate = y_coordinate_registry.get_value_in_private(header);

Point { x: x_coordinate, y: y_coordinate, is_infinite: false }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ contract PrivateToken {
note::utils::compute_inner_note_hash_from_preimage, hash::compute_secret_hash,
prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress},
protocol_types::{abis::function_selector::FunctionSelector, point::Point, hash::pedersen_hash},
oracle::unsafe_rand::unsafe_rand,
encrypted_logs::encrypted_note_emission::{encode_and_encrypt_note, encode_and_encrypt_note_with_keys}
oracle::unsafe_rand::unsafe_rand, encrypted_logs::encrypted_note_emission::{encode_and_encrypt_note,
encode_and_encrypt_note_with_keys}
};
use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}};
use crate::types::{token_note::{TokenNote, TOKEN_NOTE_LEN}, balances_map::BalancesMap};
Expand Down Expand Up @@ -149,6 +149,11 @@ contract PrivateToken {
storage.balances.to_unconstrained().balance_of(owner_npk_m_hash).to_integer()
}

unconstrained fn balance_of(owner: AztecAddress) -> pub Field {
let owner_npk_m_hash = context.get_npk_m_hash(owner);
storage.balances.balance_of(owner_npk_m_hash).to_integer()
}

unconstrained fn balance_of_unconstrained(owner_npk_m_hash: Field) -> pub Field {
storage.balances.balance_of(owner_npk_m_hash).to_integer()
}
Expand Down
Loading