Skip to content

Commit

Permalink
refactor: update nullifier derivation verification
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Apr 12, 2024
1 parent a2cf4c8 commit c8621c2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
4 changes: 2 additions & 2 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ library Constants {
uint256 internal constant L1_TO_L2_MESSAGE_LENGTH = 6;
uint256 internal constant L2_TO_L1_MESSAGE_LENGTH = 2;
uint256 internal constant MAX_BLOCK_NUMBER_LENGTH = 2;
uint256 internal constant NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH = 4;
uint256 internal constant NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH = 5;
uint256 internal constant NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH = 3;
uint256 internal constant NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH = 4;
uint256 internal constant PARTIAL_STATE_REFERENCE_LENGTH = 6;
uint256 internal constant PRIVATE_CALL_STACK_ITEM_LENGTH = 221;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 218;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct PrivateValidationRequestProcessor {
pending_note_hashes: [SideEffect; MAX_NEW_NOTE_HASHES_PER_TX],
nullifier_read_request_hints: NullifierReadRequestHints,
pending_nullifiers: [SideEffectLinkedToNoteHash; MAX_NEW_NULLIFIERS_PER_TX],
master_nullifier_secret_keys: [GrumpkinPrivateKey; MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_TX],
master_nullifier_secret_key: GrumpkinPrivateKey,
nullifier_tree_root: Field
}

Expand Down Expand Up @@ -61,12 +61,17 @@ impl PrivateValidationRequestProcessor {
for i in 0..MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_TX {
let request = requests[i];
if !is_empty(request) {
let master_secret_key = self.master_nullifier_secret_keys[i];
let computed_public_key = master_secret_key.derive_public_key();
// First we check that derived public key matches master nullifier public key
let derived_nullifier_public_key = self.master_nullifier_secret_key.derive_public_key();
assert(
computed_public_key.eq(request.public_key), "Cannot derive nullifier public key from the master key."
derived_nullifier_public_key.eq(request.master_nullifier_public_key), "Cannot derive master nullifier public key from the secret key."
);

// Then we check that siloing the master secret key with the contract address gives the app secret key


let application_nullifier_secret_key_preimage = [request.contract_address, self.master_nullifier_secret_key];
dep::std::hash::poseidon2::Poseidon2::hash(application_nullifier_secret_key_preimage, 3);
let computed_secret_key = compute_siloed_nullifier_secret_key(master_secret_key, request.contract_address);
assert(
computed_secret_key.eq(request.secret_key), "Cannot derive siloed secret key from the master key."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,70 @@ use crate::{
};

struct NullifierKeyValidationRequest {
public_key: GrumpkinPoint,
secret_key: GrumpkinPrivateKey,
master_nullifier_public_key: GrumpkinPoint,
application_nullifier_secret_key: Field, // not a GrumpkinScalar because it's output of poseidon2
}

impl Eq for NullifierKeyValidationRequest {
fn eq(self, request: NullifierKeyValidationRequest) -> bool {
(request.public_key.eq(self.public_key))
& (request.secret_key.eq(self.secret_key))
(request.master_nullifier_public_key.eq(self.master_nullifier_public_key))
& (request.application_nullifier_secret_key.eq(self.application_nullifier_secret_key))
}
}

impl Empty for NullifierKeyValidationRequest {
fn empty() -> Self {
NullifierKeyValidationRequest {
public_key: GrumpkinPoint::zero(),
secret_key: GrumpkinPrivateKey::zero(),
master_nullifier_public_key: GrumpkinPoint::zero(),
application_nullifier_secret_key: 0,
}
}
}

impl Serialize<NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH> for NullifierKeyValidationRequest {
fn serialize(self) -> [Field; NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH] {
[
self.public_key.x,
self.public_key.y,
self.secret_key.high,
self.secret_key.low,
self.master_nullifier_public_key.x,
self.master_nullifier_public_key.y,
self.application_nullifier_secret_key,
]
}
}

impl Deserialize<NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH> for NullifierKeyValidationRequest {
fn deserialize(fields: [Field; NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH]) -> Self {
Self {
public_key: GrumpkinPoint::new(fields[0], fields[1]),
secret_key: GrumpkinPrivateKey::new(fields[2], fields[3]),
master_nullifier_public_key: GrumpkinPoint::new(fields[0], fields[1]),
application_nullifier_secret_key: fields[2],
}
}
}

impl NullifierKeyValidationRequest {
pub fn to_context(self, contract_address: AztecAddress) -> NullifierKeyValidationRequestContext {
NullifierKeyValidationRequestContext { public_key: self.public_key, secret_key: self.secret_key, contract_address }
NullifierKeyValidationRequestContext { master_nullifier_public_key: self.master_nullifier_public_key, application_nullifier_secret_key: self.application_nullifier_secret_key, contract_address }
}
}

struct NullifierKeyValidationRequestContext {
public_key: GrumpkinPoint,
secret_key: GrumpkinPrivateKey,
master_nullifier_public_key: GrumpkinPoint,
application_nullifier_secret_key: Field,
contract_address: AztecAddress,
}

impl Eq for NullifierKeyValidationRequestContext {
fn eq(self, request: NullifierKeyValidationRequestContext) -> bool {
(request.public_key.eq(self.public_key))
& (request.secret_key.eq(self.secret_key))
(request.master_nullifier_public_key.eq(self.master_nullifier_public_key))
& (request.application_nullifier_secret_key.eq(self.application_nullifier_secret_key))
& (request.contract_address.eq(self.contract_address))
}
}

impl Empty for NullifierKeyValidationRequestContext {
fn empty() -> Self {
NullifierKeyValidationRequestContext {
public_key: GrumpkinPoint::zero(),
secret_key: GrumpkinPrivateKey::zero(),
master_nullifier_public_key: GrumpkinPoint::zero(),
application_nullifier_secret_key: 0,
contract_address: AztecAddress::zero(),
}
}
Expand All @@ -80,10 +79,9 @@ impl Empty for NullifierKeyValidationRequestContext {
impl Serialize<NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH> for NullifierKeyValidationRequestContext {
fn serialize(self) -> [Field; NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH] {
[
self.public_key.x,
self.public_key.y,
self.secret_key.high,
self.secret_key.low,
self.master_nullifier_public_key.x,
self.master_nullifier_public_key.y,
self.application_nullifier_secret_key,
self.contract_address.to_field(),
]
}
Expand All @@ -92,9 +90,9 @@ impl Serialize<NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH> for NullifierKey
impl Deserialize<NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH> for NullifierKeyValidationRequestContext {
fn deserialize(fields: [Field; NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH]) -> Self {
Self {
public_key: GrumpkinPoint::new(fields[0], fields[1]),
secret_key: GrumpkinPrivateKey::new(fields[2], fields[3]),
contract_address: AztecAddress::from_field(fields[4]),
master_nullifier_public_key: GrumpkinPoint::new(fields[0], fields[1]),
application_nullifier_secret_key: fields[2],
contract_address: AztecAddress::from_field(fields[3]),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ global HEADER_LENGTH: u64 = 23; // 2 for last_archive + 4 for content commitment
global L1_TO_L2_MESSAGE_LENGTH: u64 = 6;
global L2_TO_L1_MESSAGE_LENGTH: u64 = 2;
global MAX_BLOCK_NUMBER_LENGTH: u64 = 2; // 1 for the option flag, 1 for the value
global NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH = 4;
global NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH = 5;
global NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH = 3;
global NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH = 4;
global PARTIAL_STATE_REFERENCE_LENGTH: u64 = 6;
global PRIVATE_CALL_STACK_ITEM_LENGTH: u64 = 221;
// Change this ONLY if you have changed the PrivateCircuitPublicInputs structure.
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export const HEADER_LENGTH = 23;
export const L1_TO_L2_MESSAGE_LENGTH = 6;
export const L2_TO_L1_MESSAGE_LENGTH = 2;
export const MAX_BLOCK_NUMBER_LENGTH = 2;
export const NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH = 4;
export const NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH = 5;
export const NULLIFIER_KEY_VALIDATION_REQUEST_LENGTH = 3;
export const NULLIFIER_KEY_VALIDATION_REQUEST_CONTEXT_LENGTH = 4;
export const PARTIAL_STATE_REFERENCE_LENGTH = 6;
export const PRIVATE_CALL_STACK_ITEM_LENGTH = 221;
export const PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 218;
Expand Down

0 comments on commit c8621c2

Please sign in to comment.