@aztec/simulator 0.23.0 → 0.26.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/dest/acvm/deserialize.d.ts +5 -0
  2. package/dest/acvm/deserialize.d.ts.map +1 -1
  3. package/dest/acvm/deserialize.js +8 -1
  4. package/dest/acvm/oracle/oracle.d.ts +7 -6
  5. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  6. package/dest/acvm/oracle/oracle.js +28 -15
  7. package/dest/acvm/oracle/typed_oracle.d.ts +9 -11
  8. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  9. package/dest/acvm/oracle/typed_oracle.js +11 -11
  10. package/dest/avm/avm_context.d.ts +4 -4
  11. package/dest/avm/avm_context.d.ts.map +1 -1
  12. package/dest/avm/avm_context.js +6 -6
  13. package/dest/avm/avm_execution_environment.d.ts +3 -2
  14. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  15. package/dest/avm/avm_execution_environment.js +6 -5
  16. package/dest/avm/avm_memory_types.d.ts +127 -37
  17. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  18. package/dest/avm/avm_memory_types.js +98 -106
  19. package/dest/avm/avm_simulator.d.ts +6 -4
  20. package/dest/avm/avm_simulator.d.ts.map +1 -1
  21. package/dest/avm/avm_simulator.js +17 -19
  22. package/dest/avm/errors.d.ts +3 -1
  23. package/dest/avm/errors.d.ts.map +1 -1
  24. package/dest/avm/errors.js +9 -3
  25. package/dest/avm/fixtures/index.d.ts +21 -5
  26. package/dest/avm/fixtures/index.d.ts.map +1 -1
  27. package/dest/avm/fixtures/index.js +28 -9
  28. package/dest/avm/journal/host_storage.d.ts +1 -1
  29. package/dest/avm/journal/host_storage.d.ts.map +1 -1
  30. package/dest/avm/journal/host_storage.js +1 -1
  31. package/dest/avm/journal/journal.d.ts +78 -50
  32. package/dest/avm/journal/journal.d.ts.map +1 -1
  33. package/dest/avm/journal/journal.js +125 -169
  34. package/dest/avm/journal/nullifiers.d.ts +85 -0
  35. package/dest/avm/journal/nullifiers.d.ts.map +1 -0
  36. package/dest/avm/journal/nullifiers.js +147 -0
  37. package/dest/avm/journal/public_storage.d.ts +88 -0
  38. package/dest/avm/journal/public_storage.d.ts.map +1 -0
  39. package/dest/avm/journal/public_storage.js +135 -0
  40. package/dest/avm/journal/trace.d.ts +43 -0
  41. package/dest/avm/journal/trace.d.ts.map +1 -0
  42. package/dest/avm/journal/trace.js +204 -0
  43. package/dest/avm/journal/trace_types.d.ts +26 -0
  44. package/dest/avm/journal/trace_types.d.ts.map +1 -0
  45. package/dest/avm/journal/trace_types.js +6 -0
  46. package/dest/avm/opcodes/accrued_substate.d.ts +37 -4
  47. package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
  48. package/dest/avm/opcodes/accrued_substate.js +109 -12
  49. package/dest/avm/opcodes/addressing_mode.d.ts +24 -0
  50. package/dest/avm/opcodes/addressing_mode.d.ts.map +1 -0
  51. package/dest/avm/opcodes/addressing_mode.js +62 -0
  52. package/dest/avm/opcodes/environment_getters.d.ts +14 -13
  53. package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
  54. package/dest/avm/opcodes/environment_getters.js +1 -1
  55. package/dest/avm/opcodes/external_calls.js +5 -5
  56. package/dest/avm/opcodes/hashing.d.ts +48 -0
  57. package/dest/avm/opcodes/hashing.d.ts.map +1 -0
  58. package/dest/avm/opcodes/hashing.js +127 -0
  59. package/dest/avm/opcodes/instruction.d.ts +4 -4
  60. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  61. package/dest/avm/opcodes/instruction.js +1 -1
  62. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  63. package/dest/avm/opcodes/memory.js +5 -3
  64. package/dest/avm/opcodes/storage.d.ts.map +1 -1
  65. package/dest/avm/opcodes/storage.js +3 -3
  66. package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  67. package/dest/avm/serialization/bytecode_serialization.js +28 -22
  68. package/dest/avm/serialization/instruction_serialization.d.ts +21 -16
  69. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  70. package/dest/avm/serialization/instruction_serialization.js +23 -18
  71. package/dest/avm/temporary_executor_migration.d.ts +25 -0
  72. package/dest/avm/temporary_executor_migration.d.ts.map +1 -0
  73. package/dest/avm/temporary_executor_migration.js +71 -0
  74. package/dest/client/client_execution_context.d.ts +13 -7
  75. package/dest/client/client_execution_context.d.ts.map +1 -1
  76. package/dest/client/client_execution_context.js +52 -27
  77. package/dest/client/db_oracle.d.ts +7 -0
  78. package/dest/client/db_oracle.d.ts.map +1 -1
  79. package/dest/client/db_oracle.js +1 -1
  80. package/dest/client/execution_note_cache.js +1 -1
  81. package/dest/client/execution_result.d.ts +4 -2
  82. package/dest/client/execution_result.d.ts.map +1 -1
  83. package/dest/client/execution_result.js +1 -1
  84. package/dest/client/private_execution.d.ts.map +1 -1
  85. package/dest/client/private_execution.js +4 -4
  86. package/dest/client/simulator.d.ts +11 -6
  87. package/dest/client/simulator.d.ts.map +1 -1
  88. package/dest/client/simulator.js +21 -12
  89. package/dest/client/unconstrained_execution.js +2 -2
  90. package/dest/client/view_data_oracle.d.ts +9 -2
  91. package/dest/client/view_data_oracle.d.ts.map +1 -1
  92. package/dest/client/view_data_oracle.js +13 -5
  93. package/dest/public/db.d.ts +17 -4
  94. package/dest/public/db.d.ts.map +1 -1
  95. package/dest/public/execution.d.ts +9 -4
  96. package/dest/public/execution.d.ts.map +1 -1
  97. package/dest/public/execution.js +18 -5
  98. package/dest/public/executor.d.ts +7 -0
  99. package/dest/public/executor.d.ts.map +1 -1
  100. package/dest/public/executor.js +40 -6
  101. package/dest/public/public_execution_context.d.ts +5 -4
  102. package/dest/public/public_execution_context.d.ts.map +1 -1
  103. package/dest/public/public_execution_context.js +24 -13
  104. package/dest/public/state_actions.d.ts +1 -1
  105. package/dest/public/state_actions.d.ts.map +1 -1
  106. package/dest/public/state_actions.js +6 -7
  107. package/dest/test/utils.js +4 -4
  108. package/dest/utils.d.ts +5 -20
  109. package/dest/utils.d.ts.map +1 -1
  110. package/dest/utils.js +4 -21
  111. package/package.json +9 -6
  112. package/src/acvm/acvm.ts +156 -0
  113. package/src/acvm/acvm_types.ts +11 -0
  114. package/src/acvm/deserialize.ts +44 -0
  115. package/src/acvm/index.ts +5 -0
  116. package/src/acvm/oracle/debug.ts +109 -0
  117. package/src/acvm/oracle/index.ts +17 -0
  118. package/src/acvm/oracle/oracle.ts +356 -0
  119. package/src/acvm/oracle/typed_oracle.ts +225 -0
  120. package/src/acvm/serialize.ts +75 -0
  121. package/src/avm/avm_context.ts +63 -0
  122. package/src/avm/avm_execution_environment.ts +98 -0
  123. package/src/avm/avm_machine_state.ts +93 -0
  124. package/src/avm/avm_memory_types.ts +324 -0
  125. package/src/avm/avm_message_call_result.ts +29 -0
  126. package/src/avm/avm_simulator.ts +87 -0
  127. package/src/avm/errors.ts +57 -0
  128. package/src/avm/fixtures/index.ts +115 -0
  129. package/src/avm/journal/host_storage.ts +14 -0
  130. package/src/avm/journal/index.ts +2 -0
  131. package/src/avm/journal/journal.ts +231 -0
  132. package/src/avm/journal/nullifiers.ts +170 -0
  133. package/src/avm/journal/public_storage.ts +149 -0
  134. package/src/avm/journal/trace.ts +223 -0
  135. package/src/avm/journal/trace_types.ts +79 -0
  136. package/src/avm/opcodes/.eslintrc.cjs +8 -0
  137. package/src/avm/opcodes/accrued_substate.ts +214 -0
  138. package/src/avm/opcodes/addressing_mode.ts +66 -0
  139. package/src/avm/opcodes/arithmetic.ts +79 -0
  140. package/src/avm/opcodes/bitwise.ts +129 -0
  141. package/src/avm/opcodes/comparators.ts +69 -0
  142. package/src/avm/opcodes/control_flow.ts +129 -0
  143. package/src/avm/opcodes/environment_getters.ts +201 -0
  144. package/src/avm/opcodes/external_calls.ts +122 -0
  145. package/src/avm/opcodes/hashing.ts +170 -0
  146. package/src/avm/opcodes/index.ts +10 -0
  147. package/src/avm/opcodes/instruction.ts +64 -0
  148. package/src/avm/opcodes/instruction_impl.ts +52 -0
  149. package/src/avm/opcodes/memory.ts +194 -0
  150. package/src/avm/opcodes/storage.ts +79 -0
  151. package/src/avm/serialization/buffer_cursor.ts +109 -0
  152. package/src/avm/serialization/bytecode_serialization.ts +179 -0
  153. package/src/avm/serialization/instruction_serialization.ts +170 -0
  154. package/src/avm/temporary_executor_migration.ts +109 -0
  155. package/src/client/client_execution_context.ts +502 -0
  156. package/src/client/db_oracle.ts +192 -0
  157. package/src/client/execution_note_cache.ts +90 -0
  158. package/src/client/execution_result.ts +89 -0
  159. package/src/client/index.ts +3 -0
  160. package/src/client/pick_notes.ts +125 -0
  161. package/src/client/private_execution.ts +79 -0
  162. package/src/client/simulator.ts +317 -0
  163. package/src/client/unconstrained_execution.ts +49 -0
  164. package/src/client/view_data_oracle.ts +253 -0
  165. package/src/common/errors.ts +61 -0
  166. package/src/common/index.ts +3 -0
  167. package/src/common/packed_args_cache.ts +55 -0
  168. package/src/common/side_effect_counter.ts +12 -0
  169. package/src/index.ts +3 -0
  170. package/src/public/db.ts +100 -0
  171. package/src/public/execution.ts +161 -0
  172. package/src/public/executor.ts +178 -0
  173. package/src/public/index.ts +9 -0
  174. package/src/public/public_execution_context.ts +241 -0
  175. package/src/public/state_actions.ts +100 -0
  176. package/src/test/utils.ts +38 -0
  177. package/src/utils.ts +18 -0
@@ -0,0 +1,129 @@
1
+ import type { AvmContext } from '../avm_context.js';
2
+ import { IntegralValue } from '../avm_memory_types.js';
3
+ import { Opcode } from '../serialization/instruction_serialization.js';
4
+ import { ThreeOperandInstruction, TwoOperandInstruction } from './instruction_impl.js';
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
+ }
13
+
14
+ async execute(context: AvmContext): Promise<void> {
15
+ context.machineState.memory.checkTags(this.inTag, this.aOffset, this.bOffset);
16
+
17
+ const a = context.machineState.memory.getAs<IntegralValue>(this.aOffset);
18
+ const b = context.machineState.memory.getAs<IntegralValue>(this.bOffset);
19
+
20
+ const res = a.and(b);
21
+ context.machineState.memory.set(this.dstOffset, res);
22
+
23
+ context.machineState.incrementPc();
24
+ }
25
+ }
26
+
27
+ export class Or extends ThreeOperandInstruction {
28
+ static readonly type: string = 'OR';
29
+ static readonly opcode = Opcode.OR;
30
+
31
+ constructor(indirect: number, inTag: number, aOffset: number, bOffset: number, dstOffset: number) {
32
+ super(indirect, inTag, aOffset, bOffset, dstOffset);
33
+ }
34
+
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);
43
+
44
+ context.machineState.incrementPc();
45
+ }
46
+ }
47
+
48
+ export class Xor extends ThreeOperandInstruction {
49
+ static readonly type: string = 'XOR';
50
+ static readonly opcode = Opcode.XOR;
51
+
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.getAs<IntegralValue>(this.aOffset);
60
+ const b = context.machineState.memory.getAs<IntegralValue>(this.bOffset);
61
+
62
+ const res = a.xor(b);
63
+ context.machineState.memory.set(this.dstOffset, res);
64
+
65
+ context.machineState.incrementPc();
66
+ }
67
+ }
68
+
69
+ export class Not extends TwoOperandInstruction {
70
+ static readonly type: string = 'NOT';
71
+ static readonly opcode = Opcode.NOT;
72
+
73
+ constructor(indirect: number, inTag: number, aOffset: number, dstOffset: number) {
74
+ super(indirect, inTag, aOffset, dstOffset);
75
+ }
76
+
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);
99
+
100
+ const a = context.machineState.memory.getAs<IntegralValue>(this.aOffset);
101
+ const b = context.machineState.memory.getAs<IntegralValue>(this.bOffset);
102
+
103
+ const res = a.shl(b);
104
+ context.machineState.memory.set(this.dstOffset, res);
105
+
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);
126
+
127
+ context.machineState.incrementPc();
128
+ }
129
+ }
@@ -0,0 +1,69 @@
1
+ import type { AvmContext } from '../avm_context.js';
2
+ import { Opcode } from '../serialization/instruction_serialization.js';
3
+ import { ThreeOperandInstruction } from './instruction_impl.js';
4
+
5
+ export class Eq extends ThreeOperandInstruction {
6
+ static readonly type: string = 'EQ';
7
+ static readonly opcode = Opcode.EQ;
8
+
9
+ constructor(indirect: number, inTag: number, aOffset: number, bOffset: number, dstOffset: number) {
10
+ super(indirect, inTag, aOffset, bOffset, dstOffset);
11
+ }
12
+
13
+ async execute(context: AvmContext): Promise<void> {
14
+ context.machineState.memory.checkTags(this.inTag, this.aOffset, this.bOffset);
15
+
16
+ const a = context.machineState.memory.get(this.aOffset);
17
+ const b = context.machineState.memory.get(this.bOffset);
18
+
19
+ // Result will be of the same type as 'a'.
20
+ const dest = a.build(a.equals(b) ? 1n : 0n);
21
+ context.machineState.memory.set(this.dstOffset, dest);
22
+
23
+ context.machineState.incrementPc();
24
+ }
25
+ }
26
+
27
+ export class Lt extends ThreeOperandInstruction {
28
+ static readonly type: string = 'LT';
29
+ static readonly opcode = Opcode.LT;
30
+
31
+ constructor(indirect: number, inTag: number, aOffset: number, bOffset: number, dstOffset: number) {
32
+ super(indirect, inTag, aOffset, bOffset, dstOffset);
33
+ }
34
+
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
+ // Result will be of the same type as 'a'.
42
+ const dest = a.build(a.lt(b) ? 1n : 0n);
43
+ context.machineState.memory.set(this.dstOffset, dest);
44
+
45
+ context.machineState.incrementPc();
46
+ }
47
+ }
48
+
49
+ export class Lte extends ThreeOperandInstruction {
50
+ static readonly type: string = 'LTE';
51
+ static readonly opcode = Opcode.LTE;
52
+
53
+ constructor(indirect: number, inTag: number, aOffset: number, bOffset: number, dstOffset: number) {
54
+ super(indirect, inTag, aOffset, bOffset, dstOffset);
55
+ }
56
+
57
+ async execute(context: AvmContext): Promise<void> {
58
+ context.machineState.memory.checkTags(this.inTag, this.aOffset, this.bOffset);
59
+
60
+ const a = context.machineState.memory.get(this.aOffset);
61
+ const b = context.machineState.memory.get(this.bOffset);
62
+
63
+ // Result will be of the same type as 'a'.
64
+ const dest = a.build(a.equals(b) || a.lt(b) ? 1n : 0n);
65
+ context.machineState.memory.set(this.dstOffset, dest);
66
+
67
+ context.machineState.incrementPc();
68
+ }
69
+ }
@@ -0,0 +1,129 @@
1
+ import type { AvmContext } from '../avm_context.js';
2
+ import { IntegralValue } from '../avm_memory_types.js';
3
+ import { InstructionExecutionError } from '../errors.js';
4
+ import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
5
+ import { Instruction } from './instruction.js';
6
+
7
+ export class Return extends Instruction {
8
+ static type: string = 'RETURN';
9
+ static readonly opcode: Opcode = Opcode.RETURN;
10
+ // Informs (de)serialization. See Instruction.deserialize.
11
+ static readonly wireFormat: OperandType[] = [
12
+ OperandType.UINT8,
13
+ OperandType.UINT8,
14
+ OperandType.UINT32,
15
+ OperandType.UINT32,
16
+ ];
17
+
18
+ constructor(private indirect: number, private returnOffset: number, private copySize: number) {
19
+ super();
20
+ }
21
+
22
+ async execute(context: AvmContext): Promise<void> {
23
+ const output = context.machineState.memory.getSlice(this.returnOffset, this.copySize).map(word => word.toFr());
24
+
25
+ context.machineState.return(output);
26
+ }
27
+ }
28
+
29
+ export class Revert extends Instruction {
30
+ static type: string = 'RETURN';
31
+ static readonly opcode: Opcode = Opcode.REVERT;
32
+ // Informs (de)serialization. See Instruction.deserialize.
33
+ static readonly wireFormat: OperandType[] = [
34
+ OperandType.UINT8,
35
+ OperandType.UINT8,
36
+ OperandType.UINT32,
37
+ OperandType.UINT32,
38
+ ];
39
+
40
+ constructor(private indirect: number, private returnOffset: number, private retSize: number) {
41
+ super();
42
+ }
43
+
44
+ async execute(context: AvmContext): Promise<void> {
45
+ const output = context.machineState.memory
46
+ .getSlice(this.returnOffset, this.returnOffset + this.retSize)
47
+ .map(word => word.toFr());
48
+
49
+ context.machineState.revert(output);
50
+ }
51
+ }
52
+
53
+ export class Jump extends Instruction {
54
+ static type: string = 'JUMP';
55
+ static readonly opcode: Opcode = Opcode.JUMP;
56
+ // Informs (de)serialization. See Instruction.deserialize.
57
+ static readonly wireFormat: OperandType[] = [OperandType.UINT8, OperandType.UINT32];
58
+
59
+ constructor(private jumpOffset: number) {
60
+ super();
61
+ }
62
+
63
+ async execute(context: AvmContext): Promise<void> {
64
+ context.machineState.pc = this.jumpOffset;
65
+ }
66
+ }
67
+
68
+ export class JumpI extends Instruction {
69
+ static type: string = 'JUMPI';
70
+ static readonly opcode: Opcode = Opcode.JUMPI;
71
+
72
+ // Instruction wire format with opcode.
73
+ static readonly wireFormat: OperandType[] = [
74
+ OperandType.UINT8,
75
+ OperandType.UINT8,
76
+ OperandType.UINT32,
77
+ OperandType.UINT32,
78
+ ];
79
+
80
+ constructor(private indirect: number, private loc: number, private condOffset: number) {
81
+ super();
82
+ }
83
+
84
+ async execute(context: AvmContext): Promise<void> {
85
+ const condition = context.machineState.memory.getAs<IntegralValue>(this.condOffset);
86
+
87
+ // TODO: reconsider this casting
88
+ if (condition.toBigInt() == 0n) {
89
+ context.machineState.incrementPc();
90
+ } else {
91
+ context.machineState.pc = this.loc;
92
+ }
93
+ }
94
+ }
95
+
96
+ export class InternalCall extends Instruction {
97
+ static readonly type: string = 'INTERNALCALL';
98
+ static readonly opcode: Opcode = Opcode.INTERNALCALL;
99
+ // Informs (de)serialization. See Instruction.deserialize.
100
+ static readonly wireFormat: OperandType[] = [OperandType.UINT8, OperandType.UINT32];
101
+
102
+ constructor(private loc: number) {
103
+ super();
104
+ }
105
+
106
+ async execute(context: AvmContext): Promise<void> {
107
+ context.machineState.internalCallStack.push(context.machineState.pc + 1);
108
+ context.machineState.pc = this.loc;
109
+ }
110
+ }
111
+
112
+ export class InternalReturn extends Instruction {
113
+ static readonly type: string = 'INTERNALRETURN';
114
+ static readonly opcode: Opcode = Opcode.INTERNALRETURN;
115
+ // Informs (de)serialization. See Instruction.deserialize.
116
+ static readonly wireFormat: OperandType[] = [OperandType.UINT8];
117
+
118
+ constructor() {
119
+ super();
120
+ }
121
+
122
+ async execute(context: AvmContext): Promise<void> {
123
+ const jumpOffset = context.machineState.internalCallStack.pop();
124
+ if (jumpOffset === undefined) {
125
+ throw new InstructionExecutionError('Internal call empty!');
126
+ }
127
+ context.machineState.pc = jumpOffset;
128
+ }
129
+ }
@@ -0,0 +1,201 @@
1
+ import { Fr } from '@aztec/circuits.js';
2
+
3
+ import type { AvmContext } from '../avm_context.js';
4
+ import type { AvmExecutionEnvironment } from '../avm_execution_environment.js';
5
+ import { Field } from '../avm_memory_types.js';
6
+ import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
7
+ import { Instruction } from './instruction.js';
8
+
9
+ abstract class GetterInstruction extends Instruction {
10
+ // Informs (de)serialization. See Instruction.deserialize.
11
+ static readonly wireFormat: OperandType[] = [OperandType.UINT8, OperandType.UINT8, OperandType.UINT32];
12
+
13
+ constructor(protected indirect: number, protected dstOffset: number) {
14
+ super();
15
+ }
16
+
17
+ async execute(context: AvmContext): Promise<void> {
18
+ const res = new Field(this.getIt(context.environment));
19
+ context.machineState.memory.set(this.dstOffset, res);
20
+ context.machineState.incrementPc();
21
+ }
22
+
23
+ protected abstract getIt(env: AvmExecutionEnvironment): Fr | number | bigint;
24
+ }
25
+
26
+ export class Address extends GetterInstruction {
27
+ static type: string = 'ADDRESS';
28
+ static readonly opcode: Opcode = Opcode.ADDRESS;
29
+
30
+ protected getIt(env: AvmExecutionEnvironment) {
31
+ return env.address;
32
+ }
33
+ }
34
+
35
+ export class StorageAddress extends GetterInstruction {
36
+ static type: string = 'STORAGEADDRESS';
37
+ static readonly opcode: Opcode = Opcode.STORAGEADDRESS;
38
+
39
+ protected getIt(env: AvmExecutionEnvironment) {
40
+ return env.storageAddress;
41
+ }
42
+ }
43
+
44
+ export class Sender extends GetterInstruction {
45
+ static type: string = 'SENDER';
46
+ static readonly opcode: Opcode = Opcode.SENDER;
47
+
48
+ protected getIt(env: AvmExecutionEnvironment) {
49
+ return env.sender;
50
+ }
51
+ }
52
+
53
+ export class Origin extends GetterInstruction {
54
+ static type: string = 'ORIGIN';
55
+ static readonly opcode: Opcode = Opcode.ORIGIN;
56
+
57
+ protected getIt(env: AvmExecutionEnvironment) {
58
+ return env.origin;
59
+ }
60
+ }
61
+
62
+ export class FeePerL1Gas extends GetterInstruction {
63
+ static type: string = 'FEEPERL1GAS';
64
+ static readonly opcode: Opcode = Opcode.FEEPERL1GAS;
65
+
66
+ protected getIt(env: AvmExecutionEnvironment) {
67
+ return env.feePerL1Gas;
68
+ }
69
+ }
70
+
71
+ export class FeePerL2Gas extends GetterInstruction {
72
+ static type: string = 'FEEPERL2GAS';
73
+ static readonly opcode: Opcode = Opcode.FEEPERL2GAS;
74
+
75
+ protected getIt(env: AvmExecutionEnvironment) {
76
+ return env.feePerL2Gas;
77
+ }
78
+ }
79
+
80
+ export class FeePerDAGas extends GetterInstruction {
81
+ static type: string = 'FEEPERDAGAS';
82
+ static readonly opcode: Opcode = Opcode.FEEPERDAGAS;
83
+
84
+ protected getIt(env: AvmExecutionEnvironment) {
85
+ return env.feePerDaGas;
86
+ }
87
+ }
88
+
89
+ export class Portal extends GetterInstruction {
90
+ static type: string = 'PORTAL';
91
+ static readonly opcode: Opcode = Opcode.PORTAL;
92
+
93
+ protected getIt(env: AvmExecutionEnvironment) {
94
+ return env.portal.toField();
95
+ }
96
+ }
97
+
98
+ export class ChainId extends GetterInstruction {
99
+ static type: string = 'CHAINID';
100
+ static readonly opcode: Opcode = Opcode.CHAINID;
101
+
102
+ protected getIt(env: AvmExecutionEnvironment) {
103
+ return env.globals.chainId;
104
+ }
105
+ }
106
+
107
+ export class Version extends GetterInstruction {
108
+ static type: string = 'VERSION';
109
+ static readonly opcode: Opcode = Opcode.VERSION;
110
+
111
+ protected getIt(env: AvmExecutionEnvironment) {
112
+ return env.globals.version;
113
+ }
114
+ }
115
+
116
+ export class BlockNumber extends GetterInstruction {
117
+ static type: string = 'BLOCKNUMBER';
118
+ static readonly opcode: Opcode = Opcode.BLOCKNUMBER;
119
+
120
+ protected getIt(env: AvmExecutionEnvironment) {
121
+ return env.globals.blockNumber;
122
+ }
123
+ }
124
+
125
+ export class Timestamp extends GetterInstruction {
126
+ static type: string = 'TIMESTAMP';
127
+ static readonly opcode: Opcode = Opcode.TIMESTAMP;
128
+
129
+ protected getIt(env: AvmExecutionEnvironment) {
130
+ return env.globals.timestamp;
131
+ }
132
+ }
133
+
134
+ // export class Coinbase extends GetterInstruction {
135
+ // static type: string = 'COINBASE';
136
+ // static numberOfOperands = 1;
137
+
138
+ // constructor(private destOffset: number) {
139
+ // super();
140
+ // }
141
+
142
+ // async execute(machineState: AvmMachineState, _journal: AvmJournal): Promise<void> {
143
+ // const {coinbase} = machineState.executionEnvironment.globals;
144
+
145
+ // machineState.memory.set(this.destOffset, coinbase);
146
+
147
+ // this.incrementPc(machineState);
148
+ // }
149
+ // }
150
+
151
+ // // TODO: are these even needed within the block? (both block gas limit variables - why does the execution env care?)
152
+ // export class BlockL1GasLimit extends GetterInstruction {
153
+ // static type: string = 'BLOCKL1GASLIMIT';
154
+ // static numberOfOperands = 1;
155
+
156
+ // constructor(private destOffset: number) {
157
+ // super();
158
+ // }
159
+
160
+ // async execute(machineState: AvmMachineState, _journal: AvmJournal): Promise<void> {
161
+ // const {blockL1GasLimit} = machineState.executionEnvironment.globals;
162
+
163
+ // machineState.memory.set(this.destOffset, blockL1GasLimit);
164
+
165
+ // this.incrementPc(machineState);
166
+ // }
167
+ // }
168
+
169
+ // export class BlockL2GasLimit extends GetterInstruction {
170
+ // static type: string = 'BLOCKL2GASLIMIT';
171
+ // static numberOfOperands = 1;
172
+
173
+ // constructor(private destOffset: number) {
174
+ // super();
175
+ // }
176
+
177
+ // async execute(machineState: AvmMachineState, _journal: AvmJournal): Promise<void> {
178
+ // const {blockL2GasLimit} = machineState.executionEnvironment.globals;
179
+
180
+ // machineState.memory.set(this.destOffset, blockL2GasLimit);
181
+
182
+ // this.incrementPc(machineState);
183
+ // }
184
+ // }
185
+
186
+ // export class BlockDAGasLimit extends GetterInstruction {
187
+ // static type: string = 'BLOCKDAGASLIMIT';
188
+ // static numberOfOperands = 1;
189
+
190
+ // constructor(private destOffset: number) {
191
+ // super();
192
+ // }
193
+
194
+ // async execute(machineState: AvmMachineState, _journal: AvmJournal): Promise<void> {
195
+ // const {blockDAGasLimit} = machineState.executionEnvironment.globals;
196
+
197
+ // machineState.memory.set(this.destOffset, blockDAGasLimit);
198
+
199
+ // this.incrementPc(machineState);
200
+ // }
201
+ // }
@@ -0,0 +1,122 @@
1
+ import { Fr } from '@aztec/foundation/fields';
2
+
3
+ import type { AvmContext } from '../avm_context.js';
4
+ import { Field } from '../avm_memory_types.js';
5
+ import { AvmSimulator } from '../avm_simulator.js';
6
+ import { Opcode, OperandType } from '../serialization/instruction_serialization.js';
7
+ import { Instruction } from './instruction.js';
8
+
9
+ export class Call extends Instruction {
10
+ static type: string = 'CALL';
11
+ static readonly opcode: Opcode = Opcode.CALL;
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
+ OperandType.UINT32,
19
+ OperandType.UINT32,
20
+ OperandType.UINT32,
21
+ OperandType.UINT32,
22
+ OperandType.UINT32,
23
+ ];
24
+
25
+ constructor(
26
+ private indirect: number,
27
+ private _gasOffset: number /* Unused due to no formal gas implementation at this moment */,
28
+ private addrOffset: number,
29
+ private argsOffset: number,
30
+ private argsSize: number,
31
+ private retOffset: number,
32
+ private retSize: number,
33
+ private successOffset: number,
34
+ ) {
35
+ super();
36
+ }
37
+
38
+ // TODO(https://github.com/AztecProtocol/aztec-packages/issues/3992): there is no concept of remaining / available gas at this moment
39
+ async execute(context: AvmContext): Promise<void> {
40
+ const callAddress = context.machineState.memory.getAs<Field>(this.addrOffset);
41
+ const calldata = context.machineState.memory.getSlice(this.argsOffset, this.argsSize).map(f => f.toFr());
42
+
43
+ const nestedContext = context.createNestedContractCallContext(callAddress.toFr(), calldata);
44
+
45
+ const nestedCallResults = await new AvmSimulator(nestedContext).execute();
46
+ const success = !nestedCallResults.reverted;
47
+
48
+ // We only take as much data as was specified in the return size -> TODO: should we be reverting here
49
+ const returnData = nestedCallResults.output.slice(0, this.retSize);
50
+ const convertedReturnData = returnData.map(f => new Field(f));
51
+
52
+ // Write our return data into memory
53
+ context.machineState.memory.set(this.successOffset, new Field(success ? 1 : 0));
54
+ context.machineState.memory.setSlice(this.retOffset, convertedReturnData);
55
+
56
+ if (success) {
57
+ context.persistableState.acceptNestedCallState(nestedContext.persistableState);
58
+ } else {
59
+ context.persistableState.rejectNestedCallState(nestedContext.persistableState);
60
+ }
61
+
62
+ context.machineState.incrementPc();
63
+ }
64
+ }
65
+
66
+ export class StaticCall extends Instruction {
67
+ static type: string = 'STATICCALL';
68
+ static readonly opcode: Opcode = Opcode.STATICCALL;
69
+ // Informs (de)serialization. See Instruction.deserialize.
70
+ static readonly wireFormat: OperandType[] = [
71
+ OperandType.UINT8,
72
+ OperandType.UINT8,
73
+ OperandType.UINT32,
74
+ OperandType.UINT32,
75
+ OperandType.UINT32,
76
+ OperandType.UINT32,
77
+ OperandType.UINT32,
78
+ OperandType.UINT32,
79
+ OperandType.UINT32,
80
+ ];
81
+
82
+ constructor(
83
+ private indirect: number,
84
+ private _gasOffset: number /* Unused due to no formal gas implementation at this moment */,
85
+ private addrOffset: number,
86
+ private argsOffset: number,
87
+ private argsSize: number,
88
+ private retOffset: number,
89
+ private retSize: number,
90
+ private successOffset: number,
91
+ ) {
92
+ super();
93
+ }
94
+
95
+ async execute(context: AvmContext): Promise<void> {
96
+ const callAddress = context.machineState.memory.get(this.addrOffset);
97
+ const calldata = context.machineState.memory
98
+ .getSlice(this.argsOffset, this.argsSize)
99
+ .map(f => new Fr(f.toBigInt()));
100
+
101
+ const nestedContext = context.createNestedContractStaticCallContext(callAddress.toFr(), calldata);
102
+
103
+ const nestedCallResults = await new AvmSimulator(nestedContext).execute();
104
+ const success = !nestedCallResults.reverted;
105
+
106
+ // We only take as much data as was specified in the return size -> TODO: should we be reverting here
107
+ const returnData = nestedCallResults.output.slice(0, this.retSize);
108
+ const convertedReturnData = returnData.map(f => new Field(f));
109
+
110
+ // Write our return data into memory
111
+ context.machineState.memory.set(this.successOffset, new Field(success ? 1 : 0));
112
+ context.machineState.memory.setSlice(this.retOffset, convertedReturnData);
113
+
114
+ if (success) {
115
+ context.persistableState.acceptNestedCallState(nestedContext.persistableState);
116
+ } else {
117
+ context.persistableState.rejectNestedCallState(nestedContext.persistableState);
118
+ }
119
+
120
+ context.machineState.incrementPc();
121
+ }
122
+ }