@aztec/simulator 0.87.5 → 0.87.6
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/client.d.ts +5 -3
- package/dest/client.d.ts.map +1 -1
- package/dest/client.js +4 -2
- package/dest/private/acvm/acvm.d.ts +5 -2
- package/dest/private/acvm/acvm.d.ts.map +1 -1
- package/dest/private/acvm/index.d.ts +0 -1
- package/dest/private/acvm/index.d.ts.map +1 -1
- package/dest/private/acvm/index.js +0 -1
- package/dest/private/{providers/acvm_native.d.ts → acvm_native.d.ts} +4 -4
- package/dest/private/acvm_native.d.ts.map +1 -0
- package/dest/private/{providers/acvm_wasm.d.ts → acvm_wasm.d.ts} +4 -4
- package/dest/private/acvm_wasm.d.ts.map +1 -0
- package/dest/private/{providers/acvm_wasm.js → acvm_wasm.js} +2 -2
- package/dest/private/{providers/acvm_wasm_with_blobs.d.ts → acvm_wasm_with_blobs.d.ts} +5 -5
- package/dest/private/acvm_wasm_with_blobs.d.ts.map +1 -0
- package/dest/private/{providers/acvm_wasm_with_blobs.js → acvm_wasm_with_blobs.js} +2 -2
- package/dest/private/{providers/circuit_recording → circuit_recording}/circuit_recorder.d.ts +41 -21
- package/dest/private/circuit_recording/circuit_recorder.d.ts.map +1 -0
- package/dest/private/circuit_recording/circuit_recorder.js +209 -0
- package/dest/private/circuit_recording/file_circuit_recorder.d.ts +31 -0
- package/dest/private/circuit_recording/file_circuit_recorder.d.ts.map +1 -0
- package/dest/private/circuit_recording/file_circuit_recorder.js +135 -0
- package/dest/private/circuit_recording/memory_circuit_recorder.d.ts +5 -0
- package/dest/private/circuit_recording/memory_circuit_recorder.d.ts.map +1 -0
- package/dest/private/circuit_recording/memory_circuit_recorder.js +9 -0
- package/dest/private/{providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts → circuit_recording/simulator_recorder_wrapper.d.ts} +9 -7
- package/dest/private/circuit_recording/simulator_recorder_wrapper.d.ts.map +1 -0
- package/dest/private/{providers/circuit_recording/simulation_provider_recorder_wrapper.js → circuit_recording/simulator_recorder_wrapper.js} +18 -13
- package/dest/private/{providers/simulation_provider.d.ts → circuit_simulator.d.ts} +4 -4
- package/dest/private/circuit_simulator.d.ts.map +1 -0
- package/dest/private/{providers/simulation_provider.js → circuit_simulator.js} +1 -1
- package/dest/private/factory.d.ts +12 -0
- package/dest/private/factory.d.ts.map +1 -0
- package/dest/private/{providers/factory.js → factory.js} +2 -2
- package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts +1 -0
- package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts.map +1 -1
- package/dest/public/avm/fixtures/base_avm_simulation_tester.js +6 -0
- package/dest/public/public_tx_simulator/apps_tests/amm_test.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/apps_tests/amm_test.js +27 -55
- package/dest/server.d.ts +6 -3
- package/dest/server.d.ts.map +1 -1
- package/dest/server.js +5 -2
- package/dest/testing.d.ts +1 -1
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +1 -1
- package/package.json +15 -15
- package/src/client.ts +5 -3
- package/src/private/acvm/acvm.ts +5 -3
- package/src/private/acvm/index.ts +0 -1
- package/src/private/{providers/acvm_native.ts → acvm_native.ts} +4 -4
- package/src/private/{providers/acvm_wasm.ts → acvm_wasm.ts} +4 -4
- package/src/private/{providers/acvm_wasm_with_blobs.ts → acvm_wasm_with_blobs.ts} +5 -5
- package/src/private/circuit_recording/circuit_recorder.ts +260 -0
- package/src/private/circuit_recording/file_circuit_recorder.ts +158 -0
- package/src/private/circuit_recording/memory_circuit_recorder.ts +11 -0
- package/src/private/{providers/circuit_recording/simulation_provider_recorder_wrapper.ts → circuit_recording/simulator_recorder_wrapper.ts} +26 -18
- package/src/private/{providers/simulation_provider.ts → circuit_simulator.ts} +3 -3
- package/src/private/{providers/factory.ts → factory.ts} +6 -6
- package/src/public/avm/fixtures/base_avm_simulation_tester.ts +5 -0
- package/src/public/public_tx_simulator/apps_tests/amm_test.ts +49 -44
- package/src/server.ts +6 -3
- package/src/testing.ts +1 -1
- package/dest/private/acvm/oracle/index.d.ts +0 -14
- package/dest/private/acvm/oracle/index.d.ts.map +0 -1
- package/dest/private/acvm/oracle/index.js +0 -2
- package/dest/private/acvm/oracle/oracle.d.ts +0 -52
- package/dest/private/acvm/oracle/oracle.d.ts.map +0 -1
- package/dest/private/acvm/oracle/oracle.js +0 -312
- package/dest/private/acvm/oracle/typed_oracle.d.ts +0 -83
- package/dest/private/acvm/oracle/typed_oracle.d.ts.map +0 -1
- package/dest/private/acvm/oracle/typed_oracle.js +0 -138
- package/dest/private/execution_data_provider.d.ts +0 -301
- package/dest/private/execution_data_provider.d.ts.map +0 -1
- package/dest/private/execution_data_provider.js +0 -14
- package/dest/private/execution_note_cache.d.ts +0 -93
- package/dest/private/execution_note_cache.d.ts.map +0 -1
- package/dest/private/execution_note_cache.js +0 -180
- package/dest/private/hashed_values_cache.d.ts +0 -28
- package/dest/private/hashed_values_cache.d.ts.map +0 -1
- package/dest/private/hashed_values_cache.js +0 -36
- package/dest/private/index.d.ts +0 -16
- package/dest/private/index.d.ts.map +0 -1
- package/dest/private/index.js +0 -15
- package/dest/private/message_load_oracle_inputs.d.ts +0 -19
- package/dest/private/message_load_oracle_inputs.d.ts.map +0 -1
- package/dest/private/message_load_oracle_inputs.js +0 -24
- package/dest/private/pick_notes.d.ts +0 -85
- package/dest/private/pick_notes.d.ts.map +0 -1
- package/dest/private/pick_notes.js +0 -51
- package/dest/private/private_execution.d.ts +0 -25
- package/dest/private/private_execution.d.ts.map +0 -1
- package/dest/private/private_execution.js +0 -95
- package/dest/private/private_execution_oracle.d.ts +0 -187
- package/dest/private/private_execution_oracle.d.ts.map +0 -1
- package/dest/private/private_execution_oracle.js +0 -318
- package/dest/private/providers/acvm_native.d.ts.map +0 -1
- package/dest/private/providers/acvm_wasm.d.ts.map +0 -1
- package/dest/private/providers/acvm_wasm_with_blobs.d.ts.map +0 -1
- package/dest/private/providers/circuit_recording/circuit_recorder.d.ts.map +0 -1
- package/dest/private/providers/circuit_recording/circuit_recorder.js +0 -246
- package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts.map +0 -1
- package/dest/private/providers/factory.d.ts +0 -12
- package/dest/private/providers/factory.d.ts.map +0 -1
- package/dest/private/providers/simulation_provider.d.ts.map +0 -1
- package/dest/private/simulator.d.ts +0 -36
- package/dest/private/simulator.d.ts.map +0 -1
- package/dest/private/simulator.js +0 -104
- package/dest/private/utility_execution_oracle.d.ts +0 -163
- package/dest/private/utility_execution_oracle.d.ts.map +0 -1
- package/dest/private/utility_execution_oracle.js +0 -257
- package/dest/test/utils.d.ts +0 -13
- package/dest/test/utils.d.ts.map +0 -1
- package/dest/test/utils.js +0 -22
- package/src/private/acvm/oracle/index.ts +0 -16
- package/src/private/acvm/oracle/oracle.ts +0 -523
- package/src/private/acvm/oracle/typed_oracle.ts +0 -273
- package/src/private/execution_data_provider.ts +0 -388
- package/src/private/execution_note_cache.ts +0 -217
- package/src/private/hashed_values_cache.ts +0 -47
- package/src/private/index.ts +0 -19
- package/src/private/message_load_oracle_inputs.ts +0 -23
- package/src/private/pick_notes.ts +0 -141
- package/src/private/private_execution.ts +0 -159
- package/src/private/private_execution_oracle.ts +0 -508
- package/src/private/providers/circuit_recording/circuit_recorder.ts +0 -283
- package/src/private/simulator.ts +0 -170
- package/src/private/utility_execution_oracle.ts +0 -384
- package/src/test/utils.ts +0 -36
- /package/dest/private/{providers/acvm_native.js → acvm_native.js} +0 -0
|
@@ -9,9 +9,9 @@ import type { NoirCompiledCircuitWithName } from '@aztec/stdlib/noir';
|
|
|
9
9
|
import * as proc from 'child_process';
|
|
10
10
|
import { promises as fs } from 'fs';
|
|
11
11
|
|
|
12
|
-
import type { ACIRCallback, ACIRExecutionResult } from '
|
|
13
|
-
import type { ACVMWitness } from '
|
|
14
|
-
import type {
|
|
12
|
+
import type { ACIRCallback, ACIRExecutionResult } from './acvm/acvm.js';
|
|
13
|
+
import type { ACVMWitness } from './acvm/acvm_types.js';
|
|
14
|
+
import type { CircuitSimulator } from './circuit_simulator.js';
|
|
15
15
|
|
|
16
16
|
const logger = createLogger('simulator:acvm-native');
|
|
17
17
|
|
|
@@ -137,7 +137,7 @@ export async function executeNativeCircuit(
|
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
-
export class NativeACVMSimulator implements
|
|
140
|
+
export class NativeACVMSimulator implements CircuitSimulator {
|
|
141
141
|
constructor(
|
|
142
142
|
private workingDirectory: string,
|
|
143
143
|
private pathToAcvm: string,
|
|
@@ -5,12 +5,12 @@ import initAbi from '@aztec/noir-noirc_abi';
|
|
|
5
5
|
import type { FunctionArtifactWithContractName } from '@aztec/stdlib/abi';
|
|
6
6
|
import type { NoirCompiledCircuitWithName } from '@aztec/stdlib/noir';
|
|
7
7
|
|
|
8
|
-
import { type ACIRCallback, type ACIRExecutionResult, acvm } from '
|
|
9
|
-
import type { ACVMWitness } from '
|
|
8
|
+
import { type ACIRCallback, type ACIRExecutionResult, acvm } from './acvm/acvm.js';
|
|
9
|
+
import type { ACVMWitness } from './acvm/acvm_types.js';
|
|
10
10
|
import type { ACVMSuccess } from './acvm_native.js';
|
|
11
|
-
import { type
|
|
11
|
+
import { type CircuitSimulator, enrichNoirError } from './circuit_simulator.js';
|
|
12
12
|
|
|
13
|
-
export class WASMSimulator implements
|
|
13
|
+
export class WASMSimulator implements CircuitSimulator {
|
|
14
14
|
constructor(protected log = createLogger('wasm-simulator')) {}
|
|
15
15
|
|
|
16
16
|
async init(): Promise<void> {
|
|
@@ -4,19 +4,19 @@ import type { WitnessMap } from '@aztec/noir-types';
|
|
|
4
4
|
import type { FunctionArtifactWithContractName } from '@aztec/stdlib/abi';
|
|
5
5
|
import type { NoirCompiledCircuitWithName } from '@aztec/stdlib/noir';
|
|
6
6
|
|
|
7
|
-
import type { ACIRCallback, ACIRExecutionResult } from '
|
|
8
|
-
import type { ACVMWitness } from '
|
|
7
|
+
import type { ACIRCallback, ACIRExecutionResult } from './acvm/acvm.js';
|
|
8
|
+
import type { ACVMWitness } from './acvm/acvm_types.js';
|
|
9
9
|
import type { ACVMSuccess } from './acvm_native.js';
|
|
10
|
-
import { type
|
|
10
|
+
import { type CircuitSimulator, enrichNoirError } from './circuit_simulator.js';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* A
|
|
13
|
+
* A circuit simulator that uses the WASM simulator with the ability to handle blobs via the foreign call handler.
|
|
14
14
|
* This class is temporary while brillig cannot handle the blob math, and it is kept separate
|
|
15
15
|
* because the zkg commitment library used in the blob code is not browser compatible.
|
|
16
16
|
*
|
|
17
17
|
* It is only used in the context of server-side code executing simulated protocol circuits.
|
|
18
18
|
*/
|
|
19
|
-
export class WASMSimulatorWithBlobs implements
|
|
19
|
+
export class WASMSimulatorWithBlobs implements CircuitSimulator {
|
|
20
20
|
async executeProtocolCircuit(
|
|
21
21
|
input: WitnessMap,
|
|
22
22
|
artifact: NoirCompiledCircuitWithName,
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { sha512 } from '@aztec/foundation/crypto';
|
|
2
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
3
|
+
import { Timer } from '@aztec/foundation/timer';
|
|
4
|
+
import type { ForeignCallHandler, ForeignCallInput, ForeignCallOutput } from '@aztec/noir-acvm_js';
|
|
5
|
+
|
|
6
|
+
import type { ACIRCallback } from '../acvm/acvm.js';
|
|
7
|
+
import type { ACVMWitness } from '../acvm/acvm_types.js';
|
|
8
|
+
|
|
9
|
+
export type OracleCall = {
|
|
10
|
+
name: string;
|
|
11
|
+
inputs: unknown[];
|
|
12
|
+
outputs: unknown;
|
|
13
|
+
time: number;
|
|
14
|
+
// Due to the recursive nature of the simulator, we might have
|
|
15
|
+
// oracle calls performed after a foreign call (which is itself an oracle call)
|
|
16
|
+
// We keep track of the stack depth in this variable to ensure the recorded oracle
|
|
17
|
+
// calls are correctly associated with the right circuit.
|
|
18
|
+
// This is only use as a debugging tool
|
|
19
|
+
stackDepth: number;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export class CircuitRecording {
|
|
23
|
+
circuitName: string;
|
|
24
|
+
functionName: string;
|
|
25
|
+
bytecodeSHA512Hash: string;
|
|
26
|
+
timestamp: number;
|
|
27
|
+
inputs: Record<string, string>;
|
|
28
|
+
oracleCalls: OracleCall[];
|
|
29
|
+
error?: string;
|
|
30
|
+
parent?: CircuitRecording;
|
|
31
|
+
|
|
32
|
+
constructor(circuitName: string, functionName: string, bytecodeSHA512Hash: string, inputs: Record<string, string>) {
|
|
33
|
+
this.circuitName = circuitName;
|
|
34
|
+
this.functionName = functionName;
|
|
35
|
+
this.bytecodeSHA512Hash = bytecodeSHA512Hash;
|
|
36
|
+
this.timestamp = Date.now();
|
|
37
|
+
this.inputs = inputs;
|
|
38
|
+
this.oracleCalls = [];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
setParent(recording?: CircuitRecording): void {
|
|
42
|
+
this.parent = recording;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Class responsible for recording circuit inputs necessary to replay the circuit. These inputs are the initial witness
|
|
48
|
+
* map and the oracle calls made during the circuit execution/witness generation.
|
|
49
|
+
*
|
|
50
|
+
* Example recording object:
|
|
51
|
+
* ```json
|
|
52
|
+
* {
|
|
53
|
+
* "circuitName": "AMM",
|
|
54
|
+
* "functionName": "add_liquidity",
|
|
55
|
+
* "bytecodeSHA512Hash": "b46c640ed38f20eac5f61a5e41d8dd1e",
|
|
56
|
+
* "timestamp": 1740691464360,
|
|
57
|
+
* "inputs": {
|
|
58
|
+
* "0": "0x1e89de1f0ad5204263733b7ddf65bec45b8f44714a4da85a46474dad677679ef",
|
|
59
|
+
* "1": "0x00f4d59c0ff773427bb0fed5b422557ca4dc5655abe53d31fa9408cb3c5a672f",
|
|
60
|
+
* "5": "0x000000000000000000000000000000000000000000000000000000000000000f"
|
|
61
|
+
* },
|
|
62
|
+
* "oracleCalls": [
|
|
63
|
+
* {
|
|
64
|
+
* "name": "loadCapsule",
|
|
65
|
+
* "inputs": [
|
|
66
|
+
* [
|
|
67
|
+
* "0x102422483bad6abd385948435667e144ac4c272576e325e7563608876cd446fd"
|
|
68
|
+
* ],
|
|
69
|
+
* [
|
|
70
|
+
* "0x000000000000000000000000000000000000000000000000000000000000004d"
|
|
71
|
+
* ],
|
|
72
|
+
* [
|
|
73
|
+
* "0x0000000000000000000000000000000000000000000000000000000000000001"
|
|
74
|
+
* ]
|
|
75
|
+
* ],
|
|
76
|
+
* "outputs": [
|
|
77
|
+
* "0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
78
|
+
* [
|
|
79
|
+
* "0x0000000000000000000000000000000000000000000000000000000000000000"
|
|
80
|
+
* ]
|
|
81
|
+
* ]
|
|
82
|
+
* },
|
|
83
|
+
* {
|
|
84
|
+
* "name": "fetchTaggedLogs",
|
|
85
|
+
* "inputs": []
|
|
86
|
+
* }
|
|
87
|
+
* ]
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export class CircuitRecorder {
|
|
92
|
+
protected readonly logger = createLogger('simulator:acvm:recording');
|
|
93
|
+
|
|
94
|
+
protected recording?: CircuitRecording;
|
|
95
|
+
|
|
96
|
+
private stackDepth: number = 0;
|
|
97
|
+
private newCircuit: boolean = true;
|
|
98
|
+
|
|
99
|
+
protected constructor() {}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Initializes a new circuit recording session.
|
|
103
|
+
* @param recordDir - Directory to store the recording
|
|
104
|
+
* @param input - Circuit input witness
|
|
105
|
+
* @param circuitBytecode - Compiled circuit bytecode
|
|
106
|
+
* @param circuitName - Name of the circuit
|
|
107
|
+
* @param functionName - Name of the circuit function (defaults to 'main'). This is meaningful only for
|
|
108
|
+
* contracts as protocol circuits artifacts always contain a single entrypoint function called 'main'.
|
|
109
|
+
*/
|
|
110
|
+
start(input: ACVMWitness, circuitBytecode: Buffer, circuitName: string, functionName: string): Promise<void> {
|
|
111
|
+
const parentRef = this.recording;
|
|
112
|
+
if (this.newCircuit) {
|
|
113
|
+
this.recording = new CircuitRecording(
|
|
114
|
+
circuitName,
|
|
115
|
+
functionName,
|
|
116
|
+
sha512(circuitBytecode).toString('hex'),
|
|
117
|
+
Object.fromEntries(input),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
this.recording!.setParent(parentRef);
|
|
121
|
+
|
|
122
|
+
return Promise.resolve();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Wraps a callback to record all oracle/foreign calls.
|
|
127
|
+
* @param callback - The original callback to wrap, either a user circuit callback or protocol circuit callback.
|
|
128
|
+
* @returns A wrapped callback that records all oracle interactions.
|
|
129
|
+
*/
|
|
130
|
+
wrapCallback(callback: ACIRCallback | ForeignCallHandler | undefined): ACIRCallback | ForeignCallHandler | undefined {
|
|
131
|
+
if (!callback) {
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
if (this.#isACIRCallback(callback)) {
|
|
135
|
+
return this.#wrapUserCircuitCallback(callback);
|
|
136
|
+
}
|
|
137
|
+
return this.#wrapProtocolCircuitCallback(callback);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Type guard to check if a callback is an ACIRCallback.
|
|
142
|
+
*/
|
|
143
|
+
#isACIRCallback(callback: ACIRCallback | ForeignCallHandler): callback is ACIRCallback {
|
|
144
|
+
return typeof callback === 'object' && callback !== null && !('call' in callback);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Wraps a user circuit callback to record all oracle calls.
|
|
149
|
+
* @param callback - The original circuit callback.
|
|
150
|
+
* @returns A wrapped callback that records all oracle interactions which is to be provided to the ACVM.
|
|
151
|
+
*/
|
|
152
|
+
#wrapUserCircuitCallback(callback: ACIRCallback): ACIRCallback {
|
|
153
|
+
const recordingCallback: ACIRCallback = {} as ACIRCallback;
|
|
154
|
+
const oracleMethods = Object.keys(callback);
|
|
155
|
+
|
|
156
|
+
for (const name of oracleMethods) {
|
|
157
|
+
const fn = callback[name as keyof ACIRCallback];
|
|
158
|
+
if (!fn || typeof fn !== 'function') {
|
|
159
|
+
throw new Error(`Oracle method ${name} not found when setting up recording callback`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const isExternalCall = (name as keyof ACIRCallback) === 'callPrivateFunction';
|
|
163
|
+
|
|
164
|
+
recordingCallback[name as keyof ACIRCallback] = (...args: ForeignCallInput[]): ReturnType<typeof fn> => {
|
|
165
|
+
const timer = new Timer();
|
|
166
|
+
// If we're entering another circuit via `callPrivateFunction`, we increase the stack depth and set the
|
|
167
|
+
// newCircuit variable to ensure we are creating a new recording object.
|
|
168
|
+
if (isExternalCall) {
|
|
169
|
+
this.stackDepth++;
|
|
170
|
+
this.newCircuit = true;
|
|
171
|
+
}
|
|
172
|
+
const result = fn.call(callback, ...args);
|
|
173
|
+
if (result instanceof Promise) {
|
|
174
|
+
return result.then(async r => {
|
|
175
|
+
// Once we leave the nested circuit, we decrease the stack depth and set newCircuit to false
|
|
176
|
+
// since we are going back to the "parent" circuit which can never be new
|
|
177
|
+
if (isExternalCall) {
|
|
178
|
+
this.stackDepth--;
|
|
179
|
+
this.newCircuit = false;
|
|
180
|
+
this.recording = this.recording!.parent;
|
|
181
|
+
}
|
|
182
|
+
await this.recordCall(name, args, r, timer.ms(), this.stackDepth);
|
|
183
|
+
return r;
|
|
184
|
+
}) as ReturnType<typeof fn>;
|
|
185
|
+
}
|
|
186
|
+
// Once we leave the nested circuit, we decrease the stack depth and set newCircuit to false
|
|
187
|
+
// since we are going back to the "parent" circuit which can never be new
|
|
188
|
+
if (isExternalCall) {
|
|
189
|
+
this.stackDepth--;
|
|
190
|
+
this.newCircuit = false;
|
|
191
|
+
this.recording = this.recording!.parent;
|
|
192
|
+
}
|
|
193
|
+
void this.recordCall(name, args, result, timer.ms(), this.stackDepth);
|
|
194
|
+
return result;
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return recordingCallback;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Wraps a protocol circuit callback to record all oracle calls.
|
|
203
|
+
* @param callback - The original oracle circuit callback.
|
|
204
|
+
* @returns A wrapped handler that records all oracle interactions which is to be provided to the ACVM.
|
|
205
|
+
*/
|
|
206
|
+
#wrapProtocolCircuitCallback(callback: ForeignCallHandler): ForeignCallHandler {
|
|
207
|
+
return async (name: string, inputs: ForeignCallInput[]): Promise<ForeignCallOutput[]> => {
|
|
208
|
+
const timer = new Timer();
|
|
209
|
+
const result = await callback(name, inputs);
|
|
210
|
+
await this.recordCall(name, inputs, result, timer.ms(), 0);
|
|
211
|
+
return result;
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Records a single oracle/foreign call with its inputs and outputs.
|
|
217
|
+
* @param name - Name of the call
|
|
218
|
+
* @param inputs - Input arguments
|
|
219
|
+
* @param outputs - Output results
|
|
220
|
+
*/
|
|
221
|
+
recordCall(name: string, inputs: unknown[], outputs: unknown, time: number, stackDepth: number): Promise<OracleCall> {
|
|
222
|
+
const entry = {
|
|
223
|
+
name,
|
|
224
|
+
inputs,
|
|
225
|
+
outputs,
|
|
226
|
+
time,
|
|
227
|
+
stackDepth,
|
|
228
|
+
};
|
|
229
|
+
this.recording!.oracleCalls.push(entry);
|
|
230
|
+
return Promise.resolve(entry);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Finalizes the recording by resetting the state and returning the recording object.
|
|
235
|
+
*/
|
|
236
|
+
finish(): Promise<CircuitRecording> {
|
|
237
|
+
const result = this.recording;
|
|
238
|
+
// If this is the top-level circuit recording, we reset the state for the next simulator call
|
|
239
|
+
if (!result!.parent) {
|
|
240
|
+
this.newCircuit = true;
|
|
241
|
+
this.recording = undefined;
|
|
242
|
+
}
|
|
243
|
+
return Promise.resolve(result!);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Finalizes the recording by resetting the state and returning the recording object with an attached error.
|
|
248
|
+
* @param error - The error that occurred during circuit execution
|
|
249
|
+
*/
|
|
250
|
+
finishWithError(error: unknown): Promise<CircuitRecording> {
|
|
251
|
+
const result = this.recording;
|
|
252
|
+
// If this is the top-level circuit recording, we reset the state for the next simulator call
|
|
253
|
+
if (!result!.parent) {
|
|
254
|
+
this.newCircuit = true;
|
|
255
|
+
this.recording = undefined;
|
|
256
|
+
}
|
|
257
|
+
result!.error = JSON.stringify(error);
|
|
258
|
+
return Promise.resolve(result!);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
import type { ACVMWitness } from '../acvm/acvm_types.js';
|
|
5
|
+
import { CircuitRecorder, type CircuitRecording } from './circuit_recorder.js';
|
|
6
|
+
|
|
7
|
+
export class FileCircuitRecorder extends CircuitRecorder {
|
|
8
|
+
declare recording?: CircuitRecording & { filePath: string; isFirstCall: boolean };
|
|
9
|
+
|
|
10
|
+
constructor(private readonly recordDir: string) {
|
|
11
|
+
super();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
override async start(
|
|
15
|
+
input: ACVMWitness,
|
|
16
|
+
circuitBytecode: Buffer,
|
|
17
|
+
circuitName: string,
|
|
18
|
+
functionName: string = 'main',
|
|
19
|
+
) {
|
|
20
|
+
await super.start(input, circuitBytecode, circuitName, functionName);
|
|
21
|
+
|
|
22
|
+
const recordingStringWithoutClosingBracket = JSON.stringify(
|
|
23
|
+
{ ...this.recording, isFirstCall: undefined, parent: undefined, oracleCalls: undefined, filePath: undefined },
|
|
24
|
+
null,
|
|
25
|
+
2,
|
|
26
|
+
).slice(0, -2);
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
// Check if the recording directory exists and is a directory
|
|
30
|
+
const stats = await fs.stat(this.recordDir);
|
|
31
|
+
if (!stats.isDirectory()) {
|
|
32
|
+
throw new Error(`Recording path ${this.recordDir} exists but is not a directory`);
|
|
33
|
+
}
|
|
34
|
+
} catch (err) {
|
|
35
|
+
if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
36
|
+
// The directory does not exist so we create it
|
|
37
|
+
await fs.mkdir(this.recordDir, { recursive: true });
|
|
38
|
+
} else {
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this.recording!.isFirstCall = true;
|
|
44
|
+
this.recording!.filePath = await FileCircuitRecorder.#computeFilePathAndStoreInitialRecording(
|
|
45
|
+
this.recordDir,
|
|
46
|
+
this.recording!.circuitName,
|
|
47
|
+
this.recording!.functionName,
|
|
48
|
+
recordingStringWithoutClosingBracket,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Computes a unique file path for the recording by trying different counter values.
|
|
54
|
+
* This is needed because multiple recordings of the same circuit could be happening simultaneously or an older
|
|
55
|
+
* recording might be present.
|
|
56
|
+
* @param recordDir - Directory to store the recording
|
|
57
|
+
* @param circuitName - Name of the circuit
|
|
58
|
+
* @param functionName - Name of the circuit function
|
|
59
|
+
* @param recordingContent - Initial recording content
|
|
60
|
+
* @returns A unique file path for the recording
|
|
61
|
+
*/
|
|
62
|
+
static async #computeFilePathAndStoreInitialRecording(
|
|
63
|
+
recordDir: string,
|
|
64
|
+
circuitName: string,
|
|
65
|
+
functionName: string,
|
|
66
|
+
recordingContent: string,
|
|
67
|
+
): Promise<string> {
|
|
68
|
+
let counter = 0;
|
|
69
|
+
while (true) {
|
|
70
|
+
try {
|
|
71
|
+
const filePath = getFilePath(recordDir, circuitName, functionName, counter);
|
|
72
|
+
// Write the initial recording content to the file
|
|
73
|
+
await fs.writeFile(filePath, recordingContent + ',\n "oracleCalls": [\n', {
|
|
74
|
+
flag: 'wx', // wx flag fails if file exists
|
|
75
|
+
});
|
|
76
|
+
return filePath;
|
|
77
|
+
} catch (err) {
|
|
78
|
+
if ((err as NodeJS.ErrnoException).code === 'EEXIST') {
|
|
79
|
+
counter++;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Records a single oracle/foreign call with its inputs and outputs.
|
|
89
|
+
* @param name - Name of the call
|
|
90
|
+
* @param inputs - Input arguments
|
|
91
|
+
* @param outputs - Output results
|
|
92
|
+
*/
|
|
93
|
+
override async recordCall(name: string, inputs: unknown[], outputs: unknown, time: number, stackDepth: number) {
|
|
94
|
+
const entry = await super.recordCall(name, inputs, outputs, time, stackDepth);
|
|
95
|
+
try {
|
|
96
|
+
const prefix = this.recording!.isFirstCall ? ' ' : ' ,';
|
|
97
|
+
this.recording!.isFirstCall = false;
|
|
98
|
+
await fs.appendFile(this.recording!.filePath, prefix + JSON.stringify(entry) + '\n');
|
|
99
|
+
} catch (err) {
|
|
100
|
+
this.logger.error('Failed to log circuit call', { error: err });
|
|
101
|
+
}
|
|
102
|
+
return entry;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Finalizes the recording file by adding closing brackets. Without calling this method, the recording file is
|
|
107
|
+
* incomplete and it fails to parse.
|
|
108
|
+
*/
|
|
109
|
+
override async finish(): Promise<CircuitRecording> {
|
|
110
|
+
// Finish sets the recording to undefined if we are at the topmost circuit,
|
|
111
|
+
// so we save the current file path before that
|
|
112
|
+
const filePath = this.recording!.filePath;
|
|
113
|
+
const result = await super.finish();
|
|
114
|
+
try {
|
|
115
|
+
await fs.appendFile(filePath, ' ]\n}\n');
|
|
116
|
+
} catch (err) {
|
|
117
|
+
this.logger.error('Failed to finalize recording file', { error: err });
|
|
118
|
+
}
|
|
119
|
+
return result!;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Finalizes the recording file by adding the error and closing brackets. Without calling this method or `finish`,
|
|
124
|
+
* the recording file is incomplete and it fails to parse.
|
|
125
|
+
* @param error - The error that occurred during circuit execution
|
|
126
|
+
*/
|
|
127
|
+
override async finishWithError(error: unknown): Promise<CircuitRecording> {
|
|
128
|
+
// Finish sets the recording to undefined if we are at the topmost circuit,
|
|
129
|
+
// so we save the current file path before that
|
|
130
|
+
const filePath = this.recording!.filePath;
|
|
131
|
+
const result = await super.finishWithError(error);
|
|
132
|
+
try {
|
|
133
|
+
await fs.appendFile(filePath, ' ],\n');
|
|
134
|
+
await fs.appendFile(filePath, ` "error": ${JSON.stringify(error)}\n`);
|
|
135
|
+
await fs.appendFile(filePath, '}\n');
|
|
136
|
+
} catch (err) {
|
|
137
|
+
this.logger.error('Failed to finalize recording file with error', { error: err });
|
|
138
|
+
}
|
|
139
|
+
return result!;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Generates a file path for storing circuit recordings. The format of the filename is:
|
|
145
|
+
* `circuit_name_circuit_function_name_YYYY-MM-DD_N.json` where N is a counter to ensure unique filenames.
|
|
146
|
+
* @param recordDir - Base directory for recordings
|
|
147
|
+
* @param circuitName - Name of the circuit
|
|
148
|
+
* @param functionName - Name of the circuit function
|
|
149
|
+
* @param counter - Counter to ensure unique filenames. This is expected to be incremented in a loop until there is no
|
|
150
|
+
* existing file with the same name.
|
|
151
|
+
* @returns A file path for the recording.
|
|
152
|
+
*/
|
|
153
|
+
function getFilePath(recordDir: string, circuitName: string, functionName: string, counter: number): string {
|
|
154
|
+
const date = new Date();
|
|
155
|
+
const formattedDate = date.toISOString().split('T')[0];
|
|
156
|
+
const filename = `${circuitName}_${functionName}_${formattedDate}_${counter}.json`;
|
|
157
|
+
return path.join(recordDir, filename);
|
|
158
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CircuitRecorder } from './circuit_recorder.js';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* In memory circuit recorder uses the default implementation. This is kept
|
|
5
|
+
* while we decide the fate of the FileCircuitRecorder
|
|
6
|
+
*/
|
|
7
|
+
export class MemoryCircuitRecorder extends CircuitRecorder {
|
|
8
|
+
constructor() {
|
|
9
|
+
super();
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -2,18 +2,21 @@ import type { ForeignCallHandler } from '@aztec/noir-protocol-circuits-types/typ
|
|
|
2
2
|
import type { FunctionArtifactWithContractName } from '@aztec/stdlib/abi';
|
|
3
3
|
import type { NoirCompiledCircuitWithName } from '@aztec/stdlib/noir';
|
|
4
4
|
|
|
5
|
-
import type { ACIRCallback, ACIRExecutionResult } from '
|
|
6
|
-
import type { ACVMWitness } from '
|
|
5
|
+
import type { ACIRCallback, ACIRCallbackStats, ACIRExecutionResult } from '../acvm/acvm.js';
|
|
6
|
+
import type { ACVMWitness } from '../acvm/acvm_types.js';
|
|
7
7
|
import type { ACVMSuccess } from '../acvm_native.js';
|
|
8
|
-
import type {
|
|
9
|
-
import { CircuitRecorder } from './circuit_recorder.js';
|
|
8
|
+
import type { CircuitSimulator } from '../circuit_simulator.js';
|
|
9
|
+
import type { CircuitRecorder } from './circuit_recorder.js';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* Takes a
|
|
12
|
+
* Takes a circuit simulator and wraps it in a circuit recorder. See CircuitRecorder for more details on how circuit
|
|
13
13
|
* recording works.
|
|
14
14
|
*/
|
|
15
|
-
export class
|
|
16
|
-
constructor(
|
|
15
|
+
export class SimulatorRecorderWrapper implements CircuitSimulator {
|
|
16
|
+
constructor(
|
|
17
|
+
private simulator: CircuitSimulator,
|
|
18
|
+
private recorder: CircuitRecorder,
|
|
19
|
+
) {}
|
|
17
20
|
|
|
18
21
|
executeProtocolCircuit(
|
|
19
22
|
input: ACVMWitness,
|
|
@@ -47,7 +50,7 @@ export class SimulationProviderRecorderWrapper implements SimulationProvider {
|
|
|
47
50
|
);
|
|
48
51
|
}
|
|
49
52
|
|
|
50
|
-
async #simulate<C extends ACIRCallback | ForeignCallHandler | undefined, T>(
|
|
53
|
+
async #simulate<C extends ACIRCallback | ForeignCallHandler | undefined, T extends ACIRExecutionResult | ACVMSuccess>(
|
|
51
54
|
simulateFn: (wrappedCallback: C) => Promise<T>,
|
|
52
55
|
input: ACVMWitness,
|
|
53
56
|
bytecode: Buffer,
|
|
@@ -55,28 +58,33 @@ export class SimulationProviderRecorderWrapper implements SimulationProvider {
|
|
|
55
58
|
functionName: string,
|
|
56
59
|
callback: C,
|
|
57
60
|
): Promise<T> {
|
|
58
|
-
const recordDir = process.env.CIRCUIT_RECORD_DIR;
|
|
59
|
-
if (!recordDir) {
|
|
60
|
-
// Recording is not enabled so we just execute the circuit
|
|
61
|
-
return simulateFn(callback);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
61
|
// Start recording circuit execution
|
|
65
|
-
|
|
62
|
+
await this.recorder.start(input, bytecode, contractName, functionName);
|
|
66
63
|
|
|
67
64
|
// If callback was provided, we wrap it in a circuit recorder callback wrapper
|
|
68
|
-
const wrappedCallback = recorder.wrapCallback(callback);
|
|
65
|
+
const wrappedCallback = this.recorder.wrapCallback(callback);
|
|
69
66
|
let result: T;
|
|
70
67
|
try {
|
|
71
68
|
result = await simulateFn(wrappedCallback as C);
|
|
72
69
|
} catch (error) {
|
|
73
70
|
// If an error occurs, we finalize the recording file with the error
|
|
74
|
-
await recorder.finishWithError(error);
|
|
71
|
+
await this.recorder.finishWithError(error);
|
|
75
72
|
throw error;
|
|
76
73
|
}
|
|
77
74
|
|
|
78
75
|
// Witness generation is complete so we finish the circuit recorder
|
|
79
|
-
await recorder.finish();
|
|
76
|
+
const recording = await this.recorder.finish();
|
|
77
|
+
|
|
78
|
+
(result as ACIRExecutionResult).oracles = recording.oracleCalls?.reduce(
|
|
79
|
+
(acc, { time, name }) => {
|
|
80
|
+
if (!acc[name]) {
|
|
81
|
+
acc[name] = { times: [] };
|
|
82
|
+
}
|
|
83
|
+
acc[name].times.push(time);
|
|
84
|
+
return acc;
|
|
85
|
+
},
|
|
86
|
+
{} as Record<string, ACIRCallbackStats>,
|
|
87
|
+
);
|
|
80
88
|
|
|
81
89
|
return result;
|
|
82
90
|
}
|
|
@@ -4,14 +4,14 @@ import { parseDebugSymbols } from '@aztec/stdlib/abi';
|
|
|
4
4
|
import type { FunctionArtifactWithContractName } from '@aztec/stdlib/abi';
|
|
5
5
|
import type { NoirCompiledCircuit, NoirCompiledCircuitWithName } from '@aztec/stdlib/noir';
|
|
6
6
|
|
|
7
|
-
import { type ACIRCallback, type ACIRExecutionResult, extractCallStack } from '
|
|
8
|
-
import type { ACVMWitness } from '
|
|
7
|
+
import { type ACIRCallback, type ACIRExecutionResult, extractCallStack } from './acvm/acvm.js';
|
|
8
|
+
import type { ACVMWitness } from './acvm/acvm_types.js';
|
|
9
9
|
import type { ACVMSuccess } from './acvm_native.js';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Low level simulation interface
|
|
13
13
|
*/
|
|
14
|
-
export interface
|
|
14
|
+
export interface CircuitSimulator {
|
|
15
15
|
/**
|
|
16
16
|
* Execute a protocol circuit/generate a witness
|
|
17
17
|
* @param input - The initial witness map defining all of the inputs to `circuit`.
|
|
@@ -4,14 +4,14 @@ import { promises as fs } from 'fs';
|
|
|
4
4
|
|
|
5
5
|
import { NativeACVMSimulator } from './acvm_native.js';
|
|
6
6
|
import { WASMSimulator } from './acvm_wasm.js';
|
|
7
|
-
import type {
|
|
7
|
+
import type { CircuitSimulator } from './circuit_simulator.js';
|
|
8
8
|
|
|
9
|
-
export type
|
|
9
|
+
export type SimulatorConfig = {
|
|
10
10
|
acvmBinaryPath?: string;
|
|
11
11
|
acvmWorkingDirectory?: string;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
export function
|
|
14
|
+
export function getSimulatorConfigFromEnv() {
|
|
15
15
|
const { ACVM_BINARY_PATH, ACVM_WORKING_DIRECTORY } = process.env;
|
|
16
16
|
return {
|
|
17
17
|
acvmWorkingDirectory: ACVM_WORKING_DIRECTORY ? ACVM_WORKING_DIRECTORY : undefined,
|
|
@@ -19,10 +19,10 @@ export function getSimulationProviderConfigFromEnv() {
|
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export async function
|
|
23
|
-
config:
|
|
22
|
+
export async function createSimulator(
|
|
23
|
+
config: SimulatorConfig,
|
|
24
24
|
logger: Logger = createLogger('simulator'),
|
|
25
|
-
): Promise<
|
|
25
|
+
): Promise<CircuitSimulator> {
|
|
26
26
|
if (config.acvmBinaryPath && config.acvmWorkingDirectory) {
|
|
27
27
|
try {
|
|
28
28
|
await fs.access(config.acvmBinaryPath, fs.constants.R_OK);
|
|
@@ -100,4 +100,9 @@ export abstract class BaseAvmSimulationTester {
|
|
|
100
100
|
);
|
|
101
101
|
await this.merkleTrees.sequentialInsert(MerkleTreeId.NULLIFIER_TREE, [contractAddressNullifier.toBuffer()]);
|
|
102
102
|
}
|
|
103
|
+
|
|
104
|
+
async insertNullifier(contractThatEmitted: AztecAddress, nullifier: Fr) {
|
|
105
|
+
const siloedNullifier = await siloNullifier(contractThatEmitted, nullifier);
|
|
106
|
+
await this.merkleTrees.sequentialInsert(MerkleTreeId.NULLIFIER_TREE, [siloedNullifier.toBuffer()]);
|
|
107
|
+
}
|
|
103
108
|
}
|