@aztec/simulator 0.33.0 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. package/dest/acvm/acvm.d.ts +4 -1
  2. package/dest/acvm/acvm.d.ts.map +1 -1
  3. package/dest/acvm/acvm.js +5 -5
  4. package/dest/acvm/deserialize.d.ts +3 -5
  5. package/dest/acvm/deserialize.d.ts.map +1 -1
  6. package/dest/acvm/deserialize.js +6 -9
  7. package/dest/acvm/oracle/oracle.d.ts +3 -1
  8. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  9. package/dest/acvm/oracle/oracle.js +14 -6
  10. package/dest/acvm/oracle/typed_oracle.d.ts +2 -0
  11. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  12. package/dest/acvm/oracle/typed_oracle.js +7 -1
  13. package/dest/avm/avm_execution_environment.d.ts +6 -3
  14. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  15. package/dest/avm/avm_execution_environment.js +12 -9
  16. package/dest/avm/avm_gas.js +2 -2
  17. package/dest/avm/avm_machine_state.d.ts +2 -0
  18. package/dest/avm/avm_machine_state.d.ts.map +1 -1
  19. package/dest/avm/avm_machine_state.js +24 -6
  20. package/dest/avm/avm_memory_types.js +6 -6
  21. package/dest/avm/avm_simulator.js +7 -7
  22. package/dest/avm/fixtures/index.d.ts +3 -0
  23. package/dest/avm/fixtures/index.d.ts.map +1 -1
  24. package/dest/avm/fixtures/index.js +11 -4
  25. package/dest/avm/journal/journal.d.ts +15 -13
  26. package/dest/avm/journal/journal.d.ts.map +1 -1
  27. package/dest/avm/journal/journal.js +16 -5
  28. package/dest/avm/journal/trace.d.ts +8 -19
  29. package/dest/avm/journal/trace.d.ts.map +1 -1
  30. package/dest/avm/journal/trace.js +48 -116
  31. package/dest/avm/journal/trace_types.d.ts +23 -4
  32. package/dest/avm/journal/trace_types.d.ts.map +1 -1
  33. package/dest/avm/opcodes/accrued_substate.d.ts +2 -1
  34. package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
  35. package/dest/avm/opcodes/accrued_substate.js +14 -6
  36. package/dest/avm/opcodes/context_getters.js +2 -2
  37. package/dest/avm/opcodes/external_calls.d.ts +2 -2
  38. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  39. package/dest/avm/opcodes/external_calls.js +26 -10
  40. package/dest/avm/opcodes/hashing.d.ts +8 -8
  41. package/dest/avm/opcodes/hashing.d.ts.map +1 -1
  42. package/dest/avm/opcodes/hashing.js +35 -43
  43. package/dest/avm/serialization/instruction_serialization.d.ts +1 -1
  44. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  45. package/dest/avm/serialization/instruction_serialization.js +2 -2
  46. package/dest/client/client_execution_context.d.ts +14 -4
  47. package/dest/client/client_execution_context.d.ts.map +1 -1
  48. package/dest/client/client_execution_context.js +28 -13
  49. package/dest/client/private_execution.d.ts +1 -1
  50. package/dest/client/private_execution.d.ts.map +1 -1
  51. package/dest/client/private_execution.js +10 -8
  52. package/dest/client/simulator.d.ts.map +1 -1
  53. package/dest/client/simulator.js +6 -5
  54. package/dest/client/unconstrained_execution.d.ts +1 -1
  55. package/dest/client/unconstrained_execution.d.ts.map +1 -1
  56. package/dest/client/unconstrained_execution.js +6 -5
  57. package/dest/client/view_data_oracle.d.ts +2 -2
  58. package/dest/client/view_data_oracle.d.ts.map +1 -1
  59. package/dest/client/view_data_oracle.js +2 -2
  60. package/dest/common/index.d.ts +1 -1
  61. package/dest/common/index.d.ts.map +1 -1
  62. package/dest/common/index.js +2 -2
  63. package/dest/common/packed_values_cache.d.ts +28 -0
  64. package/dest/common/packed_values_cache.d.ts.map +1 -0
  65. package/dest/common/packed_values_cache.js +50 -0
  66. package/dest/index.d.ts +1 -0
  67. package/dest/index.d.ts.map +1 -1
  68. package/dest/index.js +2 -1
  69. package/dest/mocks/fixtures.d.ts +42 -0
  70. package/dest/mocks/fixtures.d.ts.map +1 -0
  71. package/dest/mocks/fixtures.js +84 -0
  72. package/dest/mocks/index.d.ts +2 -0
  73. package/dest/mocks/index.d.ts.map +1 -0
  74. package/dest/mocks/index.js +2 -0
  75. package/dest/public/abstract_phase_manager.d.ts +82 -0
  76. package/dest/public/abstract_phase_manager.d.ts.map +1 -0
  77. package/dest/public/abstract_phase_manager.js +320 -0
  78. package/dest/public/app_logic_phase_manager.d.ts +29 -0
  79. package/dest/public/app_logic_phase_manager.d.ts.map +1 -0
  80. package/dest/public/app_logic_phase_manager.js +50 -0
  81. package/dest/public/execution.d.ts +3 -0
  82. package/dest/public/execution.d.ts.map +1 -1
  83. package/dest/public/execution.js +1 -1
  84. package/dest/public/executor.d.ts +1 -15
  85. package/dest/public/executor.d.ts.map +1 -1
  86. package/dest/public/executor.js +80 -83
  87. package/dest/public/hints_builder.d.ts +23 -0
  88. package/dest/public/hints_builder.d.ts.map +1 -0
  89. package/dest/public/hints_builder.js +62 -0
  90. package/dest/public/index.d.ts +5 -0
  91. package/dest/public/index.d.ts.map +1 -1
  92. package/dest/public/index.js +6 -1
  93. package/dest/public/phase_manager_factory.d.ts +18 -0
  94. package/dest/public/phase_manager_factory.d.ts.map +1 -0
  95. package/dest/public/phase_manager_factory.js +56 -0
  96. package/dest/public/public_execution_context.d.ts +20 -10
  97. package/dest/public/public_execution_context.d.ts.map +1 -1
  98. package/dest/public/public_execution_context.js +35 -23
  99. package/dest/public/public_executor.d.ts +79 -0
  100. package/dest/public/public_executor.d.ts.map +1 -0
  101. package/dest/public/public_executor.js +198 -0
  102. package/dest/public/public_kernel.d.ts +37 -0
  103. package/dest/public/public_kernel.d.ts.map +1 -0
  104. package/dest/public/public_kernel.js +97 -0
  105. package/dest/public/public_kernel_circuit_simulator.d.ts +31 -0
  106. package/dest/public/public_kernel_circuit_simulator.d.ts.map +1 -0
  107. package/dest/public/public_kernel_circuit_simulator.js +2 -0
  108. package/dest/public/public_processor.d.ts +53 -0
  109. package/dest/public/public_processor.d.ts.map +1 -0
  110. package/dest/public/public_processor.js +144 -0
  111. package/dest/public/setup_phase_manager.d.ts +30 -0
  112. package/dest/public/setup_phase_manager.d.ts.map +1 -0
  113. package/dest/public/setup_phase_manager.js +46 -0
  114. package/dest/public/tail_phase_manager.d.ts +30 -0
  115. package/dest/public/tail_phase_manager.d.ts.map +1 -0
  116. package/dest/public/tail_phase_manager.js +60 -0
  117. package/dest/public/teardown_phase_manager.d.ts +30 -0
  118. package/dest/public/teardown_phase_manager.d.ts.map +1 -0
  119. package/dest/public/teardown_phase_manager.js +46 -0
  120. package/dest/public/transitional_adaptors.d.ts +33 -0
  121. package/dest/public/transitional_adaptors.d.ts.map +1 -0
  122. package/dest/public/transitional_adaptors.js +162 -0
  123. package/dest/public/utils.d.ts +8 -0
  124. package/dest/public/utils.d.ts.map +1 -0
  125. package/dest/public/utils.js +29 -0
  126. package/dest/simulator/acvm_native.d.ts +19 -3
  127. package/dest/simulator/acvm_native.d.ts.map +1 -1
  128. package/dest/simulator/acvm_native.js +75 -48
  129. package/dest/simulator/acvm_wasm.d.ts.map +1 -1
  130. package/dest/simulator/acvm_wasm.js +3 -4
  131. package/package.json +8 -5
  132. package/src/acvm/acvm.ts +8 -5
  133. package/src/acvm/deserialize.ts +5 -9
  134. package/src/acvm/oracle/oracle.ts +15 -5
  135. package/src/acvm/oracle/typed_oracle.ts +8 -0
  136. package/src/avm/avm_execution_environment.ts +17 -17
  137. package/src/avm/avm_gas.ts +1 -1
  138. package/src/avm/avm_machine_state.ts +26 -5
  139. package/src/avm/avm_memory_types.ts +5 -5
  140. package/src/avm/avm_simulator.ts +6 -6
  141. package/src/avm/fixtures/index.ts +14 -1
  142. package/src/avm/journal/journal.ts +37 -17
  143. package/src/avm/journal/trace.ts +59 -121
  144. package/src/avm/journal/trace_types.ts +39 -39
  145. package/src/avm/opcodes/accrued_substate.ts +17 -5
  146. package/src/avm/opcodes/context_getters.ts +1 -1
  147. package/src/avm/opcodes/external_calls.ts +32 -9
  148. package/src/avm/opcodes/hashing.ts +38 -54
  149. package/src/avm/serialization/instruction_serialization.ts +1 -1
  150. package/src/client/client_execution_context.ts +30 -11
  151. package/src/client/private_execution.ts +9 -8
  152. package/src/client/simulator.ts +7 -3
  153. package/src/client/unconstrained_execution.ts +5 -4
  154. package/src/client/view_data_oracle.ts +1 -1
  155. package/src/common/index.ts +1 -1
  156. package/src/common/packed_values_cache.ts +55 -0
  157. package/src/index.ts +1 -0
  158. package/src/mocks/fixtures.ts +169 -0
  159. package/src/mocks/index.ts +1 -0
  160. package/src/public/abstract_phase_manager.ts +571 -0
  161. package/src/public/app_logic_phase_manager.ts +76 -0
  162. package/src/public/execution.ts +4 -0
  163. package/src/public/executor.ts +93 -93
  164. package/src/public/hints_builder.ts +119 -0
  165. package/src/public/index.ts +5 -0
  166. package/src/public/phase_manager_factory.ts +126 -0
  167. package/src/public/public_execution_context.ts +43 -37
  168. package/src/public/public_executor.ts +267 -0
  169. package/src/public/public_kernel.ts +139 -0
  170. package/src/public/public_kernel_circuit_simulator.ts +36 -0
  171. package/src/public/public_processor.ts +212 -0
  172. package/src/public/setup_phase_manager.ts +66 -0
  173. package/src/public/tail_phase_manager.ts +120 -0
  174. package/src/public/teardown_phase_manager.ts +66 -0
  175. package/src/public/transitional_adaptors.ts +249 -0
  176. package/src/public/utils.ts +31 -0
  177. package/src/simulator/acvm_native.ts +94 -47
  178. package/src/simulator/acvm_wasm.ts +7 -3
  179. package/dest/avm/temporary_executor_migration.d.ts +0 -27
  180. package/dest/avm/temporary_executor_migration.d.ts.map +0 -1
  181. package/dest/avm/temporary_executor_migration.js +0 -94
  182. package/dest/common/packed_args_cache.d.ts +0 -28
  183. package/dest/common/packed_args_cache.d.ts.map +0 -1
  184. package/dest/common/packed_args_cache.js +0 -50
  185. package/src/avm/temporary_executor_migration.ts +0 -136
  186. package/src/common/packed_args_cache.ts +0 -55
@@ -1,23 +1,28 @@
1
1
  import { Fr } from '@aztec/foundation/fields';
2
2
 
3
- import { type TracedL1toL2MessageCheck, type TracedNoteHashCheck, type TracedNullifierCheck } from './trace_types.js';
3
+ import {
4
+ type TracedL1toL2MessageCheck,
5
+ type TracedNoteHash,
6
+ type TracedNoteHashCheck,
7
+ type TracedNullifier,
8
+ type TracedNullifierCheck,
9
+ type TracedPublicStorageRead,
10
+ type TracedPublicStorageWrite,
11
+ } from './trace_types.js';
4
12
 
5
13
  export class WorldStateAccessTrace {
6
14
  public accessCounter: number;
7
- //public contractCalls: Array<TracedContractCall> = [];
8
15
 
9
- //public publicStorageReads: Array<TracedPublicStorageRead> = [];
10
- public publicStorageReads: Map<bigint, Map<bigint, Fr[]>> = new Map();
11
- //public publicStorageWrites: Array<TracedPublicStorageWrite> = [];
12
- public publicStorageWrites: Map<bigint, Map<bigint, Fr[]>> = new Map();
16
+ public publicStorageReads: TracedPublicStorageRead[] = [];
17
+ public publicStorageWrites: TracedPublicStorageWrite[] = [];
13
18
 
14
19
  public noteHashChecks: TracedNoteHashCheck[] = [];
15
- //public newNoteHashes: TracedNoteHash[] = [];
16
- public newNoteHashes: Fr[] = [];
20
+ public newNoteHashes: TracedNoteHash[] = [];
17
21
  public nullifierChecks: TracedNullifierCheck[] = [];
18
- //public newNullifiers: TracedNullifier[] = [];
19
- public newNullifiers: Fr[] = [];
22
+ public newNullifiers: TracedNullifier[] = [];
20
23
  public l1ToL2MessageChecks: TracedL1toL2MessageCheck[] = [];
24
+
25
+ //public contractCalls: TracedContractCall[] = [];
21
26
  //public archiveChecks: TracedArchiveLeafCheck[] = [];
22
27
 
23
28
  constructor(parentTrace?: WorldStateAccessTrace) {
@@ -28,76 +33,73 @@ export class WorldStateAccessTrace {
28
33
  return this.accessCounter;
29
34
  }
30
35
 
31
- public tracePublicStorageRead(storageAddress: Fr, slot: Fr, value: Fr /*, _exists: boolean*/) {
36
+ public tracePublicStorageRead(storageAddress: Fr, slot: Fr, value: Fr, exists: boolean) {
32
37
  // TODO(4805): check if some threshold is reached for max storage reads
33
38
  // (need access to parent length, or trace needs to be initialized with parent's contents)
34
- //const traced: TracedPublicStorageRead = {
35
- // callPointer: Fr.ZERO,
36
- // storageAddress,
37
- // slot,
38
- // value,
39
- // exists,
40
- // counter: new Fr(this.accessCounter),
41
- // endLifetime: Fr.ZERO,
42
- //};
43
- //this.publicStorageReads.push(traced);
44
- this.journalRead(storageAddress, slot, value);
39
+ const traced: TracedPublicStorageRead = {
40
+ // callPointer: Fr.ZERO,
41
+ storageAddress,
42
+ slot,
43
+ value,
44
+ exists,
45
+ counter: new Fr(this.accessCounter),
46
+ // endLifetime: Fr.ZERO,
47
+ };
48
+ this.publicStorageReads.push(traced);
45
49
  this.incrementAccessCounter();
46
50
  }
47
51
 
48
52
  public tracePublicStorageWrite(storageAddress: Fr, slot: Fr, value: Fr) {
49
53
  // TODO(4805): check if some threshold is reached for max storage writes
50
54
  // (need access to parent length, or trace needs to be initialized with parent's contents)
51
- //const traced: TracedPublicStorageWrite = {
52
- // callPointer: Fr.ZERO,
53
- // storageAddress,
54
- // slot,
55
- // value,
56
- // counter: new Fr(this.accessCounter),
57
- // endLifetime: Fr.ZERO,
58
- //};
59
- //this.publicStorageWrites.push(traced);
60
- this.journalWrite(storageAddress, slot, value);
55
+ const traced: TracedPublicStorageWrite = {
56
+ // callPointer: Fr.ZERO,
57
+ storageAddress,
58
+ slot,
59
+ value,
60
+ counter: new Fr(this.accessCounter),
61
+ // endLifetime: Fr.ZERO,
62
+ };
63
+ this.publicStorageWrites.push(traced);
61
64
  this.incrementAccessCounter();
62
65
  }
63
66
 
64
67
  public traceNoteHashCheck(storageAddress: Fr, noteHash: Fr, exists: boolean, leafIndex: Fr) {
65
68
  const traced: TracedNoteHashCheck = {
66
- callPointer: Fr.ZERO, // FIXME
69
+ // callPointer: Fr.ZERO,
67
70
  storageAddress,
68
71
  noteHash,
69
72
  exists,
70
73
  counter: new Fr(this.accessCounter),
71
- endLifetime: Fr.ZERO,
74
+ // endLifetime: Fr.ZERO,
72
75
  leafIndex,
73
76
  };
74
77
  this.noteHashChecks.push(traced);
75
78
  this.incrementAccessCounter();
76
79
  }
77
80
 
78
- public traceNewNoteHash(_storageAddress: Fr, noteHash: Fr) {
81
+ public traceNewNoteHash(storageAddress: Fr, noteHash: Fr) {
79
82
  // TODO(4805): check if some threshold is reached for max new note hash
80
- //const traced: TracedNoteHash = {
81
- // callPointer: Fr.ZERO,
82
- // storageAddress,
83
- // noteHash,
84
- // counter: new Fr(this.accessCounter),
85
- // endLifetime: Fr.ZERO,
86
- //};
87
- //this.newNoteHashes.push(traced);
88
- this.newNoteHashes.push(noteHash);
83
+ const traced: TracedNoteHash = {
84
+ // callPointer: Fr.ZERO,
85
+ storageAddress,
86
+ noteHash,
87
+ counter: new Fr(this.accessCounter),
88
+ // endLifetime: Fr.ZERO,
89
+ };
90
+ this.newNoteHashes.push(traced);
89
91
  this.incrementAccessCounter();
90
92
  }
91
93
 
92
94
  public traceNullifierCheck(storageAddress: Fr, nullifier: Fr, exists: boolean, isPending: boolean, leafIndex: Fr) {
93
95
  // TODO(4805): check if some threshold is reached for max new nullifier
94
96
  const traced: TracedNullifierCheck = {
95
- callPointer: Fr.ZERO, // FIXME
97
+ // callPointer: Fr.ZERO,
96
98
  storageAddress,
97
99
  nullifier,
98
100
  exists,
99
101
  counter: new Fr(this.accessCounter),
100
- endLifetime: Fr.ZERO,
102
+ // endLifetime: Fr.ZERO,
101
103
  isPending,
102
104
  leafIndex,
103
105
  };
@@ -105,17 +107,16 @@ export class WorldStateAccessTrace {
105
107
  this.incrementAccessCounter();
106
108
  }
107
109
 
108
- public traceNewNullifier(_storageAddress: Fr, nullifier: Fr) {
110
+ public traceNewNullifier(storageAddress: Fr, nullifier: Fr) {
109
111
  // TODO(4805): check if some threshold is reached for max new nullifier
110
- //const traced: TracedNullifier = {
111
- // callPointer: Fr.ZERO,
112
- // storageAddress,
113
- // nullifier,
114
- // counter: new Fr(this.accessCounter),
115
- // endLifetime: Fr.ZERO,
116
- //};
117
- //this.newNullifiers.push(traced);
118
- this.newNullifiers.push(nullifier);
112
+ const tracedNullifier: TracedNullifier = {
113
+ // callPointer: Fr.ZERO,
114
+ storageAddress,
115
+ nullifier,
116
+ counter: new Fr(this.accessCounter),
117
+ // endLifetime: Fr.ZERO,
118
+ };
119
+ this.newNullifiers.push(tracedNullifier);
119
120
  this.incrementAccessCounter();
120
121
  }
121
122
 
@@ -146,8 +147,8 @@ export class WorldStateAccessTrace {
146
147
  */
147
148
  public acceptAndMerge(incomingTrace: WorldStateAccessTrace) {
148
149
  // Merge storage read and write journals
149
- mergeContractJournalMaps(this.publicStorageReads, incomingTrace.publicStorageReads);
150
- mergeContractJournalMaps(this.publicStorageWrites, incomingTrace.publicStorageWrites);
150
+ this.publicStorageReads = this.publicStorageReads.concat(incomingTrace.publicStorageReads);
151
+ this.publicStorageWrites = this.publicStorageWrites.concat(incomingTrace.publicStorageWrites);
151
152
  // Merge new note hashes and nullifiers
152
153
  this.noteHashChecks = this.noteHashChecks.concat(incomingTrace.noteHashChecks);
153
154
  this.newNoteHashes = this.newNoteHashes.concat(incomingTrace.newNoteHashes);
@@ -157,67 +158,4 @@ export class WorldStateAccessTrace {
157
158
  // it is assumed that the incoming trace was initialized with this as parent, so accept counter
158
159
  this.accessCounter = incomingTrace.accessCounter;
159
160
  }
160
-
161
- /**
162
- * We want to keep track of all performed reads in the journal
163
- * This information is hinted to the avm circuit
164
-
165
- * @param contractAddress -
166
- * @param key -
167
- * @param value -
168
- */
169
- journalUpdate(map: Map<bigint, Map<bigint, Fr[]>>, contractAddress: Fr, key: Fr, value: Fr): void {
170
- let contractMap = map.get(contractAddress.toBigInt());
171
- if (!contractMap) {
172
- contractMap = new Map<bigint, Array<Fr>>();
173
- map.set(contractAddress.toBigInt(), contractMap);
174
- }
175
-
176
- let accessArray = contractMap.get(key.toBigInt());
177
- if (!accessArray) {
178
- accessArray = new Array<Fr>();
179
- contractMap.set(key.toBigInt(), accessArray);
180
- }
181
- accessArray.push(value);
182
- }
183
-
184
- // Create an instance of journalUpdate that appends to the read array
185
- private journalRead = this.journalUpdate.bind(this, this.publicStorageReads);
186
- // Create an instance of journalUpdate that appends to the writes array
187
- private journalWrite = this.journalUpdate.bind(this, this.publicStorageWrites);
188
- }
189
-
190
- /**
191
- * Merges two contract journalling maps together
192
- * For read maps, we just append the childMap arrays into the host map arrays, as the order is important
193
- *
194
- * @param hostMap - The map to be merged into
195
- * @param childMap - The map to be merged from
196
- */
197
- function mergeContractJournalMaps(hostMap: Map<bigint, Map<bigint, Fr[]>>, childMap: Map<bigint, Map<bigint, Fr[]>>) {
198
- for (const [key, value] of childMap) {
199
- const map1Value = hostMap.get(key);
200
- if (!map1Value) {
201
- hostMap.set(key, value);
202
- } else {
203
- mergeStorageJournalMaps(map1Value, value);
204
- }
205
- }
206
- }
207
-
208
- /**
209
- * Merge two storage journalling maps together (for a particular contract).
210
- *
211
- * @param hostMap - The map to be merge into
212
- * @param childMap - The map to be merged from
213
- */
214
- function mergeStorageJournalMaps(hostMap: Map<bigint, Fr[]>, childMap: Map<bigint, Fr[]>) {
215
- for (const [key, value] of childMap) {
216
- const readArr = hostMap.get(key);
217
- if (!readArr) {
218
- hostMap.set(key, value);
219
- } else {
220
- hostMap.set(key, readArr?.concat(...value));
221
- }
222
- }
223
161
  }
@@ -6,64 +6,64 @@ import { type Fr } from '@aztec/foundation/fields';
6
6
  // storageAddress: Fr;
7
7
  // endLifetime: Fr;
8
8
  //};
9
- //
10
- //export type TracedPublicStorageRead = {
11
- // callPointer: Fr;
12
- // storageAddress: Fr;
13
- // exists: boolean;
14
- // slot: Fr;
15
- // value: Fr;
16
- // counter: Fr;
17
- // endLifetime: Fr;
18
- //};
19
- //
20
- //export type TracedPublicStorageWrite = {
21
- // callPointer: Fr;
22
- // storageAddress: Fr;
23
- // slot: Fr;
24
- // value: Fr;
25
- // counter: Fr;
26
- // endLifetime: Fr;
27
- //};
28
- //
9
+
10
+ export type TracedPublicStorageRead = {
11
+ // callPointer: Fr;
12
+ storageAddress: Fr;
13
+ exists: boolean;
14
+ slot: Fr;
15
+ value: Fr;
16
+ counter: Fr;
17
+ // endLifetime: Fr;
18
+ };
19
+
20
+ export type TracedPublicStorageWrite = {
21
+ // callPointer: Fr;
22
+ storageAddress: Fr;
23
+ slot: Fr;
24
+ value: Fr;
25
+ counter: Fr;
26
+ // endLifetime: Fr;
27
+ };
28
+
29
29
  export type TracedNoteHashCheck = {
30
- callPointer: Fr;
30
+ // callPointer: Fr;
31
31
  storageAddress: Fr;
32
32
  leafIndex: Fr;
33
33
  noteHash: Fr;
34
34
  exists: boolean;
35
35
  counter: Fr;
36
- endLifetime: Fr;
36
+ // endLifetime: Fr;
37
+ };
38
+
39
+ export type TracedNoteHash = {
40
+ // callPointer: Fr;
41
+ storageAddress: Fr;
42
+ noteHash: Fr;
43
+ counter: Fr;
44
+ // endLifetime: Fr;
37
45
  };
38
- //
39
- //export type TracedNoteHash = {
40
- // callPointer: Fr;
41
- // storageAddress: Fr;
42
- // noteHash: Fr;
43
- // counter: Fr;
44
- // endLifetime: Fr;
45
- //};
46
46
 
47
47
  export type TracedNullifierCheck = {
48
- callPointer: Fr;
48
+ // callPointer: Fr;
49
49
  storageAddress: Fr;
50
50
  nullifier: Fr;
51
51
  exists: boolean;
52
52
  counter: Fr;
53
- endLifetime: Fr;
53
+ // endLifetime: Fr;
54
54
  // the fields below are relevant only to the public kernel
55
55
  // and are therefore omitted from VM inputs
56
56
  isPending: boolean;
57
57
  leafIndex: Fr;
58
58
  };
59
59
 
60
- //export type TracedNullifier = {
61
- // callPointer: Fr;
62
- // storageAddress: Fr;
63
- // nullifier: Fr;
64
- // counter: Fr;
65
- // endLifetime: Fr;
66
- //};
60
+ export type TracedNullifier = {
61
+ // callPointer: Fr;
62
+ storageAddress: Fr;
63
+ nullifier: Fr;
64
+ counter: Fr;
65
+ // endLifetime: Fr;
66
+ };
67
67
 
68
68
  export type TracedL1toL2MessageCheck = {
69
69
  //callPointer: Fr;
@@ -69,7 +69,7 @@ export class EmitNoteHash extends Instruction {
69
69
  }
70
70
 
71
71
  const noteHash = memory.get(this.noteHashOffset).toFr();
72
- context.persistableState.writeNoteHash(noteHash);
72
+ context.persistableState.writeNoteHash(context.environment.storageAddress, noteHash);
73
73
 
74
74
  memory.assert(memoryOperations);
75
75
  context.machineState.incrementPc();
@@ -80,19 +80,31 @@ export class NullifierExists extends Instruction {
80
80
  static type: string = 'NULLIFIEREXISTS';
81
81
  static readonly opcode: Opcode = Opcode.NULLIFIEREXISTS;
82
82
  // Informs (de)serialization. See Instruction.deserialize.
83
- static readonly wireFormat = [OperandType.UINT8, OperandType.UINT8, OperandType.UINT32, OperandType.UINT32];
83
+ static readonly wireFormat = [
84
+ OperandType.UINT8,
85
+ OperandType.UINT8,
86
+ OperandType.UINT32,
87
+ OperandType.UINT32,
88
+ OperandType.UINT32,
89
+ ];
84
90
 
85
- constructor(private indirect: number, private nullifierOffset: number, private existsOffset: number) {
91
+ constructor(
92
+ private indirect: number,
93
+ private nullifierOffset: number,
94
+ private addressOffset: number,
95
+ private existsOffset: number,
96
+ ) {
86
97
  super();
87
98
  }
88
99
 
89
100
  public async execute(context: AvmContext): Promise<void> {
90
- const memoryOperations = { reads: 1, writes: 1, indirect: this.indirect };
101
+ const memoryOperations = { reads: 2, writes: 1, indirect: this.indirect };
91
102
  const memory = context.machineState.memory.track(this.type);
92
103
  context.machineState.consumeGas(this.gasCost(memoryOperations));
93
104
 
94
105
  const nullifier = memory.get(this.nullifierOffset).toFr();
95
- const exists = await context.persistableState.checkNullifierExists(context.environment.storageAddress, nullifier);
106
+ const address = memory.get(this.addressOffset).toFr();
107
+ const exists = await context.persistableState.checkNullifierExists(address, nullifier);
96
108
 
97
109
  memory.set(this.existsOffset, exists ? new Uint8(1) : new Uint8(0));
98
110
 
@@ -7,7 +7,7 @@ export class L2GasLeft extends GetterInstruction {
7
7
  static type: string = 'L2GASLEFT';
8
8
  static readonly opcode: Opcode = Opcode.L2GASLEFT;
9
9
 
10
- // TODO(@spalladino) Yellow paper specifies that the value should be an Uint32, not a Field.
10
+ // TODO(@spalladino) Protocol specs specifies that the value should be an Uint32, not a Field.
11
11
  protected getValue(context: AvmContext): MemoryValue {
12
12
  return new Field(context.machineState.l2GasLeft);
13
13
  }
@@ -1,10 +1,16 @@
1
1
  import { FunctionSelector } from '@aztec/circuits.js';
2
2
  import { padArrayEnd } from '@aztec/foundation/collection';
3
3
 
4
+ import { executePublicFunction } from '../../public/executor.js';
5
+ import {
6
+ convertPublicExecutionResult,
7
+ createPublicExecutionContext,
8
+ updateAvmContextFromPublicExecutionResult,
9
+ } from '../../public/transitional_adaptors.js';
4
10
  import type { AvmContext } from '../avm_context.js';
5
11
  import { gasLeftToGas, sumGas } from '../avm_gas.js';
6
12
  import { Field, Uint8 } from '../avm_memory_types.js';
7
- import { AvmSimulator } from '../avm_simulator.js';
13
+ import { type AvmContractCallResults } from '../avm_message_call_result.js';
8
14
  import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
9
15
  import { Addressing } from './addressing_mode.js';
10
16
  import { Instruction } from './instruction.js';
@@ -30,7 +36,7 @@ abstract class ExternalCall extends Instruction {
30
36
  private gasOffset: number /* Unused due to no formal gas implementation at this moment */,
31
37
  private addrOffset: number,
32
38
  private argsOffset: number,
33
- private argsSize: number,
39
+ private argsSizeOffset: number,
34
40
  private retOffset: number,
35
41
  private retSize: number,
36
42
  private successOffset: number,
@@ -44,23 +50,27 @@ abstract class ExternalCall extends Instruction {
44
50
 
45
51
  public async execute(context: AvmContext) {
46
52
  const memory = context.machineState.memory.track(this.type);
47
- const [gasOffset, addrOffset, argsOffset, retOffset, successOffset] = Addressing.fromWire(this.indirect).resolve(
48
- [this.gasOffset, this.addrOffset, this.argsOffset, this.retOffset, this.successOffset],
53
+ const [gasOffset, addrOffset, argsOffset, argsSizeOffset, retOffset, successOffset] = Addressing.fromWire(
54
+ this.indirect,
55
+ ).resolve(
56
+ [this.gasOffset, this.addrOffset, this.argsOffset, this.argsSizeOffset, this.retOffset, this.successOffset],
49
57
  memory,
50
58
  );
51
59
 
52
60
  const callAddress = memory.getAs<Field>(addrOffset);
53
- const calldata = memory.getSlice(argsOffset, this.argsSize).map(f => f.toFr());
61
+ const calldataSize = memory.get(argsSizeOffset).toNumber();
62
+ const calldata = memory.getSlice(argsOffset, calldataSize).map(f => f.toFr());
54
63
  const l1Gas = memory.get(gasOffset).toNumber();
55
64
  const l2Gas = memory.getAs<Field>(gasOffset + 1).toNumber();
56
65
  const daGas = memory.getAs<Field>(gasOffset + 2).toNumber();
57
66
  const functionSelector = memory.getAs<Field>(this.temporaryFunctionSelectorOffset).toFr();
58
67
 
59
68
  const allocatedGas = { l1Gas, l2Gas, daGas };
60
- const memoryOperations = { reads: this.argsSize + 5, writes: 1 + this.retSize, indirect: this.indirect };
69
+ const memoryOperations = { reads: calldataSize + 6, writes: 1 + this.retSize, indirect: this.indirect };
61
70
  const totalGas = sumGas(this.gasCost(memoryOperations), allocatedGas);
62
71
  context.machineState.consumeGas(totalGas);
63
72
 
73
+ // TRANSITIONAL: This should be removed once the AVM is fully operational and the public executor is gone.
64
74
  const nestedContext = context.createNestedContractCallContext(
65
75
  callAddress.toFr(),
66
76
  calldata,
@@ -68,8 +78,21 @@ abstract class ExternalCall extends Instruction {
68
78
  this.type,
69
79
  FunctionSelector.fromField(functionSelector),
70
80
  );
81
+ const pxContext = createPublicExecutionContext(nestedContext, calldata);
82
+ const pxResults = await executePublicFunction(pxContext, /*nested=*/ true);
83
+ const nestedCallResults: AvmContractCallResults = convertPublicExecutionResult(pxResults);
84
+ updateAvmContextFromPublicExecutionResult(nestedContext, pxResults);
85
+ const nestedPersistableState = nestedContext.persistableState;
86
+ // const nestedContext = context.createNestedContractCallContext(
87
+ // callAddress.toFr(),
88
+ // calldata,
89
+ // allocatedGas,
90
+ // this.type,
91
+ // FunctionSelector.fromField(functionSelector),
92
+ // );
93
+ // const nestedCallResults: AvmContractCallResults = await new AvmSimulator(nestedContext).execute();
94
+ // const nestedPersistableState = nestedContext.persistableState;
71
95
 
72
- const nestedCallResults = await new AvmSimulator(nestedContext).execute();
73
96
  const success = !nestedCallResults.reverted;
74
97
 
75
98
  // We only take as much data as was specified in the return size and pad with zeroes if the return data is smaller
@@ -90,9 +113,9 @@ abstract class ExternalCall extends Instruction {
90
113
 
91
114
  // TODO: Should we merge the changes from a nested call in the case of a STATIC call?
92
115
  if (success) {
93
- context.persistableState.acceptNestedCallState(nestedContext.persistableState);
116
+ context.persistableState.acceptNestedCallState(nestedPersistableState);
94
117
  } else {
95
- context.persistableState.rejectNestedCallState(nestedContext.persistableState);
118
+ context.persistableState.rejectNestedCallState(nestedPersistableState);
96
119
  }
97
120
 
98
121
  memory.assert(memoryOperations);
@@ -1,15 +1,15 @@
1
- import { toBigIntBE } from '@aztec/foundation/bigint-buffer';
2
- import { keccak, pedersenHash, poseidonHash, sha256 } from '@aztec/foundation/crypto';
1
+ import { keccak256, pedersenHash, poseidon2Permutation, sha256 } from '@aztec/foundation/crypto';
3
2
 
4
3
  import { type AvmContext } from '../avm_context.js';
5
- import { Field } from '../avm_memory_types.js';
4
+ import { Field, Uint8 } from '../avm_memory_types.js';
6
5
  import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
7
6
  import { Addressing } from './addressing_mode.js';
8
7
  import { Instruction } from './instruction.js';
9
8
 
10
9
  export class Poseidon2 extends Instruction {
11
10
  static type: string = 'POSEIDON2';
12
- static readonly opcode: Opcode = Opcode.POSEIDON;
11
+ static readonly opcode: Opcode = Opcode.POSEIDON2;
12
+ static readonly stateSize = 4;
13
13
 
14
14
  // Informs (de)serialization. See Instruction.deserialize.
15
15
  static readonly wireFormat: OperandType[] = [
@@ -17,34 +17,28 @@ export class Poseidon2 extends Instruction {
17
17
  OperandType.UINT8,
18
18
  OperandType.UINT32,
19
19
  OperandType.UINT32,
20
- OperandType.UINT32,
21
20
  ];
22
21
 
23
- constructor(
24
- private indirect: number,
25
- private dstOffset: number,
26
- private messageOffset: number,
27
- private messageSize: number,
28
- ) {
22
+ constructor(private indirect: number, private inputStateOffset: number, private outputStateOffset: number) {
29
23
  super();
30
24
  }
31
25
 
32
26
  public async execute(context: AvmContext): Promise<void> {
33
- const memoryOperations = { reads: this.messageSize, writes: 1, indirect: this.indirect };
27
+ const memoryOperations = { reads: Poseidon2.stateSize, writes: Poseidon2.stateSize, indirect: this.indirect };
34
28
  const memory = context.machineState.memory.track(this.type);
35
29
  context.machineState.consumeGas(this.gasCost(memoryOperations));
36
30
 
37
- // We hash a set of field elements
38
- const [dstOffset, messageOffset] = Addressing.fromWire(this.indirect).resolve(
39
- [this.dstOffset, this.messageOffset],
31
+ const [inputOffset, outputOffset] = Addressing.fromWire(this.indirect).resolve(
32
+ [this.inputStateOffset, this.outputStateOffset],
40
33
  memory,
41
34
  );
42
35
 
43
- // Memory pointer will be indirect
44
- const hashData = memory.getSlice(messageOffset, this.messageSize).map(word => word.toBuffer());
45
-
46
- const hash = poseidonHash(hashData);
47
- memory.set(dstOffset, new Field(hash));
36
+ const inputState = memory.getSlice(inputOffset, Poseidon2.stateSize);
37
+ const outputState = poseidon2Permutation(inputState);
38
+ memory.setSlice(
39
+ outputOffset,
40
+ outputState.map(word => new Field(word)),
41
+ );
48
42
 
49
43
  memory.assert(memoryOperations);
50
44
  context.machineState.incrementPc();
@@ -68,33 +62,28 @@ export class Keccak extends Instruction {
68
62
  private indirect: number,
69
63
  private dstOffset: number,
70
64
  private messageOffset: number,
71
- private messageSize: number,
65
+ private messageSizeOffset: number,
72
66
  ) {
73
67
  super();
74
68
  }
75
69
 
76
- // Note hash output is 32 bytes, so takes up two fields
70
+ // pub fn keccak256(input: [u8], message_size: u32) -> [u8; 32]
77
71
  public async execute(context: AvmContext): Promise<void> {
78
- const memoryOperations = { reads: this.messageSize, writes: 2, indirect: this.indirect };
79
72
  const memory = context.machineState.memory.track(this.type);
80
- context.machineState.consumeGas(this.gasCost(memoryOperations));
81
-
82
- // We hash a set of field elements
83
- const [dstOffset, messageOffset] = Addressing.fromWire(this.indirect).resolve(
84
- [this.dstOffset, this.messageOffset],
73
+ const [dstOffset, messageOffset, messageSizeOffset] = Addressing.fromWire(this.indirect).resolve(
74
+ [this.dstOffset, this.messageOffset, this.messageSizeOffset],
85
75
  memory,
86
76
  );
77
+ const messageSize = memory.get(messageSizeOffset).toNumber();
78
+ const memoryOperations = { reads: messageSize + 1, writes: 32, indirect: this.indirect };
79
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
87
80
 
88
- const hashData = memory.getSlice(messageOffset, this.messageSize).map(word => word.toBuffer());
89
-
90
- const hash = keccak(Buffer.concat(hashData));
91
-
92
- // Split output into two fields
93
- const high = new Field(toBigIntBE(hash.subarray(0, 16)));
94
- const low = new Field(toBigIntBE(hash.subarray(16, 32)));
81
+ const messageData = Buffer.concat(memory.getSlice(messageOffset, messageSize).map(word => word.toBuffer()));
82
+ const hashBuffer = keccak256(messageData);
95
83
 
96
- memory.set(dstOffset, high);
97
- memory.set(dstOffset + 1, low);
84
+ // We need to convert the hashBuffer because map doesn't work as expected on an Uint8Array (Buffer).
85
+ const res = [...hashBuffer].map(byte => new Uint8(byte));
86
+ memory.setSlice(dstOffset, res);
98
87
 
99
88
  memory.assert(memoryOperations);
100
89
  context.machineState.incrementPc();
@@ -118,33 +107,28 @@ export class Sha256 extends Instruction {
118
107
  private indirect: number,
119
108
  private dstOffset: number,
120
109
  private messageOffset: number,
121
- private messageSize: number,
110
+ private messageSizeOffset: number,
122
111
  ) {
123
112
  super();
124
113
  }
125
114
 
126
- // Note hash output is 32 bytes, so takes up two fields
115
+ // pub fn sha256_slice(input: [u8]) -> [u8; 32]
127
116
  public async execute(context: AvmContext): Promise<void> {
128
- const memoryOperations = { reads: this.messageSize, writes: 2, indirect: this.indirect };
129
117
  const memory = context.machineState.memory.track(this.type);
130
- context.machineState.consumeGas(this.gasCost(memoryOperations));
131
-
132
- const [dstOffset, messageOffset] = Addressing.fromWire(this.indirect).resolve(
133
- [this.dstOffset, this.messageOffset],
118
+ const [dstOffset, messageOffset, messageSizeOffset] = Addressing.fromWire(this.indirect).resolve(
119
+ [this.dstOffset, this.messageOffset, this.messageSizeOffset],
134
120
  memory,
135
121
  );
122
+ const messageSize = memory.get(messageSizeOffset).toNumber();
123
+ const memoryOperations = { reads: messageSize + 1, writes: 32, indirect: this.indirect };
124
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
136
125
 
137
- // We hash a set of field elements
138
- const hashData = memory.getSlice(messageOffset, this.messageSize).map(word => word.toBuffer());
139
-
140
- const hash = sha256(Buffer.concat(hashData));
141
-
142
- // Split output into two fields
143
- const high = new Field(toBigIntBE(hash.subarray(0, 16)));
144
- const low = new Field(toBigIntBE(hash.subarray(16, 32)));
126
+ const messageData = Buffer.concat(memory.getSlice(messageOffset, messageSize).map(word => word.toBuffer()));
127
+ const hashBuffer = sha256(messageData);
145
128
 
146
- memory.set(dstOffset, high);
147
- memory.set(dstOffset + 1, low);
129
+ // We need to convert the hashBuffer because map doesn't work as expected on an Uint8Array (Buffer).
130
+ const res = [...hashBuffer].map(byte => new Uint8(byte));
131
+ memory.setSlice(dstOffset, res);
148
132
 
149
133
  memory.assert(memoryOperations);
150
134
  context.machineState.incrementPc();