@aztec/simulator 0.32.0 → 0.33.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 (227) hide show
  1. package/README.md +5 -3
  2. package/dest/acvm/acvm.d.ts +5 -5
  3. package/dest/acvm/acvm.d.ts.map +1 -1
  4. package/dest/acvm/acvm_types.d.ts +1 -1
  5. package/dest/acvm/acvm_types.d.ts.map +1 -1
  6. package/dest/acvm/deserialize.d.ts +1 -1
  7. package/dest/acvm/deserialize.d.ts.map +1 -1
  8. package/dest/acvm/oracle/index.d.ts +1 -2
  9. package/dest/acvm/oracle/index.d.ts.map +1 -1
  10. package/dest/acvm/oracle/index.js +1 -2
  11. package/dest/acvm/oracle/oracle.d.ts +2 -2
  12. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  13. package/dest/acvm/oracle/oracle.js +2 -3
  14. package/dest/acvm/oracle/typed_oracle.d.ts +6 -6
  15. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  16. package/dest/acvm/serialize.d.ts +4 -4
  17. package/dest/acvm/serialize.d.ts.map +1 -1
  18. package/dest/acvm/serialize.js +1 -1
  19. package/dest/avm/avm_context.d.ts +8 -18
  20. package/dest/avm/avm_context.d.ts.map +1 -1
  21. package/dest/avm/avm_context.js +10 -22
  22. package/dest/avm/avm_execution_environment.d.ts +4 -4
  23. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  24. package/dest/avm/avm_execution_environment.js +1 -1
  25. package/dest/avm/avm_gas.d.ts +71 -0
  26. package/dest/avm/avm_gas.d.ts.map +1 -0
  27. package/dest/avm/avm_gas.js +161 -0
  28. package/dest/avm/avm_machine_state.d.ts +5 -3
  29. package/dest/avm/avm_machine_state.d.ts.map +1 -1
  30. package/dest/avm/avm_machine_state.js +8 -2
  31. package/dest/avm/avm_memory_types.d.ts +54 -2
  32. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  33. package/dest/avm/avm_memory_types.js +95 -2
  34. package/dest/avm/avm_message_call_result.d.ts +1 -1
  35. package/dest/avm/avm_message_call_result.d.ts.map +1 -1
  36. package/dest/avm/avm_simulator.d.ts.map +1 -1
  37. package/dest/avm/avm_simulator.js +10 -8
  38. package/dest/avm/errors.d.ts +1 -1
  39. package/dest/avm/errors.d.ts.map +1 -1
  40. package/dest/avm/fixtures/index.d.ts +1 -1
  41. package/dest/avm/fixtures/index.d.ts.map +1 -1
  42. package/dest/avm/fixtures/index.js +2 -2
  43. package/dest/avm/journal/host_storage.d.ts +1 -1
  44. package/dest/avm/journal/host_storage.d.ts.map +1 -1
  45. package/dest/avm/journal/journal.d.ts +2 -2
  46. package/dest/avm/journal/journal.d.ts.map +1 -1
  47. package/dest/avm/journal/trace.d.ts +1 -1
  48. package/dest/avm/journal/trace.d.ts.map +1 -1
  49. package/dest/avm/journal/trace_types.d.ts +1 -1
  50. package/dest/avm/journal/trace_types.d.ts.map +1 -1
  51. package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
  52. package/dest/avm/opcodes/accrued_substate.js +44 -16
  53. package/dest/avm/opcodes/addressing_mode.d.ts +5 -3
  54. package/dest/avm/opcodes/addressing_mode.d.ts.map +1 -1
  55. package/dest/avm/opcodes/addressing_mode.js +5 -1
  56. package/dest/avm/opcodes/arithmetic.d.ts +7 -3
  57. package/dest/avm/opcodes/arithmetic.d.ts.map +1 -1
  58. package/dest/avm/opcodes/arithmetic.js +27 -16
  59. package/dest/avm/opcodes/bitwise.d.ts +21 -20
  60. package/dest/avm/opcodes/bitwise.d.ts.map +1 -1
  61. package/dest/avm/opcodes/bitwise.js +43 -65
  62. package/dest/avm/opcodes/comparators.d.ts +12 -9
  63. package/dest/avm/opcodes/comparators.d.ts.map +1 -1
  64. package/dest/avm/opcodes/comparators.js +22 -32
  65. package/dest/avm/opcodes/context_getters.d.ts +20 -0
  66. package/dest/avm/opcodes/context_getters.d.ts.map +1 -0
  67. package/dest/avm/opcodes/context_getters.js +26 -0
  68. package/dest/avm/opcodes/contract.d.ts +14 -0
  69. package/dest/avm/opcodes/contract.d.ts.map +1 -0
  70. package/dest/avm/opcodes/contract.js +49 -0
  71. package/dest/avm/opcodes/control_flow.d.ts.map +1 -1
  72. package/dest/avm/opcodes/control_flow.js +12 -2
  73. package/dest/avm/opcodes/environment_getters.d.ts +31 -34
  74. package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
  75. package/dest/avm/opcodes/environment_getters.js +34 -43
  76. package/dest/avm/opcodes/external_calls.d.ts +13 -19
  77. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  78. package/dest/avm/opcodes/external_calls.js +50 -68
  79. package/dest/avm/opcodes/hashing.d.ts +3 -2
  80. package/dest/avm/opcodes/hashing.d.ts.map +1 -1
  81. package/dest/avm/opcodes/hashing.js +37 -18
  82. package/dest/avm/opcodes/index.d.ts +1 -0
  83. package/dest/avm/opcodes/index.d.ts.map +1 -1
  84. package/dest/avm/opcodes/index.js +2 -1
  85. package/dest/avm/opcodes/instruction.d.ts +12 -17
  86. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  87. package/dest/avm/opcodes/instruction.js +12 -22
  88. package/dest/avm/opcodes/instruction_impl.d.ts +14 -0
  89. package/dest/avm/opcodes/instruction_impl.d.ts.map +1 -1
  90. package/dest/avm/opcodes/instruction_impl.js +37 -16
  91. package/dest/avm/opcodes/memory.d.ts +4 -3
  92. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  93. package/dest/avm/opcodes/memory.js +38 -19
  94. package/dest/avm/opcodes/storage.d.ts +5 -0
  95. package/dest/avm/opcodes/storage.d.ts.map +1 -1
  96. package/dest/avm/opcodes/storage.js +21 -7
  97. package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  98. package/dest/avm/serialization/bytecode_serialization.js +7 -5
  99. package/dest/avm/serialization/instruction_serialization.d.ts +12 -11
  100. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  101. package/dest/avm/serialization/instruction_serialization.js +13 -12
  102. package/dest/avm/temporary_executor_migration.d.ts +6 -4
  103. package/dest/avm/temporary_executor_migration.d.ts.map +1 -1
  104. package/dest/avm/temporary_executor_migration.js +14 -3
  105. package/dest/client/client_execution_context.d.ts +11 -11
  106. package/dest/client/client_execution_context.d.ts.map +1 -1
  107. package/dest/client/client_execution_context.js +1 -1
  108. package/dest/client/db_oracle.d.ts +9 -9
  109. package/dest/client/db_oracle.d.ts.map +1 -1
  110. package/dest/client/db_oracle.js +1 -1
  111. package/dest/client/execution_note_cache.d.ts +2 -2
  112. package/dest/client/execution_note_cache.d.ts.map +1 -1
  113. package/dest/client/execution_result.d.ts +5 -5
  114. package/dest/client/execution_result.d.ts.map +1 -1
  115. package/dest/client/execution_result.js +1 -1
  116. package/dest/client/pick_notes.d.ts +1 -1
  117. package/dest/client/pick_notes.d.ts.map +1 -1
  118. package/dest/client/pick_notes.js +1 -1
  119. package/dest/client/private_execution.d.ts +5 -5
  120. package/dest/client/private_execution.d.ts.map +1 -1
  121. package/dest/client/private_execution.js +6 -2
  122. package/dest/client/simulator.d.ts +6 -6
  123. package/dest/client/simulator.d.ts.map +1 -1
  124. package/dest/client/simulator.js +1 -1
  125. package/dest/client/unconstrained_execution.d.ts +5 -5
  126. package/dest/client/unconstrained_execution.d.ts.map +1 -1
  127. package/dest/client/unconstrained_execution.js +1 -1
  128. package/dest/client/view_data_oracle.d.ts +6 -6
  129. package/dest/client/view_data_oracle.d.ts.map +1 -1
  130. package/dest/client/view_data_oracle.js +1 -1
  131. package/dest/common/errors.d.ts +1 -1
  132. package/dest/common/errors.d.ts.map +1 -1
  133. package/dest/common/errors.js +1 -1
  134. package/dest/public/db.d.ts +6 -6
  135. package/dest/public/db.d.ts.map +1 -1
  136. package/dest/public/execution.d.ts +2 -2
  137. package/dest/public/execution.d.ts.map +1 -1
  138. package/dest/public/executor.d.ts +13 -5
  139. package/dest/public/executor.d.ts.map +1 -1
  140. package/dest/public/executor.js +59 -20
  141. package/dest/public/index.d.ts +1 -1
  142. package/dest/public/index.d.ts.map +1 -1
  143. package/dest/public/public_execution_context.d.ts +7 -7
  144. package/dest/public/public_execution_context.d.ts.map +1 -1
  145. package/dest/public/public_execution_context.js +3 -3
  146. package/dest/public/state_actions.d.ts +2 -2
  147. package/dest/public/state_actions.d.ts.map +1 -1
  148. package/dest/simulator/acvm_native.d.ts +3 -3
  149. package/dest/simulator/acvm_native.d.ts.map +1 -1
  150. package/dest/simulator/acvm_wasm.d.ts +3 -3
  151. package/dest/simulator/acvm_wasm.d.ts.map +1 -1
  152. package/dest/simulator/acvm_wasm.js +2 -2
  153. package/dest/simulator/simulation_provider.d.ts +2 -2
  154. package/dest/simulator/simulation_provider.d.ts.map +1 -1
  155. package/dest/test/utils.d.ts +1 -1
  156. package/dest/test/utils.d.ts.map +1 -1
  157. package/dest/test/utils.js +1 -1
  158. package/dest/utils.d.ts +1 -1
  159. package/dest/utils.d.ts.map +1 -1
  160. package/package.json +15 -9
  161. package/src/acvm/acvm.ts +8 -8
  162. package/src/acvm/acvm_types.ts +1 -1
  163. package/src/acvm/deserialize.ts +1 -1
  164. package/src/acvm/oracle/index.ts +1 -2
  165. package/src/acvm/oracle/oracle.ts +3 -4
  166. package/src/acvm/oracle/typed_oracle.ts +18 -18
  167. package/src/acvm/serialize.ts +4 -4
  168. package/src/avm/avm_context.ts +15 -37
  169. package/src/avm/avm_execution_environment.ts +4 -4
  170. package/src/avm/{avm_gas_cost.ts → avm_gas.ts} +75 -21
  171. package/src/avm/avm_machine_state.ts +10 -3
  172. package/src/avm/avm_memory_types.ts +131 -3
  173. package/src/avm/avm_message_call_result.ts +1 -1
  174. package/src/avm/avm_simulator.ts +10 -8
  175. package/src/avm/errors.ts +1 -1
  176. package/src/avm/fixtures/index.ts +6 -1
  177. package/src/avm/journal/host_storage.ts +1 -1
  178. package/src/avm/journal/journal.ts +2 -2
  179. package/src/avm/journal/trace.ts +1 -1
  180. package/src/avm/journal/trace_types.ts +1 -1
  181. package/src/avm/opcodes/accrued_substate.ts +57 -22
  182. package/src/avm/opcodes/addressing_mode.ts +8 -3
  183. package/src/avm/opcodes/arithmetic.ts +32 -22
  184. package/src/avm/opcodes/bitwise.ts +50 -84
  185. package/src/avm/opcodes/comparators.ts +28 -43
  186. package/src/avm/opcodes/context_getters.ts +32 -0
  187. package/src/avm/opcodes/contract.ts +58 -0
  188. package/src/avm/opcodes/control_flow.ts +24 -6
  189. package/src/avm/opcodes/environment_getters.ts +36 -45
  190. package/src/avm/opcodes/external_calls.ts +65 -84
  191. package/src/avm/opcodes/hashing.ts +46 -23
  192. package/src/avm/opcodes/index.ts +1 -0
  193. package/src/avm/opcodes/instruction.ts +16 -28
  194. package/src/avm/opcodes/instruction_impl.ts +45 -15
  195. package/src/avm/opcodes/memory.ts +48 -28
  196. package/src/avm/opcodes/storage.ts +26 -12
  197. package/src/avm/serialization/bytecode_serialization.ts +6 -3
  198. package/src/avm/serialization/instruction_serialization.ts +1 -0
  199. package/src/avm/temporary_executor_migration.ts +20 -6
  200. package/src/client/client_execution_context.ts +15 -15
  201. package/src/client/db_oracle.ts +16 -10
  202. package/src/client/execution_note_cache.ts +2 -2
  203. package/src/client/execution_result.ts +9 -5
  204. package/src/client/pick_notes.ts +1 -1
  205. package/src/client/private_execution.ts +12 -6
  206. package/src/client/simulator.ts +8 -8
  207. package/src/client/unconstrained_execution.ts +5 -5
  208. package/src/client/view_data_oracle.ts +11 -11
  209. package/src/common/errors.ts +1 -1
  210. package/src/public/db.ts +6 -6
  211. package/src/public/execution.ts +10 -10
  212. package/src/public/executor.ts +78 -25
  213. package/src/public/index.ts +2 -2
  214. package/src/public/public_execution_context.ts +17 -7
  215. package/src/public/state_actions.ts +2 -2
  216. package/src/simulator/acvm_native.ts +3 -3
  217. package/src/simulator/acvm_wasm.ts +8 -4
  218. package/src/simulator/simulation_provider.ts +2 -2
  219. package/src/test/utils.ts +1 -1
  220. package/src/utils.ts +1 -1
  221. package/dest/acvm/oracle/debug.d.ts +0 -19
  222. package/dest/acvm/oracle/debug.d.ts.map +0 -1
  223. package/dest/acvm/oracle/debug.js +0 -95
  224. package/dest/avm/avm_gas_cost.d.ts +0 -322
  225. package/dest/avm/avm_gas_cost.d.ts.map +0 -1
  226. package/dest/avm/avm_gas_cost.js +0 -118
  227. package/src/acvm/oracle/debug.ts +0 -109
@@ -1,19 +1,34 @@
1
+ import { type AvmContext } from '../avm_context.js';
2
+ import { type MemoryValue } from '../avm_memory_types.js';
1
3
  import { OperandType } from '../serialization/instruction_serialization.js';
2
4
  import { Instruction } from './instruction.js';
3
5
 
6
+ /** Wire format that informs deserialization for instructions with two operands. */
7
+ export const TwoOperandWireFormat = [
8
+ OperandType.UINT8,
9
+ OperandType.UINT8,
10
+ OperandType.UINT8,
11
+ OperandType.UINT32,
12
+ OperandType.UINT32,
13
+ ];
14
+
15
+ /** Wire format that informs deserialization for instructions with three operands. */
16
+ export const ThreeOperandWireFormat = [
17
+ OperandType.UINT8,
18
+ OperandType.UINT8,
19
+ OperandType.UINT8,
20
+ OperandType.UINT32,
21
+ OperandType.UINT32,
22
+ OperandType.UINT32,
23
+ ];
24
+
4
25
  /**
5
26
  * Covers (de)serialization for an instruction with:
6
27
  * indirect, inTag, and two UINT32s.
7
28
  */
8
29
  export abstract class TwoOperandInstruction extends Instruction {
9
30
  // Informs (de)serialization. See Instruction.deserialize.
10
- static readonly wireFormat: OperandType[] = [
11
- OperandType.UINT8,
12
- OperandType.UINT8,
13
- OperandType.UINT8,
14
- OperandType.UINT32,
15
- OperandType.UINT32,
16
- ];
31
+ static readonly wireFormat: OperandType[] = TwoOperandWireFormat;
17
32
 
18
33
  constructor(
19
34
  protected indirect: number,
@@ -31,14 +46,7 @@ export abstract class TwoOperandInstruction extends Instruction {
31
46
  */
32
47
  export abstract class ThreeOperandInstruction extends Instruction {
33
48
  // Informs (de)serialization. See Instruction.deserialize.
34
- static readonly wireFormat: OperandType[] = [
35
- OperandType.UINT8,
36
- OperandType.UINT8,
37
- OperandType.UINT8,
38
- OperandType.UINT32,
39
- OperandType.UINT32,
40
- OperandType.UINT32,
41
- ];
49
+ static readonly wireFormat: OperandType[] = ThreeOperandWireFormat;
42
50
 
43
51
  constructor(
44
52
  protected indirect: number,
@@ -50,3 +58,25 @@ export abstract class ThreeOperandInstruction extends Instruction {
50
58
  super();
51
59
  }
52
60
  }
61
+
62
+ export abstract class GetterInstruction extends Instruction {
63
+ // Informs (de)serialization. See Instruction.deserialize.
64
+ static readonly wireFormat: OperandType[] = [OperandType.UINT8, OperandType.UINT8, OperandType.UINT32];
65
+
66
+ constructor(protected indirect: number, protected dstOffset: number) {
67
+ super();
68
+ }
69
+
70
+ public async execute(context: AvmContext): Promise<void> {
71
+ const memoryOperations = { writes: 1, indirect: this.indirect };
72
+ const memory = context.machineState.memory.track(this.type);
73
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
74
+
75
+ memory.set(this.dstOffset, this.getValue(context));
76
+
77
+ memory.assert(memoryOperations);
78
+ context.machineState.incrementPc();
79
+ }
80
+
81
+ protected abstract getValue(env: AvmContext): MemoryValue;
82
+ }
@@ -1,6 +1,6 @@
1
1
  import type { AvmContext } from '../avm_context.js';
2
- import { GasCost, GasCostConstants, getGasCostMultiplierFromTypeTag, makeGasCost } from '../avm_gas_cost.js';
3
- import { Field, TaggedMemory, TypeTag } from '../avm_memory_types.js';
2
+ import { getBaseGasCost, getMemoryGasCost, mulGas, sumGas } from '../avm_gas.js';
3
+ import { Field, type MemoryOperations, TaggedMemory, TypeTag } from '../avm_memory_types.js';
4
4
  import { InstructionExecutionError } from '../errors.js';
5
5
  import { BufferCursor } from '../serialization/buffer_cursor.js';
6
6
  import { Opcode, OperandType, deserialize, serialize } from '../serialization/instruction_serialization.js';
@@ -69,21 +69,22 @@ export class Set extends Instruction {
69
69
  return new this(...args);
70
70
  }
71
71
 
72
- async execute(context: AvmContext): Promise<void> {
72
+ public async execute(context: AvmContext): Promise<void> {
73
+ const memoryOperations = { writes: 1, indirect: this.indirect };
74
+ const memory = context.machineState.memory.track(this.type);
75
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
76
+
73
77
  // Per the YP, the tag cannot be a field.
74
78
  if ([TypeTag.FIELD, TypeTag.UNINITIALIZED, TypeTag.INVALID].includes(this.inTag)) {
75
79
  throw new InstructionExecutionError(`Invalid tag ${TypeTag[this.inTag]} for SET.`);
76
80
  }
77
81
 
78
82
  const res = TaggedMemory.integralFromTag(this.value, this.inTag);
79
- context.machineState.memory.set(this.dstOffset, res);
83
+ memory.set(this.dstOffset, res);
80
84
 
85
+ memory.assert(memoryOperations);
81
86
  context.machineState.incrementPc();
82
87
  }
83
-
84
- protected gasCost(): GasCost {
85
- return makeGasCost({ l2Gas: GasCostConstants.SET_COST_PER_BYTE * getGasCostMultiplierFromTypeTag(this.inTag) });
86
- }
87
88
  }
88
89
 
89
90
  export class CMov extends Instruction {
@@ -109,14 +110,19 @@ export class CMov extends Instruction {
109
110
  super();
110
111
  }
111
112
 
112
- async execute(context: AvmContext): Promise<void> {
113
- const a = context.machineState.memory.get(this.aOffset);
114
- const b = context.machineState.memory.get(this.bOffset);
115
- const cond = context.machineState.memory.get(this.condOffset);
113
+ public async execute(context: AvmContext): Promise<void> {
114
+ const memoryOperations = { reads: 3, writes: 1, indirect: this.indirect };
115
+ const memory = context.machineState.memory.track(this.type);
116
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
117
+
118
+ const a = memory.get(this.aOffset);
119
+ const b = memory.get(this.bOffset);
120
+ const cond = memory.get(this.condOffset);
116
121
 
117
122
  // TODO: reconsider toBigInt() here
118
- context.machineState.memory.set(this.dstOffset, cond.toBigInt() > 0 ? a : b);
123
+ memory.set(this.dstOffset, cond.toBigInt() > 0 ? a : b);
119
124
 
125
+ memory.assert(memoryOperations);
120
126
  context.machineState.incrementPc();
121
127
  }
122
128
  }
@@ -129,15 +135,20 @@ export class Cast extends TwoOperandInstruction {
129
135
  super(indirect, dstTag, aOffset, dstOffset);
130
136
  }
131
137
 
132
- async execute(context: AvmContext): Promise<void> {
133
- const a = context.machineState.memory.get(this.aOffset);
138
+ public async execute(context: AvmContext): Promise<void> {
139
+ const memoryOperations = { reads: 1, writes: 1, indirect: this.indirect };
140
+ const memory = context.machineState.memory.track(this.type);
141
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
142
+
143
+ const a = memory.get(this.aOffset);
134
144
 
135
145
  // TODO: consider not using toBigInt()
136
146
  const casted =
137
147
  this.inTag == TypeTag.FIELD ? new Field(a.toBigInt()) : TaggedMemory.integralFromTag(a.toBigInt(), this.inTag);
138
148
 
139
- context.machineState.memory.set(this.dstOffset, casted);
149
+ memory.set(this.dstOffset, casted);
140
150
 
151
+ memory.assert(memoryOperations);
141
152
  context.machineState.incrementPc();
142
153
  }
143
154
  }
@@ -157,16 +168,18 @@ export class Mov extends Instruction {
157
168
  super();
158
169
  }
159
170
 
160
- async execute(context: AvmContext): Promise<void> {
161
- const [srcOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve(
162
- [this.srcOffset, this.dstOffset],
163
- context.machineState.memory,
164
- );
171
+ public async execute(context: AvmContext): Promise<void> {
172
+ const memoryOperations = { reads: 1, writes: 1, indirect: this.indirect };
173
+ const memory = context.machineState.memory.track(this.type);
174
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
175
+
176
+ const [srcOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve([this.srcOffset, this.dstOffset], memory);
165
177
 
166
- const a = context.machineState.memory.get(srcOffset);
178
+ const a = memory.get(srcOffset);
167
179
 
168
- context.machineState.memory.set(dstOffset, a);
180
+ memory.set(dstOffset, a);
169
181
 
182
+ memory.assert(memoryOperations);
170
183
  context.machineState.incrementPc();
171
184
  }
172
185
  }
@@ -187,19 +200,26 @@ export class CalldataCopy extends Instruction {
187
200
  super();
188
201
  }
189
202
 
190
- async execute(context: AvmContext): Promise<void> {
191
- const [dstOffset] = Addressing.fromWire(this.indirect).resolve([this.dstOffset], context.machineState.memory);
203
+ public async execute(context: AvmContext): Promise<void> {
204
+ const memoryOperations = { writes: this.copySize, indirect: this.indirect };
205
+ const memory = context.machineState.memory.track(this.type);
206
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
207
+
208
+ const [dstOffset] = Addressing.fromWire(this.indirect).resolve([this.dstOffset], memory);
192
209
 
193
210
  const transformedData = context.environment.calldata
194
211
  .slice(this.cdOffset, this.cdOffset + this.copySize)
195
212
  .map(f => new Field(f));
196
213
 
197
- context.machineState.memory.setSlice(dstOffset, transformedData);
214
+ memory.setSlice(dstOffset, transformedData);
198
215
 
216
+ memory.assert(memoryOperations);
199
217
  context.machineState.incrementPc();
200
218
  }
201
219
 
202
- protected gasCost(): GasCost {
203
- return makeGasCost({ l2Gas: GasCostConstants.CALLDATACOPY_COST_PER_BYTE * this.copySize });
220
+ protected gasCost(memoryOps: Partial<MemoryOperations & { indirect: number }> = {}) {
221
+ const baseGasCost = mulGas(getBaseGasCost(this.opcode), this.copySize);
222
+ const memoryGasCost = getMemoryGasCost(memoryOps);
223
+ return sumGas(baseGasCost, memoryGasCost);
204
224
  }
205
225
  }
@@ -1,7 +1,8 @@
1
1
  import { Fr } from '@aztec/foundation/fields';
2
2
 
3
3
  import type { AvmContext } from '../avm_context.js';
4
- import { Field } from '../avm_memory_types.js';
4
+ import { type Gas, getBaseGasCost, getMemoryGasCost, mulGas, sumGas } from '../avm_gas.js';
5
+ import { Field, type MemoryOperations } from '../avm_memory_types.js';
5
6
  import { InstructionExecutionError } from '../errors.js';
6
7
  import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
7
8
  import { Addressing } from './addressing_mode.js';
@@ -25,6 +26,12 @@ abstract class BaseStorageInstruction extends Instruction {
25
26
  ) {
26
27
  super();
27
28
  }
29
+
30
+ protected 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
+ }
28
35
  }
29
36
 
30
37
  export class SStore extends BaseStorageInstruction {
@@ -35,24 +42,26 @@ export class SStore extends BaseStorageInstruction {
35
42
  super(indirect, srcOffset, srcSize, slotOffset);
36
43
  }
37
44
 
38
- async execute(context: AvmContext): Promise<void> {
45
+ public async execute(context: AvmContext): Promise<void> {
39
46
  if (context.environment.isStaticCall) {
40
47
  throw new StaticCallStorageAlterError();
41
48
  }
42
49
 
43
- const [srcOffset, slotOffset] = Addressing.fromWire(this.indirect).resolve(
44
- [this.aOffset, this.bOffset],
45
- context.machineState.memory,
46
- );
50
+ const memoryOperations = { reads: this.size + 1, indirect: this.indirect };
51
+ const memory = context.machineState.memory.track(this.type);
52
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
47
53
 
48
- const slot = context.machineState.memory.get(slotOffset).toFr();
49
- const data = context.machineState.memory.getSlice(srcOffset, this.size).map(field => field.toFr());
54
+ const [srcOffset, slotOffset] = Addressing.fromWire(this.indirect).resolve([this.aOffset, this.bOffset], memory);
55
+
56
+ const slot = memory.get(slotOffset).toFr();
57
+ const data = memory.getSlice(srcOffset, this.size).map(field => field.toFr());
50
58
 
51
59
  for (const [index, value] of Object.entries(data)) {
52
60
  const adjustedSlot = slot.add(new Fr(BigInt(index)));
53
61
  context.persistableState.writeStorage(context.environment.storageAddress, adjustedSlot, value);
54
62
  }
55
63
 
64
+ memory.assert(memoryOperations);
56
65
  context.machineState.incrementPc();
57
66
  }
58
67
  }
@@ -65,13 +74,17 @@ export class SLoad extends BaseStorageInstruction {
65
74
  super(indirect, slotOffset, size, dstOffset);
66
75
  }
67
76
 
68
- async execute(context: AvmContext): Promise<void> {
77
+ public async execute(context: AvmContext): Promise<void> {
78
+ const memoryOperations = { writes: this.size, reads: 1, indirect: this.indirect };
79
+ const memory = context.machineState.memory.track(this.type);
80
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
81
+
69
82
  const [aOffset, size, bOffset] = Addressing.fromWire(this.indirect).resolve(
70
83
  [this.aOffset, this.size, this.bOffset],
71
- context.machineState.memory,
84
+ memory,
72
85
  );
73
86
 
74
- const slot = context.machineState.memory.get(aOffset);
87
+ const slot = memory.get(aOffset);
75
88
 
76
89
  // Write each read value from storage into memory
77
90
  for (let i = 0; i < size; i++) {
@@ -80,10 +93,11 @@ export class SLoad extends BaseStorageInstruction {
80
93
  new Fr(slot.toBigInt() + BigInt(i)),
81
94
  );
82
95
 
83
- context.machineState.memory.set(bOffset + i, new Field(data));
96
+ memory.set(bOffset + i, new Field(data));
84
97
  }
85
98
 
86
99
  context.machineState.incrementPc();
100
+ memory.assert(memoryOperations);
87
101
  }
88
102
  }
89
103
 
@@ -1,3 +1,4 @@
1
+ import { DAGasLeft, L1GasLeft, L2GasLeft } from '../opcodes/context_getters.js';
1
2
  import { Keccak, Pedersen, Poseidon2, Sha256 } from '../opcodes/hashing.js';
2
3
  import {
3
4
  Add,
@@ -18,6 +19,7 @@ import {
18
19
  FeePerL1Gas,
19
20
  FeePerL2Gas,
20
21
  FieldDiv,
22
+ GetContractInstance,
21
23
  InternalCall,
22
24
  InternalReturn,
23
25
  Jump,
@@ -101,9 +103,9 @@ const INSTRUCTION_SET = () =>
101
103
 
102
104
  // Machine State
103
105
  // Machine State - Gas
104
- //[L1gasleft.opcode, L1gasleft],
105
- //[L2gasleft.opcode, L2gasleft],
106
- //[Dagasleft.opcode, Dagasleft],
106
+ [L1GasLeft.opcode, L1GasLeft],
107
+ [L2GasLeft.opcode, L2GasLeft],
108
+ [DAGasLeft.opcode, DAGasLeft],
107
109
  // Machine State - Internal Control Flow
108
110
  [Jump.opcode, Jump],
109
111
  [JumpI.opcode, JumpI],
@@ -126,6 +128,7 @@ const INSTRUCTION_SET = () =>
126
128
  // Accrued Substate
127
129
  [EmitUnencryptedLog.opcode, EmitUnencryptedLog],
128
130
  [SendL2ToL1Message.opcode, SendL2ToL1Message],
131
+ [GetContractInstance.opcode, GetContractInstance],
129
132
 
130
133
  // Control Flow - Contract Calls
131
134
  [Call.opcode, Call],
@@ -64,6 +64,7 @@ export enum Opcode {
64
64
  EMITNULLIFIER,
65
65
  L1TOL2MSGEXISTS,
66
66
  HEADERMEMBER,
67
+ GETCONTRACTINSTANCE,
67
68
  EMITUNENCRYPTEDLOG,
68
69
  SENDL2TOL1MSG,
69
70
  // External calls
@@ -3,19 +3,20 @@ import { UnencryptedFunctionL2Logs } from '@aztec/circuit-types';
3
3
  import {
4
4
  ContractStorageRead,
5
5
  ContractStorageUpdateRequest,
6
- GlobalVariables,
6
+ type GlobalVariables,
7
7
  L2ToL1Message,
8
- ReadRequest,
8
+ type ReadRequest,
9
9
  SideEffect,
10
10
  SideEffectLinkedToNoteHash,
11
11
  } from '@aztec/circuits.js';
12
12
  import { Fr } from '@aztec/foundation/fields';
13
13
 
14
14
  import { createSimulationError } from '../common/errors.js';
15
- import { PublicExecution, PublicExecutionResult } from '../public/execution.js';
15
+ import { type PublicExecution, type PublicExecutionResult } from '../public/execution.js';
16
16
  import { AvmExecutionEnvironment } from './avm_execution_environment.js';
17
- import { AvmContractCallResults } from './avm_message_call_result.js';
18
- import { JournalData } from './journal/journal.js';
17
+ import { type AvmContractCallResults } from './avm_message_call_result.js';
18
+ import { type JournalData } from './journal/journal.js';
19
+ import { Mov } from './opcodes/memory.js';
19
20
 
20
21
  /** Temporary Method
21
22
  *
@@ -95,7 +96,9 @@ export function temporaryConvertAvmResults(
95
96
  const nestedExecutions: PublicExecutionResult[] = [];
96
97
  const nullifierReadRequests: ReadRequest[] = [];
97
98
  const nullifierNonExistentReadRequests: ReadRequest[] = [];
98
- const newNullifiers: SideEffectLinkedToNoteHash[] = [];
99
+ const newNullifiers: SideEffectLinkedToNoteHash[] = newWorldState.newNullifiers.map(
100
+ (nullifier, i) => new SideEffectLinkedToNoteHash(nullifier.toField(), Fr.zero(), new Fr(i + 1)),
101
+ );
99
102
  const unencryptedLogs = UnencryptedFunctionL2Logs.empty();
100
103
  const newL2ToL1Messages = newWorldState.newL1Messages.map(() => L2ToL1Message.empty());
101
104
  // TODO keep track of side effect counters
@@ -120,3 +123,14 @@ export function temporaryConvertAvmResults(
120
123
  revertReason: result.revertReason ? createSimulationError(result.revertReason) : undefined,
121
124
  };
122
125
  }
126
+
127
+ export function isAvmBytecode(bytecode: Buffer): boolean {
128
+ const magicBuf = Buffer.from([
129
+ Mov.opcode, // opcode
130
+ 0x00, // indirect
131
+ ...Buffer.from('000018ca', 'hex'), // srcOffset
132
+ ...Buffer.from('000018ca', 'hex'), // dstOffset
133
+ ]);
134
+ const magicSize = magicBuf.length;
135
+ return bytecode.subarray(-magicSize).equals(magicBuf);
136
+ }
@@ -1,37 +1,37 @@
1
1
  import {
2
- AuthWitness,
3
- AztecNode,
2
+ type AuthWitness,
3
+ type AztecNode,
4
4
  EncryptedFunctionL2Logs,
5
5
  EncryptedL2Log,
6
6
  L1NotePayload,
7
7
  Note,
8
- NoteStatus,
8
+ type NoteStatus,
9
9
  TaggedNote,
10
10
  UnencryptedFunctionL2Logs,
11
- UnencryptedL2Log,
11
+ type UnencryptedL2Log,
12
12
  } from '@aztec/circuit-types';
13
13
  import {
14
14
  CallContext,
15
15
  FunctionData,
16
16
  FunctionSelector,
17
- Header,
17
+ type Header,
18
18
  NoteHashReadRequestMembershipWitness,
19
19
  PublicCallRequest,
20
- SideEffect,
20
+ type SideEffect,
21
21
  TxContext,
22
22
  } from '@aztec/circuits.js';
23
- import { Grumpkin } from '@aztec/circuits.js/barretenberg';
23
+ import { type Grumpkin } from '@aztec/circuits.js/barretenberg';
24
24
  import { computePublicDataTreeLeafSlot, computeUniqueCommitment, siloNoteHash } from '@aztec/circuits.js/hash';
25
- import { FunctionAbi, FunctionArtifact, countArgumentsSize } from '@aztec/foundation/abi';
26
- import { AztecAddress } from '@aztec/foundation/aztec-address';
27
- import { Fr, Point } from '@aztec/foundation/fields';
25
+ import { type FunctionAbi, type FunctionArtifact, countArgumentsSize } from '@aztec/foundation/abi';
26
+ import { type AztecAddress } from '@aztec/foundation/aztec-address';
27
+ import { Fr, type Point } from '@aztec/foundation/fields';
28
28
  import { createDebugLogger } from '@aztec/foundation/log';
29
29
 
30
- import { NoteData, toACVMWitness } from '../acvm/index.js';
31
- import { PackedArgsCache } from '../common/packed_args_cache.js';
32
- import { DBOracle } from './db_oracle.js';
33
- import { ExecutionNoteCache } from './execution_note_cache.js';
34
- import { ExecutionResult, NoteAndSlot } from './execution_result.js';
30
+ import { type NoteData, toACVMWitness } from '../acvm/index.js';
31
+ import { type PackedArgsCache } from '../common/packed_args_cache.js';
32
+ import { type DBOracle } from './db_oracle.js';
33
+ import { type ExecutionNoteCache } from './execution_note_cache.js';
34
+ import { type ExecutionResult, type NoteAndSlot } from './execution_result.js';
35
35
  import { pickNotes } from './pick_notes.js';
36
36
  import { executePrivateFunction } from './private_execution.js';
37
37
  import { ViewDataOracle } from './view_data_oracle.js';
@@ -1,13 +1,19 @@
1
- import { L2Block, MerkleTreeId, NoteStatus, NullifierMembershipWitness, PublicDataWitness } from '@aztec/circuit-types';
2
- import { CompleteAddress, Header } from '@aztec/circuits.js';
3
- import { FunctionArtifactWithDebugMetadata, FunctionSelector } from '@aztec/foundation/abi';
4
- import { AztecAddress } from '@aztec/foundation/aztec-address';
5
- import { EthAddress } from '@aztec/foundation/eth-address';
6
- import { Fr } from '@aztec/foundation/fields';
7
- import { ContractInstance } from '@aztec/types/contracts';
8
-
9
- import { KeyPair, NoteData } from '../acvm/index.js';
10
- import { CommitmentsDB } from '../public/db.js';
1
+ import {
2
+ type L2Block,
3
+ type MerkleTreeId,
4
+ type NoteStatus,
5
+ type NullifierMembershipWitness,
6
+ type PublicDataWitness,
7
+ } from '@aztec/circuit-types';
8
+ import { type CompleteAddress, type Header } from '@aztec/circuits.js';
9
+ import { type FunctionArtifactWithDebugMetadata, type FunctionSelector } from '@aztec/foundation/abi';
10
+ import { type AztecAddress } from '@aztec/foundation/aztec-address';
11
+ import { type EthAddress } from '@aztec/foundation/eth-address';
12
+ import { type Fr } from '@aztec/foundation/fields';
13
+ import { type ContractInstance } from '@aztec/types/contracts';
14
+
15
+ import { type KeyPair, type NoteData } from '../acvm/index.js';
16
+ import { type CommitmentsDB } from '../public/db.js';
11
17
 
12
18
  /**
13
19
  * Error thrown when a contract is not found in the database.
@@ -1,8 +1,8 @@
1
1
  import { siloNullifier } from '@aztec/circuits.js/hash';
2
- import { AztecAddress } from '@aztec/foundation/aztec-address';
2
+ import { type AztecAddress } from '@aztec/foundation/aztec-address';
3
3
  import { Fr } from '@aztec/foundation/fields';
4
4
 
5
- import { NoteData } from '../acvm/index.js';
5
+ import { type NoteData } from '../acvm/index.js';
6
6
 
7
7
  /**
8
8
  * Data that's accessible by all the function calls in an execution.
@@ -1,9 +1,13 @@
1
- import { EncryptedFunctionL2Logs, Note, UnencryptedFunctionL2Logs } from '@aztec/circuit-types';
2
- import { NoteHashReadRequestMembershipWitness, PrivateCallStackItem, PublicCallRequest } from '@aztec/circuits.js';
3
- import { DecodedReturn } from '@aztec/foundation/abi';
4
- import { Fr } from '@aztec/foundation/fields';
1
+ import { type EncryptedFunctionL2Logs, type Note, type UnencryptedFunctionL2Logs } from '@aztec/circuit-types';
2
+ import {
3
+ type NoteHashReadRequestMembershipWitness,
4
+ type PrivateCallStackItem,
5
+ type PublicCallRequest,
6
+ } from '@aztec/circuits.js';
7
+ import { type DecodedReturn } from '@aztec/foundation/abi';
8
+ import { type Fr } from '@aztec/foundation/fields';
5
9
 
6
- import { ACVMField } from '../acvm/index.js';
10
+ import { type ACVMField } from '../acvm/index.js';
7
11
 
8
12
  /**
9
13
  * The contents of a new note.
@@ -1,4 +1,4 @@
1
- import { Comparator, Note } from '@aztec/circuit-types';
1
+ import { Comparator, type Note } from '@aztec/circuit-types';
2
2
  import { Fr } from '@aztec/foundation/fields';
3
3
 
4
4
  export interface PropertySelector {
@@ -1,14 +1,14 @@
1
- import { FunctionData, PrivateCallStackItem, PrivateCircuitPublicInputs } from '@aztec/circuits.js';
2
- import { FunctionArtifactWithDebugMetadata, decodeReturnValues } from '@aztec/foundation/abi';
3
- import { AztecAddress } from '@aztec/foundation/aztec-address';
1
+ import { type FunctionData, PrivateCallStackItem, PrivateCircuitPublicInputs } from '@aztec/circuits.js';
2
+ import { type AbiType, type FunctionArtifactWithDebugMetadata, decodeReturnValues } from '@aztec/foundation/abi';
3
+ import { type AztecAddress } from '@aztec/foundation/aztec-address';
4
4
  import { Fr } from '@aztec/foundation/fields';
5
5
  import { createDebugLogger } from '@aztec/foundation/log';
6
6
 
7
7
  import { extractReturnWitness } from '../acvm/deserialize.js';
8
8
  import { Oracle, acvm, extractCallStack } from '../acvm/index.js';
9
9
  import { ExecutionError } from '../common/errors.js';
10
- import { ClientExecutionContext } from './client_execution_context.js';
11
- import { ExecutionResult } from './execution_result.js';
10
+ import { type ClientExecutionContext } from './client_execution_context.js';
11
+ import { type ExecutionResult } from './execution_result.js';
12
12
  import { AcirSimulator } from './simulator.js';
13
13
 
14
14
  /**
@@ -52,7 +52,13 @@ export async function executePrivateFunction(
52
52
  publicInputs.unencryptedLogPreimagesLength = new Fr(unencryptedLogs.getSerializedLength());
53
53
 
54
54
  const callStackItem = new PrivateCallStackItem(contractAddress, functionData, publicInputs);
55
- const returnValues = decodeReturnValues(artifact, publicInputs.returnValues);
55
+
56
+ // Mocking the return type to be an array of 4 fields
57
+ // TODO: @LHerskind must be updated as we are progressing with the macros to get the information
58
+ const returnTypes: AbiType[] = [{ kind: 'array', length: 4, type: { kind: 'field' } }];
59
+ const mockArtifact = { ...artifact, returnTypes };
60
+ const returnValues = decodeReturnValues(mockArtifact, publicInputs.returnValues);
61
+
56
62
  const noteHashReadRequestPartialWitnesses = context.getNoteHashReadRequestPartialWitnesses(
57
63
  publicInputs.noteHashReadRequests,
58
64
  );
@@ -1,26 +1,26 @@
1
- import { AztecNode, FunctionCall, Note, TxExecutionRequest } from '@aztec/circuit-types';
1
+ import { type AztecNode, type FunctionCall, type Note, type TxExecutionRequest } from '@aztec/circuit-types';
2
2
  import { CallContext, FunctionData } from '@aztec/circuits.js';
3
3
  import { Grumpkin } from '@aztec/circuits.js/barretenberg';
4
4
  import {
5
- ArrayType,
6
- FunctionArtifactWithDebugMetadata,
5
+ type ArrayType,
6
+ type FunctionArtifactWithDebugMetadata,
7
7
  FunctionSelector,
8
8
  FunctionType,
9
9
  encodeArguments,
10
10
  } from '@aztec/foundation/abi';
11
11
  import { AztecAddress } from '@aztec/foundation/aztec-address';
12
- import { EthAddress } from '@aztec/foundation/eth-address';
12
+ import { type EthAddress } from '@aztec/foundation/eth-address';
13
13
  import { Fr } from '@aztec/foundation/fields';
14
- import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
14
+ import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';
15
15
 
16
- import { WasmBlackBoxFunctionSolver, createBlackBoxSolver } from '@noir-lang/acvm_js';
16
+ import { type WasmBlackBoxFunctionSolver, createBlackBoxSolver } from '@noir-lang/acvm_js';
17
17
 
18
18
  import { createSimulationError } from '../common/errors.js';
19
19
  import { PackedArgsCache } from '../common/packed_args_cache.js';
20
20
  import { ClientExecutionContext } from './client_execution_context.js';
21
- import { DBOracle } from './db_oracle.js';
21
+ import { type DBOracle } from './db_oracle.js';
22
22
  import { ExecutionNoteCache } from './execution_note_cache.js';
23
- import { ExecutionResult } from './execution_result.js';
23
+ import { type ExecutionResult } from './execution_result.js';
24
24
  import { executePrivateFunction } from './private_execution.js';
25
25
  import { executeUnconstrainedFunction } from './unconstrained_execution.js';
26
26
  import { ViewDataOracle } from './view_data_oracle.js';
@@ -1,14 +1,14 @@
1
- import { FunctionData } from '@aztec/circuits.js';
2
- import { DecodedReturn, FunctionArtifactWithDebugMetadata, decodeReturnValues } from '@aztec/foundation/abi';
3
- import { AztecAddress } from '@aztec/foundation/aztec-address';
4
- import { Fr } from '@aztec/foundation/fields';
1
+ import { type FunctionData } from '@aztec/circuits.js';
2
+ import { type DecodedReturn, type FunctionArtifactWithDebugMetadata, decodeReturnValues } from '@aztec/foundation/abi';
3
+ import { type AztecAddress } from '@aztec/foundation/aztec-address';
4
+ import { type Fr } from '@aztec/foundation/fields';
5
5
  import { createDebugLogger } from '@aztec/foundation/log';
6
6
 
7
7
  import { extractReturnWitness } from '../acvm/deserialize.js';
8
8
  import { Oracle, acvm, extractCallStack, toACVMWitness } from '../acvm/index.js';
9
9
  import { ExecutionError } from '../common/errors.js';
10
10
  import { AcirSimulator } from './simulator.js';
11
- import { ViewDataOracle } from './view_data_oracle.js';
11
+ import { type ViewDataOracle } from './view_data_oracle.js';
12
12
 
13
13
  // docs:start:execute_unconstrained_function
14
14
  /**