@aztec/simulator 0.23.0 → 0.26.1

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 (177) hide show
  1. package/dest/acvm/deserialize.d.ts +5 -0
  2. package/dest/acvm/deserialize.d.ts.map +1 -1
  3. package/dest/acvm/deserialize.js +8 -1
  4. package/dest/acvm/oracle/oracle.d.ts +7 -6
  5. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  6. package/dest/acvm/oracle/oracle.js +28 -15
  7. package/dest/acvm/oracle/typed_oracle.d.ts +9 -11
  8. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  9. package/dest/acvm/oracle/typed_oracle.js +11 -11
  10. package/dest/avm/avm_context.d.ts +4 -4
  11. package/dest/avm/avm_context.d.ts.map +1 -1
  12. package/dest/avm/avm_context.js +6 -6
  13. package/dest/avm/avm_execution_environment.d.ts +3 -2
  14. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  15. package/dest/avm/avm_execution_environment.js +6 -5
  16. package/dest/avm/avm_memory_types.d.ts +127 -37
  17. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  18. package/dest/avm/avm_memory_types.js +98 -106
  19. package/dest/avm/avm_simulator.d.ts +6 -4
  20. package/dest/avm/avm_simulator.d.ts.map +1 -1
  21. package/dest/avm/avm_simulator.js +17 -19
  22. package/dest/avm/errors.d.ts +3 -1
  23. package/dest/avm/errors.d.ts.map +1 -1
  24. package/dest/avm/errors.js +9 -3
  25. package/dest/avm/fixtures/index.d.ts +21 -5
  26. package/dest/avm/fixtures/index.d.ts.map +1 -1
  27. package/dest/avm/fixtures/index.js +28 -9
  28. package/dest/avm/journal/host_storage.d.ts +1 -1
  29. package/dest/avm/journal/host_storage.d.ts.map +1 -1
  30. package/dest/avm/journal/host_storage.js +1 -1
  31. package/dest/avm/journal/journal.d.ts +78 -50
  32. package/dest/avm/journal/journal.d.ts.map +1 -1
  33. package/dest/avm/journal/journal.js +125 -169
  34. package/dest/avm/journal/nullifiers.d.ts +85 -0
  35. package/dest/avm/journal/nullifiers.d.ts.map +1 -0
  36. package/dest/avm/journal/nullifiers.js +147 -0
  37. package/dest/avm/journal/public_storage.d.ts +88 -0
  38. package/dest/avm/journal/public_storage.d.ts.map +1 -0
  39. package/dest/avm/journal/public_storage.js +135 -0
  40. package/dest/avm/journal/trace.d.ts +43 -0
  41. package/dest/avm/journal/trace.d.ts.map +1 -0
  42. package/dest/avm/journal/trace.js +204 -0
  43. package/dest/avm/journal/trace_types.d.ts +26 -0
  44. package/dest/avm/journal/trace_types.d.ts.map +1 -0
  45. package/dest/avm/journal/trace_types.js +6 -0
  46. package/dest/avm/opcodes/accrued_substate.d.ts +37 -4
  47. package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
  48. package/dest/avm/opcodes/accrued_substate.js +109 -12
  49. package/dest/avm/opcodes/addressing_mode.d.ts +24 -0
  50. package/dest/avm/opcodes/addressing_mode.d.ts.map +1 -0
  51. package/dest/avm/opcodes/addressing_mode.js +62 -0
  52. package/dest/avm/opcodes/environment_getters.d.ts +14 -13
  53. package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
  54. package/dest/avm/opcodes/environment_getters.js +1 -1
  55. package/dest/avm/opcodes/external_calls.js +5 -5
  56. package/dest/avm/opcodes/hashing.d.ts +48 -0
  57. package/dest/avm/opcodes/hashing.d.ts.map +1 -0
  58. package/dest/avm/opcodes/hashing.js +127 -0
  59. package/dest/avm/opcodes/instruction.d.ts +4 -4
  60. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  61. package/dest/avm/opcodes/instruction.js +1 -1
  62. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  63. package/dest/avm/opcodes/memory.js +5 -3
  64. package/dest/avm/opcodes/storage.d.ts.map +1 -1
  65. package/dest/avm/opcodes/storage.js +3 -3
  66. package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  67. package/dest/avm/serialization/bytecode_serialization.js +28 -22
  68. package/dest/avm/serialization/instruction_serialization.d.ts +21 -16
  69. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  70. package/dest/avm/serialization/instruction_serialization.js +23 -18
  71. package/dest/avm/temporary_executor_migration.d.ts +25 -0
  72. package/dest/avm/temporary_executor_migration.d.ts.map +1 -0
  73. package/dest/avm/temporary_executor_migration.js +71 -0
  74. package/dest/client/client_execution_context.d.ts +13 -7
  75. package/dest/client/client_execution_context.d.ts.map +1 -1
  76. package/dest/client/client_execution_context.js +52 -27
  77. package/dest/client/db_oracle.d.ts +7 -0
  78. package/dest/client/db_oracle.d.ts.map +1 -1
  79. package/dest/client/db_oracle.js +1 -1
  80. package/dest/client/execution_note_cache.js +1 -1
  81. package/dest/client/execution_result.d.ts +4 -2
  82. package/dest/client/execution_result.d.ts.map +1 -1
  83. package/dest/client/execution_result.js +1 -1
  84. package/dest/client/private_execution.d.ts.map +1 -1
  85. package/dest/client/private_execution.js +4 -4
  86. package/dest/client/simulator.d.ts +11 -6
  87. package/dest/client/simulator.d.ts.map +1 -1
  88. package/dest/client/simulator.js +21 -12
  89. package/dest/client/unconstrained_execution.js +2 -2
  90. package/dest/client/view_data_oracle.d.ts +9 -2
  91. package/dest/client/view_data_oracle.d.ts.map +1 -1
  92. package/dest/client/view_data_oracle.js +13 -5
  93. package/dest/public/db.d.ts +17 -4
  94. package/dest/public/db.d.ts.map +1 -1
  95. package/dest/public/execution.d.ts +9 -4
  96. package/dest/public/execution.d.ts.map +1 -1
  97. package/dest/public/execution.js +18 -5
  98. package/dest/public/executor.d.ts +7 -0
  99. package/dest/public/executor.d.ts.map +1 -1
  100. package/dest/public/executor.js +40 -6
  101. package/dest/public/public_execution_context.d.ts +5 -4
  102. package/dest/public/public_execution_context.d.ts.map +1 -1
  103. package/dest/public/public_execution_context.js +24 -13
  104. package/dest/public/state_actions.d.ts +1 -1
  105. package/dest/public/state_actions.d.ts.map +1 -1
  106. package/dest/public/state_actions.js +6 -7
  107. package/dest/test/utils.js +4 -4
  108. package/dest/utils.d.ts +5 -20
  109. package/dest/utils.d.ts.map +1 -1
  110. package/dest/utils.js +4 -21
  111. package/package.json +9 -6
  112. package/src/acvm/acvm.ts +156 -0
  113. package/src/acvm/acvm_types.ts +11 -0
  114. package/src/acvm/deserialize.ts +44 -0
  115. package/src/acvm/index.ts +5 -0
  116. package/src/acvm/oracle/debug.ts +109 -0
  117. package/src/acvm/oracle/index.ts +17 -0
  118. package/src/acvm/oracle/oracle.ts +356 -0
  119. package/src/acvm/oracle/typed_oracle.ts +225 -0
  120. package/src/acvm/serialize.ts +75 -0
  121. package/src/avm/avm_context.ts +63 -0
  122. package/src/avm/avm_execution_environment.ts +98 -0
  123. package/src/avm/avm_machine_state.ts +93 -0
  124. package/src/avm/avm_memory_types.ts +324 -0
  125. package/src/avm/avm_message_call_result.ts +29 -0
  126. package/src/avm/avm_simulator.ts +87 -0
  127. package/src/avm/errors.ts +57 -0
  128. package/src/avm/fixtures/index.ts +115 -0
  129. package/src/avm/journal/host_storage.ts +14 -0
  130. package/src/avm/journal/index.ts +2 -0
  131. package/src/avm/journal/journal.ts +231 -0
  132. package/src/avm/journal/nullifiers.ts +170 -0
  133. package/src/avm/journal/public_storage.ts +149 -0
  134. package/src/avm/journal/trace.ts +223 -0
  135. package/src/avm/journal/trace_types.ts +79 -0
  136. package/src/avm/opcodes/.eslintrc.cjs +8 -0
  137. package/src/avm/opcodes/accrued_substate.ts +214 -0
  138. package/src/avm/opcodes/addressing_mode.ts +66 -0
  139. package/src/avm/opcodes/arithmetic.ts +79 -0
  140. package/src/avm/opcodes/bitwise.ts +129 -0
  141. package/src/avm/opcodes/comparators.ts +69 -0
  142. package/src/avm/opcodes/control_flow.ts +129 -0
  143. package/src/avm/opcodes/environment_getters.ts +201 -0
  144. package/src/avm/opcodes/external_calls.ts +122 -0
  145. package/src/avm/opcodes/hashing.ts +170 -0
  146. package/src/avm/opcodes/index.ts +10 -0
  147. package/src/avm/opcodes/instruction.ts +64 -0
  148. package/src/avm/opcodes/instruction_impl.ts +52 -0
  149. package/src/avm/opcodes/memory.ts +194 -0
  150. package/src/avm/opcodes/storage.ts +79 -0
  151. package/src/avm/serialization/buffer_cursor.ts +109 -0
  152. package/src/avm/serialization/bytecode_serialization.ts +179 -0
  153. package/src/avm/serialization/instruction_serialization.ts +170 -0
  154. package/src/avm/temporary_executor_migration.ts +109 -0
  155. package/src/client/client_execution_context.ts +502 -0
  156. package/src/client/db_oracle.ts +192 -0
  157. package/src/client/execution_note_cache.ts +90 -0
  158. package/src/client/execution_result.ts +89 -0
  159. package/src/client/index.ts +3 -0
  160. package/src/client/pick_notes.ts +125 -0
  161. package/src/client/private_execution.ts +79 -0
  162. package/src/client/simulator.ts +317 -0
  163. package/src/client/unconstrained_execution.ts +49 -0
  164. package/src/client/view_data_oracle.ts +253 -0
  165. package/src/common/errors.ts +61 -0
  166. package/src/common/index.ts +3 -0
  167. package/src/common/packed_args_cache.ts +55 -0
  168. package/src/common/side_effect_counter.ts +12 -0
  169. package/src/index.ts +3 -0
  170. package/src/public/db.ts +100 -0
  171. package/src/public/execution.ts +161 -0
  172. package/src/public/executor.ts +178 -0
  173. package/src/public/index.ts +9 -0
  174. package/src/public/public_execution_context.ts +241 -0
  175. package/src/public/state_actions.ts +100 -0
  176. package/src/test/utils.ts +38 -0
  177. package/src/utils.ts +18 -0
@@ -0,0 +1,324 @@
1
+ import { toBufferBE } from '@aztec/foundation/bigint-buffer';
2
+ import { Fr } from '@aztec/foundation/fields';
3
+
4
+ import { strict as assert } from 'assert';
5
+
6
+ import { TagCheckError } from './errors.js';
7
+
8
+ /** MemoryValue gathers the common operations for all memory types. */
9
+ export abstract class MemoryValue {
10
+ public abstract add(rhs: MemoryValue): MemoryValue;
11
+ public abstract sub(rhs: MemoryValue): MemoryValue;
12
+ public abstract mul(rhs: MemoryValue): MemoryValue;
13
+ public abstract div(rhs: MemoryValue): MemoryValue;
14
+
15
+ public abstract equals(rhs: MemoryValue): boolean;
16
+ public abstract lt(rhs: MemoryValue): boolean;
17
+
18
+ // We need this to be able to build an instance of the subclasses.
19
+ public abstract build(n: bigint): MemoryValue;
20
+
21
+ // Use sparingly.
22
+ public abstract toBigInt(): bigint;
23
+
24
+ // To Buffer
25
+ public abstract toBuffer(): Buffer;
26
+
27
+ // To field
28
+ public toFr(): Fr {
29
+ return new Fr(this.toBigInt());
30
+ }
31
+ }
32
+
33
+ /** IntegralValue gathers the common operations for all integral memory types. */
34
+ export abstract class IntegralValue extends MemoryValue {
35
+ public abstract shl(rhs: IntegralValue): IntegralValue;
36
+ public abstract shr(rhs: IntegralValue): IntegralValue;
37
+ public abstract and(rhs: IntegralValue): IntegralValue;
38
+ public abstract or(rhs: IntegralValue): IntegralValue;
39
+ public abstract xor(rhs: IntegralValue): IntegralValue;
40
+ public abstract not(): IntegralValue;
41
+ }
42
+
43
+ /**
44
+ * This function creates a class for unsigned integers of a given number of bits.
45
+ * In TypeScript terms, it's a class mixin.
46
+ **/
47
+ function UnsignedIntegerClassFactory(bits: number) {
48
+ return class NewUintClass extends IntegralValue {
49
+ static readonly mod: bigint = 1n << BigInt(bits);
50
+ static readonly bitmask: bigint = this.mod - 1n;
51
+ public readonly n: bigint; // Cannot be private due to TS limitations.
52
+
53
+ public constructor(n: bigint | number) {
54
+ super();
55
+ this.n = BigInt(n);
56
+ assert(n < NewUintClass.mod, `Value ${n} is too large for ${this.constructor.name}.`);
57
+ }
58
+
59
+ public build(n: bigint): NewUintClass {
60
+ return new this.constructor.prototype.constructor(n);
61
+ }
62
+
63
+ public add(rhs: NewUintClass): NewUintClass {
64
+ return this.build((this.n + rhs.n) & NewUintClass.bitmask);
65
+ }
66
+
67
+ public sub(rhs: NewUintClass): NewUintClass {
68
+ const res: bigint = this.n - rhs.n;
69
+ return this.build(res >= 0 ? res : res + NewUintClass.mod);
70
+ }
71
+
72
+ public mul(rhs: NewUintClass): NewUintClass {
73
+ return this.build((this.n * rhs.n) & NewUintClass.bitmask);
74
+ }
75
+
76
+ public div(rhs: NewUintClass): NewUintClass {
77
+ return this.build(this.n / rhs.n);
78
+ }
79
+
80
+ // No sign extension.
81
+ public shr(rhs: NewUintClass): NewUintClass {
82
+ // Note that this.n is > 0 by class invariant.
83
+ return this.build(this.n >> rhs.n);
84
+ }
85
+
86
+ public shl(rhs: NewUintClass): NewUintClass {
87
+ return this.build((this.n << rhs.n) & NewUintClass.bitmask);
88
+ }
89
+
90
+ public and(rhs: NewUintClass): NewUintClass {
91
+ return this.build(this.n & rhs.n);
92
+ }
93
+
94
+ public or(rhs: NewUintClass): NewUintClass {
95
+ return this.build(this.n | rhs.n);
96
+ }
97
+
98
+ public xor(rhs: NewUintClass): NewUintClass {
99
+ return this.build(this.n ^ rhs.n);
100
+ }
101
+
102
+ public not(): NewUintClass {
103
+ return this.build(~this.n & NewUintClass.bitmask);
104
+ }
105
+
106
+ public equals(rhs: NewUintClass): boolean {
107
+ return this.n === rhs.n;
108
+ }
109
+
110
+ public lt(rhs: NewUintClass): boolean {
111
+ return this.n < rhs.n;
112
+ }
113
+
114
+ public toBigInt(): bigint {
115
+ return this.n;
116
+ }
117
+
118
+ public toBuffer(): Buffer {
119
+ return toBufferBE(this.n, bits / 8);
120
+ }
121
+ };
122
+ }
123
+
124
+ // Now we can create the classes for each unsigned integer type.
125
+ // We extend instead of just assigning so that the class has the right name.
126
+ // Otherwise they are all called "NewUintClass".
127
+ export class Uint8 extends UnsignedIntegerClassFactory(8) {}
128
+ export class Uint16 extends UnsignedIntegerClassFactory(16) {}
129
+ export class Uint32 extends UnsignedIntegerClassFactory(32) {}
130
+ export class Uint64 extends UnsignedIntegerClassFactory(64) {}
131
+ export class Uint128 extends UnsignedIntegerClassFactory(128) {}
132
+
133
+ export class Field extends MemoryValue {
134
+ public static readonly MODULUS: bigint = Fr.MODULUS;
135
+ private readonly rep: Fr;
136
+
137
+ constructor(v: number | bigint | Fr | Buffer) {
138
+ super();
139
+ this.rep = new Fr(v);
140
+ }
141
+
142
+ public build(n: bigint): Field {
143
+ return new Field(n);
144
+ }
145
+
146
+ public add(rhs: Field): Field {
147
+ return new Field(this.rep.add(rhs.rep));
148
+ }
149
+
150
+ public sub(rhs: Field): Field {
151
+ return new Field(this.rep.sub(rhs.rep));
152
+ }
153
+
154
+ public mul(rhs: Field): Field {
155
+ return new Field(this.rep.mul(rhs.rep));
156
+ }
157
+
158
+ public div(rhs: Field): Field {
159
+ return new Field(this.rep.div(rhs.rep));
160
+ }
161
+
162
+ public equals(rhs: Field): boolean {
163
+ return this.rep.equals(rhs.rep);
164
+ }
165
+
166
+ public lt(rhs: Field): boolean {
167
+ return this.rep.lt(rhs.rep);
168
+ }
169
+
170
+ public toBigInt(): bigint {
171
+ return this.rep.toBigInt();
172
+ }
173
+
174
+ public toBuffer(): Buffer {
175
+ return this.rep.toBuffer();
176
+ }
177
+ }
178
+
179
+ export enum TypeTag {
180
+ UNINITIALIZED,
181
+ UINT8,
182
+ UINT16,
183
+ UINT32,
184
+ UINT64,
185
+ UINT128,
186
+ FIELD,
187
+ INVALID,
188
+ }
189
+
190
+ // TODO: Consider automatic conversion when getting undefined values.
191
+ export class TaggedMemory {
192
+ // FIXME: memory should be 2^32, but TS doesn't allow for arrays that big.
193
+ static readonly MAX_MEMORY_SIZE = Number((1n << 32n) - 2n);
194
+ private _mem: MemoryValue[];
195
+
196
+ constructor() {
197
+ // We do not initialize memory size here because otherwise tests blow up when diffing.
198
+ this._mem = [];
199
+ }
200
+
201
+ public get(offset: number): MemoryValue {
202
+ assert(offset < TaggedMemory.MAX_MEMORY_SIZE);
203
+ return this.getAs<MemoryValue>(offset);
204
+ }
205
+
206
+ public getAs<T>(offset: number): T {
207
+ assert(offset < TaggedMemory.MAX_MEMORY_SIZE);
208
+ const word = this._mem[offset];
209
+ return word as T;
210
+ }
211
+
212
+ public getSlice(offset: number, size: number): MemoryValue[] {
213
+ assert(offset < TaggedMemory.MAX_MEMORY_SIZE);
214
+ assert(offset + size < TaggedMemory.MAX_MEMORY_SIZE);
215
+ return this._mem.slice(offset, offset + size);
216
+ }
217
+
218
+ public getSliceAs<T>(offset: number, size: number): T[] {
219
+ assert(offset < TaggedMemory.MAX_MEMORY_SIZE);
220
+ assert(offset + size < TaggedMemory.MAX_MEMORY_SIZE);
221
+ return this._mem.slice(offset, offset + size) as T[];
222
+ }
223
+
224
+ public getSliceTags(offset: number, size: number): TypeTag[] {
225
+ assert(offset < TaggedMemory.MAX_MEMORY_SIZE);
226
+ assert(offset + size < TaggedMemory.MAX_MEMORY_SIZE);
227
+ return this._mem.slice(offset, offset + size).map(TaggedMemory.getTag);
228
+ }
229
+
230
+ public set(offset: number, v: MemoryValue) {
231
+ assert(offset < TaggedMemory.MAX_MEMORY_SIZE);
232
+ this._mem[offset] = v;
233
+ }
234
+
235
+ public setSlice(offset: number, vs: MemoryValue[]) {
236
+ assert(offset < TaggedMemory.MAX_MEMORY_SIZE);
237
+ assert(offset + vs.length < TaggedMemory.MAX_MEMORY_SIZE);
238
+ // We may need to extend the memory size, otherwise splice doesn't insert.
239
+ if (offset + vs.length > this._mem.length) {
240
+ this._mem.length = offset + vs.length;
241
+ }
242
+ this._mem.splice(offset, vs.length, ...vs);
243
+ }
244
+
245
+ public getTag(offset: number): TypeTag {
246
+ return TaggedMemory.getTag(this._mem[offset]);
247
+ }
248
+
249
+ /**
250
+ * Check that the memory at the given offset matches the specified tag.
251
+ */
252
+ public checkTag(tag: TypeTag, offset: number) {
253
+ if (this.getTag(offset) !== tag) {
254
+ throw TagCheckError.forOffset(offset, TypeTag[this.getTag(offset)], TypeTag[tag]);
255
+ }
256
+ }
257
+
258
+ public static checkIsIntegralTag(tag: TypeTag) {
259
+ if (![TypeTag.UINT8, TypeTag.UINT16, TypeTag.UINT32, TypeTag.UINT64, TypeTag.UINT128].includes(tag)) {
260
+ throw TagCheckError.forTag(TypeTag[tag], 'integral');
261
+ }
262
+ }
263
+
264
+ /**
265
+ * Check tags for memory at all of the specified offsets.
266
+ */
267
+ public checkTags(tag: TypeTag, ...offsets: number[]) {
268
+ for (const offset of offsets) {
269
+ this.checkTag(tag, offset);
270
+ }
271
+ }
272
+
273
+ /**
274
+ * Check tags for all memory in the specified range.
275
+ */
276
+ public checkTagsRange(tag: TypeTag, startOffset: number, size: number) {
277
+ for (let offset = startOffset; offset < startOffset + size; offset++) {
278
+ this.checkTag(tag, offset);
279
+ }
280
+ }
281
+
282
+ // TODO: this might be slow, but I don't want to have the types know of their tags.
283
+ // It might be possible to have a map<Prototype, TypeTag>.
284
+ public static getTag(v: MemoryValue | undefined): TypeTag {
285
+ let tag = TypeTag.INVALID;
286
+
287
+ if (v === undefined) {
288
+ tag = TypeTag.UNINITIALIZED;
289
+ } else if (v instanceof Field) {
290
+ tag = TypeTag.FIELD;
291
+ } else if (v instanceof Uint8) {
292
+ tag = TypeTag.UINT8;
293
+ } else if (v instanceof Uint16) {
294
+ tag = TypeTag.UINT16;
295
+ } else if (v instanceof Uint32) {
296
+ tag = TypeTag.UINT32;
297
+ } else if (v instanceof Uint64) {
298
+ tag = TypeTag.UINT64;
299
+ } else if (v instanceof Uint128) {
300
+ tag = TypeTag.UINT128;
301
+ }
302
+
303
+ return tag;
304
+ }
305
+
306
+ // Truncates the value to fit the type.
307
+ public static integralFromTag(v: bigint | number, tag: TypeTag): IntegralValue {
308
+ v = BigInt(v);
309
+ switch (tag) {
310
+ case TypeTag.UINT8:
311
+ return new Uint8(v & ((1n << 8n) - 1n));
312
+ case TypeTag.UINT16:
313
+ return new Uint16(v & ((1n << 16n) - 1n));
314
+ case TypeTag.UINT32:
315
+ return new Uint32(v & ((1n << 32n) - 1n));
316
+ case TypeTag.UINT64:
317
+ return new Uint64(v & ((1n << 64n) - 1n));
318
+ case TypeTag.UINT128:
319
+ return new Uint128(v & ((1n << 128n) - 1n));
320
+ default:
321
+ throw new Error(`${TypeTag[tag]} is not a valid integral type.`);
322
+ }
323
+ }
324
+ }
@@ -0,0 +1,29 @@
1
+ import { Fr } from '@aztec/foundation/fields';
2
+
3
+ /**
4
+ * Results of an contract call's execution in the AVM.
5
+ */
6
+ export class AvmContractCallResults {
7
+ public readonly reverted: boolean;
8
+ public readonly output: Fr[];
9
+
10
+ /** For exceptional halts */
11
+ public readonly revertReason: Error | undefined;
12
+
13
+ constructor(reverted: boolean, output: Fr[], revertReason?: Error) {
14
+ this.reverted = reverted;
15
+ this.output = output;
16
+ this.revertReason = revertReason;
17
+ }
18
+
19
+ /**
20
+ * Generate a string representation of call results.
21
+ */
22
+ toString(): string {
23
+ let resultsStr = `reverted: ${this.reverted}, output: ${this.output}`;
24
+ if (this.revertReason) {
25
+ resultsStr += `, revertReason: ${this.revertReason}`;
26
+ }
27
+ return resultsStr;
28
+ }
29
+ }
@@ -0,0 +1,87 @@
1
+ import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
2
+
3
+ import { strict as assert } from 'assert';
4
+
5
+ import type { AvmContext } from './avm_context.js';
6
+ import { AvmContractCallResults } from './avm_message_call_result.js';
7
+ import { AvmExecutionError, InvalidProgramCounterError, NoBytecodeForContractError } from './errors.js';
8
+ import type { Instruction } from './opcodes/index.js';
9
+ import { decodeFromBytecode } from './serialization/bytecode_serialization.js';
10
+
11
+ export class AvmSimulator {
12
+ private log: DebugLogger = createDebugLogger('aztec:avm_simulator');
13
+
14
+ constructor(private context: AvmContext) {}
15
+
16
+ /**
17
+ * Fetch the bytecode and execute it in the current context.
18
+ */
19
+ public async execute(): Promise<AvmContractCallResults> {
20
+ const selector = this.context.environment.temporaryFunctionSelector;
21
+ const bytecode = await this.context.persistableState.hostStorage.contractsDb.getBytecode(
22
+ this.context.environment.address,
23
+ selector,
24
+ );
25
+
26
+ // This assumes that we will not be able to send messages to accounts without code
27
+ // Pending classes and instances impl details
28
+ if (!bytecode) {
29
+ throw new NoBytecodeForContractError(this.context.environment.address);
30
+ }
31
+
32
+ return await this.executeBytecode(bytecode);
33
+ }
34
+
35
+ /**
36
+ * Executes the provided bytecode in the current context.
37
+ * This method is useful for testing and debugging.
38
+ */
39
+ public async executeBytecode(bytecode: Buffer): Promise<AvmContractCallResults> {
40
+ return await this.executeInstructions(decodeFromBytecode(bytecode));
41
+ }
42
+
43
+ /**
44
+ * Executes the provided instructions in the current context.
45
+ * This method is useful for testing and debugging.
46
+ */
47
+ public async executeInstructions(instructions: Instruction[]): Promise<AvmContractCallResults> {
48
+ assert(instructions.length > 0);
49
+
50
+ try {
51
+ // Execute instruction pointed to by the current program counter
52
+ // continuing until the machine state signifies a halt
53
+ while (!this.context.machineState.halted) {
54
+ const instruction = instructions[this.context.machineState.pc];
55
+ assert(!!instruction); // This should never happen
56
+
57
+ this.log.debug(`@${this.context.machineState.pc} ${instruction.toString()}`);
58
+ // Execute the instruction.
59
+ // Normal returns and reverts will return normally here.
60
+ // "Exceptional halts" will throw.
61
+ await instruction.execute(this.context);
62
+
63
+ if (this.context.machineState.pc >= instructions.length) {
64
+ this.log('Passed end of program!');
65
+ throw new InvalidProgramCounterError(this.context.machineState.pc, /*max=*/ instructions.length);
66
+ }
67
+ }
68
+
69
+ // Return results for processing by calling context
70
+ const results = this.context.machineState.getResults();
71
+ this.log(`Context execution results: ${results.toString()}`);
72
+ return results;
73
+ } catch (e) {
74
+ this.log('Exceptional halt');
75
+ if (!(e instanceof AvmExecutionError)) {
76
+ this.log(`Unknown error thrown by avm: ${e}`);
77
+ throw e;
78
+ }
79
+
80
+ // Return results for processing by calling context
81
+ // Note: "exceptional halts" cannot return data
82
+ const results = new AvmContractCallResults(/*reverted=*/ true, /*output=*/ [], /*revertReason=*/ e);
83
+ this.log(`Context execution results: ${results.toString()}`);
84
+ return results;
85
+ }
86
+ }
87
+ }
@@ -0,0 +1,57 @@
1
+ import { AztecAddress } from '@aztec/circuits.js';
2
+
3
+ /**
4
+ * Avm-specific errors should derive from this
5
+ */
6
+ export abstract class AvmExecutionError extends Error {
7
+ constructor(message: string, ...rest: any[]) {
8
+ super(message, ...rest);
9
+ this.name = 'AvmInterpreterError';
10
+ }
11
+ }
12
+
13
+ export class NoBytecodeForContractError extends AvmExecutionError {
14
+ constructor(contractAddress: AztecAddress) {
15
+ super(`No bytecode found at: ${contractAddress}`);
16
+ this.name = 'NoBytecodeFoundInterpreterError';
17
+ }
18
+ }
19
+
20
+ /**
21
+ * Error is thrown when the program counter goes to an invalid location.
22
+ * There is no instruction at the provided pc
23
+ */
24
+ export class InvalidProgramCounterError extends AvmExecutionError {
25
+ constructor(pc: number, max: number) {
26
+ super(`Invalid program counter ${pc}, max is ${max}`);
27
+ this.name = 'InvalidProgramCounterError';
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Error thrown during an instruction's execution (during its execute()).
33
+ */
34
+ export class InstructionExecutionError extends AvmExecutionError {
35
+ constructor(message: string) {
36
+ super(message);
37
+ this.name = 'InstructionExecutionError';
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Error thrown on failed AVM memory tag check.
43
+ */
44
+ export class TagCheckError extends AvmExecutionError {
45
+ public static forOffset(offset: number, gotTag: string, expectedTag: string): TagCheckError {
46
+ return new TagCheckError(`Tag mismatch at offset ${offset}, got ${gotTag}, expected ${expectedTag}`);
47
+ }
48
+
49
+ public static forTag(gotTag: string, expectedTag: string): TagCheckError {
50
+ return new TagCheckError(`Tag mismatch, got ${gotTag}, expected ${expectedTag}`);
51
+ }
52
+
53
+ constructor(message: string) {
54
+ super(message);
55
+ this.name = 'TagCheckError';
56
+ }
57
+ }
@@ -0,0 +1,115 @@
1
+ import { SiblingPath } from '@aztec/circuit-types';
2
+ import { GlobalVariables, L1_TO_L2_MSG_TREE_HEIGHT } from '@aztec/circuits.js';
3
+ import { 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
+
8
+ import { mock } from 'jest-mock-extended';
9
+ import merge from 'lodash.merge';
10
+
11
+ import { CommitmentsDB, MessageLoadOracleInputs, PublicContractsDB, PublicStateDB } from '../../index.js';
12
+ import { AvmContext } from '../avm_context.js';
13
+ import { AvmExecutionEnvironment } from '../avm_execution_environment.js';
14
+ import { AvmMachineState } from '../avm_machine_state.js';
15
+ import { HostStorage } from '../journal/host_storage.js';
16
+ import { AvmPersistableStateManager } from '../journal/journal.js';
17
+
18
+ /**
19
+ * Create a new AVM context with default values.
20
+ */
21
+ export function initContext(overrides?: {
22
+ persistableState?: AvmPersistableStateManager;
23
+ env?: AvmExecutionEnvironment;
24
+ machineState?: AvmMachineState;
25
+ }): AvmContext {
26
+ return new AvmContext(
27
+ overrides?.persistableState || initMockPersistableStateManager(),
28
+ overrides?.env || initExecutionEnvironment(),
29
+ overrides?.machineState || initMachineState(),
30
+ );
31
+ }
32
+
33
+ /** Creates an empty host storage with mocked dbs. */
34
+ export function initHostStorage(overrides?: {
35
+ publicDb?: PublicStateDB;
36
+ contractsDb?: PublicContractsDB;
37
+ commitmentsDb?: CommitmentsDB;
38
+ }): HostStorage {
39
+ return new HostStorage(
40
+ overrides?.publicDb || mock<PublicStateDB>(),
41
+ overrides?.contractsDb || mock<PublicContractsDB>(),
42
+ overrides?.commitmentsDb || mock<CommitmentsDB>(),
43
+ );
44
+ }
45
+
46
+ /** Creates an empty state manager with mocked storage. */
47
+ export function initMockPersistableStateManager(): AvmPersistableStateManager {
48
+ return new AvmPersistableStateManager(initHostStorage());
49
+ }
50
+
51
+ /**
52
+ * Create an empty instance of the Execution Environment where all values are zero, unless overridden in the overrides object
53
+ */
54
+ export function initExecutionEnvironment(overrides?: Partial<AvmExecutionEnvironment>): AvmExecutionEnvironment {
55
+ return new AvmExecutionEnvironment(
56
+ overrides?.address ?? AztecAddress.zero(),
57
+ overrides?.storageAddress ?? AztecAddress.zero(),
58
+ overrides?.origin ?? AztecAddress.zero(),
59
+ overrides?.sender ?? AztecAddress.zero(),
60
+ overrides?.portal ?? EthAddress.ZERO,
61
+ overrides?.feePerL1Gas ?? Fr.zero(),
62
+ overrides?.feePerL2Gas ?? Fr.zero(),
63
+ overrides?.feePerDaGas ?? Fr.zero(),
64
+ overrides?.contractCallDepth ?? Fr.zero(),
65
+ overrides?.globals ?? GlobalVariables.empty(),
66
+ overrides?.isStaticCall ?? false,
67
+ overrides?.isDelegateCall ?? false,
68
+ overrides?.calldata ?? [],
69
+ overrides?.temporaryFunctionSelector ?? FunctionSelector.empty(),
70
+ );
71
+ }
72
+
73
+ /**
74
+ * Create an empty instance of the Execution Environment where all values are zero, unless overridden in the overrides object
75
+ */
76
+ export function initGlobalVariables(overrides?: Partial<GlobalVariables>): GlobalVariables {
77
+ return new GlobalVariables(
78
+ overrides?.chainId ?? Fr.zero(),
79
+ overrides?.version ?? Fr.zero(),
80
+ overrides?.blockNumber ?? Fr.zero(),
81
+ overrides?.timestamp ?? Fr.zero(),
82
+ overrides?.coinbase ?? EthAddress.ZERO,
83
+ overrides?.feeRecipient ?? AztecAddress.zero(),
84
+ );
85
+ }
86
+
87
+ /**
88
+ * Create an empty instance of the Machine State where all values are zero, unless overridden in the overrides object
89
+ */
90
+ export function initMachineState(overrides?: Partial<AvmMachineState>): AvmMachineState {
91
+ return AvmMachineState.fromState({
92
+ l1GasLeft: overrides?.l1GasLeft ?? 0,
93
+ l2GasLeft: overrides?.l2GasLeft ?? 0,
94
+ daGasLeft: overrides?.daGasLeft ?? 0,
95
+ });
96
+ }
97
+
98
+ /**
99
+ * Create a new object with all the same properties as the original, except for the ones in the overrides object.
100
+ */
101
+ export function allSameExcept(original: any, overrides: any): any {
102
+ return merge({}, original, overrides);
103
+ }
104
+
105
+ /**
106
+ * Create an empty L1ToL2Message oracle input
107
+ */
108
+ export function initL1ToL2MessageOracleInput(
109
+ leafIndex?: bigint,
110
+ ): MessageLoadOracleInputs<typeof L1_TO_L2_MSG_TREE_HEIGHT> {
111
+ return new MessageLoadOracleInputs<typeof L1_TO_L2_MSG_TREE_HEIGHT>(
112
+ leafIndex ?? 0n,
113
+ new SiblingPath(L1_TO_L2_MSG_TREE_HEIGHT, Array(L1_TO_L2_MSG_TREE_HEIGHT)),
114
+ );
115
+ }
@@ -0,0 +1,14 @@
1
+ import { CommitmentsDB, PublicContractsDB, PublicStateDB } from '../../public/db.js';
2
+
3
+ /**
4
+ * Host storage
5
+ *
6
+ * A wrapper around the node dbs
7
+ */
8
+ export class HostStorage {
9
+ constructor(
10
+ public readonly publicStateDb: PublicStateDB,
11
+ public readonly contractsDb: PublicContractsDB,
12
+ public readonly commitmentsDb: CommitmentsDB,
13
+ ) {}
14
+ }
@@ -0,0 +1,2 @@
1
+ export * from './host_storage.js';
2
+ export * from './journal.js';