Skip to content

Commit

Permalink
feat: Trust tree roots from the AVM in public base (#11823)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirasistant authored Feb 10, 2025
1 parent eef5302 commit 5d12f94
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 948 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use super::components::constants::validate_tx_constant_data;
use dep::types::{
abis::{
append_only_tree_snapshot::AppendOnlyTreeSnapshot, constant_rollup_data::ConstantRollupData,
nullifier_leaf_preimage::NullifierLeafPreimage, public_data_write::PublicDataWrite,
public_log::PublicLog, sponge_blob::SpongeBlob, tube::PrivateTubeData,
public_data_write::PublicDataWrite, public_log::PublicLog, sponge_blob::SpongeBlob,
tube::PrivateTubeData,
},
constants::{
ARCHIVE_HEIGHT, MAX_PUBLIC_LOGS_PER_TX, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
Expand Down Expand Up @@ -173,10 +173,6 @@ impl PrivateBaseRollupInputs {
)
}

fn create_nullifier_subtree<let N: u32>(leaves: [NullifierLeafPreimage; N]) -> Field {
calculate_subtree_root(leaves.map(|leaf: NullifierLeafPreimage| leaf.hash()))
}

fn build_fee_public_data_write(self, tx_fee: Field) -> PublicDataWrite {
let fee_payer = self.tube_data.public_inputs.fee_payer;

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use dep::types::{
abis::nullifier_leaf_preimage::NullifierLeafPreimage,
constants::{
MAX_NULLIFIERS_PER_TX, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH, NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH,
NULLIFIER_TREE_HEIGHT, PUBLIC_DATA_TREE_HEIGHT,
MAX_NULLIFIERS_PER_TX, NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH,
NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH, NULLIFIER_TREE_HEIGHT, PUBLIC_DATA_TREE_HEIGHT,
},
data::PublicDataTreeLeafPreimage,
merkle_tree::MembershipWitness,
Expand All @@ -27,20 +26,3 @@ pub(crate) struct PrivateBaseStateDiffHints {
pub(crate) fee_write_sibling_path: [Field; PUBLIC_DATA_TREE_HEIGHT],
}

pub(crate) struct PublicBaseStateDiffHints {
pub(crate) nullifier_predecessor_preimages: [NullifierLeafPreimage; MAX_NULLIFIERS_PER_TX],
pub(crate) nullifier_predecessor_membership_witnesses: [MembershipWitness<NULLIFIER_TREE_HEIGHT>; MAX_NULLIFIERS_PER_TX],

pub(crate) sorted_nullifiers: [Field; MAX_NULLIFIERS_PER_TX],
pub(crate) sorted_nullifier_indexes: [u32; MAX_NULLIFIERS_PER_TX],

// For inserting the new subtrees into their respective trees:
// Note: the insertion leaf index can be derived from the snapshots' `next_available_leaf_index` values (tree
// snapshots of the relevant trees are stored in partial state reference).
pub(crate) note_hash_subtree_sibling_path: [Field; NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH],
pub(crate) nullifier_subtree_sibling_path: [Field; NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH],

pub(crate) low_public_data_writes_preimages: [PublicDataTreeLeafPreimage; MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX],
pub(crate) low_public_data_writes_witnesses: [MembershipWitness<PUBLIC_DATA_TREE_HEIGHT>; MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX],
pub(crate) public_data_tree_sibling_paths: [[Field; PUBLIC_DATA_TREE_HEIGHT]; MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX],
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
abis::append_only_tree_snapshot::AppendOnlyTreeSnapshot,
constants::TREE_SNAPSHOTS_LENGTH,
partial_state_reference::PartialStateReference,
traits::{Deserialize, Empty, Serialize},
};

Expand All @@ -11,6 +12,16 @@ pub struct TreeSnapshots {
pub public_data_tree: AppendOnlyTreeSnapshot,
}

impl TreeSnapshots {
pub fn to_partial_state_reference(self) -> PartialStateReference {
PartialStateReference {
note_hash_tree: self.note_hash_tree,
nullifier_tree: self.nullifier_tree,
public_data_tree: self.public_data_tree,
}
}
}

impl Eq for TreeSnapshots {
fn eq(self, other: TreeSnapshots) -> bool {
self.l1_to_l2_message_tree.eq(other.l1_to_l2_message_tree)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::{
public_log::PublicLog,
read_request::{ReadRequest, ScopedReadRequest},
side_effect::{Counted, OrderedValue, scoped::Scoped},
tree_snapshots::TreeSnapshots,
tube::{PrivateTubeData, PublicTubeData},
tx_constant_data::TxConstantData,
validation_requests::{
Expand Down Expand Up @@ -163,6 +164,8 @@ pub struct FixtureBuilder {

// Tree snapshots.
pub archive_tree: AppendOnlyTreeSnapshot,
pub start_snapshots: TreeSnapshots,
pub end_snapshots: TreeSnapshots,

// Counters.
pub min_revertible_side_effect_counter: u32,
Expand Down Expand Up @@ -569,6 +572,8 @@ impl FixtureBuilder {
result.public_inputs.reverted = reverted;
result.public_inputs.global_variables = self.global_variables;
result.public_inputs.accumulated_data = self.to_avm_accumulated_data();
result.public_inputs.start_tree_snapshots = self.start_snapshots;
result.public_inputs.end_tree_snapshots = self.end_snapshots;

result
}
Expand Down Expand Up @@ -1138,6 +1143,8 @@ impl Empty for FixtureBuilder {
protocol_contract_tree_root: 0,
protocol_contract_sibling_path: [0; PROTOCOL_CONTRACT_TREE_HEIGHT],
archive_tree: AppendOnlyTreeSnapshot::zero(),
start_snapshots: TreeSnapshots::empty(),
end_snapshots: TreeSnapshots::empty(),
revert_code: 0,
min_revertible_side_effect_counter: 0,
counter_start: 0,
Expand Down
18 changes: 2 additions & 16 deletions yarn-project/circuits.js/src/structs/rollup/base_rollup_hints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { MembershipWitness } from '../membership_witness.js';
import { PartialStateReference } from '../partial_state_reference.js';
import { PublicDataHint } from '../public_data_hint.js';
import { ConstantRollupData } from './constant_rollup_data.js';
import { PrivateBaseStateDiffHints, PublicBaseStateDiffHints } from './state_diff_hints.js';
import { PrivateBaseStateDiffHints } from './state_diff_hints.js';

export type BaseRollupHints = PrivateBaseRollupHints | PublicBaseRollupHints;

Expand Down Expand Up @@ -94,12 +94,8 @@ export class PrivateBaseRollupHints {

export class PublicBaseRollupHints {
constructor(
/** Partial state reference at the start of the rollup. */
public start: PartialStateReference,
/** Sponge state to absorb blob inputs at the start of the rollup. */
public startSpongeBlob: SpongeBlob,
/** Hints used while proving state diff validity. */
public stateDiffHints: PublicBaseStateDiffHints,
/**
* Membership witnesses of blocks referred by each of the 2 kernels.
*/
Expand All @@ -115,13 +111,7 @@ export class PublicBaseRollupHints {
}

static getFields(fields: FieldsOf<PublicBaseRollupHints>) {
return [
fields.start,
fields.startSpongeBlob,
fields.stateDiffHints,
fields.archiveRootMembershipWitness,
fields.constants,
] as const;
return [fields.startSpongeBlob, fields.archiveRootMembershipWitness, fields.constants] as const;
}

/**
Expand All @@ -143,9 +133,7 @@ export class PublicBaseRollupHints {
static fromBuffer(buffer: Buffer | BufferReader): PublicBaseRollupHints {
const reader = BufferReader.asReader(buffer);
return new PublicBaseRollupHints(
reader.readObject(PartialStateReference),
reader.readObject(SpongeBlob),
reader.readObject(PublicBaseStateDiffHints),
MembershipWitness.fromBuffer(reader, ARCHIVE_HEIGHT),
reader.readObject(ConstantRollupData),
);
Expand All @@ -157,9 +145,7 @@ export class PublicBaseRollupHints {

static empty() {
return new PublicBaseRollupHints(
PartialStateReference.empty(),
SpongeBlob.empty(),
PublicBaseStateDiffHints.empty(),
MembershipWitness.empty(ARCHIVE_HEIGHT),
ConstantRollupData.empty(),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { makePrivateBaseStateDiffHints, makePublicBaseStateDiffHints } from '../../tests/factories.js';
import { PrivateBaseStateDiffHints, PublicBaseStateDiffHints } from './state_diff_hints.js';
import { makePrivateBaseStateDiffHints } from '../../tests/factories.js';
import { PrivateBaseStateDiffHints } from './state_diff_hints.js';

describe('StateDiffHints', () => {
it('serializes private hints to buffer and deserializes it back', () => {
Expand All @@ -8,11 +8,4 @@ describe('StateDiffHints', () => {
const res = PrivateBaseStateDiffHints.fromBuffer(buffer);
expect(res).toEqual(expected);
});

it('serializes public hints to buffer and deserializes it back', () => {
const expected = makePublicBaseStateDiffHints();
const buffer = expected.toBuffer();
const res = PublicBaseStateDiffHints.fromBuffer(buffer);
expect(res).toEqual(expected);
});
});
127 changes: 0 additions & 127 deletions yarn-project/circuits.js/src/structs/rollup/state_diff_hints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { type FieldsOf } from '@aztec/foundation/types';

import {
MAX_NULLIFIERS_PER_TX,
MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH,
NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH,
NULLIFIER_TREE_HEIGHT,
Expand Down Expand Up @@ -125,129 +124,3 @@ export class PrivateBaseStateDiffHints {
);
}
}

export class PublicBaseStateDiffHints {
constructor(
/**
* The nullifiers which need to be updated to perform the batch insertion of the new nullifiers.
* See `StandardIndexedTree.batchInsert` function for more details.
*/
public nullifierPredecessorPreimages: Tuple<NullifierLeafPreimage, typeof MAX_NULLIFIERS_PER_TX>,
/**
* Membership witnesses for the nullifiers which need to be updated to perform the batch insertion of the new
* nullifiers.
*/
public nullifierPredecessorMembershipWitnesses: Tuple<
MembershipWitness<typeof NULLIFIER_TREE_HEIGHT>,
typeof MAX_NULLIFIERS_PER_TX
>,
/**
* The nullifiers to be inserted in the tree, sorted high to low.
*/
public sortedNullifiers: Tuple<Fr, typeof MAX_NULLIFIERS_PER_TX>,
/**
* The indexes of the sorted nullifiers to the original ones.
*/
public sortedNullifierIndexes: Tuple<number, typeof MAX_NULLIFIERS_PER_TX>,
/**
* Sibling path "pointing to" where the new note hash subtree should be inserted into the note hash tree.
*/
public noteHashSubtreeSiblingPath: Tuple<Fr, typeof NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH>,
/**
* Sibling path "pointing to" where the new nullifiers subtree should be inserted into the nullifier tree.
*/
public nullifierSubtreeSiblingPath: Tuple<Fr, typeof NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH>,

/**
* Preimages of the low leaves for the public data writes
*/
public lowPublicDataWritesPreimages: Tuple<
PublicDataTreeLeafPreimage,
typeof MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
>,

/**
* Membership witnesses for the low leaves for the public data writes
*/
public lowPublicDataWritesMembershipWitnesses: Tuple<
MembershipWitness<typeof PUBLIC_DATA_TREE_HEIGHT>,
typeof MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
>,

/**
* Sibling paths "pointing to" where the new public data writes should be inserted (one by one) into the public data tree.
*/
public publicDataTreeSiblingPaths: Tuple<
Tuple<Fr, typeof PUBLIC_DATA_TREE_HEIGHT>,
typeof MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
>,
) {}

static from(fields: FieldsOf<PublicBaseStateDiffHints>): PublicBaseStateDiffHints {
return new PublicBaseStateDiffHints(...PublicBaseStateDiffHints.getFields(fields));
}

static getFields(fields: FieldsOf<PublicBaseStateDiffHints>) {
return [
fields.nullifierPredecessorPreimages,
fields.nullifierPredecessorMembershipWitnesses,
fields.sortedNullifiers,
fields.sortedNullifierIndexes,
fields.noteHashSubtreeSiblingPath,
fields.nullifierSubtreeSiblingPath,
fields.lowPublicDataWritesPreimages,
fields.lowPublicDataWritesMembershipWitnesses,
fields.publicDataTreeSiblingPaths,
] as const;
}

/**
* Serializes the state diff hints to a buffer.
* @returns A buffer of the serialized state diff hints.
*/
toBuffer(): Buffer {
return serializeToBuffer(...PublicBaseStateDiffHints.getFields(this));
}

/**
* Deserializes the state diff hints from a buffer.
* @param buffer - A buffer to deserialize from.
* @returns A new PublicBaseStateDiffHints instance.
*/
static fromBuffer(buffer: Buffer | BufferReader): PublicBaseStateDiffHints {
const reader = BufferReader.asReader(buffer);
return new PublicBaseStateDiffHints(
reader.readArray(MAX_NULLIFIERS_PER_TX, NullifierLeafPreimage),
reader.readArray(MAX_NULLIFIERS_PER_TX, {
fromBuffer: buffer => MembershipWitness.fromBuffer(buffer, NULLIFIER_TREE_HEIGHT),
}),
reader.readArray(MAX_NULLIFIERS_PER_TX, Fr),
reader.readNumbers(MAX_NULLIFIERS_PER_TX),
reader.readArray(NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH, Fr),
reader.readArray(NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH, Fr),
reader.readArray(MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataTreeLeafPreimage),
reader.readArray(MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, {
fromBuffer: buffer => MembershipWitness.fromBuffer(buffer, PUBLIC_DATA_TREE_HEIGHT),
}),
reader.readArray(MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, {
fromBuffer(reader) {
return BufferReader.asReader(reader).readArray(PUBLIC_DATA_TREE_HEIGHT, Fr);
},
}),
);
}

static empty() {
return new PublicBaseStateDiffHints(
makeTuple(MAX_NULLIFIERS_PER_TX, NullifierLeafPreimage.empty),
makeTuple(MAX_NULLIFIERS_PER_TX, () => MembershipWitness.empty(NULLIFIER_TREE_HEIGHT)),
makeTuple(MAX_NULLIFIERS_PER_TX, Fr.zero),
makeTuple(MAX_NULLIFIERS_PER_TX, () => 0),
makeTuple(NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH, Fr.zero),
makeTuple(NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH, Fr.zero),
makeTuple(MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataTreeLeafPreimage.empty),
makeTuple(MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, () => MembershipWitness.empty(PUBLIC_DATA_TREE_HEIGHT)),
makeTuple(MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, () => makeTuple(PUBLIC_DATA_TREE_HEIGHT, Fr.zero)),
);
}
}
Loading

0 comments on commit 5d12f94

Please sign in to comment.