Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed May 16, 2024
1 parent 456a982 commit 8c76c2e
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 49 deletions.
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dep::aztec::{
grumpkin_point::GrumpkinPoint, hash::poseidon2_hash
},
note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_consumption},
oracle::unsafe_rand::unsafe_rand, keys::getters::get_nsk_app, context::PrivateContext
oracle::unsafe_rand::unsafe_rand, oracle::nullifier_keys::get_nsk_app, context::PrivateContext
};

global ADDRESS_NOTE_LEN: Field = 3;
Expand Down
34 changes: 16 additions & 18 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::{
context::{inputs::PrivateContextInputs, interface::ContextInterface},
keys::getters::get_nullifier_keys, messaging::process_l1_to_l2_message,
messaging::process_l1_to_l2_message,
hash::{hash_args_array, ArgsHasher, compute_encrypted_log_hash, compute_unencrypted_log_hash},
note::{note_interface::NoteInterface, utils::compute_note_hash_for_insertion},
oracle::{
arguments, returns, call_private_function::call_private_function_internal, header::get_header_at,
nullifier_keys::get_nullifier_key_validation_request, arguments, returns,
call_private_function::call_private_function_internal, header::get_header_at,
logs::emit_encrypted_log, logs_traits::{LensForEncryptedLog, ToBytesForUnencryptedLog},
enqueue_public_function_call::{
enqueue_public_function_call_internal, set_public_teardown_function_call_internal,
Expand Down Expand Up @@ -70,7 +71,7 @@ struct PrivateContext {
encrypted_log_preimages_length: Field,
unencrypted_log_preimages_length: Field,

nullifier_key: Option<NullifierKeyValidationRequest>,
last_nullifier_key_validation_request: Option<NullifierKeyValidationRequest>,
}

impl ContextInterface for PrivateContext {
Expand Down Expand Up @@ -132,7 +133,7 @@ impl PrivateContext {
unencrypted_logs_hashes: BoundedVec::new(),
encrypted_log_preimages_length: 0,
unencrypted_log_preimages_length: 0,
nullifier_key: Option::none()
last_nullifier_key_validation_request: Option::none()
}
}

Expand Down Expand Up @@ -207,24 +208,21 @@ impl PrivateContext {
}

pub fn request_nsk_app(&mut self, npk_m_hash: Field) -> Field {
// A value of empty nullifier keys will fail the key validation request.
let cached_nullifier_keys = self.nullifier_key.unwrap_or(NullifierKeyValidationRequest::empty());
let cached_request = self.last_nullifier_key_validation_request.unwrap_or(NullifierKeyValidationRequest::empty());

let nullifier_keys = if cached_nullifier_keys.master_nullifier_public_key.hash() == npk_m_hash {
cached_nullifier_keys
let request = if cached_request.master_nullifier_public_key.hash() == npk_m_hash {
// We get a match so the cached request is the latest one
cached_request
} else {
let fetched_nullifier_keys = get_nullifier_keys(npk_m_hash);
let request = NullifierKeyValidationRequest {
master_nullifier_public_key: fetched_nullifier_keys.master_nullifier_public_key,
app_nullifier_secret_key: fetched_nullifier_keys.app_nullifier_secret_key
};
// We didn't get a match meaning the cached result is stale. We fetch new values from oracle and instruct
// protocol circuit to validate them by storing the validation request.
let request = get_nullifier_key_validation_request(npk_m_hash);
self.nullifier_key_validation_requests.push(request);
self.nullifier_key = Option::some(fetched_nullifier_keys);
fetched_nullifier_keys
self.last_nullifier_key_validation_request = Option::some(request);
request
};

assert_eq(nullifier_keys.master_nullifier_public_key.hash(), npk_m_hash);
nullifier_keys.app_nullifier_secret_key
request.app_nullifier_secret_key
}

// docs:start:context_message_portal
Expand Down Expand Up @@ -674,7 +672,7 @@ impl Empty for PrivateContext {
unencrypted_logs_hashes: BoundedVec::new(),
encrypted_log_preimages_length: 0,
unencrypted_log_preimages_length: 0,
nullifier_key: Option::none(),
last_nullifier_key_validation_request: Option::none(),
}
}
}
Expand Down
19 changes: 1 addition & 18 deletions noir-projects/aztec-nr/aztec/src/keys/getters.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ use dep::protocol_types::{
constants::CANONICAL_KEY_REGISTRY_ADDRESS, grumpkin_point::GrumpkinPoint
};
use crate::{
context::PrivateContext,
oracle::{
keys::get_public_keys_and_partial_address,
nullifier_keys::get_nullifier_keys as get_nullifier_keys_oracle
},
context::PrivateContext, oracle::{keys::get_public_keys_and_partial_address},
keys::public_keys::{PublicKeys, NULLIFIER_INDEX, INCOMING_INDEX},
state_vars::{
map::derive_storage_slot_in_map,
Expand Down Expand Up @@ -93,16 +89,3 @@ fn fetch_and_constrain_keys(address: AztecAddress) -> PublicKeys {

public_keys
}

// We get the full struct Nullifier Keys here
pub fn get_nullifier_keys(npk_m_hash: Field) -> NullifierKeyValidationRequest {
let nullifier_keys = get_nullifier_keys_oracle(npk_m_hash);
assert_eq(nullifier_keys.master_nullifier_public_key.hash(), npk_m_hash);

nullifier_keys
}

// We are only getting the app_nullifier_secret_key here
pub fn get_nsk_app(npk_m_hash: Field) -> Field {
get_nullifier_keys(npk_m_hash).app_nullifier_secret_key
}
14 changes: 9 additions & 5 deletions noir-projects/aztec-nr/aztec/src/oracle/nullifier_keys.nr
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use dep::protocol_types::{grumpkin_point::GrumpkinPoint, abis::nullifier_key_validation_request::NullifierKeyValidationRequest};

#[oracle(getNullifierKeys)]
fn get_nullifier_keys_oracle(_npk_m_hash: Field) -> [Field; 3] {}
fn get_nullifier_key_validation_request_oracle(_npk_m_hash: Field) -> [Field; 3] {}

unconstrained fn get_nullifier_keys_internal(npk_m_hash: Field) -> NullifierKeyValidationRequest {
let result = get_nullifier_keys_oracle(npk_m_hash);
unconstrained fn get_nullifier_key_validation_request_internal(npk_m_hash: Field) -> NullifierKeyValidationRequest {
let result = get_nullifier_key_validation_request_oracle(npk_m_hash);
NullifierKeyValidationRequest {
master_nullifier_public_key: GrumpkinPoint { x: result[0], y: result[1] },
app_nullifier_secret_key: result[2]
}
}

// We get the full struct Nullifier Keys here
pub fn get_nullifier_keys(npk_m_hash: Field) -> NullifierKeyValidationRequest {
get_nullifier_keys_internal(npk_m_hash)
pub fn get_nullifier_key_validation_request(npk_m_hash: Field) -> NullifierKeyValidationRequest {
get_nullifier_key_validation_request_internal(npk_m_hash)
}

pub fn get_nsk_app(npk_m_hash: Field) -> Field {
get_nullifier_key_validation_request_internal(npk_m_hash).app_nullifier_secret_key
}
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/value-note/src/value_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use dep::aztec::{
constants::GENERATOR_INDEX__NOTE_NULLIFIER, hash::poseidon2_hash
},
note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_consumption},
oracle::unsafe_rand::unsafe_rand, keys::getters::get_nsk_app, context::PrivateContext
oracle::unsafe_rand::unsafe_rand, oracle::nullifier_keys::get_nsk_app, context::PrivateContext
};

global VALUE_NOTE_LEN: Field = 3; // 3 plus a header.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dep::aztec::prelude::{AztecAddress, PrivateContext, NoteHeader, NoteInterfac
use dep::aztec::{
keys::getters::get_ivpk_m,
protocol_types::{constants::GENERATOR_INDEX__NOTE_NULLIFIER, grumpkin_point::GrumpkinPoint, hash::poseidon2_hash},
note::utils::compute_note_hash_for_consumption, keys::getters::get_nsk_app
note::utils::compute_note_hash_for_consumption, oracle::nullifier_keys::get_nsk_app
};

global SUBSCRIPTION_NOTE_LEN: Field = 3;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dep::aztec::prelude::{AztecAddress, NoteInterface, NoteHeader, PrivateContext};
use dep::aztec::{
keys::getters::get_ivpk_m, note::{utils::compute_note_hash_for_consumption},
keys::getters::get_nsk_app,
oracle::nullifier_keys::get_nsk_app,
protocol_types::{
traits::Empty, grumpkin_point::GrumpkinPoint, constants::GENERATOR_INDEX__NOTE_NULLIFIER,
hash::poseidon2_hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dep::aztec::prelude::{AztecAddress, FunctionSelector, NoteHeader, NoteInterf

use dep::aztec::{
keys::getters::get_ivpk_m, note::utils::compute_note_hash_for_consumption,
keys::getters::get_nsk_app,
oracle::nullifier_keys::get_nsk_app,
protocol_types::{constants::GENERATOR_INDEX__NOTE_NULLIFIER, grumpkin_point::GrumpkinPoint, hash::poseidon2_hash}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::aztec::prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext};
use dep::aztec::{
note::utils::compute_note_hash_for_consumption, keys::getters::get_nsk_app,
note::utils::compute_note_hash_for_consumption, oracle::nullifier_keys::get_nsk_app,
protocol_types::{constants::GENERATOR_INDEX__NOTE_NULLIFIER, grumpkin_point::GrumpkinPoint, hash::poseidon2_hash}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dep::aztec::{
prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext},
protocol_types::{constants::GENERATOR_INDEX__NOTE_NULLIFIER, grumpkin_point::GrumpkinPoint, hash::poseidon2_hash},
note::utils::compute_note_hash_for_consumption, oracle::unsafe_rand::unsafe_rand,
keys::getters::get_nsk_app
oracle::nullifier_keys::get_nsk_app
};

trait OwnedNote {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dep::aztec::{
prelude::{AztecAddress, NoteHeader, NoteInterface, PrivateContext},
protocol_types::{constants::GENERATOR_INDEX__NOTE_NULLIFIER, grumpkin_point::GrumpkinPoint, hash::poseidon2_hash},
note::utils::compute_note_hash_for_consumption, oracle::unsafe_rand::unsafe_rand,
keys::getters::get_nsk_app
oracle::nullifier_keys::get_nsk_app
};

trait OwnedNote {
Expand Down

0 comments on commit 8c76c2e

Please sign in to comment.