@aztec/simulator 0.32.1 → 0.33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/README.md +5 -3
  2. package/dest/acvm/oracle/index.d.ts +0 -1
  3. package/dest/acvm/oracle/index.d.ts.map +1 -1
  4. package/dest/acvm/oracle/index.js +1 -2
  5. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  6. package/dest/acvm/oracle/oracle.js +2 -3
  7. package/dest/avm/avm_context.d.ts +4 -14
  8. package/dest/avm/avm_context.d.ts.map +1 -1
  9. package/dest/avm/avm_context.js +10 -22
  10. package/dest/avm/avm_gas.d.ts +71 -0
  11. package/dest/avm/avm_gas.d.ts.map +1 -0
  12. package/dest/avm/avm_gas.js +161 -0
  13. package/dest/avm/avm_machine_state.d.ts +4 -2
  14. package/dest/avm/avm_machine_state.d.ts.map +1 -1
  15. package/dest/avm/avm_machine_state.js +8 -2
  16. package/dest/avm/avm_memory_types.d.ts +53 -1
  17. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  18. package/dest/avm/avm_memory_types.js +95 -2
  19. package/dest/avm/avm_simulator.d.ts.map +1 -1
  20. package/dest/avm/avm_simulator.js +10 -8
  21. package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
  22. package/dest/avm/opcodes/accrued_substate.js +44 -16
  23. package/dest/avm/opcodes/addressing_mode.d.ts +5 -3
  24. package/dest/avm/opcodes/addressing_mode.d.ts.map +1 -1
  25. package/dest/avm/opcodes/addressing_mode.js +5 -1
  26. package/dest/avm/opcodes/arithmetic.d.ts +7 -3
  27. package/dest/avm/opcodes/arithmetic.d.ts.map +1 -1
  28. package/dest/avm/opcodes/arithmetic.js +27 -16
  29. package/dest/avm/opcodes/bitwise.d.ts +21 -20
  30. package/dest/avm/opcodes/bitwise.d.ts.map +1 -1
  31. package/dest/avm/opcodes/bitwise.js +43 -65
  32. package/dest/avm/opcodes/comparators.d.ts +12 -9
  33. package/dest/avm/opcodes/comparators.d.ts.map +1 -1
  34. package/dest/avm/opcodes/comparators.js +22 -32
  35. package/dest/avm/opcodes/context_getters.d.ts +20 -0
  36. package/dest/avm/opcodes/context_getters.d.ts.map +1 -0
  37. package/dest/avm/opcodes/context_getters.js +26 -0
  38. package/dest/avm/opcodes/contract.d.ts +14 -0
  39. package/dest/avm/opcodes/contract.d.ts.map +1 -0
  40. package/dest/avm/opcodes/contract.js +49 -0
  41. package/dest/avm/opcodes/control_flow.d.ts.map +1 -1
  42. package/dest/avm/opcodes/control_flow.js +12 -2
  43. package/dest/avm/opcodes/environment_getters.d.ts +30 -33
  44. package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
  45. package/dest/avm/opcodes/environment_getters.js +34 -43
  46. package/dest/avm/opcodes/external_calls.d.ts +13 -19
  47. package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
  48. package/dest/avm/opcodes/external_calls.js +50 -68
  49. package/dest/avm/opcodes/hashing.d.ts +2 -1
  50. package/dest/avm/opcodes/hashing.d.ts.map +1 -1
  51. package/dest/avm/opcodes/hashing.js +37 -18
  52. package/dest/avm/opcodes/index.d.ts +1 -0
  53. package/dest/avm/opcodes/index.d.ts.map +1 -1
  54. package/dest/avm/opcodes/index.js +2 -1
  55. package/dest/avm/opcodes/instruction.d.ts +10 -15
  56. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  57. package/dest/avm/opcodes/instruction.js +12 -22
  58. package/dest/avm/opcodes/instruction_impl.d.ts +14 -0
  59. package/dest/avm/opcodes/instruction_impl.d.ts.map +1 -1
  60. package/dest/avm/opcodes/instruction_impl.js +37 -16
  61. package/dest/avm/opcodes/memory.d.ts +4 -3
  62. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  63. package/dest/avm/opcodes/memory.js +38 -19
  64. package/dest/avm/opcodes/storage.d.ts +5 -0
  65. package/dest/avm/opcodes/storage.d.ts.map +1 -1
  66. package/dest/avm/opcodes/storage.js +21 -7
  67. package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  68. package/dest/avm/serialization/bytecode_serialization.js +7 -5
  69. package/dest/avm/serialization/instruction_serialization.d.ts +12 -11
  70. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  71. package/dest/avm/serialization/instruction_serialization.js +13 -12
  72. package/dest/avm/temporary_executor_migration.d.ts +2 -0
  73. package/dest/avm/temporary_executor_migration.d.ts.map +1 -1
  74. package/dest/avm/temporary_executor_migration.js +14 -3
  75. package/dest/client/private_execution.d.ts.map +1 -1
  76. package/dest/client/private_execution.js +6 -2
  77. package/dest/public/executor.d.ts +10 -2
  78. package/dest/public/executor.d.ts.map +1 -1
  79. package/dest/public/executor.js +59 -20
  80. package/dest/public/index.d.ts +1 -1
  81. package/dest/public/index.d.ts.map +1 -1
  82. package/package.json +15 -9
  83. package/src/acvm/oracle/index.ts +0 -1
  84. package/src/acvm/oracle/oracle.ts +1 -2
  85. package/src/avm/avm_context.ts +11 -33
  86. package/src/avm/{avm_gas_cost.ts → avm_gas.ts} +75 -21
  87. package/src/avm/avm_machine_state.ts +9 -2
  88. package/src/avm/avm_memory_types.ts +130 -2
  89. package/src/avm/avm_simulator.ts +9 -7
  90. package/src/avm/opcodes/accrued_substate.ts +57 -22
  91. package/src/avm/opcodes/addressing_mode.ts +8 -3
  92. package/src/avm/opcodes/arithmetic.ts +32 -22
  93. package/src/avm/opcodes/bitwise.ts +49 -83
  94. package/src/avm/opcodes/comparators.ts +28 -43
  95. package/src/avm/opcodes/context_getters.ts +32 -0
  96. package/src/avm/opcodes/contract.ts +58 -0
  97. package/src/avm/opcodes/control_flow.ts +23 -5
  98. package/src/avm/opcodes/environment_getters.ts +35 -44
  99. package/src/avm/opcodes/external_calls.ts +65 -84
  100. package/src/avm/opcodes/hashing.ts +45 -22
  101. package/src/avm/opcodes/index.ts +1 -0
  102. package/src/avm/opcodes/instruction.ts +14 -26
  103. package/src/avm/opcodes/instruction_impl.ts +45 -15
  104. package/src/avm/opcodes/memory.ts +48 -28
  105. package/src/avm/opcodes/storage.ts +26 -12
  106. package/src/avm/serialization/bytecode_serialization.ts +6 -3
  107. package/src/avm/serialization/instruction_serialization.ts +1 -0
  108. package/src/avm/temporary_executor_migration.ts +16 -2
  109. package/src/client/private_execution.ts +8 -2
  110. package/src/public/executor.ts +75 -22
  111. package/src/public/index.ts +2 -2
  112. package/dest/acvm/oracle/debug.d.ts +0 -19
  113. package/dest/acvm/oracle/debug.d.ts.map +0 -1
  114. package/dest/acvm/oracle/debug.js +0 -95
  115. package/dest/avm/avm_gas_cost.d.ts +0 -322
  116. package/dest/avm/avm_gas_cost.d.ts.map +0 -1
  117. package/dest/avm/avm_gas_cost.js +0 -118
  118. package/src/acvm/oracle/debug.ts +0 -109
@@ -1,33 +1,32 @@
1
1
  import type { AvmContext } from '../avm_context.js';
2
- import { type GasCost, GasCostConstants, getGasCostMultiplierFromTypeTag, makeGasCost } from '../avm_gas_cost.js';
3
- import { type Field, type MemoryValue, TypeTag } from '../avm_memory_types.js';
2
+ import { getBaseGasCost, getGasCostForTypeTag, getMemoryGasCost, sumGas } from '../avm_gas.js';
3
+ import { type Field, type MemoryOperations, type MemoryValue, TypeTag } from '../avm_memory_types.js';
4
4
  import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
5
- import { Addressing, AddressingMode } from './addressing_mode.js';
6
5
  import { Instruction } from './instruction.js';
7
6
  import { ThreeOperandInstruction } from './instruction_impl.js';
8
7
 
9
8
  export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInstruction {
10
- async execute(context: AvmContext): Promise<void> {
11
- context.machineState.memory.checkTags(this.inTag, this.aOffset, this.bOffset);
9
+ public async execute(context: AvmContext): Promise<void> {
10
+ const memoryOperations = { reads: 2, writes: 1, indirect: this.indirect };
11
+ const memory = context.machineState.memory.track(this.type);
12
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
12
13
 
13
- const a = context.machineState.memory.get(this.aOffset);
14
- const b = context.machineState.memory.get(this.bOffset);
14
+ memory.checkTags(this.inTag, this.aOffset, this.bOffset);
15
+
16
+ const a = memory.get(this.aOffset);
17
+ const b = memory.get(this.bOffset);
15
18
 
16
19
  const dest = this.compute(a, b);
17
- context.machineState.memory.set(this.dstOffset, dest);
20
+ memory.set(this.dstOffset, dest);
18
21
 
22
+ memory.assert(memoryOperations);
19
23
  context.machineState.incrementPc();
20
24
  }
21
25
 
22
- protected gasCost(): GasCost {
23
- const indirectCount = Addressing.fromWire(this.indirect).modePerOperand.filter(
24
- mode => mode === AddressingMode.INDIRECT,
25
- ).length;
26
-
27
- const l2Gas =
28
- indirectCount * GasCostConstants.ARITHMETIC_COST_PER_INDIRECT_ACCESS +
29
- getGasCostMultiplierFromTypeTag(this.inTag) * GasCostConstants.ARITHMETIC_COST_PER_BYTE;
30
- return makeGasCost({ l2Gas });
26
+ protected gasCost(memoryOps: Partial<MemoryOperations & { indirect: number }>) {
27
+ const baseGasCost = getGasCostForTypeTag(this.inTag, getBaseGasCost(this.opcode));
28
+ const memoryGasCost = getMemoryGasCost(memoryOps);
29
+ return sumGas(baseGasCost, memoryGasCost);
31
30
  }
32
31
 
33
32
  protected abstract compute(a: MemoryValue, b: MemoryValue): MemoryValue;
@@ -86,15 +85,26 @@ export class FieldDiv extends Instruction {
86
85
  super();
87
86
  }
88
87
 
89
- async execute(context: AvmContext): Promise<void> {
90
- context.machineState.memory.checkTags(TypeTag.FIELD, this.aOffset, this.bOffset);
88
+ public async execute(context: AvmContext): Promise<void> {
89
+ const memoryOperations = { reads: 2, writes: 1, indirect: this.indirect };
90
+ const memory = context.machineState.memory.track(this.type);
91
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
92
+
93
+ memory.checkTags(TypeTag.FIELD, this.aOffset, this.bOffset);
91
94
 
92
- const a = context.machineState.memory.getAs<Field>(this.aOffset);
93
- const b = context.machineState.memory.getAs<Field>(this.bOffset);
95
+ const a = memory.getAs<Field>(this.aOffset);
96
+ const b = memory.getAs<Field>(this.bOffset);
94
97
 
95
98
  const dest = a.fdiv(b);
96
- context.machineState.memory.set(this.dstOffset, dest);
99
+ memory.set(this.dstOffset, dest);
97
100
 
101
+ memory.assert(memoryOperations);
98
102
  context.machineState.incrementPc();
99
103
  }
104
+
105
+ protected gasCost(memoryOps: Partial<MemoryOperations & { indirect: number }>) {
106
+ const baseGasCost = getGasCostForTypeTag(TypeTag.FIELD, getBaseGasCost(this.opcode));
107
+ const memoryGasCost = getMemoryGasCost(memoryOps);
108
+ return sumGas(baseGasCost, memoryGasCost);
109
+ }
100
110
  }
@@ -3,66 +3,69 @@ import { type IntegralValue } from '../avm_memory_types.js';
3
3
  import { Opcode } from '../serialization/instruction_serialization.js';
4
4
  import { ThreeOperandInstruction, TwoOperandInstruction } from './instruction_impl.js';
5
5
 
6
- export class And extends ThreeOperandInstruction {
7
- static readonly type: string = 'AND';
8
- static readonly opcode = Opcode.AND;
9
-
10
- constructor(indirect: number, inTag: number, aOffset: number, bOffset: number, dstOffset: number) {
11
- super(indirect, inTag, aOffset, bOffset, dstOffset);
12
- }
6
+ abstract class ThreeOperandBitwiseInstruction extends ThreeOperandInstruction {
7
+ public async execute(context: AvmContext): Promise<void> {
8
+ const memoryOperations = { reads: 2, writes: 1, indirect: this.indirect };
9
+ const memory = context.machineState.memory.track(this.type);
10
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
13
11
 
14
- async execute(context: AvmContext): Promise<void> {
15
- context.machineState.memory.checkTags(this.inTag, this.aOffset, this.bOffset);
12
+ memory.checkTags(this.inTag, this.aOffset, this.bOffset);
16
13
 
17
- const a = context.machineState.memory.getAs<IntegralValue>(this.aOffset);
18
- const b = context.machineState.memory.getAs<IntegralValue>(this.bOffset);
14
+ const a = memory.getAs<IntegralValue>(this.aOffset);
15
+ const b = memory.getAs<IntegralValue>(this.bOffset);
19
16
 
20
- const res = a.and(b);
21
- context.machineState.memory.set(this.dstOffset, res);
17
+ const res = this.compute(a, b);
18
+ memory.set(this.dstOffset, res);
22
19
 
20
+ memory.assert(memoryOperations);
23
21
  context.machineState.incrementPc();
24
22
  }
23
+
24
+ protected abstract compute(a: IntegralValue, b: IntegralValue): IntegralValue;
25
25
  }
26
26
 
27
- export class Or extends ThreeOperandInstruction {
28
- static readonly type: string = 'OR';
29
- static readonly opcode = Opcode.OR;
27
+ export class And extends ThreeOperandBitwiseInstruction {
28
+ static readonly type: string = 'AND';
29
+ static readonly opcode = Opcode.AND;
30
30
 
31
- constructor(indirect: number, inTag: number, aOffset: number, bOffset: number, dstOffset: number) {
32
- super(indirect, inTag, aOffset, bOffset, dstOffset);
31
+ protected compute(a: IntegralValue, b: IntegralValue): IntegralValue {
32
+ return a.and(b);
33
33
  }
34
+ }
34
35
 
35
- async execute(context: AvmContext): Promise<void> {
36
- context.machineState.memory.checkTags(this.inTag, this.aOffset, this.bOffset);
37
-
38
- const a = context.machineState.memory.getAs<IntegralValue>(this.aOffset);
39
- const b = context.machineState.memory.getAs<IntegralValue>(this.bOffset);
40
-
41
- const res = a.or(b);
42
- context.machineState.memory.set(this.dstOffset, res);
36
+ export class Or extends ThreeOperandBitwiseInstruction {
37
+ static readonly type: string = 'OR';
38
+ static readonly opcode = Opcode.OR;
43
39
 
44
- context.machineState.incrementPc();
40
+ protected compute(a: IntegralValue, b: IntegralValue): IntegralValue {
41
+ return a.or(b);
45
42
  }
46
43
  }
47
44
 
48
- export class Xor extends ThreeOperandInstruction {
45
+ export class Xor extends ThreeOperandBitwiseInstruction {
49
46
  static readonly type: string = 'XOR';
50
47
  static readonly opcode = Opcode.XOR;
51
48
 
52
- constructor(indirect: number, inTag: number, aOffset: number, bOffset: number, dstOffset: number) {
53
- super(indirect, inTag, aOffset, bOffset, dstOffset);
49
+ protected compute(a: IntegralValue, b: IntegralValue): IntegralValue {
50
+ return a.xor(b);
54
51
  }
52
+ }
55
53
 
56
- async execute(context: AvmContext): Promise<void> {
57
- context.machineState.memory.checkTags(this.inTag, this.aOffset, this.bOffset);
54
+ export class Shl extends ThreeOperandBitwiseInstruction {
55
+ static readonly type: string = 'SHL';
56
+ static readonly opcode = Opcode.SHL;
58
57
 
59
- const a = context.machineState.memory.getAs<IntegralValue>(this.aOffset);
60
- const b = context.machineState.memory.getAs<IntegralValue>(this.bOffset);
58
+ protected compute(a: IntegralValue, b: IntegralValue): IntegralValue {
59
+ return a.shl(b);
60
+ }
61
+ }
61
62
 
62
- const res = a.xor(b);
63
- context.machineState.memory.set(this.dstOffset, res);
63
+ export class Shr extends ThreeOperandBitwiseInstruction {
64
+ static readonly type: string = 'SHR';
65
+ static readonly opcode = Opcode.SHR;
64
66
 
65
- context.machineState.incrementPc();
67
+ protected compute(a: IntegralValue, b: IntegralValue): IntegralValue {
68
+ return a.shr(b);
66
69
  }
67
70
  }
68
71
 
@@ -74,56 +77,19 @@ export class Not extends TwoOperandInstruction {
74
77
  super(indirect, inTag, aOffset, dstOffset);
75
78
  }
76
79
 
77
- async execute(context: AvmContext): Promise<void> {
78
- context.machineState.memory.checkTags(this.inTag, this.aOffset);
79
-
80
- const a = context.machineState.memory.getAs<IntegralValue>(this.aOffset);
81
-
82
- const res = a.not();
83
- context.machineState.memory.set(this.dstOffset, res);
84
-
85
- context.machineState.incrementPc();
86
- }
87
- }
88
-
89
- export class Shl extends ThreeOperandInstruction {
90
- static readonly type: string = 'SHL';
91
- static readonly opcode = Opcode.SHL;
92
-
93
- constructor(indirect: number, inTag: number, aOffset: number, bOffset: number, dstOffset: number) {
94
- super(indirect, inTag, aOffset, bOffset, dstOffset);
95
- }
96
-
97
- async execute(context: AvmContext): Promise<void> {
98
- context.machineState.memory.checkTags(this.inTag, this.aOffset, this.bOffset);
80
+ public async execute(context: AvmContext): Promise<void> {
81
+ const memoryOperations = { reads: 1, writes: 1, indirect: this.indirect };
82
+ const memory = context.machineState.memory.track(this.type);
83
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
99
84
 
100
- const a = context.machineState.memory.getAs<IntegralValue>(this.aOffset);
101
- const b = context.machineState.memory.getAs<IntegralValue>(this.bOffset);
85
+ memory.checkTags(this.inTag, this.aOffset);
102
86
 
103
- const res = a.shl(b);
104
- context.machineState.memory.set(this.dstOffset, res);
87
+ const a = memory.getAs<IntegralValue>(this.aOffset);
105
88
 
106
- context.machineState.incrementPc();
107
- }
108
- }
109
-
110
- export class Shr extends ThreeOperandInstruction {
111
- static readonly type: string = 'SHR';
112
- static readonly opcode = Opcode.SHR;
113
-
114
- constructor(indirect: number, inTag: number, aOffset: number, bOffset: number, dstOffset: number) {
115
- super(indirect, inTag, aOffset, bOffset, dstOffset);
116
- }
117
-
118
- async execute(context: AvmContext): Promise<void> {
119
- context.machineState.memory.checkTags(this.inTag, this.aOffset, this.bOffset);
120
-
121
- const a = context.machineState.memory.getAs<IntegralValue>(this.aOffset);
122
- const b = context.machineState.memory.getAs<IntegralValue>(this.bOffset);
123
-
124
- const res = a.shr(b);
125
- context.machineState.memory.set(this.dstOffset, res);
89
+ const res = a.not();
90
+ memory.set(this.dstOffset, res);
126
91
 
92
+ memory.assert(memoryOperations);
127
93
  context.machineState.incrementPc();
128
94
  }
129
95
  }
@@ -1,67 +1,52 @@
1
1
  import type { AvmContext } from '../avm_context.js';
2
- import { Uint8 } from '../avm_memory_types.js';
2
+ import { type MemoryValue, Uint8 } from '../avm_memory_types.js';
3
3
  import { Opcode } from '../serialization/instruction_serialization.js';
4
4
  import { ThreeOperandInstruction } from './instruction_impl.js';
5
5
 
6
- export class Eq extends ThreeOperandInstruction {
7
- static readonly type: string = 'EQ';
8
- static readonly opcode = Opcode.EQ;
9
-
10
- constructor(indirect: number, inTag: number, aOffset: number, bOffset: number, dstOffset: number) {
11
- super(indirect, inTag, aOffset, bOffset, dstOffset);
12
- }
6
+ abstract class ComparatorInstruction extends ThreeOperandInstruction {
7
+ public async execute(context: AvmContext): Promise<void> {
8
+ const memoryOperations = { reads: 2, writes: 1, indirect: this.indirect };
9
+ const memory = context.machineState.memory.track(this.type);
10
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
13
11
 
14
- async execute(context: AvmContext): Promise<void> {
15
- context.machineState.memory.checkTags(this.inTag, this.aOffset, this.bOffset);
12
+ memory.checkTags(this.inTag, this.aOffset, this.bOffset);
16
13
 
17
- const a = context.machineState.memory.get(this.aOffset);
18
- const b = context.machineState.memory.get(this.bOffset);
14
+ const a = memory.get(this.aOffset);
15
+ const b = memory.get(this.bOffset);
19
16
 
20
- const dest = new Uint8(a.equals(b) ? 1 : 0);
21
- context.machineState.memory.set(this.dstOffset, dest);
17
+ const dest = new Uint8(this.compare(a, b) ? 1 : 0);
18
+ memory.set(this.dstOffset, dest);
22
19
 
20
+ memory.assert(memoryOperations);
23
21
  context.machineState.incrementPc();
24
22
  }
23
+
24
+ protected abstract compare(a: MemoryValue, b: MemoryValue): boolean;
25
25
  }
26
26
 
27
- export class Lt extends ThreeOperandInstruction {
28
- static readonly type: string = 'LT';
29
- static readonly opcode = Opcode.LT;
27
+ export class Eq extends ComparatorInstruction {
28
+ static readonly type: string = 'EQ';
29
+ static readonly opcode = Opcode.EQ;
30
30
 
31
- constructor(indirect: number, inTag: number, aOffset: number, bOffset: number, dstOffset: number) {
32
- super(indirect, inTag, aOffset, bOffset, dstOffset);
31
+ protected compare(a: MemoryValue, b: MemoryValue): boolean {
32
+ return a.equals(b);
33
33
  }
34
+ }
34
35
 
35
- async execute(context: AvmContext): Promise<void> {
36
- context.machineState.memory.checkTags(this.inTag, this.aOffset, this.bOffset);
37
-
38
- const a = context.machineState.memory.get(this.aOffset);
39
- const b = context.machineState.memory.get(this.bOffset);
40
-
41
- const dest = new Uint8(a.lt(b) ? 1 : 0);
42
- context.machineState.memory.set(this.dstOffset, dest);
36
+ export class Lt extends ComparatorInstruction {
37
+ static readonly type: string = 'LT';
38
+ static readonly opcode = Opcode.LT;
43
39
 
44
- context.machineState.incrementPc();
40
+ protected compare(a: MemoryValue, b: MemoryValue): boolean {
41
+ return a.lt(b);
45
42
  }
46
43
  }
47
44
 
48
- export class Lte extends ThreeOperandInstruction {
45
+ export class Lte extends ComparatorInstruction {
49
46
  static readonly type: string = 'LTE';
50
47
  static readonly opcode = Opcode.LTE;
51
48
 
52
- constructor(indirect: number, inTag: number, aOffset: number, bOffset: number, dstOffset: number) {
53
- super(indirect, inTag, aOffset, bOffset, dstOffset);
54
- }
55
-
56
- async execute(context: AvmContext): Promise<void> {
57
- context.machineState.memory.checkTags(this.inTag, this.aOffset, this.bOffset);
58
-
59
- const a = context.machineState.memory.get(this.aOffset);
60
- const b = context.machineState.memory.get(this.bOffset);
61
-
62
- const dest = new Uint8(a.lt(b) || a.equals(b) ? 1 : 0);
63
- context.machineState.memory.set(this.dstOffset, dest);
64
-
65
- context.machineState.incrementPc();
49
+ protected compare(a: MemoryValue, b: MemoryValue): boolean {
50
+ return a.lt(b) || a.equals(b);
66
51
  }
67
52
  }
@@ -0,0 +1,32 @@
1
+ import type { AvmContext } from '../avm_context.js';
2
+ import { Field, type MemoryValue } from '../avm_memory_types.js';
3
+ import { Opcode } from '../serialization/instruction_serialization.js';
4
+ import { GetterInstruction } from './instruction_impl.js';
5
+
6
+ export class L2GasLeft extends GetterInstruction {
7
+ static type: string = 'L2GASLEFT';
8
+ static readonly opcode: Opcode = Opcode.L2GASLEFT;
9
+
10
+ // TODO(@spalladino) Yellow paper specifies that the value should be an Uint32, not a Field.
11
+ protected getValue(context: AvmContext): MemoryValue {
12
+ return new Field(context.machineState.l2GasLeft);
13
+ }
14
+ }
15
+
16
+ export class L1GasLeft extends GetterInstruction {
17
+ static type: string = 'L1GASLEFT';
18
+ static readonly opcode: Opcode = Opcode.L1GASLEFT;
19
+
20
+ protected getValue(context: AvmContext): MemoryValue {
21
+ return new Field(context.machineState.l1GasLeft);
22
+ }
23
+ }
24
+
25
+ export class DAGasLeft extends GetterInstruction {
26
+ static type: string = 'DAGASLEFT';
27
+ static readonly opcode: Opcode = Opcode.DAGASLEFT;
28
+
29
+ protected getValue(context: AvmContext): MemoryValue {
30
+ return new Field(context.machineState.daGasLeft);
31
+ }
32
+ }
@@ -0,0 +1,58 @@
1
+ import { AztecAddress, Fr } from '@aztec/circuits.js';
2
+
3
+ import type { AvmContext } from '../avm_context.js';
4
+ import { Field } from '../avm_memory_types.js';
5
+ import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
6
+ import { Addressing } from './addressing_mode.js';
7
+ import { Instruction } from './instruction.js';
8
+
9
+ export class GetContractInstance extends Instruction {
10
+ static readonly type: string = 'GETCONTRACTINSTANCE';
11
+ static readonly opcode: Opcode = Opcode.GETCONTRACTINSTANCE;
12
+ // Informs (de)serialization. See Instruction.deserialize.
13
+ static readonly wireFormat: OperandType[] = [
14
+ OperandType.UINT8,
15
+ OperandType.UINT8,
16
+ OperandType.UINT32,
17
+ OperandType.UINT32,
18
+ ];
19
+
20
+ constructor(private indirect: number, private addressOffset: number, private dstOffset: number) {
21
+ super();
22
+ }
23
+
24
+ async execute(context: AvmContext): Promise<void> {
25
+ const [addressOffset, dstOffset] = Addressing.fromWire(this.indirect).resolve(
26
+ [this.addressOffset, this.dstOffset],
27
+ context.machineState.memory,
28
+ );
29
+
30
+ const address = AztecAddress.fromField(context.machineState.memory.get(addressOffset).toFr());
31
+ const instance = await context.persistableState.hostStorage.contractsDb.getContractInstance(address);
32
+
33
+ const data =
34
+ instance === undefined
35
+ ? [
36
+ new Field(0), // not found
37
+ new Field(0),
38
+ new Field(0),
39
+ new Field(0),
40
+ new Field(0),
41
+ new Field(0),
42
+ new Field(0),
43
+ ]
44
+ : [
45
+ new Fr(1), // found
46
+ instance.salt,
47
+ instance.deployer.toField(),
48
+ instance.contractClassId,
49
+ instance.initializationHash,
50
+ instance.portalContractAddress.toField(),
51
+ instance.publicKeysHash,
52
+ ].map(f => new Field(f));
53
+
54
+ context.machineState.memory.setSlice(dstOffset, data);
55
+
56
+ context.machineState.incrementPc();
57
+ }
58
+ }
@@ -14,8 +14,12 @@ export class Jump extends Instruction {
14
14
  super();
15
15
  }
16
16
 
17
- async execute(context: AvmContext): Promise<void> {
17
+ public async execute(context: AvmContext): Promise<void> {
18
+ context.machineState.consumeGas(this.gasCost());
19
+
18
20
  context.machineState.pc = this.jumpOffset;
21
+
22
+ context.machineState.memory.assert({});
19
23
  }
20
24
  }
21
25
 
@@ -35,8 +39,12 @@ export class JumpI extends Instruction {
35
39
  super();
36
40
  }
37
41
 
38
- async execute(context: AvmContext): Promise<void> {
39
- const condition = context.machineState.memory.getAs<IntegralValue>(this.condOffset);
42
+ public async execute(context: AvmContext): Promise<void> {
43
+ const memoryOperations = { reads: 1, indirect: this.indirect };
44
+ const memory = context.machineState.memory.track(this.type);
45
+ context.machineState.consumeGas(this.gasCost(memoryOperations));
46
+
47
+ const condition = memory.getAs<IntegralValue>(this.condOffset);
40
48
 
41
49
  // TODO: reconsider this casting
42
50
  if (condition.toBigInt() == 0n) {
@@ -44,6 +52,8 @@ export class JumpI extends Instruction {
44
52
  } else {
45
53
  context.machineState.pc = this.loc;
46
54
  }
55
+
56
+ memory.assert(memoryOperations);
47
57
  }
48
58
  }
49
59
 
@@ -57,9 +67,13 @@ export class InternalCall extends Instruction {
57
67
  super();
58
68
  }
59
69
 
60
- async execute(context: AvmContext): Promise<void> {
70
+ public async execute(context: AvmContext): Promise<void> {
71
+ context.machineState.consumeGas(this.gasCost());
72
+
61
73
  context.machineState.internalCallStack.push(context.machineState.pc + 1);
62
74
  context.machineState.pc = this.loc;
75
+
76
+ context.machineState.memory.assert({});
63
77
  }
64
78
  }
65
79
 
@@ -73,11 +87,15 @@ export class InternalReturn extends Instruction {
73
87
  super();
74
88
  }
75
89
 
76
- async execute(context: AvmContext): Promise<void> {
90
+ public async execute(context: AvmContext): Promise<void> {
91
+ context.machineState.consumeGas(this.gasCost());
92
+
77
93
  const jumpOffset = context.machineState.internalCallStack.pop();
78
94
  if (jumpOffset === undefined) {
79
95
  throw new InstructionExecutionError('Internal call stack empty!');
80
96
  }
81
97
  context.machineState.pc = jumpOffset;
98
+
99
+ context.machineState.memory.assert({});
82
100
  }
83
101
  }