@aztec/simulator 0.81.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 +6 -12
- package/dest/common/db_interfaces.d.ts.map +1 -1
- package/dest/common/db_interfaces.js +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 +0 -18
- package/dest/private/acvm/deserialize.d.ts.map +1 -1
- package/dest/private/acvm/deserialize.js +3 -24
- package/dest/private/acvm/oracle/oracle.d.ts +34 -34
- package/dest/private/acvm/oracle/oracle.d.ts.map +1 -1
- package/dest/private/acvm/oracle/oracle.js +119 -79
- package/dest/private/acvm/oracle/typed_oracle.d.ts +1 -1
- package/dest/private/acvm/oracle/typed_oracle.d.ts.map +1 -1
- package/dest/private/acvm/oracle/typed_oracle.js +2 -2
- package/dest/private/execution_data_provider.d.ts +3 -9
- 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/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 +1 -1
- package/dest/private/unconstrained_execution_oracle.d.ts.map +1 -1
- package/dest/private/unconstrained_execution_oracle.js +3 -3
- package/dest/public/avm/avm_simulator.d.ts.map +1 -1
- package/dest/public/avm/avm_simulator.js +0 -2
- package/dest/public/avm/journal/journal.js +1 -1
- package/dest/public/avm/test_utils.js +1 -1
- package/dest/public/public_db_sources.d.ts +1 -1
- package/dest/public/public_db_sources.d.ts.map +1 -1
- package/dest/public/public_db_sources.js +3 -3
- 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 +6 -13
- package/src/common/message_load_oracle_inputs.ts +8 -0
- package/src/private/acvm/acvm.ts +8 -24
- package/src/private/acvm/deserialize.ts +3 -30
- package/src/private/acvm/oracle/oracle.ts +144 -136
- package/src/private/acvm/oracle/typed_oracle.ts +2 -2
- package/src/private/execution_data_provider.ts +6 -10
- package/src/private/private_execution.ts +11 -7
- 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 +3 -6
- package/src/public/avm/avm_simulator.ts +0 -2
- package/src/public/avm/journal/journal.ts +1 -1
- package/src/public/avm/test_utils.ts +1 -1
- package/src/public/public_db_sources.ts +3 -3
- 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,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
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { ForeignCallHandler } from '@aztec/noir-protocol-circuits-types/types';
|
|
2
|
+
import type { FunctionArtifactWithContractName } from '@aztec/stdlib/abi';
|
|
3
|
+
import type { NoirCompiledCircuitWithName } from '@aztec/stdlib/noir';
|
|
4
|
+
|
|
5
|
+
import type { ACIRCallback, ACIRExecutionResult } from '../../acvm/acvm.js';
|
|
6
|
+
import type { ACVMWitness } from '../../acvm/acvm_types.js';
|
|
7
|
+
import type { SimulationProvider } from '../simulation_provider.js';
|
|
8
|
+
import { CircuitRecorder } from './circuit_recorder.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Takes a simulation provider and wraps it in a circuit recorder. See CircuitRecorder for more details on how circuit
|
|
12
|
+
* recording works.
|
|
13
|
+
*/
|
|
14
|
+
export class SimulationProviderRecorderWrapper implements SimulationProvider {
|
|
15
|
+
constructor(private simulator: SimulationProvider) {}
|
|
16
|
+
|
|
17
|
+
executeProtocolCircuit(
|
|
18
|
+
input: ACVMWitness,
|
|
19
|
+
artifact: NoirCompiledCircuitWithName,
|
|
20
|
+
callback: ForeignCallHandler | undefined,
|
|
21
|
+
): Promise<ACVMWitness> {
|
|
22
|
+
const bytecode = Buffer.from(artifact.bytecode, 'base64');
|
|
23
|
+
|
|
24
|
+
return this.#simulate<ForeignCallHandler | undefined, ACVMWitness>(
|
|
25
|
+
wrappedCallback => this.simulator.executeProtocolCircuit(input, artifact, wrappedCallback),
|
|
26
|
+
input,
|
|
27
|
+
bytecode,
|
|
28
|
+
artifact.name,
|
|
29
|
+
'main',
|
|
30
|
+
callback,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
executeUserCircuit(
|
|
35
|
+
input: ACVMWitness,
|
|
36
|
+
artifact: FunctionArtifactWithContractName,
|
|
37
|
+
callback: ACIRCallback,
|
|
38
|
+
): Promise<ACIRExecutionResult> {
|
|
39
|
+
return this.#simulate<ACIRCallback, ACIRExecutionResult>(
|
|
40
|
+
wrappedCallback => this.simulator.executeUserCircuit(input, artifact, wrappedCallback),
|
|
41
|
+
input,
|
|
42
|
+
artifact.bytecode,
|
|
43
|
+
artifact.contractName,
|
|
44
|
+
artifact.name,
|
|
45
|
+
callback,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async #simulate<C extends ACIRCallback | ForeignCallHandler | undefined, T>(
|
|
50
|
+
simulateFn: (wrappedCallback: C) => Promise<T>,
|
|
51
|
+
input: ACVMWitness,
|
|
52
|
+
bytecode: Buffer,
|
|
53
|
+
contractName: string,
|
|
54
|
+
functionName: string,
|
|
55
|
+
callback: C,
|
|
56
|
+
): Promise<T> {
|
|
57
|
+
const recordDir = process.env.CIRCUIT_RECORD_DIR;
|
|
58
|
+
if (!recordDir) {
|
|
59
|
+
// Recording is not enabled so we just execute the circuit
|
|
60
|
+
return simulateFn(callback);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Start recording circuit execution
|
|
64
|
+
const recorder = await CircuitRecorder.start(recordDir, input, bytecode, contractName, functionName);
|
|
65
|
+
|
|
66
|
+
// If callback was provided, we wrap it in a circuit recorder callback wrapper
|
|
67
|
+
const wrappedCallback = recorder.wrapCallback(callback);
|
|
68
|
+
let result: T;
|
|
69
|
+
try {
|
|
70
|
+
result = await simulateFn(wrappedCallback as C);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
// If an error occurs, we finalize the recording file with the error
|
|
73
|
+
await recorder.finishWithError(error);
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Witness generation is complete so we finish the circuit recorder
|
|
78
|
+
await recorder.finish();
|
|
79
|
+
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { ExecutionError } from '@aztec/noir-acvm_js';
|
|
1
|
+
import type { ExecutionError, ForeignCallHandler } from '@aztec/noir-acvm_js';
|
|
2
2
|
import { abiDecodeError } from '@aztec/noir-noirc_abi';
|
|
3
|
-
import type { WitnessMap } from '@aztec/noir-types';
|
|
4
3
|
import { parseDebugSymbols } from '@aztec/stdlib/abi';
|
|
5
|
-
import type {
|
|
4
|
+
import type { FunctionArtifactWithContractName } from '@aztec/stdlib/abi';
|
|
5
|
+
import type { NoirCompiledCircuit, NoirCompiledCircuitWithName } from '@aztec/stdlib/noir';
|
|
6
6
|
|
|
7
7
|
import { type ACIRCallback, type ACIRExecutionResult, extractCallStack } from '../acvm/acvm.js';
|
|
8
8
|
import type { ACVMWitness } from '../acvm/acvm_types.js';
|
|
@@ -11,8 +11,33 @@ import type { ACVMWitness } from '../acvm/acvm_types.js';
|
|
|
11
11
|
* Low level simulation interface
|
|
12
12
|
*/
|
|
13
13
|
export interface SimulationProvider {
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Execute a protocol circuit/generate a witness
|
|
16
|
+
* @param input - The initial witness map defining all of the inputs to `circuit`.
|
|
17
|
+
* @param artifact - ACIR circuit bytecode and its metadata.
|
|
18
|
+
* @param callback - A callback to process any foreign calls from the circuit. Can be undefined as for native
|
|
19
|
+
* ACVM simulator we don't process foreign calls.
|
|
20
|
+
* @returns The solved witness calculated by executing the circuit on the provided inputs.
|
|
21
|
+
*/
|
|
22
|
+
executeProtocolCircuit(
|
|
23
|
+
input: ACVMWitness,
|
|
24
|
+
artifact: NoirCompiledCircuitWithName,
|
|
25
|
+
callback: ForeignCallHandler | undefined,
|
|
26
|
+
): Promise<ACVMWitness>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Execute a user circuit (smart contract function)/generate a witness
|
|
30
|
+
* @param input - The initial witness map defining all of the inputs to `circuit`.
|
|
31
|
+
* @param artifact - Contract function ACIR circuit bytecode and its metadata.
|
|
32
|
+
* @param callback - A callback to process any foreign calls from the circuit.
|
|
33
|
+
* @returns The solved witness calculated by executing the circuit on the provided inputs, as well as the return
|
|
34
|
+
* witness indices as specified by the circuit.
|
|
35
|
+
*/
|
|
36
|
+
executeUserCircuit(
|
|
37
|
+
input: ACVMWitness,
|
|
38
|
+
artifact: FunctionArtifactWithContractName,
|
|
39
|
+
callback: ACIRCallback,
|
|
40
|
+
): Promise<ACIRExecutionResult>;
|
|
16
41
|
}
|
|
17
42
|
|
|
18
43
|
export type DecodedError = ExecutionError & { decodedAssertionPayload?: any; noirCallStack?: string[] };
|
package/src/private/simulator.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Fr } from '@aztec/foundation/fields';
|
|
2
2
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
3
|
-
import type { FunctionCall } from '@aztec/stdlib/abi';
|
|
3
|
+
import type { AbiDecoded, FunctionCall } from '@aztec/stdlib/abi';
|
|
4
4
|
import { FunctionSelector, FunctionType } from '@aztec/stdlib/abi';
|
|
5
|
+
import type { AuthWitness } from '@aztec/stdlib/auth-witness';
|
|
5
6
|
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
6
7
|
import { CallContext, PrivateExecutionResult, TxExecutionRequest } from '@aztec/stdlib/tx';
|
|
7
8
|
|
|
@@ -113,8 +114,9 @@ export class AcirSimulator {
|
|
|
113
114
|
request: FunctionCall,
|
|
114
115
|
contractAddress: AztecAddress,
|
|
115
116
|
selector: FunctionSelector,
|
|
117
|
+
authwits: AuthWitness[],
|
|
116
118
|
scopes?: AztecAddress[],
|
|
117
|
-
) {
|
|
119
|
+
): Promise<AbiDecoded> {
|
|
118
120
|
await verifyCurrentClassId(contractAddress, this.executionDataProvider);
|
|
119
121
|
const entryPointArtifact = await this.executionDataProvider.getFunctionArtifact(contractAddress, selector);
|
|
120
122
|
|
|
@@ -124,7 +126,7 @@ export class AcirSimulator {
|
|
|
124
126
|
|
|
125
127
|
const context = new UnconstrainedExecutionOracle(
|
|
126
128
|
contractAddress,
|
|
127
|
-
|
|
129
|
+
authwits,
|
|
128
130
|
[],
|
|
129
131
|
this.executionDataProvider,
|
|
130
132
|
undefined,
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { Fr } from '@aztec/foundation/fields';
|
|
2
2
|
import { createLogger } from '@aztec/foundation/log';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
type AbiDecoded,
|
|
5
|
+
type FunctionArtifactWithContractName,
|
|
6
|
+
type FunctionSelector,
|
|
7
|
+
decodeFromAbi,
|
|
8
|
+
} from '@aztec/stdlib/abi';
|
|
4
9
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
5
10
|
|
|
6
11
|
import { ExecutionError, resolveAssertionMessageFromError } from '../common/errors.js';
|
|
@@ -16,7 +21,7 @@ import type { UnconstrainedExecutionOracle } from './unconstrained_execution_ora
|
|
|
16
21
|
export async function executeUnconstrainedFunction(
|
|
17
22
|
simulatorProvider: SimulationProvider,
|
|
18
23
|
oracle: UnconstrainedExecutionOracle,
|
|
19
|
-
artifact:
|
|
24
|
+
artifact: FunctionArtifactWithContractName,
|
|
20
25
|
contractAddress: AztecAddress,
|
|
21
26
|
functionSelector: FunctionSelector,
|
|
22
27
|
args: Fr[],
|
|
@@ -27,10 +32,9 @@ export async function executeUnconstrainedFunction(
|
|
|
27
32
|
selector: functionSelector,
|
|
28
33
|
});
|
|
29
34
|
|
|
30
|
-
const acir = artifact.bytecode;
|
|
31
35
|
const initialWitness = toACVMWitness(0, args);
|
|
32
36
|
const acirExecutionResult = await simulatorProvider
|
|
33
|
-
.executeUserCircuit(
|
|
37
|
+
.executeUserCircuit(initialWitness, artifact, new Oracle(oracle))
|
|
34
38
|
.catch((err: Error) => {
|
|
35
39
|
err.message = resolveAssertionMessageFromError(err, artifact);
|
|
36
40
|
throw new ExecutionError(
|