diff --git a/circuits b/circuits index b7ec0b28356..6bacd566132 160000 --- a/circuits +++ b/circuits @@ -1 +1 @@ -Subproject commit b7ec0b28356a1f1f96951dfc605b925adf33c2ff +Subproject commit 6bacd566132d8aa937dee914c2e99ff0f62ecf76 diff --git a/yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap b/yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap index d0ed2e77a20..abb56870315 100644 --- a/yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap +++ b/yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap @@ -122,42 +122,8 @@ Object { `; exports[`abis wasm bindings computes contract leaf 1`] = ` -Object { - "data": Array [ - 37, - 164, - 27, - 46, - 63, - 9, - 182, - 227, - 172, - 238, - 84, - 38, - 169, - 47, - 59, - 48, - 111, - 49, - 185, - 159, - 58, - 31, - 73, - 162, - 28, - 126, - 118, - 148, - 55, - 26, - 100, - 78, - ], - "type": "Buffer", +Fr { + "value": 13761632275218847273520262751574664149269181891159611676226614084913136324343n, } `; diff --git a/yarn-project/circuits.js/src/abis/abis.test.ts b/yarn-project/circuits.js/src/abis/abis.test.ts index a03a8306401..4d97d2af8b8 100644 --- a/yarn-project/circuits.js/src/abis/abis.test.ts +++ b/yarn-project/circuits.js/src/abis/abis.test.ts @@ -1,5 +1,6 @@ -import { Fr, FunctionData, NullifierLeafPreimage } from '../index.js'; -import { fr, makeAztecAddress, makeBytes, makeTxRequest, makeVerificationKey } from '../tests/factories.js'; +import { Fr, FunctionData, NewContractData } from '../index.js'; +import { makeEthAddress } from '../tests/factories.js'; +import { makeAztecAddress, makeBytes, makeTxRequest, makeVerificationKey } from '../tests/factories.js'; import { CircuitsWasm } from '../wasm/circuits_wasm.js'; import { computeContractAddress, @@ -72,8 +73,8 @@ describe('abis wasm bindings', () => { }); it('computes contract leaf', async () => { - const leafPreImage = new NullifierLeafPreimage(fr(2), fr(2 + 0x100), 2 + 0x200); - const res = await computeContractLeaf(wasm, leafPreImage); + const cd = new NewContractData(makeAztecAddress(), makeEthAddress(), new Fr(3n)); + const res = await computeContractLeaf(wasm, cd); expect(res).toMatchSnapshot(); }); }); diff --git a/yarn-project/circuits.js/src/abis/abis.ts b/yarn-project/circuits.js/src/abis/abis.ts index b10ff8a5667..9194839789c 100644 --- a/yarn-project/circuits.js/src/abis/abis.ts +++ b/yarn-project/circuits.js/src/abis/abis.ts @@ -1,29 +1,43 @@ import { Buffer } from 'buffer'; import { AztecAddress, Fr, serializeBufferArrayToVector } from '@aztec/foundation'; import { CircuitsWasm } from '../wasm/index.js'; -import { FunctionData, FUNCTION_SELECTOR_NUM_BYTES, NullifierLeafPreimage, TxRequest } from '../index.js'; +import { FunctionData, FUNCTION_SELECTOR_NUM_BYTES, TxRequest, NewContractData } from '../index.js'; import { serializeToBuffer } from '../utils/serialize.js'; +export async function wasmCall( + wasm: CircuitsWasm, + fnName: string, + input: { toBuffer: () => Buffer }, + expectedOutputLength: number, +): Promise { + const inputData = input.toBuffer(); + const outputBuf = wasm.call('bbmalloc', expectedOutputLength); + const inputBuf = wasm.call('bbmalloc', inputData.length); + wasm.writeMemory(inputBuf, inputData); + await wasm.asyncCall(fnName, inputBuf, outputBuf); + const buf = Buffer.from(wasm.getMemorySlice(outputBuf, outputBuf + expectedOutputLength)); + wasm.call('bbfree', outputBuf); + wasm.call('bbfree', inputBuf); + return buf; +} + export async function hashTxRequest(wasm: CircuitsWasm, txRequest: TxRequest) { - const data = txRequest.toBuffer(); wasm.call('pedersen__init'); - wasm.writeMemory(0, data); - await wasm.asyncCall('abis__hash_tx_request', 0, data.length); - return Buffer.from(wasm.getMemorySlice(data.length, data.length + 32)); + return await wasmCall(wasm, 'abis__hash_tx_request', txRequest, 32); } export async function computeFunctionSelector(wasm: CircuitsWasm, funcSig: string) { - const buf = Buffer.from(funcSig); - wasm.writeMemory(0, buf); - await wasm.asyncCall('abis__compute_function_selector', 0, buf.length); - return Buffer.from(wasm.getMemorySlice(buf.length, buf.length + FUNCTION_SELECTOR_NUM_BYTES)); + return await wasmCall( + wasm, + 'abis__compute_function_selector', + { toBuffer: () => Buffer.from(funcSig) }, + FUNCTION_SELECTOR_NUM_BYTES, + ); } export async function hashVK(wasm: CircuitsWasm, vkBuf: Buffer) { wasm.call('pedersen__init'); - wasm.writeMemory(0, vkBuf); - await wasm.asyncCall('abis__hash_vk', 0, vkBuf.length); - return Buffer.from(wasm.getMemorySlice(vkBuf.length, vkBuf.length + 32)); + return await wasmCall(wasm, 'abis__hash_vk', { toBuffer: () => vkBuf }, 32); } export async function computeFunctionLeaf(wasm: CircuitsWasm, fnLeaf: Buffer) { @@ -41,6 +55,28 @@ export async function computeFunctionTreeRoot(wasm: CircuitsWasm, fnLeafs: Buffe return Buffer.from(wasm.getMemorySlice(inputVector.length, inputVector.length + 32)); } +// not yet working +// export async function inputBuffersToOutputBuffer( +// wasm: CircuitsWasm, +// fnName: string, +// buffers: Buffer[], +// expectedOutputLength: number, +// ) { +// const offsets: number[] = []; +// const totalLength = buffers.reduce((total, cur) => { +// offsets.push(total); +// return total + cur.length; +// }, 0); +// const inputBuf = wasm.call('bbmalloc', totalLength); +// const outputBuf = wasm.call('bbmalloc', expectedOutputLength); +// wasm.writeMemory(inputBuf, Buffer.concat(buffers)); +// await wasm.asyncCall(fnName, ...offsets.map(x => x + inputBuf), outputBuf); +// const output = Buffer.from(wasm.getMemorySlice(outputBuf, outputBuf + expectedOutputLength)); +// wasm.call('bbfree', inputBuf); +// wasm.call('bbfree', outputBuf); +// return output; +// } + export async function hashConstructor( wasm: CircuitsWasm, functionData: FunctionData, @@ -59,6 +95,14 @@ export async function hashConstructor( wasm.writeMemory(memLoc2, constructorVKHash); await wasm.asyncCall('abis__hash_constructor', 0, memLoc1, memLoc2, memLoc3); return Buffer.from(wasm.getMemorySlice(memLoc3, memLoc3 + 32)); + + // wasm.call('pedersen__init'); + // return await inputBuffersToOutputBuffer( + // wasm, + // 'abis__hash_constructor', + // [functionData.toBuffer(), serializeToBuffer(args.map(fr => fr.toBuffer())), constructorVKHash], + // 32, + // ); } export async function computeContractAddress( @@ -83,10 +127,8 @@ export async function computeContractAddress( return AztecAddress.fromBuffer(resultBuf); } -export async function computeContractLeaf(wasm: CircuitsWasm, leafPreimage: NullifierLeafPreimage) { - const data = leafPreimage.toBuffer(); +export async function computeContractLeaf(wasm: CircuitsWasm, cd: NewContractData) { wasm.call('pedersen__init'); - wasm.writeMemory(0, leafPreimage.toBuffer()); - await wasm.asyncCall('abis__compute_contract_leaf', 0, data.length); - return Buffer.from(wasm.getMemorySlice(data.length, data.length + 32)); + const value = await wasmCall(wasm, 'abis__compute_contract_leaf', { toBuffer: () => cd.toBuffer() }, 32); + return Fr.fromBuffer(value); } diff --git a/yarn-project/circuits.js/src/rollup/rollup_wasm_wrapper.ts b/yarn-project/circuits.js/src/rollup/rollup_wasm_wrapper.ts index c10bd34ffbd..66c307eb9e7 100644 --- a/yarn-project/circuits.js/src/rollup/rollup_wasm_wrapper.ts +++ b/yarn-project/circuits.js/src/rollup/rollup_wasm_wrapper.ts @@ -1,12 +1,12 @@ -import { BaseRollupInputs, BaseRollupPublicInputs, RootRollupInputs, RootRollupPublicInputs } from '../index.js'; +import { BaseRollupInputs, BaseOrMergeRollupPublicInputs, RootRollupInputs, RootRollupPublicInputs } from '../index.js'; import { uint8ArrayToNum } from '../utils/serialize.js'; import { CircuitsWasm } from '../wasm/circuits_wasm.js'; export class RollupWasmWrapper { constructor(private wasm: CircuitsWasm) {} - public simulateBaseRollup(baseRollupInputs: BaseRollupInputs): Promise { - return this.callWasm('base_rollup__sim', baseRollupInputs, BaseRollupPublicInputs); + public simulateBaseRollup(baseRollupInputs: BaseRollupInputs): Promise { + return this.callWasm('base_rollup__sim', baseRollupInputs, BaseOrMergeRollupPublicInputs); } public simulateRootRollup(rootRollupInputs: RootRollupInputs): Promise { diff --git a/yarn-project/circuits.js/src/structs/__snapshots__/base_rollup.test.ts.snap b/yarn-project/circuits.js/src/structs/__snapshots__/base_rollup.test.ts.snap index 75ffb71fbd8..05b5e054289 100644 --- a/yarn-project/circuits.js/src/structs/__snapshots__/base_rollup.test.ts.snap +++ b/yarn-project/circuits.js/src/structs/__snapshots__/base_rollup.test.ts.snap @@ -329,7 +329,11 @@ merge_rollup_vk_hash: 0x404 `; exports[`structs/base_rollup serializes and prints BaseRollupPublicInputs 1`] = ` -"end_aggregation_object: +"rollup_type: +0 +rollup_subtree_height: +0x0 +end_aggregation_object: P0: { 0x100, 0x101 } P1: { 0x200, 0x201 } public_inputs: [ diff --git a/yarn-project/circuits.js/src/structs/__snapshots__/root_rollup.test.ts.snap b/yarn-project/circuits.js/src/structs/__snapshots__/root_rollup.test.ts.snap index 2fa85ce8095..20a283f61aa 100644 --- a/yarn-project/circuits.js/src/structs/__snapshots__/root_rollup.test.ts.snap +++ b/yarn-project/circuits.js/src/structs/__snapshots__/root_rollup.test.ts.snap @@ -1,7 +1,11 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`structs/root_rollup serializes a RootRollupInput and prints it 1`] = ` -"previous_rollup_data: [ merge_rollup_public_inputs: end_aggregation_object: +"previous_rollup_data: [ base_or_merge_rollup_public_inputs: rollup_type: +0 +rollup_subtree_height: +0x0 +end_aggregation_object: P0: { 0x100, 0x101 } P1: { 0x200, 0x201 } public_inputs: [ @@ -58,20 +62,16 @@ next_available_leaf_index: 800 calldata_hash: [ 0x901 0x902 ] proof: [ 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 ] -vk: key.composer_type: 0 -key.circuit_size: 65 -key.num_public_inputs: 66 -key.commitments: [ - A: { 0x200, 0x300 } -] -key.contains_recursive_proof: 1 -key.recursive_proof_public_input_indices: [ 190 191 192 193 194 ] - +vk: 0x12b300 vk_index: 110 vk_sibling_path: leaf_index: 120 sibling_path: [ 0x120 0x121 0x122 0x123 0x124 0x125 0x126 0x127 ] - merge_rollup_public_inputs: end_aggregation_object: + base_or_merge_rollup_public_inputs: rollup_type: +0 +rollup_subtree_height: +0x0 +end_aggregation_object: P0: { 0x1100, 0x1101 } P1: { 0x1200, 0x1201 } public_inputs: [ @@ -128,15 +128,7 @@ next_available_leaf_index: 1800 calldata_hash: [ 0x1901 0x1902 ] proof: [ 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 ] -vk: key.composer_type: 0 -key.circuit_size: 65 -key.num_public_inputs: 66 -key.commitments: [ - A: { 0x200, 0x300 } -] -key.contains_recursive_proof: 1 -key.recursive_proof_public_input_indices: [ 190 191 192 193 194 ] - +vk: 0x133e40 vk_index: 1110 vk_sibling_path: leaf_index: 1120 sibling_path: [ 0x1120 0x1121 0x1122 0x1123 0x1124 0x1125 0x1126 0x1127 ] diff --git a/yarn-project/circuits.js/src/structs/base_rollup.test.ts b/yarn-project/circuits.js/src/structs/base_rollup.test.ts index 10644e1fdbb..1b41c07071b 100644 --- a/yarn-project/circuits.js/src/structs/base_rollup.test.ts +++ b/yarn-project/circuits.js/src/structs/base_rollup.test.ts @@ -1,6 +1,6 @@ import { expectReserializeToMatchObject, expectSerializeToMatchSnapshot } from '../tests/expectSerialize.js'; import { makeBaseRollupInputs, makeBaseRollupPublicInputs } from '../tests/factories.js'; -import { BaseRollupPublicInputs } from './base_rollup.js'; +import { BaseOrMergeRollupPublicInputs } from './base_rollup.js'; describe('structs/base_rollup', () => { it(`serializes and prints BaseRollupInputs`, async () => { @@ -16,7 +16,7 @@ describe('structs/base_rollup', () => { await expectSerializeToMatchSnapshot( baseRollupPublicInputs.toBuffer(), - 'abis__test_roundtrip_serialize_base_rollup_public_inputs', + 'abis__test_roundtrip_serialize_base_or_merge_rollup_public_inputs', ); }); @@ -25,8 +25,8 @@ describe('structs/base_rollup', () => { await expectReserializeToMatchObject( baseRollupPublicInputs, - 'abis__test_roundtrip_reserialize_base_rollup_public_inputs', - BaseRollupPublicInputs.fromBuffer, + 'abis__test_roundtrip_reserialize_base_or_merge_rollup_public_inputs', + BaseOrMergeRollupPublicInputs.fromBuffer, ); }); }); diff --git a/yarn-project/circuits.js/src/structs/base_rollup.ts b/yarn-project/circuits.js/src/structs/base_rollup.ts index fbd2a508f58..cf36092f5b8 100644 --- a/yarn-project/circuits.js/src/structs/base_rollup.ts +++ b/yarn-project/circuits.js/src/structs/base_rollup.ts @@ -12,7 +12,7 @@ import { PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT, } from './constants.js'; import { PreviousKernelData } from './kernel.js'; -import { AggregationObject, MembershipWitness, UInt32 } from './shared.js'; +import { AggregationObject, MembershipWitness, RollupTypes, UInt32 } from './shared.js'; export class NullifierLeafPreimage { constructor(public leafValue: Fr, public nextValue: Fr, public nextIndex: UInt32) {} @@ -168,8 +168,10 @@ export class BaseRollupInputs { /** * Output of the base rollup circuit */ -export class BaseRollupPublicInputs { +export class BaseOrMergeRollupPublicInputs { constructor( + public rollupType: RollupTypes, + public rollupSubTreeHeight: Fr, public endAggregationObject: AggregationObject, public constants: ConstantBaseRollupData, @@ -190,9 +192,11 @@ export class BaseRollupPublicInputs { * Deserializes from a buffer or reader, corresponding to a write in cpp. * @param bufferReader - Buffer to read from. */ - static fromBuffer(buffer: Buffer | BufferReader): BaseRollupPublicInputs { + static fromBuffer(buffer: Buffer | BufferReader): BaseOrMergeRollupPublicInputs { const reader = BufferReader.asReader(buffer); - return new BaseRollupPublicInputs( + return new BaseOrMergeRollupPublicInputs( + reader.readNumber(), + reader.readFr(), reader.readObject(AggregationObject), reader.readObject(ConstantBaseRollupData), reader.readObject(AppendOnlyTreeSnapshot), @@ -211,6 +215,8 @@ export class BaseRollupPublicInputs { */ toBuffer() { return serializeToBuffer( + this.rollupType, + this.rollupSubTreeHeight, this.endAggregationObject, this.constants, diff --git a/yarn-project/circuits.js/src/structs/merge_rollup.ts b/yarn-project/circuits.js/src/structs/merge_rollup.ts index fd562badb89..9520b4bb23a 100644 --- a/yarn-project/circuits.js/src/structs/merge_rollup.ts +++ b/yarn-project/circuits.js/src/structs/merge_rollup.ts @@ -1,14 +1,14 @@ import { Fr } from '@aztec/foundation'; import { FieldsOf } from '../utils/jsUtils.js'; import { serializeToBuffer } from '../utils/serialize.js'; -import { AppendOnlyTreeSnapshot, BaseRollupPublicInputs, ConstantBaseRollupData } from './base_rollup.js'; +import { AppendOnlyTreeSnapshot, BaseOrMergeRollupPublicInputs, ConstantBaseRollupData } from './base_rollup.js'; import { ROLLUP_VK_TREE_HEIGHT } from './constants.js'; import { AggregationObject, MembershipWitness, RollupTypes, UInt32, UInt8Vector } from './shared.js'; import { VerificationKey } from './verification_key.js'; export class PreviousRollupData { constructor( - public publicInputs: BaseRollupPublicInputs | MergeRollupPublicInputs, + public publicInputs: BaseOrMergeRollupPublicInputs | MergeRollupPublicInputs, public proof: UInt8Vector, public vk: VerificationKey, /** diff --git a/yarn-project/circuits.js/src/tests/factories.ts b/yarn-project/circuits.js/src/tests/factories.ts index 3a244c3b33b..38270a3af31 100644 --- a/yarn-project/circuits.js/src/tests/factories.ts +++ b/yarn-project/circuits.js/src/tests/factories.ts @@ -9,7 +9,7 @@ import { import { AppendOnlyTreeSnapshot, BaseRollupInputs, - BaseRollupPublicInputs, + BaseOrMergeRollupPublicInputs, ConstantBaseRollupData, NullifierLeafPreimage, } from '../structs/base_rollup.js'; @@ -57,6 +57,7 @@ import { ComposerType, EcdsaSignature, MembershipWitness, + RollupTypes, UInt8Vector, } from '../structs/shared.js'; import { ContractDeploymentData, SignedTxRequest, TxContext, TxRequest } from '../structs/tx.js'; @@ -264,7 +265,9 @@ export function makeEcdsaSignature(seed = 1): EcdsaSignature { } export function makeBaseRollupPublicInputs(seed = 0) { - return new BaseRollupPublicInputs( + return new BaseOrMergeRollupPublicInputs( + RollupTypes.Base, + new Fr(0n), makeAggregationObject(seed + 0x100), makeConstantBaseRollupData(seed + 0x200), makeAppendOnlyTreeSnapshot(seed + 0x300), diff --git a/yarn-project/sequencer-client/src/block_builder/circuit_powered_block_builder.test.ts b/yarn-project/sequencer-client/src/block_builder/circuit_powered_block_builder.test.ts index 2d7944b1541..2a41d92ced3 100644 --- a/yarn-project/sequencer-client/src/block_builder/circuit_powered_block_builder.test.ts +++ b/yarn-project/sequencer-client/src/block_builder/circuit_powered_block_builder.test.ts @@ -1,7 +1,7 @@ import { AppendOnlyTreeSnapshot, BaseRollupInputs, - BaseRollupPublicInputs, + BaseOrMergeRollupPublicInputs, CircuitsWasm, Fr, RootRollupPublicInputs, @@ -19,13 +19,14 @@ import { MockProxy, mock } from 'jest-mock-extended'; import { default as levelup } from 'levelup'; import flatMap from 'lodash.flatmap'; import { default as memdown } from 'memdown'; -import { hashNewContractData, makeEmptyTx, makeEmptyUnverifiedData } from '../deps/tx.js'; +import { makeEmptyTx, makeEmptyUnverifiedData } from '../deps/tx.js'; import { VerificationKeys, getVerificationKeys } from '../deps/verification_keys.js'; import { EmptyProver } from '../prover/empty.js'; import { Prover } from '../prover/index.js'; import { Simulator } from '../simulator/index.js'; import { WasmCircuitSimulator } from '../simulator/wasm.js'; import { CircuitPoweredBlockBuilder } from './circuit_powered_block_builder.js'; +import { computeContractLeaf } from '@aztec/circuits.js/abis'; /* eslint-disable @typescript-eslint/ban-ts-comment */ // @ts-ignore @@ -41,8 +42,8 @@ describe('sequencer/circuit_block_builder', () => { let prover: MockProxy; let blockNumber: number; - let baseRollupOutputLeft: BaseRollupPublicInputs; - let baseRollupOutputRight: BaseRollupPublicInputs; + let baseRollupOutputLeft: BaseOrMergeRollupPublicInputs; + let baseRollupOutputRight: BaseOrMergeRollupPublicInputs; let rootRollupOutput: RootRollupPublicInputs; let wasm: CircuitsWasm; @@ -93,9 +94,12 @@ describe('sequencer/circuit_block_builder', () => { // Updates the expectedDb trees based on the new commitments, contracts, and nullifiers from these txs const updateExpectedTreesFromTxs = async (txs: Tx[]) => { + const newContracts = await Promise.all( + flatMap(txs, tx => tx.data.end.newContracts.map(async n => await computeContractLeaf(wasm, n))), + ); for (const [tree, leaves] of [ [MerkleTreeId.DATA_TREE, flatMap(txs, tx => tx.data.end.newCommitments.map(l => l.toBuffer()))], - [MerkleTreeId.CONTRACT_TREE, flatMap(txs, tx => tx.data.end.newContracts.map(n => hashNewContractData(wasm, n)))], + [MerkleTreeId.CONTRACT_TREE, newContracts.map(x => x.toBuffer())], [MerkleTreeId.NULLIFIER_TREE, flatMap(txs, tx => tx.data.end.newNullifiers.map(l => l.toBuffer()))], ] as const) { await expectsDb.appendLeaves(tree, leaves); diff --git a/yarn-project/sequencer-client/src/block_builder/circuit_powered_block_builder.ts b/yarn-project/sequencer-client/src/block_builder/circuit_powered_block_builder.ts index 4eabd13bc1b..902a743e39c 100644 --- a/yarn-project/sequencer-client/src/block_builder/circuit_powered_block_builder.ts +++ b/yarn-project/sequencer-client/src/block_builder/circuit_powered_block_builder.ts @@ -1,7 +1,7 @@ import { AppendOnlyTreeSnapshot, BaseRollupInputs, - BaseRollupPublicInputs, + BaseOrMergeRollupPublicInputs, CircuitsWasm, ConstantBaseRollupData, CONTRACT_TREE_ROOTS_TREE_HEIGHT, @@ -11,7 +11,6 @@ import { PreviousKernelData, PreviousRollupData, PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT, - PRIVATE_DATA_TREE_HEIGHT, ROLLUP_VK_TREE_HEIGHT, RootRollupInputs, RootRollupPublicInputs, @@ -24,11 +23,12 @@ import { Tx } from '@aztec/tx'; import { MerkleTreeId, MerkleTreeOperations } from '@aztec/world-state'; import flatMap from 'lodash.flatmap'; import times from 'lodash.times'; -import { hashNewContractData, makeEmptyTx } from '../deps/tx.js'; +import { makeEmptyTx } from '../deps/tx.js'; import { VerificationKeys } from '../deps/verification_keys.js'; import { Proof, Prover } from '../prover/index.js'; import { Simulator } from '../simulator/index.js'; import { ContractData, L2Block } from '@aztec/l2-block'; +import { computeContractLeaf } from '@aztec/circuits.js/abis'; const frToBigInt = (fr: Fr) => toBigIntBE(fr.toBuffer()); const bigintToFr = (num: bigint) => new Fr(num); @@ -102,8 +102,8 @@ export class CircuitPoweredBlockBuilder { // Collect all new nullifiers, commitments, and contracts from all txs in this block const newNullifiers = flatMap(txs, tx => tx.data.end.newNullifiers); const newCommitments = flatMap(txs, tx => tx.data.end.newCommitments); - const newContracts = flatMap(txs, tx => tx.data.end.newContracts).map(cd => - Fr.fromBuffer(hashNewContractData(this.wasm, cd)), + const newContracts = await Promise.all( + flatMap(txs, tx => tx.data.end.newContracts).map(async cd => await computeContractLeaf(this.wasm, cd)), ); const newContractData = flatMap(txs, tx => tx.data.end.newContracts).map( n => new ContractData(n.contractAddress, n.portalContractAddress), @@ -194,7 +194,7 @@ export class CircuitPoweredBlockBuilder { } // Validate that the new roots we calculated from manual insertions match the outputs of the simulation - protected async validateTrees(rollupOutput: BaseRollupPublicInputs | RootRollupPublicInputs) { + protected async validateTrees(rollupOutput: BaseOrMergeRollupPublicInputs | RootRollupPublicInputs) { await Promise.all([ this.validateTree(rollupOutput, MerkleTreeId.CONTRACT_TREE, 'Contract'), this.validateTree(rollupOutput, MerkleTreeId.DATA_TREE, 'PrivateData'), @@ -224,7 +224,7 @@ export class CircuitPoweredBlockBuilder { // Helper for validating a non-roots tree against a circuit simulation output protected async validateTree( - output: BaseRollupPublicInputs | RootRollupPublicInputs, + output: BaseOrMergeRollupPublicInputs | RootRollupPublicInputs, treeId: MerkleTreeId, name: 'PrivateData' | 'Contract' | 'Nullifier', ) { @@ -254,9 +254,9 @@ export class CircuitPoweredBlockBuilder { // Builds the inputs for the root rollup circuit, without making any changes to trees protected async getRootRollupInput( - rollupOutputLeft: BaseRollupPublicInputs, + rollupOutputLeft: BaseOrMergeRollupPublicInputs, rollupProofLeft: Proof, - rollupOutputRight: BaseRollupPublicInputs, + rollupOutputRight: BaseOrMergeRollupPublicInputs, rollupProofRight: Proof, ) { const previousRollupData: RootRollupInputs['previousRollupData'] = [ @@ -284,7 +284,7 @@ export class CircuitPoweredBlockBuilder { }); } - protected getPreviousRollupDataFromBaseRollup(rollupOutput: BaseRollupPublicInputs, rollupProof: Proof) { + protected getPreviousRollupDataFromBaseRollup(rollupOutput: BaseOrMergeRollupPublicInputs, rollupProof: Proof) { return new PreviousRollupData( rollupOutput, rollupProof, @@ -548,12 +548,14 @@ export class CircuitPoweredBlockBuilder { // Update the contract and data trees with the new items being inserted to get the new roots // that will be used by the next iteration of the base rollup circuit, skipping the empty ones - const newContracts = flatMap([tx1, tx2], tx => - tx.data.end.newContracts.map(cd => hashNewContractData(this.wasm, cd)), + const newContracts = await Promise.all( + flatMap([tx1, tx2], tx => tx.data.end.newContracts.map(async cd => await computeContractLeaf(this.wasm, cd))), ); const newCommitments = flatMap([tx1, tx2], tx => tx.data.end.newCommitments.map(x => x.toBuffer())); - - await this.db.appendLeaves(MerkleTreeId.CONTRACT_TREE, newContracts); + await this.db.appendLeaves( + MerkleTreeId.CONTRACT_TREE, + newContracts.map(x => x.toBuffer()), + ); await this.db.appendLeaves(MerkleTreeId.DATA_TREE, newCommitments); diff --git a/yarn-project/sequencer-client/src/deps/tx.ts b/yarn-project/sequencer-client/src/deps/tx.ts index 0c392ffe648..650e622ed79 100644 --- a/yarn-project/sequencer-client/src/deps/tx.ts +++ b/yarn-project/sequencer-client/src/deps/tx.ts @@ -1,6 +1,4 @@ -import { pedersenCompressInputs } from '@aztec/barretenberg.js/crypto'; -import { BarretenbergWasm } from '@aztec/barretenberg.js/wasm'; -import { CircuitsWasm, NewContractData, PrivateKernelPublicInputs, UInt8Vector } from '@aztec/circuits.js'; +import { PrivateKernelPublicInputs, UInt8Vector } from '@aztec/circuits.js'; import { Tx } from '@aztec/tx'; import { UnverifiedData } from '@aztec/unverified-data'; @@ -17,14 +15,3 @@ export function makeEmptyTx(): Tx { const isEmpty = true; return new Tx(PrivateKernelPublicInputs.makeEmpty(), makeEmptyProof(), makeEmptyUnverifiedData(), undefined, isEmpty); } - -export function hashNewContractData(wasm: CircuitsWasm | BarretenbergWasm, cd: NewContractData) { - if (cd.contractAddress.isZero() && cd.portalContractAddress.isZero() && cd.functionTreeRoot.isZero()) { - return Buffer.alloc(32, 0); - } - return pedersenCompressInputs(wasm as BarretenbergWasm, [ - cd.contractAddress.toBuffer(), - cd.portalContractAddress.toBuffer32(), - cd.functionTreeRoot.toBuffer(), - ]); -} diff --git a/yarn-project/sequencer-client/src/prover/empty.ts b/yarn-project/sequencer-client/src/prover/empty.ts index 38958091240..9eace688dbb 100644 --- a/yarn-project/sequencer-client/src/prover/empty.ts +++ b/yarn-project/sequencer-client/src/prover/empty.ts @@ -1,7 +1,7 @@ import { AggregationObject, BaseRollupInputs, - BaseRollupPublicInputs, + BaseOrMergeRollupPublicInputs, MergeRollupInputs, MergeRollupPublicInputs, RootRollupInputs, @@ -16,7 +16,7 @@ const EMPTY_PROOF_SIZE = 42; // TODO: Silently modifying one of the inputs is horrible. Rethink these interfaces. export class EmptyProver implements Prover { - async getBaseRollupProof(input: BaseRollupInputs, publicInputs: BaseRollupPublicInputs): Promise { + async getBaseRollupProof(input: BaseRollupInputs, publicInputs: BaseOrMergeRollupPublicInputs): Promise { publicInputs.endAggregationObject = AggregationObject.makeFake(); return new UInt8Vector(Buffer.alloc(EMPTY_PROOF_SIZE, 0)); } diff --git a/yarn-project/sequencer-client/src/prover/index.ts b/yarn-project/sequencer-client/src/prover/index.ts index 846eb2160f8..4b433ca4bd5 100644 --- a/yarn-project/sequencer-client/src/prover/index.ts +++ b/yarn-project/sequencer-client/src/prover/index.ts @@ -1,6 +1,6 @@ import { BaseRollupInputs, - BaseRollupPublicInputs, + BaseOrMergeRollupPublicInputs, MergeRollupInputs, MergeRollupPublicInputs, RootRollupInputs, @@ -10,7 +10,7 @@ import { export type Proof = UInt8Vector; export interface Prover { - getBaseRollupProof(input: BaseRollupInputs, publicInputs: BaseRollupPublicInputs): Promise; + getBaseRollupProof(input: BaseRollupInputs, publicInputs: BaseOrMergeRollupPublicInputs): Promise; getMergeRollupProof(input: MergeRollupInputs, publicInputs: MergeRollupPublicInputs): Promise; getRootRollupProof(input: RootRollupInputs, publicInputs: RootRollupPublicInputs): Promise; } diff --git a/yarn-project/sequencer-client/src/simulator/index.ts b/yarn-project/sequencer-client/src/simulator/index.ts index ac9fd00fc3e..b8bac937fc6 100644 --- a/yarn-project/sequencer-client/src/simulator/index.ts +++ b/yarn-project/sequencer-client/src/simulator/index.ts @@ -1,6 +1,6 @@ import { BaseRollupInputs, - BaseRollupPublicInputs, + BaseOrMergeRollupPublicInputs, MergeRollupInputs, MergeRollupPublicInputs, RootRollupInputs, @@ -8,7 +8,7 @@ import { } from '@aztec/circuits.js'; export interface Simulator { - baseRollupCircuit(input: BaseRollupInputs): Promise; + baseRollupCircuit(input: BaseRollupInputs): Promise; mergeRollupCircuit(input: MergeRollupInputs): Promise; rootRollupCircuit(input: RootRollupInputs): Promise; } diff --git a/yarn-project/sequencer-client/src/simulator/mock.ts b/yarn-project/sequencer-client/src/simulator/mock.ts index ad986f833af..5a702c3c1a4 100644 --- a/yarn-project/sequencer-client/src/simulator/mock.ts +++ b/yarn-project/sequencer-client/src/simulator/mock.ts @@ -1,6 +1,6 @@ import { BaseRollupInputs, - BaseRollupPublicInputs, + BaseOrMergeRollupPublicInputs, MergeRollupInputs, MergeRollupPublicInputs, RootRollupInputs, @@ -11,7 +11,7 @@ import { Simulator } from './index.js'; /* eslint-disable */ export class MockSimulator implements Simulator { - baseRollupCircuit(input: BaseRollupInputs): Promise { + baseRollupCircuit(input: BaseRollupInputs): Promise { throw new Error('Method not implemented.'); } mergeRollupCircuit(input: MergeRollupInputs): Promise { diff --git a/yarn-project/sequencer-client/src/simulator/wasm.ts b/yarn-project/sequencer-client/src/simulator/wasm.ts index 2c4274de858..41126285895 100644 --- a/yarn-project/sequencer-client/src/simulator/wasm.ts +++ b/yarn-project/sequencer-client/src/simulator/wasm.ts @@ -2,7 +2,7 @@ import { CircuitsWasm } from '@aztec/circuits.js'; import { RollupWasmWrapper } from '@aztec/circuits.js'; import { BaseRollupInputs, - BaseRollupPublicInputs, + BaseOrMergeRollupPublicInputs, MergeRollupInputs, MergeRollupPublicInputs, RootRollupInputs, @@ -17,7 +17,7 @@ export class WasmCircuitSimulator implements Simulator { this.rollupWasmWrapper = new RollupWasmWrapper(wasm); } - baseRollupCircuit(input: BaseRollupInputs): Promise { + baseRollupCircuit(input: BaseRollupInputs): Promise { return this.rollupWasmWrapper.simulateBaseRollup(input); } mergeRollupCircuit(input: MergeRollupInputs): Promise {