@aztec/simulator 0.33.0 → 0.35.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.
Files changed (186) hide show
  1. package/dest/acvm/acvm.d.ts +4 -1
  2. package/dest/acvm/acvm.d.ts.map +1 -1
  3. package/dest/acvm/acvm.js +5 -5
  4. package/dest/acvm/deserialize.d.ts +3 -5
  5. package/dest/acvm/deserialize.d.ts.map +1 -1
  6. package/dest/acvm/deserialize.js +6 -9
  7. package/dest/acvm/oracle/oracle.d.ts +3 -1
  8. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  9. package/dest/acvm/oracle/oracle.js +14 -6
  10. package/dest/acvm/oracle/typed_oracle.d.ts +2 -0
  11. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  12. package/dest/acvm/oracle/typed_oracle.js +7 -1
  13. package/dest/avm/avm_execution_environment.d.ts +6 -3
  14. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  15. package/dest/avm/avm_execution_environment.js +12 -9
  16. package/dest/avm/avm_gas.js +2 -2
  17. package/dest/avm/avm_machine_state.d.ts +2 -0
  18. package/dest/avm/avm_machine_state.d.ts.map +1 -1
  19. package/dest/avm/avm_machine_state.js +24 -6
  20. package/dest/avm/avm_memory_types.js +6 -6
  21. package/dest/avm/avm_simulator.js +7 -7
  22. package/dest/avm/fixtures/index.d.ts +3 -0
  23. package/dest/avm/fixtures/index.d.ts.map +1 -1
  24. package/dest/avm/fixtures/index.js +11 -4
  25. package/dest/avm/journal/journal.d.ts +15 -13
  26. package/dest/avm/journal/journal.d.ts.map +1 -1
  27. package/dest/avm/journal/journal.js +16 -5
  28. package/dest/avm/journal/trace.d.ts +8 -19
  29. package/dest/avm/journal/trace.d.ts.map +1 -1
  30. package/dest/avm/journal/trace.js +48 -116
  31. package/dest/avm/journal/trace_types.d.ts +23 -4
  32. package/dest/avm/journal/trace_types.d.ts.map +1 -1
  33. package/dest/avm/opcodes/accrued_substate.d.ts +2 -1
  34. package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
  35. package/dest/avm/opcodes/accrued_substate.js +14 -6
  36. package/dest/avm/opcodes/context_getters.js +2 -2
  37. package/dest/avm/opcodes/external_calls.d.ts +2 -2
  38. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  39. package/dest/avm/opcodes/external_calls.js +26 -10
  40. package/dest/avm/opcodes/hashing.d.ts +8 -8
  41. package/dest/avm/opcodes/hashing.d.ts.map +1 -1
  42. package/dest/avm/opcodes/hashing.js +35 -43
  43. package/dest/avm/serialization/instruction_serialization.d.ts +1 -1
  44. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  45. package/dest/avm/serialization/instruction_serialization.js +2 -2
  46. package/dest/client/client_execution_context.d.ts +14 -4
  47. package/dest/client/client_execution_context.d.ts.map +1 -1
  48. package/dest/client/client_execution_context.js +28 -13
  49. package/dest/client/private_execution.d.ts +1 -1
  50. package/dest/client/private_execution.d.ts.map +1 -1
  51. package/dest/client/private_execution.js +10 -8
  52. package/dest/client/simulator.d.ts.map +1 -1
  53. package/dest/client/simulator.js +6 -5
  54. package/dest/client/unconstrained_execution.d.ts +1 -1
  55. package/dest/client/unconstrained_execution.d.ts.map +1 -1
  56. package/dest/client/unconstrained_execution.js +6 -5
  57. package/dest/client/view_data_oracle.d.ts +2 -2
  58. package/dest/client/view_data_oracle.d.ts.map +1 -1
  59. package/dest/client/view_data_oracle.js +2 -2
  60. package/dest/common/index.d.ts +1 -1
  61. package/dest/common/index.d.ts.map +1 -1
  62. package/dest/common/index.js +2 -2
  63. package/dest/common/packed_values_cache.d.ts +28 -0
  64. package/dest/common/packed_values_cache.d.ts.map +1 -0
  65. package/dest/common/packed_values_cache.js +50 -0
  66. package/dest/index.d.ts +1 -0
  67. package/dest/index.d.ts.map +1 -1
  68. package/dest/index.js +2 -1
  69. package/dest/mocks/fixtures.d.ts +42 -0
  70. package/dest/mocks/fixtures.d.ts.map +1 -0
  71. package/dest/mocks/fixtures.js +84 -0
  72. package/dest/mocks/index.d.ts +2 -0
  73. package/dest/mocks/index.d.ts.map +1 -0
  74. package/dest/mocks/index.js +2 -0
  75. package/dest/public/abstract_phase_manager.d.ts +82 -0
  76. package/dest/public/abstract_phase_manager.d.ts.map +1 -0
  77. package/dest/public/abstract_phase_manager.js +320 -0
  78. package/dest/public/app_logic_phase_manager.d.ts +29 -0
  79. package/dest/public/app_logic_phase_manager.d.ts.map +1 -0
  80. package/dest/public/app_logic_phase_manager.js +50 -0
  81. package/dest/public/execution.d.ts +3 -0
  82. package/dest/public/execution.d.ts.map +1 -1
  83. package/dest/public/execution.js +1 -1
  84. package/dest/public/executor.d.ts +1 -15
  85. package/dest/public/executor.d.ts.map +1 -1
  86. package/dest/public/executor.js +80 -83
  87. package/dest/public/hints_builder.d.ts +23 -0
  88. package/dest/public/hints_builder.d.ts.map +1 -0
  89. package/dest/public/hints_builder.js +62 -0
  90. package/dest/public/index.d.ts +5 -0
  91. package/dest/public/index.d.ts.map +1 -1
  92. package/dest/public/index.js +6 -1
  93. package/dest/public/phase_manager_factory.d.ts +18 -0
  94. package/dest/public/phase_manager_factory.d.ts.map +1 -0
  95. package/dest/public/phase_manager_factory.js +56 -0
  96. package/dest/public/public_execution_context.d.ts +20 -10
  97. package/dest/public/public_execution_context.d.ts.map +1 -1
  98. package/dest/public/public_execution_context.js +35 -23
  99. package/dest/public/public_executor.d.ts +79 -0
  100. package/dest/public/public_executor.d.ts.map +1 -0
  101. package/dest/public/public_executor.js +198 -0
  102. package/dest/public/public_kernel.d.ts +37 -0
  103. package/dest/public/public_kernel.d.ts.map +1 -0
  104. package/dest/public/public_kernel.js +97 -0
  105. package/dest/public/public_kernel_circuit_simulator.d.ts +31 -0
  106. package/dest/public/public_kernel_circuit_simulator.d.ts.map +1 -0
  107. package/dest/public/public_kernel_circuit_simulator.js +2 -0
  108. package/dest/public/public_processor.d.ts +53 -0
  109. package/dest/public/public_processor.d.ts.map +1 -0
  110. package/dest/public/public_processor.js +144 -0
  111. package/dest/public/setup_phase_manager.d.ts +30 -0
  112. package/dest/public/setup_phase_manager.d.ts.map +1 -0
  113. package/dest/public/setup_phase_manager.js +46 -0
  114. package/dest/public/tail_phase_manager.d.ts +30 -0
  115. package/dest/public/tail_phase_manager.d.ts.map +1 -0
  116. package/dest/public/tail_phase_manager.js +60 -0
  117. package/dest/public/teardown_phase_manager.d.ts +30 -0
  118. package/dest/public/teardown_phase_manager.d.ts.map +1 -0
  119. package/dest/public/teardown_phase_manager.js +46 -0
  120. package/dest/public/transitional_adaptors.d.ts +33 -0
  121. package/dest/public/transitional_adaptors.d.ts.map +1 -0
  122. package/dest/public/transitional_adaptors.js +162 -0
  123. package/dest/public/utils.d.ts +8 -0
  124. package/dest/public/utils.d.ts.map +1 -0
  125. package/dest/public/utils.js +29 -0
  126. package/dest/simulator/acvm_native.d.ts +19 -3
  127. package/dest/simulator/acvm_native.d.ts.map +1 -1
  128. package/dest/simulator/acvm_native.js +75 -48
  129. package/dest/simulator/acvm_wasm.d.ts.map +1 -1
  130. package/dest/simulator/acvm_wasm.js +3 -4
  131. package/package.json +8 -5
  132. package/src/acvm/acvm.ts +8 -5
  133. package/src/acvm/deserialize.ts +5 -9
  134. package/src/acvm/oracle/oracle.ts +15 -5
  135. package/src/acvm/oracle/typed_oracle.ts +8 -0
  136. package/src/avm/avm_execution_environment.ts +17 -17
  137. package/src/avm/avm_gas.ts +1 -1
  138. package/src/avm/avm_machine_state.ts +26 -5
  139. package/src/avm/avm_memory_types.ts +5 -5
  140. package/src/avm/avm_simulator.ts +6 -6
  141. package/src/avm/fixtures/index.ts +14 -1
  142. package/src/avm/journal/journal.ts +37 -17
  143. package/src/avm/journal/trace.ts +59 -121
  144. package/src/avm/journal/trace_types.ts +39 -39
  145. package/src/avm/opcodes/accrued_substate.ts +17 -5
  146. package/src/avm/opcodes/context_getters.ts +1 -1
  147. package/src/avm/opcodes/external_calls.ts +32 -9
  148. package/src/avm/opcodes/hashing.ts +38 -54
  149. package/src/avm/serialization/instruction_serialization.ts +1 -1
  150. package/src/client/client_execution_context.ts +30 -11
  151. package/src/client/private_execution.ts +9 -8
  152. package/src/client/simulator.ts +7 -3
  153. package/src/client/unconstrained_execution.ts +5 -4
  154. package/src/client/view_data_oracle.ts +1 -1
  155. package/src/common/index.ts +1 -1
  156. package/src/common/packed_values_cache.ts +55 -0
  157. package/src/index.ts +1 -0
  158. package/src/mocks/fixtures.ts +169 -0
  159. package/src/mocks/index.ts +1 -0
  160. package/src/public/abstract_phase_manager.ts +571 -0
  161. package/src/public/app_logic_phase_manager.ts +76 -0
  162. package/src/public/execution.ts +4 -0
  163. package/src/public/executor.ts +93 -93
  164. package/src/public/hints_builder.ts +119 -0
  165. package/src/public/index.ts +5 -0
  166. package/src/public/phase_manager_factory.ts +126 -0
  167. package/src/public/public_execution_context.ts +43 -37
  168. package/src/public/public_executor.ts +267 -0
  169. package/src/public/public_kernel.ts +139 -0
  170. package/src/public/public_kernel_circuit_simulator.ts +36 -0
  171. package/src/public/public_processor.ts +212 -0
  172. package/src/public/setup_phase_manager.ts +66 -0
  173. package/src/public/tail_phase_manager.ts +120 -0
  174. package/src/public/teardown_phase_manager.ts +66 -0
  175. package/src/public/transitional_adaptors.ts +249 -0
  176. package/src/public/utils.ts +31 -0
  177. package/src/simulator/acvm_native.ts +94 -47
  178. package/src/simulator/acvm_wasm.ts +7 -3
  179. package/dest/avm/temporary_executor_migration.d.ts +0 -27
  180. package/dest/avm/temporary_executor_migration.d.ts.map +0 -1
  181. package/dest/avm/temporary_executor_migration.js +0 -94
  182. package/dest/common/packed_args_cache.d.ts +0 -28
  183. package/dest/common/packed_args_cache.d.ts.map +0 -1
  184. package/dest/common/packed_args_cache.js +0 -50
  185. package/src/avm/temporary_executor_migration.ts +0 -136
  186. package/src/common/packed_args_cache.ts +0 -55
@@ -75,7 +75,7 @@ export enum Opcode {
75
75
  REVERT,
76
76
  // Gadgets
77
77
  KECCAK,
78
- POSEIDON,
78
+ POSEIDON2,
79
79
  SHA256, // temp - may be removed, but alot of contracts rely on it
80
80
  PEDERSEN, // temp - may be removed, but alot of contracts rely on it
81
81
  }
@@ -28,7 +28,7 @@ import { Fr, type Point } from '@aztec/foundation/fields';
28
28
  import { createDebugLogger } from '@aztec/foundation/log';
29
29
 
30
30
  import { type NoteData, toACVMWitness } from '../acvm/index.js';
31
- import { type PackedArgsCache } from '../common/packed_args_cache.js';
31
+ import { type PackedValuesCache } from '../common/packed_values_cache.js';
32
32
  import { type DBOracle } from './db_oracle.js';
33
33
  import { type ExecutionNoteCache } from './execution_note_cache.js';
34
34
  import { type ExecutionResult, type NoteAndSlot } from './execution_result.js';
@@ -72,7 +72,7 @@ export class ClientExecutionContext extends ViewDataOracle {
72
72
  protected readonly historicalHeader: Header,
73
73
  /** List of transient auth witnesses to be used during this simulation */
74
74
  protected readonly authWitnesses: AuthWitness[],
75
- private readonly packedArgsCache: PackedArgsCache,
75
+ private readonly packedValuesCache: PackedValuesCache,
76
76
  private readonly noteCache: ExecutionNoteCache,
77
77
  protected readonly db: DBOracle,
78
78
  private readonly curve: Grumpkin,
@@ -93,7 +93,7 @@ export class ClientExecutionContext extends ViewDataOracle {
93
93
  public getInitialWitness(abi: FunctionAbi) {
94
94
  const argumentsSize = countArgumentsSize(abi);
95
95
 
96
- const args = this.packedArgsCache.unpack(this.argsHash);
96
+ const args = this.packedValuesCache.unpack(this.argsHash);
97
97
 
98
98
  if (args.length !== argumentsSize) {
99
99
  throw new Error('Invalid arguments size');
@@ -175,7 +175,23 @@ export class ClientExecutionContext extends ViewDataOracle {
175
175
  * @param args - Arguments to pack
176
176
  */
177
177
  public packArguments(args: Fr[]): Promise<Fr> {
178
- return Promise.resolve(this.packedArgsCache.pack(args));
178
+ return Promise.resolve(this.packedValuesCache.pack(args));
179
+ }
180
+
181
+ /**
182
+ * Pack the given returns.
183
+ * @param returns - Returns to pack
184
+ */
185
+ public packReturns(returns: Fr[]): Promise<Fr> {
186
+ return Promise.resolve(this.packedValuesCache.pack(returns));
187
+ }
188
+
189
+ /**
190
+ * Unpack the given returns.
191
+ * @param returnsHash - Returns hash to unpack
192
+ */
193
+ public unpackReturns(returnsHash: Fr): Promise<Fr[]> {
194
+ return Promise.resolve(this.packedValuesCache.unpack(returnsHash));
179
195
  }
180
196
 
181
197
  /**
@@ -235,7 +251,7 @@ export class ClientExecutionContext extends ViewDataOracle {
235
251
  offset,
236
252
  });
237
253
 
238
- this.log(
254
+ this.log.debug(
239
255
  `Returning ${notes.length} notes for ${this.callContext.storageContractAddress} at ${storageSlot}: ${notes
240
256
  .map(n => `${n.nonce.toString()}:[${n.note.items.map(i => i.toString()).join(',')}]`)
241
257
  .join(', ')}`,
@@ -316,7 +332,7 @@ export class ClientExecutionContext extends ViewDataOracle {
316
332
  public emitUnencryptedLog(log: UnencryptedL2Log) {
317
333
  this.unencryptedLogs.push(log);
318
334
  const text = log.toHumanReadable();
319
- this.log(`Emitted unencrypted log: "${text.length > 100 ? text.slice(0, 100) + '...' : text}"`);
335
+ this.log.verbose(`Emitted unencrypted log: "${text.length > 100 ? text.slice(0, 100) + '...' : text}"`);
320
336
  }
321
337
 
322
338
  #checkValidStaticCall(childExecutionResult: ExecutionResult) {
@@ -349,7 +365,7 @@ export class ClientExecutionContext extends ViewDataOracle {
349
365
  isStaticCall: boolean,
350
366
  isDelegateCall: boolean,
351
367
  ) {
352
- this.log(
368
+ this.log.debug(
353
369
  `Calling private function ${this.contractAddress}:${functionSelector} from ${this.callContext.storageContractAddress}`,
354
370
  );
355
371
 
@@ -375,7 +391,7 @@ export class ClientExecutionContext extends ViewDataOracle {
375
391
  derivedCallContext,
376
392
  this.historicalHeader,
377
393
  this.authWitnesses,
378
- this.packedArgsCache,
394
+ this.packedValuesCache,
379
395
  this.noteCache,
380
396
  this.db,
381
397
  this.curve,
@@ -428,7 +444,7 @@ export class ClientExecutionContext extends ViewDataOracle {
428
444
  isDelegateCall,
429
445
  isStaticCall,
430
446
  );
431
- const args = this.packedArgsCache.unpack(argsHash);
447
+ const args = this.packedValuesCache.unpack(argsHash);
432
448
  const enqueuedRequest = PublicCallRequest.from({
433
449
  args,
434
450
  callContext: derivedCallContext,
@@ -441,7 +457,7 @@ export class ClientExecutionContext extends ViewDataOracle {
441
457
  // side-effect counter, that will leak info about how many other private
442
458
  // side-effects occurred in the TX. Ultimately the private kernel should
443
459
  // just output everything in the proper order without any counters.
444
- this.log(
460
+ this.log.verbose(
445
461
  `Enqueued call to public function (with side-effect counter #${sideEffectCounter}) ${targetContractAddress}:${functionSelector}(${targetArtifact.name})`,
446
462
  );
447
463
 
@@ -472,9 +488,12 @@ export class ClientExecutionContext extends ViewDataOracle {
472
488
  isDelegateCall ? this.contractAddress : targetContractAddress,
473
489
  portalContractAddress,
474
490
  FunctionSelector.fromNameAndParameters(targetArtifact.name, targetArtifact.parameters),
491
+ this.callContext.gasLeft, // TODO(palla/gas): We should deduct DA and L1 gas used for the derived context
475
492
  isDelegateCall,
476
493
  isStaticCall,
477
494
  startSideEffectCounter,
495
+ this.callContext.gasSettings,
496
+ this.callContext.transactionFee,
478
497
  );
479
498
  }
480
499
 
@@ -497,7 +516,7 @@ export class ClientExecutionContext extends ViewDataOracle {
497
516
  throw new Error(`No witness for slot ${storageSlot.toString()}`);
498
517
  }
499
518
  const value = witness.leafPreimage.value;
500
- this.log(`Oracle storage read: slot=${storageSlot.toString()} value=${value}`);
519
+ this.log.debug(`Oracle storage read: slot=${storageSlot.toString()} value=${value}`);
501
520
  values.push(value);
502
521
  }
503
522
  return values;
@@ -4,7 +4,7 @@ import { type AztecAddress } from '@aztec/foundation/aztec-address';
4
4
  import { Fr } from '@aztec/foundation/fields';
5
5
  import { createDebugLogger } from '@aztec/foundation/log';
6
6
 
7
- import { extractReturnWitness } from '../acvm/deserialize.js';
7
+ import { witnessMapToFields } from '../acvm/deserialize.js';
8
8
  import { Oracle, acvm, extractCallStack } from '../acvm/index.js';
9
9
  import { ExecutionError } from '../common/errors.js';
10
10
  import { type ClientExecutionContext } from './client_execution_context.js';
@@ -22,11 +22,11 @@ export async function executePrivateFunction(
22
22
  log = createDebugLogger('aztec:simulator:secret_execution'),
23
23
  ): Promise<ExecutionResult> {
24
24
  const functionSelector = functionData.selector;
25
- log(`Executing external function ${contractAddress}:${functionSelector}(${artifact.name})`);
25
+ log.verbose(`Executing external function ${contractAddress}:${functionSelector}(${artifact.name})`);
26
26
  const acir = artifact.bytecode;
27
27
  const initialWitness = context.getInitialWitness(artifact);
28
28
  const acvmCallback = new Oracle(context);
29
- const { partialWitness } = await acvm(await AcirSimulator.getSolver(), acir, initialWitness, acvmCallback).catch(
29
+ const acirExecutionResult = await acvm(await AcirSimulator.getSolver(), acir, initialWitness, acvmCallback).catch(
30
30
  (err: Error) => {
31
31
  throw new ExecutionError(
32
32
  err.message,
@@ -39,8 +39,8 @@ export async function executePrivateFunction(
39
39
  );
40
40
  },
41
41
  );
42
-
43
- const returnWitness = extractReturnWitness(acir, partialWitness);
42
+ const partialWitness = acirExecutionResult.partialWitness;
43
+ const returnWitness = witnessMapToFields(acirExecutionResult.returnWitness);
44
44
  const publicInputs = PrivateCircuitPublicInputs.fromFields(returnWitness);
45
45
 
46
46
  const encryptedLogs = context.getEncryptedLogs();
@@ -55,9 +55,10 @@ export async function executePrivateFunction(
55
55
 
56
56
  // Mocking the return type to be an array of 4 fields
57
57
  // TODO: @LHerskind must be updated as we are progressing with the macros to get the information
58
- const returnTypes: AbiType[] = [{ kind: 'array', length: 4, type: { kind: 'field' } }];
58
+ const rawReturnValues = await context.unpackReturns(publicInputs.returnsHash);
59
+ const returnTypes: AbiType[] = [{ kind: 'array', length: rawReturnValues.length, type: { kind: 'field' } }];
59
60
  const mockArtifact = { ...artifact, returnTypes };
60
- const returnValues = decodeReturnValues(mockArtifact, publicInputs.returnValues);
61
+ const returnValues = decodeReturnValues(mockArtifact, rawReturnValues);
61
62
 
62
63
  const noteHashReadRequestPartialWitnesses = context.getNoteHashReadRequestPartialWitnesses(
63
64
  publicInputs.noteHashReadRequests,
@@ -66,7 +67,7 @@ export async function executePrivateFunction(
66
67
  const nestedExecutions = context.getNestedExecutions();
67
68
  const enqueuedPublicFunctionCalls = context.getEnqueuedPublicFunctionCalls();
68
69
 
69
- log(`Returning from call to ${contractAddress.toString()}:${functionSelector}`);
70
+ log.debug(`Returning from call to ${contractAddress.toString()}:${functionSelector}`);
70
71
 
71
72
  return {
72
73
  acir,
@@ -16,7 +16,7 @@ import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';
16
16
  import { type WasmBlackBoxFunctionSolver, createBlackBoxSolver } from '@noir-lang/acvm_js';
17
17
 
18
18
  import { createSimulationError } from '../common/errors.js';
19
- import { PackedArgsCache } from '../common/packed_args_cache.js';
19
+ import { PackedValuesCache } from '../common/packed_values_cache.js';
20
20
  import { ClientExecutionContext } from './client_execution_context.js';
21
21
  import { type DBOracle } from './db_oracle.js';
22
22
  import { ExecutionNoteCache } from './execution_note_cache.js';
@@ -89,14 +89,18 @@ export class AcirSimulator {
89
89
  // reserve the first side effect for the tx hash (inserted by the private kernel)
90
90
  const startSideEffectCounter = 1;
91
91
 
92
+ const transactionFee = Fr.ZERO;
92
93
  const callContext = new CallContext(
93
94
  msgSender,
94
95
  contractAddress,
95
96
  portalContractAddress,
96
97
  FunctionSelector.fromNameAndParameters(entryPointArtifact.name, entryPointArtifact.parameters),
98
+ request.gasSettings.getInitialAvailable(),
97
99
  false,
98
100
  false,
99
101
  startSideEffectCounter,
102
+ request.gasSettings,
103
+ transactionFee,
100
104
  );
101
105
  const context = new ClientExecutionContext(
102
106
  contractAddress,
@@ -105,7 +109,7 @@ export class AcirSimulator {
105
109
  callContext,
106
110
  header,
107
111
  request.authWitnesses,
108
- PackedArgsCache.create(request.packedArguments),
112
+ PackedValuesCache.create(request.packedArguments),
109
113
  new ExecutionNoteCache(),
110
114
  this.db,
111
115
  curve,
@@ -194,7 +198,7 @@ export class AcirSimulator {
194
198
  const maxNoteFields = (artifact.parameters[artifact.parameters.length - 1].type as ArrayType).length;
195
199
  if (maxNoteFields < note.items.length) {
196
200
  throw new Error(
197
- `The note being processed has ${note.items.length} fields, while "compute_note_hash_and_nullifier" can only handle a maximum of ${maxNoteFields} fields. Please consider increasing the allowed field size to accommodate all notes generated from the contract.`,
201
+ `The note being processed has ${note.items.length} fields, while "compute_note_hash_and_nullifier" can only handle a maximum of ${maxNoteFields} fields. Please reduce the number of fields in your note.`,
198
202
  );
199
203
  }
200
204
 
@@ -4,7 +4,7 @@ import { type AztecAddress } from '@aztec/foundation/aztec-address';
4
4
  import { type Fr } from '@aztec/foundation/fields';
5
5
  import { createDebugLogger } from '@aztec/foundation/log';
6
6
 
7
- import { extractReturnWitness } from '../acvm/deserialize.js';
7
+ import { witnessMapToFields } from '../acvm/deserialize.js';
8
8
  import { Oracle, acvm, extractCallStack, toACVMWitness } from '../acvm/index.js';
9
9
  import { ExecutionError } from '../common/errors.js';
10
10
  import { AcirSimulator } from './simulator.js';
@@ -23,11 +23,11 @@ export async function executeUnconstrainedFunction(
23
23
  log = createDebugLogger('aztec:simulator:unconstrained_execution'),
24
24
  ): Promise<DecodedReturn> {
25
25
  const functionSelector = functionData.selector;
26
- log(`Executing unconstrained function ${contractAddress}:${functionSelector}`);
26
+ log.verbose(`Executing unconstrained function ${contractAddress}:${functionSelector}(${artifact.name})`);
27
27
 
28
28
  const acir = artifact.bytecode;
29
29
  const initialWitness = toACVMWitness(0, args);
30
- const { partialWitness } = await acvm(
30
+ const acirExecutionResult = await acvm(
31
31
  await AcirSimulator.getSolver(),
32
32
  acir,
33
33
  initialWitness,
@@ -44,6 +44,7 @@ export async function executeUnconstrainedFunction(
44
44
  );
45
45
  });
46
46
 
47
- return decodeReturnValues(artifact, extractReturnWitness(acir, partialWitness));
47
+ const returnWitness = witnessMapToFields(acirExecutionResult.returnWitness);
48
+ return decodeReturnValues(artifact, returnWitness);
48
49
  }
49
50
  // docs:end:execute_unconstrained_function
@@ -257,7 +257,7 @@ export class ViewDataOracle extends TypedOracle {
257
257
  const storageSlot = new Fr(startStorageSlot.value + i);
258
258
  const value = await this.aztecNode.getPublicStorageAt(this.contractAddress, storageSlot);
259
259
 
260
- this.log(`Oracle storage read: slot=${storageSlot.toString()} value=${value}`);
260
+ this.log.debug(`Oracle storage read: slot=${storageSlot.toString()} value=${value}`);
261
261
  values.push(value);
262
262
  }
263
263
  return values;
@@ -1,3 +1,3 @@
1
- export * from './packed_args_cache.js';
1
+ export * from './packed_values_cache.js';
2
2
  export * from './errors.js';
3
3
  export * from './side_effect_counter.js';
@@ -0,0 +1,55 @@
1
+ import { PackedValues } from '@aztec/circuit-types';
2
+ import { Fr } from '@aztec/circuits.js';
3
+
4
+ /**
5
+ * A cache for packed values (arguments, returns) during transaction execution.
6
+ */
7
+ export class PackedValuesCache {
8
+ private cache: Map<bigint, Fr[]>;
9
+
10
+ constructor(initialArguments: PackedValues[] = []) {
11
+ this.cache = new Map();
12
+ for (const initialArg of initialArguments) {
13
+ this.cache.set(initialArg.hash.toBigInt(), initialArg.values);
14
+ }
15
+ }
16
+
17
+ /**
18
+ * Creates a new packed values cache.
19
+ * @param initialArguments - The initial arguments to add to the cache.
20
+ * @returns The new packed values cache.
21
+ */
22
+ public static create(initialArguments: PackedValues[] = []) {
23
+ return new PackedValuesCache(initialArguments);
24
+ }
25
+
26
+ /**
27
+ * Unpacks packed values.
28
+ * @param hash - The hash of the packed values.
29
+ * @returns The unpacked values.
30
+ */
31
+ public unpack(hash: Fr): Fr[] {
32
+ if (hash.equals(Fr.ZERO)) {
33
+ return [];
34
+ }
35
+ const packedValues = this.cache.get(hash.value);
36
+ if (!packedValues) {
37
+ throw new Error(`Packed values for hash ${hash.toString()} not found in cache`);
38
+ }
39
+ return packedValues;
40
+ }
41
+
42
+ /**
43
+ * Packs values.
44
+ * @param values - The values to pack.
45
+ * @returns The hash of the packed values.
46
+ */
47
+ public pack(values: Fr[]) {
48
+ if (values.length === 0) {
49
+ return Fr.ZERO;
50
+ }
51
+ const packedValues = PackedValues.fromValues(values);
52
+ this.cache.set(packedValues.hash.value, packedValues.values);
53
+ return packedValues.hash;
54
+ }
55
+ }
package/src/index.ts CHANGED
@@ -3,3 +3,4 @@ export * from './client/index.js';
3
3
  export * from './common/index.js';
4
4
  export * from './public/index.js';
5
5
  export * from './simulator/index.js';
6
+ export * from './mocks/index.js';
@@ -0,0 +1,169 @@
1
+ import { type FunctionCall, type SimulationError, UnencryptedFunctionL2Logs } from '@aztec/circuit-types';
2
+ import {
3
+ ARGS_LENGTH,
4
+ type AztecAddress,
5
+ CallContext,
6
+ CallRequest,
7
+ type ContractStorageUpdateRequest,
8
+ EthAddress,
9
+ Fr,
10
+ FunctionData,
11
+ Gas,
12
+ GasSettings,
13
+ MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX,
14
+ type PrivateKernelTailCircuitPublicInputs,
15
+ type PublicCallRequest,
16
+ } from '@aztec/circuits.js';
17
+ import { makeAztecAddress, makeSelector } from '@aztec/circuits.js/testing';
18
+ import { padArrayEnd } from '@aztec/foundation/collection';
19
+
20
+ import { type PublicExecution, type PublicExecutionResult } from '../public/execution.js';
21
+
22
+ export class PublicExecutionResultBuilder {
23
+ private _execution: PublicExecution;
24
+ private _nestedExecutions: PublicExecutionResult[] = [];
25
+ private _contractStorageUpdateRequests: ContractStorageUpdateRequest[] = [];
26
+ private _returnValues: Fr[] = [];
27
+ private _reverted = false;
28
+ private _revertReason: SimulationError | undefined = undefined;
29
+
30
+ constructor(execution: PublicExecution) {
31
+ this._execution = execution;
32
+ }
33
+
34
+ static fromPublicCallRequest({
35
+ request,
36
+ returnValues = [new Fr(1n)],
37
+ nestedExecutions = [],
38
+ contractStorageUpdateRequests = [],
39
+ }: {
40
+ request: PublicCallRequest;
41
+ returnValues?: Fr[];
42
+ nestedExecutions?: PublicExecutionResult[];
43
+ contractStorageUpdateRequests?: ContractStorageUpdateRequest[];
44
+ }): PublicExecutionResultBuilder {
45
+ const builder = new PublicExecutionResultBuilder(request);
46
+
47
+ builder.withNestedExecutions(...nestedExecutions);
48
+ builder.withContractStorageUpdateRequest(...contractStorageUpdateRequests);
49
+ builder.withReturnValues(...returnValues);
50
+
51
+ return builder;
52
+ }
53
+
54
+ static fromFunctionCall({
55
+ from,
56
+ tx,
57
+ returnValues = [new Fr(1n)],
58
+ nestedExecutions = [],
59
+ contractStorageUpdateRequests = [],
60
+ revertReason,
61
+ }: {
62
+ from: AztecAddress;
63
+ tx: FunctionCall;
64
+ returnValues?: Fr[];
65
+ nestedExecutions?: PublicExecutionResult[];
66
+ contractStorageUpdateRequests?: ContractStorageUpdateRequest[];
67
+ revertReason?: SimulationError;
68
+ }) {
69
+ const builder = new PublicExecutionResultBuilder({
70
+ callContext: new CallContext(
71
+ from,
72
+ tx.to,
73
+ EthAddress.ZERO,
74
+ tx.functionData.selector,
75
+ Gas.test(),
76
+ false,
77
+ false,
78
+ 0,
79
+ GasSettings.default(),
80
+ Fr.ZERO,
81
+ ),
82
+ contractAddress: tx.to,
83
+ functionData: tx.functionData,
84
+ args: tx.args,
85
+ });
86
+
87
+ builder.withNestedExecutions(...nestedExecutions);
88
+ builder.withContractStorageUpdateRequest(...contractStorageUpdateRequests);
89
+ builder.withReturnValues(...returnValues);
90
+ if (revertReason) {
91
+ builder.withReverted(revertReason);
92
+ }
93
+
94
+ return builder;
95
+ }
96
+
97
+ withNestedExecutions(...nested: PublicExecutionResult[]): PublicExecutionResultBuilder {
98
+ this._nestedExecutions.push(...nested);
99
+ return this;
100
+ }
101
+
102
+ withContractStorageUpdateRequest(...request: ContractStorageUpdateRequest[]): PublicExecutionResultBuilder {
103
+ this._contractStorageUpdateRequests.push(...request);
104
+ return this;
105
+ }
106
+
107
+ withReturnValues(...values: Fr[]): PublicExecutionResultBuilder {
108
+ this._returnValues.push(...values);
109
+ return this;
110
+ }
111
+
112
+ withReverted(reason: SimulationError): PublicExecutionResultBuilder {
113
+ this._reverted = true;
114
+ this._revertReason = reason;
115
+ return this;
116
+ }
117
+
118
+ build(): PublicExecutionResult {
119
+ return {
120
+ execution: this._execution,
121
+ nestedExecutions: this._nestedExecutions,
122
+ nullifierReadRequests: [],
123
+ nullifierNonExistentReadRequests: [],
124
+ contractStorageUpdateRequests: this._contractStorageUpdateRequests,
125
+ returnValues: padArrayEnd(this._returnValues, Fr.ZERO, 4), // TODO(#5450) Need to use the proper return values here
126
+ newNoteHashes: [],
127
+ newNullifiers: [],
128
+ newL2ToL1Messages: [],
129
+ contractStorageReads: [],
130
+ unencryptedLogs: UnencryptedFunctionL2Logs.empty(),
131
+ startSideEffectCounter: Fr.ZERO,
132
+ endSideEffectCounter: Fr.ZERO,
133
+ reverted: this._reverted,
134
+ revertReason: this._revertReason,
135
+ gasLeft: this._execution.callContext.gasLeft.mul(0.9),
136
+ };
137
+ }
138
+ }
139
+
140
+ export const makeFunctionCall = (
141
+ to = makeAztecAddress(30),
142
+ selector = makeSelector(5),
143
+ args = new Array(ARGS_LENGTH).fill(Fr.ZERO),
144
+ ) => ({ to, functionData: new FunctionData(selector, false), args });
145
+
146
+ export function addKernelPublicCallStack(
147
+ kernelOutput: PrivateKernelTailCircuitPublicInputs,
148
+ calls: {
149
+ setupCalls: PublicCallRequest[];
150
+ appLogicCalls: PublicCallRequest[];
151
+ teardownCall: PublicCallRequest;
152
+ },
153
+ ) {
154
+ // the first two calls are non-revertible
155
+ // the first is for setup, the second is for teardown
156
+ kernelOutput.forPublic!.endNonRevertibleData.publicCallStack = padArrayEnd(
157
+ // this is a stack, so the first item is the last call
158
+ // and callRequests is in the order of the calls
159
+ [calls.teardownCall.toCallRequest(), ...calls.setupCalls.map(c => c.toCallRequest())],
160
+ CallRequest.empty(),
161
+ MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX,
162
+ );
163
+
164
+ kernelOutput.forPublic!.end.publicCallStack = padArrayEnd(
165
+ calls.appLogicCalls.map(c => c.toCallRequest()),
166
+ CallRequest.empty(),
167
+ MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX,
168
+ );
169
+ }
@@ -0,0 +1 @@
1
+ export * from './fixtures.js';