@aztec/simulator 0.81.0 → 0.82.1-alpha-testnet.1

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.
Files changed (138) hide show
  1. package/dest/common/db_interfaces.d.ts +6 -12
  2. package/dest/common/db_interfaces.d.ts.map +1 -1
  3. package/dest/common/db_interfaces.js +1 -1
  4. package/dest/common/debug_fn_name.js +5 -2
  5. package/dest/common/message_load_oracle_inputs.d.ts +4 -0
  6. package/dest/common/message_load_oracle_inputs.d.ts.map +1 -1
  7. package/dest/common/message_load_oracle_inputs.js +9 -0
  8. package/dest/private/acvm/acvm.d.ts +6 -1
  9. package/dest/private/acvm/acvm.d.ts.map +1 -1
  10. package/dest/private/acvm/acvm.js +7 -13
  11. package/dest/private/acvm/deserialize.d.ts +0 -18
  12. package/dest/private/acvm/deserialize.d.ts.map +1 -1
  13. package/dest/private/acvm/deserialize.js +3 -24
  14. package/dest/private/acvm/oracle/oracle.d.ts +34 -34
  15. package/dest/private/acvm/oracle/oracle.d.ts.map +1 -1
  16. package/dest/private/acvm/oracle/oracle.js +116 -82
  17. package/dest/private/acvm/oracle/typed_oracle.d.ts +4 -4
  18. package/dest/private/acvm/oracle/typed_oracle.d.ts.map +1 -1
  19. package/dest/private/acvm/oracle/typed_oracle.js +8 -8
  20. package/dest/private/execution_data_provider.d.ts +3 -9
  21. package/dest/private/execution_data_provider.d.ts.map +1 -1
  22. package/dest/private/hashed_values_cache.d.ts +2 -2
  23. package/dest/private/hashed_values_cache.d.ts.map +1 -1
  24. package/dest/private/hashed_values_cache.js +5 -15
  25. package/dest/private/private_execution.d.ts +2 -2
  26. package/dest/private/private_execution.d.ts.map +1 -1
  27. package/dest/private/private_execution.js +4 -7
  28. package/dest/private/private_execution_oracle.d.ts +9 -37
  29. package/dest/private/private_execution_oracle.d.ts.map +1 -1
  30. package/dest/private/private_execution_oracle.js +32 -92
  31. package/dest/private/providers/acvm_native.d.ts +6 -4
  32. package/dest/private/providers/acvm_native.d.ts.map +1 -1
  33. package/dest/private/providers/acvm_native.js +6 -3
  34. package/dest/private/providers/acvm_wasm.d.ts +6 -7
  35. package/dest/private/providers/acvm_wasm.d.ts.map +1 -1
  36. package/dest/private/providers/acvm_wasm.js +13 -15
  37. package/dest/private/providers/acvm_wasm_with_blobs.d.ts +5 -5
  38. package/dest/private/providers/acvm_wasm_with_blobs.d.ts.map +1 -1
  39. package/dest/private/providers/acvm_wasm_with_blobs.js +7 -9
  40. package/dest/private/providers/circuit_recording/circuit_recorder.d.ts +90 -0
  41. package/dest/private/providers/circuit_recording/circuit_recorder.d.ts.map +1 -0
  42. package/dest/private/providers/circuit_recording/circuit_recorder.js +246 -0
  43. package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts +18 -0
  44. package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts.map +1 -0
  45. package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.js +39 -0
  46. package/dest/private/providers/simulation_provider.d.ts +21 -7
  47. package/dest/private/providers/simulation_provider.d.ts.map +1 -1
  48. package/dest/private/simulator.d.ts +3 -2
  49. package/dest/private/simulator.d.ts.map +1 -1
  50. package/dest/private/simulator.js +14 -4
  51. package/dest/private/unconstrained_execution.d.ts +2 -2
  52. package/dest/private/unconstrained_execution.d.ts.map +1 -1
  53. package/dest/private/unconstrained_execution.js +1 -2
  54. package/dest/private/unconstrained_execution_oracle.d.ts +1 -1
  55. package/dest/private/unconstrained_execution_oracle.d.ts.map +1 -1
  56. package/dest/private/unconstrained_execution_oracle.js +3 -3
  57. package/dest/public/avm/avm_simulator.d.ts.map +1 -1
  58. package/dest/public/avm/avm_simulator.js +0 -2
  59. package/dest/public/avm/fixtures/avm_simulation_tester.js +2 -2
  60. package/dest/public/avm/fixtures/index.d.ts +2 -1
  61. package/dest/public/avm/fixtures/index.d.ts.map +1 -1
  62. package/dest/public/avm/fixtures/index.js +7 -12
  63. package/dest/public/avm/fixtures/simple_contract_data_source.d.ts +2 -2
  64. package/dest/public/avm/fixtures/simple_contract_data_source.d.ts.map +1 -1
  65. package/dest/public/avm/fixtures/simple_contract_data_source.js +1 -1
  66. package/dest/public/avm/journal/journal.d.ts +2 -2
  67. package/dest/public/avm/journal/journal.d.ts.map +1 -1
  68. package/dest/public/avm/journal/journal.js +4 -4
  69. package/dest/public/avm/test_utils.js +1 -1
  70. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +2 -2
  71. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  72. package/dest/public/fixtures/public_tx_simulation_tester.js +27 -47
  73. package/dest/public/fixtures/utils.d.ts +2 -2
  74. package/dest/public/fixtures/utils.d.ts.map +1 -1
  75. package/dest/public/fixtures/utils.js +18 -22
  76. package/dest/public/index.d.ts +1 -2
  77. package/dest/public/index.d.ts.map +1 -1
  78. package/dest/public/index.js +1 -1
  79. package/dest/public/public_db_sources.d.ts +1 -1
  80. package/dest/public/public_db_sources.d.ts.map +1 -1
  81. package/dest/public/public_db_sources.js +4 -4
  82. package/dest/public/public_processor/public_processor.js +1 -1
  83. package/dest/public/public_tx_simulator/public_tx_context.d.ts +3 -10
  84. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
  85. package/dest/public/public_tx_simulator/public_tx_context.js +4 -22
  86. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +2 -3
  87. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
  88. package/dest/public/public_tx_simulator/public_tx_simulator.js +20 -24
  89. package/dest/public/utils.d.ts +2 -4
  90. package/dest/public/utils.d.ts.map +1 -1
  91. package/dest/public/utils.js +4 -21
  92. package/dest/testing.d.ts +2 -0
  93. package/dest/testing.d.ts.map +1 -0
  94. package/dest/testing.js +1 -0
  95. package/package.json +15 -14
  96. package/src/common/db_interfaces.ts +6 -13
  97. package/src/common/debug_fn_name.ts +5 -5
  98. package/src/common/message_load_oracle_inputs.ts +8 -0
  99. package/src/private/acvm/acvm.ts +8 -24
  100. package/src/private/acvm/deserialize.ts +3 -30
  101. package/src/private/acvm/oracle/oracle.ts +148 -144
  102. package/src/private/acvm/oracle/typed_oracle.ts +12 -14
  103. package/src/private/execution_data_provider.ts +6 -10
  104. package/src/private/hashed_values_cache.ts +6 -14
  105. package/src/private/private_execution.ts +11 -11
  106. package/src/private/private_execution_oracle.ts +39 -138
  107. package/src/private/providers/acvm_native.ts +17 -6
  108. package/src/private/providers/acvm_wasm.ts +27 -20
  109. package/src/private/providers/acvm_wasm_with_blobs.ts +15 -12
  110. package/src/private/providers/circuit_recording/circuit_recorder.ts +283 -0
  111. package/src/private/providers/circuit_recording/simulation_provider_recorder_wrapper.ts +82 -0
  112. package/src/private/providers/simulation_provider.ts +30 -5
  113. package/src/private/simulator.ts +19 -5
  114. package/src/private/unconstrained_execution.ts +8 -4
  115. package/src/private/unconstrained_execution_oracle.ts +3 -6
  116. package/src/public/avm/avm_simulator.ts +0 -2
  117. package/src/public/avm/fixtures/avm_simulation_tester.ts +2 -2
  118. package/src/public/avm/fixtures/index.ts +15 -17
  119. package/src/public/avm/fixtures/simple_contract_data_source.ts +2 -2
  120. package/src/public/avm/journal/journal.ts +7 -7
  121. package/src/public/avm/test_utils.ts +1 -1
  122. package/src/public/fixtures/public_tx_simulation_tester.ts +31 -88
  123. package/src/public/fixtures/utils.ts +28 -26
  124. package/src/public/index.ts +1 -2
  125. package/src/public/public_db_sources.ts +4 -4
  126. package/src/public/public_processor/public_processor.ts +1 -1
  127. package/src/public/public_tx_simulator/public_tx_context.ts +12 -32
  128. package/src/public/public_tx_simulator/public_tx_simulator.ts +24 -30
  129. package/src/public/utils.ts +5 -21
  130. package/src/testing.ts +1 -0
  131. package/dest/public/avm/bytecode_utils.d.ts +0 -5
  132. package/dest/public/avm/bytecode_utils.d.ts.map +0 -1
  133. package/dest/public/avm/bytecode_utils.js +0 -17
  134. package/dest/public/execution.d.ts +0 -108
  135. package/dest/public/execution.d.ts.map +0 -1
  136. package/dest/public/execution.js +0 -9
  137. package/src/public/avm/bytecode_utils.ts +0 -17
  138. package/src/public/execution.ts +0 -140
@@ -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 { NoirCompiledCircuit } from '@aztec/stdlib/noir';
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
- executeProtocolCircuit(input: WitnessMap, compiledCircuit: NoirCompiledCircuit): Promise<WitnessMap>;
15
- executeUserCircuit(acir: Buffer, initialWitness: ACVMWitness, callback: ACIRCallback): Promise<ACIRExecutionResult>;
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[] };
@@ -1,9 +1,10 @@
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
- import { CallContext, PrivateExecutionResult, TxExecutionRequest } from '@aztec/stdlib/tx';
7
+ import { CallContext, HashedValues, PrivateExecutionResult, TxExecutionRequest, collectNested } from '@aztec/stdlib/tx';
7
8
 
8
9
  import { createSimulationError } from '../common/errors.js';
9
10
  import type { ExecutionDataProvider } from './execution_data_provider.js';
@@ -96,7 +97,19 @@ export class AcirSimulator {
96
97
  );
97
98
  const { usedTxRequestHashForNonces } = noteCache.finish();
98
99
  const firstNullifierHint = usedTxRequestHashForNonces ? Fr.ZERO : noteCache.getAllNullifiers()[0];
99
- return new PrivateExecutionResult(executionResult, firstNullifierHint);
100
+
101
+ const publicCallRequests = collectNested([executionResult], r => [
102
+ ...r.publicInputs.publicCallRequests.map(r => r.inner),
103
+ r.publicInputs.publicTeardownCallRequest,
104
+ ]).filter(r => !r.isEmpty());
105
+ const publicFunctionsCalldata = await Promise.all(
106
+ publicCallRequests.map(async r => {
107
+ const calldata = await context.loadFromExecutionCache(r.calldataHash);
108
+ return new HashedValues(calldata, r.calldataHash);
109
+ }),
110
+ );
111
+
112
+ return new PrivateExecutionResult(executionResult, firstNullifierHint, publicFunctionsCalldata);
100
113
  } catch (err) {
101
114
  throw createSimulationError(err instanceof Error ? err : new Error('Unknown error during private execution'));
102
115
  }
@@ -113,8 +126,9 @@ export class AcirSimulator {
113
126
  request: FunctionCall,
114
127
  contractAddress: AztecAddress,
115
128
  selector: FunctionSelector,
129
+ authwits: AuthWitness[],
116
130
  scopes?: AztecAddress[],
117
- ) {
131
+ ): Promise<AbiDecoded> {
118
132
  await verifyCurrentClassId(contractAddress, this.executionDataProvider);
119
133
  const entryPointArtifact = await this.executionDataProvider.getFunctionArtifact(contractAddress, selector);
120
134
 
@@ -124,7 +138,7 @@ export class AcirSimulator {
124
138
 
125
139
  const context = new UnconstrainedExecutionOracle(
126
140
  contractAddress,
127
- [],
141
+ authwits,
128
142
  [],
129
143
  this.executionDataProvider,
130
144
  undefined,
@@ -1,6 +1,11 @@
1
1
  import type { Fr } from '@aztec/foundation/fields';
2
2
  import { createLogger } from '@aztec/foundation/log';
3
- import { type AbiDecoded, type FunctionArtifact, type FunctionSelector, decodeFromAbi } from '@aztec/stdlib/abi';
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: FunctionArtifact,
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(acir, initialWitness, new Oracle(oracle))
37
+ .executeUserCircuit(initialWitness, artifact, new Oracle(oracle))
34
38
  .catch((err: Error) => {
35
39
  err.message = resolveAssertionMessageFromError(err, artifact);
36
40
  throw new ExecutionError(
@@ -103,11 +103,11 @@ export class UnconstrainedExecutionOracle extends TypedOracle {
103
103
  * @param leafSlot - The slot of the public data tree to get the witness for.
104
104
  * @returns - The witness
105
105
  */
106
- public override async getPublicDataTreeWitness(
106
+ public override async getPublicDataWitness(
107
107
  blockNumber: number,
108
108
  leafSlot: Fr,
109
109
  ): Promise<PublicDataWitness | undefined> {
110
- return await this.executionDataProvider.getPublicDataTreeWitness(blockNumber, leafSlot);
110
+ return await this.executionDataProvider.getPublicDataWitness(blockNumber, leafSlot);
111
111
  }
112
112
 
113
113
  /**
@@ -149,10 +149,7 @@ export class UnconstrainedExecutionOracle extends TypedOracle {
149
149
  * @returns Authentication witness for the requested message hash.
150
150
  */
151
151
  public override getAuthWitness(messageHash: Fr): Promise<Fr[] | undefined> {
152
- return Promise.resolve(
153
- this.authWitnesses.find(w => w.requestHash.equals(messageHash))?.witness ??
154
- this.executionDataProvider.getAuthWitness(messageHash),
155
- );
152
+ return Promise.resolve(this.authWitnesses.find(w => w.requestHash.equals(messageHash))?.witness);
156
153
  }
157
154
 
158
155
  /**
@@ -12,7 +12,6 @@ import { AvmContractCallResult } from './avm_contract_call_result.js';
12
12
  import { AvmExecutionEnvironment } from './avm_execution_environment.js';
13
13
  import type { Gas } from './avm_gas.js';
14
14
  import { AvmMachineState } from './avm_machine_state.js';
15
- import { isAvmBytecode } from './bytecode_utils.js';
16
15
  import {
17
16
  AvmExecutionError,
18
17
  AvmRevertReason,
@@ -139,7 +138,6 @@ export class AvmSimulator {
139
138
  */
140
139
  public async executeBytecode(bytecode: Buffer): Promise<AvmContractCallResult> {
141
140
  const startTotalTime = performance.now();
142
- assert(isAvmBytecode(bytecode), "AVM simulator can't execute non-AVM bytecode");
143
141
  assert(bytecode.length > 0, "AVM simulator can't execute empty bytecode");
144
142
 
145
143
  this.bytecode = bytecode;
@@ -9,7 +9,7 @@ import { NativeWorldStateService } from '@aztec/world-state';
9
9
  import { SideEffectTrace } from '../../../public/side_effect_trace.js';
10
10
  import type { AvmContractCallResult } from '../../avm/avm_contract_call_result.js';
11
11
  import {
12
- getContractFunctionArtifact,
12
+ getContractFunctionAbi,
13
13
  getFunctionSelector,
14
14
  initContext,
15
15
  initExecutionEnvironment,
@@ -73,7 +73,7 @@ export class AvmSimulationTester extends BaseAvmSimulationTester {
73
73
  throw new Error(`Contract not found at address: ${address}`);
74
74
  }
75
75
  const fnSelector = await getFunctionSelector(fnName, contractArtifact);
76
- const fnAbi = getContractFunctionArtifact(fnName, contractArtifact);
76
+ const fnAbi = getContractFunctionAbi(fnName, contractArtifact);
77
77
  const encodedArgs = encodeArguments(fnAbi!, args);
78
78
  const calldata = [fnSelector.toField(), ...encodedArgs];
79
79
 
@@ -1,8 +1,4 @@
1
- import {
2
- DEPLOYER_CONTRACT_ADDRESS,
3
- MAX_L2_GAS_PER_TX_PUBLIC_PORTION,
4
- PUBLIC_DISPATCH_SELECTOR,
5
- } from '@aztec/constants';
1
+ import { DEPLOYER_CONTRACT_ADDRESS, MAX_L2_GAS_PER_TX_PUBLIC_PORTION } from '@aztec/constants';
6
2
  import { EthAddress } from '@aztec/foundation/eth-address';
7
3
  import { Fr } from '@aztec/foundation/fields';
8
4
  import { AvmGadgetsTestContract } from '@aztec/noir-contracts.js/AvmGadgetsTest';
@@ -165,13 +161,18 @@ export function getFunctionSelector(
165
161
  export function getContractFunctionArtifact(
166
162
  functionName: string,
167
163
  contractArtifact: ContractArtifact,
168
- ): FunctionArtifact | FunctionAbi | undefined {
169
- const artifact = contractArtifact.functions.find(f => f.name === functionName)!;
170
- if (!artifact) {
171
- const abi = getAllFunctionAbis(contractArtifact).find(f => f.name === functionName);
172
- return abi || undefined;
173
- }
174
- return artifact;
164
+ ): FunctionArtifact | undefined {
165
+ return contractArtifact.functions.find(f => f.name === functionName);
166
+ }
167
+
168
+ export function getContractFunctionAbi(
169
+ functionName: string,
170
+ contractArtifact: ContractArtifact,
171
+ ): FunctionAbi | undefined {
172
+ return (
173
+ contractArtifact.functions.find(f => f.name === functionName) ??
174
+ contractArtifact.nonDispatchPublicFunctions.find(f => f.name === functionName)
175
+ );
175
176
  }
176
177
 
177
178
  export function resolveContractAssertionMessage(
@@ -279,12 +280,9 @@ export async function createContractClassAndInstance(
279
280
  }> {
280
281
  const bytecode = (getContractFunctionArtifact(PUBLIC_DISPATCH_FN_NAME, contractArtifact) as FunctionArtifact)!
281
282
  .bytecode;
282
- const contractClass = await makeContractClassPublic(
283
- seed,
284
- /*publicDispatchFunction=*/ { bytecode, selector: new FunctionSelector(PUBLIC_DISPATCH_SELECTOR) },
285
- );
283
+ const contractClass = await makeContractClassPublic(seed, bytecode);
286
284
 
287
- const constructorAbi = getContractFunctionArtifact('constructor', contractArtifact);
285
+ const constructorAbi = getContractFunctionAbi('constructor', contractArtifact);
288
286
  const { publicKeys } = await deriveKeys(Fr.random());
289
287
  const initializationHash = await computeInitializationHash(constructorAbi, constructorArgs);
290
288
  const contractInstance =
@@ -1,6 +1,6 @@
1
1
  import type { Fr } from '@aztec/foundation/fields';
2
2
  import { createLogger } from '@aztec/foundation/log';
3
- import { type ContractArtifact, FunctionSelector } from '@aztec/stdlib/abi';
3
+ import type { ContractArtifact, FunctionSelector } from '@aztec/stdlib/abi';
4
4
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
5
5
  import type { ContractClassPublic, ContractDataSource, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
6
6
 
@@ -76,7 +76,7 @@ export class SimpleContractDataSource implements ContractDataSource {
76
76
  return Promise.resolve(this.contractArtifacts.get(contractInstance!.currentContractClassId.toString()));
77
77
  }
78
78
 
79
- getContractFunctionName(_address: AztecAddress, _selector: FunctionSelector): Promise<string> {
79
+ getDebugFunctionName(_address: AztecAddress, _selector: FunctionSelector): Promise<string> {
80
80
  return Promise.resolve(PUBLIC_DISPATCH_FN_NAME);
81
81
  }
82
82
 
@@ -13,8 +13,8 @@ import { createLogger } from '@aztec/foundation/log';
13
13
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
14
14
  import { PublicDataWrite } from '@aztec/stdlib/avm';
15
15
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
16
+ import type { ContractClassPublicWithCommitment, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
16
17
  import { SerializableContractInstance } from '@aztec/stdlib/contract';
17
- import type { ContractClassWithCommitment, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
18
18
  import {
19
19
  computeNoteHashNonce,
20
20
  computePublicDataTreeLeafSlot,
@@ -194,7 +194,7 @@ export class AvmPersistableStateManager {
194
194
  * @returns true if the note hash exists at the given leaf index, false otherwise
195
195
  */
196
196
  public async checkNoteHashExists(contractAddress: AztecAddress, noteHash: Fr, leafIndex: Fr): Promise<boolean> {
197
- const gotLeafValue = (await this.treesDB.getCommitmentValue(leafIndex.toBigInt())) ?? Fr.ZERO;
197
+ const gotLeafValue = (await this.treesDB.getNoteHash(leafIndex.toBigInt())) ?? Fr.ZERO;
198
198
  const exists = gotLeafValue.equals(noteHash);
199
199
  this.log.trace(
200
200
  `noteHashes(${contractAddress})@${noteHash} ?? leafIndex: ${leafIndex} | gotLeafValue: ${gotLeafValue}, exists: ${exists}.`,
@@ -439,11 +439,11 @@ export class AvmPersistableStateManager {
439
439
  * @param classId - class id to retrieve.
440
440
  * @returns the contract class or undefined if it does not exist.
441
441
  */
442
- public async getContractClass(classId: Fr): Promise<ContractClassWithCommitment | undefined> {
442
+ public async getContractClass(classId: Fr): Promise<ContractClassPublicWithCommitment | undefined> {
443
443
  this.log.trace(`Getting contract class for id ${classId}`);
444
- const klass = await this.contractsDB.getContractClass(classId);
445
- const exists = klass !== undefined;
446
- let extendedClass: ContractClassWithCommitment | undefined = undefined;
444
+ const contractClass = await this.contractsDB.getContractClass(classId);
445
+ const exists = contractClass !== undefined;
446
+ let extendedClass: ContractClassPublicWithCommitment | undefined = undefined;
447
447
 
448
448
  // Note: We currently do not generate info to check the nullifier tree, because
449
449
  // this is not needed for our use cases.
@@ -456,7 +456,7 @@ export class AvmPersistableStateManager {
456
456
  `Bytecode commitment was not found in DB for contract class (${classId}). This should not happen!`,
457
457
  );
458
458
  extendedClass = {
459
- ...klass,
459
+ ...contractClass,
460
460
  publicBytecodeCommitment: bytecodeCommitment,
461
461
  };
462
462
  } else {