@aztec/simulator 0.22.0 → 0.24.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 (124) hide show
  1. package/dest/acvm/oracle/oracle.d.ts +2 -2
  2. package/dest/acvm/oracle/oracle.d.ts.map +1 -1
  3. package/dest/acvm/oracle/oracle.js +5 -5
  4. package/dest/acvm/oracle/typed_oracle.d.ts +2 -2
  5. package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
  6. package/dest/acvm/oracle/typed_oracle.js +3 -3
  7. package/dest/avm/avm_execution_environment.d.ts +3 -2
  8. package/dest/avm/avm_execution_environment.d.ts.map +1 -1
  9. package/dest/avm/avm_execution_environment.js +6 -5
  10. package/dest/avm/avm_memory_types.d.ts +120 -39
  11. package/dest/avm/avm_memory_types.d.ts.map +1 -1
  12. package/dest/avm/avm_memory_types.js +91 -109
  13. package/dest/avm/avm_simulator.d.ts.map +1 -1
  14. package/dest/avm/avm_simulator.js +2 -3
  15. package/dest/avm/errors.d.ts +3 -1
  16. package/dest/avm/errors.d.ts.map +1 -1
  17. package/dest/avm/errors.js +9 -3
  18. package/dest/avm/fixtures/index.d.ts +4 -0
  19. package/dest/avm/fixtures/index.d.ts.map +1 -1
  20. package/dest/avm/fixtures/index.js +11 -3
  21. package/dest/avm/journal/host_storage.d.ts +1 -1
  22. package/dest/avm/journal/host_storage.d.ts.map +1 -1
  23. package/dest/avm/opcodes/addressing_mode.d.ts +24 -0
  24. package/dest/avm/opcodes/addressing_mode.d.ts.map +1 -0
  25. package/dest/avm/opcodes/addressing_mode.js +62 -0
  26. package/dest/avm/opcodes/comparators.d.ts.map +1 -1
  27. package/dest/avm/opcodes/comparators.js +8 -5
  28. package/dest/avm/opcodes/instruction.d.ts +6 -4
  29. package/dest/avm/opcodes/instruction.d.ts.map +1 -1
  30. package/dest/avm/opcodes/instruction.js +4 -3
  31. package/dest/avm/opcodes/memory.d.ts +9 -2
  32. package/dest/avm/opcodes/memory.d.ts.map +1 -1
  33. package/dest/avm/opcodes/memory.js +46 -8
  34. package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
  35. package/dest/avm/serialization/bytecode_serialization.js +24 -22
  36. package/dest/avm/serialization/instruction_serialization.d.ts +17 -15
  37. package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
  38. package/dest/avm/serialization/instruction_serialization.js +22 -17
  39. package/dest/avm/temporary_executor_migration.d.ts +25 -0
  40. package/dest/avm/temporary_executor_migration.d.ts.map +1 -0
  41. package/dest/avm/temporary_executor_migration.js +71 -0
  42. package/dest/client/client_execution_context.d.ts +4 -2
  43. package/dest/client/client_execution_context.d.ts.map +1 -1
  44. package/dest/client/client_execution_context.js +7 -4
  45. package/dest/client/execution_result.d.ts +2 -0
  46. package/dest/client/execution_result.d.ts.map +1 -1
  47. package/dest/client/execution_result.js +1 -1
  48. package/dest/client/simulator.d.ts +10 -5
  49. package/dest/client/simulator.d.ts.map +1 -1
  50. package/dest/client/simulator.js +19 -11
  51. package/dest/client/unconstrained_execution.js +2 -2
  52. package/dest/public/execution.js +2 -2
  53. package/dest/public/executor.d.ts +7 -0
  54. package/dest/public/executor.d.ts.map +1 -1
  55. package/dest/public/executor.js +26 -1
  56. package/dest/public/public_execution_context.js +2 -2
  57. package/dest/public/state_actions.d.ts +1 -1
  58. package/dest/public/state_actions.d.ts.map +1 -1
  59. package/dest/public/state_actions.js +5 -6
  60. package/dest/utils.d.ts +5 -20
  61. package/dest/utils.d.ts.map +1 -1
  62. package/dest/utils.js +4 -20
  63. package/package.json +7 -5
  64. package/src/acvm/acvm.ts +156 -0
  65. package/src/acvm/acvm_types.ts +11 -0
  66. package/src/acvm/deserialize.ts +36 -0
  67. package/src/acvm/index.ts +5 -0
  68. package/src/acvm/oracle/debug.ts +109 -0
  69. package/src/acvm/oracle/index.ts +17 -0
  70. package/src/acvm/oracle/oracle.ts +332 -0
  71. package/src/acvm/oracle/typed_oracle.ts +217 -0
  72. package/src/acvm/serialize.ts +75 -0
  73. package/src/avm/avm_context.ts +63 -0
  74. package/src/avm/avm_execution_environment.ts +98 -0
  75. package/src/avm/avm_machine_state.ts +93 -0
  76. package/src/avm/avm_memory_types.ts +309 -0
  77. package/src/avm/avm_message_call_result.ts +29 -0
  78. package/src/avm/avm_simulator.ts +89 -0
  79. package/src/avm/errors.ts +57 -0
  80. package/src/avm/fixtures/index.ts +90 -0
  81. package/src/avm/journal/host_storage.ts +20 -0
  82. package/src/avm/journal/index.ts +2 -0
  83. package/src/avm/journal/journal.ts +266 -0
  84. package/src/avm/opcodes/.eslintrc.cjs +8 -0
  85. package/src/avm/opcodes/accrued_substate.ts +92 -0
  86. package/src/avm/opcodes/addressing_mode.ts +66 -0
  87. package/src/avm/opcodes/arithmetic.ts +79 -0
  88. package/src/avm/opcodes/bitwise.ts +129 -0
  89. package/src/avm/opcodes/comparators.ts +72 -0
  90. package/src/avm/opcodes/control_flow.ts +129 -0
  91. package/src/avm/opcodes/environment_getters.ts +199 -0
  92. package/src/avm/opcodes/external_calls.ts +122 -0
  93. package/src/avm/opcodes/index.ts +10 -0
  94. package/src/avm/opcodes/instruction.ts +64 -0
  95. package/src/avm/opcodes/instruction_impl.ts +52 -0
  96. package/src/avm/opcodes/memory.ts +193 -0
  97. package/src/avm/opcodes/storage.ts +76 -0
  98. package/src/avm/serialization/buffer_cursor.ts +109 -0
  99. package/src/avm/serialization/bytecode_serialization.ts +172 -0
  100. package/src/avm/serialization/instruction_serialization.ts +167 -0
  101. package/src/avm/temporary_executor_migration.ts +108 -0
  102. package/src/client/client_execution_context.ts +472 -0
  103. package/src/client/db_oracle.ts +184 -0
  104. package/src/client/execution_note_cache.ts +90 -0
  105. package/src/client/execution_result.ts +89 -0
  106. package/src/client/index.ts +3 -0
  107. package/src/client/pick_notes.ts +125 -0
  108. package/src/client/private_execution.ts +78 -0
  109. package/src/client/simulator.ts +316 -0
  110. package/src/client/unconstrained_execution.ts +49 -0
  111. package/src/client/view_data_oracle.ts +243 -0
  112. package/src/common/errors.ts +61 -0
  113. package/src/common/index.ts +3 -0
  114. package/src/common/packed_args_cache.ts +55 -0
  115. package/src/common/side_effect_counter.ts +12 -0
  116. package/src/index.ts +3 -0
  117. package/src/public/db.ts +85 -0
  118. package/src/public/execution.ts +137 -0
  119. package/src/public/executor.ts +158 -0
  120. package/src/public/index.ts +9 -0
  121. package/src/public/public_execution_context.ts +217 -0
  122. package/src/public/state_actions.ts +100 -0
  123. package/src/test/utils.ts +38 -0
  124. package/src/utils.ts +18 -0
@@ -0,0 +1,167 @@
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 AvmMini_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
+ READL1TOL2MSG,
59
+ HEADERMEMBER,
60
+ EMITUNENCRYPTEDLOG,
61
+ SENDL2TOL1MSG,
62
+ CALL,
63
+ STATICCALL,
64
+ RETURN,
65
+ REVERT,
66
+ KECCAK,
67
+ POSEIDON,
68
+ // Add new opcodes before this
69
+ TOTAL_OPCODES_NUMBER,
70
+ }
71
+
72
+ // Possible types for an instruction's operand in its wire format. (Keep in sync with CPP code.
73
+ // See vm/avm_trace/AvmMini_deserialization.cpp)
74
+ // Note that cpp code introduced an additional enum value TAG to express the instruction tag. In TS,
75
+ // this one is parsed as UINT8.
76
+ export enum OperandType {
77
+ UINT8,
78
+ UINT16,
79
+ UINT32,
80
+ UINT64,
81
+ UINT128,
82
+ }
83
+
84
+ type OperandNativeType = number | bigint;
85
+ type OperandWriter = (value: any) => void;
86
+
87
+ // Specifies how to read and write each operand type.
88
+ const OPERAND_SPEC = new Map<OperandType, [number, () => OperandNativeType, OperandWriter]>([
89
+ [OperandType.UINT8, [1, Buffer.prototype.readUint8, Buffer.prototype.writeUint8]],
90
+ [OperandType.UINT16, [2, Buffer.prototype.readUint16BE, Buffer.prototype.writeUint16BE]],
91
+ [OperandType.UINT32, [4, Buffer.prototype.readUint32BE, Buffer.prototype.writeUint32BE]],
92
+ [OperandType.UINT64, [8, Buffer.prototype.readBigInt64BE, Buffer.prototype.writeBigInt64BE]],
93
+ [OperandType.UINT128, [16, readBigInt128BE, writeBigInt128BE]],
94
+ ]);
95
+
96
+ function readBigInt128BE(this: Buffer): bigint {
97
+ const totalBytes = 16;
98
+ let ret: bigint = 0n;
99
+ for (let i = 0; i < totalBytes; ++i) {
100
+ ret <<= 8n;
101
+ ret |= BigInt(this.readUint8(i));
102
+ }
103
+ return ret;
104
+ }
105
+
106
+ function writeBigInt128BE(this: Buffer, value: bigint): void {
107
+ const totalBytes = 16;
108
+ for (let offset = totalBytes - 1; offset >= 0; --offset) {
109
+ this.writeUint8(Number(value & 0xffn), offset);
110
+ value >>= 8n;
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Reads an array of operands from a buffer.
116
+ * @param cursor Buffer to read from. Might be longer than needed.
117
+ * @param operands Specification of the operand types.
118
+ * @returns An array as big as {@code operands}, with the converted TS values.
119
+ */
120
+ export function deserialize(cursor: BufferCursor | Buffer, operands: OperandType[]): (number | bigint)[] {
121
+ const argValues = [];
122
+ if (cursor instanceof Buffer) {
123
+ cursor = new BufferCursor(cursor);
124
+ }
125
+
126
+ for (const op of operands) {
127
+ const opType = op;
128
+ const [sizeBytes, reader, _writer] = OPERAND_SPEC.get(opType)!;
129
+ argValues.push(reader.call(cursor.bufferAtPosition()));
130
+ cursor.advance(sizeBytes);
131
+ }
132
+
133
+ return argValues;
134
+ }
135
+
136
+ /**
137
+ * Serializes a class using the specified operand types.
138
+ * More specifically, this serializes {@code [cls.constructor.opcode, ...Object.values(cls)]}.
139
+ * Observe in particular that:
140
+ * (1) the first operand type specified must correspond to the opcode;
141
+ * (2) the rest of the operand types must be specified in the order returned by {@code Object.values()}.
142
+ * @param operands Type specification for the values to be serialized.
143
+ * @param cls The class to be serialized.
144
+ * @returns
145
+ */
146
+ export function serialize(operands: OperandType[], cls: any): Buffer {
147
+ const chunks: Buffer[] = [];
148
+
149
+ // TODO: infer opcode not in this loop
150
+ assert(cls.constructor.opcode !== undefined && cls.constructor.opcode !== null);
151
+ const rawClassValues: any[] = [cls.constructor.opcode, ...Object.values(cls)];
152
+ assert(
153
+ rawClassValues.length === operands.length,
154
+ `Got ${rawClassValues.length} values but only ${operands.length} serialization operands are specified!`,
155
+ );
156
+ const classValues = rawClassValues as OperandNativeType[];
157
+
158
+ for (let i = 0; i < operands.length; i++) {
159
+ const opType = operands[i];
160
+ const [sizeBytes, _reader, writer] = OPERAND_SPEC.get(opType)!;
161
+ const buf = Buffer.alloc(sizeBytes);
162
+ writer.call(buf, classValues[i]);
163
+ chunks.push(buf);
164
+ }
165
+
166
+ return Buffer.concat(chunks);
167
+ }
@@ -0,0 +1,108 @@
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
+ SideEffect,
8
+ SideEffectLinkedToNoteHash,
9
+ } from '@aztec/circuits.js';
10
+ import { Fr } from '@aztec/foundation/fields';
11
+
12
+ import { PublicExecution, PublicExecutionResult } from '../public/execution.js';
13
+ import { AvmExecutionEnvironment } from './avm_execution_environment.js';
14
+ import { AvmContractCallResults } from './avm_message_call_result.js';
15
+ import { JournalData } from './journal/journal.js';
16
+
17
+ /** Temporary Method
18
+ *
19
+ * Convert a PublicExecution(Environment) object to an AvmExecutionEnvironment
20
+ *
21
+ * @param current
22
+ * @param globalVariables
23
+ * @returns
24
+ */
25
+ export function temporaryCreateAvmExecutionEnvironment(
26
+ current: PublicExecution,
27
+ globalVariables: GlobalVariables,
28
+ ): AvmExecutionEnvironment {
29
+ // Function selector is included temporarily until noir codegens public contract bytecode in a single blob
30
+ return new AvmExecutionEnvironment(
31
+ current.contractAddress,
32
+ current.callContext.storageContractAddress,
33
+ current.callContext.msgSender, // TODO: origin is not available
34
+ current.callContext.msgSender,
35
+ current.callContext.portalContractAddress,
36
+ /*feePerL1Gas=*/ Fr.zero(),
37
+ /*feePerL2Gas=*/ Fr.zero(),
38
+ /*feePerDaGas=*/ Fr.zero(),
39
+ /*contractCallDepth=*/ Fr.zero(),
40
+ globalVariables,
41
+ current.callContext.isStaticCall,
42
+ current.callContext.isDelegateCall,
43
+ current.args,
44
+ current.functionData.selector,
45
+ );
46
+ }
47
+
48
+ /** Temporary Method
49
+ *
50
+ * Convert the result of an AVM contract call to a PublicExecutionResult for the public kernel
51
+ *
52
+ * @param execution
53
+ * @param newWorldState
54
+ * @param result
55
+ * @returns
56
+ */
57
+ export function temporaryConvertAvmResults(
58
+ execution: PublicExecution,
59
+ newWorldState: JournalData,
60
+ result: AvmContractCallResults,
61
+ ): PublicExecutionResult {
62
+ const newCommitments = newWorldState.newNoteHashes.map(noteHash => new SideEffect(noteHash, Fr.zero()));
63
+
64
+ const contractStorageReads: ContractStorageRead[] = [];
65
+ const reduceStorageReadRequests = (contractAddress: bigint, storageReads: Map<bigint, Fr[]>) => {
66
+ return storageReads.forEach((innerArray, key) => {
67
+ innerArray.forEach(value => {
68
+ contractStorageReads.push(new ContractStorageRead(new Fr(key), new Fr(value), 0));
69
+ });
70
+ });
71
+ };
72
+ newWorldState.storageReads.forEach((storageMap: Map<bigint, Fr[]>, address: bigint) =>
73
+ reduceStorageReadRequests(address, storageMap),
74
+ );
75
+
76
+ const contractStorageUpdateRequests: ContractStorageUpdateRequest[] = [];
77
+ const reduceStorageUpdateRequests = (contractAddress: bigint, storageUpdateRequests: Map<bigint, Fr[]>) => {
78
+ return storageUpdateRequests.forEach((innerArray, key) => {
79
+ innerArray.forEach(value => {
80
+ contractStorageUpdateRequests.push(new ContractStorageUpdateRequest(new Fr(key), new Fr(value), 0));
81
+ });
82
+ });
83
+ };
84
+ newWorldState.storageWrites.forEach((storageMap: Map<bigint, Fr[]>, address: bigint) =>
85
+ reduceStorageUpdateRequests(address, storageMap),
86
+ );
87
+
88
+ const returnValues = result.output;
89
+
90
+ // TODO(follow up in pr tree): NOT SUPPORTED YET, make sure hashing and log resolution is done correctly
91
+ // Disabled.
92
+ const nestedExecutions: PublicExecutionResult[] = [];
93
+ const newNullifiers: SideEffectLinkedToNoteHash[] = [];
94
+ const unencryptedLogs = FunctionL2Logs.empty();
95
+ const newL2ToL1Messages = newWorldState.newL1Messages.map(() => Fr.zero());
96
+
97
+ return {
98
+ execution,
99
+ newCommitments,
100
+ newL2ToL1Messages,
101
+ newNullifiers,
102
+ contractStorageReads,
103
+ contractStorageUpdateRequests,
104
+ returnValues,
105
+ nestedExecutions,
106
+ unencryptedLogs,
107
+ };
108
+ }