@aztec/simulator 0.55.1 → 0.56.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/dest/avm/avm_execution_environment.d.ts +0 -8
  2. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  3. package/dest/avm/avm_execution_environment.js +1 -19
  4. package/dest/avm/avm_gas.d.ts +2 -2
  5. package/dest/avm/avm_gas.d.ts.map +1 -1
  6. package/dest/avm/avm_gas.js +31 -115
  7. package/dest/avm/avm_machine_state.d.ts.map +1 -1
  8. package/dest/avm/avm_machine_state.js +6 -6
  9. package/dest/avm/avm_memory_types.d.ts +35 -7
  10. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  11. package/dest/avm/avm_memory_types.js +25 -11
  12. package/dest/avm/avm_simulator.d.ts.map +1 -1
  13. package/dest/avm/avm_simulator.js +5 -6
  14. package/dest/avm/bytecode_utils.d.ts +1 -2
  15. package/dest/avm/bytecode_utils.d.ts.map +1 -1
  16. package/dest/avm/bytecode_utils.js +3 -17
  17. package/dest/avm/fixtures/index.d.ts +4 -16
  18. package/dest/avm/fixtures/index.d.ts.map +1 -1
  19. package/dest/avm/fixtures/index.js +9 -25
  20. package/dest/avm/journal/index.d.ts +0 -1
  21. package/dest/avm/journal/index.d.ts.map +1 -1
  22. package/dest/avm/journal/index.js +1 -2
  23. package/dest/avm/journal/journal.d.ts +4 -4
  24. package/dest/avm/journal/journal.d.ts.map +1 -1
  25. package/dest/avm/journal/journal.js +14 -13
  26. package/dest/avm/opcodes/bitwise.d.ts +10 -4
  27. package/dest/avm/opcodes/bitwise.d.ts.map +1 -1
  28. package/dest/avm/opcodes/bitwise.js +17 -11
  29. package/dest/avm/opcodes/commitment.js +2 -2
  30. package/dest/avm/opcodes/comparators.js +2 -2
  31. package/dest/avm/opcodes/conversion.d.ts +3 -2
  32. package/dest/avm/opcodes/conversion.d.ts.map +1 -1
  33. package/dest/avm/opcodes/conversion.js +18 -11
  34. package/dest/avm/opcodes/environment_getters.d.ts +27 -63
  35. package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
  36. package/dest/avm/opcodes/environment_getters.js +75 -80
  37. package/dest/avm/opcodes/hashing.d.ts +7 -5
  38. package/dest/avm/opcodes/hashing.d.ts.map +1 -1
  39. package/dest/avm/opcodes/hashing.js +40 -24
  40. package/dest/avm/opcodes/index.d.ts +3 -0
  41. package/dest/avm/opcodes/index.d.ts.map +1 -1
  42. package/dest/avm/opcodes/index.js +4 -1
  43. package/dest/avm/opcodes/instruction_impl.d.ts +0 -26
  44. package/dest/avm/opcodes/instruction_impl.d.ts.map +1 -1
  45. package/dest/avm/opcodes/instruction_impl.js +1 -51
  46. package/dest/avm/opcodes/memory.d.ts +7 -2
  47. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  48. package/dest/avm/opcodes/memory.js +23 -6
  49. package/dest/avm/opcodes/multi_scalar_mul.d.ts.map +1 -1
  50. package/dest/avm/opcodes/multi_scalar_mul.js +7 -6
  51. package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  52. package/dest/avm/serialization/bytecode_serialization.js +5 -24
  53. package/dest/avm/serialization/instruction_serialization.d.ts +48 -60
  54. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  55. package/dest/avm/serialization/instruction_serialization.js +50 -63
  56. package/dest/avm/test_utils.d.ts +8 -8
  57. package/dest/avm/test_utils.d.ts.map +1 -1
  58. package/dest/avm/test_utils.js +15 -15
  59. package/dest/client/simulator.d.ts +1 -1
  60. package/dest/client/test_utils.d.ts.map +1 -1
  61. package/dest/client/test_utils.js +12 -6
  62. package/dest/client/unconstrained_execution.d.ts +2 -2
  63. package/dest/client/unconstrained_execution.d.ts.map +1 -1
  64. package/dest/client/unconstrained_execution.js +3 -3
  65. package/dest/public/enqueued_call_simulator.d.ts +43 -0
  66. package/dest/public/enqueued_call_simulator.d.ts.map +1 -0
  67. package/dest/public/enqueued_call_simulator.js +155 -0
  68. package/dest/public/enqueued_calls_processor.d.ts +44 -0
  69. package/dest/public/enqueued_calls_processor.d.ts.map +1 -0
  70. package/dest/public/enqueued_calls_processor.js +218 -0
  71. package/dest/public/execution.d.ts +4 -9
  72. package/dest/public/execution.d.ts.map +1 -1
  73. package/dest/public/execution.js +5 -10
  74. package/dest/public/executor.d.ts +3 -5
  75. package/dest/public/executor.d.ts.map +1 -1
  76. package/dest/public/executor.js +5 -9
  77. package/dest/public/hints_builder.d.ts +4 -3
  78. package/dest/public/hints_builder.d.ts.map +1 -1
  79. package/dest/public/hints_builder.js +4 -4
  80. package/dest/public/index.d.ts +3 -2
  81. package/dest/public/index.d.ts.map +1 -1
  82. package/dest/public/index.js +3 -3
  83. package/dest/public/public_db_sources.d.ts +17 -24
  84. package/dest/public/public_db_sources.d.ts.map +1 -1
  85. package/dest/public/public_db_sources.js +58 -65
  86. package/dest/public/public_kernel.d.ts +3 -9
  87. package/dest/public/public_kernel.d.ts.map +1 -1
  88. package/dest/public/public_kernel.js +13 -40
  89. package/dest/public/public_kernel_circuit_simulator.d.ts +5 -11
  90. package/dest/public/public_kernel_circuit_simulator.d.ts.map +1 -1
  91. package/dest/public/public_kernel_tail_simulator.d.ts +19 -0
  92. package/dest/public/public_kernel_tail_simulator.d.ts.map +1 -0
  93. package/dest/public/public_kernel_tail_simulator.js +45 -0
  94. package/dest/public/public_processor.d.ts +8 -5
  95. package/dest/public/public_processor.d.ts.map +1 -1
  96. package/dest/public/public_processor.js +31 -48
  97. package/dest/public/public_processor_metrics.d.ts +3 -3
  98. package/dest/public/public_processor_metrics.d.ts.map +1 -1
  99. package/dest/public/public_processor_metrics.js +1 -1
  100. package/dest/public/side_effect_trace.d.ts.map +1 -1
  101. package/dest/public/side_effect_trace.js +4 -8
  102. package/package.json +9 -9
  103. package/src/avm/avm_execution_environment.ts +1 -21
  104. package/src/avm/avm_gas.ts +30 -114
  105. package/src/avm/avm_machine_state.ts +5 -5
  106. package/src/avm/avm_memory_types.ts +31 -9
  107. package/src/avm/avm_simulator.ts +4 -5
  108. package/src/avm/bytecode_utils.ts +2 -17
  109. package/src/avm/fixtures/index.ts +12 -44
  110. package/src/avm/journal/index.ts +0 -1
  111. package/src/avm/journal/journal.ts +13 -18
  112. package/src/avm/opcodes/bitwise.ts +14 -10
  113. package/src/avm/opcodes/commitment.ts +1 -1
  114. package/src/avm/opcodes/comparators.ts +2 -2
  115. package/src/avm/opcodes/conversion.ts +19 -10
  116. package/src/avm/opcodes/environment_getters.ts +72 -99
  117. package/src/avm/opcodes/hashing.ts +47 -22
  118. package/src/avm/opcodes/index.ts +3 -0
  119. package/src/avm/opcodes/instruction_impl.ts +0 -62
  120. package/src/avm/opcodes/memory.ts +20 -6
  121. package/src/avm/opcodes/multi_scalar_mul.ts +6 -6
  122. package/src/avm/serialization/bytecode_serialization.ts +11 -33
  123. package/src/avm/serialization/instruction_serialization.ts +5 -18
  124. package/src/avm/test_utils.ts +20 -16
  125. package/src/client/test_utils.ts +36 -9
  126. package/src/client/unconstrained_execution.ts +3 -8
  127. package/src/public/enqueued_call_simulator.ts +336 -0
  128. package/src/public/enqueued_calls_processor.ts +388 -0
  129. package/src/public/execution.ts +5 -12
  130. package/src/public/executor.ts +4 -12
  131. package/src/public/hints_builder.ts +14 -12
  132. package/src/public/index.ts +3 -2
  133. package/src/public/public_db_sources.ts +62 -67
  134. package/src/public/public_kernel.ts +15 -49
  135. package/src/public/public_kernel_circuit_simulator.ts +6 -10
  136. package/src/public/public_kernel_tail_simulator.ts +106 -0
  137. package/src/public/public_processor.ts +61 -86
  138. package/src/public/public_processor_metrics.ts +3 -3
  139. package/src/public/side_effect_trace.ts +5 -12
  140. package/dest/avm/journal/host_storage.d.ts +0 -13
  141. package/dest/avm/journal/host_storage.d.ts.map +0 -1
  142. package/dest/avm/journal/host_storage.js +0 -13
  143. package/dest/avm/opcodes/context_getters.d.ts +0 -15
  144. package/dest/avm/opcodes/context_getters.d.ts.map +0 -1
  145. package/dest/avm/opcodes/context_getters.js +0 -19
  146. package/dest/public/abstract_phase_manager.d.ts +0 -93
  147. package/dest/public/abstract_phase_manager.d.ts.map +0 -1
  148. package/dest/public/abstract_phase_manager.js +0 -285
  149. package/dest/public/app_logic_phase_manager.d.ts +0 -25
  150. package/dest/public/app_logic_phase_manager.d.ts.map +0 -1
  151. package/dest/public/app_logic_phase_manager.js +0 -52
  152. package/dest/public/phase_manager_factory.d.ts +0 -18
  153. package/dest/public/phase_manager_factory.d.ts.map +0 -1
  154. package/dest/public/phase_manager_factory.js +0 -56
  155. package/dest/public/setup_phase_manager.d.ts +0 -25
  156. package/dest/public/setup_phase_manager.d.ts.map +0 -1
  157. package/dest/public/setup_phase_manager.js +0 -39
  158. package/dest/public/tail_phase_manager.d.ts +0 -23
  159. package/dest/public/tail_phase_manager.d.ts.map +0 -1
  160. package/dest/public/tail_phase_manager.js +0 -50
  161. package/dest/public/teardown_phase_manager.d.ts +0 -27
  162. package/dest/public/teardown_phase_manager.d.ts.map +0 -1
  163. package/dest/public/teardown_phase_manager.js +0 -56
  164. package/src/avm/journal/host_storage.ts +0 -14
  165. package/src/avm/opcodes/context_getters.ts +0 -23
  166. package/src/public/abstract_phase_manager.ts +0 -522
  167. package/src/public/app_logic_phase_manager.ts +0 -81
  168. package/src/public/phase_manager_factory.ts +0 -126
  169. package/src/public/setup_phase_manager.ts +0 -61
  170. package/src/public/tail_phase_manager.ts +0 -127
  171. package/src/public/teardown_phase_manager.ts +0 -86
@@ -1,6 +1,6 @@
1
1
  import { type Fr } from '@aztec/circuits.js';
2
2
 
3
- import { type Gas, GasDimensions } from './avm_gas.js';
3
+ import { GAS_DIMENSIONS, type Gas } from './avm_gas.js';
4
4
  import { TaggedMemory } from './avm_memory_types.js';
5
5
  import { OutOfGasError } from './errors.js';
6
6
 
@@ -67,7 +67,7 @@ export class AvmMachineState {
67
67
  */
68
68
  public consumeGas(gasCost: Partial<Gas>) {
69
69
  // Assert there is enough gas on every dimension.
70
- const outOfGasDimensions = GasDimensions.filter(
70
+ const outOfGasDimensions = GAS_DIMENSIONS.filter(
71
71
  dimension => this[`${dimension}Left`] - (gasCost[dimension] ?? 0) < 0,
72
72
  );
73
73
  // If not, trigger an exceptional halt.
@@ -77,14 +77,14 @@ export class AvmMachineState {
77
77
  throw new OutOfGasError(outOfGasDimensions);
78
78
  }
79
79
  // Otherwise, charge the corresponding gas
80
- for (const dimension of GasDimensions) {
80
+ for (const dimension of GAS_DIMENSIONS) {
81
81
  this[`${dimension}Left`] -= gasCost[dimension] ?? 0;
82
82
  }
83
83
  }
84
84
 
85
85
  /** Increases the gas left by the amounts specified. */
86
86
  public refundGas(gasRefund: Partial<Gas>) {
87
- for (const dimension of GasDimensions) {
87
+ for (const dimension of GAS_DIMENSIONS) {
88
88
  this[`${dimension}Left`] += gasRefund[dimension] ?? 0;
89
89
  }
90
90
  }
@@ -133,7 +133,7 @@ export class AvmMachineState {
133
133
  * Flag an exceptional halt. Clears gas left and sets the reverted flag. No output data.
134
134
  */
135
135
  private exceptionalHalt() {
136
- GasDimensions.forEach(dimension => (this[`${dimension}Left`] = 0));
136
+ GAS_DIMENSIONS.forEach(dimension => (this[`${dimension}Left`] = 0));
137
137
  this.reverted = true;
138
138
  this.halted = true;
139
139
  }
@@ -1,3 +1,12 @@
1
+ import {
2
+ MEM_TAG_FF,
3
+ MEM_TAG_U1,
4
+ MEM_TAG_U8,
5
+ MEM_TAG_U16,
6
+ MEM_TAG_U32,
7
+ MEM_TAG_U64,
8
+ MEM_TAG_U128,
9
+ } from '@aztec/circuits.js';
1
10
  import { toBufferBE } from '@aztec/foundation/bigint-buffer';
2
11
  import { Fr } from '@aztec/foundation/fields';
3
12
  import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';
@@ -128,6 +137,9 @@ function UnsignedIntegerClassFactory(bits: number) {
128
137
  }
129
138
 
130
139
  public toBuffer(): Buffer {
140
+ if (bits < 8) {
141
+ return toBufferBE(this.n, 1);
142
+ }
131
143
  return toBufferBE(this.n, bits / 8);
132
144
  }
133
145
  };
@@ -136,6 +148,7 @@ function UnsignedIntegerClassFactory(bits: number) {
136
148
  // Now we can create the classes for each unsigned integer type.
137
149
  // We extend instead of just assigning so that the class has the right name.
138
150
  // Otherwise they are all called "NewUintClass".
151
+ export class Uint1 extends UnsignedIntegerClassFactory(1) {}
139
152
  export class Uint8 extends UnsignedIntegerClassFactory(8) {}
140
153
  export class Uint16 extends UnsignedIntegerClassFactory(16) {}
141
154
  export class Uint32 extends UnsignedIntegerClassFactory(32) {}
@@ -196,12 +209,13 @@ export class Field extends MemoryValue {
196
209
 
197
210
  export enum TypeTag {
198
211
  UNINITIALIZED,
199
- UINT8,
200
- UINT16,
201
- UINT32,
202
- UINT64,
203
- UINT128,
204
- FIELD,
212
+ UINT1 = MEM_TAG_U1,
213
+ UINT8 = MEM_TAG_U8,
214
+ UINT16 = MEM_TAG_U16,
215
+ UINT32 = MEM_TAG_U32,
216
+ UINT64 = MEM_TAG_U64,
217
+ UINT128 = MEM_TAG_U128,
218
+ FIELD = MEM_TAG_FF,
205
219
  INVALID,
206
220
  }
207
221
 
@@ -302,7 +316,9 @@ export class TaggedMemory implements TaggedMemoryInterface {
302
316
  }
303
317
 
304
318
  public static checkIsIntegralTag(tag: TypeTag) {
305
- if (![TypeTag.UINT8, TypeTag.UINT16, TypeTag.UINT32, TypeTag.UINT64, TypeTag.UINT128].includes(tag)) {
319
+ if (
320
+ ![TypeTag.UINT1, TypeTag.UINT8, TypeTag.UINT16, TypeTag.UINT32, TypeTag.UINT64, TypeTag.UINT128].includes(tag)
321
+ ) {
306
322
  throw TagCheckError.forTag(TypeTag[tag], 'integral');
307
323
  }
308
324
  }
@@ -332,8 +348,8 @@ export class TaggedMemory implements TaggedMemoryInterface {
332
348
 
333
349
  if (v === undefined) {
334
350
  tag = TypeTag.UNINITIALIZED;
335
- } else if (v instanceof Field) {
336
- tag = TypeTag.FIELD;
351
+ } else if (v instanceof Uint1) {
352
+ tag = TypeTag.UINT1;
337
353
  } else if (v instanceof Uint8) {
338
354
  tag = TypeTag.UINT8;
339
355
  } else if (v instanceof Uint16) {
@@ -344,6 +360,8 @@ export class TaggedMemory implements TaggedMemoryInterface {
344
360
  tag = TypeTag.UINT64;
345
361
  } else if (v instanceof Uint128) {
346
362
  tag = TypeTag.UINT128;
363
+ } else if (v instanceof Field) {
364
+ tag = TypeTag.FIELD;
347
365
  }
348
366
 
349
367
  return tag;
@@ -353,6 +371,8 @@ export class TaggedMemory implements TaggedMemoryInterface {
353
371
  public static buildFromTagTruncating(v: bigint | number, tag: TypeTag): MemoryValue {
354
372
  v = BigInt(v);
355
373
  switch (tag) {
374
+ case TypeTag.UINT1:
375
+ return new Uint1(v & 1n);
356
376
  case TypeTag.UINT8:
357
377
  return new Uint8(v & ((1n << 8n) - 1n));
358
378
  case TypeTag.UINT16:
@@ -373,6 +393,8 @@ export class TaggedMemory implements TaggedMemoryInterface {
373
393
  // Does not truncate. Type constructor will check that it fits.
374
394
  public static buildFromTagOrDie(v: bigint | number, tag: TypeTag): MemoryValue {
375
395
  switch (tag) {
396
+ case TypeTag.UINT1:
397
+ return new Uint1(v);
376
398
  case TypeTag.UINT8:
377
399
  return new Uint8(v);
378
400
  case TypeTag.UINT16:
@@ -4,7 +4,7 @@ import { strict as assert } from 'assert';
4
4
 
5
5
  import type { AvmContext } from './avm_context.js';
6
6
  import { AvmContractCallResult } from './avm_contract_call_result.js';
7
- import { decompressBytecodeIfCompressed, isAvmBytecode } from './bytecode_utils.js';
7
+ import { isAvmBytecode } from './bytecode_utils.js';
8
8
  import {
9
9
  AvmExecutionError,
10
10
  InvalidProgramCounterError,
@@ -53,11 +53,10 @@ export class AvmSimulator {
53
53
  * This method is useful for testing and debugging.
54
54
  */
55
55
  public async executeBytecode(bytecode: Buffer): Promise<AvmContractCallResult> {
56
- const decompressedBytecode = await decompressBytecodeIfCompressed(bytecode);
57
- assert(await isAvmBytecode(decompressedBytecode), "AVM simulator can't execute non-AVM bytecode");
56
+ assert(isAvmBytecode(bytecode), "AVM simulator can't execute non-AVM bytecode");
58
57
 
59
- this.bytecode = decompressedBytecode;
60
- return await this.executeInstructions(decodeFromBytecode(decompressedBytecode));
58
+ this.bytecode = bytecode;
59
+ return await this.executeInstructions(decodeFromBytecode(bytecode));
61
60
  }
62
61
 
63
62
  /**
@@ -1,6 +1,3 @@
1
- import { promisify } from 'util';
2
- import { gunzip } from 'zlib';
3
-
4
1
  import { Opcode } from './serialization/instruction_serialization.js';
5
2
 
6
3
  const AVM_MAGIC_SUFFIX = Buffer.from([
@@ -14,19 +11,7 @@ export function markBytecodeAsAvm(bytecode: Buffer): Buffer {
14
11
  return Buffer.concat([bytecode, AVM_MAGIC_SUFFIX]);
15
12
  }
16
13
 
17
- // This is just a helper function for the AVM simulator
18
- export async function decompressBytecodeIfCompressed(bytecode: Buffer): Promise<Buffer> {
19
- try {
20
- return await promisify(gunzip)(bytecode);
21
- } catch {
22
- // If the bytecode is not compressed, the gunzip call will throw an error
23
- // In this case, we assume the bytecode is not compressed and continue.
24
- return Promise.resolve(bytecode);
25
- }
26
- }
27
-
28
- export async function isAvmBytecode(bytecode: Buffer): Promise<boolean> {
29
- const decompressedBytecode = await decompressBytecodeIfCompressed(bytecode);
14
+ export function isAvmBytecode(bytecode: Buffer): boolean {
30
15
  const magicSize = AVM_MAGIC_SUFFIX.length;
31
- return decompressedBytecode.subarray(-magicSize).equals(AVM_MAGIC_SUFFIX);
16
+ return bytecode.subarray(-magicSize).equals(AVM_MAGIC_SUFFIX);
32
17
  }
@@ -10,20 +10,13 @@ import { strict as assert } from 'assert';
10
10
  import { mock } from 'jest-mock-extended';
11
11
  import merge from 'lodash.merge';
12
12
 
13
- import {
14
- type CommitmentsDB,
15
- type PublicContractsDB,
16
- type PublicStateDB,
17
- resolveAssertionMessage,
18
- traverseCauseChain,
19
- } from '../../index.js';
13
+ import { type WorldStateDB, resolveAssertionMessage, traverseCauseChain } from '../../index.js';
20
14
  import { type PublicSideEffectTraceInterface } from '../../public/side_effect_trace_interface.js';
21
15
  import { AvmContext } from '../avm_context.js';
22
- import { AvmContextInputs, AvmExecutionEnvironment } from '../avm_execution_environment.js';
16
+ import { AvmExecutionEnvironment } from '../avm_execution_environment.js';
23
17
  import { AvmMachineState } from '../avm_machine_state.js';
24
- import { Field, Uint8, Uint64 } from '../avm_memory_types.js';
18
+ import { Field, Uint8, Uint32, Uint64 } from '../avm_memory_types.js';
25
19
  import { type AvmRevertReason } from '../errors.js';
26
- import { HostStorage } from '../journal/host_storage.js';
27
20
  import { AvmPersistableStateManager } from '../journal/journal.js';
28
21
  import { NullifierManager } from '../journal/nullifiers.js';
29
22
  import { PublicStorage } from '../journal/public_storage.js';
@@ -43,32 +36,19 @@ export function initContext(overrides?: {
43
36
  );
44
37
  }
45
38
 
46
- /** Creates an empty host storage with mocked dbs. */
47
- export function initHostStorage(overrides?: {
48
- publicDb?: PublicStateDB;
49
- contractsDb?: PublicContractsDB;
50
- commitmentsDb?: CommitmentsDB;
51
- }): HostStorage {
52
- return new HostStorage(
53
- overrides?.publicDb || mock<PublicStateDB>(),
54
- overrides?.contractsDb || mock<PublicContractsDB>(),
55
- overrides?.commitmentsDb || mock<CommitmentsDB>(),
56
- );
57
- }
58
-
59
39
  /** Creates an empty state manager with mocked host storage. */
60
40
  export function initPersistableStateManager(overrides?: {
61
- hostStorage?: HostStorage;
41
+ worldStateDB?: WorldStateDB;
62
42
  trace?: PublicSideEffectTraceInterface;
63
43
  publicStorage?: PublicStorage;
64
44
  nullifiers?: NullifierManager;
65
45
  }): AvmPersistableStateManager {
66
- const hostStorage = overrides?.hostStorage || initHostStorage();
46
+ const worldStateDB = overrides?.worldStateDB || mock<WorldStateDB>();
67
47
  return new AvmPersistableStateManager(
68
- hostStorage,
48
+ worldStateDB,
69
49
  overrides?.trace || mock<PublicSideEffectTraceInterface>(),
70
- overrides?.publicStorage || new PublicStorage(hostStorage.publicStateDb),
71
- overrides?.nullifiers || new NullifierManager(hostStorage.commitmentsDb),
50
+ overrides?.publicStorage || new PublicStorage(worldStateDB),
51
+ overrides?.nullifiers || new NullifierManager(worldStateDB),
72
52
  );
73
53
  }
74
54
 
@@ -124,26 +104,14 @@ export function allSameExcept(original: any, overrides: any): any {
124
104
  return merge({}, original, overrides);
125
105
  }
126
106
 
127
- /**
128
- * Adjust the user index to account for the AvmContextInputs size.
129
- * This is a hack for testing, and should go away once AvmContextInputs themselves go away.
130
- */
131
- export function adjustCalldataIndex(userIndex: number): number {
132
- return userIndex + AvmContextInputs.SIZE;
133
- }
134
-
135
- export function anyAvmContextInputs() {
136
- const tv = [];
137
- for (let i = 0; i < AvmContextInputs.SIZE; i++) {
138
- tv.push(expect.any(Fr));
139
- }
140
- return tv;
141
- }
142
-
143
107
  export function randomMemoryBytes(length: number): Uint8[] {
144
108
  return [...Array(length)].map(_ => new Uint8(Math.floor(Math.random() * 255)));
145
109
  }
146
110
 
111
+ export function randomMemoryUint32s(length: number): Uint32[] {
112
+ return [...Array(length)].map(_ => new Uint32(Math.floor(Math.random() * 255)));
113
+ }
114
+
147
115
  export function randomMemoryUint64s(length: number): Uint64[] {
148
116
  return [...Array(length)].map(_ => new Uint64(Math.floor(Math.random() * 255)));
149
117
  }
@@ -1,2 +1 @@
1
- export * from './host_storage.js';
2
1
  export * from './journal.js';
@@ -3,11 +3,11 @@ import { Fr } from '@aztec/foundation/fields';
3
3
  import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';
4
4
  import { SerializableContractInstance } from '@aztec/types/contracts';
5
5
 
6
+ import { type WorldStateDB } from '../../public/public_db_sources.js';
6
7
  import { type TracedContractInstance } from '../../public/side_effect_trace.js';
7
8
  import { type PublicSideEffectTraceInterface } from '../../public/side_effect_trace_interface.js';
8
9
  import { type AvmContractCallResult } from '../avm_contract_call_result.js';
9
10
  import { type AvmExecutionEnvironment } from '../avm_execution_environment.js';
10
- import { type HostStorage } from './host_storage.js';
11
11
  import { NullifierManager } from './nullifiers.js';
12
12
  import { PublicStorage } from './public_storage.js';
13
13
 
@@ -25,7 +25,7 @@ export class AvmPersistableStateManager {
25
25
 
26
26
  constructor(
27
27
  /** Reference to node storage */
28
- private readonly hostStorage: HostStorage,
28
+ private readonly worldStateDB: WorldStateDB,
29
29
  /** Side effect trace */
30
30
  private readonly trace: PublicSideEffectTraceInterface,
31
31
  /** Public storage, including cached writes */
@@ -39,18 +39,15 @@ export class AvmPersistableStateManager {
39
39
  * Create a new state manager with some preloaded pending siloed nullifiers
40
40
  */
41
41
  public static newWithPendingSiloedNullifiers(
42
- hostStorage: HostStorage,
42
+ worldStateDB: WorldStateDB,
43
43
  trace: PublicSideEffectTraceInterface,
44
44
  pendingSiloedNullifiers: Fr[],
45
45
  ) {
46
- const parentNullifiers = NullifierManager.newWithPendingSiloedNullifiers(
47
- hostStorage.commitmentsDb,
48
- pendingSiloedNullifiers,
49
- );
46
+ const parentNullifiers = NullifierManager.newWithPendingSiloedNullifiers(worldStateDB, pendingSiloedNullifiers);
50
47
  return new AvmPersistableStateManager(
51
- hostStorage,
48
+ worldStateDB,
52
49
  trace,
53
- /*publicStorage=*/ new PublicStorage(hostStorage.publicStateDb),
50
+ /*publicStorage=*/ new PublicStorage(worldStateDB),
54
51
  /*nullifiers=*/ parentNullifiers.fork(),
55
52
  );
56
53
  }
@@ -60,7 +57,7 @@ export class AvmPersistableStateManager {
60
57
  */
61
58
  public fork() {
62
59
  return new AvmPersistableStateManager(
63
- this.hostStorage,
60
+ this.worldStateDB,
64
61
  this.trace.fork(),
65
62
  this.publicStorage.fork(),
66
63
  this.nullifiers.fork(),
@@ -122,7 +119,7 @@ export class AvmPersistableStateManager {
122
119
  * @returns true if the note hash exists at the given leaf index, false otherwise
123
120
  */
124
121
  public async checkNoteHashExists(storageAddress: Fr, noteHash: Fr, leafIndex: Fr): Promise<boolean> {
125
- const gotLeafValue = (await this.hostStorage.commitmentsDb.getCommitmentValue(leafIndex.toBigInt())) ?? Fr.ZERO;
122
+ const gotLeafValue = (await this.worldStateDB.getCommitmentValue(leafIndex.toBigInt())) ?? Fr.ZERO;
126
123
  const exists = gotLeafValue.equals(noteHash);
127
124
  this.log.debug(
128
125
  `noteHashes(${storageAddress})@${noteHash} ?? leafIndex: ${leafIndex} | gotLeafValue: ${gotLeafValue}, exists: ${exists}.`,
@@ -177,7 +174,7 @@ export class AvmPersistableStateManager {
177
174
  * @returns exists - whether the message exists in the L1 to L2 Messages tree
178
175
  */
179
176
  public async checkL1ToL2MessageExists(contractAddress: Fr, msgHash: Fr, msgLeafIndex: Fr): Promise<boolean> {
180
- const valueAtIndex = (await this.hostStorage.commitmentsDb.getL1ToL2LeafValue(msgLeafIndex.toBigInt())) ?? Fr.ZERO;
177
+ const valueAtIndex = (await this.worldStateDB.getL1ToL2LeafValue(msgLeafIndex.toBigInt())) ?? Fr.ZERO;
181
178
  const exists = valueAtIndex.equals(msgHash);
182
179
  this.log.debug(
183
180
  `l1ToL2Messages(@${msgLeafIndex}) ?? exists: ${exists}, expected: ${msgHash}, found: ${valueAtIndex}.`,
@@ -217,7 +214,7 @@ export class AvmPersistableStateManager {
217
214
  public async getContractInstance(contractAddress: Fr): Promise<TracedContractInstance> {
218
215
  let exists = true;
219
216
  const aztecAddress = AztecAddress.fromField(contractAddress);
220
- let instance = await this.hostStorage.contractsDb.getContractInstance(aztecAddress);
217
+ let instance = await this.worldStateDB.getContractInstance(aztecAddress);
221
218
  if (instance === undefined) {
222
219
  instance = SerializableContractInstance.empty().withAddress(aztecAddress);
223
220
  exists = false;
@@ -242,7 +239,7 @@ export class AvmPersistableStateManager {
242
239
  * Get a contract's bytecode from the contracts DB
243
240
  */
244
241
  public async getBytecode(contractAddress: AztecAddress, selector: FunctionSelector): Promise<Buffer | undefined> {
245
- return await this.hostStorage.contractsDb.getBytecode(contractAddress, selector);
242
+ return await this.worldStateDB.getBytecode(contractAddress, selector);
246
243
  }
247
244
 
248
245
  /**
@@ -260,10 +257,8 @@ export class AvmPersistableStateManager {
260
257
  this.acceptNestedCallState(nestedState);
261
258
  }
262
259
  const functionName =
263
- (await nestedState.hostStorage.contractsDb.getDebugFunctionName(
264
- nestedEnvironment.address,
265
- nestedEnvironment.functionSelector,
266
- )) ?? `${nestedEnvironment.address}:${nestedEnvironment.functionSelector}`;
260
+ (await this.worldStateDB.getDebugFunctionName(nestedEnvironment.address, nestedEnvironment.functionSelector)) ??
261
+ `${nestedEnvironment.address}:${nestedEnvironment.functionSelector}`;
267
262
 
268
263
  this.log.verbose(`[AVM] Calling nested function ${functionName}`);
269
264
 
@@ -1,8 +1,9 @@
1
1
  import type { AvmContext } from '../avm_context.js';
2
- import { type IntegralValue, type TaggedMemoryInterface, TypeTag } from '../avm_memory_types.js';
3
- import { Opcode } from '../serialization/instruction_serialization.js';
2
+ import { type IntegralValue, TaggedMemory, type TaggedMemoryInterface, TypeTag } from '../avm_memory_types.js';
3
+ import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
4
4
  import { Addressing } from './addressing_mode.js';
5
- import { ThreeOperandInstruction, TwoOperandInstruction } from './instruction_impl.js';
5
+ import { Instruction } from './instruction.js';
6
+ import { ThreeOperandInstruction } from './instruction_impl.js';
6
7
 
7
8
  abstract class ThreeOperandBitwiseInstruction extends ThreeOperandInstruction {
8
9
  public async execute(context: AvmContext): Promise<void> {
@@ -85,12 +86,15 @@ export class Shr extends ThreeOperandBitwiseInstruction {
85
86
  }
86
87
  }
87
88
 
88
- export class Not extends TwoOperandInstruction {
89
+ export class Not extends Instruction {
89
90
  static readonly type: string = 'NOT';
90
91
  static readonly opcode = Opcode.NOT_8;
91
92
 
92
- constructor(indirect: number, inTag: number, aOffset: number, dstOffset: number) {
93
- super(indirect, inTag, aOffset, dstOffset);
93
+ static readonly wireFormat8 = [OperandType.UINT8, OperandType.UINT8, OperandType.UINT8, OperandType.UINT8];
94
+ static readonly wireFormat16 = [OperandType.UINT8, OperandType.UINT8, OperandType.UINT16, OperandType.UINT16];
95
+
96
+ constructor(private indirect: number, private srcOffset: number, private dstOffset: number) {
97
+ super();
94
98
  }
95
99
 
96
100
  public async execute(context: AvmContext): Promise<void> {
@@ -98,11 +102,11 @@ export class Not extends TwoOperandInstruction {
98
102
  const memory = context.machineState.memory.track(this.type);
99
103
  context.machineState.consumeGas(this.gasCost(memoryOperations));
100
104
 
101
- const [aOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve([this.aOffset, this.dstOffset], memory);
102
- memory.checkTags(this.inTag, aOffset);
103
- const a = memory.getAs<IntegralValue>(aOffset);
105
+ const [srcOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve([this.srcOffset, this.dstOffset], memory);
106
+ TaggedMemory.checkIsIntegralTag(memory.getTag(srcOffset));
107
+ const value = memory.getAs<IntegralValue>(srcOffset);
104
108
 
105
- const res = a.not();
109
+ const res = value.not();
106
110
  memory.set(dstOffset, res);
107
111
 
108
112
  memory.assert(memoryOperations);
@@ -47,7 +47,7 @@ export class PedersenCommitment extends Instruction {
47
47
  memory.checkTag(TypeTag.UINT32, genIndexOffset);
48
48
 
49
49
  const memoryOperations = { reads: inputSize + 2, writes: 3, indirect: this.indirect };
50
- context.machineState.consumeGas(this.gasCost(memoryOperations));
50
+ context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: inputSize }));
51
51
 
52
52
  const inputBuffer: Buffer[] = inputs.map(input => input.toBuffer());
53
53
  // TODO: Add the generate index to the pedersenCommit function
@@ -1,5 +1,5 @@
1
1
  import type { AvmContext } from '../avm_context.js';
2
- import { type MemoryValue, Uint8 } from '../avm_memory_types.js';
2
+ import { type MemoryValue, Uint1 } from '../avm_memory_types.js';
3
3
  import { Opcode } from '../serialization/instruction_serialization.js';
4
4
  import { Addressing } from './addressing_mode.js';
5
5
  import { ThreeOperandInstruction } from './instruction_impl.js';
@@ -19,7 +19,7 @@ abstract class ComparatorInstruction extends ThreeOperandInstruction {
19
19
  const a = memory.get(aOffset);
20
20
  const b = memory.get(bOffset);
21
21
 
22
- const dest = new Uint8(this.compare(a, b) ? 1 : 0);
22
+ const dest = new Uint1(this.compare(a, b) ? 1 : 0);
23
23
  memory.set(dstOffset, dest);
24
24
 
25
25
  memory.assert(memoryOperations);
@@ -1,7 +1,6 @@
1
- import { strict as assert } from 'assert';
2
-
3
1
  import { type AvmContext } from '../avm_context.js';
4
- import { TypeTag, Uint8 } from '../avm_memory_types.js';
2
+ import { TypeTag, Uint1, Uint8 } from '../avm_memory_types.js';
3
+ import { InstructionExecutionError } from '../errors.js';
5
4
  import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
6
5
  import { Addressing } from './addressing_mode.js';
7
6
  import { Instruction } from './instruction.js';
@@ -16,32 +15,41 @@ export class ToRadixLE extends Instruction {
16
15
  OperandType.UINT8, // Indirect
17
16
  OperandType.UINT32, // src memory address
18
17
  OperandType.UINT32, // dst memory address
19
- OperandType.UINT32, // radix (immediate)
18
+ OperandType.UINT32, // radix memory address
20
19
  OperandType.UINT32, // number of limbs (Immediate)
20
+ OperandType.UINT1, // output is in "bits" mode (Immediate - Uint1 still takes up a whole byte)
21
21
  ];
22
22
 
23
23
  constructor(
24
24
  private indirect: number,
25
25
  private srcOffset: number,
26
26
  private dstOffset: number,
27
- private radix: number,
27
+ private radixOffset: number,
28
28
  private numLimbs: number,
29
+ private outputBits: number, // effectively a bool
29
30
  ) {
30
- assert(radix <= 256, 'Radix cannot be greater than 256');
31
31
  super();
32
32
  }
33
33
 
34
34
  public async execute(context: AvmContext): Promise<void> {
35
35
  const memory = context.machineState.memory.track(this.type);
36
- const [srcOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve([this.srcOffset, this.dstOffset], memory);
37
- const memoryOperations = { reads: 1, writes: this.numLimbs, indirect: this.indirect };
36
+ const [srcOffset, dstOffset, radixOffset] = Addressing.fromWire(this.indirect).resolve(
37
+ [this.srcOffset, this.dstOffset, this.radixOffset],
38
+ memory,
39
+ );
40
+ const memoryOperations = { reads: 2, writes: this.numLimbs, indirect: this.indirect };
38
41
  context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: this.numLimbs }));
39
42
 
40
43
  // The radix gadget only takes in a Field
41
44
  memory.checkTag(TypeTag.FIELD, srcOffset);
45
+ memory.checkTag(TypeTag.UINT32, radixOffset);
42
46
 
43
47
  let value: bigint = memory.get(srcOffset).toBigInt();
44
- const radixBN: bigint = BigInt(this.radix);
48
+ const radix: bigint = memory.get(radixOffset).toBigInt();
49
+ if (radix > 256) {
50
+ throw new InstructionExecutionError(`ToRadixLE instruction's radix should be <= 256 (was ${radix})`);
51
+ }
52
+ const radixBN: bigint = BigInt(radix);
45
53
  const limbArray = [];
46
54
 
47
55
  for (let i = 0; i < this.numLimbs; i++) {
@@ -50,7 +58,8 @@ export class ToRadixLE extends Instruction {
50
58
  value /= radixBN;
51
59
  }
52
60
 
53
- const res = limbArray.map(byte => new Uint8(byte));
61
+ const outputType = this.outputBits != 0 ? Uint1 : Uint8;
62
+ const res = limbArray.map(byte => new outputType(byte));
54
63
  memory.setSlice(dstOffset, res);
55
64
 
56
65
  memory.assert(memoryOperations);