@aztec/simulator 0.26.6 → 0.27.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 (59) hide show
  1. package/dest/acvm/acvm.js +2 -2
  2. package/dest/acvm/serialize.d.ts.map +1 -1
  3. package/dest/acvm/serialize.js +8 -3
  4. package/dest/avm/avm_context.d.ts +3 -3
  5. package/dest/avm/avm_context.d.ts.map +1 -1
  6. package/dest/avm/avm_context.js +6 -5
  7. package/dest/avm/avm_execution_environment.d.ts +3 -3
  8. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  9. package/dest/avm/avm_execution_environment.js +16 -17
  10. package/dest/avm/avm_memory_types.d.ts +9 -0
  11. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  12. package/dest/avm/avm_memory_types.js +34 -4
  13. package/dest/avm/avm_simulator.d.ts.map +1 -1
  14. package/dest/avm/avm_simulator.js +3 -3
  15. package/dest/avm/opcodes/arithmetic.d.ts.map +1 -1
  16. package/dest/avm/opcodes/arithmetic.js +2 -1
  17. package/dest/avm/opcodes/comparators.d.ts.map +1 -1
  18. package/dest/avm/opcodes/comparators.js +5 -7
  19. package/dest/avm/opcodes/control_flow.d.ts +0 -20
  20. package/dest/avm/opcodes/control_flow.d.ts.map +1 -1
  21. package/dest/avm/opcodes/control_flow.js +2 -46
  22. package/dest/avm/opcodes/external_calls.d.ts +24 -2
  23. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  24. package/dest/avm/opcodes/external_calls.js +74 -17
  25. package/dest/avm/serialization/instruction_serialization.d.ts +1 -2
  26. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  27. package/dest/avm/serialization/instruction_serialization.js +9 -3
  28. package/dest/avm/temporary_executor_migration.d.ts.map +1 -1
  29. package/dest/avm/temporary_executor_migration.js +3 -1
  30. package/dest/client/client_execution_context.d.ts.map +1 -1
  31. package/dest/client/client_execution_context.js +4 -6
  32. package/dest/client/simulator.d.ts.map +1 -1
  33. package/dest/client/simulator.js +3 -3
  34. package/dest/public/execution.d.ts +2 -0
  35. package/dest/public/execution.d.ts.map +1 -1
  36. package/dest/public/execution.js +1 -1
  37. package/dest/public/executor.d.ts +21 -0
  38. package/dest/public/executor.d.ts.map +1 -1
  39. package/dest/public/executor.js +91 -2
  40. package/dest/public/public_execution_context.d.ts.map +1 -1
  41. package/dest/public/public_execution_context.js +1 -2
  42. package/package.json +5 -5
  43. package/src/acvm/acvm.ts +1 -1
  44. package/src/acvm/serialize.ts +9 -3
  45. package/src/avm/avm_context.ts +21 -5
  46. package/src/avm/avm_execution_environment.ts +27 -12
  47. package/src/avm/avm_memory_types.ts +36 -3
  48. package/src/avm/avm_simulator.ts +10 -3
  49. package/src/avm/opcodes/arithmetic.ts +2 -0
  50. package/src/avm/opcodes/comparators.ts +4 -6
  51. package/src/avm/opcodes/control_flow.ts +1 -47
  52. package/src/avm/opcodes/external_calls.ts +88 -14
  53. package/src/avm/serialization/instruction_serialization.ts +8 -2
  54. package/src/avm/temporary_executor_migration.ts +2 -0
  55. package/src/client/client_execution_context.ts +1 -13
  56. package/src/client/simulator.ts +1 -2
  57. package/src/public/execution.ts +2 -0
  58. package/src/public/executor.ts +103 -0
  59. package/src/public/public_execution_context.ts +0 -1
@@ -2,6 +2,10 @@ import { FunctionL2Logs } from '@aztec/circuit-types';
2
2
  import { GlobalVariables, Header, PublicCircuitPublicInputs } from '@aztec/circuits.js';
3
3
  import { createDebugLogger } from '@aztec/foundation/log';
4
4
 
5
+ import { spawn } from 'child_process';
6
+ import fs from 'fs/promises';
7
+ import path from 'path';
8
+
5
9
  import { Oracle, acvm, extractCallStack, extractReturnWitness } from '../acvm/index.js';
6
10
  import { AvmContext } from '../avm/avm_context.js';
7
11
  import { AvmMachineState } from '../avm/avm_machine_state.js';
@@ -81,6 +85,7 @@ export async function executePublicFunction(
81
85
  newL2ToL1Messages: [],
82
86
  newNullifiers: [],
83
87
  nullifierReadRequests: [],
88
+ nullifierNonExistentReadRequests: [],
84
89
  contractStorageReads: [],
85
90
  contractStorageUpdateRequests: [],
86
91
  nestedExecutions: [],
@@ -98,12 +103,14 @@ export async function executePublicFunction(
98
103
  const {
99
104
  returnValues,
100
105
  nullifierReadRequests: nullifierReadRequestsPadded,
106
+ nullifierNonExistentReadRequests: nullifierNonExistentReadRequestsPadded,
101
107
  newL2ToL1Msgs,
102
108
  newNoteHashes: newNoteHashesPadded,
103
109
  newNullifiers: newNullifiersPadded,
104
110
  } = PublicCircuitPublicInputs.fromFields(returnWitness);
105
111
 
106
112
  const nullifierReadRequests = nullifierReadRequestsPadded.filter(v => !v.isEmpty());
113
+ const nullifierNonExistentReadRequests = nullifierNonExistentReadRequestsPadded.filter(v => !v.isEmpty());
107
114
  const newL2ToL1Messages = newL2ToL1Msgs.filter(v => !v.isEmpty());
108
115
  const newNoteHashes = newNoteHashesPadded.filter(v => !v.isEmpty());
109
116
  const newNullifiers = newNullifiersPadded.filter(v => !v.isEmpty());
@@ -130,6 +137,7 @@ export async function executePublicFunction(
130
137
  newL2ToL1Messages,
131
138
  newNullifiers,
132
139
  nullifierReadRequests,
140
+ nullifierNonExistentReadRequests,
133
141
  contractStorageReads,
134
142
  contractStorageUpdateRequests,
135
143
  returnValues,
@@ -220,4 +228,99 @@ export class PublicExecutor {
220
228
  const newWorldState = context.persistableState.flush();
221
229
  return temporaryConvertAvmResults(execution, newWorldState, result);
222
230
  }
231
+
232
+ /**
233
+ * These functions are currently housed in the temporary executor as it relies on access to
234
+ * oracles like the contractsDB and this is the least intrusive way to achieve this.
235
+ * When we remove this executor(tracking issue #4792) and have an interface that is compatible with the kernel circuits,
236
+ * this will be moved to sequencer-client/prover.
237
+ */
238
+
239
+ /**
240
+ * Generates a proof for an associated avm execution. This is currently only used for testing purposes,
241
+ * as proof generation is not fully complete in the AVM yet.
242
+ * @param execution - The execution to run.
243
+ * @returns An AVM proof and the verification key.
244
+ */
245
+ public async getAvmProof(avmExecution: PublicExecution): Promise<Buffer[]> {
246
+ // The paths for the barretenberg binary and the write path are hardcoded for now.
247
+ // We additionally need the path to a valid crs for proof generation.
248
+ // const bbPath = '../../barretenberg/cpp';
249
+ const bbPath = path.resolve('../../barretenberg/cpp');
250
+ const artifactsPath = path.resolve('target');
251
+
252
+ // Create the directory if it does not exist
253
+ await fs.mkdir(artifactsPath, { recursive: true });
254
+
255
+ const calldataPath = path.join(artifactsPath, 'calldata.bin');
256
+ const bytecodePath = path.join(artifactsPath, 'avm_bytecode.bin');
257
+ const proofPath = path.join(artifactsPath, 'proof');
258
+
259
+ const { args, functionData, contractAddress } = avmExecution;
260
+ const bytecode = await this.contractsDb.getBytecode(contractAddress, functionData.selector);
261
+ // Write call data and bytecode to files.
262
+ await Promise.all([
263
+ fs.writeFile(
264
+ calldataPath,
265
+ args.map(c => c.toBuffer()),
266
+ ),
267
+ fs.writeFile(bytecodePath, bytecode!),
268
+ ]);
269
+
270
+ const bbBinary = spawn(path.join(bbPath, 'build', 'bin', 'bb'), [
271
+ 'avm_prove',
272
+ '-b',
273
+ bytecodePath,
274
+ '-d',
275
+ calldataPath,
276
+ '-c',
277
+ path.join(bbPath, 'srs_db', 'ignition'),
278
+ '-o',
279
+ proofPath,
280
+ ]);
281
+ // The binary writes the proof and the verification key to the write path.
282
+ return new Promise((resolve, reject) => {
283
+ bbBinary.on('close', () => {
284
+ resolve(Promise.all([fs.readFile(proofPath), fs.readFile(path.join(artifactsPath, 'vk'))]));
285
+ });
286
+ // Catch and propagate errors from spawning
287
+ bbBinary.on('error', err => {
288
+ reject(err);
289
+ });
290
+ });
291
+ }
292
+ /**
293
+ * Verifies an AVM proof. This function is currently only used for testing purposes, as verification
294
+ * is not fully complete in the AVM yet.
295
+ * @param vk - The verification key to use.
296
+ * @param proof - The proof to verify.
297
+ * @returns True if the proof is valid, false otherwise.
298
+ */
299
+ async verifyAvmProof(vk: Buffer, proof: Buffer): Promise<boolean> {
300
+ // The relative paths for the barretenberg binary and the write path are hardcoded for now.
301
+ const bbPath = path.resolve('../../barretenberg/cpp');
302
+ const artifactsPath = path.resolve('./target');
303
+
304
+ const vkPath = path.join(artifactsPath, 'vk');
305
+ const proofPath = path.join(artifactsPath, 'proof');
306
+
307
+ // Write the verification key and the proof to files.
308
+ await Promise.all([fs.writeFile(vkPath, vk), fs.writeFile(proofPath, proof)]);
309
+
310
+ const bbBinary = spawn(path.join(bbPath, 'build', 'bin', 'bb'), ['avm_verify', '-p', proofPath]);
311
+ // The binary prints to stdout 1 if the proof is valid and 0 if it is not.
312
+ return new Promise((resolve, reject) => {
313
+ let result = Buffer.alloc(0);
314
+ bbBinary.stdout.on('data', data => {
315
+ result += data;
316
+ });
317
+ bbBinary.on('close', () => {
318
+ resolve(result.toString() === '1');
319
+ });
320
+ // Catch and propagate errors from spawning
321
+ bbBinary.on('error', err => {
322
+ reject(err);
323
+ });
324
+ });
325
+ }
223
326
  }
@@ -186,7 +186,6 @@ export class PublicExecutionContext extends TypedOracle {
186
186
  storageContractAddress: isDelegateCall ? this.execution.contractAddress : targetContractAddress,
187
187
  portalContractAddress: portalAddress,
188
188
  functionSelector,
189
- isContractDeployment: false,
190
189
  isDelegateCall,
191
190
  isStaticCall,
192
191
  startSideEffectCounter: 0, // TODO use counters in public execution