@aztec/simulator 0.55.0 → 0.56.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 (171) hide show
  1. package/dest/avm/avm_execution_environment.d.ts +0 -8
  2. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  3. package/dest/avm/avm_execution_environment.js +1 -19
  4. package/dest/avm/avm_gas.d.ts +2 -2
  5. package/dest/avm/avm_gas.d.ts.map +1 -1
  6. package/dest/avm/avm_gas.js +31 -115
  7. package/dest/avm/avm_machine_state.d.ts.map +1 -1
  8. package/dest/avm/avm_machine_state.js +6 -6
  9. package/dest/avm/avm_memory_types.d.ts +35 -7
  10. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  11. package/dest/avm/avm_memory_types.js +25 -11
  12. package/dest/avm/avm_simulator.d.ts.map +1 -1
  13. package/dest/avm/avm_simulator.js +5 -6
  14. package/dest/avm/bytecode_utils.d.ts +1 -2
  15. package/dest/avm/bytecode_utils.d.ts.map +1 -1
  16. package/dest/avm/bytecode_utils.js +3 -17
  17. package/dest/avm/fixtures/index.d.ts +4 -16
  18. package/dest/avm/fixtures/index.d.ts.map +1 -1
  19. package/dest/avm/fixtures/index.js +9 -25
  20. package/dest/avm/journal/index.d.ts +0 -1
  21. package/dest/avm/journal/index.d.ts.map +1 -1
  22. package/dest/avm/journal/index.js +1 -2
  23. package/dest/avm/journal/journal.d.ts +4 -4
  24. package/dest/avm/journal/journal.d.ts.map +1 -1
  25. package/dest/avm/journal/journal.js +14 -13
  26. package/dest/avm/opcodes/bitwise.d.ts +10 -4
  27. package/dest/avm/opcodes/bitwise.d.ts.map +1 -1
  28. package/dest/avm/opcodes/bitwise.js +17 -11
  29. package/dest/avm/opcodes/commitment.js +2 -2
  30. package/dest/avm/opcodes/comparators.js +2 -2
  31. package/dest/avm/opcodes/conversion.d.ts +3 -2
  32. package/dest/avm/opcodes/conversion.d.ts.map +1 -1
  33. package/dest/avm/opcodes/conversion.js +18 -11
  34. package/dest/avm/opcodes/environment_getters.d.ts +27 -63
  35. package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
  36. package/dest/avm/opcodes/environment_getters.js +75 -80
  37. package/dest/avm/opcodes/hashing.d.ts +7 -5
  38. package/dest/avm/opcodes/hashing.d.ts.map +1 -1
  39. package/dest/avm/opcodes/hashing.js +40 -24
  40. package/dest/avm/opcodes/index.d.ts +3 -0
  41. package/dest/avm/opcodes/index.d.ts.map +1 -1
  42. package/dest/avm/opcodes/index.js +4 -1
  43. package/dest/avm/opcodes/instruction_impl.d.ts +0 -26
  44. package/dest/avm/opcodes/instruction_impl.d.ts.map +1 -1
  45. package/dest/avm/opcodes/instruction_impl.js +1 -51
  46. package/dest/avm/opcodes/memory.d.ts +7 -2
  47. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  48. package/dest/avm/opcodes/memory.js +23 -6
  49. package/dest/avm/opcodes/multi_scalar_mul.d.ts.map +1 -1
  50. package/dest/avm/opcodes/multi_scalar_mul.js +7 -6
  51. package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  52. package/dest/avm/serialization/bytecode_serialization.js +5 -24
  53. package/dest/avm/serialization/instruction_serialization.d.ts +48 -60
  54. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  55. package/dest/avm/serialization/instruction_serialization.js +50 -63
  56. package/dest/avm/test_utils.d.ts +8 -8
  57. package/dest/avm/test_utils.d.ts.map +1 -1
  58. package/dest/avm/test_utils.js +15 -15
  59. package/dest/client/simulator.d.ts +1 -1
  60. package/dest/client/test_utils.d.ts.map +1 -1
  61. package/dest/client/test_utils.js +12 -6
  62. package/dest/client/unconstrained_execution.d.ts +2 -2
  63. package/dest/client/unconstrained_execution.d.ts.map +1 -1
  64. package/dest/client/unconstrained_execution.js +3 -3
  65. package/dest/public/enqueued_call_simulator.d.ts +43 -0
  66. package/dest/public/enqueued_call_simulator.d.ts.map +1 -0
  67. package/dest/public/enqueued_call_simulator.js +155 -0
  68. package/dest/public/enqueued_calls_processor.d.ts +44 -0
  69. package/dest/public/enqueued_calls_processor.d.ts.map +1 -0
  70. package/dest/public/enqueued_calls_processor.js +218 -0
  71. package/dest/public/execution.d.ts +4 -9
  72. package/dest/public/execution.d.ts.map +1 -1
  73. package/dest/public/execution.js +5 -10
  74. package/dest/public/executor.d.ts +3 -5
  75. package/dest/public/executor.d.ts.map +1 -1
  76. package/dest/public/executor.js +5 -9
  77. package/dest/public/hints_builder.d.ts +4 -3
  78. package/dest/public/hints_builder.d.ts.map +1 -1
  79. package/dest/public/hints_builder.js +4 -4
  80. package/dest/public/index.d.ts +3 -2
  81. package/dest/public/index.d.ts.map +1 -1
  82. package/dest/public/index.js +3 -3
  83. package/dest/public/public_db_sources.d.ts +17 -24
  84. package/dest/public/public_db_sources.d.ts.map +1 -1
  85. package/dest/public/public_db_sources.js +58 -65
  86. package/dest/public/public_kernel.d.ts +3 -9
  87. package/dest/public/public_kernel.d.ts.map +1 -1
  88. package/dest/public/public_kernel.js +13 -40
  89. package/dest/public/public_kernel_circuit_simulator.d.ts +5 -11
  90. package/dest/public/public_kernel_circuit_simulator.d.ts.map +1 -1
  91. package/dest/public/public_kernel_tail_simulator.d.ts +19 -0
  92. package/dest/public/public_kernel_tail_simulator.d.ts.map +1 -0
  93. package/dest/public/public_kernel_tail_simulator.js +45 -0
  94. package/dest/public/public_processor.d.ts +8 -5
  95. package/dest/public/public_processor.d.ts.map +1 -1
  96. package/dest/public/public_processor.js +31 -48
  97. package/dest/public/public_processor_metrics.d.ts +3 -3
  98. package/dest/public/public_processor_metrics.d.ts.map +1 -1
  99. package/dest/public/public_processor_metrics.js +1 -1
  100. package/dest/public/side_effect_trace.d.ts.map +1 -1
  101. package/dest/public/side_effect_trace.js +4 -8
  102. package/package.json +9 -9
  103. package/src/avm/avm_execution_environment.ts +1 -21
  104. package/src/avm/avm_gas.ts +30 -114
  105. package/src/avm/avm_machine_state.ts +5 -5
  106. package/src/avm/avm_memory_types.ts +31 -9
  107. package/src/avm/avm_simulator.ts +4 -5
  108. package/src/avm/bytecode_utils.ts +2 -17
  109. package/src/avm/fixtures/index.ts +12 -44
  110. package/src/avm/journal/index.ts +0 -1
  111. package/src/avm/journal/journal.ts +13 -18
  112. package/src/avm/opcodes/bitwise.ts +14 -10
  113. package/src/avm/opcodes/commitment.ts +1 -1
  114. package/src/avm/opcodes/comparators.ts +2 -2
  115. package/src/avm/opcodes/conversion.ts +19 -10
  116. package/src/avm/opcodes/environment_getters.ts +72 -99
  117. package/src/avm/opcodes/hashing.ts +47 -22
  118. package/src/avm/opcodes/index.ts +3 -0
  119. package/src/avm/opcodes/instruction_impl.ts +0 -62
  120. package/src/avm/opcodes/memory.ts +20 -6
  121. package/src/avm/opcodes/multi_scalar_mul.ts +6 -6
  122. package/src/avm/serialization/bytecode_serialization.ts +11 -33
  123. package/src/avm/serialization/instruction_serialization.ts +5 -18
  124. package/src/avm/test_utils.ts +20 -16
  125. package/src/client/test_utils.ts +36 -9
  126. package/src/client/unconstrained_execution.ts +3 -8
  127. package/src/public/enqueued_call_simulator.ts +336 -0
  128. package/src/public/enqueued_calls_processor.ts +388 -0
  129. package/src/public/execution.ts +5 -12
  130. package/src/public/executor.ts +4 -12
  131. package/src/public/hints_builder.ts +14 -12
  132. package/src/public/index.ts +3 -2
  133. package/src/public/public_db_sources.ts +62 -67
  134. package/src/public/public_kernel.ts +15 -49
  135. package/src/public/public_kernel_circuit_simulator.ts +6 -10
  136. package/src/public/public_kernel_tail_simulator.ts +106 -0
  137. package/src/public/public_processor.ts +61 -86
  138. package/src/public/public_processor_metrics.ts +3 -3
  139. package/src/public/side_effect_trace.ts +5 -12
  140. package/dest/avm/journal/host_storage.d.ts +0 -13
  141. package/dest/avm/journal/host_storage.d.ts.map +0 -1
  142. package/dest/avm/journal/host_storage.js +0 -13
  143. package/dest/avm/opcodes/context_getters.d.ts +0 -15
  144. package/dest/avm/opcodes/context_getters.d.ts.map +0 -1
  145. package/dest/avm/opcodes/context_getters.js +0 -19
  146. package/dest/public/abstract_phase_manager.d.ts +0 -93
  147. package/dest/public/abstract_phase_manager.d.ts.map +0 -1
  148. package/dest/public/abstract_phase_manager.js +0 -285
  149. package/dest/public/app_logic_phase_manager.d.ts +0 -25
  150. package/dest/public/app_logic_phase_manager.d.ts.map +0 -1
  151. package/dest/public/app_logic_phase_manager.js +0 -52
  152. package/dest/public/phase_manager_factory.d.ts +0 -18
  153. package/dest/public/phase_manager_factory.d.ts.map +0 -1
  154. package/dest/public/phase_manager_factory.js +0 -56
  155. package/dest/public/setup_phase_manager.d.ts +0 -25
  156. package/dest/public/setup_phase_manager.d.ts.map +0 -1
  157. package/dest/public/setup_phase_manager.js +0 -39
  158. package/dest/public/tail_phase_manager.d.ts +0 -23
  159. package/dest/public/tail_phase_manager.d.ts.map +0 -1
  160. package/dest/public/tail_phase_manager.js +0 -50
  161. package/dest/public/teardown_phase_manager.d.ts +0 -27
  162. package/dest/public/teardown_phase_manager.d.ts.map +0 -1
  163. package/dest/public/teardown_phase_manager.js +0 -56
  164. package/src/avm/journal/host_storage.ts +0 -14
  165. package/src/avm/opcodes/context_getters.ts +0 -23
  166. package/src/public/abstract_phase_manager.ts +0 -522
  167. package/src/public/app_logic_phase_manager.ts +0 -81
  168. package/src/public/phase_manager_factory.ts +0 -126
  169. package/src/public/setup_phase_manager.ts +0 -61
  170. package/src/public/tail_phase_manager.ts +0 -127
  171. package/src/public/teardown_phase_manager.ts +0 -86
@@ -38,7 +38,7 @@ export class ContractsDataSourcePublicDB implements PublicContractsDB {
38
38
 
39
39
  private log = createDebugLogger('aztec:sequencer:contracts-data-source');
40
40
 
41
- constructor(private db: ContractDataSource) {}
41
+ constructor(private dataSource: ContractDataSource) {}
42
42
 
43
43
  /**
44
44
  * Add new contracts from a transaction
@@ -78,11 +78,11 @@ export class ContractsDataSourcePublicDB implements PublicContractsDB {
78
78
  }
79
79
 
80
80
  public async getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
81
- return this.instanceCache.get(address.toString()) ?? (await this.db.getContract(address));
81
+ return this.instanceCache.get(address.toString()) ?? (await this.dataSource.getContract(address));
82
82
  }
83
83
 
84
84
  public async getContractClass(contractClassId: Fr): Promise<ContractClassPublic | undefined> {
85
- return this.classCache.get(contractClassId.toString()) ?? (await this.db.getContractClass(contractClassId));
85
+ return this.classCache.get(contractClassId.toString()) ?? (await this.dataSource.getContractClass(contractClassId));
86
86
  }
87
87
 
88
88
  async getBytecode(address: AztecAddress, selector: FunctionSelector): Promise<Buffer | undefined> {
@@ -98,7 +98,7 @@ export class ContractsDataSourcePublicDB implements PublicContractsDB {
98
98
  }
99
99
 
100
100
  public async getDebugFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
101
- const artifact = await this.db.getContractArtifact(address);
101
+ const artifact = await this.dataSource.getContractArtifact(address);
102
102
  if (!artifact) {
103
103
  return Promise.resolve(undefined);
104
104
  }
@@ -115,14 +115,18 @@ export class ContractsDataSourcePublicDB implements PublicContractsDB {
115
115
  }
116
116
 
117
117
  /**
118
- * Implements the PublicStateDB using a world-state database.
118
+ * A public state DB that reads and writes to the world state.
119
119
  */
120
- export class WorldStatePublicDB implements PublicStateDB {
121
- private committedWriteCache: Map<bigint, Fr> = new Map();
122
- private checkpointedWriteCache: Map<bigint, Fr> = new Map();
123
- private uncommittedWriteCache: Map<bigint, Fr> = new Map();
120
+ export class WorldStateDB extends ContractsDataSourcePublicDB implements PublicStateDB, CommitmentsDB {
121
+ private logger = createDebugLogger('aztec:sequencer:world-state-db');
124
122
 
125
- constructor(private db: MerkleTreeOperations) {}
123
+ private publicCommittedWriteCache: Map<bigint, Fr> = new Map();
124
+ private publicCheckpointedWriteCache: Map<bigint, Fr> = new Map();
125
+ private publicUncommittedWriteCache: Map<bigint, Fr> = new Map();
126
+
127
+ constructor(private db: MerkleTreeOperations, dataSource: ContractDataSource) {
128
+ super(dataSource);
129
+ }
126
130
 
127
131
  /**
128
132
  * Reads a value from public storage, returning zero if none.
@@ -132,15 +136,15 @@ export class WorldStatePublicDB implements PublicStateDB {
132
136
  */
133
137
  public async storageRead(contract: AztecAddress, slot: Fr): Promise<Fr> {
134
138
  const leafSlot = computePublicDataTreeLeafSlot(contract, slot).value;
135
- const uncommitted = this.uncommittedWriteCache.get(leafSlot);
139
+ const uncommitted = this.publicUncommittedWriteCache.get(leafSlot);
136
140
  if (uncommitted !== undefined) {
137
141
  return uncommitted;
138
142
  }
139
- const checkpointed = this.checkpointedWriteCache.get(leafSlot);
143
+ const checkpointed = this.publicCheckpointedWriteCache.get(leafSlot);
140
144
  if (checkpointed !== undefined) {
141
145
  return checkpointed;
142
146
  }
143
- const committed = this.committedWriteCache.get(leafSlot);
147
+ const committed = this.publicCommittedWriteCache.get(leafSlot);
144
148
  if (committed !== undefined) {
145
149
  return committed;
146
150
  }
@@ -167,57 +171,10 @@ export class WorldStatePublicDB implements PublicStateDB {
167
171
  */
168
172
  public storageWrite(contract: AztecAddress, slot: Fr, newValue: Fr): Promise<bigint> {
169
173
  const index = computePublicDataTreeLeafSlot(contract, slot).value;
170
- this.uncommittedWriteCache.set(index, newValue);
174
+ this.publicUncommittedWriteCache.set(index, newValue);
171
175
  return Promise.resolve(index);
172
176
  }
173
177
 
174
- /**
175
- * Commit the pending changes to the DB.
176
- * @returns Nothing.
177
- */
178
- commit(): Promise<void> {
179
- for (const [k, v] of this.checkpointedWriteCache) {
180
- this.committedWriteCache.set(k, v);
181
- }
182
- // uncommitted writes take precedence over checkpointed writes
183
- // since they are the most recent
184
- for (const [k, v] of this.uncommittedWriteCache) {
185
- this.committedWriteCache.set(k, v);
186
- }
187
- return this.rollbackToCommit();
188
- }
189
-
190
- /**
191
- * Rollback the pending changes.
192
- * @returns Nothing.
193
- */
194
- async rollbackToCommit(): Promise<void> {
195
- await this.rollbackToCheckpoint();
196
- this.checkpointedWriteCache = new Map<bigint, Fr>();
197
- return Promise.resolve();
198
- }
199
-
200
- checkpoint(): Promise<void> {
201
- for (const [k, v] of this.uncommittedWriteCache) {
202
- this.checkpointedWriteCache.set(k, v);
203
- }
204
- return this.rollbackToCheckpoint();
205
- }
206
-
207
- rollbackToCheckpoint(): Promise<void> {
208
- this.uncommittedWriteCache = new Map<bigint, Fr>();
209
- return Promise.resolve();
210
- }
211
- }
212
-
213
- /**
214
- * Implements WorldState db using a world state database.
215
- */
216
- export class WorldStateDB implements CommitmentsDB {
217
- private log = createDebugLogger('aztec:sequencer:world-state-db');
218
-
219
- constructor(private db: MerkleTreeOperations) {}
220
-
221
178
  public async getNullifierMembershipWitnessAtLatestBlock(
222
179
  nullifier: Fr,
223
180
  ): Promise<NullifierMembershipWitness | undefined> {
@@ -239,7 +196,7 @@ export class WorldStateDB implements CommitmentsDB {
239
196
  return undefined;
240
197
  }
241
198
 
242
- this.log.debug(`[DB] Fetched nullifier membership`, {
199
+ this.logger.debug(`[DB] Fetched nullifier membership`, {
243
200
  eventName: 'public-db-access',
244
201
  duration: timer.ms(),
245
202
  operation: 'get-nullifier-membership-witness-at-latest-block',
@@ -277,7 +234,7 @@ export class WorldStateDB implements CommitmentsDB {
277
234
  messageIndex,
278
235
  );
279
236
 
280
- this.log.debug(`[DB] Fetched L1 to L2 message membership`, {
237
+ this.logger.debug(`[DB] Fetched L1 to L2 message membership`, {
281
238
  eventName: 'public-db-access',
282
239
  duration: timer.ms(),
283
240
  operation: 'get-l1-to-l2-message-membership-witness',
@@ -289,7 +246,7 @@ export class WorldStateDB implements CommitmentsDB {
289
246
  public async getL1ToL2LeafValue(leafIndex: bigint): Promise<Fr | undefined> {
290
247
  const timer = new Timer();
291
248
  const leafValue = await this.db.getLeafValue(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, leafIndex);
292
- this.log.debug(`[DB] Fetched L1 to L2 message leaf value`, {
249
+ this.logger.debug(`[DB] Fetched L1 to L2 message leaf value`, {
293
250
  eventName: 'public-db-access',
294
251
  duration: timer.ms(),
295
252
  operation: 'get-l1-to-l2-message-leaf-value',
@@ -300,7 +257,7 @@ export class WorldStateDB implements CommitmentsDB {
300
257
  public async getCommitmentIndex(commitment: Fr): Promise<bigint | undefined> {
301
258
  const timer = new Timer();
302
259
  const index = await this.db.findLeafIndex(MerkleTreeId.NOTE_HASH_TREE, commitment);
303
- this.log.debug(`[DB] Fetched commitment index`, {
260
+ this.logger.debug(`[DB] Fetched commitment index`, {
304
261
  eventName: 'public-db-access',
305
262
  duration: timer.ms(),
306
263
  operation: 'get-commitment-index',
@@ -311,7 +268,7 @@ export class WorldStateDB implements CommitmentsDB {
311
268
  public async getCommitmentValue(leafIndex: bigint): Promise<Fr | undefined> {
312
269
  const timer = new Timer();
313
270
  const leafValue = await this.db.getLeafValue(MerkleTreeId.NOTE_HASH_TREE, leafIndex);
314
- this.log.debug(`[DB] Fetched commitment leaf value`, {
271
+ this.logger.debug(`[DB] Fetched commitment leaf value`, {
315
272
  eventName: 'public-db-access',
316
273
  duration: timer.ms(),
317
274
  operation: 'get-commitment-leaf-value',
@@ -322,11 +279,49 @@ export class WorldStateDB implements CommitmentsDB {
322
279
  public async getNullifierIndex(nullifier: Fr): Promise<bigint | undefined> {
323
280
  const timer = new Timer();
324
281
  const index = await this.db.findLeafIndex(MerkleTreeId.NULLIFIER_TREE, nullifier.toBuffer());
325
- this.log.debug(`[DB] Fetched nullifier index`, {
282
+ this.logger.debug(`[DB] Fetched nullifier index`, {
326
283
  eventName: 'public-db-access',
327
284
  duration: timer.ms(),
328
285
  operation: 'get-nullifier-index',
329
286
  } satisfies PublicDBAccessStats);
330
287
  return index;
331
288
  }
289
+
290
+ /**
291
+ * Commit the pending public changes to the DB.
292
+ * @returns Nothing.
293
+ */
294
+ commit(): Promise<void> {
295
+ for (const [k, v] of this.publicCheckpointedWriteCache) {
296
+ this.publicCommittedWriteCache.set(k, v);
297
+ }
298
+ // uncommitted writes take precedence over checkpointed writes
299
+ // since they are the most recent
300
+ for (const [k, v] of this.publicUncommittedWriteCache) {
301
+ this.publicCommittedWriteCache.set(k, v);
302
+ }
303
+ return this.rollbackToCommit();
304
+ }
305
+
306
+ /**
307
+ * Rollback the pending public changes.
308
+ * @returns Nothing.
309
+ */
310
+ async rollbackToCommit(): Promise<void> {
311
+ await this.rollbackToCheckpoint();
312
+ this.publicCheckpointedWriteCache = new Map<bigint, Fr>();
313
+ return Promise.resolve();
314
+ }
315
+
316
+ checkpoint(): Promise<void> {
317
+ for (const [k, v] of this.publicUncommittedWriteCache) {
318
+ this.publicCheckpointedWriteCache.set(k, v);
319
+ }
320
+ return this.rollbackToCheckpoint();
321
+ }
322
+
323
+ rollbackToCheckpoint(): Promise<void> {
324
+ this.publicUncommittedWriteCache = new Map<bigint, Fr>();
325
+ return Promise.resolve();
326
+ }
332
327
  }
@@ -3,7 +3,9 @@ import {
3
3
  type KernelCircuitPublicInputs,
4
4
  type PublicKernelCircuitPrivateInputs,
5
5
  type PublicKernelCircuitPublicInputs,
6
+ type PublicKernelInnerCircuitPrivateInputs,
6
7
  type PublicKernelTailCircuitPrivateInputs,
8
+ type VMCircuitPublicInputs,
7
9
  } from '@aztec/circuits.js';
8
10
  import { createDebugLogger } from '@aztec/foundation/log';
9
11
  import { elapsed } from '@aztec/foundation/timer';
@@ -11,12 +13,10 @@ import {
11
13
  SimulatedServerCircuitArtifacts,
12
14
  convertSimulatedPublicInnerInputsToWitnessMap,
13
15
  convertSimulatedPublicInnerOutputFromWitnessMap,
14
- convertSimulatedPublicSetupInputsToWitnessMap,
15
- convertSimulatedPublicSetupOutputFromWitnessMap,
16
+ convertSimulatedPublicMergeInputsToWitnessMap,
17
+ convertSimulatedPublicMergeOutputFromWitnessMap,
16
18
  convertSimulatedPublicTailInputsToWitnessMap,
17
19
  convertSimulatedPublicTailOutputFromWitnessMap,
18
- convertSimulatedPublicTeardownInputsToWitnessMap,
19
- convertSimulatedPublicTeardownOutputFromWitnessMap,
20
20
  } from '@aztec/noir-protocol-circuits-types';
21
21
 
22
22
  import { WASMSimulator } from '../providers/acvm_wasm.js';
@@ -39,46 +39,15 @@ export class RealPublicKernelCircuitSimulator implements PublicKernelCircuitSimu
39
39
  * @param input - Inputs to the circuit.
40
40
  * @returns The public inputs as outputs of the simulation.
41
41
  */
42
- public async publicKernelCircuitSetup(
43
- input: PublicKernelCircuitPrivateInputs,
44
- ): Promise<PublicKernelCircuitPublicInputs> {
45
- if (!input.previousKernel.publicInputs.needsSetup) {
46
- throw new Error(`Expected previous kernel inputs to need setup`);
47
- }
48
- const inputWitness = convertSimulatedPublicSetupInputsToWitnessMap(input);
49
- const [duration, witness] = await elapsed(() =>
50
- this.wasmSimulator.simulateCircuit(inputWitness, SimulatedServerCircuitArtifacts.PublicKernelSetupArtifact),
51
- );
52
- const result = convertSimulatedPublicSetupOutputFromWitnessMap(witness);
53
- this.log.debug(`Simulated public kernel setup circuit`, {
54
- eventName: 'circuit-simulation',
55
- circuitName: 'public-kernel-setup',
56
- duration,
57
- inputSize: input.toBuffer().length,
58
- outputSize: result.toBuffer().length,
59
- } satisfies CircuitSimulationStats);
60
- return result;
61
- }
62
-
63
- /**
64
- * Simulates the public kernel app logic circuit from its inputs.
65
- * @param input - Inputs to the circuit.
66
- * @returns The public inputs as outputs of the simulation.
67
- */
68
- public async publicKernelCircuitAppLogic(
69
- input: PublicKernelCircuitPrivateInputs,
70
- ): Promise<PublicKernelCircuitPublicInputs> {
71
- if (!input.previousKernel.publicInputs.needsAppLogic) {
72
- throw new Error(`Expected previous kernel inputs to need app logic`);
73
- }
42
+ public async publicKernelCircuitInner(input: PublicKernelInnerCircuitPrivateInputs): Promise<VMCircuitPublicInputs> {
74
43
  const inputWitness = convertSimulatedPublicInnerInputsToWitnessMap(input);
75
44
  const [duration, witness] = await elapsed(() =>
76
- this.wasmSimulator.simulateCircuit(inputWitness, SimulatedServerCircuitArtifacts.PublicKernelAppLogicArtifact),
45
+ this.wasmSimulator.simulateCircuit(inputWitness, SimulatedServerCircuitArtifacts.PublicKernelInnerArtifact),
77
46
  );
78
47
  const result = convertSimulatedPublicInnerOutputFromWitnessMap(witness);
79
- this.log.debug(`Simulated public kernel app logic circuit`, {
48
+ this.log.debug(`Simulated public kernel inner circuit`, {
80
49
  eventName: 'circuit-simulation',
81
- circuitName: 'public-kernel-app-logic',
50
+ circuitName: 'public-kernel-inner',
82
51
  duration,
83
52
  inputSize: input.toBuffer().length,
84
53
  outputSize: result.toBuffer().length,
@@ -87,24 +56,21 @@ export class RealPublicKernelCircuitSimulator implements PublicKernelCircuitSimu
87
56
  }
88
57
 
89
58
  /**
90
- * Simulates the public kernel teardown circuit from its inputs.
59
+ * Simulates the public kernel app logic circuit from its inputs.
91
60
  * @param input - Inputs to the circuit.
92
61
  * @returns The public inputs as outputs of the simulation.
93
62
  */
94
- public async publicKernelCircuitTeardown(
63
+ public async publicKernelCircuitMerge(
95
64
  input: PublicKernelCircuitPrivateInputs,
96
65
  ): Promise<PublicKernelCircuitPublicInputs> {
97
- if (!input.previousKernel.publicInputs.needsTeardown) {
98
- throw new Error(`Expected previous kernel inputs to need teardown`);
99
- }
100
- const inputWitness = convertSimulatedPublicTeardownInputsToWitnessMap(input);
66
+ const inputWitness = convertSimulatedPublicMergeInputsToWitnessMap(input);
101
67
  const [duration, witness] = await elapsed(() =>
102
- this.wasmSimulator.simulateCircuit(inputWitness, SimulatedServerCircuitArtifacts.PublicKernelTeardownArtifact),
68
+ this.wasmSimulator.simulateCircuit(inputWitness, SimulatedServerCircuitArtifacts.PublicKernelMergeArtifact),
103
69
  );
104
- const result = convertSimulatedPublicTeardownOutputFromWitnessMap(witness);
105
- this.log.debug(`Simulated public kernel teardown circuit`, {
70
+ const result = convertSimulatedPublicMergeOutputFromWitnessMap(witness);
71
+ this.log.debug(`Simulated public kernel merge circuit`, {
106
72
  eventName: 'circuit-simulation',
107
- circuitName: 'public-kernel-teardown',
73
+ circuitName: 'public-kernel-merge',
108
74
  duration,
109
75
  inputSize: input.toBuffer().length,
110
76
  outputSize: result.toBuffer().length,
@@ -2,7 +2,9 @@ import {
2
2
  type KernelCircuitPublicInputs,
3
3
  type PublicKernelCircuitPrivateInputs,
4
4
  type PublicKernelCircuitPublicInputs,
5
+ type PublicKernelInnerCircuitPrivateInputs,
5
6
  type PublicKernelTailCircuitPrivateInputs,
7
+ type VMCircuitPublicInputs,
6
8
  } from '@aztec/circuits.js';
7
9
 
8
10
  /**
@@ -10,23 +12,17 @@ import {
10
12
  */
11
13
  export interface PublicKernelCircuitSimulator {
12
14
  /**
13
- * Simulates the public kernel setup circuit from its inputs.
15
+ * Simulates the public kernel inner circuit from its inputs.
14
16
  * @param inputs - Inputs to the circuit.
15
17
  * @returns The public inputs as outputs of the simulation.
16
18
  */
17
- publicKernelCircuitSetup(inputs: PublicKernelCircuitPrivateInputs): Promise<PublicKernelCircuitPublicInputs>;
19
+ publicKernelCircuitInner(inputs: PublicKernelInnerCircuitPrivateInputs): Promise<VMCircuitPublicInputs>;
18
20
  /**
19
- * Simulates the public kernel app logic circuit from its inputs.
21
+ * Simulates the public kernel merge circuit from its inputs.
20
22
  * @param inputs - Inputs to the circuit.
21
23
  * @returns The public inputs as outputs of the simulation.
22
24
  */
23
- publicKernelCircuitAppLogic(inputs: PublicKernelCircuitPrivateInputs): Promise<PublicKernelCircuitPublicInputs>;
24
- /**
25
- * Simulates the public kernel teardown circuit from its inputs.
26
- * @param inputs - Inputs to the circuit.
27
- * @returns The public inputs as outputs of the simulation.
28
- */
29
- publicKernelCircuitTeardown(inputs: PublicKernelCircuitPrivateInputs): Promise<PublicKernelCircuitPublicInputs>;
25
+ publicKernelCircuitMerge(inputs: PublicKernelCircuitPrivateInputs): Promise<PublicKernelCircuitPublicInputs>;
30
26
  /**
31
27
  * Simulates the public kernel tail circuit from its inputs.
32
28
  * @param inputs - Inputs to the circuit.
@@ -0,0 +1,106 @@
1
+ import { ProvingRequestType, type PublicKernelTailRequest } from '@aztec/circuit-types';
2
+ import {
3
+ type KernelCircuitPublicInputs,
4
+ MAX_NULLIFIERS_PER_TX,
5
+ MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
6
+ NESTED_RECURSIVE_PROOF_LENGTH,
7
+ type PublicKernelCircuitPublicInputs,
8
+ PublicKernelData,
9
+ PublicKernelTailCircuitPrivateInputs,
10
+ makeEmptyRecursiveProof,
11
+ mergeAccumulatedData,
12
+ } from '@aztec/circuits.js';
13
+ import { ProtocolCircuitVks, getVKIndex, getVKSiblingPath } from '@aztec/noir-protocol-circuits-types';
14
+ import { type MerkleTreeOperations } from '@aztec/world-state';
15
+
16
+ import { HintsBuilder } from './hints_builder.js';
17
+ import { type PublicKernelCircuitSimulator } from './public_kernel_circuit_simulator.js';
18
+
19
+ export class PublicKernelTailSimulator {
20
+ constructor(
21
+ private db: MerkleTreeOperations,
22
+ private publicKernelSimulator: PublicKernelCircuitSimulator,
23
+ private hintsBuilder: HintsBuilder,
24
+ ) {}
25
+
26
+ static create(db: MerkleTreeOperations, publicKernelSimulator: PublicKernelCircuitSimulator) {
27
+ const hintsBuilder = new HintsBuilder(db);
28
+ return new PublicKernelTailSimulator(db, publicKernelSimulator, hintsBuilder);
29
+ }
30
+
31
+ async simulate(
32
+ previousOutput: PublicKernelCircuitPublicInputs,
33
+ ): Promise<{ output: KernelCircuitPublicInputs; provingRequest: PublicKernelTailRequest }> {
34
+ const inputs = await this.buildPrivateInputs(previousOutput);
35
+
36
+ const output = await this.publicKernelSimulator.publicKernelCircuitTail(inputs);
37
+
38
+ const provingRequest: PublicKernelTailRequest = {
39
+ type: ProvingRequestType.PUBLIC_KERNEL_TAIL,
40
+ inputs,
41
+ };
42
+
43
+ return { output, provingRequest };
44
+ }
45
+
46
+ private async buildPrivateInputs(previousOutput: PublicKernelCircuitPublicInputs) {
47
+ const previousKernel = this.getPreviousKernelData(previousOutput);
48
+
49
+ const { validationRequests, endNonRevertibleData: nonRevertibleData, end: revertibleData } = previousOutput;
50
+
51
+ const noteHashReadRequestHints = await this.hintsBuilder.getNoteHashReadRequestsHints(
52
+ validationRequests.noteHashReadRequests,
53
+ );
54
+
55
+ const pendingNullifiers = mergeAccumulatedData(
56
+ nonRevertibleData.nullifiers,
57
+ revertibleData.nullifiers,
58
+ MAX_NULLIFIERS_PER_TX,
59
+ );
60
+
61
+ const nullifierReadRequestHints = await this.hintsBuilder.getNullifierReadRequestHints(
62
+ validationRequests.nullifierReadRequests,
63
+ pendingNullifiers,
64
+ );
65
+
66
+ const nullifierNonExistentReadRequestHints = await this.hintsBuilder.getNullifierNonExistentReadRequestHints(
67
+ validationRequests.nullifierNonExistentReadRequests,
68
+ pendingNullifiers,
69
+ );
70
+
71
+ const l1ToL2MsgReadRequestHints = await this.hintsBuilder.getL1ToL2MsgReadRequestsHints(
72
+ validationRequests.l1ToL2MsgReadRequests,
73
+ );
74
+
75
+ const pendingPublicDataWrites = mergeAccumulatedData(
76
+ nonRevertibleData.publicDataUpdateRequests,
77
+ revertibleData.publicDataUpdateRequests,
78
+ MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
79
+ );
80
+
81
+ const publicDataHints = await this.hintsBuilder.getPublicDataHints(
82
+ validationRequests.publicDataReads,
83
+ pendingPublicDataWrites,
84
+ );
85
+
86
+ const currentState = await this.db.getStateReference();
87
+
88
+ return new PublicKernelTailCircuitPrivateInputs(
89
+ previousKernel,
90
+ noteHashReadRequestHints,
91
+ nullifierReadRequestHints,
92
+ nullifierNonExistentReadRequestHints,
93
+ l1ToL2MsgReadRequestHints,
94
+ publicDataHints,
95
+ currentState.partial,
96
+ );
97
+ }
98
+
99
+ private getPreviousKernelData(previousOutput: PublicKernelCircuitPublicInputs): PublicKernelData {
100
+ const proof = makeEmptyRecursiveProof(NESTED_RECURSIVE_PROOF_LENGTH);
101
+ const vk = ProtocolCircuitVks.PublicKernelMergeArtifact;
102
+ const vkIndex = getVKIndex(vk);
103
+ const siblingPath = getVKSiblingPath(vkIndex);
104
+ return new PublicKernelData(previousOutput, proof, vk, vkIndex, siblingPath);
105
+ }
106
+ }