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

refactor: minor naming cleanup #7144

Merged
merged 2 commits into from
Jun 26, 2024
Merged
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 @@ -44,10 +44,12 @@ unconstrained pub fn generate_hints(previous_kernel: PrivateKernelCircuitPublicI
let sorted_note_hash_hints = sort_get_order_hints_asc(previous_kernel.end.new_note_hashes);

let mut siloed_note_hashes = [0; MAX_NEW_NOTE_HASHES_PER_TX];
let first_nullifier = previous_kernel.end.new_nullifiers[0].value();

// First nullifier is tx hash.
let tx_hash = previous_kernel.end.new_nullifiers[0].value();
let unsiloed_note_hashes = previous_kernel.end.new_note_hashes;
for i in 0..unsiloed_note_hashes.len() {
siloed_note_hashes[i] = silo_note_hash(unsiloed_note_hashes[i], first_nullifier, i);
siloed_note_hashes[i] = silo_note_hash(unsiloed_note_hashes[i], tx_hash, i);
}

// nullifiers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ impl KernelCircuitOutputValidator {
}

fn validate_propagated_sorted_siloed_values(self, hints: Hints) {
// new_note_hashes
let first_nullifier = self.output.end.new_nullifiers[0];
// First nullifier is tx hash.
let tx_hash = self.output.end.new_nullifiers[0];
let unsiloed_note_hashes = self.previous_kernel.end.new_note_hashes;
for i in 0..unsiloed_note_hashes.len() {
let siloed_note_hash = silo_note_hash(unsiloed_note_hashes[i], first_nullifier, i);
let siloed_note_hash = silo_note_hash(unsiloed_note_hashes[i], tx_hash, i);
assert_eq(hints.siloed_note_hashes[i], siloed_note_hash, "mismatch siloed note hashes");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,14 @@ impl KernelCircuitPublicInputsComposer {
}

fn silo_note_hashes(&mut self) {
let first_nullifier = self.public_inputs.end.new_nullifiers.get_unchecked(0).value();
// This check is unnecessary. The 0th nullifier will always be set a non-zero value in private_kernel_init.
// assert(first_nullifier != 0, "The 0th nullifier in the accumulated nullifier array is zero");
// First nullifier is tx hash.
let tx_hash = self.public_inputs.end.new_nullifiers.get_unchecked(0).value();

let note_hashes = self.public_inputs.end.new_note_hashes.storage;
for i in 0..note_hashes.len() {
self.public_inputs.end.new_note_hashes.storage[i].note_hash.value = silo_note_hash(
note_hashes[i],
first_nullifier,
tx_hash,
i
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ mod tests {
// A helper function that uses the first nullifer in the previous kernel to compute the unique siloed
// note_hashes for the given note_hashes.
pub fn compute_output_note_hashes<N>(self, note_hashes: [ScopedNoteHash; N]) -> [Field; N] {
let first_nullifier = self.previous_kernel.new_nullifiers.get_unchecked(0).value();
// First nullifier is tx hash.
let tx_hash = self.previous_kernel.new_nullifiers.get_unchecked(0).value();
let mut output = [0; N];
for i in 0..N {
output[i] = silo_note_hash(note_hashes[i], first_nullifier, i);
output[i] = silo_note_hash(note_hashes[i], tx_hash, i);
}
output
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ mod tests {
// A helper function that uses the first nullifer in the previous kernel to compute the unique siloed
// note_hashes for the given note_hashes.
pub fn compute_output_note_hashes<N>(self, note_hashes: [ScopedNoteHash; N]) -> [NoteHash; N] {
let first_nullifier = self.previous_kernel.new_nullifiers.get_unchecked(0).value();
// First nullifier is tx hash.
let tx_hash = self.previous_kernel.new_nullifiers.get_unchecked(0).value();
let mut output = [NoteHash::empty(); N];
for i in 0..N {
output[i] = NoteHash {
value: silo_note_hash(note_hashes[i], first_nullifier, i),
value: silo_note_hash(note_hashes[i], tx_hash, i),
counter: 0, // Counter is cleared so it's not exposed to the public.
};
}
Expand Down
18 changes: 9 additions & 9 deletions noir-projects/noir-protocol-circuits/crates/types/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ use crate::{
GENERATOR_INDEX__VK, GENERATOR_INDEX__NOTE_HASH_NONCE, GENERATOR_INDEX__UNIQUE_NOTE_HASH,
MAX_ENCRYPTED_LOGS_PER_TX, MAX_NOTE_ENCRYPTED_LOGS_PER_TX
},
contract_class_id::ContractClassId, merkle_tree::root::root_from_sibling_path,
messaging::l2_to_l1_message::{L2ToL1Message, ScopedL2ToL1Message},
recursion::verification_key::VerificationKey, traits::{Hash, is_empty},
utils::{uint256::U256, field::field_from_bytes_32_trunc}
merkle_tree::root::root_from_sibling_path, messaging::l2_to_l1_message::ScopedL2ToL1Message,
recursion::verification_key::VerificationKey, traits::is_empty,
utils::field::field_from_bytes_32_trunc
};
use dep::std::hash::{pedersen_hash_with_separator, sha256};

Expand All @@ -35,11 +34,12 @@ pub fn private_functions_root_from_siblings(
root_from_sibling_path(function_leaf, function_leaf_index, function_leaf_sibling_path)
}

pub fn compute_note_hash_nonce(first_nullifier: Field, note_hash_index: u32) -> Field {
fn compute_note_hash_nonce(tx_hash: Field, note_index_in_tx: u32) -> Field {
// Hashing tx hash with note index in tx is guaranteed to be unique
pedersen_hash(
[
first_nullifier,
note_hash_index as Field
tx_hash,
note_index_in_tx as Field
],
GENERATOR_INDEX__NOTE_HASH_NONCE
)
Expand All @@ -60,11 +60,11 @@ pub fn compute_siloed_note_hash(app: AztecAddress, unique_note_hash: Field) -> F
)
}

pub fn silo_note_hash(note_hash: ScopedNoteHash, first_nullifier: Field, index: u32) -> Field {
pub fn silo_note_hash(note_hash: ScopedNoteHash, tx_hash: Field, note_index_in_tx: u32) -> Field {
if note_hash.contract_address.is_zero() {
0
} else {
let nonce = compute_note_hash_nonce(first_nullifier, index);
let nonce = compute_note_hash_nonce(tx_hash, note_index_in_tx);
let unique_note_hash = compute_unique_note_hash(nonce, note_hash.value());
compute_siloed_note_hash(note_hash.contract_address, unique_note_hash)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,11 @@ impl FixtureBuilder {
}

pub fn add_siloed_note_hash(&mut self, value: Field) {
let first_nullifier = self.new_nullifiers.get(0).value();
// First nullifier is tx hash.
let tx_hash = self.new_nullifiers.get(0).value();
let index = self.new_note_hashes.len();
let note_hash = NoteHash { value, counter: 0 }.scope(0, self.storage_contract_address);
let siloed_value = silo_note_hash(note_hash, first_nullifier, index);
let siloed_value = silo_note_hash(note_hash, tx_hash, index);
self.add_new_note_hash(siloed_value, 0);
}

Expand Down
Loading