Skip to content

Commit

Permalink
refactor: Use serialize functions in getInitialWitness (#2713)
Browse files Browse the repository at this point in the history
Fixes #2625. Using the functions defined in `serialize` when applicable.
  • Loading branch information
LHerskind authored Oct 5, 2023
1 parent f6fc7f2 commit 93cc668
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
21 changes: 20 additions & 1 deletion yarn-project/acir-simulator/src/acvm/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
CallContext,
ContractDeploymentData,
FunctionData,
GlobalVariables,
HistoricBlockData,
PrivateCallStackItem,
PrivateCircuitPublicInputs,
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 10 additions & 17 deletions yarn-project/acir-simulator/src/client/client_execution_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 10 additions & 15 deletions yarn-project/acir-simulator/src/public/public_execution_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
];
Expand Down

0 comments on commit 93cc668

Please sign in to comment.