@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,179 @@
1
+ import { Keccak, Pedersen, Poseidon2, Sha256 } from '../opcodes/hashing.js';
2
+ import {
3
+ Add,
4
+ Address,
5
+ And,
6
+ BlockNumber,
7
+ CMov,
8
+ Call,
9
+ CalldataCopy,
10
+ Cast,
11
+ ChainId,
12
+ Div,
13
+ EmitNoteHash,
14
+ EmitNullifier,
15
+ EmitUnencryptedLog,
16
+ Eq,
17
+ FeePerDAGas,
18
+ FeePerL1Gas,
19
+ FeePerL2Gas,
20
+ InternalCall,
21
+ InternalReturn,
22
+ Jump,
23
+ JumpI,
24
+ L1ToL2MessageExists,
25
+ Lt,
26
+ Lte,
27
+ Mov,
28
+ Mul,
29
+ Not,
30
+ NoteHashExists,
31
+ NullifierExists,
32
+ Or,
33
+ Origin,
34
+ Portal,
35
+ Return,
36
+ Revert,
37
+ SLoad,
38
+ SStore,
39
+ SendL2ToL1Message,
40
+ Sender,
41
+ Set,
42
+ Shl,
43
+ Shr,
44
+ StaticCall,
45
+ StorageAddress,
46
+ Sub,
47
+ Timestamp,
48
+ Version,
49
+ Xor,
50
+ } from '../opcodes/index.js';
51
+ import type { Instruction } from '../opcodes/index.js';
52
+ import { BufferCursor } from './buffer_cursor.js';
53
+ import { Opcode } from './instruction_serialization.js';
54
+
55
+ interface DeserializableInstruction {
56
+ deserialize(buf: BufferCursor | Buffer): Instruction;
57
+ opcode: Opcode;
58
+ }
59
+
60
+ export type InstructionSet = Map<Opcode, DeserializableInstruction>;
61
+ // TODO(4359): This is a function so that Call and StaticCall can be lazily resolved.
62
+ // This is a temporary solution until we solve the dependency cycle.
63
+ const INSTRUCTION_SET = () =>
64
+ new Map<Opcode, DeserializableInstruction>([
65
+ [Add.opcode, Add],
66
+ [Sub.opcode, Sub],
67
+ [Mul.opcode, Mul],
68
+ [Div.opcode, Div],
69
+ [Eq.opcode, Eq],
70
+ [Lt.opcode, Lt],
71
+ [Lte.opcode, Lte],
72
+ [And.opcode, And],
73
+ [Or.opcode, Or],
74
+ [Xor.opcode, Xor],
75
+ [Not.opcode, Not],
76
+ [Shl.opcode, Shl],
77
+ [Shr.opcode, Shr],
78
+ [Cast.opcode, Cast],
79
+ [Address.opcode, Address],
80
+ [StorageAddress.opcode, StorageAddress],
81
+ [Origin.opcode, Origin],
82
+ [Sender.opcode, Sender],
83
+ [Portal.opcode, Portal],
84
+ [FeePerL1Gas.opcode, FeePerL1Gas],
85
+ [FeePerL2Gas.opcode, FeePerL2Gas],
86
+ [FeePerDAGas.opcode, FeePerDAGas],
87
+ //[Contractcalldepth.opcode, Contractcalldepth],
88
+ // Execution Environment - Globals
89
+ [ChainId.opcode, ChainId],
90
+ [Version.opcode, Version],
91
+ [BlockNumber.opcode, BlockNumber],
92
+ [Timestamp.opcode, Timestamp],
93
+ //[Coinbase.opcode, Coinbase],
94
+ //[Blockl1gaslimit.opcode, Blockl1gaslimit],
95
+ //[Blockl2gaslimit.opcode, Blockl2gaslimit],
96
+ //[Blockdagaslimit.opcode, Blockdagaslimit],
97
+ // Execution Environment - Calldata
98
+ [CalldataCopy.opcode, CalldataCopy],
99
+
100
+ // Machine State
101
+ // Machine State - Gas
102
+ //[L1gasleft.opcode, L1gasleft],
103
+ //[L2gasleft.opcode, L2gasleft],
104
+ //[Dagasleft.opcode, Dagasleft],
105
+ // Machine State - Internal Control Flow
106
+ [Jump.opcode, Jump],
107
+ [JumpI.opcode, JumpI],
108
+ [InternalCall.opcode, InternalCall],
109
+ [InternalReturn.opcode, InternalReturn],
110
+ [Set.opcode, Set],
111
+ [Mov.opcode, Mov],
112
+ [CMov.opcode, CMov],
113
+
114
+ // World State
115
+ [SLoad.opcode, SLoad], // Public Storage
116
+ [SStore.opcode, SStore], // Public Storage
117
+ [NoteHashExists.opcode, NoteHashExists], // Notes & Nullifiers
118
+ [EmitNoteHash.opcode, EmitNoteHash], // Notes & Nullifiers
119
+ [NullifierExists.opcode, NullifierExists], // Notes & Nullifiers
120
+ [EmitNullifier.opcode, EmitNullifier], // Notes & Nullifiers
121
+ [L1ToL2MessageExists.opcode, L1ToL2MessageExists], // Messages
122
+ //[HeaderMember.opcode, HeaderMember], // Header
123
+
124
+ // Accrued Substate
125
+ [EmitUnencryptedLog.opcode, EmitUnencryptedLog],
126
+ [SendL2ToL1Message.opcode, SendL2ToL1Message],
127
+
128
+ // Control Flow - Contract Calls
129
+ [Call.opcode, Call],
130
+ [StaticCall.opcode, StaticCall],
131
+ //[DelegateCall.opcode, DelegateCall],
132
+ [Return.opcode, Return],
133
+ [Revert.opcode, Revert],
134
+
135
+ // //// Gadgets
136
+ [Keccak.opcode, Keccak],
137
+ [Poseidon2.opcode, Poseidon2],
138
+ [Sha256.opcode, Sha256],
139
+ [Pedersen.opcode, Pedersen],
140
+ ]);
141
+
142
+ interface Serializable {
143
+ serialize(): Buffer;
144
+ }
145
+
146
+ /**
147
+ * Serializes an array of instructions to bytecode.
148
+ */
149
+ export function encodeToBytecode(instructions: Serializable[]): Buffer {
150
+ return Buffer.concat(instructions.map(i => i.serialize()));
151
+ }
152
+
153
+ /**
154
+ * Convert a buffer of bytecode into an array of instructions.
155
+ * @param bytecode Buffer of bytecode.
156
+ * @param instructionSet Optional {@code InstructionSet} to be used for deserialization.
157
+ * @returns Bytecode decoded into an ordered array of Instructions
158
+ */
159
+ export function decodeFromBytecode(
160
+ bytecode: Buffer,
161
+ instructionSet: InstructionSet = INSTRUCTION_SET(),
162
+ ): Instruction[] {
163
+ const instructions: Instruction[] = [];
164
+ const cursor = new BufferCursor(bytecode);
165
+
166
+ while (!cursor.eof()) {
167
+ const opcode: Opcode = cursor.bufferAtPosition().readUint8(); // peek.
168
+ const instructionDeserializerOrUndef = instructionSet.get(opcode);
169
+ if (instructionDeserializerOrUndef === undefined) {
170
+ throw new Error(`Opcode ${Opcode[opcode]} (0x${opcode.toString(16)}) not implemented`);
171
+ }
172
+
173
+ const instructionDeserializer: DeserializableInstruction = instructionDeserializerOrUndef;
174
+ const i: Instruction = instructionDeserializer.deserialize(cursor);
175
+ instructions.push(i);
176
+ }
177
+
178
+ return instructions;
179
+ }
@@ -0,0 +1,170 @@
1
+ import { strict as assert } from 'assert';
2
+
3
+ import { BufferCursor } from './buffer_cursor.js';
4
+
5
+ /**
6
+ * All AVM opcodes. (Keep in sync with cpp counterpart code avm_opcode.hpp).
7
+ * Source: https://yp-aztec.netlify.app/docs/public-vm/instruction-set
8
+ */
9
+ export enum Opcode {
10
+ ADD,
11
+ SUB,
12
+ MUL,
13
+ DIV,
14
+ EQ,
15
+ LT,
16
+ LTE,
17
+ AND,
18
+ OR,
19
+ XOR,
20
+ NOT,
21
+ SHL,
22
+ SHR,
23
+ CAST,
24
+ ADDRESS,
25
+ STORAGEADDRESS,
26
+ ORIGIN,
27
+ SENDER,
28
+ PORTAL,
29
+ FEEPERL1GAS,
30
+ FEEPERL2GAS,
31
+ FEEPERDAGAS,
32
+ CONTRACTCALLDEPTH,
33
+ CHAINID,
34
+ VERSION,
35
+ BLOCKNUMBER,
36
+ TIMESTAMP,
37
+ COINBASE,
38
+ BLOCKL1GASLIMIT,
39
+ BLOCKL2GASLIMIT,
40
+ BLOCKDAGASLIMIT,
41
+ CALLDATACOPY,
42
+ L1GASLEFT,
43
+ L2GASLEFT,
44
+ DAGASLEFT,
45
+ JUMP,
46
+ JUMPI,
47
+ INTERNALCALL,
48
+ INTERNALRETURN,
49
+ SET,
50
+ MOV,
51
+ CMOV,
52
+ SLOAD,
53
+ SSTORE,
54
+ NOTEHASHEXISTS,
55
+ EMITNOTEHASH,
56
+ NULLIFIEREXISTS,
57
+ EMITNULLIFIER,
58
+ L1TOL2MSGEXISTS,
59
+ HEADERMEMBER,
60
+ EMITUNENCRYPTEDLOG,
61
+ SENDL2TOL1MSG,
62
+ CALL,
63
+ STATICCALL,
64
+ DELEGATECALL,
65
+ RETURN,
66
+ REVERT,
67
+ KECCAK,
68
+ POSEIDON,
69
+ // Add new opcodes before this
70
+ SHA256, // temp - may be removed, but alot of contracts rely on it
71
+ PEDERSEN, // temp - may be removed, but alot of contracts rely on it
72
+ TOTAL_OPCODES_NUMBER,
73
+ }
74
+
75
+ // Possible types for an instruction's operand in its wire format. (Keep in sync with CPP code.
76
+ // See vm/avm_trace/avm_deserialization.cpp)
77
+ // Note that cpp code introduced an additional enum value TAG to express the instruction tag. In TS,
78
+ // this one is parsed as UINT8.
79
+ export enum OperandType {
80
+ UINT8,
81
+ UINT16,
82
+ UINT32,
83
+ UINT64,
84
+ UINT128,
85
+ }
86
+
87
+ type OperandNativeType = number | bigint;
88
+ type OperandWriter = (value: any) => void;
89
+
90
+ // Specifies how to read and write each operand type.
91
+ const OPERAND_SPEC = new Map<OperandType, [number, () => OperandNativeType, OperandWriter]>([
92
+ [OperandType.UINT8, [1, Buffer.prototype.readUint8, Buffer.prototype.writeUint8]],
93
+ [OperandType.UINT16, [2, Buffer.prototype.readUint16BE, Buffer.prototype.writeUint16BE]],
94
+ [OperandType.UINT32, [4, Buffer.prototype.readUint32BE, Buffer.prototype.writeUint32BE]],
95
+ [OperandType.UINT64, [8, Buffer.prototype.readBigInt64BE, Buffer.prototype.writeBigInt64BE]],
96
+ [OperandType.UINT128, [16, readBigInt128BE, writeBigInt128BE]],
97
+ ]);
98
+
99
+ function readBigInt128BE(this: Buffer): bigint {
100
+ const totalBytes = 16;
101
+ let ret: bigint = 0n;
102
+ for (let i = 0; i < totalBytes; ++i) {
103
+ ret <<= 8n;
104
+ ret |= BigInt(this.readUint8(i));
105
+ }
106
+ return ret;
107
+ }
108
+
109
+ function writeBigInt128BE(this: Buffer, value: bigint): void {
110
+ const totalBytes = 16;
111
+ for (let offset = totalBytes - 1; offset >= 0; --offset) {
112
+ this.writeUint8(Number(value & 0xffn), offset);
113
+ value >>= 8n;
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Reads an array of operands from a buffer.
119
+ * @param cursor Buffer to read from. Might be longer than needed.
120
+ * @param operands Specification of the operand types.
121
+ * @returns An array as big as {@code operands}, with the converted TS values.
122
+ */
123
+ export function deserialize(cursor: BufferCursor | Buffer, operands: OperandType[]): (number | bigint)[] {
124
+ const argValues = [];
125
+ if (cursor instanceof Buffer) {
126
+ cursor = new BufferCursor(cursor);
127
+ }
128
+
129
+ for (const op of operands) {
130
+ const opType = op;
131
+ const [sizeBytes, reader, _writer] = OPERAND_SPEC.get(opType)!;
132
+ argValues.push(reader.call(cursor.bufferAtPosition()));
133
+ cursor.advance(sizeBytes);
134
+ }
135
+
136
+ return argValues;
137
+ }
138
+
139
+ /**
140
+ * Serializes a class using the specified operand types.
141
+ * More specifically, this serializes {@code [cls.constructor.opcode, ...Object.values(cls)]}.
142
+ * Observe in particular that:
143
+ * (1) the first operand type specified must correspond to the opcode;
144
+ * (2) the rest of the operand types must be specified in the order returned by {@code Object.values()}.
145
+ * @param operands Type specification for the values to be serialized.
146
+ * @param cls The class to be serialized.
147
+ * @returns
148
+ */
149
+ export function serialize(operands: OperandType[], cls: any): Buffer {
150
+ const chunks: Buffer[] = [];
151
+
152
+ // TODO: infer opcode not in this loop
153
+ assert(cls.constructor.opcode !== undefined && cls.constructor.opcode !== null);
154
+ const rawClassValues: any[] = [cls.constructor.opcode, ...Object.values(cls)];
155
+ assert(
156
+ rawClassValues.length === operands.length,
157
+ `Got ${rawClassValues.length} values but only ${operands.length} serialization operands are specified!`,
158
+ );
159
+ const classValues = rawClassValues as OperandNativeType[];
160
+
161
+ for (let i = 0; i < operands.length; i++) {
162
+ const opType = operands[i];
163
+ const [sizeBytes, _reader, writer] = OPERAND_SPEC.get(opType)!;
164
+ const buf = Buffer.alloc(sizeBytes);
165
+ writer.call(buf, classValues[i]);
166
+ chunks.push(buf);
167
+ }
168
+
169
+ return Buffer.concat(chunks);
170
+ }
@@ -0,0 +1,109 @@
1
+ // All code in this file needs to die once the public executor is phased out.
2
+ import { FunctionL2Logs } from '@aztec/circuit-types';
3
+ import {
4
+ ContractStorageRead,
5
+ ContractStorageUpdateRequest,
6
+ GlobalVariables,
7
+ L2ToL1Message,
8
+ SideEffect,
9
+ SideEffectLinkedToNoteHash,
10
+ } from '@aztec/circuits.js';
11
+ import { Fr } from '@aztec/foundation/fields';
12
+
13
+ import { PublicExecution, PublicExecutionResult } from '../public/execution.js';
14
+ import { AvmExecutionEnvironment } from './avm_execution_environment.js';
15
+ import { AvmContractCallResults } from './avm_message_call_result.js';
16
+ import { JournalData } from './journal/journal.js';
17
+
18
+ /** Temporary Method
19
+ *
20
+ * Convert a PublicExecution(Environment) object to an AvmExecutionEnvironment
21
+ *
22
+ * @param current
23
+ * @param globalVariables
24
+ * @returns
25
+ */
26
+ export function temporaryCreateAvmExecutionEnvironment(
27
+ current: PublicExecution,
28
+ globalVariables: GlobalVariables,
29
+ ): AvmExecutionEnvironment {
30
+ // Function selector is included temporarily until noir codegens public contract bytecode in a single blob
31
+ return new AvmExecutionEnvironment(
32
+ current.contractAddress,
33
+ current.callContext.storageContractAddress,
34
+ current.callContext.msgSender, // TODO: origin is not available
35
+ current.callContext.msgSender,
36
+ current.callContext.portalContractAddress,
37
+ /*feePerL1Gas=*/ Fr.zero(),
38
+ /*feePerL2Gas=*/ Fr.zero(),
39
+ /*feePerDaGas=*/ Fr.zero(),
40
+ /*contractCallDepth=*/ Fr.zero(),
41
+ globalVariables,
42
+ current.callContext.isStaticCall,
43
+ current.callContext.isDelegateCall,
44
+ current.args,
45
+ current.functionData.selector,
46
+ );
47
+ }
48
+
49
+ /** Temporary Method
50
+ *
51
+ * Convert the result of an AVM contract call to a PublicExecutionResult for the public kernel
52
+ *
53
+ * @param execution
54
+ * @param newWorldState
55
+ * @param result
56
+ * @returns
57
+ */
58
+ export function temporaryConvertAvmResults(
59
+ execution: PublicExecution,
60
+ newWorldState: JournalData,
61
+ result: AvmContractCallResults,
62
+ ): PublicExecutionResult {
63
+ const newNoteHashes = newWorldState.newNoteHashes.map(noteHash => new SideEffect(noteHash, Fr.zero()));
64
+
65
+ const contractStorageReads: ContractStorageRead[] = [];
66
+ const reduceStorageReadRequests = (contractAddress: bigint, storageReads: Map<bigint, Fr[]>) => {
67
+ return storageReads.forEach((innerArray, key) => {
68
+ innerArray.forEach(value => {
69
+ contractStorageReads.push(new ContractStorageRead(new Fr(key), new Fr(value), 0));
70
+ });
71
+ });
72
+ };
73
+ newWorldState.storageReads.forEach((storageMap: Map<bigint, Fr[]>, address: bigint) =>
74
+ reduceStorageReadRequests(address, storageMap),
75
+ );
76
+
77
+ const contractStorageUpdateRequests: ContractStorageUpdateRequest[] = [];
78
+ const reduceStorageUpdateRequests = (contractAddress: bigint, storageUpdateRequests: Map<bigint, Fr[]>) => {
79
+ return storageUpdateRequests.forEach((innerArray, key) => {
80
+ innerArray.forEach(value => {
81
+ contractStorageUpdateRequests.push(new ContractStorageUpdateRequest(new Fr(key), new Fr(value), 0));
82
+ });
83
+ });
84
+ };
85
+ newWorldState.storageWrites.forEach((storageMap: Map<bigint, Fr[]>, address: bigint) =>
86
+ reduceStorageUpdateRequests(address, storageMap),
87
+ );
88
+
89
+ const returnValues = result.output;
90
+
91
+ // TODO(follow up in pr tree): NOT SUPPORTED YET, make sure hashing and log resolution is done correctly
92
+ // Disabled.
93
+ const nestedExecutions: PublicExecutionResult[] = [];
94
+ const newNullifiers: SideEffectLinkedToNoteHash[] = [];
95
+ const unencryptedLogs = FunctionL2Logs.empty();
96
+ const newL2ToL1Messages = newWorldState.newL1Messages.map(() => L2ToL1Message.empty());
97
+
98
+ return {
99
+ execution,
100
+ newNoteHashes,
101
+ newL2ToL1Messages,
102
+ newNullifiers,
103
+ contractStorageReads,
104
+ contractStorageUpdateRequests,
105
+ returnValues,
106
+ nestedExecutions,
107
+ unencryptedLogs,
108
+ };
109
+ }