@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,65 +1,66 @@
1
1
  import {
2
+ AVM_REQUEST,
3
+ type AvmProvingRequest,
2
4
  MerkleTreeId,
3
5
  type NestedProcessReturnValues,
4
- type PublicKernelRequest,
6
+ type PublicKernelNonTailRequest,
5
7
  PublicKernelType,
8
+ type PublicProvingRequest,
6
9
  type SimulationError,
7
10
  type Tx,
8
11
  type UnencryptedFunctionL2Logs,
9
12
  } from '@aztec/circuit-types';
10
13
  import {
14
+ type AvmExecutionHints,
11
15
  AztecAddress,
12
16
  CallRequest,
13
17
  ContractStorageRead,
14
18
  ContractStorageUpdateRequest,
15
19
  Fr,
20
+ FunctionData,
16
21
  Gas,
17
22
  type GlobalVariables,
18
23
  type Header,
19
24
  type KernelCircuitPublicInputs,
20
25
  L2ToL1Message,
21
26
  LogHash,
27
+ MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_CALL,
22
28
  MAX_NEW_L2_TO_L1_MSGS_PER_CALL,
23
29
  MAX_NEW_NOTE_HASHES_PER_CALL,
24
30
  MAX_NEW_NULLIFIERS_PER_CALL,
31
+ MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
25
32
  MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL,
26
33
  MAX_NULLIFIER_READ_REQUESTS_PER_CALL,
27
34
  MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
28
35
  MAX_PUBLIC_DATA_READS_PER_CALL,
29
- MAX_PUBLIC_DATA_READS_PER_TX,
30
36
  MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
31
- MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
32
37
  MAX_UNENCRYPTED_LOGS_PER_CALL,
33
38
  MembershipWitness,
39
+ NESTED_RECURSIVE_PROOF_LENGTH,
34
40
  NoteHash,
35
41
  Nullifier,
36
- type Proof,
37
42
  PublicCallData,
38
43
  type PublicCallRequest,
39
44
  PublicCallStackItem,
40
45
  PublicCircuitPublicInputs,
41
- PublicDataRead,
42
- PublicDataUpdateRequest,
43
46
  PublicKernelCircuitPrivateInputs,
44
47
  type PublicKernelCircuitPublicInputs,
45
48
  PublicKernelData,
46
49
  ReadRequest,
47
50
  RevertCode,
48
51
  VK_TREE_HEIGHT,
49
- VerificationKey,
52
+ VerificationKeyData,
50
53
  makeEmptyProof,
54
+ makeEmptyRecursiveProof,
51
55
  } from '@aztec/circuits.js';
52
56
  import { computeVarArgsHash } from '@aztec/circuits.js/hash';
53
- import { arrayNonEmptyLength, padArrayEnd } from '@aztec/foundation/collection';
57
+ import { padArrayEnd } from '@aztec/foundation/collection';
54
58
  import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';
55
- import { type Tuple } from '@aztec/foundation/serialize';
56
59
  import {
57
60
  type PublicExecution,
58
61
  type PublicExecutionResult,
59
62
  type PublicExecutor,
60
63
  accumulateReturnValues,
61
- collectPublicDataReads,
62
- collectPublicDataUpdateRequests,
63
64
  isPublicExecutionResult,
64
65
  } from '@aztec/simulator';
65
66
  import { type MerkleTreeOperations } from '@aztec/world-state';
@@ -78,7 +79,7 @@ export enum PublicKernelPhase {
78
79
  export const PhaseIsRevertible: Record<PublicKernelPhase, boolean> = {
79
80
  [PublicKernelPhase.SETUP]: false,
80
81
  [PublicKernelPhase.APP_LOGIC]: true,
81
- [PublicKernelPhase.TEARDOWN]: false,
82
+ [PublicKernelPhase.TEARDOWN]: true,
82
83
  [PublicKernelPhase.TAIL]: false,
83
84
  };
84
85
 
@@ -96,6 +97,59 @@ export function publicKernelPhaseToKernelType(phase: PublicKernelPhase): PublicK
96
97
  }
97
98
  }
98
99
 
100
+ export type PublicProvingInformation = {
101
+ calldata: Fr[];
102
+ bytecode: Buffer;
103
+ inputs: PublicKernelCircuitPrivateInputs;
104
+ avmHints: AvmExecutionHints;
105
+ };
106
+
107
+ export function makeAvmProvingRequest(
108
+ info: PublicProvingInformation,
109
+ kernelType: PublicKernelNonTailRequest['type'],
110
+ ): AvmProvingRequest {
111
+ return {
112
+ type: AVM_REQUEST,
113
+ bytecode: info.bytecode,
114
+ calldata: info.calldata,
115
+ avmHints: info.avmHints,
116
+ kernelRequest: {
117
+ type: kernelType,
118
+ inputs: info.inputs,
119
+ },
120
+ };
121
+ }
122
+
123
+ export type TxPublicCallsResult = {
124
+ /** Inputs to be used for proving */
125
+ publicProvingInformation: PublicProvingInformation[];
126
+ /** The public kernel output at the end of the Tx */
127
+ kernelOutput: PublicKernelCircuitPublicInputs;
128
+ /** Unencrypted logs generated during the execution of this Tx */
129
+ newUnencryptedLogs: UnencryptedFunctionL2Logs[];
130
+ /** Revert reason, if any */
131
+ revertReason?: SimulationError;
132
+ /** Return values of simulating complete callstack */
133
+ returnValues: NestedProcessReturnValues[];
134
+ /** Gas used during the execution this Tx */
135
+ gasUsed?: Gas;
136
+ };
137
+
138
+ export type PhaseResult = {
139
+ /** The collection of public proving requests */
140
+ publicProvingRequests: PublicProvingRequest[];
141
+ /** The output of the public kernel circuit simulation for this phase */
142
+ publicKernelOutput: PublicKernelCircuitPublicInputs;
143
+ /** The final output of the public kernel circuit for this phase */
144
+ finalKernelOutput?: KernelCircuitPublicInputs;
145
+ /** Revert reason, if any */
146
+ revertReason?: SimulationError;
147
+ /** Return values of simulating complete callstack */
148
+ returnValues: NestedProcessReturnValues[];
149
+ /** Gas used during the execution this phase */
150
+ gasUsed?: Gas;
151
+ };
152
+
99
153
  export abstract class AbstractPhaseManager {
100
154
  protected hintsBuilder: HintsBuilder;
101
155
  protected log: DebugLogger;
@@ -110,41 +164,12 @@ export abstract class AbstractPhaseManager {
110
164
  this.hintsBuilder = new HintsBuilder(db);
111
165
  this.log = createDebugLogger(`aztec:sequencer:${phase}`);
112
166
  }
167
+
113
168
  /**
114
- *
115
169
  * @param tx - the tx to be processed
116
170
  * @param publicKernelPublicInputs - the output of the public kernel circuit for the previous phase
117
- * @param previousPublicKernelProof - the proof of the public kernel circuit for the previous phase
118
171
  */
119
- abstract handle(
120
- tx: Tx,
121
- publicKernelPublicInputs: PublicKernelCircuitPublicInputs,
122
- previousPublicKernelProof: Proof,
123
- ): Promise<{
124
- /**
125
- * The collection of public kernel requests
126
- */
127
- kernelRequests: PublicKernelRequest[];
128
- /**
129
- * the output of the public kernel circuit for this phase
130
- */
131
- publicKernelOutput: PublicKernelCircuitPublicInputs;
132
- /**
133
- * the final output of the public kernel circuit for this phase
134
- */
135
- finalKernelOutput?: KernelCircuitPublicInputs;
136
- /**
137
- * the proof of the public kernel circuit for this phase
138
- */
139
- publicKernelProof: Proof;
140
- /**
141
- * revert reason, if any
142
- */
143
- revertReason: SimulationError | undefined;
144
- returnValues: NestedProcessReturnValues[];
145
- /** Gas used during the execution this particular phase. */
146
- gasUsed: Gas | undefined;
147
- }>;
172
+ abstract handle(tx: Tx, publicKernelPublicInputs: PublicKernelCircuitPublicInputs): Promise<PhaseResult>;
148
173
 
149
174
  public static extractEnqueuedPublicCallsByPhase(tx: Tx): Record<PublicKernelPhase, PublicCallRequest[]> {
150
175
  const data = tx.data.forPublic;
@@ -216,43 +241,32 @@ export abstract class AbstractPhaseManager {
216
241
  return calls;
217
242
  }
218
243
 
219
- // REFACTOR: Do not return an array and instead return a struct with similar shape to that returned by `handle`
220
244
  protected async processEnqueuedPublicCalls(
221
245
  tx: Tx,
222
246
  previousPublicKernelOutput: PublicKernelCircuitPublicInputs,
223
- previousPublicKernelProof: Proof,
224
- ): Promise<
225
- [
226
- PublicKernelCircuitPrivateInputs[],
227
- PublicKernelCircuitPublicInputs,
228
- Proof,
229
- UnencryptedFunctionL2Logs[],
230
- SimulationError | undefined,
231
- NestedProcessReturnValues[],
232
- Gas,
233
- ]
234
- > {
235
- let kernelOutput = previousPublicKernelOutput;
236
- const kernelProof = previousPublicKernelProof;
237
- const publicKernelInputs: PublicKernelCircuitPrivateInputs[] = [];
238
-
247
+ ): Promise<TxPublicCallsResult> {
239
248
  const enqueuedCalls = this.extractEnqueuedPublicCalls(tx);
240
249
 
241
250
  if (!enqueuedCalls || !enqueuedCalls.length) {
242
- return [[], kernelOutput, kernelProof, [], undefined, [], Gas.empty()];
251
+ return {
252
+ publicProvingInformation: [],
253
+ kernelOutput: previousPublicKernelOutput,
254
+ newUnencryptedLogs: [],
255
+ returnValues: [],
256
+ gasUsed: Gas.empty(),
257
+ };
243
258
  }
244
259
 
245
- const newUnencryptedFunctionLogs: UnencryptedFunctionL2Logs[] = [];
246
-
247
- // Transaction fee is zero for all phases except teardown
248
- const transactionFee = this.getTransactionFee(tx, previousPublicKernelOutput);
249
-
250
260
  // TODO(#1684): Should multiple separately enqueued public calls be treated as
251
261
  // separate public callstacks to be proven by separate public kernel sequences
252
262
  // and submitted separately to the base rollup?
253
263
 
264
+ const provingInformationList: PublicProvingInformation[] = [];
265
+ const newUnencryptedFunctionLogs: UnencryptedFunctionL2Logs[] = [];
266
+ // Transaction fee is zero for all phases except teardown
267
+ const transactionFee = this.getTransactionFee(tx, previousPublicKernelOutput);
254
268
  let gasUsed = Gas.empty();
255
-
269
+ let kernelPublicOutput: PublicKernelCircuitPublicInputs = previousPublicKernelOutput;
256
270
  const enqueuedCallResults = [];
257
271
 
258
272
  for (const enqueuedCall of enqueuedCalls) {
@@ -264,10 +278,9 @@ export abstract class AbstractPhaseManager {
264
278
  while (executionStack.length) {
265
279
  const current = executionStack.pop()!;
266
280
  const isExecutionRequest = !isPublicExecutionResult(current);
267
- // TODO(6052): Extract correct new counter from nested calls
268
- const sideEffectCounter = lastSideEffectCounter(tx) + 1;
269
- const availableGas = this.getAvailableGas(tx, kernelOutput);
270
- const pendingNullifiers = this.getSiloedPendingNullifiers(kernelOutput);
281
+ const sideEffectCounter = lastSideEffectCounter(kernelPublicOutput) + 1;
282
+ const availableGas = this.getAvailableGas(tx, kernelPublicOutput);
283
+ const pendingNullifiers = this.getSiloedPendingNullifiers(kernelPublicOutput);
271
284
 
272
285
  const result = isExecutionRequest
273
286
  ? await this.publicExecutor.simulate(
@@ -281,18 +294,18 @@ export abstract class AbstractPhaseManager {
281
294
  )
282
295
  : current;
283
296
 
297
+ // Accumulate gas used in this execution
298
+ gasUsed = gasUsed.add(Gas.from(result.startGasLeft).sub(Gas.from(result.endGasLeft)));
299
+
284
300
  // Sanity check for a current upstream assumption.
285
301
  // Consumers of the result seem to expect "reverted <=> revertReason !== undefined".
286
- const functionSelector = result.execution.functionData.selector.toString();
302
+ const functionSelector = result.execution.functionSelector.toString();
287
303
  if (result.reverted && !result.revertReason) {
288
304
  throw new Error(
289
305
  `Simulation of ${result.execution.contractAddress.toString()}:${functionSelector} reverted with no reason.`,
290
306
  );
291
307
  }
292
308
 
293
- // Accumulate gas used in this execution
294
- gasUsed = gasUsed.add(Gas.from(result.startGasLeft).sub(Gas.from(result.endGasLeft)));
295
-
296
309
  if (result.reverted && !PhaseIsRevertible[this.phase]) {
297
310
  this.log.debug(
298
311
  `Simulation error on ${result.execution.contractAddress.toString()}:${functionSelector} with reason: ${
@@ -305,23 +318,29 @@ export abstract class AbstractPhaseManager {
305
318
  if (isExecutionRequest) {
306
319
  newUnencryptedFunctionLogs.push(result.allUnencryptedLogs);
307
320
  }
321
+ executionStack.push(...result.nestedExecutions);
308
322
 
323
+ // Simulate the public kernel circuit.
309
324
  this.log.debug(
310
325
  `Running public kernel circuit for ${result.execution.contractAddress.toString()}:${functionSelector}`,
311
326
  );
312
- executionStack.push(...result.nestedExecutions);
313
327
  const callData = await this.getPublicCallData(result, isExecutionRequest);
314
-
315
- const circuitResult = await this.runKernelCircuit(kernelOutput, kernelProof, callData);
316
- kernelOutput = circuitResult[1];
317
-
318
- // Capture the inputs to the kernel circuit for later proving
319
- publicKernelInputs.push(circuitResult[0]);
320
-
321
- // sanity check. Note we can't expect them to just be equal, because e.g.
328
+ const [privateInputs, publicInputs] = await this.runKernelCircuit(kernelPublicOutput, callData);
329
+ kernelPublicOutput = publicInputs;
330
+
331
+ // Capture the inputs for later proving in the AVM and kernel.
332
+ const publicProvingInformation: PublicProvingInformation = {
333
+ calldata: result.calldata,
334
+ bytecode: result.bytecode!,
335
+ inputs: privateInputs,
336
+ avmHints: result.avmHints,
337
+ };
338
+ provingInformationList.push(publicProvingInformation);
339
+
340
+ // Sanity check: Note we can't expect them to just be equal, because e.g.
322
341
  // if the simulator reverts in app logic, it "resets" and result.reverted will be false when we run teardown,
323
342
  // but the kernel carries the reverted flag forward. But if the simulator reverts, so should the kernel.
324
- if (result.reverted && kernelOutput.revertCode.isOK()) {
343
+ if (result.reverted && kernelPublicOutput.revertCode.isOK()) {
325
344
  throw new Error(
326
345
  `Public kernel circuit did not revert on ${result.execution.contractAddress.toString()}:${functionSelector}, but simulator did.`,
327
346
  );
@@ -336,7 +355,14 @@ export abstract class AbstractPhaseManager {
336
355
  }`,
337
356
  );
338
357
  // TODO(@spalladino): Check gasUsed is correct. The AVM should take care of setting gasLeft to zero upon a revert.
339
- return [[], kernelOutput, kernelProof, [], result.revertReason, [], gasUsed];
358
+ return {
359
+ publicProvingInformation: [],
360
+ kernelOutput: kernelPublicOutput,
361
+ newUnencryptedLogs: [],
362
+ revertReason: result.revertReason,
363
+ returnValues: [],
364
+ gasUsed,
365
+ };
340
366
  }
341
367
 
342
368
  if (!enqueuedExecutionResult) {
@@ -345,26 +371,18 @@ export abstract class AbstractPhaseManager {
345
371
 
346
372
  enqueuedCallResults.push(accumulateReturnValues(enqueuedExecutionResult));
347
373
  }
348
- // HACK(#1622): Manually patches the ordering of public state actions
349
- // TODO(#757): Enforce proper ordering of public state actions
350
- patchPublicStorageActionOrdering(kernelOutput, enqueuedExecutionResult!, this.phase);
351
374
  }
352
375
 
353
- // TODO(#3675): This should be done in a public kernel circuit
354
- removeRedundantPublicDataWrites(kernelOutput, this.phase);
355
-
356
- return [
357
- publicKernelInputs,
358
- kernelOutput,
359
- kernelProof,
360
- newUnencryptedFunctionLogs,
361
- undefined,
362
- enqueuedCallResults,
376
+ return {
377
+ publicProvingInformation: provingInformationList,
378
+ kernelOutput: kernelPublicOutput,
379
+ newUnencryptedLogs: newUnencryptedFunctionLogs,
380
+ returnValues: enqueuedCallResults,
363
381
  gasUsed,
364
- ];
382
+ };
365
383
  }
366
384
 
367
- /** Returns all pending private and public nullifiers. */
385
+ /** Returns all pending private and public nullifiers. */
368
386
  private getSiloedPendingNullifiers(ko: PublicKernelCircuitPublicInputs) {
369
387
  return [...ko.end.newNullifiers, ...ko.endNonRevertibleData.newNullifiers].filter(n => !n.isEmpty());
370
388
  }
@@ -380,20 +398,11 @@ export abstract class AbstractPhaseManager {
380
398
  return Fr.ZERO;
381
399
  }
382
400
 
383
- protected async runKernelCircuit(
401
+ private async runKernelCircuit(
384
402
  previousOutput: PublicKernelCircuitPublicInputs,
385
- previousProof: Proof,
386
403
  callData: PublicCallData,
387
404
  ): Promise<[PublicKernelCircuitPrivateInputs, PublicKernelCircuitPublicInputs]> {
388
- return await this.getKernelCircuitOutput(previousOutput, previousProof, callData);
389
- }
390
-
391
- protected async getKernelCircuitOutput(
392
- previousOutput: PublicKernelCircuitPublicInputs,
393
- previousProof: Proof,
394
- callData: PublicCallData,
395
- ): Promise<[PublicKernelCircuitPrivateInputs, PublicKernelCircuitPublicInputs]> {
396
- const previousKernel = this.getPreviousKernelData(previousOutput, previousProof);
405
+ const previousKernel = this.getPreviousKernelData(previousOutput);
397
406
 
398
407
  // We take a deep copy (clone) of these inputs to be passed to the prover
399
408
  const inputs = new PublicKernelCircuitPrivateInputs(previousKernel, callData);
@@ -409,15 +418,13 @@ export abstract class AbstractPhaseManager {
409
418
  }
410
419
  }
411
420
 
412
- protected getPreviousKernelData(
413
- previousOutput: PublicKernelCircuitPublicInputs,
414
- previousProof: Proof,
415
- ): PublicKernelData {
416
- // TODO(@PhilWindle) Fix once we move this to prover-client
417
- const vk = VerificationKey.makeFake();
421
+ protected getPreviousKernelData(previousOutput: PublicKernelCircuitPublicInputs): PublicKernelData {
422
+ // The proof and verification key are not used in simulation
423
+ const vk = VerificationKeyData.makeFake();
424
+ const proof = makeEmptyRecursiveProof(NESTED_RECURSIVE_PROOF_LENGTH);
418
425
  const vkIndex = 0;
419
426
  const vkSiblingPath = MembershipWitness.random(VK_TREE_HEIGHT).siblingPath;
420
- return new PublicKernelData(previousOutput, previousProof, vk, vkIndex, vkSiblingPath);
427
+ return new PublicKernelData(previousOutput, proof, vk, vkIndex, vkSiblingPath);
421
428
  }
422
429
 
423
430
  protected async getPublicCallStackItem(result: PublicExecutionResult, isExecutionRequest = false) {
@@ -441,6 +448,11 @@ export abstract class AbstractPhaseManager {
441
448
  startSideEffectCounter: result.startSideEffectCounter,
442
449
  endSideEffectCounter: result.endSideEffectCounter,
443
450
  returnsHash: computeVarArgsHash(result.returnValues),
451
+ noteHashReadRequests: padArrayEnd(
452
+ result.noteHashReadRequests,
453
+ ReadRequest.empty(),
454
+ MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
455
+ ),
444
456
  nullifierReadRequests: padArrayEnd(
445
457
  result.nullifierReadRequests,
446
458
  ReadRequest.empty(),
@@ -451,6 +463,11 @@ export abstract class AbstractPhaseManager {
451
463
  ReadRequest.empty(),
452
464
  MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL,
453
465
  ),
466
+ l1ToL2MsgReadRequests: padArrayEnd(
467
+ result.l1ToL2MsgReadRequests,
468
+ ReadRequest.empty(),
469
+ MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_CALL,
470
+ ),
454
471
  contractStorageReads: padArrayEnd(
455
472
  result.contractStorageReads,
456
473
  ContractStorageRead.empty(),
@@ -469,12 +486,12 @@ export abstract class AbstractPhaseManager {
469
486
  endGasLeft: Gas.from(result.endGasLeft),
470
487
  transactionFee: result.transactionFee,
471
488
  // TODO(@just-mitch): need better mapping from simulator to revert code.
472
- revertCode: result.reverted ? RevertCode.REVERTED : RevertCode.OK,
489
+ revertCode: result.reverted ? RevertCode.APP_LOGIC_REVERTED : RevertCode.OK,
473
490
  });
474
491
 
475
492
  return new PublicCallStackItem(
476
493
  result.execution.contractAddress,
477
- result.execution.functionData,
494
+ new FunctionData(result.execution.functionSelector, false),
478
495
  publicCircuitPublicInputs,
479
496
  isExecutionRequest,
480
497
  );
@@ -516,87 +533,3 @@ export abstract class AbstractPhaseManager {
516
533
  return new PublicCallData(callStackItem, publicCallStack, makeEmptyProof(), bytecodeHash);
517
534
  }
518
535
  }
519
-
520
- function removeRedundantPublicDataWrites(publicInputs: PublicKernelCircuitPublicInputs, phase: PublicKernelPhase) {
521
- const lastWritesMap = new Map<string, boolean>();
522
- const patch = <N extends number>(requests: Tuple<PublicDataUpdateRequest, N>) =>
523
- requests.filter(write => {
524
- const leafSlot = write.leafSlot.toString();
525
- const exists = lastWritesMap.get(leafSlot);
526
- lastWritesMap.set(leafSlot, true);
527
- return !exists;
528
- });
529
-
530
- const [prev, curr] = PhaseIsRevertible[phase]
531
- ? [publicInputs.endNonRevertibleData, publicInputs.end]
532
- : [publicInputs.end, publicInputs.endNonRevertibleData];
533
-
534
- curr.publicDataUpdateRequests = padArrayEnd(
535
- patch(curr.publicDataUpdateRequests.reverse()).reverse(),
536
- PublicDataUpdateRequest.empty(),
537
- MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
538
- );
539
-
540
- prev.publicDataUpdateRequests = padArrayEnd(
541
- patch(prev.publicDataUpdateRequests.reverse()),
542
- PublicDataUpdateRequest.empty(),
543
- MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
544
- );
545
- }
546
-
547
- // HACK(#1622): this is a hack to fix ordering of public state in the call stack. Since the private kernel
548
- // cannot keep track of side effects that happen after or before a nested call, we override the public
549
- // state actions it emits with whatever we got from the simulator. As a sanity check, we at least verify
550
- // that the elements are the same, so we are only tweaking their ordering.
551
- // See yarn-project/end-to-end/src/e2e_ordering.test.ts
552
- // See https://github.com/AztecProtocol/aztec-packages/issues/1616
553
- // TODO(#757): Enforce proper ordering of public state actions
554
- /**
555
- * Patch the ordering of storage actions output from the public kernel.
556
- * @param publicInputs - to be patched here: public inputs to the kernel iteration up to this point
557
- * @param execResult - result of the top/first execution for this enqueued public call
558
- */
559
- function patchPublicStorageActionOrdering(
560
- publicInputs: PublicKernelCircuitPublicInputs,
561
- execResult: PublicExecutionResult,
562
- phase: PublicKernelPhase,
563
- ) {
564
- const { publicDataUpdateRequests } = PhaseIsRevertible[phase] ? publicInputs.end : publicInputs.endNonRevertibleData;
565
- const { publicDataReads } = publicInputs.validationRequests;
566
-
567
- // Convert ContractStorage* objects to PublicData* objects and sort them in execution order.
568
- // Note, this only pulls simulated reads/writes from the current phase,
569
- // so the returned result will be a subset of the public kernel output.
570
-
571
- const simPublicDataReads = collectPublicDataReads(execResult);
572
-
573
- const simPublicDataUpdateRequests = collectPublicDataUpdateRequests(execResult);
574
-
575
- // We only want to reorder the items from the public inputs of the
576
- // most recently processed top/enqueued call.
577
-
578
- const effectSet = PhaseIsRevertible[phase] ? 'end' : 'endNonRevertibleData';
579
-
580
- const numReadsInKernel = arrayNonEmptyLength(publicDataReads, f => f.isEmpty());
581
- const numReadsBeforeThisEnqueuedCall = numReadsInKernel - simPublicDataReads.length;
582
- publicInputs.validationRequests.publicDataReads = padArrayEnd(
583
- [
584
- // do not mess with items from previous top/enqueued calls in kernel output
585
- ...publicInputs.validationRequests.publicDataReads.slice(0, numReadsBeforeThisEnqueuedCall),
586
- ...simPublicDataReads,
587
- ],
588
- PublicDataRead.empty(),
589
- MAX_PUBLIC_DATA_READS_PER_TX,
590
- );
591
-
592
- const numUpdatesInKernel = arrayNonEmptyLength(publicDataUpdateRequests, f => f.isEmpty());
593
- const numUpdatesBeforeThisEnqueuedCall = numUpdatesInKernel - simPublicDataUpdateRequests.length;
594
- publicInputs[effectSet].publicDataUpdateRequests = padArrayEnd(
595
- [
596
- ...publicInputs[effectSet].publicDataUpdateRequests.slice(0, numUpdatesBeforeThisEnqueuedCall),
597
- ...simPublicDataUpdateRequests,
598
- ],
599
- PublicDataUpdateRequest.empty(),
600
- MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
601
- );
602
- }
@@ -1,15 +1,10 @@
1
- import { type PublicKernelRequest, PublicKernelType, type Tx } from '@aztec/circuit-types';
2
- import {
3
- type GlobalVariables,
4
- type Header,
5
- type Proof,
6
- type PublicKernelCircuitPublicInputs,
7
- } from '@aztec/circuits.js';
1
+ import { PublicKernelType, type PublicProvingRequest, type Tx } from '@aztec/circuit-types';
2
+ import { type GlobalVariables, type Header, type PublicKernelCircuitPublicInputs } from '@aztec/circuits.js';
8
3
  import { type PublicExecutor, type PublicStateDB } from '@aztec/simulator';
9
4
  import { type MerkleTreeOperations } from '@aztec/world-state';
10
5
 
11
- import { AbstractPhaseManager, PublicKernelPhase } from './abstract_phase_manager.js';
12
- import { type ContractsDataSourcePublicDB } from './public_executor.js';
6
+ import { AbstractPhaseManager, PublicKernelPhase, makeAvmProvingRequest } from './abstract_phase_manager.js';
7
+ import { type ContractsDataSourcePublicDB } from './public_db_sources.js';
13
8
  import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
14
9
 
15
10
  /**
@@ -29,49 +24,38 @@ export class AppLogicPhaseManager extends AbstractPhaseManager {
29
24
  super(db, publicExecutor, publicKernel, globalVariables, historicalHeader, phase);
30
25
  }
31
26
 
32
- override async handle(
33
- tx: Tx,
34
- previousPublicKernelOutput: PublicKernelCircuitPublicInputs,
35
- previousPublicKernelProof: Proof,
36
- ) {
27
+ override async handle(tx: Tx, previousPublicKernelOutput: PublicKernelCircuitPublicInputs) {
28
+ this.log.verbose(`Processing tx ${tx.getTxHash()}`);
37
29
  // add new contracts to the contracts db so that their functions may be found and called
38
30
  // TODO(#4073): This is catching only private deployments, when we add public ones, we'll
39
31
  // have to capture contracts emitted in that phase as well.
40
32
  // TODO(@spalladino): Should we allow emitting contracts in the fee preparation phase?
41
- this.log.verbose(`Processing tx ${tx.getTxHash()}`);
33
+ // TODO(#6464): Should we allow emitting contracts in the private setup phase?
34
+ // if so, this should only add contracts that were deployed during private app logic.
42
35
  await this.publicContractsDB.addNewContracts(tx);
43
- const [
44
- kernelInputs,
45
- publicKernelOutput,
46
- publicKernelProof,
47
- newUnencryptedFunctionLogs,
48
- revertReason,
49
- returnValues,
50
- gasUsed,
51
- ] = await this.processEnqueuedPublicCalls(tx, previousPublicKernelOutput, previousPublicKernelProof).catch(
52
- // if we throw for any reason other than simulation, we need to rollback and drop the TX
53
- async err => {
54
- await this.publicStateDB.rollbackToCommit();
55
- throw err;
56
- },
57
- );
36
+ const { publicProvingInformation, kernelOutput, newUnencryptedLogs, revertReason, returnValues, gasUsed } =
37
+ await this.processEnqueuedPublicCalls(tx, previousPublicKernelOutput).catch(
38
+ // if we throw for any reason other than simulation, we need to rollback and drop the TX
39
+ async err => {
40
+ await this.publicStateDB.rollbackToCommit();
41
+ throw err;
42
+ },
43
+ );
58
44
 
59
45
  if (revertReason) {
46
+ // TODO(#6464): Should we allow emitting contracts in the private setup phase?
47
+ // if so, this is removing contracts deployed in private setup
60
48
  await this.publicContractsDB.removeNewContracts(tx);
61
49
  await this.publicStateDB.rollbackToCheckpoint();
62
50
  } else {
63
- tx.unencryptedLogs.addFunctionLogs(newUnencryptedFunctionLogs);
64
- await this.publicStateDB.checkpoint();
51
+ tx.unencryptedLogs.addFunctionLogs(newUnencryptedLogs);
52
+ // TODO(#6470): we should be adding contracts deployed in those logs to the publicContractsDB
65
53
  }
66
54
 
67
55
  // Return a list of app logic proving requests
68
- const kernelRequests = kernelInputs.map(input => {
69
- const request: PublicKernelRequest = {
70
- type: PublicKernelType.APP_LOGIC,
71
- inputs: input,
72
- };
73
- return request;
56
+ const publicProvingRequests: PublicProvingRequest[] = publicProvingInformation.map(info => {
57
+ return makeAvmProvingRequest(info, PublicKernelType.APP_LOGIC);
74
58
  });
75
- return { kernelRequests, publicKernelOutput, publicKernelProof, revertReason, returnValues, gasUsed };
59
+ return { publicProvingRequests, publicKernelOutput: kernelOutput, revertReason, returnValues, gasUsed };
76
60
  }
77
61
  }
@@ -23,9 +23,9 @@ export interface PublicStateDB {
23
23
  * @param contract - Owner of the storage.
24
24
  * @param slot - Slot to read in the contract storage.
25
25
  * @param newValue - The new value to store.
26
- * @returns Nothing.
26
+ * @returns The slot of the written leaf in the public data tree.
27
27
  */
28
- storageWrite(contract: AztecAddress, slot: Fr, newValue: Fr): Promise<void>;
28
+ storageWrite(contract: AztecAddress, slot: Fr, newValue: Fr): Promise<bigint>;
29
29
 
30
30
  /**
31
31
  * Mark the uncommitted changes in this TX as a checkpoint.