@aztec/simulator 0.80.0 → 0.82.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/common/db_interfaces.d.ts +30 -17
- package/dest/common/db_interfaces.d.ts.map +1 -1
- package/dest/common/db_interfaces.js +1 -1
- package/dest/common/debug_fn_name.d.ts +2 -2
- package/dest/common/debug_fn_name.d.ts.map +1 -1
- package/dest/common/message_load_oracle_inputs.d.ts +4 -0
- package/dest/common/message_load_oracle_inputs.d.ts.map +1 -1
- package/dest/common/message_load_oracle_inputs.js +9 -0
- package/dest/private/acvm/acvm.d.ts +6 -1
- package/dest/private/acvm/acvm.d.ts.map +1 -1
- package/dest/private/acvm/acvm.js +7 -13
- package/dest/private/acvm/deserialize.d.ts +19 -18
- package/dest/private/acvm/deserialize.d.ts.map +1 -1
- package/dest/private/acvm/deserialize.js +31 -23
- package/dest/private/acvm/oracle/oracle.d.ts +36 -34
- package/dest/private/acvm/oracle/oracle.d.ts.map +1 -1
- package/dest/private/acvm/oracle/oracle.js +134 -79
- package/dest/private/acvm/oracle/typed_oracle.d.ts +3 -2
- package/dest/private/acvm/oracle/typed_oracle.d.ts.map +1 -1
- package/dest/private/acvm/oracle/typed_oracle.js +5 -2
- package/dest/private/acvm/serialize.d.ts +11 -0
- package/dest/private/acvm/serialize.d.ts.map +1 -1
- package/dest/private/acvm/serialize.js +27 -0
- package/dest/private/execution_data_provider.d.ts +15 -13
- package/dest/private/execution_data_provider.d.ts.map +1 -1
- package/dest/private/private_execution.d.ts +2 -2
- package/dest/private/private_execution.d.ts.map +1 -1
- package/dest/private/private_execution.js +4 -5
- package/dest/private/private_execution_oracle.d.ts.map +1 -1
- package/dest/private/private_execution_oracle.js +1 -1
- package/dest/private/providers/acvm_native.d.ts +6 -4
- package/dest/private/providers/acvm_native.d.ts.map +1 -1
- package/dest/private/providers/acvm_native.js +6 -3
- package/dest/private/providers/acvm_wasm.d.ts +6 -7
- package/dest/private/providers/acvm_wasm.d.ts.map +1 -1
- package/dest/private/providers/acvm_wasm.js +13 -15
- package/dest/private/providers/acvm_wasm_with_blobs.d.ts +5 -5
- package/dest/private/providers/acvm_wasm_with_blobs.d.ts.map +1 -1
- package/dest/private/providers/acvm_wasm_with_blobs.js +7 -9
- package/dest/private/providers/circuit_recording/circuit_recorder.d.ts +90 -0
- package/dest/private/providers/circuit_recording/circuit_recorder.d.ts.map +1 -0
- package/dest/private/providers/circuit_recording/circuit_recorder.js +246 -0
- package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts +18 -0
- package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts.map +1 -0
- package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.js +39 -0
- package/dest/private/providers/simulation_provider.d.ts +21 -7
- package/dest/private/providers/simulation_provider.d.ts.map +1 -1
- package/dest/private/simulator.d.ts +3 -2
- package/dest/private/simulator.d.ts.map +1 -1
- package/dest/private/simulator.js +2 -2
- package/dest/private/unconstrained_execution.d.ts +2 -2
- package/dest/private/unconstrained_execution.d.ts.map +1 -1
- package/dest/private/unconstrained_execution.js +1 -2
- package/dest/private/unconstrained_execution_oracle.d.ts +5 -3
- package/dest/private/unconstrained_execution_oracle.d.ts.map +1 -1
- package/dest/private/unconstrained_execution_oracle.js +9 -5
- package/dest/public/avm/avm_simulator.d.ts.map +1 -1
- package/dest/public/avm/avm_simulator.js +0 -2
- package/dest/public/avm/fixtures/avm_simulation_tester.d.ts.map +1 -1
- package/dest/public/avm/fixtures/avm_simulation_tester.js +5 -5
- package/dest/public/avm/fixtures/index.d.ts +4 -4
- package/dest/public/avm/fixtures/index.d.ts.map +1 -1
- package/dest/public/avm/fixtures/index.js +9 -6
- package/dest/public/avm/fixtures/simple_contract_data_source.d.ts +1 -2
- package/dest/public/avm/fixtures/simple_contract_data_source.d.ts.map +1 -1
- package/dest/public/avm/fixtures/simple_contract_data_source.js +0 -3
- package/dest/public/avm/journal/journal.d.ts +16 -70
- package/dest/public/avm/journal/journal.d.ts.map +1 -1
- package/dest/public/avm/journal/journal.js +88 -210
- package/dest/public/avm/journal/nullifiers.d.ts +2 -2
- package/dest/public/avm/journal/nullifiers.d.ts.map +1 -1
- package/dest/public/avm/journal/public_storage.d.ts +2 -2
- package/dest/public/avm/journal/public_storage.d.ts.map +1 -1
- package/dest/public/avm/test_utils.d.ts +10 -13
- package/dest/public/avm/test_utils.d.ts.map +1 -1
- package/dest/public/avm/test_utils.js +8 -13
- package/dest/public/fixtures/public_tx_simulation_tester.d.ts +3 -3
- package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
- package/dest/public/fixtures/public_tx_simulation_tester.js +10 -9
- package/dest/public/hinting_db_sources.d.ts +19 -0
- package/dest/public/hinting_db_sources.d.ts.map +1 -0
- package/dest/public/hinting_db_sources.js +36 -0
- package/dest/public/public_db_sources.d.ts +46 -22
- package/dest/public/public_db_sources.d.ts.map +1 -1
- package/dest/public/public_db_sources.js +82 -27
- package/dest/public/public_processor/public_processor.d.ts +5 -5
- package/dest/public/public_processor/public_processor.d.ts.map +1 -1
- package/dest/public/public_processor/public_processor.js +21 -20
- package/dest/public/public_tx_simulator/public_tx_context.d.ts +9 -14
- package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/public_tx_context.js +15 -19
- package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +9 -6
- package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/public_tx_simulator.js +28 -14
- package/dest/public/side_effect_trace.d.ts +6 -22
- package/dest/public/side_effect_trace.d.ts.map +1 -1
- package/dest/public/side_effect_trace.js +11 -70
- package/dest/public/side_effect_trace_interface.d.ts +5 -19
- package/dest/public/side_effect_trace_interface.d.ts.map +1 -1
- package/dest/testing.d.ts +2 -0
- package/dest/testing.d.ts.map +1 -0
- package/dest/testing.js +1 -0
- package/package.json +15 -14
- package/src/common/db_interfaces.ts +32 -18
- package/src/common/debug_fn_name.ts +2 -2
- package/src/common/message_load_oracle_inputs.ts +8 -0
- package/src/private/acvm/acvm.ts +8 -24
- package/src/private/acvm/deserialize.ts +35 -29
- package/src/private/acvm/oracle/oracle.ts +171 -129
- package/src/private/acvm/oracle/typed_oracle.ts +7 -3
- package/src/private/acvm/serialize.ts +28 -0
- package/src/private/execution_data_provider.ts +19 -14
- package/src/private/private_execution.ts +11 -7
- package/src/private/private_execution_oracle.ts +5 -1
- package/src/private/providers/acvm_native.ts +17 -6
- package/src/private/providers/acvm_wasm.ts +27 -20
- package/src/private/providers/acvm_wasm_with_blobs.ts +15 -12
- package/src/private/providers/circuit_recording/circuit_recorder.ts +283 -0
- package/src/private/providers/circuit_recording/simulation_provider_recorder_wrapper.ts +82 -0
- package/src/private/providers/simulation_provider.ts +30 -5
- package/src/private/simulator.ts +5 -3
- package/src/private/unconstrained_execution.ts +8 -4
- package/src/private/unconstrained_execution_oracle.ts +15 -9
- package/src/public/avm/avm_simulator.ts +0 -2
- package/src/public/avm/fixtures/avm_simulation_tester.ts +8 -5
- package/src/public/avm/fixtures/index.ts +16 -10
- package/src/public/avm/fixtures/simple_contract_data_source.ts +1 -10
- package/src/public/avm/journal/journal.ts +119 -353
- package/src/public/avm/journal/nullifiers.ts +2 -2
- package/src/public/avm/journal/public_storage.ts +2 -2
- package/src/public/avm/test_utils.ts +20 -29
- package/src/public/fixtures/public_tx_simulation_tester.ts +9 -12
- package/src/public/hinting_db_sources.ts +71 -0
- package/src/public/public_db_sources.ts +134 -32
- package/src/public/public_processor/public_processor.ts +22 -21
- package/src/public/public_tx_simulator/public_tx_context.ts +30 -38
- package/src/public/public_tx_simulator/public_tx_simulator.ts +47 -17
- package/src/public/side_effect_trace.ts +8 -172
- package/src/public/side_effect_trace_interface.ts +4 -55
- package/src/testing.ts +1 -0
- package/dest/public/avm/bytecode_utils.d.ts +0 -5
- package/dest/public/avm/bytecode_utils.d.ts.map +0 -1
- package/dest/public/avm/bytecode_utils.js +0 -17
- package/src/public/avm/bytecode_utils.ts +0 -17
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Fr } from '@aztec/foundation/fields';
|
|
2
|
-
import type { FunctionArtifact, FunctionSelector } from '@aztec/stdlib/abi';
|
|
1
|
+
import type { Fr, Point } from '@aztec/foundation/fields';
|
|
2
|
+
import type { FunctionArtifact, FunctionArtifactWithContractName, FunctionSelector } from '@aztec/stdlib/abi';
|
|
3
3
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
4
4
|
import type { L2Block } from '@aztec/stdlib/block';
|
|
5
5
|
import type { CompleteAddress, ContractInstance } from '@aztec/stdlib/contract';
|
|
@@ -9,7 +9,7 @@ import type { NoteStatus } from '@aztec/stdlib/note';
|
|
|
9
9
|
import { type MerkleTreeId, type NullifierMembershipWitness, PublicDataWitness } from '@aztec/stdlib/trees';
|
|
10
10
|
import type { BlockHeader } from '@aztec/stdlib/tx';
|
|
11
11
|
|
|
12
|
-
import type {
|
|
12
|
+
import type { CommitmentsDBInterface } from '../common/db_interfaces.js';
|
|
13
13
|
import type { NoteData } from './acvm/index.js';
|
|
14
14
|
|
|
15
15
|
/**
|
|
@@ -33,7 +33,7 @@ export class ContractClassNotFoundError extends Error {
|
|
|
33
33
|
/**
|
|
34
34
|
* The interface for the data layer required to perform private and unconstrained execution.
|
|
35
35
|
*/
|
|
36
|
-
export interface ExecutionDataProvider extends
|
|
36
|
+
export interface ExecutionDataProvider extends CommitmentsDBInterface {
|
|
37
37
|
/**
|
|
38
38
|
* Returns a contract instance associated with an address, if available.
|
|
39
39
|
* @param address - Address.
|
|
@@ -49,13 +49,6 @@ export interface ExecutionDataProvider extends CommitmentsDB {
|
|
|
49
49
|
*/
|
|
50
50
|
getCompleteAddress(account: AztecAddress): Promise<CompleteAddress>;
|
|
51
51
|
|
|
52
|
-
/**
|
|
53
|
-
* Retrieve the auth witness for a given message hash.
|
|
54
|
-
* @param messageHash - The message hash.
|
|
55
|
-
* @returns A Promise that resolves to an array of field elements representing the auth witness.
|
|
56
|
-
*/
|
|
57
|
-
getAuthWitness(messageHash: Fr): Promise<Fr[] | undefined>;
|
|
58
|
-
|
|
59
52
|
/**
|
|
60
53
|
* Retrieve keys associated with a specific master public key and app address.
|
|
61
54
|
* @param pkMHash - The master public key hash.
|
|
@@ -90,7 +83,10 @@ export interface ExecutionDataProvider extends CommitmentsDB {
|
|
|
90
83
|
* @param selector - The corresponding function selector.
|
|
91
84
|
* @returns A Promise that resolves to a FunctionArtifact object.
|
|
92
85
|
*/
|
|
93
|
-
getFunctionArtifact(
|
|
86
|
+
getFunctionArtifact(
|
|
87
|
+
contractAddress: AztecAddress,
|
|
88
|
+
selector: FunctionSelector,
|
|
89
|
+
): Promise<FunctionArtifactWithContractName>;
|
|
94
90
|
|
|
95
91
|
/**
|
|
96
92
|
* Generates a stable function name for debug purposes.
|
|
@@ -157,7 +153,7 @@ export interface ExecutionDataProvider extends CommitmentsDB {
|
|
|
157
153
|
* @param blockNumber - The block number at which to get the witness.
|
|
158
154
|
* @param leafSlot - The slot of the public data in the public data tree.
|
|
159
155
|
*/
|
|
160
|
-
|
|
156
|
+
getPublicDataWitness(blockNumber: number, leafSlot: Fr): Promise<PublicDataWitness | undefined>;
|
|
161
157
|
|
|
162
158
|
/**
|
|
163
159
|
* Gets the storage value at the given contract storage slot.
|
|
@@ -239,10 +235,11 @@ export interface ExecutionDataProvider extends CommitmentsDB {
|
|
|
239
235
|
|
|
240
236
|
/**
|
|
241
237
|
* Processes the tagged logs returned by syncTaggedLogs by decrypting them and storing them in the database.
|
|
238
|
+
* @param contractAddress - The address of the contract that emitted the logs.
|
|
242
239
|
* @param logs - The logs to process.
|
|
243
240
|
* @param recipient - The recipient of the logs.
|
|
244
241
|
*/
|
|
245
|
-
processTaggedLogs(logs: TxScopedL2Log[], recipient: AztecAddress): Promise<void>;
|
|
242
|
+
processTaggedLogs(contractAddress: AztecAddress, logs: TxScopedL2Log[], recipient: AztecAddress): Promise<void>;
|
|
246
243
|
|
|
247
244
|
/**
|
|
248
245
|
* Delivers the preimage and metadata of a committed note so that it can be later requested via the `getNotes`
|
|
@@ -320,4 +317,12 @@ export interface ExecutionDataProvider extends CommitmentsDB {
|
|
|
320
317
|
* @param numEntries - The number of entries to copy.
|
|
321
318
|
*/
|
|
322
319
|
copyCapsule(contractAddress: AztecAddress, srcSlot: Fr, dstSlot: Fr, numEntries: number): Promise<void>;
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Retrieves the shared secret for a given address and ephemeral public key.
|
|
323
|
+
* @param address - The address to get the secret for.
|
|
324
|
+
* @param ephPk - The ephemeral public key to get the secret for.
|
|
325
|
+
* @returns The secret for the given address.
|
|
326
|
+
*/
|
|
327
|
+
getSharedSecret(address: AztecAddress, ephPk: Point): Promise<Point>;
|
|
323
328
|
}
|
|
@@ -3,7 +3,12 @@ import { Fr } from '@aztec/foundation/fields';
|
|
|
3
3
|
import { createLogger } from '@aztec/foundation/log';
|
|
4
4
|
import { Timer } from '@aztec/foundation/timer';
|
|
5
5
|
import { ProtocolContractAddress } from '@aztec/protocol-contracts';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
type FunctionArtifact,
|
|
8
|
+
type FunctionArtifactWithContractName,
|
|
9
|
+
type FunctionSelector,
|
|
10
|
+
countArgumentsSize,
|
|
11
|
+
} from '@aztec/stdlib/abi';
|
|
7
12
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
8
13
|
import type { ContractInstance } from '@aztec/stdlib/contract';
|
|
9
14
|
import type { AztecNode } from '@aztec/stdlib/interfaces/client';
|
|
@@ -13,7 +18,7 @@ import type { CircuitWitnessGenerationStats } from '@aztec/stdlib/stats';
|
|
|
13
18
|
import { PrivateCallExecutionResult } from '@aztec/stdlib/tx';
|
|
14
19
|
|
|
15
20
|
import { ExecutionError, resolveAssertionMessageFromError } from '../common/errors.js';
|
|
16
|
-
import {
|
|
21
|
+
import { witnessMapToFields } from './acvm/deserialize.js';
|
|
17
22
|
import { type ACVMWitness, Oracle, extractCallStack } from './acvm/index.js';
|
|
18
23
|
import type { ExecutionDataProvider } from './execution_data_provider.js';
|
|
19
24
|
import type { PrivateExecutionOracle } from './private_execution_oracle.js';
|
|
@@ -25,19 +30,18 @@ import type { SimulationProvider } from './providers/simulation_provider.js';
|
|
|
25
30
|
export async function executePrivateFunction(
|
|
26
31
|
simulator: SimulationProvider,
|
|
27
32
|
privateExecutionOracle: PrivateExecutionOracle,
|
|
28
|
-
artifact:
|
|
33
|
+
artifact: FunctionArtifactWithContractName,
|
|
29
34
|
contractAddress: AztecAddress,
|
|
30
35
|
functionSelector: FunctionSelector,
|
|
31
36
|
log = createLogger('simulator:private_execution'),
|
|
32
37
|
): Promise<PrivateCallExecutionResult> {
|
|
33
38
|
const functionName = await privateExecutionOracle.getDebugFunctionName();
|
|
34
39
|
log.verbose(`Executing private function ${functionName}`, { contract: contractAddress });
|
|
35
|
-
const acir = artifact.bytecode;
|
|
36
40
|
const initialWitness = privateExecutionOracle.getInitialWitness(artifact);
|
|
37
41
|
const acvmCallback = new Oracle(privateExecutionOracle);
|
|
38
42
|
const timer = new Timer();
|
|
39
43
|
const acirExecutionResult = await simulator
|
|
40
|
-
.executeUserCircuit(
|
|
44
|
+
.executeUserCircuit(initialWitness, artifact, acvmCallback)
|
|
41
45
|
.catch((err: Error) => {
|
|
42
46
|
err.message = resolveAssertionMessageFromError(err, artifact);
|
|
43
47
|
throw new ExecutionError(
|
|
@@ -79,7 +83,7 @@ export async function executePrivateFunction(
|
|
|
79
83
|
log.debug(`Returning from call to ${contractAddress.toString()}:${functionSelector}`);
|
|
80
84
|
|
|
81
85
|
return new PrivateCallExecutionResult(
|
|
82
|
-
|
|
86
|
+
artifact.bytecode,
|
|
83
87
|
Buffer.from(artifact.verificationKey!, 'base64'),
|
|
84
88
|
partialWitness,
|
|
85
89
|
publicInputs,
|
|
@@ -113,7 +117,7 @@ export function extractPrivateCircuitPublicInputs(
|
|
|
113
117
|
if (returnedField === undefined) {
|
|
114
118
|
throw new Error(`Missing return value for index ${i}`);
|
|
115
119
|
}
|
|
116
|
-
returnData.push(
|
|
120
|
+
returnData.push(Fr.fromString(returnedField));
|
|
117
121
|
}
|
|
118
122
|
return PrivateCircuitPublicInputs.fromFields(returnData);
|
|
119
123
|
}
|
|
@@ -606,7 +606,11 @@ export class PrivateExecutionOracle extends UnconstrainedExecutionOracle {
|
|
|
606
606
|
this.scopes,
|
|
607
607
|
);
|
|
608
608
|
for (const [recipient, taggedLogs] of taggedLogsByRecipient.entries()) {
|
|
609
|
-
await this.executionDataProvider.processTaggedLogs(
|
|
609
|
+
await this.executionDataProvider.processTaggedLogs(
|
|
610
|
+
this.contractAddress,
|
|
611
|
+
taggedLogs,
|
|
612
|
+
AztecAddress.fromString(recipient),
|
|
613
|
+
);
|
|
610
614
|
}
|
|
611
615
|
|
|
612
616
|
await this.executionDataProvider.removeNullifiedNotes(this.contractAddress);
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { runInDirectory } from '@aztec/foundation/fs';
|
|
2
2
|
import { createLogger } from '@aztec/foundation/log';
|
|
3
3
|
import { Timer } from '@aztec/foundation/timer';
|
|
4
|
-
import type { WitnessMap } from '@aztec/noir-
|
|
5
|
-
import type {
|
|
4
|
+
import type { WitnessMap } from '@aztec/noir-acvm_js';
|
|
5
|
+
import type { ForeignCallHandler } from '@aztec/noir-protocol-circuits-types/types';
|
|
6
|
+
import type { FunctionArtifactWithContractName } from '@aztec/stdlib/abi';
|
|
7
|
+
import type { NoirCompiledCircuitWithName } from '@aztec/stdlib/noir';
|
|
6
8
|
|
|
7
9
|
import * as proc from 'child_process';
|
|
8
10
|
import { promises as fs } from 'fs';
|
|
@@ -136,12 +138,21 @@ export async function executeNativeCircuit(
|
|
|
136
138
|
|
|
137
139
|
export class NativeACVMSimulator implements SimulationProvider {
|
|
138
140
|
constructor(private workingDirectory: string, private pathToAcvm: string, private witnessFilename?: string) {}
|
|
139
|
-
|
|
141
|
+
|
|
142
|
+
async executeProtocolCircuit(
|
|
143
|
+
input: ACVMWitness,
|
|
144
|
+
artifact: NoirCompiledCircuitWithName,
|
|
145
|
+
callback: ForeignCallHandler | undefined,
|
|
146
|
+
): Promise<ACVMWitness> {
|
|
140
147
|
// Execute the circuit on those initial witness values
|
|
141
148
|
|
|
149
|
+
if (callback) {
|
|
150
|
+
throw new Error('Native ACVM simulator does not support foreign calls. Ignoring callback.');
|
|
151
|
+
}
|
|
152
|
+
|
|
142
153
|
const operation = async (directory: string) => {
|
|
143
154
|
// Decode the bytecode from base64 since the acvm does not know about base64 encoding
|
|
144
|
-
const decodedBytecode = Buffer.from(
|
|
155
|
+
const decodedBytecode = Buffer.from(artifact.bytecode, 'base64');
|
|
145
156
|
// Execute the circuit
|
|
146
157
|
const result = await executeNativeCircuit(
|
|
147
158
|
input,
|
|
@@ -162,8 +173,8 @@ export class NativeACVMSimulator implements SimulationProvider {
|
|
|
162
173
|
}
|
|
163
174
|
|
|
164
175
|
executeUserCircuit(
|
|
165
|
-
|
|
166
|
-
|
|
176
|
+
_input: ACVMWitness,
|
|
177
|
+
_artifact: FunctionArtifactWithContractName,
|
|
167
178
|
_callback: ACIRCallback,
|
|
168
179
|
): Promise<ACIRExecutionResult> {
|
|
169
180
|
throw new Error('Not implemented');
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { createLogger } from '@aztec/foundation/log';
|
|
2
|
-
import initACVM, { type ExecutionError, executeCircuit } from '@aztec/noir-acvm_js';
|
|
2
|
+
import initACVM, { type ExecutionError, type ForeignCallHandler, executeCircuit } from '@aztec/noir-acvm_js';
|
|
3
3
|
import initAbi from '@aztec/noir-noirc_abi';
|
|
4
|
-
import {
|
|
5
|
-
import type {
|
|
6
|
-
import type { NoirCompiledCircuit } from '@aztec/stdlib/noir';
|
|
4
|
+
import type { FunctionArtifactWithContractName } from '@aztec/stdlib/abi';
|
|
5
|
+
import type { NoirCompiledCircuitWithName } from '@aztec/stdlib/noir';
|
|
7
6
|
|
|
8
|
-
import { type ACIRCallback, acvm } from '../acvm/acvm.js';
|
|
7
|
+
import { type ACIRCallback, type ACIRExecutionResult, acvm } from '../acvm/acvm.js';
|
|
9
8
|
import type { ACVMWitness } from '../acvm/acvm_types.js';
|
|
10
9
|
import { type SimulationProvider, enrichNoirError } from './simulation_provider.js';
|
|
11
10
|
|
|
@@ -22,41 +21,49 @@ export class WASMSimulator implements SimulationProvider {
|
|
|
22
21
|
}
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
async executeProtocolCircuit(
|
|
26
|
-
|
|
24
|
+
async executeProtocolCircuit(
|
|
25
|
+
input: ACVMWitness,
|
|
26
|
+
artifact: NoirCompiledCircuitWithName,
|
|
27
|
+
callback: ForeignCallHandler,
|
|
28
|
+
): Promise<ACVMWitness> {
|
|
29
|
+
this.log.debug('init', { hash: artifact.hash });
|
|
27
30
|
await this.init();
|
|
28
|
-
|
|
29
|
-
//
|
|
31
|
+
|
|
30
32
|
// Decode the bytecode from base64 since the acvm does not know about base64 encoding
|
|
31
|
-
const decodedBytecode = Buffer.from(
|
|
33
|
+
const decodedBytecode = Buffer.from(artifact.bytecode, 'base64');
|
|
32
34
|
//
|
|
33
35
|
// Execute the circuit
|
|
34
36
|
try {
|
|
35
|
-
const
|
|
37
|
+
const result = await executeCircuit(
|
|
36
38
|
decodedBytecode,
|
|
37
39
|
input,
|
|
38
|
-
|
|
40
|
+
callback, // handle calls to debug_log
|
|
39
41
|
);
|
|
40
|
-
this.log.debug('execution successful', { hash:
|
|
41
|
-
return
|
|
42
|
+
this.log.debug('execution successful', { hash: artifact.hash });
|
|
43
|
+
return result;
|
|
42
44
|
} catch (err) {
|
|
43
|
-
// Typescript types
|
|
45
|
+
// Typescript types caught errors as unknown or any, so we need to narrow its type to check if it has raw
|
|
46
|
+
// assertion payload.
|
|
44
47
|
if (typeof err === 'object' && err !== null && 'rawAssertionPayload' in err) {
|
|
45
|
-
const parsed = enrichNoirError(
|
|
48
|
+
const parsed = enrichNoirError(artifact, err as ExecutionError);
|
|
46
49
|
this.log.debug('execution failed', {
|
|
47
|
-
hash:
|
|
50
|
+
hash: artifact.hash,
|
|
48
51
|
error: parsed,
|
|
49
52
|
message: parsed.message,
|
|
50
53
|
});
|
|
51
54
|
throw parsed;
|
|
52
55
|
}
|
|
53
|
-
this.log.debug('execution failed', { hash:
|
|
56
|
+
this.log.debug('execution failed', { hash: artifact.hash, error: err });
|
|
54
57
|
throw new Error(`Circuit execution failed: ${err}`);
|
|
55
58
|
}
|
|
56
59
|
}
|
|
57
60
|
|
|
58
|
-
async executeUserCircuit(
|
|
61
|
+
async executeUserCircuit(
|
|
62
|
+
input: ACVMWitness,
|
|
63
|
+
artifact: FunctionArtifactWithContractName,
|
|
64
|
+
callback: ACIRCallback,
|
|
65
|
+
): Promise<ACIRExecutionResult> {
|
|
59
66
|
await this.init();
|
|
60
|
-
return acvm(
|
|
67
|
+
return acvm(artifact.bytecode, input, callback);
|
|
61
68
|
}
|
|
62
69
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type ExecutionError, executeCircuit } from '@aztec/noir-acvm_js';
|
|
2
|
-
import { foreignCallHandler } from '@aztec/noir-protocol-circuits-types/server';
|
|
1
|
+
import { type ExecutionError, type ForeignCallHandler, executeCircuit } from '@aztec/noir-acvm_js';
|
|
3
2
|
import type { WitnessMap } from '@aztec/noir-types';
|
|
4
|
-
import type {
|
|
3
|
+
import type { FunctionArtifactWithContractName } from '@aztec/stdlib/abi';
|
|
4
|
+
import type { NoirCompiledCircuitWithName } from '@aztec/stdlib/noir';
|
|
5
5
|
|
|
6
6
|
import type { ACIRCallback, ACIRExecutionResult } from '../acvm/acvm.js';
|
|
7
7
|
import type { ACVMWitness } from '../acvm/acvm_types.js';
|
|
@@ -15,33 +15,36 @@ import { type SimulationProvider, enrichNoirError } from './simulation_provider.
|
|
|
15
15
|
* It is only used in the context of server-side code executing simulated protocol circuits.
|
|
16
16
|
*/
|
|
17
17
|
export class WASMSimulatorWithBlobs implements SimulationProvider {
|
|
18
|
-
async executeProtocolCircuit(
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
async executeProtocolCircuit(
|
|
19
|
+
input: WitnessMap,
|
|
20
|
+
artifact: NoirCompiledCircuitWithName,
|
|
21
|
+
callback: ForeignCallHandler,
|
|
22
|
+
): Promise<WitnessMap> {
|
|
21
23
|
// Decode the bytecode from base64 since the acvm does not know about base64 encoding
|
|
22
|
-
const decodedBytecode = Buffer.from(
|
|
24
|
+
const decodedBytecode = Buffer.from(artifact.bytecode, 'base64');
|
|
23
25
|
//
|
|
24
26
|
// Execute the circuit
|
|
25
27
|
try {
|
|
26
28
|
const _witnessMap = await executeCircuit(
|
|
27
29
|
decodedBytecode,
|
|
28
30
|
input,
|
|
29
|
-
|
|
31
|
+
callback, // handle calls to debug_log and evaluate_blobs mock
|
|
30
32
|
);
|
|
31
33
|
|
|
32
34
|
return _witnessMap;
|
|
33
35
|
} catch (err) {
|
|
34
|
-
// Typescript types
|
|
36
|
+
// Typescript types caught errors as unknown or any, so we need to narrow its type to check if it has raw
|
|
37
|
+
// assertion payload.
|
|
35
38
|
if (typeof err === 'object' && err !== null && 'rawAssertionPayload' in err) {
|
|
36
|
-
throw enrichNoirError(
|
|
39
|
+
throw enrichNoirError(artifact, err as ExecutionError);
|
|
37
40
|
}
|
|
38
41
|
throw new Error(`Circuit execution failed: ${err}`);
|
|
39
42
|
}
|
|
40
43
|
}
|
|
41
44
|
|
|
42
45
|
executeUserCircuit(
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
_input: ACVMWitness,
|
|
47
|
+
_artifact: FunctionArtifactWithContractName,
|
|
45
48
|
_callback: ACIRCallback,
|
|
46
49
|
): Promise<ACIRExecutionResult> {
|
|
47
50
|
throw new Error('Not implemented');
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
2
|
+
import type { ForeignCallHandler, ForeignCallInput, ForeignCallOutput } from '@aztec/noir-acvm_js';
|
|
3
|
+
|
|
4
|
+
import { createHash } from 'crypto';
|
|
5
|
+
import fs from 'fs/promises';
|
|
6
|
+
import path from 'path';
|
|
7
|
+
|
|
8
|
+
import type { ACIRCallback } from '../../acvm/acvm.js';
|
|
9
|
+
import type { ACVMWitness } from '../../acvm/acvm_types.js';
|
|
10
|
+
import { Oracle } from '../../acvm/oracle/oracle.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Class responsible for recording circuit inputs necessary to replay the circuit. These inputs are the initial witness
|
|
14
|
+
* map and the oracle calls made during the circuit execution/witness generation.
|
|
15
|
+
*
|
|
16
|
+
* The recording is stored in a JSON file called `circuit_name_circuit_function_name_YYYY-MM-DD_N.json` where N is
|
|
17
|
+
* a counter to ensure unique filenames. The file is stored in the `recordDir` directory provided as a parameter to
|
|
18
|
+
* CircuitRecorder.start().
|
|
19
|
+
*
|
|
20
|
+
* Example recording file:
|
|
21
|
+
* ```json
|
|
22
|
+
* {
|
|
23
|
+
* "circuitName": "AMM",
|
|
24
|
+
* "functionName": "add_liquidity",
|
|
25
|
+
* "bytecodeMd5Hash": "b46c640ed38f20eac5f61a5e41d8dd1e",
|
|
26
|
+
* "timestamp": 1740691464360,
|
|
27
|
+
* "inputs": {
|
|
28
|
+
* "0": "0x1e89de1f0ad5204263733b7ddf65bec45b8f44714a4da85a46474dad677679ef",
|
|
29
|
+
* "1": "0x00f4d59c0ff773427bb0fed5b422557ca4dc5655abe53d31fa9408cb3c5a672f",
|
|
30
|
+
* "5": "0x000000000000000000000000000000000000000000000000000000000000000f"
|
|
31
|
+
* },
|
|
32
|
+
* "oracleCalls": [
|
|
33
|
+
* {
|
|
34
|
+
* "name": "loadCapsule",
|
|
35
|
+
* "inputs": [
|
|
36
|
+
* [
|
|
37
|
+
* "0x102422483bad6abd385948435667e144ac4c272576e325e7563608876cd446fd"
|
|
38
|
+
* ],
|
|
39
|
+
* [
|
|
40
|
+
* "0x000000000000000000000000000000000000000000000000000000000000004d"
|
|
41
|
+
* ],
|
|
42
|
+
* [
|
|
43
|
+
* "0x0000000000000000000000000000000000000000000000000000000000000001"
|
|
44
|
+
* ]
|
|
45
|
+
* ],
|
|
46
|
+
* "outputs": [
|
|
47
|
+
* "0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
48
|
+
* [
|
|
49
|
+
* "0x0000000000000000000000000000000000000000000000000000000000000000"
|
|
50
|
+
* ]
|
|
51
|
+
* ]
|
|
52
|
+
* },
|
|
53
|
+
* {
|
|
54
|
+
* "name": "syncNotes",
|
|
55
|
+
* "inputs": []
|
|
56
|
+
* }
|
|
57
|
+
* ]
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export class CircuitRecorder {
|
|
62
|
+
private readonly logger = createLogger('simulator:acvm:recording');
|
|
63
|
+
private isFirstCall = true;
|
|
64
|
+
|
|
65
|
+
private constructor(private readonly filePath: string) {}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Initializes a new circuit recording session.
|
|
69
|
+
* @param recordDir - Directory to store the recording
|
|
70
|
+
* @param input - Circuit input witness
|
|
71
|
+
* @param circuitBytecode - Compiled circuit bytecode
|
|
72
|
+
* @param circuitName - Name of the circuit
|
|
73
|
+
* @param functionName - Name of the circuit function (defaults to 'main'). This is meaningful only for
|
|
74
|
+
* contracts as protocol circuits artifacts always contain a single entrypoint function called 'main'.
|
|
75
|
+
* @returns A new CircuitRecorder instance
|
|
76
|
+
*/
|
|
77
|
+
static async start(
|
|
78
|
+
recordDir: string,
|
|
79
|
+
input: ACVMWitness,
|
|
80
|
+
circuitBytecode: Buffer,
|
|
81
|
+
circuitName: string,
|
|
82
|
+
functionName: string = 'main',
|
|
83
|
+
): Promise<CircuitRecorder> {
|
|
84
|
+
const recording = {
|
|
85
|
+
circuitName: circuitName,
|
|
86
|
+
functionName: functionName,
|
|
87
|
+
bytecodeMd5Hash: createHash('md5').update(circuitBytecode).digest('hex'),
|
|
88
|
+
timestamp: Date.now(),
|
|
89
|
+
inputs: Object.fromEntries(input),
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const recordingStringWithoutClosingBracket = JSON.stringify(recording, null, 2).slice(0, -2);
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
// Check if the recording directory exists and is a directory
|
|
96
|
+
const stats = await fs.stat(recordDir);
|
|
97
|
+
if (!stats.isDirectory()) {
|
|
98
|
+
throw new Error(`Recording path ${recordDir} exists but is not a directory`);
|
|
99
|
+
}
|
|
100
|
+
} catch (err) {
|
|
101
|
+
if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
102
|
+
// The directory does not exist so we create it
|
|
103
|
+
await fs.mkdir(recordDir, { recursive: true });
|
|
104
|
+
} else {
|
|
105
|
+
throw err;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const filePath = await CircuitRecorder.#computeFilePathAndStoreInitialRecording(
|
|
110
|
+
recordDir,
|
|
111
|
+
circuitName,
|
|
112
|
+
functionName,
|
|
113
|
+
recordingStringWithoutClosingBracket,
|
|
114
|
+
);
|
|
115
|
+
return new CircuitRecorder(filePath);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Computes a unique file path for the recording by trying different counter values.
|
|
120
|
+
* This is needed because multiple recordings of the same circuit could be happening simultaneously or an older
|
|
121
|
+
* recording might be present.
|
|
122
|
+
* @param recordDir - Directory to store the recording
|
|
123
|
+
* @param circuitName - Name of the circuit
|
|
124
|
+
* @param functionName - Name of the circuit function
|
|
125
|
+
* @param recordingContent - Initial recording content
|
|
126
|
+
* @returns A unique file path for the recording
|
|
127
|
+
*/
|
|
128
|
+
static async #computeFilePathAndStoreInitialRecording(
|
|
129
|
+
recordDir: string,
|
|
130
|
+
circuitName: string,
|
|
131
|
+
functionName: string,
|
|
132
|
+
recordingContent: string,
|
|
133
|
+
): Promise<string> {
|
|
134
|
+
let counter = 0;
|
|
135
|
+
while (true) {
|
|
136
|
+
try {
|
|
137
|
+
const filePath = getFilePath(recordDir, circuitName, functionName, counter);
|
|
138
|
+
// Write the initial recording content to the file
|
|
139
|
+
await fs.writeFile(filePath, recordingContent + ',\n "oracleCalls": [\n', {
|
|
140
|
+
flag: 'wx', // wx flag fails if file exists
|
|
141
|
+
});
|
|
142
|
+
return filePath;
|
|
143
|
+
} catch (err) {
|
|
144
|
+
if ((err as NodeJS.ErrnoException).code === 'EEXIST') {
|
|
145
|
+
counter++;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
throw err;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Wraps a callback to record all oracle/foreign calls.
|
|
155
|
+
* @param callback - The original callback to wrap, either a user circuit callback or protocol circuit callback.
|
|
156
|
+
* @returns A wrapped callback that records all oracle interactions.
|
|
157
|
+
*/
|
|
158
|
+
wrapCallback(callback: ACIRCallback | ForeignCallHandler | undefined): ACIRCallback | ForeignCallHandler | undefined {
|
|
159
|
+
if (!callback) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
if (this.#isACIRCallback(callback)) {
|
|
163
|
+
return this.#wrapUserCircuitCallback(callback);
|
|
164
|
+
}
|
|
165
|
+
return this.#wrapProtocolCircuitCallback(callback);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Type guard to check if a callback is an ACIRCallback.
|
|
170
|
+
*/
|
|
171
|
+
#isACIRCallback(callback: ACIRCallback | ForeignCallHandler): callback is ACIRCallback {
|
|
172
|
+
return typeof callback === 'object' && callback !== null && !('call' in callback);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Wraps a user circuit callback to record all oracle calls.
|
|
177
|
+
* @param callback - The original circuit callback.
|
|
178
|
+
* @returns A wrapped callback that records all oracle interactions which is to be provided to the ACVM.
|
|
179
|
+
*/
|
|
180
|
+
#wrapUserCircuitCallback(callback: ACIRCallback): ACIRCallback {
|
|
181
|
+
const recordingCallback: ACIRCallback = {} as ACIRCallback;
|
|
182
|
+
const oracleMethods = Object.getOwnPropertyNames(Oracle.prototype).filter(name => name !== 'constructor');
|
|
183
|
+
|
|
184
|
+
for (const name of oracleMethods) {
|
|
185
|
+
const fn = callback[name as keyof ACIRCallback];
|
|
186
|
+
if (!fn) {
|
|
187
|
+
throw new Error(`Oracle method ${name} not found when setting up recording callback`);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
recordingCallback[name as keyof ACIRCallback] = (...args: ForeignCallInput[]): ReturnType<typeof fn> => {
|
|
191
|
+
const result = fn.call(callback, ...args);
|
|
192
|
+
if (result instanceof Promise) {
|
|
193
|
+
return result.then(async r => {
|
|
194
|
+
await this.#recordCall(name, args, r);
|
|
195
|
+
return r;
|
|
196
|
+
}) as ReturnType<typeof fn>;
|
|
197
|
+
}
|
|
198
|
+
void this.#recordCall(name, args, result);
|
|
199
|
+
return result;
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return recordingCallback;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Wraps a protocol circuit callback to record all oracle calls.
|
|
208
|
+
* @param callback - The original oracle circuit callback.
|
|
209
|
+
* @returns A wrapped handler that records all oracle interactions which is to be provided to the ACVM.
|
|
210
|
+
*/
|
|
211
|
+
#wrapProtocolCircuitCallback(callback: ForeignCallHandler): ForeignCallHandler {
|
|
212
|
+
return async (name: string, inputs: ForeignCallInput[]): Promise<ForeignCallOutput[]> => {
|
|
213
|
+
const result = await callback(name, inputs);
|
|
214
|
+
await this.#recordCall(name, inputs, result);
|
|
215
|
+
return result;
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Records a single oracle/foreign call with its inputs and outputs.
|
|
221
|
+
* @param name - Name of the call
|
|
222
|
+
* @param inputs - Input arguments
|
|
223
|
+
* @param outputs - Output results
|
|
224
|
+
*/
|
|
225
|
+
async #recordCall(name: string, inputs: unknown[], outputs: unknown) {
|
|
226
|
+
try {
|
|
227
|
+
const entry = {
|
|
228
|
+
name,
|
|
229
|
+
inputs,
|
|
230
|
+
outputs,
|
|
231
|
+
};
|
|
232
|
+
const prefix = this.isFirstCall ? ' ' : ' ,';
|
|
233
|
+
this.isFirstCall = false;
|
|
234
|
+
await fs.appendFile(this.filePath, prefix + JSON.stringify(entry) + '\n');
|
|
235
|
+
} catch (err) {
|
|
236
|
+
this.logger.error('Failed to log circuit call', { error: err });
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Finalizes the recording file by adding closing brackets. Without calling this method, the recording file is
|
|
242
|
+
* incomplete and it fails to parse.
|
|
243
|
+
*/
|
|
244
|
+
async finish(): Promise<void> {
|
|
245
|
+
try {
|
|
246
|
+
await fs.appendFile(this.filePath, ' ]\n}\n');
|
|
247
|
+
} catch (err) {
|
|
248
|
+
this.logger.error('Failed to finalize recording file', { error: err });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Finalizes the recording file by adding the error and closing brackets. Without calling this method or `finish`,
|
|
254
|
+
* the recording file is incomplete and it fails to parse.
|
|
255
|
+
* @param error - The error that occurred during circuit execution
|
|
256
|
+
*/
|
|
257
|
+
async finishWithError(error: unknown): Promise<void> {
|
|
258
|
+
try {
|
|
259
|
+
await fs.appendFile(this.filePath, ' ],\n');
|
|
260
|
+
await fs.appendFile(this.filePath, ` "error": ${JSON.stringify(error)}\n`);
|
|
261
|
+
await fs.appendFile(this.filePath, '}\n');
|
|
262
|
+
} catch (err) {
|
|
263
|
+
this.logger.error('Failed to finalize recording file with error', { error: err });
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Generates a file path for storing circuit recordings. The format of the filename is:
|
|
270
|
+
* `circuit_name_circuit_function_name_YYYY-MM-DD_N.json` where N is a counter to ensure unique filenames.
|
|
271
|
+
* @param recordDir - Base directory for recordings
|
|
272
|
+
* @param circuitName - Name of the circuit
|
|
273
|
+
* @param functionName - Name of the circuit function
|
|
274
|
+
* @param counter - Counter to ensure unique filenames. This is expected to be incremented in a loop until there is no
|
|
275
|
+
* existing file with the same name.
|
|
276
|
+
* @returns A file path for the recording.
|
|
277
|
+
*/
|
|
278
|
+
function getFilePath(recordDir: string, circuitName: string, functionName: string, counter: number): string {
|
|
279
|
+
const date = new Date();
|
|
280
|
+
const formattedDate = date.toISOString().split('T')[0];
|
|
281
|
+
const filename = `${circuitName}_${functionName}_${formattedDate}_${counter}.json`;
|
|
282
|
+
return path.join(recordDir, filename);
|
|
283
|
+
}
|