@aztec/simulator 0.41.0 → 0.42.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 (204) hide show
  1. package/dest/acvm/acvm.d.ts +2 -2
  2. package/dest/acvm/acvm.d.ts.map +1 -1
  3. package/dest/acvm/acvm.js +3 -3
  4. package/dest/acvm/oracle/oracle.d.ts +6 -4
  5. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  6. package/dest/acvm/oracle/oracle.js +20 -11
  7. package/dest/acvm/oracle/typed_oracle.d.ts +5 -3
  8. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  9. package/dest/acvm/oracle/typed_oracle.js +10 -4
  10. package/dest/acvm/serialize.js +2 -2
  11. package/dest/avm/avm_gas.d.ts +1 -5
  12. package/dest/avm/avm_gas.d.ts.map +1 -1
  13. package/dest/avm/avm_gas.js +67 -73
  14. package/dest/avm/avm_simulator.d.ts +5 -0
  15. package/dest/avm/avm_simulator.d.ts.map +1 -1
  16. package/dest/avm/avm_simulator.js +8 -1
  17. package/dest/avm/fixtures/index.d.ts +3 -0
  18. package/dest/avm/fixtures/index.d.ts.map +1 -1
  19. package/dest/avm/fixtures/index.js +13 -1
  20. package/dest/avm/index.d.ts +2 -0
  21. package/dest/avm/index.d.ts.map +1 -0
  22. package/dest/avm/index.js +2 -0
  23. package/dest/avm/journal/host_storage.d.ts +1 -1
  24. package/dest/avm/journal/host_storage.d.ts.map +1 -1
  25. package/dest/avm/journal/journal.d.ts +2 -0
  26. package/dest/avm/journal/journal.d.ts.map +1 -1
  27. package/dest/avm/journal/journal.js +6 -2
  28. package/dest/avm/journal/trace.d.ts +2 -0
  29. package/dest/avm/journal/trace.d.ts.map +1 -1
  30. package/dest/avm/journal/trace.js +7 -1
  31. package/dest/avm/journal/trace_types.d.ts +1 -0
  32. package/dest/avm/journal/trace_types.d.ts.map +1 -1
  33. package/dest/avm/journal/trace_types.js +1 -1
  34. package/dest/avm/opcodes/arithmetic.d.ts +1 -7
  35. package/dest/avm/opcodes/arithmetic.d.ts.map +1 -1
  36. package/dest/avm/opcodes/arithmetic.js +1 -12
  37. package/dest/avm/opcodes/conversion.js +2 -2
  38. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  39. package/dest/avm/opcodes/external_calls.js +4 -3
  40. package/dest/avm/opcodes/instruction.d.ts +1 -1
  41. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  42. package/dest/avm/opcodes/instruction.js +6 -4
  43. package/dest/avm/opcodes/memory.d.ts +0 -4
  44. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  45. package/dest/avm/opcodes/memory.js +1 -7
  46. package/dest/avm/opcodes/storage.d.ts +0 -5
  47. package/dest/avm/opcodes/storage.d.ts.map +1 -1
  48. package/dest/avm/opcodes/storage.js +1 -7
  49. package/dest/avm/serialization/bytecode_serialization.js +2 -2
  50. package/dest/client/client_execution_context.d.ts +11 -19
  51. package/dest/client/client_execution_context.d.ts.map +1 -1
  52. package/dest/client/client_execution_context.js +26 -43
  53. package/dest/client/db_oracle.d.ts +7 -1
  54. package/dest/client/db_oracle.d.ts.map +1 -1
  55. package/dest/client/execution_note_cache.d.ts +0 -17
  56. package/dest/client/execution_note_cache.d.ts.map +1 -1
  57. package/dest/client/execution_note_cache.js +1 -24
  58. package/dest/client/execution_result.d.ts +8 -4
  59. package/dest/client/execution_result.d.ts.map +1 -1
  60. package/dest/client/execution_result.js +16 -6
  61. package/dest/client/private_execution.d.ts +2 -3
  62. package/dest/client/private_execution.d.ts.map +1 -1
  63. package/dest/client/private_execution.js +20 -9
  64. package/dest/client/simulator.d.ts +0 -16
  65. package/dest/client/simulator.d.ts.map +1 -1
  66. package/dest/client/simulator.js +8 -25
  67. package/dest/client/unconstrained_execution.d.ts +2 -3
  68. package/dest/client/unconstrained_execution.d.ts.map +1 -1
  69. package/dest/client/unconstrained_execution.js +5 -7
  70. package/dest/client/view_data_oracle.d.ts +2 -0
  71. package/dest/client/view_data_oracle.d.ts.map +1 -1
  72. package/dest/client/view_data_oracle.js +7 -1
  73. package/dest/index.d.ts +3 -1
  74. package/dest/index.d.ts.map +1 -1
  75. package/dest/index.js +4 -2
  76. package/dest/mocks/fixtures.d.ts +14 -10
  77. package/dest/mocks/fixtures.d.ts.map +1 -1
  78. package/dest/mocks/fixtures.js +22 -16
  79. package/dest/{simulator → providers}/acvm_native.d.ts.map +1 -1
  80. package/dest/{simulator → providers}/acvm_native.js +13 -14
  81. package/dest/providers/acvm_wasm.d.ts.map +1 -0
  82. package/dest/providers/acvm_wasm.js +15 -0
  83. package/dest/providers/index.d.ts.map +1 -0
  84. package/dest/{simulator → providers}/index.js +1 -1
  85. package/dest/{simulator → providers}/simulation_provider.d.ts.map +1 -1
  86. package/dest/{simulator → providers}/simulation_provider.js +1 -1
  87. package/dest/public/abstract_phase_manager.d.ts +43 -42
  88. package/dest/public/abstract_phase_manager.d.ts.map +1 -1
  89. package/dest/public/abstract_phase_manager.js +77 -106
  90. package/dest/public/app_logic_phase_manager.d.ts +6 -7
  91. package/dest/public/app_logic_phase_manager.d.ts.map +1 -1
  92. package/dest/public/app_logic_phase_manager.js +14 -14
  93. package/dest/public/{db.d.ts → db_interfaces.d.ts} +3 -3
  94. package/dest/public/db_interfaces.d.ts.map +1 -0
  95. package/dest/public/db_interfaces.js +2 -0
  96. package/dest/public/execution.d.ts +16 -9
  97. package/dest/public/execution.d.ts.map +1 -1
  98. package/dest/public/execution.js +1 -1
  99. package/dest/public/executor.d.ts +3 -30
  100. package/dest/public/executor.d.ts.map +1 -1
  101. package/dest/public/executor.js +32 -282
  102. package/dest/public/fee_payment.d.ts +11 -0
  103. package/dest/public/fee_payment.d.ts.map +1 -0
  104. package/dest/public/fee_payment.js +24 -0
  105. package/dest/public/hints_builder.d.ts +1 -0
  106. package/dest/public/hints_builder.d.ts.map +1 -1
  107. package/dest/public/hints_builder.js +6 -2
  108. package/dest/public/index.d.ts +4 -2
  109. package/dest/public/index.d.ts.map +1 -1
  110. package/dest/public/index.js +5 -3
  111. package/dest/public/phase_manager_factory.d.ts +1 -1
  112. package/dest/public/phase_manager_factory.d.ts.map +1 -1
  113. package/dest/public/{public_executor.d.ts → public_db_sources.d.ts} +3 -2
  114. package/dest/public/public_db_sources.d.ts.map +1 -0
  115. package/dest/public/public_db_sources.js +198 -0
  116. package/dest/public/public_kernel.js +14 -14
  117. package/dest/public/public_processor.d.ts +6 -4
  118. package/dest/public/public_processor.d.ts.map +1 -1
  119. package/dest/public/public_processor.js +46 -27
  120. package/dest/public/setup_phase_manager.d.ts +6 -8
  121. package/dest/public/setup_phase_manager.d.ts.map +1 -1
  122. package/dest/public/setup_phase_manager.js +11 -20
  123. package/dest/public/tail_phase_manager.d.ts +4 -10
  124. package/dest/public/tail_phase_manager.d.ts.map +1 -1
  125. package/dest/public/tail_phase_manager.js +15 -35
  126. package/dest/public/teardown_phase_manager.d.ts +6 -8
  127. package/dest/public/teardown_phase_manager.d.ts.map +1 -1
  128. package/dest/public/teardown_phase_manager.js +15 -21
  129. package/dest/public/transitional_adaptors.d.ts +1 -1
  130. package/dest/public/transitional_adaptors.d.ts.map +1 -1
  131. package/dest/public/transitional_adaptors.js +8 -6
  132. package/dest/public/utils.d.ts +2 -2
  133. package/dest/public/utils.d.ts.map +1 -1
  134. package/dest/public/utils.js +21 -14
  135. package/package.json +12 -9
  136. package/src/acvm/acvm.ts +0 -3
  137. package/src/acvm/oracle/oracle.ts +36 -13
  138. package/src/acvm/oracle/typed_oracle.ts +12 -3
  139. package/src/acvm/serialize.ts +1 -1
  140. package/src/avm/avm_gas.ts +66 -73
  141. package/src/avm/avm_simulator.ts +9 -0
  142. package/src/avm/fixtures/index.ts +20 -0
  143. package/src/avm/index.ts +1 -0
  144. package/src/avm/journal/host_storage.ts +1 -1
  145. package/src/avm/journal/journal.ts +6 -1
  146. package/src/avm/journal/trace.ts +12 -0
  147. package/src/avm/journal/trace_types.ts +1 -0
  148. package/src/avm/opcodes/arithmetic.ts +1 -14
  149. package/src/avm/opcodes/conversion.ts +1 -1
  150. package/src/avm/opcodes/external_calls.ts +3 -1
  151. package/src/avm/opcodes/instruction.ts +5 -3
  152. package/src/avm/opcodes/memory.ts +1 -8
  153. package/src/avm/opcodes/storage.ts +1 -8
  154. package/src/avm/serialization/bytecode_serialization.ts +1 -1
  155. package/src/client/client_execution_context.ts +33 -45
  156. package/src/client/db_oracle.ts +8 -1
  157. package/src/client/execution_note_cache.ts +0 -28
  158. package/src/client/execution_result.ts +21 -7
  159. package/src/client/private_execution.ts +36 -21
  160. package/src/client/simulator.ts +7 -27
  161. package/src/client/unconstrained_execution.ts +9 -12
  162. package/src/client/view_data_oracle.ts +8 -0
  163. package/src/index.ts +3 -1
  164. package/src/mocks/fixtures.ts +30 -32
  165. package/src/{simulator → providers}/acvm_native.ts +21 -19
  166. package/src/{simulator → providers}/acvm_wasm.ts +2 -16
  167. package/src/public/abstract_phase_manager.ts +138 -205
  168. package/src/public/app_logic_phase_manager.ts +23 -39
  169. package/src/public/{db.ts → db_interfaces.ts} +2 -2
  170. package/src/public/execution.ts +15 -8
  171. package/src/public/executor.ts +51 -398
  172. package/src/public/fee_payment.ts +26 -0
  173. package/src/public/hints_builder.ts +6 -0
  174. package/src/public/index.ts +4 -2
  175. package/src/public/phase_manager_factory.ts +1 -1
  176. package/src/public/{public_executor.ts → public_db_sources.ts} +3 -2
  177. package/src/public/public_kernel.ts +24 -24
  178. package/src/public/public_processor.ts +77 -33
  179. package/src/public/setup_phase_manager.ts +20 -45
  180. package/src/public/tail_phase_manager.ts +17 -65
  181. package/src/public/teardown_phase_manager.ts +22 -41
  182. package/src/public/transitional_adaptors.ts +7 -11
  183. package/src/public/utils.ts +20 -14
  184. package/dest/public/db.d.ts.map +0 -1
  185. package/dest/public/db.js +0 -2
  186. package/dest/public/public_execution_context.d.ts +0 -121
  187. package/dest/public/public_execution_context.d.ts.map +0 -1
  188. package/dest/public/public_execution_context.js +0 -214
  189. package/dest/public/public_executor.d.ts.map +0 -1
  190. package/dest/public/public_executor.js +0 -197
  191. package/dest/public/state_actions.d.ts +0 -39
  192. package/dest/public/state_actions.d.ts.map +0 -1
  193. package/dest/public/state_actions.js +0 -80
  194. package/dest/simulator/acvm_wasm.d.ts.map +0 -1
  195. package/dest/simulator/acvm_wasm.js +0 -22
  196. package/dest/simulator/index.d.ts.map +0 -1
  197. package/src/public/public_execution_context.ts +0 -289
  198. package/src/public/state_actions.ts +0 -102
  199. /package/dest/{simulator → providers}/acvm_native.d.ts +0 -0
  200. /package/dest/{simulator → providers}/acvm_wasm.d.ts +0 -0
  201. /package/dest/{simulator → providers}/index.d.ts +0 -0
  202. /package/dest/{simulator → providers}/simulation_provider.d.ts +0 -0
  203. /package/src/{simulator → providers}/index.ts +0 -0
  204. /package/src/{simulator → providers}/simulation_provider.ts +0 -0
@@ -1,6 +1,5 @@
1
1
  import type { AvmContext } from '../avm_context.js';
2
- import { getBaseGasCost, getGasCostForTypeTag, getMemoryGasCost, sumGas } from '../avm_gas.js';
3
- import { type Field, type MemoryOperations, type MemoryValue, TypeTag } from '../avm_memory_types.js';
2
+ import { type Field, type MemoryValue, TypeTag } from '../avm_memory_types.js';
4
3
  import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
5
4
  import { Instruction } from './instruction.js';
6
5
  import { ThreeOperandInstruction } from './instruction_impl.js';
@@ -23,12 +22,6 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst
23
22
  context.machineState.incrementPc();
24
23
  }
25
24
 
26
- protected override gasCost(memoryOps: Partial<MemoryOperations & { indirect: number }>) {
27
- const baseGasCost = getGasCostForTypeTag(this.inTag, getBaseGasCost(this.opcode));
28
- const memoryGasCost = getMemoryGasCost(memoryOps);
29
- return sumGas(baseGasCost, memoryGasCost);
30
- }
31
-
32
25
  protected abstract compute(a: MemoryValue, b: MemoryValue): MemoryValue;
33
26
  }
34
27
 
@@ -101,10 +94,4 @@ export class FieldDiv extends Instruction {
101
94
  memory.assert(memoryOperations);
102
95
  context.machineState.incrementPc();
103
96
  }
104
-
105
- protected override gasCost(memoryOps: Partial<MemoryOperations & { indirect: number }>) {
106
- const baseGasCost = getGasCostForTypeTag(TypeTag.FIELD, getBaseGasCost(this.opcode));
107
- const memoryGasCost = getMemoryGasCost(memoryOps);
108
- return sumGas(baseGasCost, memoryGasCost);
109
- }
110
97
  }
@@ -50,7 +50,7 @@ export class ToRadixLE extends Instruction {
50
50
  value /= radixBN;
51
51
  }
52
52
 
53
- const res = [...limbArray].map(byte => new Uint8(byte));
53
+ const res = limbArray.map(byte => new Uint8(byte));
54
54
  memory.setSlice(dstOffset, res);
55
55
 
56
56
  memory.assert(memoryOperations);
@@ -86,13 +86,15 @@ abstract class ExternalCall extends Instruction {
86
86
  const startSideEffectCounter = nestedContext.persistableState.trace.accessCounter;
87
87
 
88
88
  const oldStyleExecution = createPublicExecution(startSideEffectCounter, nestedContext.environment, calldata);
89
- const nestedCallResults: AvmContractCallResults = await new AvmSimulator(nestedContext).execute();
89
+ const simulator = new AvmSimulator(nestedContext);
90
+ const nestedCallResults: AvmContractCallResults = await simulator.execute();
90
91
  const pxResults = convertAvmResultsToPxResult(
91
92
  nestedCallResults,
92
93
  startSideEffectCounter,
93
94
  oldStyleExecution,
94
95
  Gas.from(allocatedGas),
95
96
  nestedContext,
97
+ simulator.getBytecode(),
96
98
  );
97
99
  // store the old PublicExecutionResult object to maintain a recursive data structure for the old kernel
98
100
  context.persistableState.transitionalExecutionResult.nestedExecutions.push(pxResults);
@@ -1,7 +1,7 @@
1
1
  import { strict as assert } from 'assert';
2
2
 
3
3
  import type { AvmContext } from '../avm_context.js';
4
- import { getBaseGasCost, getMemoryGasCost, sumGas } from '../avm_gas.js';
4
+ import { getBaseGasCost, sumGas } from '../avm_gas.js';
5
5
  import { type MemoryOperations } from '../avm_memory_types.js';
6
6
  import { type BufferCursor } from '../serialization/buffer_cursor.js';
7
7
  import { Opcode, type OperandType, deserialize, serialize } from '../serialization/instruction_serialization.js';
@@ -67,9 +67,11 @@ export abstract class Instruction {
67
67
  * @param memoryOps Memory operations performed by the instruction.
68
68
  * @returns Gas cost.
69
69
  */
70
- protected gasCost(memoryOps: Partial<MemoryOperations & { indirect: number }> = {}) {
70
+ protected gasCost(_memoryOps: Partial<MemoryOperations & { indirect: number }> = {}) {
71
71
  const baseGasCost = getBaseGasCost(this.opcode);
72
- const memoryGasCost = getMemoryGasCost(memoryOps);
72
+ // TODO(https://github.com/AztecProtocol/aztec-packages/issues/6861): reconsider.
73
+ // const memoryGasCost = getMemoryGasCost(memoryOps);
74
+ const memoryGasCost = { l2Gas: 0, daGas: 0 };
73
75
  return sumGas(baseGasCost, memoryGasCost);
74
76
  }
75
77
 
@@ -1,6 +1,5 @@
1
1
  import type { AvmContext } from '../avm_context.js';
2
- import { getBaseGasCost, getMemoryGasCost, sumGas } from '../avm_gas.js';
3
- import { Field, type MemoryOperations, TaggedMemory, TypeTag } from '../avm_memory_types.js';
2
+ import { Field, TaggedMemory, TypeTag } from '../avm_memory_types.js';
4
3
  import { InstructionExecutionError } from '../errors.js';
5
4
  import { BufferCursor } from '../serialization/buffer_cursor.js';
6
5
  import { Opcode, OperandType, deserialize, serialize } from '../serialization/instruction_serialization.js';
@@ -216,10 +215,4 @@ export class CalldataCopy extends Instruction {
216
215
  memory.assert(memoryOperations);
217
216
  context.machineState.incrementPc();
218
217
  }
219
-
220
- protected override gasCost(memoryOps: Partial<MemoryOperations & { indirect: number }> = {}) {
221
- const baseGasCost = getBaseGasCost(this.opcode);
222
- const memoryGasCost = getMemoryGasCost(memoryOps);
223
- return sumGas(baseGasCost, memoryGasCost);
224
- }
225
218
  }
@@ -1,8 +1,7 @@
1
1
  import { Fr } from '@aztec/foundation/fields';
2
2
 
3
3
  import type { AvmContext } from '../avm_context.js';
4
- import { type Gas, getBaseGasCost, getMemoryGasCost, mulGas, sumGas } from '../avm_gas.js';
5
- import { Field, type MemoryOperations } from '../avm_memory_types.js';
4
+ import { Field } from '../avm_memory_types.js';
6
5
  import { StaticCallAlterationError } from '../errors.js';
7
6
  import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
8
7
  import { Addressing } from './addressing_mode.js';
@@ -26,12 +25,6 @@ abstract class BaseStorageInstruction extends Instruction {
26
25
  ) {
27
26
  super();
28
27
  }
29
-
30
- protected override gasCost(memoryOps: Partial<MemoryOperations & { indirect: number }>): Gas {
31
- const baseGasCost = mulGas(getBaseGasCost(this.opcode), this.size);
32
- const memoryGasCost = getMemoryGasCost(memoryOps);
33
- return sumGas(baseGasCost, memoryGasCost);
34
- }
35
28
  }
36
29
 
37
30
  export class SStore extends BaseStorageInstruction {
@@ -136,7 +136,7 @@ const INSTRUCTION_SET = () =>
136
136
  // Misc
137
137
  [DebugLog.opcode, DebugLog],
138
138
 
139
- // //// Gadgets
139
+ // Gadgets
140
140
  [Keccak.opcode, Keccak],
141
141
  [Poseidon2.opcode, Poseidon2],
142
142
  [Sha256.opcode, Sha256],
@@ -2,6 +2,7 @@ import {
2
2
  type AuthWitness,
3
3
  type AztecNode,
4
4
  EncryptedL2Log,
5
+ EncryptedL2NoteLog,
5
6
  L1NotePayload,
6
7
  Note,
7
8
  type NoteStatus,
@@ -10,9 +11,9 @@ import {
10
11
  } from '@aztec/circuit-types';
11
12
  import {
12
13
  CallContext,
13
- FunctionData,
14
14
  FunctionSelector,
15
15
  type Header,
16
+ type KeyValidationRequest,
16
17
  PrivateContextInputs,
17
18
  PublicCallRequest,
18
19
  type TxContext,
@@ -21,6 +22,7 @@ import { Aes128 } from '@aztec/circuits.js/barretenberg';
21
22
  import { computePublicDataTreeLeafSlot, computeUniqueNoteHash, siloNoteHash } from '@aztec/circuits.js/hash';
22
23
  import { type FunctionAbi, type FunctionArtifact, countArgumentsSize } from '@aztec/foundation/abi';
23
24
  import { AztecAddress } from '@aztec/foundation/aztec-address';
25
+ import { pedersenHash } from '@aztec/foundation/crypto';
24
26
  import { Fr, GrumpkinScalar, type Point } from '@aztec/foundation/fields';
25
27
  import { applyStringFormatting, createDebugLogger } from '@aztec/foundation/log';
26
28
 
@@ -28,7 +30,7 @@ import { type NoteData, toACVMWitness } from '../acvm/index.js';
28
30
  import { type PackedValuesCache } from '../common/packed_values_cache.js';
29
31
  import { type DBOracle } from './db_oracle.js';
30
32
  import { type ExecutionNoteCache } from './execution_note_cache.js';
31
- import { CountedLog, type ExecutionResult, type NoteAndSlot } from './execution_result.js';
33
+ import { CountedLog, CountedNoteLog, type ExecutionResult, type NoteAndSlot } from './execution_result.js';
32
34
  import { pickNotes } from './pick_notes.js';
33
35
  import { executePrivateFunction } from './private_execution.js';
34
36
  import { ViewDataOracle } from './view_data_oracle.js';
@@ -56,7 +58,7 @@ export class ClientExecutionContext extends ViewDataOracle {
56
58
  */
57
59
  private noteHashLeafIndexMap: Map<bigint, bigint> = new Map();
58
60
  private nullifiedNoteHashCounters: Map<number, number> = new Map();
59
- private noteEncryptedLogs: CountedLog<EncryptedL2Log>[] = [];
61
+ private noteEncryptedLogs: CountedNoteLog[] = [];
60
62
  private encryptedLogs: CountedLog<EncryptedL2Log>[] = [];
61
63
  private unencryptedLogs: CountedLog<UnencryptedL2Log>[] = [];
62
64
  private nestedExecutions: ExecutionResult[] = [];
@@ -136,33 +138,6 @@ export class ClientExecutionContext extends ViewDataOracle {
136
138
  return this.noteEncryptedLogs;
137
139
  }
138
140
 
139
- /**
140
- * Sometimes notes can be chopped after a nested execution is complete.
141
- * This means finished nested executions still hold transient logs. This method removes them.
142
- * TODO(Miranda): is there a cleaner solution?
143
- */
144
- public chopNoteEncryptedLogs() {
145
- // Do not return logs that have been chopped in the cache
146
- const allNoteLogs = this.noteCache.getLogs();
147
- this.noteEncryptedLogs = this.noteEncryptedLogs.filter(l => allNoteLogs.includes(l));
148
- const chop = (thing: any) =>
149
- thing.nestedExecutions.forEach((result: ExecutionResult) => {
150
- if (!result.noteEncryptedLogs[0]?.isEmpty()) {
151
- // The execution has note logs
152
- result.noteEncryptedLogs = result.noteEncryptedLogs.filter(l => allNoteLogs.includes(l));
153
- }
154
- chop(result);
155
- });
156
- chop(this);
157
- }
158
-
159
- /**
160
- * Return the note encrypted logs emitted during this execution and nested executions.
161
- */
162
- public getAllNoteEncryptedLogs() {
163
- return this.noteCache.getLogs();
164
- }
165
-
166
141
  /**
167
142
  * Return the encrypted logs emitted during this execution.
168
143
  */
@@ -358,21 +333,31 @@ export class ClientExecutionContext extends ViewDataOracle {
358
333
  * @param encryptedNote - The encrypted data.
359
334
  * @param counter - The effects counter.
360
335
  */
361
- public override emitEncryptedLog(encryptedData: Buffer, counter: number) {
362
- const encryptedLog = new CountedLog(new EncryptedL2Log(encryptedData), counter);
336
+ public override emitEncryptedLog(
337
+ contractAddress: AztecAddress,
338
+ randomness: Fr,
339
+ encryptedData: Buffer,
340
+ counter: number,
341
+ ) {
342
+ // In some cases, we actually want to reveal the contract address we are siloing with:
343
+ // e.g. 'handshaking' contract w/ known address
344
+ // An app providing randomness = 0 signals to not mask the address.
345
+ const maskedContractAddress = randomness.isZero()
346
+ ? contractAddress.toField()
347
+ : pedersenHash([contractAddress, randomness], 0);
348
+ const encryptedLog = new CountedLog(new EncryptedL2Log(encryptedData, maskedContractAddress), counter);
363
349
  this.encryptedLogs.push(encryptedLog);
364
350
  }
365
351
 
366
352
  /**
367
353
  * Emit encrypted note data
368
- * @param noteHash - The note hash.
354
+ * @param noteHashCounter - The note hash counter.
369
355
  * @param encryptedNote - The encrypted note data.
370
- * @param counter - The effects counter.
356
+ * @param counter - The log counter.
371
357
  */
372
- public override emitEncryptedNoteLog(noteHash: Fr, encryptedNote: Buffer, counter: number) {
373
- const encryptedLog = new CountedLog(new EncryptedL2Log(encryptedNote), counter);
358
+ public override emitEncryptedNoteLog(noteHashCounter: number, encryptedNote: Buffer, counter: number) {
359
+ const encryptedLog = new CountedNoteLog(new EncryptedL2NoteLog(encryptedNote), counter, noteHashCounter);
374
360
  this.noteEncryptedLogs.push(encryptedLog);
375
- this.noteCache.addNewLog(encryptedLog, noteHash);
376
361
  }
377
362
 
378
363
  /**
@@ -380,14 +365,16 @@ export class ClientExecutionContext extends ViewDataOracle {
380
365
  * @param contractAddress - The contract address of the note.
381
366
  * @param storageSlot - The storage slot the note is at.
382
367
  * @param noteTypeId - The type ID of the note.
383
- * @param ivpk - The master incoming viewing public key.
368
+ * @param ovKeys - The outgoing viewing keys to use to encrypt.
369
+ * @param ivpkM - The master incoming viewing public key.
384
370
  * @param preimage - The note preimage.
385
371
  */
386
372
  public override computeEncryptedLog(
387
373
  contractAddress: AztecAddress,
388
374
  storageSlot: Fr,
389
375
  noteTypeId: Fr,
390
- ivpk: Point,
376
+ ovKeys: KeyValidationRequest,
377
+ ivpkM: Point,
391
378
  preimage: Fr[],
392
379
  ) {
393
380
  const note = new Note(preimage);
@@ -396,11 +383,9 @@ export class ClientExecutionContext extends ViewDataOracle {
396
383
 
397
384
  const ephSk = GrumpkinScalar.random();
398
385
 
399
- // @todo Issue(#6410) Right now we are completely ignoring the outgoing log. Just drawing random data.
400
- const ovsk = GrumpkinScalar.random();
401
386
  const recipient = AztecAddress.random();
402
387
 
403
- return taggedNote.encrypt(ephSk, recipient, ivpk, ovsk);
388
+ return taggedNote.encrypt(ephSk, recipient, ivpkM, ovKeys);
404
389
  }
405
390
 
406
391
  /**
@@ -468,7 +453,6 @@ export class ClientExecutionContext extends ViewDataOracle {
468
453
  isStaticCall = isStaticCall || this.callContext.isStaticCall;
469
454
 
470
455
  const targetArtifact = await this.db.getFunctionArtifact(targetContractAddress, functionSelector);
471
- const targetFunctionData = FunctionData.fromAbi(targetArtifact);
472
456
 
473
457
  const derivedTxContext = this.txContext.clone();
474
458
 
@@ -498,7 +482,7 @@ export class ClientExecutionContext extends ViewDataOracle {
498
482
  context,
499
483
  targetArtifact,
500
484
  targetContractAddress,
501
- targetFunctionData,
485
+ functionSelector,
502
486
  );
503
487
 
504
488
  if (isStaticCall) {
@@ -552,7 +536,7 @@ export class ClientExecutionContext extends ViewDataOracle {
552
536
  args,
553
537
  callContext: derivedCallContext,
554
538
  parentCallContext: this.callContext,
555
- functionData: FunctionData.fromAbi(targetArtifact),
539
+ functionSelector,
556
540
  contractAddress: targetContractAddress,
557
541
  });
558
542
  }
@@ -684,4 +668,8 @@ export class ClientExecutionContext extends ViewDataOracle {
684
668
  public override debugLog(message: string, fields: Fr[]) {
685
669
  this.log.verbose(`debug_log ${applyStringFormatting(message, fields)}`);
686
670
  }
671
+
672
+ public getDebugFunctionName() {
673
+ return this.db.getDebugFunctionName(this.contractAddress, this.callContext.functionSelector);
674
+ }
687
675
  }
@@ -12,7 +12,7 @@ import { type Fr } from '@aztec/foundation/fields';
12
12
  import { type ContractInstance } from '@aztec/types/contracts';
13
13
 
14
14
  import { type NoteData } from '../acvm/index.js';
15
- import { type CommitmentsDB } from '../public/db.js';
15
+ import { type CommitmentsDB } from '../public/db_interfaces.js';
16
16
 
17
17
  /**
18
18
  * Error thrown when a contract is not found in the database.
@@ -95,6 +95,13 @@ export interface DBOracle extends CommitmentsDB {
95
95
  */
96
96
  getFunctionArtifact(contractAddress: AztecAddress, selector: FunctionSelector): Promise<FunctionArtifact>;
97
97
 
98
+ /**
99
+ * Generates a stable function name for debug purposes.
100
+ * @param contractAddress - The contract address.
101
+ * @param selector - The corresponding function selector.
102
+ */
103
+ getDebugFunctionName(contractAddress: AztecAddress, selector: FunctionSelector): Promise<string>;
104
+
98
105
  /**
99
106
  * Retrieves the artifact of a specified function within a given contract.
100
107
  * The function is identified by its name, which is unique within a contract.
@@ -1,10 +1,8 @@
1
- import { type EncryptedL2Log } from '@aztec/circuit-types';
2
1
  import { siloNullifier } from '@aztec/circuits.js/hash';
3
2
  import { type AztecAddress } from '@aztec/foundation/aztec-address';
4
3
  import { Fr } from '@aztec/foundation/fields';
5
4
 
6
5
  import { type NoteData } from '../acvm/index.js';
7
- import { type CountedLog } from './execution_result.js';
8
6
 
9
7
  export interface PendingNote {
10
8
  note: NoteData;
@@ -29,13 +27,6 @@ export class ExecutionNoteCache {
29
27
  */
30
28
  private nullifiers: Map<bigint, Set<bigint>> = new Map();
31
29
 
32
- /**
33
- * The list of encrypted logs linked to note hashes created in this transaction.
34
- * This mapping maps from inner note hash to log(s) emitted for that note hash.
35
- * Note that their value (bigint representation) is used because Frs cannot be looked up in Sets.
36
- */
37
- private logs: Map<bigint, CountedLog<EncryptedL2Log>[]> = new Map();
38
-
39
30
  /**
40
31
  * Add a new note to cache.
41
32
  * @param note - New note created during execution.
@@ -46,16 +37,6 @@ export class ExecutionNoteCache {
46
37
  this.newNotes.set(note.contractAddress.toBigInt(), notes);
47
38
  }
48
39
 
49
- /**
50
- * Add a new note to cache.
51
- * @param note - New note created during execution.
52
- */
53
- public addNewLog(log: CountedLog<EncryptedL2Log>, innerNoteHash: Fr) {
54
- const logs = this.logs.get(innerNoteHash.toBigInt()) ?? [];
55
- logs.push(log);
56
- this.logs.set(innerNoteHash.toBigInt(), logs);
57
- }
58
-
59
40
  /**
60
41
  * Add a nullifier to cache. It could be for a db note or a new note created during execution.
61
42
  * @param contractAddress - Contract address of the note.
@@ -81,8 +62,6 @@ export class ExecutionNoteCache {
81
62
  const note = notes.splice(noteIndexToRemove, 1)[0];
82
63
  nullifiedNoteHashCounter = note.counter;
83
64
  this.newNotes.set(contractAddress.toBigInt(), notes);
84
- // If a log linked to the note hash does not exist, this method just does nothing
85
- this.logs.delete(innerNoteHash.toBigInt());
86
65
  }
87
66
 
88
67
  return nullifiedNoteHashCounter;
@@ -117,11 +96,4 @@ export class ExecutionNoteCache {
117
96
  public getNullifiers(contractAddress: AztecAddress): Set<bigint> {
118
97
  return this.nullifiers.get(contractAddress.toBigInt()) ?? new Set();
119
98
  }
120
-
121
- /**
122
- * Return all note logs emitted from a contract.
123
- */
124
- public getLogs(): CountedLog<EncryptedL2Log>[] {
125
- return Array.from(this.logs.values()).flat();
126
- }
127
99
  }
@@ -1,6 +1,8 @@
1
1
  import {
2
2
  EncryptedFunctionL2Logs,
3
3
  type EncryptedL2Log,
4
+ type EncryptedL2NoteLog,
5
+ EncryptedNoteFunctionL2Logs,
4
6
  type Note,
5
7
  UnencryptedFunctionL2Logs,
6
8
  type UnencryptedL2Log,
@@ -22,7 +24,7 @@ export interface NoteAndSlot {
22
24
  noteTypeId: Fr;
23
25
  }
24
26
 
25
- export class CountedLog<TLog extends UnencryptedL2Log | EncryptedL2Log> implements IsEmpty {
27
+ export class CountedLog<TLog extends UnencryptedL2Log | EncryptedL2NoteLog | EncryptedL2Log> implements IsEmpty {
26
28
  constructor(public log: TLog, public counter: number) {}
27
29
 
28
30
  isEmpty(): boolean {
@@ -30,6 +32,11 @@ export class CountedLog<TLog extends UnencryptedL2Log | EncryptedL2Log> implemen
30
32
  }
31
33
  }
32
34
 
35
+ export class CountedNoteLog extends CountedLog<EncryptedL2NoteLog> {
36
+ constructor(log: EncryptedL2NoteLog, counter: number, public noteHashCounter: number) {
37
+ super(log, counter);
38
+ }
39
+ }
33
40
  /**
34
41
  * The result of executing a private function.
35
42
  */
@@ -62,7 +69,7 @@ export interface ExecutionResult {
62
69
  * Encrypted note logs emitted during execution of this function call.
63
70
  * Note: These are preimages to `noteEncryptedLogsHashes`.
64
71
  */
65
- noteEncryptedLogs: CountedLog<EncryptedL2Log>[];
72
+ noteEncryptedLogs: CountedNoteLog[];
66
73
  /**
67
74
  * Encrypted logs emitted during execution of this function call.
68
75
  * Note: These are preimages to `encryptedLogsHashes`.
@@ -92,8 +99,14 @@ export function collectNullifiedNoteHashCounters(execResult: ExecutionResult, ac
92
99
  * @param execResult - The topmost execution result.
93
100
  * @returns All encrypted logs.
94
101
  */
95
- function collectNoteEncryptedLogs(execResult: ExecutionResult): CountedLog<EncryptedL2Log>[] {
96
- return [execResult.noteEncryptedLogs, ...execResult.nestedExecutions.flatMap(collectNoteEncryptedLogs)].flat();
102
+ function collectNoteEncryptedLogs(
103
+ execResult: ExecutionResult,
104
+ nullifiedNoteHashCounters: Map<number, number>,
105
+ ): CountedLog<EncryptedL2NoteLog>[] {
106
+ return [
107
+ execResult.noteEncryptedLogs.filter(noteLog => !nullifiedNoteHashCounters.has(noteLog.noteHashCounter)),
108
+ ...execResult.nestedExecutions.flatMap(res => collectNoteEncryptedLogs(res, nullifiedNoteHashCounters)),
109
+ ].flat();
97
110
  }
98
111
 
99
112
  /**
@@ -101,10 +114,11 @@ function collectNoteEncryptedLogs(execResult: ExecutionResult): CountedLog<Encry
101
114
  * @param execResult - The topmost execution result.
102
115
  * @returns All encrypted logs.
103
116
  */
104
- export function collectSortedNoteEncryptedLogs(execResult: ExecutionResult): EncryptedFunctionL2Logs {
105
- const allLogs = collectNoteEncryptedLogs(execResult);
117
+ export function collectSortedNoteEncryptedLogs(execResult: ExecutionResult): EncryptedNoteFunctionL2Logs {
118
+ const nullifiedNoteHashCounters = collectNullifiedNoteHashCounters(execResult);
119
+ const allLogs = collectNoteEncryptedLogs(execResult, nullifiedNoteHashCounters);
106
120
  const sortedLogs = sortByCounter(allLogs);
107
- return new EncryptedFunctionL2Logs(sortedLogs.map(l => l.log));
121
+ return new EncryptedNoteFunctionL2Logs(sortedLogs.map(l => l.log));
108
122
  }
109
123
  /**
110
124
  * Collect all encrypted logs across all nested executions.
@@ -1,14 +1,15 @@
1
- import { type FunctionData, PrivateCallStackItem, PrivateCircuitPublicInputs } from '@aztec/circuits.js';
2
- import { type FunctionArtifact } from '@aztec/foundation/abi';
1
+ import { type CircuitWitnessGenerationStats } from '@aztec/circuit-types/stats';
2
+ import { Fr, FunctionData, PrivateCallStackItem, PrivateCircuitPublicInputs } from '@aztec/circuits.js';
3
+ import type { FunctionArtifact, FunctionSelector } from '@aztec/foundation/abi';
3
4
  import { type AztecAddress } from '@aztec/foundation/aztec-address';
4
5
  import { createDebugLogger } from '@aztec/foundation/log';
6
+ import { Timer } from '@aztec/foundation/timer';
5
7
 
6
8
  import { witnessMapToFields } from '../acvm/deserialize.js';
7
9
  import { Oracle, acvm, extractCallStack } from '../acvm/index.js';
8
10
  import { ExecutionError } from '../common/errors.js';
9
11
  import { type ClientExecutionContext } from './client_execution_context.js';
10
12
  import { type ExecutionResult } from './execution_result.js';
11
- import { AcirSimulator } from './simulator.js';
12
13
 
13
14
  /**
14
15
  * Execute a private function and return the execution result.
@@ -17,37 +18,51 @@ export async function executePrivateFunction(
17
18
  context: ClientExecutionContext,
18
19
  artifact: FunctionArtifact,
19
20
  contractAddress: AztecAddress,
20
- functionData: FunctionData,
21
+ functionSelector: FunctionSelector,
21
22
  log = createDebugLogger('aztec:simulator:secret_execution'),
22
23
  ): Promise<ExecutionResult> {
23
- const functionSelector = functionData.selector;
24
- log.verbose(`Executing external function ${contractAddress}:${functionSelector}(${artifact.name})`);
24
+ const functionName = await context.getDebugFunctionName();
25
+ log.verbose(`Executing external function ${contractAddress}:${functionSelector}(${functionName})`);
25
26
  const acir = artifact.bytecode;
26
27
  const initialWitness = context.getInitialWitness(artifact);
27
28
  const acvmCallback = new Oracle(context);
28
- const acirExecutionResult = await acvm(await AcirSimulator.getSolver(), acir, initialWitness, acvmCallback).catch(
29
- (err: Error) => {
30
- throw new ExecutionError(
31
- err.message,
32
- {
33
- contractAddress,
34
- functionSelector,
35
- },
36
- extractCallStack(err, artifact.debug),
37
- { cause: err },
38
- );
39
- },
40
- );
29
+ const timer = new Timer();
30
+ const acirExecutionResult = await acvm(acir, initialWitness, acvmCallback).catch((err: Error) => {
31
+ throw new ExecutionError(
32
+ err.message,
33
+ {
34
+ contractAddress,
35
+ functionSelector,
36
+ },
37
+ extractCallStack(err, artifact.debug),
38
+ { cause: err },
39
+ );
40
+ });
41
+ const duration = timer.ms();
41
42
  const partialWitness = acirExecutionResult.partialWitness;
42
43
  const returnWitness = witnessMapToFields(acirExecutionResult.returnWitness);
43
44
  const publicInputs = PrivateCircuitPublicInputs.fromFields(returnWitness);
44
45
 
45
- context.chopNoteEncryptedLogs();
46
+ // TODO (alexg) estimate this size
47
+ const initialWitnessSize = witnessMapToFields(initialWitness).length * Fr.SIZE_IN_BYTES;
48
+ log.debug(`Ran external function ${contractAddress.toString()}:${functionSelector}`, {
49
+ circuitName: 'app-circuit',
50
+ duration,
51
+ eventName: 'circuit-witness-generation',
52
+ inputSize: initialWitnessSize,
53
+ outputSize: publicInputs.toBuffer().length,
54
+ appCircuitName: functionName,
55
+ } satisfies CircuitWitnessGenerationStats);
56
+
46
57
  const noteEncryptedLogs = context.getNoteEncryptedLogs();
47
58
  const encryptedLogs = context.getEncryptedLogs();
48
59
  const unencryptedLogs = context.getUnencryptedLogs();
49
60
 
50
- const callStackItem = new PrivateCallStackItem(contractAddress, functionData, publicInputs);
61
+ const callStackItem = new PrivateCallStackItem(
62
+ contractAddress,
63
+ new FunctionData(functionSelector, true),
64
+ publicInputs,
65
+ );
51
66
 
52
67
  const rawReturnValues = await context.unpackReturns(publicInputs.returnsHash);
53
68
 
@@ -1,5 +1,5 @@
1
1
  import { type AztecNode, type FunctionCall, type Note, type TxExecutionRequest } from '@aztec/circuit-types';
2
- import { CallContext, FunctionData } from '@aztec/circuits.js';
2
+ import { CallContext } from '@aztec/circuits.js';
3
3
  import {
4
4
  type ArrayType,
5
5
  type FunctionArtifact,
@@ -11,8 +11,6 @@ import { AztecAddress } from '@aztec/foundation/aztec-address';
11
11
  import { Fr } from '@aztec/foundation/fields';
12
12
  import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';
13
13
 
14
- import { type WasmBlackBoxFunctionSolver, createBlackBoxSolver } from '@noir-lang/acvm_js';
15
-
16
14
  import { createSimulationError } from '../common/errors.js';
17
15
  import { PackedValuesCache } from '../common/packed_values_cache.js';
18
16
  import { ClientExecutionContext } from './client_execution_context.js';
@@ -27,33 +25,12 @@ import { ViewDataOracle } from './view_data_oracle.js';
27
25
  * The ACIR simulator.
28
26
  */
29
27
  export class AcirSimulator {
30
- private static solver: Promise<WasmBlackBoxFunctionSolver>; // ACVM's backend
31
28
  private log: DebugLogger;
32
29
 
33
30
  constructor(private db: DBOracle, private node: AztecNode) {
34
31
  this.log = createDebugLogger('aztec:simulator');
35
32
  }
36
33
 
37
- /**
38
- * Gets or initializes the ACVM WasmBlackBoxFunctionSolver.
39
- *
40
- * @remarks
41
- *
42
- * Occurs only once across all instances of AcirSimulator.
43
- * Speeds up execution by only performing setup tasks (like pedersen
44
- * generator initialization) one time.
45
- * TODO(https://github.com/AztecProtocol/aztec-packages/issues/1627):
46
- * determine whether this requires a lock
47
- *
48
- * @returns ACVM WasmBlackBoxFunctionSolver
49
- */
50
- public static getSolver(): Promise<WasmBlackBoxFunctionSolver> {
51
- if (!this.solver) {
52
- this.solver = createBlackBoxSolver();
53
- }
54
- return this.solver;
55
- }
56
-
57
34
  /**
58
35
  * Runs a private function.
59
36
  * @param request - The transaction request.
@@ -110,7 +87,7 @@ export class AcirSimulator {
110
87
  context,
111
88
  entryPointArtifact,
112
89
  contractAddress,
113
- request.functionData,
90
+ request.functionSelector,
114
91
  );
115
92
  return executionResult;
116
93
  } catch (err) {
@@ -141,7 +118,7 @@ export class AcirSimulator {
141
118
  context,
142
119
  entryPointArtifact,
143
120
  contractAddress,
144
- request.functionData,
121
+ request.selector,
145
122
  request.args,
146
123
  );
147
124
  } catch (err) {
@@ -193,10 +170,13 @@ export class AcirSimulator {
193
170
  const extendedNoteItems = note.items.concat(Array(maxNoteFields - note.items.length).fill(Fr.ZERO));
194
171
 
195
172
  const execRequest: FunctionCall = {
173
+ name: artifact.name,
196
174
  to: contractAddress,
197
- functionData: FunctionData.empty(),
175
+ selector: FunctionSelector.empty(),
176
+ type: FunctionType.UNCONSTRAINED,
198
177
  isStatic: artifact.isStatic,
199
178
  args: encodeArguments(artifact, [contractAddress, nonce, storageSlot, noteTypeId, extendedNoteItems]),
179
+ returnTypes: artifact.returnTypes,
200
180
  };
201
181
 
202
182
  const [innerNoteHash, uniqueNoteHash, siloedNoteHash, innerNullifier] = (await this.runUnconstrained(