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

feat: StateDiffHints #3919

Merged
merged 2 commits into from
Jan 11, 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
1 change: 1 addition & 0 deletions yarn-project/circuits.js/src/structs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export * from './rollup/previous_rollup_data.js';
export * from './rollup/root_rollup.js';
export * from './shared.js';
export * from './side_effects.js';
export * from './rollup/state_diff_hints.js';
export * from './tx_context.js';
export * from './tx_request.js';
export * from './verification_key.js';
Expand Down
64 changes: 6 additions & 58 deletions yarn-project/circuits.js/src/structs/rollup/base_rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@ import { BufferReader, Tuple } from '@aztec/foundation/serialize';

import {
ARCHIVE_HEIGHT,
CONTRACT_SUBTREE_SIBLING_PATH_LENGTH,
MAX_NEW_NULLIFIERS_PER_TX,
MAX_PUBLIC_DATA_READS_PER_TX,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH,
NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH,
NULLIFIER_TREE_HEIGHT,
PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH,
PUBLIC_DATA_TREE_HEIGHT,
} from '../../constants.gen.js';
import { FieldsOf } from '../../utils/jsUtils.js';
Expand All @@ -23,6 +17,7 @@ import { UInt32 } from '../shared.js';
import { AppendOnlyTreeSnapshot } from './append_only_tree_snapshot.js';
import { NullifierLeaf, NullifierLeafPreimage } from './nullifier_leaf/index.js';
import { PublicDataTreeLeaf, PublicDataTreeLeafPreimage } from './public_data_leaf/index.js';
import { StateDiffHints } from './state_diff_hints.js';

export { NullifierLeaf, NullifierLeafPreimage, PublicDataTreeLeaf, PublicDataTreeLeafPreimage };

Expand Down Expand Up @@ -93,48 +88,13 @@ export class ConstantRollupData {
*/
export class BaseRollupInputs {
constructor(
/**
* Data of the 2 kernels that preceded this base rollup circuit.
*/
/** Data of the 2 kernels that preceded this base rollup circuit. */
public kernelData: PreviousKernelData,
/**
* Partial state reference at the start of the rollup.
*/
/** Partial state reference at the start of the rollup. */
public start: PartialStateReference,
/**
* The nullifiers to be inserted in the tree, sorted high to low.
*/
public sortedNewNullifiers: Tuple<Fr, typeof MAX_NEW_NULLIFIERS_PER_TX>,
/**
* The indexes of the sorted nullifiers to the original ones.
*/
public sortedNewNullifiersIndexes: Tuple<UInt32, typeof MAX_NEW_NULLIFIERS_PER_TX>,
/**
* The nullifiers which need to be updated to perform the batch insertion of the new nullifiers.
* See `StandardIndexedTree.batchInsert` function for more details.
*/
public lowNullifierLeafPreimages: Tuple<NullifierLeafPreimage, typeof MAX_NEW_NULLIFIERS_PER_TX>,
/**
* Membership witnesses for the nullifiers which need to be updated to perform the batch insertion of the new
* nullifiers.
*/
public lowNullifierMembershipWitness: Tuple<
MembershipWitness<typeof NULLIFIER_TREE_HEIGHT>,
typeof MAX_NEW_NULLIFIERS_PER_TX
>,
/** Hints used while proving state diff validity. */
public stateDiffHints: StateDiffHints,

/**
* Sibling path "pointing to" where the new commitments subtree should be inserted into the note hash tree.
*/
public newCommitmentsSubtreeSiblingPath: 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 newNullifiersSubtreeSiblingPath: Tuple<Fr, typeof NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH>,
/**
* Sibling path "pointing to" where the new contracts subtree should be inserted into the contract tree.
*/
public newContractsSubtreeSiblingPath: Tuple<Fr, typeof CONTRACT_SUBTREE_SIBLING_PATH_LENGTH>,
/**
* The public data writes to be inserted in the tree, sorted high slot to low slot.
*/
Expand All @@ -161,11 +121,6 @@ export class BaseRollupInputs {
typeof MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX
>,

/**
* Sibling path "pointing to" where the new public data subtree should be inserted into the public data tree.
*/
public publicDataWritesSubtreeSiblingPath: Tuple<Fr, typeof PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH>,

/**
* Preimages of leaves which are to be read by the public data reads.
*/
Expand Down Expand Up @@ -197,18 +152,11 @@ export class BaseRollupInputs {
return [
fields.kernelData,
fields.start,
fields.sortedNewNullifiers,
fields.sortedNewNullifiersIndexes,
fields.lowNullifierLeafPreimages,
fields.lowNullifierMembershipWitness,
fields.newCommitmentsSubtreeSiblingPath,
fields.newNullifiersSubtreeSiblingPath,
fields.newContractsSubtreeSiblingPath,
fields.stateDiffHints,
fields.sortedPublicDataWrites,
fields.sortedPublicDataWritesIndexes,
fields.lowPublicDataWritesPreimages,
fields.lowPublicDataWritesMembershipWitnesses,
fields.publicDataWritesSubtreeSiblingPath,
fields.publicDataReadsPreimages,
fields.publicDataReadsMembershipWitnesses,
fields.archiveRootMembershipWitness,
Expand Down
81 changes: 81 additions & 0 deletions yarn-project/circuits.js/src/structs/rollup/state_diff_hints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Fr } from '@aztec/foundation/fields';
import { Tuple } from '@aztec/foundation/serialize';
import { FieldsOf } from '@aztec/foundation/types';

import {
CONTRACT_SUBTREE_SIBLING_PATH_LENGTH,
MAX_NEW_NULLIFIERS_PER_TX,
NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH,
NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH,
NULLIFIER_TREE_HEIGHT,
PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH,
} from '../../constants.gen.js';
import { serializeToBuffer } from '../../utils/serialize.js';
import { MembershipWitness } from '../membership_witness.js';
import { NullifierLeafPreimage } from './nullifier_leaf/index.js';

/**
* Hints used while proving state diff validity.
*/
export class StateDiffHints {
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_NEW_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_NEW_NULLIFIERS_PER_TX
>,
/**
* The nullifiers to be inserted in the tree, sorted high to low.
*/
public sortedNullifiers: Tuple<Fr, typeof MAX_NEW_NULLIFIERS_PER_TX>,
/**
* The indexes of the sorted nullifiers to the original ones.
*/
public sortedNullifierIndexes: Tuple<number, typeof MAX_NEW_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>,
/**
* Sibling path "pointing to" where the new contracts subtree should be inserted into the contract tree.
*/
public contractSubtreeSiblingPath: Tuple<Fr, typeof CONTRACT_SUBTREE_SIBLING_PATH_LENGTH>,
/**
* Sibling path "pointing to" where the new public data subtree should be inserted into the public data tree.
*/
public publicDataSiblingPath: Tuple<Fr, typeof PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH>,
) {}

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

static getFields(fields: FieldsOf<StateDiffHints>) {
return [
fields.nullifierPredecessorPreimages,
fields.nullifierPredecessorMembershipWitnesses,
fields.sortedNullifiers,
fields.sortedNullifierIndexes,
fields.noteHashSubtreeSiblingPath,
fields.nullifierSubtreeSiblingPath,
fields.contractSubtreeSiblingPath,
fields.publicDataSiblingPath,
] as const;
}

toBuffer(): Buffer {
return serializeToBuffer(...StateDiffHints.getFields(this));
}
}
67 changes: 42 additions & 25 deletions yarn-project/circuits.js/src/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import {
RootRollupPublicInputs,
SideEffect,
SideEffectLinkedToNoteHash,
StateDiffHints,
StateReference,
TxContext,
TxRequest,
Expand Down Expand Up @@ -941,33 +942,58 @@ export function makePublicDataTreeLeafPreimage(seed = 0): PublicDataTreeLeafPrei
}

/**
* Makes arbitrary base rollup inputs.
* @param seed - The seed to use for generating the base rollup inputs.
* @returns A base rollup inputs.
* Creates an instance of StateDiffHints with arbitrary values based on the provided seed.
* @param seed - The seed to use for generating the hints.
* @returns A StateDiffHints object.
*/
export function makeBaseRollupInputs(seed = 0): BaseRollupInputs {
const kernelData = makePreviousKernelData(seed);

const start = makePartialStateReference(seed + 0x100);

const lowNullifierLeafPreimages = makeTuple(
export function makeStateDiffHints(seed = 1): StateDiffHints {
const nullifierPredecessorPreimages = makeTuple(
MAX_NEW_NULLIFIERS_PER_TX,
x => new NullifierLeafPreimage(fr(x), fr(x + 0x100), BigInt(x + 0x200)),
seed + 0x1000,
);

const lowNullifierMembershipWitness = makeTuple(
const nullifierPredecessorMembershipWitnesses = makeTuple(
MAX_NEW_NULLIFIERS_PER_TX,
x => makeMembershipWitness(NULLIFIER_TREE_HEIGHT, x),
seed + 0x2000,
);

const newCommitmentsSubtreeSiblingPath = makeTuple(NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH, fr, seed + 0x3000);
const newNullifiersSubtreeSiblingPath = makeTuple(NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH, fr, seed + 0x4000);
const newContractsSubtreeSiblingPath = makeTuple(CONTRACT_SUBTREE_SIBLING_PATH_LENGTH, fr, seed + 0x5000);
const sortedNullifiers = makeTuple(MAX_NEW_NULLIFIERS_PER_TX, fr, seed + 0x3000);

const sortedNullifierIndexes = makeTuple(MAX_NEW_NULLIFIERS_PER_TX, i => i, seed + 0x4000);

const noteHashSubtreeSiblingPath = makeTuple(NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH, fr, seed + 0x5000);

const nullifierSubtreeSiblingPath = makeTuple(NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH, fr, seed + 0x6000);

const sortedNewNullifiers = makeTuple(MAX_NEW_NULLIFIERS_PER_TX, fr, seed + 0x6000);
const sortedNewNullifiersIndexes = makeTuple(MAX_NEW_NULLIFIERS_PER_TX, i => i, seed + 0x7000);
const contractSubtreeSiblingPath = makeTuple(CONTRACT_SUBTREE_SIBLING_PATH_LENGTH, fr, seed + 0x7000);

const publicDataSiblingPath = makeTuple(PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH, fr, 0x8000);

return new StateDiffHints(
nullifierPredecessorPreimages,
nullifierPredecessorMembershipWitnesses,
sortedNullifiers,
sortedNullifierIndexes,
noteHashSubtreeSiblingPath,
nullifierSubtreeSiblingPath,
contractSubtreeSiblingPath,
publicDataSiblingPath,
);
}

/**
* Makes arbitrary base rollup inputs.
* @param seed - The seed to use for generating the base rollup inputs.
* @returns A base rollup inputs.
*/
export function makeBaseRollupInputs(seed = 0): BaseRollupInputs {
const kernelData = makePreviousKernelData(seed);

const start = makePartialStateReference(seed + 0x100);

const stateDiffHints = makeStateDiffHints(seed + 0x600);

const sortedPublicDataWrites = makeTuple(
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
Expand All @@ -989,8 +1015,6 @@ export function makeBaseRollupInputs(seed = 0): BaseRollupInputs {
seed + 0x8400,
);

const publicDataWritesSubtreeSiblingPath = makeTuple(PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH, fr, 0x8600);

const publicDataReadsPreimages = makeTuple(
MAX_PUBLIC_DATA_READS_PER_TX,
makePublicDataTreeLeafPreimage,
Expand All @@ -1009,19 +1033,12 @@ export function makeBaseRollupInputs(seed = 0): BaseRollupInputs {

return BaseRollupInputs.from({
kernelData,
lowNullifierMembershipWitness,
start,
sortedNewNullifiers,
sortedNewNullifiersIndexes,
lowNullifierLeafPreimages,
newCommitmentsSubtreeSiblingPath,
newNullifiersSubtreeSiblingPath,
newContractsSubtreeSiblingPath,
stateDiffHints,
sortedPublicDataWrites,
sortedPublicDataWritesIndexes,
lowPublicDataWritesPreimages,
lowPublicDataWritesMembershipWitnesses,
publicDataWritesSubtreeSiblingPath,
publicDataReadsPreimages,
publicDataReadsMembershipWitnesses,
archiveRootMembershipWitness,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod base_rollup_inputs;
mod state_diff_hints;

use base_rollup_inputs::BaseRollupInputs;
use crate::abis::base_or_merge_rollup_public_inputs::BaseOrMergeRollupPublicInputs;
Loading