@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,225 @@
1
+ import {
2
+ CompleteAddress,
3
+ MerkleTreeId,
4
+ Note,
5
+ NoteStatus,
6
+ NullifierMembershipWitness,
7
+ PublicDataWitness,
8
+ PublicKey,
9
+ SiblingPath,
10
+ UnencryptedL2Log,
11
+ } from '@aztec/circuit-types';
12
+ import {
13
+ GrumpkinPrivateKey,
14
+ Header,
15
+ L1_TO_L2_MSG_TREE_HEIGHT,
16
+ PrivateCallStackItem,
17
+ PublicCallRequest,
18
+ } from '@aztec/circuits.js';
19
+ import { FunctionSelector } from '@aztec/foundation/abi';
20
+ import { AztecAddress } from '@aztec/foundation/aztec-address';
21
+ import { EthAddress } from '@aztec/foundation/eth-address';
22
+ import { Fr } from '@aztec/foundation/fields';
23
+ import { ContractInstance } from '@aztec/types/contracts';
24
+
25
+ /**
26
+ * A pair of public key and secret key.
27
+ */
28
+ export interface KeyPair {
29
+ /**
30
+ * Public key.
31
+ */
32
+ publicKey: PublicKey;
33
+ /**
34
+ * Secret Key.
35
+ */
36
+ secretKey: GrumpkinPrivateKey;
37
+ }
38
+
39
+ /**
40
+ * Information about a note needed during execution.
41
+ */
42
+ export interface NoteData {
43
+ /** The note. */
44
+ note: Note;
45
+ /** The contract address of the note. */
46
+ contractAddress: AztecAddress;
47
+ /** The storage slot of the note. */
48
+ storageSlot: Fr;
49
+ /** The nonce of the note. */
50
+ nonce: Fr;
51
+ /** The inner note hash of the note. */
52
+ innerNoteHash: Fr;
53
+ /** The corresponding nullifier of the note. Undefined for pending notes. */
54
+ siloedNullifier?: Fr;
55
+ /** The note's leaf index in the note hash tree. Undefined for pending notes. */
56
+ index?: bigint;
57
+ }
58
+
59
+ export class MessageLoadOracleInputs<N extends number> {
60
+ constructor(
61
+ /** The index of the message commitment in the merkle tree. */
62
+ public index: bigint,
63
+ /** The path in the merkle tree to the message. */
64
+ public siblingPath: SiblingPath<N>,
65
+ ) {}
66
+
67
+ toFields(): Fr[] {
68
+ return [new Fr(this.index), ...this.siblingPath.toFields()];
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Oracle with typed parameters and typed return values.
74
+ * Methods that require read and/or write will have to be implemented based on the context (public, private, or view)
75
+ * and are unavailable by default.
76
+ */
77
+ export abstract class TypedOracle {
78
+ getRandomField(): Fr {
79
+ return Fr.random();
80
+ }
81
+
82
+ packArguments(_args: Fr[]): Promise<Fr> {
83
+ throw new Error('Not available.');
84
+ }
85
+
86
+ getNullifierKeyPair(_accountAddress: AztecAddress): Promise<KeyPair> {
87
+ throw new Error('Not available.');
88
+ }
89
+
90
+ getPublicKeyAndPartialAddress(_address: AztecAddress): Promise<Fr[] | undefined> {
91
+ throw new Error('Not available.');
92
+ }
93
+
94
+ getContractInstance(_address: AztecAddress): Promise<ContractInstance> {
95
+ throw new Error('Not available.');
96
+ }
97
+
98
+ getMembershipWitness(_blockNumber: number, _treeId: MerkleTreeId, _leafValue: Fr): Promise<Fr[] | undefined> {
99
+ throw new Error('Not available.');
100
+ }
101
+
102
+ getSiblingPath(_blockNumber: number, _treeId: MerkleTreeId, _leafIndex: Fr): Promise<Fr[]> {
103
+ throw new Error('Not available.');
104
+ }
105
+
106
+ getNullifierMembershipWitness(_blockNumber: number, _nullifier: Fr): Promise<NullifierMembershipWitness | undefined> {
107
+ throw new Error('Not available.');
108
+ }
109
+
110
+ getPublicDataTreeWitness(_blockNumber: number, _leafSlot: Fr): Promise<PublicDataWitness | undefined> {
111
+ throw new Error('Not available.');
112
+ }
113
+
114
+ getLowNullifierMembershipWitness(
115
+ _blockNumber: number,
116
+ _nullifier: Fr,
117
+ ): Promise<NullifierMembershipWitness | undefined> {
118
+ throw new Error('Not available.');
119
+ }
120
+
121
+ getHeader(_blockNumber: number): Promise<Header | undefined> {
122
+ throw new Error('Not available.');
123
+ }
124
+
125
+ getCompleteAddress(_address: AztecAddress): Promise<CompleteAddress> {
126
+ throw new Error('Not available.');
127
+ }
128
+
129
+ getAuthWitness(_messageHash: Fr): Promise<Fr[] | undefined> {
130
+ throw new Error('Not available.');
131
+ }
132
+
133
+ popCapsule(): Promise<Fr[]> {
134
+ throw new Error('Not available.');
135
+ }
136
+
137
+ getNotes(
138
+ _storageSlot: Fr,
139
+ _numSelects: number,
140
+ _selectBy: number[],
141
+ _selectValues: Fr[],
142
+ _selectComparators: number[],
143
+ _sortBy: number[],
144
+ _sortOrder: number[],
145
+ _limit: number,
146
+ _offset: number,
147
+ _status: NoteStatus,
148
+ ): Promise<NoteData[]> {
149
+ throw new Error('Not available.');
150
+ }
151
+
152
+ notifyCreatedNote(_storageSlot: Fr, _noteTypeId: Fr, _note: Fr[], _innerNoteHash: Fr): void {
153
+ throw new Error('Not available.');
154
+ }
155
+
156
+ notifyNullifiedNote(_innerNullifier: Fr, _innerNoteHash: Fr): Promise<void> {
157
+ throw new Error('Not available.');
158
+ }
159
+
160
+ checkNullifierExists(_innerNullifier: Fr): Promise<boolean> {
161
+ throw new Error('Not available.');
162
+ }
163
+
164
+ getL1ToL2MembershipWitness(_entryKey: Fr): Promise<MessageLoadOracleInputs<typeof L1_TO_L2_MSG_TREE_HEIGHT>> {
165
+ throw new Error('Not available.');
166
+ }
167
+
168
+ getPortalContractAddress(_contractAddress: AztecAddress): Promise<EthAddress> {
169
+ throw new Error('Not available.');
170
+ }
171
+
172
+ storageRead(_startStorageSlot: Fr, _numberOfElements: number): Promise<Fr[]> {
173
+ throw new Error('Not available.');
174
+ }
175
+
176
+ storageWrite(_startStorageSlot: Fr, _values: Fr[]): Promise<Fr[]> {
177
+ throw new Error('Not available.');
178
+ }
179
+
180
+ emitEncryptedLog(
181
+ _contractAddress: AztecAddress,
182
+ _storageSlot: Fr,
183
+ _noteTypeId: Fr,
184
+ _publicKey: PublicKey,
185
+ _log: Fr[],
186
+ ): void {
187
+ throw new Error('Not available.');
188
+ }
189
+
190
+ emitUnencryptedLog(_log: UnencryptedL2Log): void {
191
+ throw new Error('Not available.');
192
+ }
193
+
194
+ callPrivateFunction(
195
+ _targetContractAddress: AztecAddress,
196
+ _functionSelector: FunctionSelector,
197
+ _argsHash: Fr,
198
+ _sideffectCounter: number,
199
+ _isStaticCall: boolean,
200
+ _isDelegateCall: boolean,
201
+ ): Promise<PrivateCallStackItem> {
202
+ throw new Error('Not available.');
203
+ }
204
+
205
+ callPublicFunction(
206
+ _targetContractAddress: AztecAddress,
207
+ _functionSelector: FunctionSelector,
208
+ _argsHash: Fr,
209
+ _isStaticCall: boolean,
210
+ _isDelegateCall: boolean,
211
+ ): Promise<Fr[]> {
212
+ throw new Error('Not available.');
213
+ }
214
+
215
+ enqueuePublicFunctionCall(
216
+ _targetContractAddress: AztecAddress,
217
+ _functionSelector: FunctionSelector,
218
+ _argsHash: Fr,
219
+ _sideffectCounter: number,
220
+ _isStaticCall: boolean,
221
+ _isDelegateCall: boolean,
222
+ ): Promise<PublicCallRequest> {
223
+ throw new Error('Not available.');
224
+ }
225
+ }
@@ -0,0 +1,75 @@
1
+ import { PublicCallRequest } from '@aztec/circuits.js';
2
+ import { AztecAddress } from '@aztec/foundation/aztec-address';
3
+ import { EthAddress } from '@aztec/foundation/eth-address';
4
+ import { Fr } from '@aztec/foundation/fields';
5
+
6
+ import { ACVMField } from './acvm_types.js';
7
+
8
+ /**
9
+ * Adapts the buffer to the field size.
10
+ * @param originalBuf - The buffer to adapt.
11
+ * @returns The adapted buffer.
12
+ */
13
+ function adaptBufferSize(originalBuf: Buffer) {
14
+ const buffer = Buffer.alloc(Fr.SIZE_IN_BYTES);
15
+ if (originalBuf.length > buffer.length) {
16
+ throw new Error('Buffer does not fit in field');
17
+ }
18
+ originalBuf.copy(buffer, buffer.length - originalBuf.length);
19
+ return buffer;
20
+ }
21
+
22
+ /**
23
+ * Converts a value to an ACVM field.
24
+ * @param value - The value to convert.
25
+ * @returns The ACVM field.
26
+ */
27
+ export function toACVMField(
28
+ value: AztecAddress | EthAddress | Fr | Buffer | boolean | number | bigint | ACVMField,
29
+ ): ACVMField {
30
+ let buffer;
31
+ if (Buffer.isBuffer(value)) {
32
+ buffer = value;
33
+ } else if (typeof value === 'boolean' || typeof value === 'number' || typeof value === 'bigint') {
34
+ buffer = new Fr(value).toBuffer();
35
+ } else if (typeof value === 'string') {
36
+ buffer = Fr.fromString(value).toBuffer();
37
+ } else {
38
+ buffer = value.toBuffer();
39
+ }
40
+ return `0x${adaptBufferSize(buffer).toString('hex')}`;
41
+ }
42
+
43
+ // Utilities to write TS classes to ACVM Field arrays
44
+ // In the order that the ACVM expects them
45
+
46
+ /**
47
+ * Converts a public call stack item with the request for executing a public function to
48
+ * a set of ACVM fields accepted by the enqueue_public_function_call_oracle Aztec.nr function.
49
+ * Note that only the fields related to the request are serialized: those related to the result
50
+ * are empty since this is just an execution request, so we don't send them to the circuit.
51
+ * @param item - The public call stack item to serialize to be passed onto Noir.
52
+ * @returns The fields expected by the enqueue_public_function_call_oracle Aztec.nr function.
53
+ * TODO(#4380): Nuke this and replace it with PublicCallRequest.toFields()
54
+ */
55
+ export function toAcvmEnqueuePublicFunctionResult(item: PublicCallRequest): ACVMField[] {
56
+ return [
57
+ item.contractAddress.toField(),
58
+ ...item.functionData.toFields(),
59
+ ...item.callContext.toFields(),
60
+ item.getArgsHash(),
61
+ ].map(toACVMField);
62
+ }
63
+
64
+ /**
65
+ * Inserts a list of ACVM fields to a witness.
66
+ * @param witnessStartIndex - The index where to start inserting the fields.
67
+ * @param fields - The fields to insert.
68
+ * @returns The witness.
69
+ */
70
+ export function toACVMWitness(witnessStartIndex: number, fields: Parameters<typeof toACVMField>[0][]) {
71
+ return fields.reduce((witness, field, index) => {
72
+ witness.set(index + witnessStartIndex, toACVMField(field));
73
+ return witness;
74
+ }, new Map<number, ACVMField>());
75
+ }
@@ -0,0 +1,63 @@
1
+ import { AztecAddress } from '@aztec/circuits.js';
2
+ import { Fr } from '@aztec/foundation/fields';
3
+
4
+ import { AvmExecutionEnvironment } from './avm_execution_environment.js';
5
+ import { AvmMachineState } from './avm_machine_state.js';
6
+ import { AvmPersistableStateManager } from './journal/journal.js';
7
+
8
+ /**
9
+ * An execution context includes the information necessary to initiate AVM
10
+ * execution along with all state maintained by the AVM throughout execution.
11
+ */
12
+ export class AvmContext {
13
+ /**
14
+ * Create a new AVM context
15
+ * @param persistableState - Manages world state and accrued substate during execution - (caching, fetching, tracing)
16
+ * @param environment - Contains constant variables provided by the kernel
17
+ * @param machineState - VM state that is modified on an instruction-by-instruction basis
18
+ * @returns new AvmContext instance
19
+ */
20
+ constructor(
21
+ public persistableState: AvmPersistableStateManager,
22
+ public environment: AvmExecutionEnvironment,
23
+ public machineState: AvmMachineState,
24
+ ) {}
25
+
26
+ /**
27
+ * Prepare a new AVM context that will be ready for an external/nested call
28
+ * - Fork the world state journal
29
+ * - Derive a machine state from the current state
30
+ * - E.g., gas metering is preserved but pc is reset
31
+ * - Derive an execution environment from the caller/parent
32
+ * - Alter both address and storageAddress
33
+ *
34
+ * @param address - The contract instance to initialize a context for
35
+ * @param calldata - Data/arguments for nested call
36
+ * @returns new AvmContext instance
37
+ */
38
+ public createNestedContractCallContext(address: AztecAddress, calldata: Fr[]): AvmContext {
39
+ const newExecutionEnvironment = this.environment.deriveEnvironmentForNestedCall(address, calldata);
40
+ const forkedWorldState = this.persistableState.fork();
41
+ const machineState = AvmMachineState.fromState(this.machineState);
42
+ return new AvmContext(forkedWorldState, newExecutionEnvironment, machineState);
43
+ }
44
+
45
+ /**
46
+ * Prepare a new AVM context that will be ready for an external/nested static call
47
+ * - Fork the world state journal
48
+ * - Derive a machine state from the current state
49
+ * - E.g., gas metering is preserved but pc is reset
50
+ * - Derive an execution environment from the caller/parent
51
+ * - Alter both address and storageAddress
52
+ *
53
+ * @param address - The contract instance to initialize a context for
54
+ * @param calldata - Data/arguments for nested call
55
+ * @returns new AvmContext instance
56
+ */
57
+ public createNestedContractStaticCallContext(address: AztecAddress, calldata: Fr[]): AvmContext {
58
+ const newExecutionEnvironment = this.environment.deriveEnvironmentForNestedStaticCall(address, calldata);
59
+ const forkedWorldState = this.persistableState.fork();
60
+ const machineState = AvmMachineState.fromState(this.machineState);
61
+ return new AvmContext(forkedWorldState, newExecutionEnvironment, machineState);
62
+ }
63
+ }
@@ -0,0 +1,98 @@
1
+ import { FunctionSelector, GlobalVariables } from '@aztec/circuits.js';
2
+ import { AztecAddress } from '@aztec/foundation/aztec-address';
3
+ import { EthAddress } from '@aztec/foundation/eth-address';
4
+ import { Fr } from '@aztec/foundation/fields';
5
+
6
+ /**
7
+ * Contains variables that remain constant during AVM execution
8
+ * These variables are provided by the public kernel circuit
9
+ */
10
+ // TODO(https://github.com/AztecProtocol/aztec-packages/issues/3992): gas not implemented
11
+ export class AvmExecutionEnvironment {
12
+ constructor(
13
+ public readonly address: AztecAddress,
14
+
15
+ public readonly storageAddress: AztecAddress,
16
+
17
+ public readonly origin: AztecAddress,
18
+
19
+ public readonly sender: AztecAddress,
20
+
21
+ public readonly portal: EthAddress,
22
+
23
+ public readonly feePerL1Gas: Fr,
24
+
25
+ public readonly feePerL2Gas: Fr,
26
+
27
+ public readonly feePerDaGas: Fr,
28
+
29
+ public readonly contractCallDepth: Fr,
30
+
31
+ public readonly globals: GlobalVariables,
32
+
33
+ public readonly isStaticCall: boolean,
34
+
35
+ public readonly isDelegateCall: boolean,
36
+
37
+ public readonly calldata: Fr[],
38
+
39
+ public readonly temporaryFunctionSelector: FunctionSelector,
40
+ ) {}
41
+
42
+ public deriveEnvironmentForNestedCall(address: AztecAddress, calldata: Fr[]): AvmExecutionEnvironment {
43
+ return new AvmExecutionEnvironment(
44
+ /*address=*/ address,
45
+ /*storageAddress=*/ address,
46
+ this.origin,
47
+ this.sender,
48
+ this.portal,
49
+ this.feePerL1Gas,
50
+ this.feePerL2Gas,
51
+ this.feePerDaGas,
52
+ this.contractCallDepth,
53
+ this.globals,
54
+ this.isStaticCall,
55
+ this.isDelegateCall,
56
+ /*calldata=*/ calldata,
57
+ this.temporaryFunctionSelector,
58
+ );
59
+ }
60
+
61
+ public deriveEnvironmentForNestedStaticCall(address: AztecAddress, calldata: Fr[]): AvmExecutionEnvironment {
62
+ return new AvmExecutionEnvironment(
63
+ /*address=*/ address,
64
+ /*storageAddress=*/ address,
65
+ this.origin,
66
+ this.sender,
67
+ this.portal,
68
+ this.feePerL1Gas,
69
+ this.feePerL2Gas,
70
+ this.feePerDaGas,
71
+ this.contractCallDepth,
72
+ this.globals,
73
+ /*isStaticCall=*/ true,
74
+ this.isDelegateCall,
75
+ /*calldata=*/ calldata,
76
+ this.temporaryFunctionSelector,
77
+ );
78
+ }
79
+
80
+ public newDelegateCall(address: AztecAddress, calldata: Fr[]): AvmExecutionEnvironment {
81
+ return new AvmExecutionEnvironment(
82
+ /*address=*/ address,
83
+ this.storageAddress,
84
+ this.origin,
85
+ this.sender,
86
+ this.portal,
87
+ this.feePerL1Gas,
88
+ this.feePerL2Gas,
89
+ this.feePerDaGas,
90
+ this.contractCallDepth,
91
+ this.globals,
92
+ this.isStaticCall,
93
+ /*isDelegateCall=*/ true,
94
+ /*calldata=*/ calldata,
95
+ this.temporaryFunctionSelector,
96
+ );
97
+ }
98
+ }
@@ -0,0 +1,93 @@
1
+ import { Fr } from '@aztec/circuits.js';
2
+
3
+ import { TaggedMemory } from './avm_memory_types.js';
4
+ import { AvmContractCallResults } from './avm_message_call_result.js';
5
+
6
+ /**
7
+ * A few fields of machine state are initialized from AVM session inputs or call instruction arguments
8
+ */
9
+ export type InitialAvmMachineState = {
10
+ l1GasLeft: number;
11
+ l2GasLeft: number;
12
+ daGasLeft: number;
13
+ };
14
+
15
+ /**
16
+ * Avm state modified on an instruction-per-instruction basis.
17
+ */
18
+ export class AvmMachineState {
19
+ public l1GasLeft: number;
20
+ /** gas remaining of the gas allocated for a contract call */
21
+ public l2GasLeft: number;
22
+ public daGasLeft: number;
23
+ /** program counter */
24
+ public pc: number = 0;
25
+
26
+ /**
27
+ * On INTERNALCALL, internal call stack is pushed to with the current pc + 1
28
+ * On INTERNALRETURN, value is popped from the internal call stack and assigned to the pc.
29
+ */
30
+ public internalCallStack: number[] = [];
31
+
32
+ /** Memory accessible to user code */
33
+ public readonly memory: TaggedMemory = new TaggedMemory();
34
+
35
+ /**
36
+ * Signals that execution should end.
37
+ * AvmContext execution continues executing instructions until the machine state signals "halted"
38
+ * */
39
+ public halted: boolean = false;
40
+ /** Signals that execution has reverted normally (this does not cover exceptional halts) */
41
+ private reverted: boolean = false;
42
+ /** Output data must NOT be modified once it is set */
43
+ private output: Fr[] = [];
44
+
45
+ constructor(l1GasLeft: number, l2GasLeft: number, daGasLeft: number) {
46
+ this.l1GasLeft = l1GasLeft;
47
+ this.l2GasLeft = l2GasLeft;
48
+ this.daGasLeft = daGasLeft;
49
+ }
50
+
51
+ public static fromState(state: InitialAvmMachineState): AvmMachineState {
52
+ return new AvmMachineState(state.l1GasLeft, state.l2GasLeft, state.daGasLeft);
53
+ }
54
+
55
+ /**
56
+ * Most instructions just increment PC before they complete
57
+ */
58
+ public incrementPc() {
59
+ this.pc++;
60
+ }
61
+
62
+ /**
63
+ * Halt as successful
64
+ * Output data must NOT be modified once it is set
65
+ * @param output
66
+ */
67
+ public return(output: Fr[]) {
68
+ this.halted = true;
69
+ this.output = output;
70
+ }
71
+
72
+ /**
73
+ * Halt as reverted
74
+ * Output data must NOT be modified once it is set
75
+ * @param output
76
+ */
77
+ public revert(output: Fr[]) {
78
+ this.halted = true;
79
+ this.reverted = true;
80
+ this.output = output;
81
+ }
82
+
83
+ /**
84
+ * Get a summary of execution results for a halted machine state
85
+ * @returns summary of execution results
86
+ */
87
+ public getResults(): AvmContractCallResults {
88
+ if (!this.halted) {
89
+ throw new Error('Execution results are not ready! Execution is ongoing.');
90
+ }
91
+ return new AvmContractCallResults(this.reverted, this.output);
92
+ }
93
+ }