@aztec/simulator 2.1.0-rc.9 → 3.0.0-devnet.2

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 (161) hide show
  1. package/dest/private/acvm_native.d.ts +1 -2
  2. package/dest/private/acvm_native.d.ts.map +1 -1
  3. package/dest/private/circuit_recording/simulator_recorder_wrapper.d.ts +1 -1
  4. package/dest/private/circuit_recording/simulator_recorder_wrapper.d.ts.map +1 -1
  5. package/dest/public/avm/avm_execution_environment.d.ts +2 -1
  6. package/dest/public/avm/avm_execution_environment.d.ts.map +1 -1
  7. package/dest/public/avm/avm_execution_environment.js +5 -2
  8. package/dest/public/avm/avm_gas.js +2 -2
  9. package/dest/public/avm/avm_simulator.d.ts +1 -1
  10. package/dest/public/avm/avm_simulator.d.ts.map +1 -1
  11. package/dest/public/avm/avm_simulator.js +14 -21
  12. package/dest/public/avm/errors.d.ts +5 -14
  13. package/dest/public/avm/errors.d.ts.map +1 -1
  14. package/dest/public/avm/errors.js +7 -20
  15. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts +2 -1
  16. package/dest/public/avm/fixtures/avm_simulation_tester.d.ts.map +1 -1
  17. package/dest/public/avm/fixtures/avm_simulation_tester.js +2 -3
  18. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts +2 -0
  19. package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts.map +1 -1
  20. package/dest/public/avm/fixtures/base_avm_simulation_tester.js +22 -0
  21. package/dest/public/avm/fixtures/initializers.d.ts.map +1 -1
  22. package/dest/public/avm/fixtures/initializers.js +2 -2
  23. package/dest/public/avm/opcodes/accrued_substate.d.ts +2 -2
  24. package/dest/public/avm/opcodes/accrued_substate.d.ts.map +1 -1
  25. package/dest/public/avm/opcodes/accrued_substate.js +7 -7
  26. package/dest/public/avm/opcodes/addressing_mode.d.ts.map +1 -1
  27. package/dest/public/avm/opcodes/addressing_mode.js +2 -3
  28. package/dest/public/avm/opcodes/conversion.d.ts.map +1 -1
  29. package/dest/public/avm/opcodes/conversion.js +3 -0
  30. package/dest/public/avm/opcodes/ec_add.d.ts.map +1 -1
  31. package/dest/public/avm/opcodes/ec_add.js +3 -2
  32. package/dest/public/avm/opcodes/environment_getters.d.ts.map +1 -1
  33. package/dest/public/avm/opcodes/environment_getters.js +5 -7
  34. package/dest/public/avm/opcodes/instruction.d.ts.map +1 -1
  35. package/dest/public/avm/opcodes/instruction.js +5 -6
  36. package/dest/public/avm/opcodes/misc.d.ts +2 -1
  37. package/dest/public/avm/opcodes/misc.d.ts.map +1 -1
  38. package/dest/public/avm/opcodes/misc.js +28 -8
  39. package/dest/public/avm/serialization/instruction_serialization.d.ts +8 -0
  40. package/dest/public/avm/serialization/instruction_serialization.d.ts.map +1 -1
  41. package/dest/public/avm/serialization/instruction_serialization.js +10 -0
  42. package/dest/public/avm/test_utils.d.ts +2 -2
  43. package/dest/public/avm/test_utils.d.ts.map +1 -1
  44. package/dest/public/avm/test_utils.js +6 -9
  45. package/dest/public/fixtures/bulk_test.d.ts.map +1 -1
  46. package/dest/public/fixtures/bulk_test.js +63 -130
  47. package/dest/public/fixtures/custom_bytecode_tester.d.ts +12 -0
  48. package/dest/public/fixtures/custom_bytecode_tester.d.ts.map +1 -0
  49. package/dest/public/fixtures/custom_bytecode_tester.js +29 -0
  50. package/dest/public/fixtures/custom_bytecode_tests.d.ts +9 -0
  51. package/dest/public/fixtures/custom_bytecode_tests.d.ts.map +1 -0
  52. package/dest/public/fixtures/custom_bytecode_tests.js +109 -0
  53. package/dest/public/fixtures/index.d.ts +2 -1
  54. package/dest/public/fixtures/index.d.ts.map +1 -1
  55. package/dest/public/fixtures/index.js +2 -1
  56. package/dest/public/fixtures/minimal_public_tx.d.ts +2 -1
  57. package/dest/public/fixtures/minimal_public_tx.d.ts.map +1 -1
  58. package/dest/public/fixtures/minimal_public_tx.js +8 -22
  59. package/dest/public/fixtures/public_tx_simulation_tester.d.ts +4 -2
  60. package/dest/public/fixtures/public_tx_simulation_tester.d.ts.map +1 -1
  61. package/dest/public/fixtures/public_tx_simulation_tester.js +11 -6
  62. package/dest/public/fixtures/utils.js +1 -1
  63. package/dest/public/hinting_db_sources.d.ts +2 -0
  64. package/dest/public/hinting_db_sources.d.ts.map +1 -1
  65. package/dest/public/hinting_db_sources.js +3 -0
  66. package/dest/public/index.d.ts +1 -1
  67. package/dest/public/index.d.ts.map +1 -1
  68. package/dest/public/public_db_sources.d.ts +2 -2
  69. package/dest/public/public_db_sources.d.ts.map +1 -1
  70. package/dest/public/public_db_sources.js +11 -1
  71. package/dest/public/public_errors.d.ts +12 -0
  72. package/dest/public/public_errors.d.ts.map +1 -0
  73. package/dest/public/public_errors.js +13 -0
  74. package/dest/public/public_processor/guarded_merkle_tree.d.ts +2 -0
  75. package/dest/public/public_processor/guarded_merkle_tree.d.ts.map +1 -1
  76. package/dest/public/public_processor/guarded_merkle_tree.js +3 -0
  77. package/dest/public/public_processor/public_processor.d.ts +9 -4
  78. package/dest/public/public_processor/public_processor.d.ts.map +1 -1
  79. package/dest/public/public_processor/public_processor.js +12 -6
  80. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts +41 -0
  81. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.d.ts.map +1 -0
  82. package/dest/public/public_tx_simulator/cpp_public_tx_simulator.js +89 -0
  83. package/dest/public/public_tx_simulator/index.d.ts +1 -0
  84. package/dest/public/public_tx_simulator/index.d.ts.map +1 -1
  85. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts +5 -4
  86. package/dest/public/public_tx_simulator/measured_public_tx_simulator.d.ts.map +1 -1
  87. package/dest/public/public_tx_simulator/measured_public_tx_simulator.js +3 -4
  88. package/dest/public/public_tx_simulator/public_tx_context.d.ts +4 -2
  89. package/dest/public/public_tx_simulator/public_tx_context.d.ts.map +1 -1
  90. package/dest/public/public_tx_simulator/public_tx_context.js +12 -8
  91. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts +24 -11
  92. package/dest/public/public_tx_simulator/public_tx_simulator.d.ts.map +1 -1
  93. package/dest/public/public_tx_simulator/public_tx_simulator.js +112 -44
  94. package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts +9 -0
  95. package/dest/public/public_tx_simulator/public_tx_simulator_interface.d.ts.map +1 -0
  96. package/dest/public/public_tx_simulator/public_tx_simulator_interface.js +1 -0
  97. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts +2 -1
  98. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.d.ts.map +1 -1
  99. package/dest/public/public_tx_simulator/telemetry_public_tx_simulator.js +2 -2
  100. package/dest/public/side_effect_errors.d.ts +41 -1
  101. package/dest/public/side_effect_errors.d.ts.map +1 -1
  102. package/dest/public/side_effect_errors.js +70 -1
  103. package/dest/public/side_effect_trace.d.ts +11 -4
  104. package/dest/public/side_effect_trace.d.ts.map +1 -1
  105. package/dest/public/side_effect_trace.js +35 -20
  106. package/dest/public/side_effect_trace_interface.d.ts +6 -0
  107. package/dest/public/side_effect_trace_interface.d.ts.map +1 -1
  108. package/dest/public/state_manager/nullifiers.d.ts +0 -3
  109. package/dest/public/state_manager/nullifiers.d.ts.map +1 -1
  110. package/dest/public/state_manager/nullifiers.js +1 -6
  111. package/dest/public/state_manager/state_manager.d.ts +7 -2
  112. package/dest/public/state_manager/state_manager.d.ts.map +1 -1
  113. package/dest/public/state_manager/state_manager.js +54 -15
  114. package/package.json +21 -17
  115. package/src/private/acvm_native.ts +1 -2
  116. package/src/private/circuit_recording/simulator_recorder_wrapper.ts +1 -1
  117. package/src/public/avm/avm_execution_environment.ts +4 -1
  118. package/src/public/avm/avm_gas.ts +2 -2
  119. package/src/public/avm/avm_simulator.ts +16 -27
  120. package/src/public/avm/errors.ts +7 -24
  121. package/src/public/avm/fixtures/avm_simulation_tester.ts +4 -2
  122. package/src/public/avm/fixtures/base_avm_simulation_tester.ts +32 -0
  123. package/src/public/avm/fixtures/initializers.ts +2 -1
  124. package/src/public/avm/opcodes/accrued_substate.ts +4 -4
  125. package/src/public/avm/opcodes/addressing_mode.ts +3 -3
  126. package/src/public/avm/opcodes/conversion.ts +4 -0
  127. package/src/public/avm/opcodes/ec_add.ts +3 -2
  128. package/src/public/avm/opcodes/environment_getters.ts +6 -8
  129. package/src/public/avm/opcodes/instruction.ts +8 -6
  130. package/src/public/avm/opcodes/misc.ts +41 -7
  131. package/src/public/avm/serialization/instruction_serialization.ts +12 -0
  132. package/src/public/avm/test_utils.ts +6 -14
  133. package/src/public/fixtures/bulk_test.ts +41 -23
  134. package/src/public/fixtures/custom_bytecode_tester.ts +49 -0
  135. package/src/public/fixtures/custom_bytecode_tests.ts +135 -0
  136. package/src/public/fixtures/index.ts +2 -1
  137. package/src/public/fixtures/minimal_public_tx.ts +8 -31
  138. package/src/public/fixtures/public_tx_simulation_tester.ts +16 -12
  139. package/src/public/fixtures/utils.ts +1 -1
  140. package/src/public/hinting_db_sources.ts +5 -0
  141. package/src/public/index.ts +6 -1
  142. package/src/public/public_db_sources.ts +19 -3
  143. package/src/public/public_errors.ts +14 -0
  144. package/src/public/public_processor/guarded_merkle_tree.ts +4 -0
  145. package/src/public/public_processor/public_processor.ts +23 -25
  146. package/src/public/public_tx_simulator/cpp_public_tx_simulator.ts +128 -0
  147. package/src/public/public_tx_simulator/index.ts +1 -0
  148. package/src/public/public_tx_simulator/measured_public_tx_simulator.ts +12 -9
  149. package/src/public/public_tx_simulator/public_tx_context.ts +11 -4
  150. package/src/public/public_tx_simulator/public_tx_simulator.ts +178 -60
  151. package/src/public/public_tx_simulator/public_tx_simulator_interface.ts +11 -0
  152. package/src/public/public_tx_simulator/telemetry_public_tx_simulator.ts +3 -12
  153. package/src/public/side_effect_errors.ts +91 -1
  154. package/src/public/side_effect_trace.ts +48 -23
  155. package/src/public/side_effect_trace_interface.ts +6 -0
  156. package/src/public/state_manager/nullifiers.ts +1 -7
  157. package/src/public/state_manager/state_manager.ts +73 -25
  158. package/dest/public/bytecode_errors.d.ts +0 -4
  159. package/dest/public/bytecode_errors.d.ts.map +0 -1
  160. package/dest/public/bytecode_errors.js +0 -6
  161. package/src/public/bytecode_errors.ts +0 -6
@@ -1,26 +1,19 @@
1
1
  import type { Point } from '@aztec/foundation/fields';
2
- import type { AztecAddress } from '@aztec/stdlib/aztec-address';
3
2
  import type { FailingFunction, NoirCallStack } from '@aztec/stdlib/errors';
4
3
 
5
4
  import { ExecutionError } from '../../common/errors.js';
5
+ import { CheckedPublicExecutionError } from '../public_errors.js';
6
6
 
7
7
  /**
8
8
  * Avm-specific errors should derive from this
9
9
  */
10
- export abstract class AvmExecutionError extends Error {
10
+ export abstract class AvmExecutionError extends CheckedPublicExecutionError {
11
11
  constructor(message: string) {
12
12
  super(message);
13
13
  this.name = 'AvmExecutionError';
14
14
  }
15
15
  }
16
16
 
17
- export class NoBytecodeForContractError extends AvmExecutionError {
18
- constructor(contractAddress: AztecAddress) {
19
- super(`No bytecode found at: ${contractAddress}`);
20
- this.name = 'NoBytecodeFoundInterpreterError';
21
- }
22
- }
23
-
24
17
  export class ArithmeticError extends AvmExecutionError {
25
18
  constructor(message: string) {
26
19
  super(message);
@@ -129,22 +122,12 @@ export class OutOfGasError extends AvmExecutionError {
129
122
  }
130
123
 
131
124
  /**
132
- * Error is thrown when the supplied points length is not a multiple of 3. Specific for MSM opcode.
133
- */
134
- export class MSMPointsLengthError extends AvmExecutionError {
135
- constructor(pointsReadLength: number) {
136
- super(`Points vector length should be a multiple of 3, was ${pointsReadLength}`);
137
- this.name = 'MSMPointsLengthError';
138
- }
139
- }
140
-
141
- /**
142
- * Error is thrown when one of the supplied points does not lie on the Grumpkin curve. Specific for MSM opcode.
125
+ * Error is thrown when one of the supplied points does not lie on the Grumpkin curve. Specific for ECADD opcode.
143
126
  */
144
- export class MSMPointNotOnCurveError extends AvmExecutionError {
145
- constructor(point: Point) {
146
- super(`Point ${point.toString()} is not on the curve.`);
147
- this.name = 'MSMPointNotOnCurveError';
127
+ export class EcAddPointNotOnCurveError extends AvmExecutionError {
128
+ constructor(pointIndex: number, point: Point) {
129
+ super(`EcAdd point${pointIndex} (${point.toString()}) is not on the curve.`);
130
+ this.name = 'EcAddPointNotOnCurveError';
148
131
  }
149
132
  }
150
133
 
@@ -37,9 +37,11 @@ export class AvmSimulationTester extends BaseAvmSimulationTester {
37
37
  super(contractDataSource, merkleTrees);
38
38
  }
39
39
 
40
- static async create(): Promise<AvmSimulationTester> {
40
+ static async create(
41
+ worldStateService: NativeWorldStateService, // make sure to close this later
42
+ ): Promise<AvmSimulationTester> {
41
43
  const contractDataSource = new SimpleContractDataSource();
42
- const merkleTrees = await (await NativeWorldStateService.tmp()).fork();
44
+ const merkleTrees = await worldStateService.fork();
43
45
  const treesDB = new PublicTreesDB(merkleTrees);
44
46
  const contractsDB = new PublicContractsDB(contractDataSource);
45
47
  const trace = new SideEffectTrace();
@@ -2,7 +2,9 @@ import { CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS } from '@aztec/constants';
2
2
  import { Fr } from '@aztec/foundation/fields';
3
3
  import { createLogger } from '@aztec/foundation/log';
4
4
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
5
+ import { getCanonicalAuthRegistry } from '@aztec/protocol-contracts/auth-registry';
5
6
  import { computeFeePayerBalanceStorageSlot, getCanonicalFeeJuice } from '@aztec/protocol-contracts/fee-juice';
7
+ import { getCanonicalInstanceRegistry } from '@aztec/protocol-contracts/instance-registry';
6
8
  import type { ContractArtifact } from '@aztec/stdlib/abi';
7
9
  import { PublicDataWrite } from '@aztec/stdlib/avm';
8
10
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
@@ -88,6 +90,36 @@ export abstract class BaseAvmSimulationTester {
88
90
  return feeJuice.instance;
89
91
  }
90
92
 
93
+ async registerAuthContract(): Promise<ContractInstanceWithAddress> {
94
+ const authRegistry = await getCanonicalAuthRegistry();
95
+ const authRegistryContractClassPublic = {
96
+ ...authRegistry.contractClass,
97
+ privateFunctions: [],
98
+ utilityFunctions: [],
99
+ };
100
+ await this.contractDataSource.addNewContract(
101
+ authRegistry.artifact,
102
+ authRegistryContractClassPublic,
103
+ authRegistry.instance,
104
+ );
105
+ return authRegistry.instance;
106
+ }
107
+
108
+ async registerInstanceRegistryContract(): Promise<ContractInstanceWithAddress> {
109
+ const instanceRegistry = await getCanonicalInstanceRegistry();
110
+ const instanceRegistryContractClassPublic = {
111
+ ...instanceRegistry.contractClass,
112
+ privateFunctions: [],
113
+ utilityFunctions: [],
114
+ };
115
+ await this.contractDataSource.addNewContract(
116
+ instanceRegistry.artifact,
117
+ instanceRegistryContractClassPublic,
118
+ instanceRegistry.instance,
119
+ );
120
+ return instanceRegistry.instance;
121
+ }
122
+
91
123
  async addContractInstance(contractInstance: ContractInstanceWithAddress, skipNullifierInsertion = false) {
92
124
  if (!skipNullifierInsertion) {
93
125
  await this.insertContractAddressNullifier(contractInstance.address);
@@ -1,4 +1,4 @@
1
- import { AVM_MAX_PROCESSABLE_L2_GAS } from '@aztec/constants';
1
+ import { AVM_MAX_PROCESSABLE_L2_GAS, DEFAULT_MAX_DEBUG_LOG_MEMORY_READS } from '@aztec/constants';
2
2
  import { EthAddress } from '@aztec/foundation/eth-address';
3
3
  import { Fr } from '@aztec/foundation/fields';
4
4
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
@@ -73,6 +73,7 @@ export function initExecutionEnvironment(overrides?: Partial<AvmExecutionEnviron
73
73
  overrides?.isStaticCall ?? false,
74
74
  overrides?.calldata ?? [],
75
75
  overrides?.clientInitiatedSimulation ?? true, // default to true for testing even though internal default is false
76
+ overrides?.maxDebugLogMemoryReads ?? DEFAULT_MAX_DEBUG_LOG_MEMORY_READS,
76
77
  );
77
78
  }
78
79
 
@@ -1,4 +1,4 @@
1
- import { NullifierCollisionError } from '../../state_manager/nullifiers.js';
1
+ import { NullifierCollisionError } from '../../side_effect_errors.js';
2
2
  import type { AvmContext } from '../avm_context.js';
3
3
  import { TypeTag, Uint1 } from '../avm_memory_types.js';
4
4
  import { InstructionExecutionError, StaticCallAlterationError } from '../errors.js';
@@ -219,8 +219,8 @@ export class EmitUnencryptedLog extends Instruction {
219
219
 
220
220
  constructor(
221
221
  private indirect: number,
222
- private logOffset: number,
223
222
  private logSizeOffset: number,
223
+ private logOffset: number,
224
224
  ) {
225
225
  super();
226
226
  }
@@ -237,8 +237,8 @@ export class EmitUnencryptedLog extends Instruction {
237
237
  this.baseGasCost(addressing.indirectOperandsCount(), addressing.relativeOperandsCount()),
238
238
  );
239
239
 
240
- const operands = [this.logOffset, this.logSizeOffset];
241
- const [logOffset, logSizeOffset] = addressing.resolve(operands, memory);
240
+ const operands = [this.logSizeOffset, this.logOffset];
241
+ const [logSizeOffset, logOffset] = addressing.resolve(operands, memory);
242
242
  memory.checkTag(TypeTag.UINT32, logSizeOffset);
243
243
  const logSize = memory.get(logSizeOffset).toNumber();
244
244
  memory.checkTagsRange(TypeTag.FIELD, logOffset, logSize);
@@ -2,6 +2,8 @@ import { AVM_MAX_OPERANDS } from '@aztec/constants';
2
2
  import { padArrayEnd } from '@aztec/foundation/collection';
3
3
  import type { Tuple } from '@aztec/foundation/serialize';
4
4
 
5
+ import { strict as assert } from 'assert';
6
+
5
7
  import { MemoryValue, TaggedMemory, type TaggedMemoryInterface, TypeTag } from '../avm_memory_types.js';
6
8
  import { RelativeAddressOutOfRangeError, TagCheckError } from '../errors.js';
7
9
 
@@ -20,9 +22,7 @@ export class Addressing {
20
22
  ) {}
21
23
 
22
24
  public static fromModes(modes: AddressingMode[]): Addressing {
23
- if (modes.length > AVM_MAX_OPERANDS) {
24
- throw new Error('Too many operands for addressing mode');
25
- }
25
+ assert(modes.length <= AVM_MAX_OPERANDS, 'Too many operands for addressing mode');
26
26
  return new Addressing(padArrayEnd(modes, AddressingMode.DIRECT, AVM_MAX_OPERANDS));
27
27
  }
28
28
 
@@ -80,6 +80,10 @@ export class ToRadixBE extends Instruction {
80
80
  value /= radixBN;
81
81
  }
82
82
 
83
+ if (value !== 0n) {
84
+ throw new InvalidToRadixInputsError(`Field failed to decompose in ${numLimbs} limbs.`);
85
+ }
86
+
83
87
  const outputType = outputBits != 0 ? Uint1 : Uint8;
84
88
  const res = limbArray.map(byte => new outputType(byte));
85
89
  memory.setSlice(dstOffset, res);
@@ -3,6 +3,7 @@ import { Point } from '@aztec/foundation/fields';
3
3
 
4
4
  import type { AvmContext } from '../avm_context.js';
5
5
  import { Field, TypeTag, Uint1 } from '../avm_memory_types.js';
6
+ import { EcAddPointNotOnCurveError } from '../errors.js';
6
7
  import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
7
8
  import { Addressing } from './addressing_mode.js';
8
9
  import { Instruction } from './instruction.js';
@@ -65,7 +66,7 @@ export class EcAdd extends Instruction {
65
66
  const p1IsInfinite = memory.get(p1IsInfiniteOffset).toNumber() === 1;
66
67
  const p1 = new Point(p1X.toFr(), p1Y.toFr(), p1IsInfinite);
67
68
  if (!p1.isOnGrumpkin()) {
68
- throw new Error(`Point1 is not on the curve`);
69
+ throw new EcAddPointNotOnCurveError(/*pointIndex=*/ 1, p1);
69
70
  }
70
71
 
71
72
  const p2X = memory.get(p2XOffset);
@@ -74,7 +75,7 @@ export class EcAdd extends Instruction {
74
75
  const p2IsInfinite = memory.get(p2IsInfiniteOffset).toNumber() === 1;
75
76
  const p2 = new Point(p2X.toFr(), p2Y.toFr(), p2IsInfinite);
76
77
  if (!p2.isOnGrumpkin()) {
77
- throw new Error(`Point1 is not on the curve`);
78
+ throw new EcAddPointNotOnCurveError(/*pointIndex=*/ 2, p2);
78
79
  }
79
80
 
80
81
  const grumpkin = new Grumpkin();
@@ -20,8 +20,8 @@ export enum EnvironmentVariable {
20
20
  DAGASLEFT,
21
21
  }
22
22
 
23
- function getValue(e: EnvironmentVariable, ctx: AvmContext) {
24
- switch (e) {
23
+ function getValue(varEnum: EnvironmentVariable, ctx: AvmContext) {
24
+ switch (varEnum) {
25
25
  case EnvironmentVariable.ADDRESS:
26
26
  return new Field(ctx.environment.address.toField());
27
27
  case EnvironmentVariable.SENDER:
@@ -47,7 +47,7 @@ function getValue(e: EnvironmentVariable, ctx: AvmContext) {
47
47
  case EnvironmentVariable.DAGASLEFT:
48
48
  return new Uint32(ctx.machineState.daGasLeft);
49
49
  default:
50
- throw new Error(`Unknown environment variable ${e}`);
50
+ throw new InstructionExecutionError(`Invalid GETENVVAR var enum ${varEnum}`);
51
51
  }
52
52
  }
53
53
 
@@ -77,13 +77,11 @@ export class GetEnvVar extends Instruction {
77
77
  this.baseGasCost(addressing.indirectOperandsCount(), addressing.relativeOperandsCount()),
78
78
  );
79
79
 
80
- if (!(this.varEnum in EnvironmentVariable)) {
81
- throw new InstructionExecutionError(`Invalid GETENVVAR var enum ${this.varEnum}`);
82
- }
83
-
84
80
  const operands = [this.dstOffset];
85
81
  const [dstOffset] = addressing.resolve(operands, memory);
86
82
 
87
- memory.set(dstOffset, getValue(this.varEnum as EnvironmentVariable, context));
83
+ const value = getValue(this.varEnum as EnvironmentVariable, context);
84
+
85
+ memory.set(dstOffset, value);
88
86
  }
89
87
  }
@@ -36,6 +36,8 @@ export abstract class Instruction {
36
36
  * @returns Thee string representation.
37
37
  */
38
38
  public toString(): string {
39
+ // Note: we could have this be the actual wire opcode if `bytecode_serialization.ts` were to
40
+ // pass it into the class' static `Instruction.as(this, wireFormat)` method.
39
41
  let instructionStr = this.constructor.name + ': ';
40
42
  // assumes that all properties are flags or operands
41
43
  for (const prop of Object.getOwnPropertyNames(this) as (keyof Instruction)[]) {
@@ -114,9 +116,7 @@ export abstract class Instruction {
114
116
  */
115
117
  public get type(): string {
116
118
  const type = 'type' in this.constructor && (this.constructor.type as string);
117
- if (!type) {
118
- throw new Error(`Instruction class ${this.constructor.name} does not have a static 'type' property defined.`);
119
- }
119
+ assert(!!type, `Instruction class ${this.constructor.name} does not have a static 'type' property defined.`);
120
120
  return type;
121
121
  }
122
122
 
@@ -126,9 +126,11 @@ export abstract class Instruction {
126
126
  */
127
127
  public get opcode(): Opcode {
128
128
  const opcode = 'opcode' in this.constructor ? (this.constructor.opcode as Opcode) : undefined;
129
- if (opcode === undefined || Opcode[opcode] === undefined) {
130
- throw new Error(`Instruction class ${this.constructor.name} does not have a static 'opcode' property defined.`);
131
- }
129
+ assert(
130
+ opcode !== undefined,
131
+ `Instruction class ${this.constructor.name} does not have a static 'opcode' property defined.`,
132
+ );
133
+ assert(Opcode[opcode] !== undefined, `Invalid opcode ${opcode} for instruction class ${this.constructor.name}.`);
132
134
  return opcode;
133
135
  }
134
136
  }
@@ -1,4 +1,4 @@
1
- import { applyStringFormatting, createLogger } from '@aztec/foundation/log';
1
+ import { LogLevels, applyStringFormatting, createLogger } from '@aztec/foundation/log';
2
2
 
3
3
  import type { AvmContext } from '../avm_context.js';
4
4
  import { TypeTag } from '../avm_memory_types.js';
@@ -15,6 +15,7 @@ export class DebugLog extends Instruction {
15
15
  static readonly wireFormat: OperandType[] = [
16
16
  OperandType.UINT8, // Opcode
17
17
  OperandType.UINT8, // Indirect
18
+ OperandType.UINT16, // level memory address
18
19
  OperandType.UINT16, // message memory address
19
20
  OperandType.UINT16, // fields memory address
20
21
  OperandType.UINT16, // fields size address
@@ -23,6 +24,7 @@ export class DebugLog extends Instruction {
23
24
 
24
25
  constructor(
25
26
  private indirect: number,
27
+ private levelOffset: number,
26
28
  private messageOffset: number,
27
29
  private fieldsOffset: number,
28
30
  private fieldsSizeOffset: number,
@@ -39,16 +41,30 @@ export class DebugLog extends Instruction {
39
41
  this.baseGasCost(addressing.indirectOperandsCount(), addressing.relativeOperandsCount()),
40
42
  );
41
43
 
42
- const operands = [this.messageOffset, this.fieldsOffset, this.fieldsSizeOffset];
43
- const [messageOffset, fieldsOffset, fieldsSizeOffset] = addressing.resolve(operands, memory);
44
+ const operands = [this.levelOffset, this.messageOffset, this.fieldsOffset, this.fieldsSizeOffset];
45
+ const [levelOffset, messageOffset, fieldsOffset, fieldsSizeOffset] = addressing.resolve(operands, memory);
44
46
 
45
- // DebugLog is a no-op except when doing client-initiated simulation with debug logging enabled.
47
+ // DebugLog is a no-op except when doing client-initiated simulation.
46
48
  // Note that we still do address resolution and basic tag-checking (above)
47
49
  // To avoid a special-case in the witness generator and circuit.
48
- if (context.environment.clientInitiatedSimulation && DebugLog.logger.isLevelEnabled('verbose')) {
50
+ if (context.environment.clientInitiatedSimulation) {
51
+ memory.checkTag(TypeTag.UINT8, levelOffset);
52
+ const levelNumber = memory.get(levelOffset).toNumber();
49
53
  memory.checkTag(TypeTag.UINT32, fieldsSizeOffset);
50
54
  const fieldsSize = memory.get(fieldsSizeOffset).toNumber();
51
55
 
56
+ const memoryReads = 1 /* level */ + 1 /* fieldsSize */ + this.messageSize /* message */ + fieldsSize; /* fields */
57
+ if (
58
+ context.persistableState.getDebugLogMemoryReads() + memoryReads >
59
+ context.environment.maxDebugLogMemoryReads
60
+ ) {
61
+ // Regular error on purpose: this is not a recoverable error.
62
+ throw new Error(
63
+ `Max debug log memory reads exceeded: ${context.persistableState.getDebugLogMemoryReads() + memoryReads} > ${context.environment.maxDebugLogMemoryReads}`,
64
+ );
65
+ }
66
+ context.persistableState.writeDebugLogMemoryReads(memoryReads);
67
+
52
68
  const rawMessage = memory.getSlice(messageOffset, this.messageSize);
53
69
  const fields = memory.getSlice(fieldsOffset, fieldsSize);
54
70
 
@@ -57,12 +73,30 @@ export class DebugLog extends Instruction {
57
73
 
58
74
  // Interpret str<N> = [u8; N] to string.
59
75
  const messageAsStr = rawMessage.map(field => String.fromCharCode(field.toNumber())).join('');
60
- const formattedStr = applyStringFormatting(
76
+
77
+ if (!LogLevels[levelNumber]) {
78
+ // Regular error on purpose: this is not a recoverable error.
79
+ throw new Error(`Invalid debug log level: ${levelNumber}`);
80
+ }
81
+
82
+ const level = LogLevels[levelNumber];
83
+
84
+ context.persistableState.writeDebugLog(
85
+ context.environment.address,
86
+ level,
61
87
  messageAsStr,
62
88
  fields.map(field => field.toFr()),
63
89
  );
64
90
 
65
- DebugLog.logger.verbose(formattedStr);
91
+ // Skips string formatting if the level is disabled.
92
+ if (DebugLog.logger.isLevelEnabled(level)) {
93
+ const formattedStr = applyStringFormatting(
94
+ messageAsStr,
95
+ fields.map(field => field.toFr()),
96
+ );
97
+
98
+ DebugLog.logger[level](formattedStr);
99
+ }
66
100
  }
67
101
  }
68
102
  }
@@ -124,6 +124,18 @@ const OPERAND_SPEC = new Map<OperandType, [number, (offset: number) => OperandNa
124
124
  [OperandType.TAG, [1, Buffer.prototype.readUint8, Buffer.prototype.writeUint8]],
125
125
  ]);
126
126
 
127
+ /**
128
+ * Returns the size of an operand in bytes.
129
+ * Should not be called with unknown operand types.
130
+ * @param operandType
131
+ * @returns number size in bytes
132
+ * @throws AssertionError if the operand type is unknown
133
+ */
134
+ export function getOperandSize(operandType: OperandType): number {
135
+ assert(OPERAND_SPEC.has(operandType), `Unknown operand type: ${operandType}`);
136
+ return OPERAND_SPEC.get(operandType)![0];
137
+ }
138
+
127
139
  function readUintBE(buf: Buffer, offset: number, totalBytes: number): bigint {
128
140
  let value: bigint = 0n;
129
141
  for (let i = 0; i < totalBytes; ++i) {
@@ -27,13 +27,12 @@ export function mockStorageReadWithMap(worldStateDB: PublicTreesDB, mockedStorag
27
27
  );
28
28
  }
29
29
 
30
- export function mockNoteHashExists(worldStateDB: PublicTreesDB, _leafIndex: bigint, value?: Fr) {
30
+ export function mockGetNoteHash(worldStateDB: PublicTreesDB, _leafIndex: bigint, value?: Fr) {
31
31
  (worldStateDB as jest.Mocked<PublicTreesDB>).getNoteHash.mockImplementation((index: bigint) => {
32
- if (index == _leafIndex) {
32
+ if (index == _leafIndex && value) {
33
33
  return Promise.resolve(value);
34
34
  } else {
35
- // This is ok for now since the traceing functions handle it
36
- return Promise.resolve(undefined);
35
+ return Promise.resolve(Fr.ZERO);
37
36
  }
38
37
  });
39
38
  }
@@ -42,19 +41,12 @@ export function mockCheckNullifierExists(worldStateDB: PublicTreesDB, exists: bo
42
41
  (worldStateDB as jest.Mocked<PublicTreesDB>).checkNullifierExists.mockResolvedValue(exists);
43
42
  }
44
43
 
45
- export function mockL1ToL2MessageExists(
46
- worldStateDB: PublicTreesDB,
47
- leafIndex: bigint,
48
- value: Fr,
49
- valueAtOtherIndices?: Fr,
50
- ) {
44
+ export function mockGetL1ToL2LeafValue(worldStateDB: PublicTreesDB, leafIndex: bigint, value?: Fr) {
51
45
  (worldStateDB as jest.Mocked<PublicTreesDB>).getL1ToL2LeafValue.mockImplementation((index: bigint) => {
52
- if (index == leafIndex) {
46
+ if (index == leafIndex && value) {
53
47
  return Promise.resolve(value);
54
48
  } else {
55
- // any indices other than mockAtLeafIndex will return a different value
56
- // (or undefined if no value is specified for other indices)
57
- return Promise.resolve(valueAtOtherIndices!);
49
+ return Promise.resolve(Fr.ZERO!);
58
50
  }
59
51
  });
60
52
  }
@@ -20,11 +20,19 @@ export async function bulkTest(
20
20
  avmTestContractArtifact,
21
21
  );
22
22
 
23
+ // Needed since we invoke the Fee Juice Contract in the bulk test.registerFeeJuiceContract
24
+ await tester.registerFeeJuiceContract();
25
+ // Register multiple different protocol contracts (to ensure we don't dedup bytecode hashing events):
26
+ await tester.registerAuthContract();
27
+ await tester.registerInstanceRegistryContract();
28
+
23
29
  // Get a deployed contract instance to pass to the contract
24
30
  // for it to use as "expected" values when testing contract instance retrieval.
25
31
  const expectContractInstance = avmTestContract;
26
32
  const argsField = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
27
- const argsU8 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
33
+ const argsU8 = [1, 2, 3, 4, 5, 6, 7, 8].map(x => new Fr(x));
34
+ argsU8.push(new Fr(2n ** 128n + 9n)); // Trigger truncation from large (> 128 bits) value (canonical decomposition event)
35
+ argsU8.push(new Fr(2n ** 125n + 10n)); // Trigger truncation from small (< 128 bits) value (no canonical decomposition event)
28
36
  const args = [
29
37
  argsField,
30
38
  argsU8,
@@ -45,6 +53,26 @@ export async function bulkTest(
45
53
  fnName: 'bulk_testing',
46
54
  args,
47
55
  },
56
+ // 3 calls creating calldata + asserting calldata copy:
57
+ {
58
+ address: avmTestContract.address,
59
+ fnName: 'assert_calldata_copy_large',
60
+ args: [Array.from({ length: 300 }, () => Fr.random()), /* with_selector: */ true],
61
+ },
62
+ {
63
+ address: avmTestContract.address,
64
+ fnName: 'assert_calldata_copy',
65
+ args: [argsField.slice(3), /* with_selector: */ true],
66
+ },
67
+ {
68
+ address: avmTestContract.address,
69
+ fnName: 'assert_calldata_copy_large',
70
+ args: [Array.from({ length: 300 }, () => Fr.random()), /* with_selector: */ true],
71
+ },
72
+ // 3 calls to external contracts
73
+ { address: avmTestContract.address, fnName: 'call_fee_juice', args: [] },
74
+ { address: avmTestContract.address, fnName: 'call_auth_registry', args: [] },
75
+ { address: avmTestContract.address, fnName: 'call_instance_registry', args: [] },
48
76
  ],
49
77
  /*teardownCall=*/ undefined,
50
78
  /*feePayer*/ undefined,
@@ -78,25 +106,22 @@ export async function megaBulkTest(
78
106
  deployer,
79
107
  avmTestContractArtifact,
80
108
  );
109
+
110
+ // Needed since we invoke the Fee Juice Contract in the bulk test.registerFeeJuiceContract
111
+ await tester.registerFeeJuiceContract();
112
+
81
113
  // Get a deployed contract instance to pass to the contract
82
114
  // for it to use as "expected" values when testing contract instance retrieval.
83
115
  const expectContractInstance = avmTestContract;
84
116
  const argsField0 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
85
- const argsField1 = [2, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
86
- const argsField2 = [3, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
87
- const argsField3 = [4, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
88
- const argsField4 = [5, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
89
- const argsField5 = [6, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
90
- const argsField6 = [7, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
91
- const argsField7 = [8, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
92
- const argsField8 = [9, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
93
- const argsField9 = [10, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
94
- const argsField10 = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
95
- const argsField11 = [12, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
96
- const argsField12 = [13, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
97
- const argsField13 = [14, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
98
- const argsField14 = [15, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
99
- const argsField15 = [16, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
117
+ const argsField1 = [3, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
118
+ const argsField2 = [5, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
119
+ const argsField3 = [7, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
120
+ const argsField4 = [9, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
121
+ const argsField5 = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
122
+ const argsField6 = [13, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
123
+ const argsField7 = [15, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
124
+ const argsField8 = [17, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
100
125
  const argsU8 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
101
126
  const genArgs = (argsField: Fr[]) => [
102
127
  argsField,
@@ -123,13 +148,6 @@ export async function megaBulkTest(
123
148
  { address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField6) },
124
149
  { address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField7) },
125
150
  { address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField8) },
126
- { address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField9) },
127
- { address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField10) },
128
- { address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField11) },
129
- { address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField12) },
130
- { address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField13) },
131
- { address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField14) },
132
- { address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField15) },
133
151
  ],
134
152
  /*teardownCall=*/ undefined,
135
153
  /*feePayer*/ undefined,
@@ -0,0 +1,49 @@
1
+ import { FunctionType, emptyContractArtifact, emptyFunctionArtifact } from '@aztec/stdlib/abi';
2
+ import { AztecAddress } from '@aztec/stdlib/aztec-address';
3
+
4
+ import type { PublicTxResult } from '../public_tx_simulator/public_tx_simulator.js';
5
+ import { PublicTxSimulationTester } from './public_tx_simulation_tester.js';
6
+
7
+ /**
8
+ *
9
+ * Test custom bytecode (simulation or proving) with the provided bytecode.
10
+ * @param bytecode - The bytecode buffer to use
11
+ * @param tester - The tester to use (simulation or proving)
12
+ * @param txLabel - The label of the transaction
13
+ * @param contractName - The name of the contract (default: 'CustomBytecodeContract')
14
+ */
15
+ export async function testCustomBytecode(
16
+ bytecode: Buffer,
17
+ tester: PublicTxSimulationTester,
18
+ txLabel: string,
19
+ contractName: string = 'CustomBytecodeContract',
20
+ ): Promise<PublicTxResult> {
21
+ const deployer = AztecAddress.fromNumber(42);
22
+
23
+ const contractArtifact = emptyContractArtifact();
24
+ contractArtifact.name = contractName;
25
+ contractArtifact.functions = [emptyFunctionArtifact()];
26
+ contractArtifact.functions[0].name = 'public_dispatch';
27
+ contractArtifact.functions[0].functionType = FunctionType.PUBLIC;
28
+ contractArtifact.functions[0].bytecode = bytecode;
29
+
30
+ const testContract = await tester.registerAndDeployContract(
31
+ /*constructorArgs=*/ [],
32
+ deployer,
33
+ /*contractArtifact=*/ contractArtifact,
34
+ );
35
+
36
+ // EXECUTE! This means that if using AvmProvingTester subclass, it will PROVE the transaction!
37
+ return await tester.executeTxWithLabel(
38
+ /*txLabel=*/ txLabel,
39
+ /*sender=*/ deployer,
40
+ /*setupCalls=*/ [],
41
+ /*appCalls=*/ [
42
+ {
43
+ address: testContract.address,
44
+ fnName: 'public_dispatch',
45
+ args: [],
46
+ },
47
+ ],
48
+ );
49
+ }