@aztec/simulator 0.52.0 → 0.54.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 (92) hide show
  1. package/dest/acvm/acvm.d.ts +7 -0
  2. package/dest/acvm/acvm.d.ts.map +1 -1
  3. package/dest/acvm/acvm.js +17 -1
  4. package/dest/avm/avm_execution_environment.d.ts +2 -2
  5. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  6. package/dest/avm/avm_execution_environment.js +5 -6
  7. package/dest/avm/avm_gas.d.ts.map +1 -1
  8. package/dest/avm/avm_gas.js +73 -43
  9. package/dest/avm/avm_memory_types.d.ts +1 -1
  10. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  11. package/dest/avm/avm_memory_types.js +5 -3
  12. package/dest/avm/bytecode_utils.js +5 -5
  13. package/dest/avm/errors.js +2 -2
  14. package/dest/avm/fixtures/index.d.ts +2 -0
  15. package/dest/avm/fixtures/index.d.ts.map +1 -1
  16. package/dest/avm/fixtures/index.js +18 -2
  17. package/dest/avm/journal/journal.d.ts +1 -1
  18. package/dest/avm/journal/journal.d.ts.map +1 -1
  19. package/dest/avm/journal/journal.js +13 -8
  20. package/dest/avm/opcodes/arithmetic.d.ts +9 -16
  21. package/dest/avm/opcodes/arithmetic.d.ts.map +1 -1
  22. package/dest/avm/opcodes/arithmetic.js +12 -37
  23. package/dest/avm/opcodes/bitwise.d.ts +5 -5
  24. package/dest/avm/opcodes/bitwise.d.ts.map +1 -1
  25. package/dest/avm/opcodes/bitwise.js +6 -6
  26. package/dest/avm/opcodes/comparators.d.ts +3 -3
  27. package/dest/avm/opcodes/comparators.d.ts.map +1 -1
  28. package/dest/avm/opcodes/comparators.js +4 -4
  29. package/dest/avm/opcodes/control_flow.d.ts.map +1 -1
  30. package/dest/avm/opcodes/control_flow.js +6 -6
  31. package/dest/avm/opcodes/instruction.d.ts +18 -12
  32. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  33. package/dest/avm/opcodes/instruction.js +36 -19
  34. package/dest/avm/opcodes/instruction_impl.d.ts +4 -2
  35. package/dest/avm/opcodes/instruction_impl.d.ts.map +1 -1
  36. package/dest/avm/opcodes/instruction_impl.js +15 -7
  37. package/dest/avm/opcodes/memory.d.ts +11 -12
  38. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  39. package/dest/avm/opcodes/memory.js +68 -68
  40. package/dest/avm/serialization/bytecode_serialization.d.ts +7 -8
  41. package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  42. package/dest/avm/serialization/bytecode_serialization.js +83 -67
  43. package/dest/avm/serialization/instruction_serialization.d.ts +84 -68
  44. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  45. package/dest/avm/serialization/instruction_serialization.js +102 -71
  46. package/dest/avm/test_utils.d.ts +1 -1
  47. package/dest/avm/test_utils.d.ts.map +1 -1
  48. package/dest/avm/test_utils.js +11 -3
  49. package/dest/public/abstract_phase_manager.d.ts.map +1 -1
  50. package/dest/public/abstract_phase_manager.js +4 -4
  51. package/dest/public/db_interfaces.d.ts +6 -0
  52. package/dest/public/db_interfaces.d.ts.map +1 -1
  53. package/dest/public/execution.d.ts +3 -3
  54. package/dest/public/execution.d.ts.map +1 -1
  55. package/dest/public/execution.js +1 -1
  56. package/dest/public/hints_builder.d.ts +6 -4
  57. package/dest/public/hints_builder.d.ts.map +1 -1
  58. package/dest/public/hints_builder.js +20 -5
  59. package/dest/public/public_db_sources.d.ts +1 -0
  60. package/dest/public/public_db_sources.d.ts.map +1 -1
  61. package/dest/public/public_db_sources.js +11 -1
  62. package/dest/public/side_effect_trace.d.ts +2 -2
  63. package/dest/public/side_effect_trace.d.ts.map +1 -1
  64. package/dest/public/side_effect_trace.js +10 -12
  65. package/dest/public/tail_phase_manager.d.ts.map +1 -1
  66. package/dest/public/tail_phase_manager.js +4 -3
  67. package/package.json +9 -9
  68. package/src/acvm/acvm.ts +22 -0
  69. package/src/avm/avm_execution_environment.ts +3 -4
  70. package/src/avm/avm_gas.ts +72 -42
  71. package/src/avm/avm_memory_types.ts +4 -2
  72. package/src/avm/bytecode_utils.ts +4 -4
  73. package/src/avm/errors.ts +1 -1
  74. package/src/avm/fixtures/index.ts +32 -2
  75. package/src/avm/journal/journal.ts +14 -8
  76. package/src/avm/opcodes/arithmetic.ts +12 -41
  77. package/src/avm/opcodes/bitwise.ts +5 -5
  78. package/src/avm/opcodes/comparators.ts +3 -3
  79. package/src/avm/opcodes/control_flow.ts +5 -5
  80. package/src/avm/opcodes/instruction.ts +41 -19
  81. package/src/avm/opcodes/instruction_impl.ts +14 -6
  82. package/src/avm/opcodes/memory.ts +76 -74
  83. package/src/avm/serialization/bytecode_serialization.ts +93 -77
  84. package/src/avm/serialization/instruction_serialization.ts +58 -25
  85. package/src/avm/test_utils.ts +9 -2
  86. package/src/public/abstract_phase_manager.ts +3 -2
  87. package/src/public/db_interfaces.ts +7 -0
  88. package/src/public/execution.ts +3 -2
  89. package/src/public/hints_builder.ts +48 -13
  90. package/src/public/public_db_sources.ts +11 -0
  91. package/src/public/side_effect_trace.ts +11 -12
  92. package/src/public/tail_phase_manager.ts +10 -7
@@ -350,7 +350,7 @@ export class TaggedMemory implements TaggedMemoryInterface {
350
350
  }
351
351
 
352
352
  // Truncates the value to fit the type.
353
- public static integralFromTag(v: bigint | number, tag: TypeTag): IntegralValue {
353
+ public static buildFromTagTruncating(v: bigint | number, tag: TypeTag): MemoryValue {
354
354
  v = BigInt(v);
355
355
  switch (tag) {
356
356
  case TypeTag.UINT8:
@@ -363,8 +363,10 @@ export class TaggedMemory implements TaggedMemoryInterface {
363
363
  return new Uint64(v & ((1n << 64n) - 1n));
364
364
  case TypeTag.UINT128:
365
365
  return new Uint128(v & ((1n << 128n) - 1n));
366
+ case TypeTag.FIELD:
367
+ return new Field(v);
366
368
  default:
367
- throw new Error(`${TypeTag[tag]} is not a valid integral type.`);
369
+ throw new Error(`${TypeTag[tag]} is not a valid tag.`);
368
370
  }
369
371
  }
370
372
 
@@ -1,13 +1,13 @@
1
1
  import { promisify } from 'util';
2
2
  import { gunzip } from 'zlib';
3
3
 
4
- import { Mov } from '../avm/opcodes/memory.js';
4
+ import { Opcode } from './serialization/instruction_serialization.js';
5
5
 
6
6
  const AVM_MAGIC_SUFFIX = Buffer.from([
7
- Mov.opcode, // opcode
7
+ Opcode.MOV_16, // opcode
8
8
  0x00, // indirect
9
- ...Buffer.from('000018ca', 'hex'), // srcOffset
10
- ...Buffer.from('000018ca', 'hex'), // dstOffset
9
+ ...Buffer.from('18ca', 'hex'), // srcOffset
10
+ ...Buffer.from('18ca', 'hex'), // dstOffset
11
11
  ]);
12
12
 
13
13
  export function markBytecodeAsAvm(bytecode: Buffer): Buffer {
package/src/avm/errors.ts CHANGED
@@ -151,7 +151,7 @@ export function revertReasonFromExplicitRevert(revertData: Fr[], context: AvmCon
151
151
  */
152
152
  export function decodeRevertDataAsMessage(revertData: Fr[]): string {
153
153
  if (revertData.length === 0) {
154
- return 'Assertion failed.';
154
+ return 'Assertion failed';
155
155
  } else {
156
156
  try {
157
157
  // We remove the first element which is the 'error selector'.
@@ -1,5 +1,6 @@
1
+ import { isNoirCallStackUnresolved } from '@aztec/circuit-types';
1
2
  import { GasFees, GlobalVariables, Header } from '@aztec/circuits.js';
2
- import { FunctionSelector } from '@aztec/foundation/abi';
3
+ import { FunctionSelector, getFunctionDebugMetadata } from '@aztec/foundation/abi';
3
4
  import { AztecAddress } from '@aztec/foundation/aztec-address';
4
5
  import { EthAddress } from '@aztec/foundation/eth-address';
5
6
  import { Fr } from '@aztec/foundation/fields';
@@ -9,12 +10,19 @@ import { strict as assert } from 'assert';
9
10
  import { mock } from 'jest-mock-extended';
10
11
  import merge from 'lodash.merge';
11
12
 
12
- import { type CommitmentsDB, type PublicContractsDB, type PublicStateDB } from '../../index.js';
13
+ import {
14
+ type CommitmentsDB,
15
+ type PublicContractsDB,
16
+ type PublicStateDB,
17
+ resolveAssertionMessage,
18
+ traverseCauseChain,
19
+ } from '../../index.js';
13
20
  import { type PublicSideEffectTraceInterface } from '../../public/side_effect_trace_interface.js';
14
21
  import { AvmContext } from '../avm_context.js';
15
22
  import { AvmContextInputs, AvmExecutionEnvironment } from '../avm_execution_environment.js';
16
23
  import { AvmMachineState } from '../avm_machine_state.js';
17
24
  import { Field, Uint8, Uint64 } from '../avm_memory_types.js';
25
+ import { type AvmRevertReason } from '../errors.js';
18
26
  import { HostStorage } from '../journal/host_storage.js';
19
27
  import { AvmPersistableStateManager } from '../journal/journal.js';
20
28
  import { NullifierManager } from '../journal/nullifiers.js';
@@ -152,3 +160,25 @@ export function getAvmTestContractBytecode(functionName: string): Buffer {
152
160
  );
153
161
  return artifact.bytecode;
154
162
  }
163
+
164
+ export function resolveAvmTestContractAssertionMessage(
165
+ functionName: string,
166
+ revertReason: AvmRevertReason,
167
+ ): string | undefined {
168
+ const functionArtifact = AvmTestContractArtifact.functions.find(f => f.name === functionName)!;
169
+
170
+ traverseCauseChain(revertReason, cause => {
171
+ revertReason = cause as AvmRevertReason;
172
+ });
173
+
174
+ if (!functionArtifact || !revertReason.noirCallStack || !isNoirCallStackUnresolved(revertReason.noirCallStack)) {
175
+ return undefined;
176
+ }
177
+
178
+ const debugMetadata = getFunctionDebugMetadata(AvmTestContractArtifact, functionArtifact);
179
+ if (!debugMetadata) {
180
+ return undefined;
181
+ }
182
+
183
+ return resolveAssertionMessage(revertReason.noirCallStack, debugMetadata);
184
+ }
@@ -1,5 +1,5 @@
1
1
  import { AztecAddress, type FunctionSelector, type Gas } from '@aztec/circuits.js';
2
- import { type Fr } from '@aztec/foundation/fields';
2
+ 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
 
@@ -122,10 +122,14 @@ export class AvmPersistableStateManager {
122
122
  * @returns true if the note hash exists at the given leaf index, false otherwise
123
123
  */
124
124
  public async checkNoteHashExists(storageAddress: Fr, noteHash: Fr, leafIndex: Fr): Promise<boolean> {
125
- const gotLeafIndex = await this.hostStorage.commitmentsDb.getCommitmentIndex(noteHash);
126
- const exists = gotLeafIndex === leafIndex.toBigInt();
127
- this.log.debug(`noteHashes(${storageAddress})@${noteHash} ?? leafIndex: ${leafIndex}, exists: ${exists}.`);
128
- this.trace.traceNoteHashCheck(storageAddress, noteHash, leafIndex, exists);
125
+ const gotLeafValue = (await this.hostStorage.commitmentsDb.getCommitmentValue(leafIndex.toBigInt())) ?? Fr.ZERO;
126
+ const exists = gotLeafValue.equals(noteHash);
127
+ this.log.debug(
128
+ `noteHashes(${storageAddress})@${noteHash} ?? leafIndex: ${leafIndex} | gotLeafValue: ${gotLeafValue}, exists: ${exists}.`,
129
+ );
130
+ // TODO(8287): We still return exists here, but we need to transmit both the requested noteHash and the gotLeafValue
131
+ // such that the VM can constrain the equality and decide on exists based on that.
132
+ this.trace.traceNoteHashCheck(storageAddress, gotLeafValue, leafIndex, exists);
129
133
  return Promise.resolve(exists);
130
134
  }
131
135
 
@@ -173,12 +177,14 @@ export class AvmPersistableStateManager {
173
177
  * @returns exists - whether the message exists in the L1 to L2 Messages tree
174
178
  */
175
179
  public async checkL1ToL2MessageExists(contractAddress: Fr, msgHash: Fr, msgLeafIndex: Fr): Promise<boolean> {
176
- const valueAtIndex = await this.hostStorage.commitmentsDb.getL1ToL2LeafValue(msgLeafIndex.toBigInt());
177
- const exists = valueAtIndex?.equals(msgHash) ?? false;
180
+ const valueAtIndex = (await this.hostStorage.commitmentsDb.getL1ToL2LeafValue(msgLeafIndex.toBigInt())) ?? Fr.ZERO;
181
+ const exists = valueAtIndex.equals(msgHash);
178
182
  this.log.debug(
179
183
  `l1ToL2Messages(@${msgLeafIndex}) ?? exists: ${exists}, expected: ${msgHash}, found: ${valueAtIndex}.`,
180
184
  );
181
- this.trace.traceL1ToL2MessageCheck(contractAddress, msgHash, msgLeafIndex, exists);
185
+ // TODO(8287): We still return exists here, but we need to transmit both the requested msgHash and the value
186
+ // such that the VM can constrain the equality and decide on exists based on that.
187
+ this.trace.traceL1ToL2MessageCheck(contractAddress, valueAtIndex, msgLeafIndex, exists);
182
188
  return Promise.resolve(exists);
183
189
  }
184
190
 
@@ -1,8 +1,7 @@
1
1
  import type { AvmContext } from '../avm_context.js';
2
- import { type Field, type MemoryValue, TypeTag } from '../avm_memory_types.js';
3
- import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
2
+ import { type Field, type MemoryValue } from '../avm_memory_types.js';
3
+ import { Opcode } from '../serialization/instruction_serialization.js';
4
4
  import { Addressing } from './addressing_mode.js';
5
- import { Instruction } from './instruction.js';
6
5
  import { ThreeOperandInstruction } from './instruction_impl.js';
7
6
 
8
7
  export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInstruction {
@@ -32,7 +31,7 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst
32
31
 
33
32
  export class Add extends ThreeOperandArithmeticInstruction {
34
33
  static readonly type: string = 'ADD';
35
- static readonly opcode = Opcode.ADD;
34
+ static readonly opcode = Opcode.ADD_8; // FIXME: needed for gas.
36
35
 
37
36
  protected compute(a: MemoryValue, b: MemoryValue): MemoryValue {
38
37
  return a.add(b);
@@ -41,7 +40,7 @@ export class Add extends ThreeOperandArithmeticInstruction {
41
40
 
42
41
  export class Sub extends ThreeOperandArithmeticInstruction {
43
42
  static readonly type: string = 'SUB';
44
- static readonly opcode = Opcode.SUB;
43
+ static readonly opcode = Opcode.SUB_8; // FIXME: needed for gas.
45
44
 
46
45
  protected compute(a: MemoryValue, b: MemoryValue): MemoryValue {
47
46
  return a.sub(b);
@@ -50,7 +49,7 @@ export class Sub extends ThreeOperandArithmeticInstruction {
50
49
 
51
50
  export class Mul extends ThreeOperandArithmeticInstruction {
52
51
  static type: string = 'MUL';
53
- static readonly opcode = Opcode.MUL;
52
+ static readonly opcode = Opcode.MUL_8; // FIXME: needed for gas.
54
53
 
55
54
  protected compute(a: MemoryValue, b: MemoryValue): MemoryValue {
56
55
  return a.mul(b);
@@ -59,48 +58,20 @@ export class Mul extends ThreeOperandArithmeticInstruction {
59
58
 
60
59
  export class Div extends ThreeOperandArithmeticInstruction {
61
60
  static type: string = 'DIV';
62
- static readonly opcode = Opcode.DIV;
61
+ static readonly opcode = Opcode.DIV_8; // FIXME: needed for gas.
63
62
 
64
63
  protected compute(a: MemoryValue, b: MemoryValue): MemoryValue {
65
64
  return a.div(b);
66
65
  }
67
66
  }
68
67
 
69
- export class FieldDiv extends Instruction {
68
+ // TODO: This class now temporarily has a tag, until all tags are removed.
69
+ export class FieldDiv extends ThreeOperandArithmeticInstruction {
70
70
  static type: string = 'FDIV';
71
- static readonly opcode = Opcode.FDIV;
72
-
73
- // Informs (de)serialization. See Instruction.deserialize.
74
- static readonly wireFormat: OperandType[] = [
75
- OperandType.UINT8,
76
- OperandType.UINT8,
77
- OperandType.UINT32,
78
- OperandType.UINT32,
79
- OperandType.UINT32,
80
- ];
81
-
82
- constructor(private indirect: number, private aOffset: number, private bOffset: number, private dstOffset: number) {
83
- super();
84
- }
85
-
86
- public async execute(context: AvmContext): Promise<void> {
87
- const memoryOperations = { reads: 2, writes: 1, indirect: this.indirect };
88
- const memory = context.machineState.memory.track(this.type);
89
- context.machineState.consumeGas(this.gasCost(memoryOperations));
90
-
91
- const [aOffset, bOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve(
92
- [this.aOffset, this.bOffset, this.dstOffset],
93
- memory,
94
- );
95
- memory.checkTags(TypeTag.FIELD, aOffset, bOffset);
96
-
97
- const a = memory.getAs<Field>(aOffset);
98
- const b = memory.getAs<Field>(bOffset);
71
+ static readonly opcode = Opcode.FDIV_8; // FIXME: needed for gas.
99
72
 
100
- const dest = a.fdiv(b);
101
- memory.set(dstOffset, dest);
102
-
103
- memory.assert(memoryOperations);
104
- context.machineState.incrementPc();
73
+ protected compute(a: Field, b: Field): Field {
74
+ // return (a as Field).fdiv(b as Field);
75
+ return a.fdiv(b);
105
76
  }
106
77
  }
@@ -34,7 +34,7 @@ abstract class ThreeOperandBitwiseInstruction extends ThreeOperandInstruction {
34
34
 
35
35
  export class And extends ThreeOperandBitwiseInstruction {
36
36
  static readonly type: string = 'AND';
37
- static readonly opcode = Opcode.AND;
37
+ static readonly opcode = Opcode.AND_8; // FIXME: needed for gas.
38
38
 
39
39
  protected override compute(a: IntegralValue, b: IntegralValue): IntegralValue {
40
40
  return a.and(b);
@@ -43,7 +43,7 @@ export class And extends ThreeOperandBitwiseInstruction {
43
43
 
44
44
  export class Or extends ThreeOperandBitwiseInstruction {
45
45
  static readonly type: string = 'OR';
46
- static readonly opcode = Opcode.OR;
46
+ static readonly opcode = Opcode.OR_8; // FIXME: needed for gas.
47
47
 
48
48
  protected override compute(a: IntegralValue, b: IntegralValue): IntegralValue {
49
49
  return a.or(b);
@@ -52,7 +52,7 @@ export class Or extends ThreeOperandBitwiseInstruction {
52
52
 
53
53
  export class Xor extends ThreeOperandBitwiseInstruction {
54
54
  static readonly type: string = 'XOR';
55
- static readonly opcode = Opcode.XOR;
55
+ static readonly opcode = Opcode.XOR_8; // FIXME: needed for gas.
56
56
 
57
57
  protected override compute(a: IntegralValue, b: IntegralValue): IntegralValue {
58
58
  return a.xor(b);
@@ -61,7 +61,7 @@ export class Xor extends ThreeOperandBitwiseInstruction {
61
61
 
62
62
  export class Shl extends ThreeOperandBitwiseInstruction {
63
63
  static readonly type: string = 'SHL';
64
- static readonly opcode = Opcode.SHL;
64
+ static readonly opcode = Opcode.SHL_8; // FIXME: needed for gas.
65
65
 
66
66
  protected override compute(a: IntegralValue, b: IntegralValue): IntegralValue {
67
67
  return a.shl(b);
@@ -74,7 +74,7 @@ export class Shl extends ThreeOperandBitwiseInstruction {
74
74
 
75
75
  export class Shr extends ThreeOperandBitwiseInstruction {
76
76
  static readonly type: string = 'SHR';
77
- static readonly opcode = Opcode.SHR;
77
+ static readonly opcode = Opcode.SHR_8; // FIXME: needed for gas.
78
78
 
79
79
  protected override compute(a: IntegralValue, b: IntegralValue): IntegralValue {
80
80
  return a.shr(b);
@@ -31,7 +31,7 @@ abstract class ComparatorInstruction extends ThreeOperandInstruction {
31
31
 
32
32
  export class Eq extends ComparatorInstruction {
33
33
  static readonly type: string = 'EQ';
34
- static readonly opcode = Opcode.EQ;
34
+ static readonly opcode = Opcode.EQ_8; // FIXME: needed for gas.
35
35
 
36
36
  protected compare(a: MemoryValue, b: MemoryValue): boolean {
37
37
  return a.equals(b);
@@ -40,7 +40,7 @@ export class Eq extends ComparatorInstruction {
40
40
 
41
41
  export class Lt extends ComparatorInstruction {
42
42
  static readonly type: string = 'LT';
43
- static readonly opcode = Opcode.LT;
43
+ static readonly opcode = Opcode.LT_8; // FIXME: needed for gas.
44
44
 
45
45
  protected compare(a: MemoryValue, b: MemoryValue): boolean {
46
46
  return a.lt(b);
@@ -49,7 +49,7 @@ export class Lt extends ComparatorInstruction {
49
49
 
50
50
  export class Lte extends ComparatorInstruction {
51
51
  static readonly type: string = 'LTE';
52
- static readonly opcode = Opcode.LTE;
52
+ static readonly opcode = Opcode.LTE_8; // FIXME: needed for gas.
53
53
 
54
54
  protected compare(a: MemoryValue, b: MemoryValue): boolean {
55
55
  return a.lt(b) || a.equals(b);
@@ -7,9 +7,9 @@ import { Instruction } from './instruction.js';
7
7
 
8
8
  export class Jump extends Instruction {
9
9
  static type: string = 'JUMP';
10
- static readonly opcode: Opcode = Opcode.JUMP;
10
+ static readonly opcode: Opcode = Opcode.JUMP_16;
11
11
  // Informs (de)serialization. See Instruction.deserialize.
12
- static readonly wireFormat: OperandType[] = [OperandType.UINT8, OperandType.UINT32];
12
+ static readonly wireFormat: OperandType[] = [OperandType.UINT8, OperandType.UINT16];
13
13
 
14
14
  constructor(private jumpOffset: number) {
15
15
  super();
@@ -26,14 +26,14 @@ export class Jump extends Instruction {
26
26
 
27
27
  export class JumpI extends Instruction {
28
28
  static type: string = 'JUMPI';
29
- static readonly opcode: Opcode = Opcode.JUMPI;
29
+ static readonly opcode: Opcode = Opcode.JUMPI_16;
30
30
 
31
31
  // Instruction wire format with opcode.
32
32
  static readonly wireFormat: OperandType[] = [
33
33
  OperandType.UINT8,
34
34
  OperandType.UINT8,
35
- OperandType.UINT32,
36
- OperandType.UINT32,
35
+ OperandType.UINT16,
36
+ OperandType.UINT16,
37
37
  ];
38
38
 
39
39
  constructor(private indirect: number, private loc: number, private condOffset: number) {
@@ -4,11 +4,11 @@ import type { AvmContext } from '../avm_context.js';
4
4
  import { getBaseGasCost, getDynamicGasCost, mulGas, sumGas } from '../avm_gas.js';
5
5
  import { type MemoryOperations } from '../avm_memory_types.js';
6
6
  import { type BufferCursor } from '../serialization/buffer_cursor.js';
7
- import { Opcode, type OperandType, deserialize, serialize } from '../serialization/instruction_serialization.js';
7
+ import { type Serializable } from '../serialization/bytecode_serialization.js';
8
+ import { Opcode, type OperandType, deserialize, serializeAs } from '../serialization/instruction_serialization.js';
8
9
 
9
10
  type InstructionConstructor = {
10
11
  new (...args: any[]): Instruction;
11
- wireFormat?: OperandType[];
12
12
  };
13
13
 
14
14
  /**
@@ -37,29 +37,51 @@ export abstract class Instruction {
37
37
  return instructionStr;
38
38
  }
39
39
 
40
+ // Default deserialization which uses Class.opcode and Class.wireFormat.
41
+ public static deserialize(
42
+ this: InstructionConstructor & { wireFormat: OperandType[]; as: any },
43
+ buf: BufferCursor | Buffer,
44
+ ): Instruction {
45
+ return this.as(this.wireFormat).deserialize(buf);
46
+ }
47
+
48
+ // Default serialization which uses Class.opcode and Class.wireFormat.
49
+ public serialize(): Buffer {
50
+ const klass = this.constructor as any;
51
+ assert(klass.opcode !== undefined && klass.opcode !== null);
52
+ assert(klass.wireFormat !== undefined && klass.wireFormat !== null);
53
+ return this.as(klass.opcode, klass.wireFormat).serialize();
54
+ }
55
+
40
56
  /**
41
- * Serialize the instruction to a Buffer according to its wire format specified in its subclass.
42
- * If you want to use this, your subclass should specify a {@code static wireFormat: OperandType[]}.
43
- * @param this - The instruction to serialize.
44
- * @returns The serialized instruction.
57
+ * Returns a new instruction instance that can be serialized with the given opcode and wire format.
58
+ * @param opcode The opcode of the instruction.
59
+ * @param wireFormat The wire format of the instruction.
60
+ * @returns The new instruction instance.
45
61
  */
46
- public serialize(this: any): Buffer {
47
- assert(!!this.constructor.wireFormat, 'wireFormat must be defined on the class');
48
- return serialize(this.constructor.wireFormat, this);
62
+ public as(opcode: Opcode, wireFormat: OperandType[]): Instruction & Serializable {
63
+ return Object.defineProperty(this, 'serialize', {
64
+ value: (): Buffer => {
65
+ return serializeAs(wireFormat, opcode, this);
66
+ },
67
+ enumerable: false,
68
+ });
49
69
  }
50
70
 
51
71
  /**
52
- * Deserializes a subclass of Instruction from a Buffer.
53
- * If you want to use this, your subclass should specify a {@code static wireFormat: OperandType[]}.
54
- * @param this Class object to deserialize to.
55
- * @param buf Buffer to read from.
56
- * @returns Constructed instance of Class.
72
+ * Returns a new instruction class that can be deserialized with the given opcode and wire format.
73
+ * @param opcode The opcode of the instruction.
74
+ * @param wireFormat The wire format of the instruction.
75
+ * @returns The new instruction class.
57
76
  */
58
- public static deserialize(this: InstructionConstructor, buf: BufferCursor | Buffer): Instruction {
59
- assert(!!this.wireFormat, 'wireFormat must be defined on the instruction class');
60
- const res = deserialize(buf, this.wireFormat);
61
- const args = res.slice(1); // Remove opcode.
62
- return new this(...args);
77
+ public static as(this: InstructionConstructor, wireFormat: OperandType[]) {
78
+ return Object.assign(this, {
79
+ deserialize: (buf: BufferCursor | Buffer): Instruction => {
80
+ const res = deserialize(buf, wireFormat);
81
+ const args = res.slice(1); // Remove opcode.
82
+ return new this(...args);
83
+ },
84
+ });
63
85
  }
64
86
 
65
87
  /**
@@ -14,13 +14,21 @@ export const TwoOperandWireFormat = [
14
14
  ];
15
15
 
16
16
  /** Wire format that informs deserialization for instructions with three operands. */
17
- export const ThreeOperandWireFormat = [
17
+ export const ThreeOperandWireFormat8 = [
18
+ OperandType.UINT8,
19
+ OperandType.UINT8,
20
+ OperandType.UINT8,
18
21
  OperandType.UINT8,
19
22
  OperandType.UINT8,
20
23
  OperandType.UINT8,
21
- OperandType.UINT32,
22
- OperandType.UINT32,
23
- OperandType.UINT32,
24
+ ];
25
+ export const ThreeOperandWireFormat16 = [
26
+ OperandType.UINT8,
27
+ OperandType.UINT8,
28
+ OperandType.UINT8,
29
+ OperandType.UINT16,
30
+ OperandType.UINT16,
31
+ OperandType.UINT16,
24
32
  ];
25
33
 
26
34
  /**
@@ -46,8 +54,8 @@ export abstract class TwoOperandInstruction extends Instruction {
46
54
  * indirect, inTag, and three UINT32s.
47
55
  */
48
56
  export abstract class ThreeOperandInstruction extends Instruction {
49
- // Informs (de)serialization. See Instruction.deserialize.
50
- static readonly wireFormat: OperandType[] = ThreeOperandWireFormat;
57
+ static readonly wireFormat8: OperandType[] = ThreeOperandWireFormat8;
58
+ static readonly wireFormat16: OperandType[] = ThreeOperandWireFormat16;
51
59
 
52
60
  constructor(
53
61
  protected indirect: number,