Skip to content

Commit

Permalink
feat: remove note hash nullifier counter. (#7294)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeilaWang authored Jul 4, 2024
1 parent b8eef86 commit c0c9144
Show file tree
Hide file tree
Showing 41 changed files with 429 additions and 874 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod previous_kernel_validator;
mod private_call_data_validator;
mod private_kernel_circuit_output_validator;
mod private_kernel_circuit_public_inputs_composer;
mod reset_output_composer;
mod tail_output_composer;
mod tail_output_validator;
mod tail_to_public_output_composer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ impl PreviousKernelValidator {
}

fn verify_no_transient_data(self) {
// Currently all the transient note hashes and nullifiers must be cleared in the reset circuits.
// Check that the propagated note hashes don't link to a nullifier, and vice versa.
for note_hash in self.previous_kernel.end.note_hashes {
assert_eq(note_hash.nullifier_counter, 0, "Unresolved transient note hash");
}
// Currently all the transient nullifiers must be cleared in the reset circuits.
// When a transient nullifier is removed, the linked note hash and all the associated note logs will also be removed.
for nullifier in self.previous_kernel.end.nullifiers {
assert_eq(nullifier.nullified_note_hash(), 0, "Unresolved transient nullifier");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::types::{
abis::{
call_request::CallRequest,
kernel_circuit_public_inputs::{PrivateKernelCircuitPublicInputs, PrivateKernelCircuitPublicInputsArrayLengths},
max_block_number::MaxBlockNumber, note_hash::ScopedNoteHash,
max_block_number::MaxBlockNumber,
private_circuit_public_inputs::{PrivateCircuitPublicInputs, PrivateCircuitPublicInputsArrayLengths},
private_kernel::private_call_data::PrivateCallData, side_effect::Scoped
},
Expand Down Expand Up @@ -103,28 +103,6 @@ fn validate_array_appended_reversed_scoped<ST, T, N, M>(
}
}

fn validate_note_hash_nullifier_counters<N, M>(
dest: [ScopedNoteHash; N],
note_hash_nullifier_counters: [u32; M],
num_source_items: u32,
num_prepended_items: u32
) {
let mut is_non_empty_item = true;
for i in 0..M {
is_non_empty_item &= i != num_source_items;
if is_non_empty_item {
let nullifier_counter = note_hash_nullifier_counters[i];
let note_hash = dest[i + num_prepended_items];
assert(
(nullifier_counter == 0) | (nullifier_counter > note_hash.counter()), "invalid nullifier counter"
);
assert_eq(
note_hash.nullifier_counter, nullifier_counter, "incorrect nullifier counter assigned to dest"
);
}
}
}

struct PrivateKernelCircuitOutputValidator {
output: PrivateKernelCircuitPublicInputs,
}
Expand All @@ -134,13 +112,12 @@ impl PrivateKernelCircuitOutputValidator {
PrivateKernelCircuitOutputValidator { output }
}

pub fn validate_as_first_call<NOTE_HASH_NULLIFIER_COUNTERS_LEN, PUBLIC_CALL_REQUESTS_LEN>(
pub fn validate_as_first_call<PUBLIC_CALL_REQUESTS_LEN>(
self,
tx_request: TxRequest,
private_call: PrivateCircuitPublicInputs,
private_call_array_lengths: PrivateCircuitPublicInputsArrayLengths,
contract_address: AztecAddress,
note_hash_nullifier_counters: [u32; NOTE_HASH_NULLIFIER_COUNTERS_LEN],
public_call_requests: [CallRequest; PUBLIC_CALL_REQUESTS_LEN],
public_teardown_call_request: CallRequest
) {
Expand All @@ -153,19 +130,17 @@ impl PrivateKernelCircuitOutputValidator {
offsets,
0, // num_popped_call
contract_address,
note_hash_nullifier_counters,
public_call_requests
);
}

pub fn validate_as_inner_call<NOTE_HASH_NULLIFIER_COUNTERS_LEN, PUBLIC_CALL_REQUESTS_LEN>(
pub fn validate_as_inner_call<PUBLIC_CALL_REQUESTS_LEN>(
self,
previous_kernel: PrivateKernelCircuitPublicInputs,
previous_kernel_array_lengths: PrivateKernelCircuitPublicInputsArrayLengths,
private_call: PrivateCircuitPublicInputs,
private_call_array_lengths: PrivateCircuitPublicInputsArrayLengths,
contract_address: AztecAddress,
note_hash_nullifier_counters: [u32; NOTE_HASH_NULLIFIER_COUNTERS_LEN],
public_call_requests: [CallRequest; PUBLIC_CALL_REQUESTS_LEN],
public_teardown_call_request: CallRequest
) {
Expand All @@ -177,7 +152,6 @@ impl PrivateKernelCircuitOutputValidator {
previous_kernel_array_lengths,
1, // num_popped_call
contract_address,
note_hash_nullifier_counters,
public_call_requests
);
}
Expand Down Expand Up @@ -336,14 +310,13 @@ impl PrivateKernelCircuitOutputValidator {
);
}

fn validate_propagated_from_private_call<NOTE_HASH_NULLIFIER_COUNTERS_LEN, PUBLIC_CALL_REQUESTS_LEN>(
fn validate_propagated_from_private_call<PUBLIC_CALL_REQUESTS_LEN>(
self,
private_call: PrivateCircuitPublicInputs,
array_lengths: PrivateCircuitPublicInputsArrayLengths,
offsets: PrivateKernelCircuitPublicInputsArrayLengths,
num_popped_call: u32,
contract_address: AztecAddress,
note_hash_nullifier_counters: [u32; NOTE_HASH_NULLIFIER_COUNTERS_LEN],
public_call_requests: [CallRequest; PUBLIC_CALL_REQUESTS_LEN]
) {
let storage_contract_address = private_call.call_context.storage_contract_address;
Expand Down Expand Up @@ -375,12 +348,6 @@ impl PrivateKernelCircuitOutputValidator {
offsets.note_hashes,
storage_contract_address
);
validate_note_hash_nullifier_counters(
self.output.end.note_hashes,
note_hash_nullifier_counters,
array_lengths.note_hashes,
offsets.note_hashes
);
validate_array_appended_scoped(
self.output.end.nullifiers,
private_call.nullifiers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use dep::types::{
},
address::AztecAddress,
constants::{
MAX_NOTE_HASHES_PER_CALL, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, MAX_NOTE_HASHES_PER_TX,
MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, MAX_NOTE_HASHES_PER_TX,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL
},
hash::{
Expand All @@ -22,7 +22,6 @@ struct DataSource {
private_call_public_inputs: PrivateCircuitPublicInputs,
contract_address: AztecAddress,
storage_contract_address: AztecAddress,
note_hash_nullifier_counters: [u32; MAX_NOTE_HASHES_PER_CALL],
public_call_requests: [CallRequest; MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL],
public_teardown_call_request: CallRequest,
}
Expand Down Expand Up @@ -91,7 +90,6 @@ impl PrivateKernelCircuitPublicInputsComposer {
&mut self,
private_call_public_inputs: PrivateCircuitPublicInputs,
contract_address: AztecAddress,
note_hash_nullifier_counters: [u32; MAX_NOTE_HASHES_PER_CALL],
public_call_requests: [CallRequest; MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL],
public_teardown_call_request: CallRequest
) -> Self {
Expand All @@ -100,7 +98,6 @@ impl PrivateKernelCircuitPublicInputsComposer {
private_call_public_inputs,
contract_address,
storage_contract_address,
note_hash_nullifier_counters,
public_call_requests,
public_teardown_call_request
};
Expand Down Expand Up @@ -183,13 +180,9 @@ impl PrivateKernelCircuitPublicInputsComposer {
fn propagate_note_hashes(&mut self, source: DataSource) {
let note_hashes = source.private_call_public_inputs.note_hashes;
for i in 0..note_hashes.len() {
let mut note_hash = note_hashes[i];
let note_hash = note_hashes[i];
if note_hash.value != 0 {
let nullifier_counter = source.note_hash_nullifier_counters[i];
assert(
(nullifier_counter == 0) | (nullifier_counter > note_hash.counter), "Invalid nullifier counter"
);
self.public_inputs.end.note_hashes.push(note_hash.scope(nullifier_counter, source.storage_contract_address));
self.public_inputs.end.note_hashes.push(note_hash.scope(source.storage_contract_address));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
mod squash_transient_data;

use crate::components::reset_output_composer::squash_transient_data::squash_transient_data;
use dep::types::{
abis::{
kernel_circuit_public_inputs::PrivateKernelCircuitPublicInputs, log_hash::NoteLogHash,
note_hash::ScopedNoteHash, nullifier::ScopedNullifier
},
constants::{MAX_NOTE_ENCRYPTED_LOGS_PER_TX, MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX}
};

struct PrivateKernelResetOutputs {
note_hashes: [ScopedNoteHash; MAX_NOTE_HASHES_PER_TX],
nullifiers: [ScopedNullifier; MAX_NULLIFIERS_PER_TX],
note_encrypted_log_hashes: [NoteLogHash; MAX_NOTE_ENCRYPTED_LOGS_PER_TX],
}

struct ResetOutputComposer {
output: PrivateKernelResetOutputs,
}

impl ResetOutputComposer {
pub fn new(
previous_kernel: PrivateKernelCircuitPublicInputs,
transient_nullifier_indexes_for_note_hashes: [u32; MAX_NOTE_HASHES_PER_TX],
transient_note_hash_indexes_for_nullifiers: [u32; MAX_NULLIFIERS_PER_TX]
) -> Self {
let (note_hashes, nullifiers, note_encrypted_log_hashes) = squash_transient_data(
previous_kernel.end.note_hashes,
previous_kernel.end.nullifiers,
previous_kernel.end.note_encrypted_logs_hashes,
transient_nullifier_indexes_for_note_hashes,
transient_note_hash_indexes_for_nullifiers
);
let output = PrivateKernelResetOutputs { note_hashes, nullifiers, note_encrypted_log_hashes };
ResetOutputComposer { output }
}

pub fn finish(self) -> PrivateKernelResetOutputs {
self.output
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use dep::types::abis::{note_hash::ScopedNoteHash, nullifier::ScopedNullifier, log_hash::NoteLogHash};

unconstrained pub fn squash_transient_data<M, N, P>(
note_hashes: [ScopedNoteHash; M],
nullifiers: [ScopedNullifier; N],
logs: [NoteLogHash; P],
transient_nullifier_indexes_for_note_hashes: [u32; M],
transient_note_hash_indexes_for_nullifiers: [u32; N]
) -> ([ScopedNoteHash; M], [ScopedNullifier; N], [NoteLogHash; P]) {
let mut propagated_note_hashes = BoundedVec::new();
for i in 0..note_hashes.len() {
if transient_nullifier_indexes_for_note_hashes[i] == N {
propagated_note_hashes.push(note_hashes[i]);
}
}

let mut propagated_nullifiers = BoundedVec::new();
for i in 0..N {
if transient_note_hash_indexes_for_nullifiers[i] == M {
propagated_nullifiers.push(nullifiers[i]);
}
}

let mut propagated_logs = BoundedVec::new();
for i in 0..logs.len() {
let log = logs[i];
let linked_note_hash_propagated = propagated_note_hashes.storage.any(|n: ScopedNoteHash| (n.counter() == log.note_hash_counter));
if linked_note_hash_propagated {
propagated_logs.push(log);
}
}

(propagated_note_hashes.storage, propagated_nullifiers.storage, propagated_logs.storage)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@ use dep::types::{
kernel_circuit_public_inputs::PrivateKernelCircuitPublicInputs,
private_kernel::private_call_data::PrivateCallData
},
constants::MAX_NOTE_HASHES_PER_CALL, transaction::tx_request::TxRequest
transaction::tx_request::TxRequest
};

struct PrivateKernelInitHints {
// TODO: Remove note_hash_nullifier_counters.
note_hash_nullifier_counters: [u32; MAX_NOTE_HASHES_PER_CALL],
}

// Initialization struct for private inputs to the private kernel
struct PrivateKernelInitCircuitPrivateInputs {
tx_request: TxRequest,
private_call: PrivateCallData,
hints: PrivateKernelInitHints,
}

impl PrivateKernelInitCircuitPrivateInputs {
Expand All @@ -31,7 +25,6 @@ impl PrivateKernelInitCircuitPrivateInputs {
PrivateKernelCircuitPublicInputsComposer::new_from_tx_request(self.tx_request, private_call_public_inputs).with_private_call(
private_call_public_inputs,
self.private_call.call_stack_item.contract_address,
self.hints.note_hash_nullifier_counters,
self.private_call.public_call_stack,
self.private_call.public_teardown_call_request
).finish()
Expand All @@ -58,7 +51,6 @@ impl PrivateKernelInitCircuitPrivateInputs {
self.private_call.call_stack_item.public_inputs,
private_call_data_validator.array_lengths,
self.private_call.call_stack_item.contract_address,
self.hints.note_hash_nullifier_counters,
self.private_call.public_call_stack,
self.private_call.public_teardown_call_request
);
Expand All @@ -68,31 +60,28 @@ impl PrivateKernelInitCircuitPrivateInputs {
}

mod tests {
use crate::private_kernel_init::{PrivateKernelInitHints, PrivateKernelInitCircuitPrivateInputs};
use crate::private_kernel_init::PrivateKernelInitCircuitPrivateInputs;
use dep::types::{
abis::{kernel_circuit_public_inputs::PrivateKernelCircuitPublicInputs},
constants::MAX_NOTE_HASHES_PER_CALL,
tests::{fixture_builder::FixtureBuilder, utils::assert_array_eq},
transaction::tx_request::TxRequest
};

struct PrivateKernelInitInputsBuilder {
tx_request: TxRequest,
private_call: FixtureBuilder,
hints: PrivateKernelInitHints,
}

impl PrivateKernelInitInputsBuilder {
pub fn new() -> Self {
let private_call = FixtureBuilder::new();
let tx_request = private_call.build_tx_request();
let hints = PrivateKernelInitHints { note_hash_nullifier_counters: [0; MAX_NOTE_HASHES_PER_CALL] };
PrivateKernelInitInputsBuilder { tx_request, private_call, hints }
PrivateKernelInitInputsBuilder { tx_request, private_call }
}

pub fn execute(self) -> PrivateKernelCircuitPublicInputs {
let private_call = self.private_call.to_private_call_data();
PrivateKernelInitCircuitPrivateInputs { tx_request: self.tx_request, private_call, hints: self.hints }.execute()
PrivateKernelInitCircuitPrivateInputs { tx_request: self.tx_request, private_call }.execute()
}
}

Expand Down
Loading

0 comments on commit c0c9144

Please sign in to comment.