From 708efb8f571131d898bfdfe6d419c79a607106fc Mon Sep 17 00:00:00 2001 From: dbanks12 Date: Fri, 15 Nov 2024 02:55:42 +0000 Subject: [PATCH] refactor: stop calling public kernels --- .../aztec-node/src/aztec-node/server.ts | 1 - yarn-project/prover-node/src/prover-node.ts | 3 +- .../src/client/sequencer-client.ts | 4 +- .../public/enqueued_call_side_effect_trace.ts | 9 +- .../simulator/src/public/public_processor.ts | 13 +- .../simulator/src/public/public_tx_context.ts | 70 +++--- .../src/public/public_tx_simulator.test.ts | 7 - .../src/public/public_tx_simulator.ts | 52 +---- yarn-project/simulator/src/public/utils.ts | 208 ++++-------------- 9 files changed, 98 insertions(+), 269 deletions(-) diff --git a/yarn-project/aztec-node/src/aztec-node/server.ts b/yarn-project/aztec-node/src/aztec-node/server.ts index eec371b96fb..6e75f6154ed 100644 --- a/yarn-project/aztec-node/src/aztec-node/server.ts +++ b/yarn-project/aztec-node/src/aztec-node/server.ts @@ -735,7 +735,6 @@ export class AztecNodeService implements AztecNode { const prevHeader = (await this.blockSource.getBlock(-1))?.header; const publicProcessorFactory = new PublicProcessorFactory( this.contractDataSource, - new WASMSimulator(), this.telemetry, ); diff --git a/yarn-project/prover-node/src/prover-node.ts b/yarn-project/prover-node/src/prover-node.ts index 1b090315c8f..7451386c06c 100644 --- a/yarn-project/prover-node/src/prover-node.ts +++ b/yarn-project/prover-node/src/prover-node.ts @@ -56,7 +56,7 @@ export class ProverNode implements ClaimsMonitorHandler, EpochMonitorHandler, Pr private readonly contractDataSource: ContractDataSource, private readonly worldState: WorldStateSynchronizer, private readonly coordination: ProverCoordination & Maybe, - private readonly simulator: SimulationProvider, + private readonly _simulator: SimulationProvider, private readonly quoteProvider: QuoteProvider, private readonly quoteSigner: QuoteSigner, private readonly claimsMonitor: ClaimsMonitor, @@ -245,7 +245,6 @@ export class ProverNode implements ClaimsMonitorHandler, EpochMonitorHandler, Pr // Create a processor using the forked world state const publicProcessorFactory = new PublicProcessorFactory( this.contractDataSource, - this.simulator, this.telemetryClient, ); diff --git a/yarn-project/sequencer-client/src/client/sequencer-client.ts b/yarn-project/sequencer-client/src/client/sequencer-client.ts index a9618e8b980..eae0bd3729d 100644 --- a/yarn-project/sequencer-client/src/client/sequencer-client.ts +++ b/yarn-project/sequencer-client/src/client/sequencer-client.ts @@ -40,13 +40,13 @@ export class SequencerClient { contractDataSource: ContractDataSource, l2BlockSource: L2BlockSource, l1ToL2MessageSource: L1ToL2MessageSource, - simulationProvider: SimulationProvider, + _simulationProvider: SimulationProvider, telemetryClient: TelemetryClient, ) { const publisher = new L1Publisher(config, telemetryClient); const globalsBuilder = new GlobalVariableBuilder(config); - const publicProcessorFactory = new PublicProcessorFactory(contractDataSource, simulationProvider, telemetryClient); + const publicProcessorFactory = new PublicProcessorFactory(contractDataSource, telemetryClient); const rollup = publisher.getRollupContract(); const [l1GenesisTime, slotDuration] = await Promise.all([ diff --git a/yarn-project/simulator/src/public/enqueued_call_side_effect_trace.ts b/yarn-project/simulator/src/public/enqueued_call_side_effect_trace.ts index 6b06e838799..ccbdd098feb 100644 --- a/yarn-project/simulator/src/public/enqueued_call_side_effect_trace.ts +++ b/yarn-project/simulator/src/public/enqueued_call_side_effect_trace.ts @@ -70,7 +70,6 @@ import { makeTuple } from '@aztec/foundation/array'; import { padArrayEnd } from '@aztec/foundation/collection'; import { Fr } from '@aztec/foundation/fields'; import { createDebugLogger } from '@aztec/foundation/log'; -import { type Tuple } from '@aztec/foundation/serialize'; import { assert } from 'console'; @@ -631,9 +630,9 @@ export class PublicEnqueuedCallSideEffectTrace implements PublicSideEffectTraceI /** How much gas was available for this public execution. */ gasLimits: GasSettings, /** Call requests for setup phase. */ - publicSetupCallRequests: Tuple, + publicSetupCallRequests: PublicCallRequest[], /** Call requests for app logic phase. */ - publicAppLogicCallRequests: Tuple, + publicAppLogicCallRequests: PublicCallRequest[], /** Call request for teardown phase. */ publicTeardownCallRequest: PublicCallRequest, /** End tree snapshots. */ @@ -653,8 +652,8 @@ export class PublicEnqueuedCallSideEffectTrace implements PublicSideEffectTraceI startTreeSnapshots, startGasUsed, gasLimits, - publicSetupCallRequests, - publicAppLogicCallRequests, + padArrayEnd(publicSetupCallRequests, PublicCallRequest.empty(), MAX_ENQUEUED_CALLS_PER_TX), + padArrayEnd(publicAppLogicCallRequests, PublicCallRequest.empty(), MAX_ENQUEUED_CALLS_PER_TX), publicTeardownCallRequest, /*previousNonRevertibleAccumulatedDataArrayLengths=*/ PrivateToAvmAccumulatedDataArrayLengths.empty(), /*previousRevertibleAccumulatedDataArrayLengths=*/ PrivateToAvmAccumulatedDataArrayLengths.empty(), diff --git a/yarn-project/simulator/src/public/public_processor.ts b/yarn-project/simulator/src/public/public_processor.ts index 55fce164cfb..6d0b1fdfc54 100644 --- a/yarn-project/simulator/src/public/public_processor.ts +++ b/yarn-project/simulator/src/public/public_processor.ts @@ -31,12 +31,9 @@ import { Timer } from '@aztec/foundation/timer'; import { ProtocolContractAddress } from '@aztec/protocol-contracts'; import { Attributes, type TelemetryClient, type Tracer, trackSpan } from '@aztec/telemetry-client'; -import { type SimulationProvider } from '../providers/index.js'; import { PublicExecutor } from './executor.js'; import { computeFeePayerBalanceLeafSlot, computeFeePayerBalanceStorageSlot } from './fee_payment.js'; import { WorldStateDB } from './public_db_sources.js'; -import { RealPublicKernelCircuitSimulator } from './public_kernel.js'; -import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js'; import { PublicProcessorMetrics } from './public_processor_metrics.js'; import { PublicTxSimulator } from './public_tx_simulator.js'; @@ -44,11 +41,7 @@ import { PublicTxSimulator } from './public_tx_simulator.js'; * Creates new instances of PublicProcessor given the provided merkle tree db and contract data source. */ export class PublicProcessorFactory { - constructor( - private contractDataSource: ContractDataSource, - private simulator: SimulationProvider, - private telemetryClient: TelemetryClient, - ) {} + constructor(private contractDataSource: ContractDataSource, private telemetryClient: TelemetryClient) {} /** * Creates a new instance of a PublicProcessor. @@ -66,12 +59,10 @@ export class PublicProcessorFactory { const worldStateDB = new WorldStateDB(merkleTree, this.contractDataSource); const publicExecutor = new PublicExecutor(worldStateDB, telemetryClient); - const publicKernelSimulator = new RealPublicKernelCircuitSimulator(this.simulator); return PublicProcessor.create( merkleTree, publicExecutor, - publicKernelSimulator, globalVariables, historicalHeader, worldStateDB, @@ -101,7 +92,6 @@ export class PublicProcessor { static create( db: MerkleTreeWriteOperations, publicExecutor: PublicExecutor, - publicKernelSimulator: PublicKernelCircuitSimulator, globalVariables: GlobalVariables, historicalHeader: Header, worldStateDB: WorldStateDB, @@ -110,7 +100,6 @@ export class PublicProcessor { const enqueuedCallsProcessor = PublicTxSimulator.create( db, publicExecutor, - publicKernelSimulator, globalVariables, historicalHeader, worldStateDB, diff --git a/yarn-project/simulator/src/public/public_tx_context.ts b/yarn-project/simulator/src/public/public_tx_context.ts index 8f96471d5bb..968a5560d34 100644 --- a/yarn-project/simulator/src/public/public_tx_context.ts +++ b/yarn-project/simulator/src/public/public_tx_context.ts @@ -13,9 +13,9 @@ import { Gas, type GasSettings, type GlobalVariables, + type PrivateToPublicAccumulatedData, PublicAccumulatedDataArrayLengths, type PublicCallRequest, - type PublicKernelCircuitPublicInputs, PublicValidationRequestArrayLengths, RevertCode, type StateReference, @@ -30,7 +30,12 @@ import { DualSideEffectTrace } from './dual_side_effect_trace.js'; import { PublicEnqueuedCallSideEffectTrace } from './enqueued_call_side_effect_trace.js'; import { type WorldStateDB } from './public_db_sources.js'; import { PublicSideEffectTrace } from './side_effect_trace.js'; -import { generateAvmCircuitPublicInputs, getCallRequestsByPhase, getExecutionRequestsByPhase, getPublicKernelCircuitPublicInputs } from './utils.js'; +import { + convertPrivateToPublicAccumulatedData, + generateAvmCircuitPublicInputs, + getCallRequestsByPhase, + getExecutionRequestsByPhase, +} from './utils.js'; export class PublicTxContext { private log: DebugLogger; @@ -49,11 +54,11 @@ export class PublicTxContext { constructor( public readonly state: PhaseStateManager, - public readonly tx: Tx, // tmp hack + public readonly tx: Tx, // TODO(dbanks12): remove public readonly globalVariables: GlobalVariables, - public readonly constants: CombinedConstantData, // tmp hack + public readonly constants: CombinedConstantData, // TODO(dbanks12): remove public readonly startStateReference: StateReference, - startGasUsed: Gas, + private readonly startGasUsed: Gas, private readonly gasSettings: GasSettings, private readonly setupCallRequests: PublicCallRequest[], private readonly appLogicCallRequests: PublicCallRequest[], @@ -61,8 +66,8 @@ export class PublicTxContext { private readonly setupExecutionRequests: PublicExecutionRequest[], private readonly appLogicExecutionRequests: PublicExecutionRequest[], private readonly teardownExecutionRequests: PublicExecutionRequest[], - private firstPublicKernelOutput: PublicKernelCircuitPublicInputs, - public latestPublicKernelOutput: PublicKernelCircuitPublicInputs, + private readonly nonRevertibleAccumulatedDataFromPrivate: PrivateToPublicAccumulatedData, + private readonly revertibleAccumulatedDataFromPrivate: PrivateToPublicAccumulatedData, public trace: PublicEnqueuedCallSideEffectTrace, ) { this.log = createDebugLogger(`aztec:public_tx_context`); @@ -75,29 +80,25 @@ export class PublicTxContext { tx: Tx, globalVariables: GlobalVariables, ) { - const privateKernelOutput = tx.data; - const firstPublicKernelOutput = getPublicKernelCircuitPublicInputs(privateKernelOutput, globalVariables); + const nonRevertibleAccumulatedDataFromPrivate = convertPrivateToPublicAccumulatedData( + tx.data.forPublic!.nonRevertibleAccumulatedData, + ); + const revertibleAccumulatedDataFromPrivate = convertPrivateToPublicAccumulatedData( + tx.data.forPublic!.revertibleAccumulatedData, + ); - const nonRevertibleNullifiersFromPrivate = firstPublicKernelOutput.endNonRevertibleData.nullifiers + const nonRevertibleNullifiersFromPrivate = nonRevertibleAccumulatedDataFromPrivate.nullifiers .filter(n => !n.isEmpty()) .map(n => n.value); - const _revertibleNullifiersFromPrivate = firstPublicKernelOutput.end.nullifiers + const _revertibleNullifiersFromPrivate = revertibleAccumulatedDataFromPrivate.nullifiers .filter(n => !n.isEmpty()) .map(n => n.value); - // During SETUP, non revertible side effects from private are our "previous data" - const prevAccumulatedData = firstPublicKernelOutput.endNonRevertibleData; - const previousValidationRequestArrayLengths = PublicValidationRequestArrayLengths.new( - firstPublicKernelOutput.validationRequests, - ); - - const previousAccumulatedDataArrayLengths = PublicAccumulatedDataArrayLengths.new(prevAccumulatedData); - const innerCallTrace = new PublicSideEffectTrace(); const enqueuedCallTrace = new PublicEnqueuedCallSideEffectTrace( /*startSideEffectCounter=*/ 0, - previousValidationRequestArrayLengths, - previousAccumulatedDataArrayLengths, + PublicValidationRequestArrayLengths.empty(), + PublicAccumulatedDataArrayLengths.new(nonRevertibleAccumulatedDataFromPrivate), ); const trace = new DualSideEffectTrace(innerCallTrace, enqueuedCallTrace); @@ -122,8 +123,8 @@ export class PublicTxContext { getExecutionRequestsByPhase(tx, TxExecutionPhase.SETUP), getExecutionRequestsByPhase(tx, TxExecutionPhase.APP_LOGIC), getExecutionRequestsByPhase(tx, TxExecutionPhase.TEARDOWN), - firstPublicKernelOutput, - firstPublicKernelOutput, + tx.data.forPublic!.nonRevertibleAccumulatedData, + tx.data.forPublic!.revertibleAccumulatedData, enqueuedCallTrace, ); } @@ -258,25 +259,33 @@ export class PublicTxContext { return txFee; } - private async generateAvmCircuitPublicInputs(endStateReference: StateReference): Promise { - assert(this.currentPhase === TxExecutionPhase.TEARDOWN, 'Can only get AvmCircuitPublicInputs after teardown (tx done)'); + private generateAvmCircuitPublicInputs(endStateReference: StateReference): AvmCircuitPublicInputs { + assert( + this.currentPhase === TxExecutionPhase.TEARDOWN, + 'Can only get AvmCircuitPublicInputs after teardown (tx done)', + ); return generateAvmCircuitPublicInputs( - this.tx, this.trace, this.globalVariables, this.startStateReference, + this.startGasUsed, + this.gasSettings, + this.setupCallRequests, + this.appLogicCallRequests, + this.teardownCallRequests, + this.nonRevertibleAccumulatedDataFromPrivate, + this.revertibleAccumulatedDataFromPrivate, endStateReference, - this.gasUsed, + /*endGasUsed=*/ this.gasUsed, this.getFinalTransactionFee(), this.revertCode, - this.firstPublicKernelOutput, ); } - async generateProvingRequest(endStateReference: StateReference): Promise { + generateProvingRequest(endStateReference: StateReference): AvmProvingRequest { // TODO(dbanks12): Once we actually have tx-level proving, this will generate the entire // proving request for the first time - this.avmProvingRequest!.inputs.output = await this.generateAvmCircuitPublicInputs(endStateReference); + this.avmProvingRequest!.inputs.output = this.generateAvmCircuitPublicInputs(endStateReference); return this.avmProvingRequest!; } } @@ -313,4 +322,3 @@ class PhaseStateManager { this.currentlyActiveStateManager = undefined; } } - diff --git a/yarn-project/simulator/src/public/public_tx_simulator.test.ts b/yarn-project/simulator/src/public/public_tx_simulator.test.ts index 08780d7008c..e2251e69ea4 100644 --- a/yarn-project/simulator/src/public/public_tx_simulator.test.ts +++ b/yarn-project/simulator/src/public/public_tx_simulator.test.ts @@ -31,11 +31,8 @@ import { type MockProxy, mock } from 'jest-mock-extended'; import { type AvmPersistableStateManager } from '../avm/journal/journal.js'; import { PublicExecutionResultBuilder } from '../mocks/fixtures.js'; -import { WASMSimulator } from '../providers/acvm_wasm.js'; import { type PublicExecutor } from './executor.js'; import { type WorldStateDB } from './public_db_sources.js'; -import { RealPublicKernelCircuitSimulator } from './public_kernel.js'; -import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js'; import { PublicTxSimulator } from './public_tx_simulator.js'; describe('public_tx_simulator', () => { @@ -53,7 +50,6 @@ describe('public_tx_simulator', () => { let db: MockProxy; let publicExecutor: MockProxy; - let publicKernel: PublicKernelCircuitSimulator; let worldStateDB: MockProxy; let root: Buffer; @@ -163,12 +159,9 @@ describe('public_tx_simulator', () => { db.getPreviousValueIndex.mockResolvedValue({ index: 0n, alreadyPresent: true }); db.getLeafPreimage.mockResolvedValue(new PublicDataTreeLeafPreimage(new Fr(0), new Fr(0), new Fr(0), 0n)); - publicKernel = new RealPublicKernelCircuitSimulator(new WASMSimulator()); - processor = PublicTxSimulator.create( db, publicExecutor, - publicKernel, GlobalVariables.from({ ...GlobalVariables.empty(), gasFees }), Header.empty(), worldStateDB, diff --git a/yarn-project/simulator/src/public/public_tx_simulator.ts b/yarn-project/simulator/src/public/public_tx_simulator.ts index b2f15d8bd8f..973b5c7a232 100644 --- a/yarn-project/simulator/src/public/public_tx_simulator.ts +++ b/yarn-project/simulator/src/public/public_tx_simulator.ts @@ -14,10 +14,7 @@ import { Timer } from '@aztec/foundation/timer'; import { EnqueuedCallSimulator } from './enqueued_call_simulator.js'; import { type PublicExecutor } from './executor.js'; import { type WorldStateDB } from './public_db_sources.js'; -import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js'; -import { PublicKernelTailSimulator } from './public_kernel_tail_simulator.js'; import { PublicTxContext } from './public_tx_context.js'; -import { generateAvmCircuitPublicInputs, generateAvmCircuitPublicInputsDeprecated, runMergeKernelCircuit } from './utils.js'; export type ProcessedPhase = { phase: TxExecutionPhase; @@ -42,19 +39,16 @@ export class PublicTxSimulator { constructor( private db: MerkleTreeReadOperations, - private publicKernelSimulator: PublicKernelCircuitSimulator, private globalVariables: GlobalVariables, private worldStateDB: WorldStateDB, private enqueuedCallSimulator: EnqueuedCallSimulator, - private publicKernelTailSimulator: PublicKernelTailSimulator, ) { - this.log = createDebugLogger(`aztec:sequencer`); + this.log = createDebugLogger(`aztec:public_tx_simulator`); } static create( db: MerkleTreeReadOperations, publicExecutor: PublicExecutor, - publicKernelSimulator: PublicKernelCircuitSimulator, globalVariables: GlobalVariables, historicalHeader: Header, worldStateDB: WorldStateDB, @@ -69,16 +63,7 @@ export class PublicTxSimulator { realAvmProvingRequests, ); - const publicKernelTailSimulator = PublicKernelTailSimulator.create(db, publicKernelSimulator); - - return new PublicTxSimulator( - db, - publicKernelSimulator, - globalVariables, - worldStateDB, - enqueuedCallSimulator, - publicKernelTailSimulator, - ); + return new PublicTxSimulator(db, globalVariables, worldStateDB, enqueuedCallSimulator); } async process(tx: Tx): Promise { @@ -96,22 +81,9 @@ export class PublicTxSimulator { const endStateReference = await this.db.getStateReference(); - const tailKernelOutput = await this.publicKernelTailSimulator.simulate(context.latestPublicKernelOutput); - - generateAvmCircuitPublicInputsDeprecated( - tx, - tailKernelOutput, - context.getGasUsedForFee(), - context.getFinalTransactionFee(), - ); - - const gasUsed = { - totalGas: context.getActualGasUsed(), - teardownGas: context.teardownGasUsed, - }; return { - avmProvingRequest: await context.generateProvingRequest(endStateReference), - gasUsed, + avmProvingRequest: context.generateProvingRequest(endStateReference), + gasUsed: { totalGas: context.getActualGasUsed(), teardownGas: context.teardownGasUsed }, revertCode: context.revertCode, revertReason: context.revertReason, processedPhases: processedPhases, @@ -214,14 +186,16 @@ export class PublicTxSimulator { /*transactionFee=*/ context.getTransactionFee(), enqueuedCallStateManager, ); + if (context.avmProvingRequest === undefined) { + // Propagate the very first avmProvingRequest of the tx for now. + // Eventually this will be the proof for the entire public portion of the transaction. + context.avmProvingRequest = enqueuedCallResult.avmProvingRequest; + } txStateManager.traceEnqueuedCall(callRequest, executionRequest.args, enqueuedCallResult.reverted!); context.consumeGas(enqueuedCallResult.gasUsed); returnValues.push(enqueuedCallResult.returnValues); - // Propagate only one avmProvingRequest of a function call for now, so that we know it's still provable. - // Eventually this will be the proof for the entire public portion of the transaction. - context.avmProvingRequest = enqueuedCallResult.avmProvingRequest; if (enqueuedCallResult.reverted) { reverted = true; const culprit = `${executionRequest.callContext.contractAddress}:${executionRequest.callContext.functionSelector}`; @@ -234,7 +208,7 @@ export class PublicTxSimulator { // FIXME: we shouldn't need to directly modify worldStateDb here! await this.worldStateDB.removeNewContracts(tx); // FIXME: we shouldn't be modifying the transaction here! - tx.filterRevertedLogs(context.latestPublicKernelOutput); + //tx.filterRevertedLogs(context.latestPublicKernelOutput); // Enqueeud call reverted. Discard state updates and accumulated side effects, but keep hints traced for the circuit. txStateManager.rejectForkedState(enqueuedCallStateManager); } else { @@ -243,12 +217,6 @@ export class PublicTxSimulator { // Enqueued call succeeded! Merge in any state updates made in the forked state manager. txStateManager.mergeForkedState(enqueuedCallStateManager); } - - context.latestPublicKernelOutput = await runMergeKernelCircuit( - context.latestPublicKernelOutput, - enqueuedCallResult.kernelOutput, - this.publicKernelSimulator, - ); } return { diff --git a/yarn-project/simulator/src/public/utils.ts b/yarn-project/simulator/src/public/utils.ts index 05b9064a434..972ea55ebd7 100644 --- a/yarn-project/simulator/src/public/utils.ts +++ b/yarn-project/simulator/src/public/utils.ts @@ -1,48 +1,30 @@ import { type PublicExecutionRequest, type Tx, TxExecutionPhase } from '@aztec/circuit-types'; import { - AvmAccumulatedData, - AvmCircuitPublicInputs, - type CombinedAccumulatedData, - CombinedConstantData, - EnqueuedCallData, + type AvmCircuitPublicInputs, type Fr, type Gas, + type GasSettings, type GlobalVariables, - type KernelCircuitPublicInputs, - NESTED_RECURSIVE_PROOF_LENGTH, - type PrivateKernelTailCircuitPublicInputs, + MAX_L2_TO_L1_MSGS_PER_TX, + MAX_NOTE_HASHES_PER_TX, + MAX_NULLIFIERS_PER_TX, + MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PrivateToAvmAccumulatedData, PrivateToAvmAccumulatedDataArrayLengths, type PrivateToPublicAccumulatedData, PublicAccumulatedData, - type PublicCallRequest, - PublicKernelCircuitPrivateInputs, - PublicKernelCircuitPublicInputs, - PublicKernelData, - PublicValidationRequests, - RevertCode, + PublicCallRequest, + PublicDataWrite, + type RevertCode, + type StateReference, TreeSnapshots, - type VMCircuitPublicInputs, - VerificationKeyData, countAccumulatedItems, - makeEmptyProof, - makeEmptyRecursiveProof, - StateReference, mergeAccumulatedData, - MAX_NOTE_HASHES_PER_TX, - MAX_NULLIFIERS_PER_TX, - MAX_L2_TO_L1_MSGS_PER_TX, - MAX_UNENCRYPTED_LOGS_PER_TX, - PublicDataWrite, - MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, } from '@aztec/circuits.js'; -import { getVKSiblingPath } from '@aztec/noir-protocol-circuits-types'; - -import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js'; -import { PublicEnqueuedCallSideEffectTrace } from './enqueued_call_side_effect_trace.js'; -import { assertLength } from '@aztec/foundation/serialize'; import { padArrayEnd } from '@aztec/foundation/collection'; -import { inspect } from 'util'; +import { assertLength } from '@aztec/foundation/serialize'; + +import { type PublicEnqueuedCallSideEffectTrace } from './enqueued_call_side_effect_trace.js'; export function getExecutionRequestsByPhase(tx: Tx, phase: TxExecutionPhase): PublicExecutionRequest[] { switch (phase) { @@ -74,134 +56,36 @@ export function getCallRequestsByPhase(tx: Tx, phase: TxExecutionPhase): PublicC } } -// Temporary hack to create PublicKernelCircuitPublicInputs from PrivateKernelTailCircuitPublicInputs. -export function getPublicKernelCircuitPublicInputs( - data: PrivateKernelTailCircuitPublicInputs, - globalVariables: GlobalVariables, -) { - const constants = CombinedConstantData.combine(data.constants, globalVariables); - - const validationRequest = PublicValidationRequests.empty(); - validationRequest.forRollup = data.rollupValidationRequests; - - const convertAccumulatedData = (from: PrivateToPublicAccumulatedData) => { - const to = PublicAccumulatedData.empty(); - to.noteHashes.forEach((_, i) => (to.noteHashes[i].noteHash.value = from.noteHashes[i])); - to.nullifiers.forEach((_, i) => (to.nullifiers[i].value = from.nullifiers[i])); - to.l2ToL1Msgs.forEach((_, i) => (to.l2ToL1Msgs[i] = from.l2ToL1Msgs[i])); - to.noteEncryptedLogsHashes.forEach((_, i) => (to.noteEncryptedLogsHashes[i] = from.noteEncryptedLogsHashes[i])); - to.encryptedLogsHashes.forEach((_, i) => (to.encryptedLogsHashes[i] = from.encryptedLogsHashes[i])); - to.publicCallStack.forEach((_, i) => (to.publicCallStack[i] = from.publicCallRequests[i])); - return to; - }; - - return new PublicKernelCircuitPublicInputs( - constants, - validationRequest, - convertAccumulatedData(data.forPublic!.nonRevertibleAccumulatedData), - convertAccumulatedData(data.forPublic!.revertibleAccumulatedData), - 0, - data.forPublic!.publicTeardownCallRequest, - data.feePayer, - RevertCode.OK, +export function convertPrivateToPublicAccumulatedData( + fromPrivate: PrivateToPublicAccumulatedData, +): PublicAccumulatedData { + const to = PublicAccumulatedData.empty(); + to.noteHashes.forEach((_, i) => (to.noteHashes[i].noteHash.value = fromPrivate.noteHashes[i])); + to.nullifiers.forEach((_, i) => (to.nullifiers[i].value = fromPrivate.nullifiers[i])); + to.l2ToL1Msgs.forEach((_, i) => (to.l2ToL1Msgs[i] = fromPrivate.l2ToL1Msgs[i])); + to.noteEncryptedLogsHashes.forEach( + (_, i) => (to.noteEncryptedLogsHashes[i] = fromPrivate.noteEncryptedLogsHashes[i]), ); -} - -// Temporary hack to create the AvmCircuitPublicInputs from public tail's public inputs. -export function generateAvmCircuitPublicInputsDeprecated( - tx: Tx, - tailOutput: KernelCircuitPublicInputs, - gasUsedForFee: Gas, - transactionFee: Fr, -) { - const startTreeSnapshots = new TreeSnapshots( - tailOutput.constants.historicalHeader.state.l1ToL2MessageTree, - tailOutput.startState.noteHashTree, - tailOutput.startState.nullifierTree, - tailOutput.startState.publicDataTree, - ); - - const getArrayLengths = (from: PrivateToPublicAccumulatedData) => - new PrivateToAvmAccumulatedDataArrayLengths( - countAccumulatedItems(from.noteHashes), - countAccumulatedItems(from.nullifiers), - countAccumulatedItems(from.l2ToL1Msgs), - ); - - const convertAccumulatedData = (from: PrivateToPublicAccumulatedData) => - new PrivateToAvmAccumulatedData(from.noteHashes, from.nullifiers, from.l2ToL1Msgs); - - const convertAvmAccumulatedData = (from: CombinedAccumulatedData) => - new AvmAccumulatedData( - from.noteHashes, - from.nullifiers, - from.l2ToL1Msgs, - from.unencryptedLogsHashes, - from.publicDataWrites, - ); - - // This is wrong. But this is not used or checked in the rollup at the moment. - // Should fetch the updated roots from db. - const endTreeSnapshots = startTreeSnapshots; - - const avmCircuitpublicInputs = new AvmCircuitPublicInputs( - tailOutput.constants.globalVariables, - startTreeSnapshots, - tx.data.gasUsed, - tx.data.constants.txContext.gasSettings, - tx.data.forPublic!.nonRevertibleAccumulatedData.publicCallRequests, - tx.data.forPublic!.revertibleAccumulatedData.publicCallRequests, - tx.data.forPublic!.publicTeardownCallRequest, - getArrayLengths(tx.data.forPublic!.nonRevertibleAccumulatedData), - getArrayLengths(tx.data.forPublic!.revertibleAccumulatedData), - convertAccumulatedData(tx.data.forPublic!.nonRevertibleAccumulatedData), - convertAccumulatedData(tx.data.forPublic!.revertibleAccumulatedData), - endTreeSnapshots, - gasUsedForFee, - convertAvmAccumulatedData(tailOutput.end), - transactionFee, - !tailOutput.revertCode.equals(RevertCode.OK), - ); - return avmCircuitpublicInputs; -} - -function getPreviousKernelData(previousOutput: PublicKernelCircuitPublicInputs): PublicKernelData { - // The proof is not used in simulation. - const proof = makeEmptyRecursiveProof(NESTED_RECURSIVE_PROOF_LENGTH); - - const vk = VerificationKeyData.makeFakeHonk(); - const vkIndex = 0; - const siblingPath = getVKSiblingPath(vkIndex); - - return new PublicKernelData(previousOutput, proof, vk, vkIndex, siblingPath); -} - -export async function runMergeKernelCircuit( - previousOutput: PublicKernelCircuitPublicInputs, - enqueuedCallData: VMCircuitPublicInputs, - publicKernelSimulator: PublicKernelCircuitSimulator, -): Promise { - const previousKernel = getPreviousKernelData(previousOutput); - - // The proof is not used in simulation. - const vmProof = makeEmptyProof(); - const callData = new EnqueuedCallData(enqueuedCallData, vmProof); - - const inputs = new PublicKernelCircuitPrivateInputs(previousKernel, callData); - - return await publicKernelSimulator.publicKernelCircuitMerge(inputs); + to.encryptedLogsHashes.forEach((_, i) => (to.encryptedLogsHashes[i] = fromPrivate.encryptedLogsHashes[i])); + to.publicCallStack.forEach((_, i) => (to.publicCallStack[i] = fromPrivate.publicCallRequests[i])); + return to; } export function generateAvmCircuitPublicInputs( - tx: Tx, trace: PublicEnqueuedCallSideEffectTrace, globalVariables: GlobalVariables, startStateReference: StateReference, + startGasUsed: Gas, + gasSettings: GasSettings, + setupCallRequests: PublicCallRequest[], + appLogicCallRequests: PublicCallRequest[], + teardownCallRequests: PublicCallRequest[], + nonRevertibleAccumulatedDataFromPrivate: PrivateToPublicAccumulatedData, + revertibleAccumulatedDataFromPrivate: PrivateToPublicAccumulatedData, endStateReference: StateReference, endGasUsed: Gas, transactionFee: Fr, revertCode: RevertCode, - firstPublicKernelOutput: PublicKernelCircuitPublicInputs, ): AvmCircuitPublicInputs { const startTreeSnapshots = new TreeSnapshots( startStateReference.l1ToL2MessageTree, @@ -219,11 +103,11 @@ export function generateAvmCircuitPublicInputs( const avmCircuitPublicInputs = trace.toAvmCircuitPublicInputs( globalVariables, startTreeSnapshots, - tx.data.gasUsed, - tx.data.constants.txContext.gasSettings, - tx.data.forPublic!.nonRevertibleAccumulatedData.publicCallRequests, - tx.data.forPublic!.revertibleAccumulatedData.publicCallRequests, - tx.data.forPublic!.publicTeardownCallRequest, + startGasUsed, + gasSettings, + setupCallRequests, + appLogicCallRequests, + teardownCallRequests ? teardownCallRequests[0] : PublicCallRequest.empty(), endTreeSnapshots, endGasUsed, transactionFee, @@ -240,16 +124,16 @@ export function generateAvmCircuitPublicInputs( new PrivateToAvmAccumulatedData(from.noteHashes, from.nullifiers, from.l2ToL1Msgs); // Temporary overrides as these entries aren't yet populated in trace avmCircuitPublicInputs.previousNonRevertibleAccumulatedDataArrayLengths = getArrayLengths( - tx.data.forPublic!.nonRevertibleAccumulatedData, + nonRevertibleAccumulatedDataFromPrivate, ); avmCircuitPublicInputs.previousRevertibleAccumulatedDataArrayLengths = getArrayLengths( - tx.data.forPublic!.revertibleAccumulatedData, + revertibleAccumulatedDataFromPrivate, ); avmCircuitPublicInputs.previousNonRevertibleAccumulatedData = convertAccumulatedData( - tx.data.forPublic!.nonRevertibleAccumulatedData, + nonRevertibleAccumulatedDataFromPrivate, ); avmCircuitPublicInputs.previousRevertibleAccumulatedData = convertAccumulatedData( - tx.data.forPublic!.revertibleAccumulatedData, + revertibleAccumulatedDataFromPrivate, ); // merge all revertible & non-revertible side effects into output accumulated data @@ -283,16 +167,6 @@ export function generateAvmCircuitPublicInputs( mergeAccumulatedData(msgsFromPrivate, avmCircuitPublicInputs.accumulatedData.l2ToL1Msgs), MAX_L2_TO_L1_MSGS_PER_TX, ); - const ulogsFromPrivate = revertCode.isOK() - ? mergeAccumulatedData( - firstPublicKernelOutput.endNonRevertibleData.unencryptedLogsHashes, - firstPublicKernelOutput.end.unencryptedLogsHashes, - ) - : firstPublicKernelOutput.endNonRevertibleData.unencryptedLogsHashes; - avmCircuitPublicInputs.accumulatedData.unencryptedLogsHashes = assertLength( - mergeAccumulatedData(ulogsFromPrivate, avmCircuitPublicInputs.accumulatedData.unencryptedLogsHashes), - MAX_UNENCRYPTED_LOGS_PER_TX, - ); const dedupedPublicDataWrites: Array = []; const leafSlotOccurences: Map = new Map();