Skip to content

Commit

Permalink
feat: only export values from accumulated data (#5604)
Browse files Browse the repository at this point in the history
Exporting note hashes and nullifiers as fields instead of side effects
from CombinedAccumulatedData.
  • Loading branch information
LeilaWang authored Apr 8, 2024
1 parent 4e4f843 commit a974ec8
Show file tree
Hide file tree
Showing 34 changed files with 353 additions and 864 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ mod tests {
kernel_circuit_public_inputs::KernelCircuitPublicInputs, max_block_number::MaxBlockNumber,
side_effect::{SideEffect, SideEffectLinkedToNoteHash, Ordered}
},
hash::compute_unique_siloed_note_hashes,
hash::{compute_note_hash_nonce, compute_unique_siloed_note_hash},
tests::{fixture_builder::FixtureBuilder, sort::sort_get_sorted_hints},
utils::{arrays::{array_eq, array_length}}, traits::{Empty, is_empty, is_empty_array}
};
Expand All @@ -98,12 +98,16 @@ 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_unique_siloed_note_hashes<N>(
self,
note_hashes: [SideEffect; N]
) -> [SideEffect; N] {
pub fn compute_unique_siloed_note_hashes<N>(self, note_hashes: [SideEffect; N]) -> [Field; N] {
let first_nullifier = self.previous_kernel.new_nullifiers.get_unchecked(0);
compute_unique_siloed_note_hashes(first_nullifier.value, note_hashes)
let mut unique_siloed_note_hashes = [0; N];
for i in 0..N {
if note_hashes[i].value != 0 {
let nonce = compute_note_hash_nonce(first_nullifier.value, i);
unique_siloed_note_hashes[i] = compute_unique_siloed_note_hash(nonce, note_hashes[i].value);
}
}
unique_siloed_note_hashes
}

pub fn add_pending_note_hash_read_request(&mut self, note_hash_index: u64) {
Expand Down Expand Up @@ -271,7 +275,7 @@ mod tests {
assert(
array_eq(
public_inputs.end.new_nullifiers,
[new_nullifiers[0], new_nullifiers[2]]
[new_nullifiers[0].value, new_nullifiers[2].value]
)
);
}
Expand All @@ -298,7 +302,7 @@ mod tests {
assert(
array_eq(
public_inputs.end.new_nullifiers,
[new_nullifiers[0], new_nullifiers[2]]
[new_nullifiers[0].value, new_nullifiers[2].value]
)
);
}
Expand All @@ -317,7 +321,7 @@ mod tests {

// Only the first nullifier is left after squashing.
assert(is_empty_array(public_inputs.end.new_note_hashes));
assert(array_eq(public_inputs.end.new_nullifiers, [new_nullifiers[0]]));
assert(array_eq(public_inputs.end.new_nullifiers, [new_nullifiers[0].value]));
}

#[test]
Expand All @@ -341,17 +345,13 @@ mod tests {
builder.previous_kernel.new_note_hashes.extend_from_array(reversed_note_hashes);
builder.previous_kernel.new_nullifiers.extend_from_array(reversed_nullifiers);

let sorted_unique_note_hashes = compute_unique_siloed_note_hashes(
// tx nullifier is part of non revertible accumulated data
builder.previous_kernel.new_nullifiers.get_unchecked(0).value,
sorted_note_hashes
);
let sorted_unique_note_hashes = builder.compute_unique_siloed_note_hashes(sorted_note_hashes);

let public_inputs = builder.execute();

for i in 0..10 {
assert(public_inputs.end.new_note_hashes[i].eq(sorted_unique_note_hashes[i]));
assert(public_inputs.end.new_nullifiers[i].eq(sorted_nullifiers[i]));
assert(public_inputs.end.new_nullifiers[i].eq(sorted_nullifiers[i].value));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ mod tests {
kernel_circuit_public_inputs::PublicKernelCircuitPublicInputs,
side_effect::{SideEffect, SideEffectLinkedToNoteHash, Ordered}
},
hash::compute_unique_siloed_note_hashes,
hash::{compute_note_hash_nonce, compute_unique_siloed_note_hash},
tests::{fixture_builder::FixtureBuilder, sort::sort_get_sorted_hints},
utils::{arrays::{array_eq, array_length}}, traits::is_empty_array
};
Expand Down Expand Up @@ -104,7 +104,17 @@ mod tests {
note_hashes: [SideEffect; N]
) -> [SideEffect; N] {
let first_nullifier = self.previous_kernel.new_nullifiers.get_unchecked(0);
compute_unique_siloed_note_hashes(first_nullifier.value, note_hashes)
let mut unique_siloed_note_hashes = [SideEffect::empty(); N];
for i in 0..N {
if note_hashes[i].value != 0 {
let nonce = compute_note_hash_nonce(first_nullifier.value, i);
unique_siloed_note_hashes[i] = SideEffect {
value: compute_unique_siloed_note_hash(nonce, note_hashes[i].value),
counter: note_hashes[i].counter,
};
}
}
unique_siloed_note_hashes
}

pub fn add_pending_note_hash_read_request(&mut self, note_hash_index: u64) {
Expand Down Expand Up @@ -367,11 +377,7 @@ mod tests {
builder.previous_kernel.new_note_hashes.extend_from_array(reversed_note_hashes);
builder.previous_kernel.new_nullifiers.extend_from_array(reversed_nullifiers);

let sorted_unique_note_hashes = compute_unique_siloed_note_hashes(
// tx nullifier is part of non revertible accumulated data
builder.previous_kernel.new_nullifiers.get_unchecked(0).value,
sorted_note_hashes
);
let sorted_unique_note_hashes = builder.compute_unique_siloed_note_hashes(sorted_note_hashes);

let public_inputs = builder.execute();

Expand Down Expand Up @@ -470,11 +476,7 @@ mod tests {
let new_note_hashes = builder.previous_kernel.new_note_hashes.storage;
let public_inputs = builder.execute();

let siloed_note_hashes = compute_unique_siloed_note_hashes(
// tx nullifier is part of non revertible accumulated data
public_inputs.end_non_revertible.new_nullifiers[0].value,
new_note_hashes
);
let siloed_note_hashes = builder.compute_unique_siloed_note_hashes(new_note_hashes);

assert(
array_eq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use dep::types::{
membership_witness::{ArchiveRootMembershipWitness, NullifierMembershipWitness, PublicDataMembershipWitness},
nullifier_leaf_preimage::NullifierLeafPreimage, public_data_update_request::PublicDataUpdateRequest,
public_data_read::PublicDataRead, kernel_data::KernelData,
side_effect::{SideEffect, SideEffectLinkedToNoteHash}, accumulated_data::CombinedAccumulatedData
side_effect::{SideEffect, SideEffectLinkedToNoteHash}
},
constants::{
NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH, NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH,
Expand Down Expand Up @@ -126,13 +126,13 @@ impl BaseRollupInputs {
// TODO(Kev): This should say calculate_commitments_subtree_root
// Cpp code says calculate_commitments_subtree, so I'm leaving it as is for now
fn calculate_commitments_subtree(self) -> Field {
calculate_subtree_root(self.kernel_data.public_inputs.end.new_note_hashes.map(|c: SideEffect| c.value))
calculate_subtree_root(self.kernel_data.public_inputs.end.new_note_hashes)
}

fn check_nullifier_tree_non_membership_and_insert_to_tree(self) -> AppendOnlyTreeSnapshot {
indexed_tree::batch_insert(
self.start.nullifier_tree,
self.kernel_data.public_inputs.end.new_nullifiers.map(|nullifier: SideEffectLinkedToNoteHash| nullifier.value),
self.kernel_data.public_inputs.end.new_nullifiers,
self.state_diff_hints.sorted_nullifiers,
self.state_diff_hints.sorted_nullifier_indexes,
self.state_diff_hints.nullifier_subtree_sibling_path,
Expand Down Expand Up @@ -580,7 +580,7 @@ mod tests {

let low_index = self.new_nullifiers.get_unchecked(original_index).existing_index;

kernel_data.public_inputs.end.new_nullifiers[original_index].value = new_nullifier;
kernel_data.public_inputs.end.new_nullifiers[original_index] = new_nullifier;

let mut low_preimage = pre_existing_nullifiers[low_index];
nullifier_predecessor_preimages[i] = low_preimage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ pub fn compute_tx_effects_hash(combined: CombinedAccumulatedData, revert_code: u
offset += 1;

for j in 0..MAX_NEW_NOTE_HASHES_PER_TX {
txs_effects_hash_input[offset + j] = new_note_hashes[j].value;
txs_effects_hash_input[offset + j] = new_note_hashes[j];
}
offset += MAX_NEW_NOTE_HASHES_PER_TX ;

for j in 0..MAX_NEW_NULLIFIERS_PER_TX {
txs_effects_hash_input[offset + j] = new_nullifiers[j].value;
txs_effects_hash_input[offset + j] = new_nullifiers[j];
}
offset += MAX_NEW_NULLIFIERS_PER_TX ;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
mod combined_accumulated_data;
mod combined_accumulated_data_builder;
mod private_accumulated_data;
mod private_accumulated_data_builder;
mod public_accumulated_data;
mod public_accumulated_data_builder;

use crate::abis::accumulated_data::{
combined_accumulated_data::CombinedAccumulatedData,
combined_accumulated_data_builder::CombinedAccumulatedDataBuilder,
private_accumulated_data::PrivateAccumulatedData,
private_accumulated_data_builder::PrivateAccumulatedDataBuilder,
public_accumulated_data::PublicAccumulatedData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::{
};

struct CombinedAccumulatedData {
new_note_hashes: [SideEffect; MAX_NEW_NOTE_HASHES_PER_TX],
new_nullifiers: [SideEffectLinkedToNoteHash; MAX_NEW_NULLIFIERS_PER_TX],
new_note_hashes: [Field; MAX_NEW_NOTE_HASHES_PER_TX],
new_nullifiers: [Field; MAX_NEW_NULLIFIERS_PER_TX],
new_l2_to_l1_msgs: [Field; MAX_NEW_L2_TO_L1_MSGS_PER_TX],

encrypted_logs_hash: Field,
Expand All @@ -30,8 +30,8 @@ struct CombinedAccumulatedData {
impl CombinedAccumulatedData {
pub fn recombine(non_revertible: PublicAccumulatedData, revertible: PublicAccumulatedData) -> Self {
CombinedAccumulatedData {
new_note_hashes: array_merge(non_revertible.new_note_hashes, revertible.new_note_hashes),
new_nullifiers: array_merge(non_revertible.new_nullifiers, revertible.new_nullifiers),
new_note_hashes: array_merge(non_revertible.new_note_hashes, revertible.new_note_hashes).map(|n: SideEffect| n.value),
new_nullifiers: array_merge(non_revertible.new_nullifiers, revertible.new_nullifiers).map(|n: SideEffectLinkedToNoteHash| n.value),
new_l2_to_l1_msgs: revertible.new_l2_to_l1_msgs,
encrypted_logs_hash: revertible.encrypted_logs_hash,
unencrypted_logs_hash: revertible.unencrypted_logs_hash,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl PrivateAccumulatedDataBuilder {

pub fn to_combined(self) -> CombinedAccumulatedData {
CombinedAccumulatedData {
new_note_hashes: self.new_note_hashes.storage,
new_nullifiers: self.new_nullifiers.storage,
new_note_hashes: self.new_note_hashes.storage.map(|n: SideEffect| n.value),
new_nullifiers: self.new_nullifiers.storage.map(|n: SideEffectLinkedToNoteHash| n.value),
new_l2_to_l1_msgs: self.new_l2_to_l1_msgs.storage,
encrypted_logs_hash: self.encrypted_logs_hash,
unencrypted_logs_hash: self.unencrypted_logs_hash,
Expand Down
19 changes: 0 additions & 19 deletions noir-projects/noir-protocol-circuits/crates/types/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::mocked::VerificationKey;
use crate::abis::function_selector::FunctionSelector;
use crate::abis::contract_class_function_leaf_preimage::ContractClassFunctionLeafPreimage;
use crate::contract_class_id::ContractClassId;
use crate::abis::side_effect::SideEffect;
use crate::utils::{uint256::U256, field::field_from_bytes_32_trunc};
use crate::constants::{
FUNCTION_TREE_HEIGHT, GENERATOR_INDEX__SILOED_NOTE_HASH, GENERATOR_INDEX__OUTER_NULLIFIER,
Expand Down Expand Up @@ -148,24 +147,6 @@ pub fn compute_unique_siloed_note_hash(nonce: Field, siloed_note_hash: Field) ->
)
}

pub fn compute_unique_siloed_note_hashes<N>(
first_nullifier: Field,
siloed_note_hashes: [SideEffect; N]
) -> [SideEffect; N] {
let mut unique_siloed_note_hashes = [SideEffect::empty(); N];
for i in 0..N {
let siloed_note_hash = siloed_note_hashes[i];
if siloed_note_hash.value != 0 {
let nonce = compute_note_hash_nonce(first_nullifier, i);
unique_siloed_note_hashes[i] = SideEffect {
value: compute_unique_siloed_note_hash(nonce, siloed_note_hash.value),
counter: siloed_note_hash.counter
};
}
}
unique_siloed_note_hashes
}

pub fn pedersen_hash<N>(inputs: [Field; N], hash_index: u32) -> Field {
dep::std::hash::pedersen_hash_with_separator(inputs, hash_index)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::{
abis::{
call_context::CallContext, call_request::{CallerContext, CallRequest},
accumulated_data::{
CombinedAccumulatedData, CombinedAccumulatedDataBuilder, PrivateAccumulatedData,
PrivateAccumulatedDataBuilder, PublicAccumulatedData, PublicAccumulatedDataBuilder
CombinedAccumulatedData, PrivateAccumulatedData, PrivateAccumulatedDataBuilder,
PublicAccumulatedData, PublicAccumulatedDataBuilder
},
combined_constant_data::CombinedConstantData,
kernel_circuit_public_inputs::{KernelCircuitPublicInputs, PrivateKernelCircuitPublicInputs, PublicKernelCircuitPublicInputs},
Expand Down Expand Up @@ -137,17 +137,16 @@ impl FixtureBuilder {
}

pub fn to_combined_accumulated_data(self) -> CombinedAccumulatedData {
let public_inputs = CombinedAccumulatedDataBuilder {
new_note_hashes: self.new_note_hashes,
new_nullifiers: self.new_nullifiers,
new_l2_to_l1_msgs: self.new_l2_to_l1_msgs,
CombinedAccumulatedData {
new_note_hashes: self.new_note_hashes.storage.map(|n: SideEffect| n.value),
new_nullifiers: self.new_nullifiers.storage.map(|n: SideEffectLinkedToNoteHash| n.value),
new_l2_to_l1_msgs: self.new_l2_to_l1_msgs.storage,
encrypted_logs_hash: self.encrypted_logs_hash,
unencrypted_logs_hash: self.unencrypted_logs_hash,
encrypted_log_preimages_length: self.encrypted_log_preimages_length,
unencrypted_log_preimages_length: self.unencrypted_log_preimages_length,
public_data_update_requests: self.public_data_update_requests
};
public_inputs.finish()
public_data_update_requests: self.public_data_update_requests.storage
}
}

pub fn to_validation_requests(self) -> ValidationRequests {
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/circuit-types/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const mockTx = (

const isForPublic = totalPublicCallRequests > 0;
const data = PrivateKernelTailCircuitPublicInputs.empty();
const firstNullifier = new SideEffectLinkedToNoteHash(new Fr(seed), new Fr(seed + 1), Fr.ZERO);
const firstNullifier = new SideEffectLinkedToNoteHash(new Fr(seed + 1), new Fr(seed + 2), Fr.ZERO);

if (isForPublic) {
data.forRollup = undefined;
Expand All @@ -67,7 +67,7 @@ export const mockTx = (
: CallRequest.empty(),
);
} else {
data.forRollup!.end.newNullifiers[0] = firstNullifier;
data.forRollup!.end.newNullifiers[0] = firstNullifier.value;
}

const target = isForPublic ? data.forPublic! : data.forRollup!;
Expand Down
6 changes: 2 additions & 4 deletions yarn-project/circuit-types/src/tx/processed_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import {
KernelCircuitPublicInputs,
type Proof,
type PublicKernelCircuitPublicInputs,
type SideEffect,
type SideEffectLinkedToNoteHash,
makeEmptyProof,
} from '@aztec/circuits.js';

Expand Down Expand Up @@ -131,8 +129,8 @@ export function makeEmptyProcessedTx(header: Header, chainId: Fr, version: Fr):
export function toTxEffect(tx: ProcessedTx): TxEffect {
return new TxEffect(
tx.data.revertCode,
tx.data.end.newNoteHashes.map((c: SideEffect) => c.value).filter(h => !h.isZero()),
tx.data.end.newNullifiers.map((n: SideEffectLinkedToNoteHash) => n.value).filter(h => !h.isZero()),
tx.data.end.newNoteHashes.filter(h => !h.isZero()),
tx.data.end.newNullifiers.filter(h => !h.isZero()),
tx.data.end.newL2ToL1Msgs.filter(h => !h.isZero()),
tx.data.end.publicDataUpdateRequests
.map(t => new PublicDataWrite(t.leafSlot, t.newValue))
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/circuit-types/src/tx/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ export class Tx {
getTxHash(): TxHash {
// Private kernel functions are executed client side and for this reason tx hash is already set as first nullifier
const firstNullifier = this.data.getNonEmptyNullifiers()[0];
if (!firstNullifier || firstNullifier.isEmpty()) {
if (!firstNullifier || firstNullifier.isZero()) {
throw new Error(`Cannot get tx hash since first nullifier is missing`);
}
return new TxHash(firstNullifier.value.toBuffer());
return new TxHash(firstNullifier.toBuffer());
}

/** Returns stats about this tx. */
Expand Down
1 change: 1 addition & 0 deletions yarn-project/circuits.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"./hash": "./dest/hash/index.js",
"./barretenberg": "./dest/barretenberg/index.js",
"./testing": "./dest/tests/index.js",
"./interfaces": "./dest/interfaces/index.js",
"./utils": "./dest/utils/index.js",
"./types": "./dest/types/index.js",
"./constants": "./dest/constants.gen.js",
Expand Down
Loading

0 comments on commit a974ec8

Please sign in to comment.