Skip to content

Commit

Permalink
feat: circuit optimized indexed tree batch insertion (#3367)
Browse files Browse the repository at this point in the history
Changes the batch insertion algorithm for indexed trees to a
circuit-optimized one.
  • Loading branch information
sirasistant authored Nov 30, 2023
1 parent 11f754d commit 187d2f7
Show file tree
Hide file tree
Showing 15 changed files with 472 additions and 372 deletions.
10 changes: 10 additions & 0 deletions yarn-project/circuits.js/src/structs/rollup/base_rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ export class BaseRollupInputs {
*/
public startHistoricBlocksTreeSnapshot: AppendOnlyTreeSnapshot,

/**
* The nullifiers to be inserted in the tree, sorted high to low.
*/
public sortedNewNullifiers: Tuple<Fr, typeof MAX_NEW_NULLIFIERS_PER_BASE_ROLLUP>,
/**
* The indexes of the sorted nullifiers to the original ones.
*/
public sortednewNullifiersIndexes: Tuple<UInt32, typeof MAX_NEW_NULLIFIERS_PER_BASE_ROLLUP>,
/**
* The nullifiers which need to be updated to perform the batch insertion of the new nullifiers.
* See `StandardIndexedTree.batchInsert` function for more details.
Expand Down Expand Up @@ -210,6 +218,8 @@ export class BaseRollupInputs {
fields.startContractTreeSnapshot,
fields.startPublicDataTreeRoot,
fields.startHistoricBlocksTreeSnapshot,
fields.sortedNewNullifiers,
fields.sortednewNullifiersIndexes,
fields.lowNullifierLeafPreimages,
fields.lowNullifierMembershipWitness,
fields.newCommitmentsSubtreeSiblingPath,
Expand Down
11 changes: 8 additions & 3 deletions yarn-project/circuits.js/src/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,20 +920,23 @@ export function makeBaseRollupInputs(seed = 0): BaseRollupInputs {
const newNullifiersSubtreeSiblingPath = makeTuple(NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH, fr, seed + 0x4000);
const newContractsSubtreeSiblingPath = makeTuple(CONTRACT_SUBTREE_SIBLING_PATH_LENGTH, fr, seed + 0x5000);

const sortedNewNullifiers = makeTuple(MAX_NEW_NULLIFIERS_PER_BASE_ROLLUP, fr, seed + 0x6000);
const sortednewNullifiersIndexes = makeTuple(MAX_NEW_NULLIFIERS_PER_BASE_ROLLUP, i => i, seed + 0x7000);

const newPublicDataUpdateRequestsSiblingPaths = makeTuple(
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_BASE_ROLLUP,
x => makeTuple(PUBLIC_DATA_TREE_HEIGHT, fr, x),
seed + 0x6000,
seed + 0x8000,
);

const newPublicDataReadsSiblingPaths = makeTuple(
MAX_PUBLIC_DATA_READS_PER_BASE_ROLLUP,
x => makeTuple(PUBLIC_DATA_TREE_HEIGHT, fr, x),
seed + 0x6000,
seed + 0x8000,
);

const historicBlocksTreeRootMembershipWitnesses = makeTuple(KERNELS_PER_BASE_ROLLUP, x =>
makeMembershipWitness(HISTORIC_BLOCKS_TREE_HEIGHT, seed + x * 0x1000 + 0x7000),
makeMembershipWitness(HISTORIC_BLOCKS_TREE_HEIGHT, seed + x * 0x1000 + 0x9000),
);

const constants = makeConstantBaseRollupData(0x100);
Expand All @@ -946,6 +949,8 @@ export function makeBaseRollupInputs(seed = 0): BaseRollupInputs {
startContractTreeSnapshot,
startPublicDataTreeRoot,
startHistoricBlocksTreeSnapshot,
sortedNewNullifiers,
sortednewNullifiersIndexes,
lowNullifierLeafPreimages,
newCommitmentsSubtreeSiblingPath,
newNullifiersSubtreeSiblingPath,
Expand Down
27 changes: 23 additions & 4 deletions yarn-project/merkle-tree/src/interfaces/indexed_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@ import { LeafData, SiblingPath } from '@aztec/types';
import { LowLeafWitnessData } from '../index.js';
import { AppendOnlyTree } from './append_only_tree.js';

/**
* The result of a batch insertion in an indexed merkle tree.
*/
export interface BatchInsertionResult<TreeHeight extends number, SubtreeSiblingPathHeight extends number> {
/**
* Data for the leaves to be updated when inserting the new ones.
*/
lowLeavesWitnessData?: LowLeafWitnessData<TreeHeight>[];
/**
* Sibling path "pointing to" where the new subtree should be inserted into the tree.
*/
newSubtreeSiblingPath: SiblingPath<SubtreeSiblingPathHeight>;
/**
* The new leaves being inserted in high to low order. This order corresponds with the order of the low leaves witness.
*/
sortedNewLeaves: Buffer[];
/**
* The indexes of the sorted new leaves to the original ones.
*/
sortedNewLeavesIndexes: number[];
}

/**
* Indexed merkle tree.
*/
Expand Down Expand Up @@ -45,8 +67,5 @@ export interface IndexedTree extends AppendOnlyTree {
leaves: Buffer[],
subtreeHeight: SubtreeHeight,
includeUncommitted: boolean,
): Promise<
| [LowLeafWitnessData<TreeHeight>[], SiblingPath<SubtreeSiblingPathHeight>]
| [undefined, SiblingPath<SubtreeSiblingPathHeight>]
>;
): Promise<BatchInsertionResult<TreeHeight, SubtreeSiblingPathHeight>>;
}
Loading

0 comments on commit 187d2f7

Please sign in to comment.