From 93cc668d360ae1c599af5e347df7cd8341c59cda Mon Sep 17 00:00:00 2001 From: Lasse Herskind <16536249+LHerskind@users.noreply.github.com> Date: Thu, 5 Oct 2023 17:52:39 +0100 Subject: [PATCH] refactor: Use `serialize` functions in `getInitialWitness` (#2713) Fixes #2625. Using the functions defined in `serialize` when applicable. --- .../acir-simulator/src/acvm/serialize.ts | 21 ++++++++++++++- .../src/client/client_execution_context.ts | 27 +++++++------------ .../src/public/public_execution_context.ts | 25 +++++++---------- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/yarn-project/acir-simulator/src/acvm/serialize.ts b/yarn-project/acir-simulator/src/acvm/serialize.ts index 00b28a42e99..9f62b4b221d 100644 --- a/yarn-project/acir-simulator/src/acvm/serialize.ts +++ b/yarn-project/acir-simulator/src/acvm/serialize.ts @@ -2,6 +2,7 @@ import { CallContext, ContractDeploymentData, FunctionData, + GlobalVariables, HistoricBlockData, PrivateCallStackItem, PrivateCircuitPublicInputs, @@ -33,12 +34,16 @@ function adaptBufferSize(originalBuf: Buffer) { * @param value - The value to convert. * @returns The ACVM field. */ -export function toACVMField(value: AztecAddress | EthAddress | Fr | Buffer | boolean | number | bigint): ACVMField { +export function toACVMField( + value: AztecAddress | EthAddress | Fr | Buffer | boolean | number | bigint | ACVMField, +): ACVMField { let buffer; if (Buffer.isBuffer(value)) { buffer = value; } else if (typeof value === 'boolean' || typeof value === 'number' || typeof value === 'bigint') { buffer = new Fr(value).toBuffer(); + } else if (typeof value === 'string') { + buffer = Fr.fromString(value).toBuffer(); } else { buffer = value.toBuffer(); } @@ -112,6 +117,20 @@ export function toACVMHistoricBlockData(historicBlockData: HistoricBlockData): A ]; } +/** + * Converts global variables into ACVM fields + * @param globalVariables - The global variables object to convert. + * @returns The ACVM fields + */ +export function toACVMGlobalVariables(globalVariables: GlobalVariables): ACVMField[] { + return [ + toACVMField(globalVariables.chainId), + toACVMField(globalVariables.version), + toACVMField(globalVariables.blockNumber), + toACVMField(globalVariables.timestamp), + ]; +} + /** * Converts the public inputs structure to ACVM fields. * @param publicInputs - The public inputs to convert. diff --git a/yarn-project/acir-simulator/src/client/client_execution_context.ts b/yarn-project/acir-simulator/src/client/client_execution_context.ts index 12e5df55bf4..2627802ef89 100644 --- a/yarn-project/acir-simulator/src/client/client_execution_context.ts +++ b/yarn-project/acir-simulator/src/client/client_execution_context.ts @@ -17,7 +17,13 @@ import { Fr, Point } from '@aztec/foundation/fields'; import { createDebugLogger } from '@aztec/foundation/log'; import { AuthWitness, FunctionL2Logs, NotePreimage, NoteSpendingInfo, UnencryptedL2Log } from '@aztec/types'; -import { NoteData, toACVMWitness } from '../acvm/index.js'; +import { + NoteData, + toACVMCallContext, + toACVMContractDeploymentData, + toACVMHistoricBlockData, + toACVMWitness, +} from '../acvm/index.js'; import { SideEffectCounter } from '../common/index.js'; import { PackedArgsCache } from '../common/packed_args_cache.js'; import { DBOracle } from './db_oracle.js'; @@ -83,22 +89,9 @@ export class ClientExecutionContext extends ViewDataOracle { const contractDeploymentData = this.txContext.contractDeploymentData; const fields = [ - this.callContext.msgSender, - this.callContext.storageContractAddress, - this.callContext.portalContractAddress, - this.callContext.functionSelector.toField(), - this.callContext.isDelegateCall, - this.callContext.isStaticCall, - this.callContext.isContractDeployment, - - ...this.historicBlockData.toArray(), - - contractDeploymentData.deployerPublicKey.x, - contractDeploymentData.deployerPublicKey.y, - contractDeploymentData.constructorVkHash, - contractDeploymentData.functionTreeRoot, - contractDeploymentData.contractAddressSalt, - contractDeploymentData.portalContractAddress, + ...toACVMCallContext(this.callContext), + ...toACVMHistoricBlockData(this.historicBlockData), + ...toACVMContractDeploymentData(contractDeploymentData), this.txContext.chainId, this.txContext.version, diff --git a/yarn-project/acir-simulator/src/public/public_execution_context.ts b/yarn-project/acir-simulator/src/public/public_execution_context.ts index 5af2fbe76f0..4097087e19a 100644 --- a/yarn-project/acir-simulator/src/public/public_execution_context.ts +++ b/yarn-project/acir-simulator/src/public/public_execution_context.ts @@ -5,7 +5,13 @@ import { Fr } from '@aztec/foundation/fields'; import { createDebugLogger } from '@aztec/foundation/log'; import { FunctionL2Logs, UnencryptedL2Log } from '@aztec/types'; -import { TypedOracle, toACVMWitness } from '../acvm/index.js'; +import { + TypedOracle, + toACVMCallContext, + toACVMGlobalVariables, + toACVMHistoricBlockData, + toACVMWitness, +} from '../acvm/index.js'; import { PackedArgsCache, SideEffectCounter } from '../common/index.js'; import { CommitmentsDB, PublicContractsDB, PublicStateDB } from './db.js'; import { PublicExecution, PublicExecutionResult } from './execution.js'; @@ -50,20 +56,9 @@ export class PublicExecutionContext extends TypedOracle { public getInitialWitness(witnessStartIndex = 1) { const { callContext, args } = this.execution; const fields = [ - callContext.msgSender, - callContext.storageContractAddress, - callContext.portalContractAddress, - callContext.functionSelector.toField(), - callContext.isDelegateCall, - callContext.isStaticCall, - callContext.isContractDeployment, - - ...this.historicBlockData.toArray(), - - this.globalVariables.chainId, - this.globalVariables.version, - this.globalVariables.blockNumber, - this.globalVariables.timestamp, + ...toACVMCallContext(callContext), + ...toACVMHistoricBlockData(this.historicBlockData), + ...toACVMGlobalVariables(this.globalVariables), ...args, ];