@aztec/simulator 0.40.1 → 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 (234) 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 -2
  5. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  6. package/dest/acvm/oracle/oracle.js +31 -19
  7. package/dest/acvm/oracle/typed_oracle.d.ts +8 -11
  8. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  9. package/dest/acvm/oracle/typed_oracle.js +17 -5
  10. package/dest/acvm/serialize.js +2 -2
  11. package/dest/avm/avm_execution_environment.d.ts +4 -3
  12. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  13. package/dest/avm/avm_execution_environment.js +7 -5
  14. package/dest/avm/avm_gas.d.ts +1 -5
  15. package/dest/avm/avm_gas.d.ts.map +1 -1
  16. package/dest/avm/avm_gas.js +67 -73
  17. package/dest/avm/avm_simulator.d.ts +5 -0
  18. package/dest/avm/avm_simulator.d.ts.map +1 -1
  19. package/dest/avm/avm_simulator.js +12 -4
  20. package/dest/avm/errors.d.ts +6 -0
  21. package/dest/avm/errors.d.ts.map +1 -1
  22. package/dest/avm/errors.js +10 -1
  23. package/dest/avm/fixtures/index.d.ts +3 -0
  24. package/dest/avm/fixtures/index.d.ts.map +1 -1
  25. package/dest/avm/fixtures/index.js +15 -3
  26. package/dest/avm/index.d.ts +2 -0
  27. package/dest/avm/index.d.ts.map +1 -0
  28. package/dest/avm/index.js +2 -0
  29. package/dest/avm/journal/host_storage.d.ts +1 -1
  30. package/dest/avm/journal/host_storage.d.ts.map +1 -1
  31. package/dest/avm/journal/journal.d.ts +4 -3
  32. package/dest/avm/journal/journal.d.ts.map +1 -1
  33. package/dest/avm/journal/journal.js +10 -11
  34. package/dest/avm/journal/nullifiers.d.ts +17 -5
  35. package/dest/avm/journal/nullifiers.d.ts.map +1 -1
  36. package/dest/avm/journal/nullifiers.js +27 -10
  37. package/dest/avm/journal/public_storage.d.ts +19 -6
  38. package/dest/avm/journal/public_storage.d.ts.map +1 -1
  39. package/dest/avm/journal/public_storage.js +30 -12
  40. package/dest/avm/journal/trace.d.ts +2 -0
  41. package/dest/avm/journal/trace.d.ts.map +1 -1
  42. package/dest/avm/journal/trace.js +7 -1
  43. package/dest/avm/journal/trace_types.d.ts +1 -0
  44. package/dest/avm/journal/trace_types.d.ts.map +1 -1
  45. package/dest/avm/journal/trace_types.js +1 -1
  46. package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
  47. package/dest/avm/opcodes/accrued_substate.js +6 -7
  48. package/dest/avm/opcodes/arithmetic.d.ts +1 -7
  49. package/dest/avm/opcodes/arithmetic.d.ts.map +1 -1
  50. package/dest/avm/opcodes/arithmetic.js +1 -12
  51. package/dest/avm/opcodes/conversion.js +2 -2
  52. package/dest/avm/opcodes/external_calls.d.ts +2 -2
  53. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  54. package/dest/avm/opcodes/external_calls.js +25 -13
  55. package/dest/avm/opcodes/instruction.d.ts +1 -1
  56. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  57. package/dest/avm/opcodes/instruction.js +6 -4
  58. package/dest/avm/opcodes/memory.d.ts +0 -4
  59. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  60. package/dest/avm/opcodes/memory.js +1 -7
  61. package/dest/avm/opcodes/storage.d.ts +0 -12
  62. package/dest/avm/opcodes/storage.d.ts.map +1 -1
  63. package/dest/avm/opcodes/storage.js +3 -18
  64. package/dest/avm/serialization/bytecode_serialization.js +2 -2
  65. package/dest/client/client_execution_context.d.ts +30 -20
  66. package/dest/client/client_execution_context.d.ts.map +1 -1
  67. package/dest/client/client_execution_context.js +59 -39
  68. package/dest/client/db_oracle.d.ts +17 -12
  69. package/dest/client/db_oracle.d.ts.map +1 -1
  70. package/dest/client/execution_note_cache.js +2 -2
  71. package/dest/client/execution_result.d.ts +17 -2
  72. package/dest/client/execution_result.d.ts.map +1 -1
  73. package/dest/client/execution_result.js +32 -4
  74. package/dest/client/private_execution.d.ts +2 -3
  75. package/dest/client/private_execution.d.ts.map +1 -1
  76. package/dest/client/private_execution.js +22 -8
  77. package/dest/client/simulator.d.ts +0 -16
  78. package/dest/client/simulator.d.ts.map +1 -1
  79. package/dest/client/simulator.js +13 -30
  80. package/dest/client/unconstrained_execution.d.ts +2 -3
  81. package/dest/client/unconstrained_execution.d.ts.map +1 -1
  82. package/dest/client/unconstrained_execution.js +5 -7
  83. package/dest/client/view_data_oracle.d.ts +12 -10
  84. package/dest/client/view_data_oracle.d.ts.map +1 -1
  85. package/dest/client/view_data_oracle.js +17 -11
  86. package/dest/common/index.d.ts +1 -0
  87. package/dest/common/index.d.ts.map +1 -1
  88. package/dest/common/index.js +2 -1
  89. package/dest/common/return_values.d.ts +11 -0
  90. package/dest/common/return_values.d.ts.map +1 -0
  91. package/dest/common/return_values.js +13 -0
  92. package/dest/index.d.ts +3 -1
  93. package/dest/index.d.ts.map +1 -1
  94. package/dest/index.js +4 -2
  95. package/dest/mocks/fixtures.d.ts +15 -10
  96. package/dest/mocks/fixtures.d.ts.map +1 -1
  97. package/dest/mocks/fixtures.js +22 -17
  98. package/dest/{simulator → providers}/acvm_native.d.ts.map +1 -1
  99. package/dest/{simulator → providers}/acvm_native.js +13 -14
  100. package/dest/providers/acvm_wasm.d.ts.map +1 -0
  101. package/dest/providers/acvm_wasm.js +15 -0
  102. package/dest/providers/index.d.ts.map +1 -0
  103. package/dest/{simulator → providers}/index.js +1 -1
  104. package/dest/{simulator → providers}/simulation_provider.d.ts.map +1 -1
  105. package/dest/{simulator → providers}/simulation_provider.js +1 -1
  106. package/dest/public/abstract_phase_manager.d.ts +43 -42
  107. package/dest/public/abstract_phase_manager.d.ts.map +1 -1
  108. package/dest/public/abstract_phase_manager.js +81 -103
  109. package/dest/public/app_logic_phase_manager.d.ts +7 -8
  110. package/dest/public/app_logic_phase_manager.d.ts.map +1 -1
  111. package/dest/public/app_logic_phase_manager.js +14 -14
  112. package/dest/public/{db.d.ts → db_interfaces.d.ts} +3 -3
  113. package/dest/public/db_interfaces.d.ts.map +1 -0
  114. package/dest/public/db_interfaces.js +2 -0
  115. package/dest/public/execution.d.ts +17 -14
  116. package/dest/public/execution.d.ts.map +1 -1
  117. package/dest/public/execution.js +1 -1
  118. package/dest/public/executor.d.ts +3 -30
  119. package/dest/public/executor.d.ts.map +1 -1
  120. package/dest/public/executor.js +32 -279
  121. package/dest/public/fee_payment.d.ts +11 -0
  122. package/dest/public/fee_payment.d.ts.map +1 -0
  123. package/dest/public/fee_payment.js +24 -0
  124. package/dest/public/hints_builder.d.ts +3 -2
  125. package/dest/public/hints_builder.d.ts.map +1 -1
  126. package/dest/public/hints_builder.js +8 -4
  127. package/dest/public/index.d.ts +4 -2
  128. package/dest/public/index.d.ts.map +1 -1
  129. package/dest/public/index.js +5 -3
  130. package/dest/public/phase_manager_factory.d.ts +1 -1
  131. package/dest/public/phase_manager_factory.d.ts.map +1 -1
  132. package/dest/public/{public_executor.d.ts → public_db_sources.d.ts} +3 -2
  133. package/dest/public/public_db_sources.d.ts.map +1 -0
  134. package/dest/public/public_db_sources.js +198 -0
  135. package/dest/public/public_kernel.js +14 -14
  136. package/dest/public/public_processor.d.ts +8 -6
  137. package/dest/public/public_processor.d.ts.map +1 -1
  138. package/dest/public/public_processor.js +48 -29
  139. package/dest/public/setup_phase_manager.d.ts +7 -9
  140. package/dest/public/setup_phase_manager.d.ts.map +1 -1
  141. package/dest/public/setup_phase_manager.js +11 -20
  142. package/dest/public/tail_phase_manager.d.ts +5 -11
  143. package/dest/public/tail_phase_manager.d.ts.map +1 -1
  144. package/dest/public/tail_phase_manager.js +16 -36
  145. package/dest/public/teardown_phase_manager.d.ts +7 -9
  146. package/dest/public/teardown_phase_manager.d.ts.map +1 -1
  147. package/dest/public/teardown_phase_manager.js +15 -21
  148. package/dest/public/transitional_adaptors.d.ts +3 -2
  149. package/dest/public/transitional_adaptors.d.ts.map +1 -1
  150. package/dest/public/transitional_adaptors.js +24 -8
  151. package/dest/public/utils.d.ts +2 -2
  152. package/dest/public/utils.d.ts.map +1 -1
  153. package/dest/public/utils.js +21 -14
  154. package/package.json +12 -9
  155. package/src/acvm/acvm.ts +0 -3
  156. package/src/acvm/oracle/oracle.ts +53 -27
  157. package/src/acvm/oracle/typed_oracle.ts +25 -16
  158. package/src/acvm/serialize.ts +1 -1
  159. package/src/avm/avm_execution_environment.ts +9 -5
  160. package/src/avm/avm_gas.ts +66 -73
  161. package/src/avm/avm_simulator.ts +14 -3
  162. package/src/avm/errors.ts +10 -0
  163. package/src/avm/fixtures/index.ts +22 -2
  164. package/src/avm/index.ts +1 -0
  165. package/src/avm/journal/host_storage.ts +1 -1
  166. package/src/avm/journal/journal.ts +15 -14
  167. package/src/avm/journal/nullifiers.ts +27 -14
  168. package/src/avm/journal/public_storage.ts +30 -16
  169. package/src/avm/journal/trace.ts +12 -0
  170. package/src/avm/journal/trace_types.ts +1 -0
  171. package/src/avm/opcodes/accrued_substate.ts +5 -6
  172. package/src/avm/opcodes/arithmetic.ts +1 -14
  173. package/src/avm/opcodes/conversion.ts +1 -1
  174. package/src/avm/opcodes/external_calls.ts +26 -10
  175. package/src/avm/opcodes/instruction.ts +5 -3
  176. package/src/avm/opcodes/memory.ts +1 -8
  177. package/src/avm/opcodes/storage.ts +3 -20
  178. package/src/avm/serialization/bytecode_serialization.ts +1 -1
  179. package/src/client/client_execution_context.ts +70 -44
  180. package/src/client/db_oracle.ts +18 -12
  181. package/src/client/execution_note_cache.ts +1 -1
  182. package/src/client/execution_result.ts +41 -3
  183. package/src/client/private_execution.ts +38 -20
  184. package/src/client/simulator.ts +11 -32
  185. package/src/client/unconstrained_execution.ts +9 -12
  186. package/src/client/view_data_oracle.ts +20 -12
  187. package/src/common/index.ts +1 -0
  188. package/src/common/return_values.ts +18 -0
  189. package/src/index.ts +3 -1
  190. package/src/mocks/fixtures.ts +31 -33
  191. package/src/{simulator → providers}/acvm_native.ts +21 -19
  192. package/src/{simulator → providers}/acvm_wasm.ts +2 -16
  193. package/src/public/abstract_phase_manager.ts +146 -207
  194. package/src/public/app_logic_phase_manager.ts +23 -39
  195. package/src/public/{db.ts → db_interfaces.ts} +2 -2
  196. package/src/public/execution.ts +17 -14
  197. package/src/public/executor.ts +51 -391
  198. package/src/public/fee_payment.ts +26 -0
  199. package/src/public/hints_builder.ts +17 -3
  200. package/src/public/index.ts +4 -2
  201. package/src/public/phase_manager_factory.ts +1 -1
  202. package/src/public/{public_executor.ts → public_db_sources.ts} +3 -2
  203. package/src/public/public_kernel.ts +24 -24
  204. package/src/public/public_processor.ts +82 -38
  205. package/src/public/setup_phase_manager.ts +20 -45
  206. package/src/public/tail_phase_manager.ts +18 -68
  207. package/src/public/teardown_phase_manager.ts +22 -41
  208. package/src/public/transitional_adaptors.ts +24 -13
  209. package/src/public/utils.ts +20 -14
  210. package/dest/client/logs_cache.d.ts +0 -33
  211. package/dest/client/logs_cache.d.ts.map +0 -1
  212. package/dest/client/logs_cache.js +0 -59
  213. package/dest/public/db.d.ts.map +0 -1
  214. package/dest/public/db.js +0 -2
  215. package/dest/public/public_execution_context.d.ts +0 -121
  216. package/dest/public/public_execution_context.d.ts.map +0 -1
  217. package/dest/public/public_execution_context.js +0 -214
  218. package/dest/public/public_executor.d.ts.map +0 -1
  219. package/dest/public/public_executor.js +0 -197
  220. package/dest/public/state_actions.d.ts +0 -39
  221. package/dest/public/state_actions.d.ts.map +0 -1
  222. package/dest/public/state_actions.js +0 -80
  223. package/dest/simulator/acvm_wasm.d.ts.map +0 -1
  224. package/dest/simulator/acvm_wasm.js +0 -22
  225. package/dest/simulator/index.d.ts.map +0 -1
  226. package/src/client/logs_cache.ts +0 -65
  227. package/src/public/public_execution_context.ts +0 -289
  228. package/src/public/state_actions.ts +0 -102
  229. /package/dest/{simulator → providers}/acvm_native.d.ts +0 -0
  230. /package/dest/{simulator → providers}/acvm_wasm.d.ts +0 -0
  231. /package/dest/{simulator → providers}/index.d.ts +0 -0
  232. /package/dest/{simulator → providers}/simulation_provider.d.ts +0 -0
  233. /package/src/{simulator → providers}/index.ts +0 -0
  234. /package/src/{simulator → providers}/simulation_provider.ts +0 -0
@@ -12,21 +12,38 @@ import type { CommitmentsDB } from '../../index.js';
12
12
  export class Nullifiers {
13
13
  /** Cached nullifiers. */
14
14
  public cache: NullifierCache;
15
- /** Parent's nullifier cache. Checked on cache-miss. */
16
- private readonly parentCache: NullifierCache | undefined;
17
- /** Reference to node storage. Checked on parent cache-miss. */
18
- private readonly hostNullifiers: CommitmentsDB;
19
15
 
20
- constructor(hostNullifiers: CommitmentsDB, parent?: Nullifiers) {
21
- this.hostNullifiers = hostNullifiers;
22
- this.parentCache = parent?.cache;
16
+ constructor(
17
+ /** Reference to node storage. Checked on parent cache-miss. */
18
+ private readonly hostNullifiers: CommitmentsDB,
19
+ /** Parent's nullifiers. Checked on this' cache-miss. */
20
+ private readonly parent?: Nullifiers | undefined,
21
+ ) {
23
22
  this.cache = new NullifierCache();
24
23
  }
25
24
 
25
+ /**
26
+ * Get a nullifier's existence in this' cache or parent's (recursively).
27
+ * DOES NOT CHECK HOST STORAGE!
28
+ * @param storageAddress - the address of the contract whose storage is being read from
29
+ * @param nullifier - the nullifier to check for
30
+ * @returns exists: whether the nullifier exists in cache here or in parent's
31
+ */
32
+ private checkExistsHereOrParent(storageAddress: Fr, nullifier: Fr): boolean {
33
+ // First check this cache
34
+ let existsAsPending = this.cache.exists(storageAddress, nullifier);
35
+ // Then try parent's nullifier cache
36
+ if (!existsAsPending && this.parent) {
37
+ // Note: this will recurse to grandparent/etc until a cache-hit is encountered.
38
+ existsAsPending = this.parent.checkExistsHereOrParent(storageAddress, nullifier);
39
+ }
40
+ return existsAsPending;
41
+ }
42
+
26
43
  /**
27
44
  * Get a nullifier's existence status.
28
45
  * 1. Check cache.
29
- * 2. Check parent's cache.
46
+ * 2. Check parent cache.
30
47
  * 3. Fall back to the host state.
31
48
  * 4. Not found! Nullifier does not exist.
32
49
  *
@@ -40,12 +57,8 @@ export class Nullifiers {
40
57
  storageAddress: Fr,
41
58
  nullifier: Fr,
42
59
  ): Promise<[/*exists=*/ boolean, /*isPending=*/ boolean, /*leafIndex=*/ Fr]> {
43
- // First check this cache
44
- let existsAsPending = this.cache.exists(storageAddress, nullifier);
45
- // Then check parent's cache
46
- if (!existsAsPending && this.parentCache) {
47
- existsAsPending = this.parentCache?.exists(storageAddress, nullifier);
48
- }
60
+ // Check this cache and parent's (recursively)
61
+ const existsAsPending = this.checkExistsHereOrParent(storageAddress, nullifier);
49
62
  // Finally try the host's Aztec state (a trip to the database)
50
63
  // If the value is found in the database, it will be associated with a leaf index!
51
64
  let leafIndex: bigint | undefined = undefined;
@@ -17,14 +17,13 @@ type PublicStorageReadResult = {
17
17
  export class PublicStorage {
18
18
  /** Cached storage writes. */
19
19
  private cache: PublicStorageCache;
20
- /** Parent's storage cache. Checked on cache-miss. */
21
- private readonly parentCache: PublicStorageCache | undefined;
22
- /** Reference to node storage. Checked on parent cache-miss. */
23
- private readonly hostPublicStorage: PublicStateDB;
24
20
 
25
- constructor(hostPublicStorage: PublicStateDB, parent?: PublicStorage) {
26
- this.hostPublicStorage = hostPublicStorage;
27
- this.parentCache = parent?.cache;
21
+ constructor(
22
+ /** Reference to node storage. Checked on parent cache-miss. */
23
+ private readonly hostPublicStorage: PublicStateDB,
24
+ /** Parent's storage. Checked on this' cache-miss. */
25
+ private readonly parent?: PublicStorage,
26
+ ) {
28
27
  this.cache = new PublicStorageCache();
29
28
  }
30
29
 
@@ -35,10 +34,29 @@ export class PublicStorage {
35
34
  return this.cache;
36
35
  }
37
36
 
37
+ /**
38
+ * Read a storage value from this' cache or parent's (recursively).
39
+ * DOES NOT CHECK HOST STORAGE!
40
+ *
41
+ * @param storageAddress - the address of the contract whose storage is being read from
42
+ * @param slot - the slot in the contract's storage being read from
43
+ * @returns value: the latest value written according to this cache or the parent's. undefined on cache miss.
44
+ */
45
+ public readHereOrParent(storageAddress: Fr, slot: Fr): Fr | undefined {
46
+ // First try check this storage cache
47
+ let value = this.cache.read(storageAddress, slot);
48
+ // Then try parent's storage cache
49
+ if (!value && this.parent) {
50
+ // Note: this will recurse to grandparent/etc until a cache-hit is encountered.
51
+ value = this.parent.readHereOrParent(storageAddress, slot);
52
+ }
53
+ return value;
54
+ }
55
+
38
56
  /**
39
57
  * Read a value from storage.
40
58
  * 1. Check cache.
41
- * 2. Check parent's cache.
59
+ * 2. Check parent cache.
42
60
  * 3. Fall back to the host state.
43
61
  * 4. Not found! Value has never been written to before. Flag it as non-existent and return value zero.
44
62
  *
@@ -48,12 +66,8 @@ export class PublicStorage {
48
66
  */
49
67
  public async read(storageAddress: Fr, slot: Fr): Promise<PublicStorageReadResult> {
50
68
  let cached = false;
51
- // First try check this storage cache
52
- let value = this.cache.read(storageAddress, slot);
53
- // Then try parent's storage cache (if it exists / written to earlier in this TX)
54
- if (!value && this.parentCache) {
55
- value = this.parentCache?.read(storageAddress, slot);
56
- }
69
+ // Check this cache and parent's (recursively)
70
+ let value = this.readHereOrParent(storageAddress, slot);
57
71
  // Finally try the host's Aztec state (a trip to the database)
58
72
  if (!value) {
59
73
  value = await this.hostPublicStorage.storageRead(storageAddress, slot);
@@ -73,8 +87,8 @@ export class PublicStorage {
73
87
  * @param slot - the slot in the contract's storage being written to
74
88
  * @param value - the value being written to the slot
75
89
  */
76
- public write(storageAddress: Fr, key: Fr, value: Fr) {
77
- this.cache.write(storageAddress, key, value);
90
+ public write(storageAddress: Fr, slot: Fr, value: Fr) {
91
+ this.cache.write(storageAddress, slot, value);
78
92
  }
79
93
 
80
94
  /**
@@ -1,3 +1,4 @@
1
+ import { AvmExecutionHints, AvmHint, Vector } from '@aztec/circuits.js';
1
2
  import { Fr } from '@aztec/foundation/fields';
2
3
 
3
4
  import {
@@ -131,6 +132,7 @@ export class WorldStateAccessTrace {
131
132
  leafIndex: msgLeafIndex,
132
133
  msgHash: msgHash,
133
134
  exists: exists,
135
+ counter: new Fr(this.accessCounter),
134
136
  //endLifetime: Fr.ZERO, // FIXME
135
137
  };
136
138
  this.l1ToL2MessageChecks.push(traced);
@@ -169,4 +171,14 @@ export class WorldStateAccessTrace {
169
171
  // it is assumed that the incoming trace was initialized with this as parent, so accept counter
170
172
  this.accessCounter = incomingTrace.accessCounter;
171
173
  }
174
+
175
+ // TODO(dbanks12): should only return hints for one call.... shouldn't include nested calls (merged in traces)
176
+ public toHints(): AvmExecutionHints {
177
+ return new AvmExecutionHints(
178
+ new Vector(this.publicStorageReads.map(read => new AvmHint(read.counter, read.value))),
179
+ new Vector(this.noteHashChecks.map(check => new AvmHint(check.counter, new Fr(check.exists ? 1 : 0)))),
180
+ new Vector(this.nullifierChecks.map(check => new AvmHint(check.counter, new Fr(check.exists ? 1 : 0)))),
181
+ new Vector(this.l1ToL2MessageChecks.map(check => new AvmHint(check.counter, new Fr(check.exists ? 1 : 0)))),
182
+ );
183
+ }
172
184
  }
@@ -71,6 +71,7 @@ export type TracedL1toL2MessageCheck = {
71
71
  leafIndex: Fr;
72
72
  msgHash: Fr;
73
73
  exists: boolean;
74
+ counter: Fr;
74
75
  //endLifetime: Fr;
75
76
  };
76
77
 
@@ -1,11 +1,10 @@
1
1
  import type { AvmContext } from '../avm_context.js';
2
2
  import { Uint8 } from '../avm_memory_types.js';
3
- import { InstructionExecutionError } from '../errors.js';
3
+ import { InstructionExecutionError, StaticCallAlterationError } from '../errors.js';
4
4
  import { NullifierCollisionError } from '../journal/nullifiers.js';
5
5
  import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
6
6
  import { Addressing } from './addressing_mode.js';
7
7
  import { Instruction } from './instruction.js';
8
- import { StaticCallStorageAlterError } from './storage.js';
9
8
 
10
9
  export class NoteHashExists extends Instruction {
11
10
  static type: string = 'NOTEHASHEXISTS';
@@ -65,7 +64,7 @@ export class EmitNoteHash extends Instruction {
65
64
  context.machineState.consumeGas(this.gasCost(memoryOperations));
66
65
 
67
66
  if (context.environment.isStaticCall) {
68
- throw new StaticCallStorageAlterError();
67
+ throw new StaticCallAlterationError();
69
68
  }
70
69
 
71
70
  const noteHash = memory.get(this.noteHashOffset).toFr();
@@ -125,7 +124,7 @@ export class EmitNullifier extends Instruction {
125
124
 
126
125
  public async execute(context: AvmContext): Promise<void> {
127
126
  if (context.environment.isStaticCall) {
128
- throw new StaticCallStorageAlterError();
127
+ throw new StaticCallAlterationError();
129
128
  }
130
129
 
131
130
  const memoryOperations = { reads: 1, indirect: this.indirect };
@@ -210,7 +209,7 @@ export class EmitUnencryptedLog extends Instruction {
210
209
 
211
210
  public async execute(context: AvmContext): Promise<void> {
212
211
  if (context.environment.isStaticCall) {
213
- throw new StaticCallStorageAlterError();
212
+ throw new StaticCallAlterationError();
214
213
  }
215
214
 
216
215
  const memoryOperations = { reads: 1 + this.logSize, indirect: this.indirect };
@@ -244,7 +243,7 @@ export class SendL2ToL1Message extends Instruction {
244
243
 
245
244
  public async execute(context: AvmContext): Promise<void> {
246
245
  if (context.environment.isStaticCall) {
247
- throw new StaticCallStorageAlterError();
246
+ throw new StaticCallAlterationError();
248
247
  }
249
248
 
250
249
  const memoryOperations = { reads: 2, indirect: this.indirect };
@@ -1,6 +1,5 @@
1
1
  import type { AvmContext } from '../avm_context.js';
2
- import { getBaseGasCost, getGasCostForTypeTag, getMemoryGasCost, sumGas } from '../avm_gas.js';
3
- import { type Field, type MemoryOperations, type MemoryValue, TypeTag } from '../avm_memory_types.js';
2
+ import { type Field, type MemoryValue, TypeTag } from '../avm_memory_types.js';
4
3
  import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
5
4
  import { Instruction } from './instruction.js';
6
5
  import { ThreeOperandInstruction } from './instruction_impl.js';
@@ -23,12 +22,6 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst
23
22
  context.machineState.incrementPc();
24
23
  }
25
24
 
26
- protected override gasCost(memoryOps: Partial<MemoryOperations & { indirect: number }>) {
27
- const baseGasCost = getGasCostForTypeTag(this.inTag, getBaseGasCost(this.opcode));
28
- const memoryGasCost = getMemoryGasCost(memoryOps);
29
- return sumGas(baseGasCost, memoryGasCost);
30
- }
31
-
32
25
  protected abstract compute(a: MemoryValue, b: MemoryValue): MemoryValue;
33
26
  }
34
27
 
@@ -101,10 +94,4 @@ export class FieldDiv extends Instruction {
101
94
  memory.assert(memoryOperations);
102
95
  context.machineState.incrementPc();
103
96
  }
104
-
105
- protected override gasCost(memoryOps: Partial<MemoryOperations & { indirect: number }>) {
106
- const baseGasCost = getGasCostForTypeTag(TypeTag.FIELD, getBaseGasCost(this.opcode));
107
- const memoryGasCost = getMemoryGasCost(memoryOps);
108
- return sumGas(baseGasCost, memoryGasCost);
109
- }
110
97
  }
@@ -50,7 +50,7 @@ export class ToRadixLE extends Instruction {
50
50
  value /= radixBN;
51
51
  }
52
52
 
53
- const res = [...limbArray].map(byte => new Uint8(byte));
53
+ const res = limbArray.map(byte => new Uint8(byte));
54
54
  memory.setSlice(dstOffset, res);
55
55
 
56
56
  memory.assert(memoryOperations);
@@ -3,7 +3,7 @@ import { padArrayEnd } from '@aztec/foundation/collection';
3
3
 
4
4
  import { convertAvmResultsToPxResult, createPublicExecution } from '../../public/transitional_adaptors.js';
5
5
  import type { AvmContext } from '../avm_context.js';
6
- import { gasLeftToGas, sumGas } from '../avm_gas.js';
6
+ import { gasLeftToGas } from '../avm_gas.js';
7
7
  import { Field, Uint8 } from '../avm_memory_types.js';
8
8
  import { type AvmContractCallResults } from '../avm_message_call_result.js';
9
9
  import { AvmSimulator } from '../avm_simulator.js';
@@ -40,7 +40,7 @@ abstract class ExternalCall extends Instruction {
40
40
  // Function selector is temporary since eventually public contract bytecode will be one blob
41
41
  // containing all functions, and function selector will become an application-level mechanism
42
42
  // (e.g. first few bytes of calldata + compiler-generated jump table)
43
- private temporaryFunctionSelectorOffset: number,
43
+ private functionSelectorOffset: number,
44
44
  ) {
45
45
  super();
46
46
  }
@@ -57,33 +57,44 @@ abstract class ExternalCall extends Instruction {
57
57
  const callAddress = memory.getAs<Field>(addrOffset);
58
58
  const calldataSize = memory.get(argsSizeOffset).toNumber();
59
59
  const calldata = memory.getSlice(argsOffset, calldataSize).map(f => f.toFr());
60
- const l2Gas = memory.get(gasOffset).toNumber();
61
- const daGas = memory.getAs<Field>(gasOffset + 1).toNumber();
62
- const functionSelector = memory.getAs<Field>(this.temporaryFunctionSelectorOffset).toFr();
60
+ const functionSelector = memory.getAs<Field>(this.functionSelectorOffset).toFr();
61
+ // If we are already in a static call, we propagate the environment.
62
+ const callType = context.environment.isStaticCall ? 'STATICCALL' : this.type;
63
63
 
64
- const allocatedGas = { l2Gas, daGas };
64
+ // First we consume the gas for this operation.
65
65
  const memoryOperations = { reads: calldataSize + 5, writes: 1 + this.retSize, indirect: this.indirect };
66
- const totalGas = sumGas(this.gasCost(memoryOperations), allocatedGas);
67
- context.machineState.consumeGas(totalGas);
66
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
67
+ // Then we consume the gas allocated for the nested call. The excess will be refunded later.
68
+ // Gas allocation is capped by the amount of gas left in the current context.
69
+ // We have to do some dancing here because the gas allocation is a field,
70
+ // but in the machine state we track gas as a number.
71
+ const allocatedL2Gas = Number(BigIntMin(memory.get(gasOffset).toBigInt(), BigInt(context.machineState.l2GasLeft)));
72
+ const allocatedDaGas = Number(
73
+ BigIntMin(memory.get(gasOffset + 1).toBigInt(), BigInt(context.machineState.daGasLeft)),
74
+ );
75
+ const allocatedGas = { l2Gas: allocatedL2Gas, daGas: allocatedDaGas };
76
+ context.machineState.consumeGas(allocatedGas);
68
77
 
69
78
  // TRANSITIONAL: This should be removed once the kernel handles and entire enqueued call per circuit
70
79
  const nestedContext = context.createNestedContractCallContext(
71
80
  callAddress.toFr(),
72
81
  calldata,
73
82
  allocatedGas,
74
- this.type,
83
+ callType,
75
84
  FunctionSelector.fromField(functionSelector),
76
85
  );
77
86
  const startSideEffectCounter = nestedContext.persistableState.trace.accessCounter;
78
87
 
79
88
  const oldStyleExecution = createPublicExecution(startSideEffectCounter, nestedContext.environment, calldata);
80
- const nestedCallResults: AvmContractCallResults = await new AvmSimulator(nestedContext).execute();
89
+ const simulator = new AvmSimulator(nestedContext);
90
+ const nestedCallResults: AvmContractCallResults = await simulator.execute();
81
91
  const pxResults = convertAvmResultsToPxResult(
82
92
  nestedCallResults,
83
93
  startSideEffectCounter,
84
94
  oldStyleExecution,
85
95
  Gas.from(allocatedGas),
86
96
  nestedContext,
97
+ simulator.getBytecode(),
87
98
  );
88
99
  // store the old PublicExecutionResult object to maintain a recursive data structure for the old kernel
89
100
  context.persistableState.transitionalExecutionResult.nestedExecutions.push(pxResults);
@@ -215,3 +226,8 @@ export class Revert extends Instruction {
215
226
  memory.assert(memoryOperations);
216
227
  }
217
228
  }
229
+
230
+ /** Returns the smaller of two bigints. */
231
+ function BigIntMin(a: bigint, b: bigint): bigint {
232
+ return a < b ? a : b;
233
+ }
@@ -1,7 +1,7 @@
1
1
  import { strict as assert } from 'assert';
2
2
 
3
3
  import type { AvmContext } from '../avm_context.js';
4
- import { getBaseGasCost, getMemoryGasCost, sumGas } from '../avm_gas.js';
4
+ import { getBaseGasCost, sumGas } from '../avm_gas.js';
5
5
  import { type MemoryOperations } from '../avm_memory_types.js';
6
6
  import { type BufferCursor } from '../serialization/buffer_cursor.js';
7
7
  import { Opcode, type OperandType, deserialize, serialize } from '../serialization/instruction_serialization.js';
@@ -67,9 +67,11 @@ export abstract class Instruction {
67
67
  * @param memoryOps Memory operations performed by the instruction.
68
68
  * @returns Gas cost.
69
69
  */
70
- protected gasCost(memoryOps: Partial<MemoryOperations & { indirect: number }> = {}) {
70
+ protected gasCost(_memoryOps: Partial<MemoryOperations & { indirect: number }> = {}) {
71
71
  const baseGasCost = getBaseGasCost(this.opcode);
72
- const memoryGasCost = getMemoryGasCost(memoryOps);
72
+ // TODO(https://github.com/AztecProtocol/aztec-packages/issues/6861): reconsider.
73
+ // const memoryGasCost = getMemoryGasCost(memoryOps);
74
+ const memoryGasCost = { l2Gas: 0, daGas: 0 };
73
75
  return sumGas(baseGasCost, memoryGasCost);
74
76
  }
75
77
 
@@ -1,6 +1,5 @@
1
1
  import type { AvmContext } from '../avm_context.js';
2
- import { getBaseGasCost, getMemoryGasCost, sumGas } from '../avm_gas.js';
3
- import { Field, type MemoryOperations, TaggedMemory, TypeTag } from '../avm_memory_types.js';
2
+ import { Field, TaggedMemory, TypeTag } from '../avm_memory_types.js';
4
3
  import { InstructionExecutionError } from '../errors.js';
5
4
  import { BufferCursor } from '../serialization/buffer_cursor.js';
6
5
  import { Opcode, OperandType, deserialize, serialize } from '../serialization/instruction_serialization.js';
@@ -216,10 +215,4 @@ export class CalldataCopy extends Instruction {
216
215
  memory.assert(memoryOperations);
217
216
  context.machineState.incrementPc();
218
217
  }
219
-
220
- protected override gasCost(memoryOps: Partial<MemoryOperations & { indirect: number }> = {}) {
221
- const baseGasCost = getBaseGasCost(this.opcode);
222
- const memoryGasCost = getMemoryGasCost(memoryOps);
223
- return sumGas(baseGasCost, memoryGasCost);
224
- }
225
218
  }
@@ -1,9 +1,8 @@
1
1
  import { Fr } from '@aztec/foundation/fields';
2
2
 
3
3
  import type { AvmContext } from '../avm_context.js';
4
- import { type Gas, getBaseGasCost, getMemoryGasCost, mulGas, sumGas } from '../avm_gas.js';
5
- import { Field, type MemoryOperations } from '../avm_memory_types.js';
6
- import { InstructionExecutionError } from '../errors.js';
4
+ import { Field } from '../avm_memory_types.js';
5
+ import { StaticCallAlterationError } from '../errors.js';
7
6
  import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
8
7
  import { Addressing } from './addressing_mode.js';
9
8
  import { Instruction } from './instruction.js';
@@ -26,12 +25,6 @@ abstract class BaseStorageInstruction extends Instruction {
26
25
  ) {
27
26
  super();
28
27
  }
29
-
30
- protected override gasCost(memoryOps: Partial<MemoryOperations & { indirect: number }>): Gas {
31
- const baseGasCost = mulGas(getBaseGasCost(this.opcode), this.size);
32
- const memoryGasCost = getMemoryGasCost(memoryOps);
33
- return sumGas(baseGasCost, memoryGasCost);
34
- }
35
28
  }
36
29
 
37
30
  export class SStore extends BaseStorageInstruction {
@@ -44,7 +37,7 @@ export class SStore extends BaseStorageInstruction {
44
37
 
45
38
  public async execute(context: AvmContext): Promise<void> {
46
39
  if (context.environment.isStaticCall) {
47
- throw new StaticCallStorageAlterError();
40
+ throw new StaticCallAlterationError();
48
41
  }
49
42
 
50
43
  const memoryOperations = { reads: this.size + 1, indirect: this.indirect };
@@ -100,13 +93,3 @@ export class SLoad extends BaseStorageInstruction {
100
93
  memory.assert(memoryOperations);
101
94
  }
102
95
  }
103
-
104
- /**
105
- * Error is thrown when a static call attempts to alter storage
106
- */
107
- export class StaticCallStorageAlterError extends InstructionExecutionError {
108
- constructor() {
109
- super('Static calls cannot alter storage');
110
- this.name = 'StaticCallStorageAlterError';
111
- }
112
- }
@@ -136,7 +136,7 @@ const INSTRUCTION_SET = () =>
136
136
  // Misc
137
137
  [DebugLog.opcode, DebugLog],
138
138
 
139
- // //// Gadgets
139
+ // Gadgets
140
140
  [Keccak.opcode, Keccak],
141
141
  [Poseidon2.opcode, Poseidon2],
142
142
  [Sha256.opcode, Sha256],