@aztec/simulator 0.55.1 → 0.57.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.
- package/dest/acvm/oracle/oracle.d.ts +2 -5
- package/dest/acvm/oracle/oracle.d.ts.map +1 -1
- package/dest/acvm/oracle/oracle.js +6 -37
- package/dest/acvm/oracle/typed_oracle.d.ts +3 -6
- package/dest/acvm/oracle/typed_oracle.d.ts.map +1 -1
- package/dest/acvm/oracle/typed_oracle.js +1 -10
- package/dest/avm/avm_execution_environment.d.ts +2 -11
- package/dest/avm/avm_execution_environment.d.ts.map +1 -1
- package/dest/avm/avm_execution_environment.js +3 -22
- package/dest/avm/avm_gas.d.ts +2 -2
- package/dest/avm/avm_gas.d.ts.map +1 -1
- package/dest/avm/avm_gas.js +31 -116
- package/dest/avm/avm_machine_state.d.ts.map +1 -1
- package/dest/avm/avm_machine_state.js +6 -6
- package/dest/avm/avm_memory_types.d.ts +38 -9
- package/dest/avm/avm_memory_types.d.ts.map +1 -1
- package/dest/avm/avm_memory_types.js +27 -13
- package/dest/avm/avm_simulator.d.ts.map +1 -1
- package/dest/avm/avm_simulator.js +5 -6
- package/dest/avm/bytecode_utils.d.ts +1 -2
- package/dest/avm/bytecode_utils.d.ts.map +1 -1
- package/dest/avm/bytecode_utils.js +3 -17
- package/dest/avm/errors.d.ts +1 -1
- package/dest/avm/errors.d.ts.map +1 -1
- package/dest/avm/errors.js +12 -3
- package/dest/avm/fixtures/index.d.ts +4 -16
- package/dest/avm/fixtures/index.d.ts.map +1 -1
- package/dest/avm/fixtures/index.js +11 -27
- package/dest/avm/journal/index.d.ts +0 -1
- package/dest/avm/journal/index.d.ts.map +1 -1
- package/dest/avm/journal/index.js +1 -2
- package/dest/avm/journal/journal.d.ts +6 -5
- package/dest/avm/journal/journal.d.ts.map +1 -1
- package/dest/avm/journal/journal.js +18 -16
- package/dest/avm/opcodes/accrued_substate.d.ts.map +1 -1
- package/dest/avm/opcodes/accrued_substate.js +37 -30
- package/dest/avm/opcodes/addressing_mode.d.ts +3 -2
- package/dest/avm/opcodes/addressing_mode.d.ts.map +1 -1
- package/dest/avm/opcodes/addressing_mode.js +25 -21
- package/dest/avm/opcodes/arithmetic.d.ts.map +1 -1
- package/dest/avm/opcodes/arithmetic.js +6 -5
- package/dest/avm/opcodes/bitwise.d.ts +10 -4
- package/dest/avm/opcodes/bitwise.d.ts.map +1 -1
- package/dest/avm/opcodes/bitwise.js +26 -18
- package/dest/avm/opcodes/commitment.d.ts.map +1 -1
- package/dest/avm/opcodes/commitment.js +6 -5
- package/dest/avm/opcodes/comparators.d.ts.map +1 -1
- package/dest/avm/opcodes/comparators.js +8 -7
- package/dest/avm/opcodes/contract.d.ts.map +1 -1
- package/dest/avm/opcodes/contract.js +6 -5
- package/dest/avm/opcodes/control_flow.js +6 -6
- package/dest/avm/opcodes/conversion.d.ts +3 -2
- package/dest/avm/opcodes/conversion.d.ts.map +1 -1
- package/dest/avm/opcodes/conversion.js +21 -13
- package/dest/avm/opcodes/ec_add.d.ts.map +1 -1
- package/dest/avm/opcodes/ec_add.js +8 -7
- package/dest/avm/opcodes/environment_getters.d.ts +27 -63
- package/dest/avm/opcodes/environment_getters.d.ts.map +1 -1
- package/dest/avm/opcodes/environment_getters.js +76 -80
- package/dest/avm/opcodes/external_calls.d.ts.map +1 -1
- package/dest/avm/opcodes/external_calls.js +27 -16
- package/dest/avm/opcodes/hashing.d.ts +5 -5
- package/dest/avm/opcodes/hashing.d.ts.map +1 -1
- package/dest/avm/opcodes/hashing.js +47 -40
- package/dest/avm/opcodes/index.d.ts +3 -0
- package/dest/avm/opcodes/index.d.ts.map +1 -1
- package/dest/avm/opcodes/index.js +4 -1
- package/dest/avm/opcodes/instruction.d.ts +2 -6
- package/dest/avm/opcodes/instruction.d.ts.map +1 -1
- package/dest/avm/opcodes/instruction.js +3 -9
- package/dest/avm/opcodes/instruction_impl.d.ts +0 -26
- package/dest/avm/opcodes/instruction_impl.d.ts.map +1 -1
- package/dest/avm/opcodes/instruction_impl.js +1 -51
- package/dest/avm/opcodes/memory.d.ts +5 -12
- package/dest/avm/opcodes/memory.d.ts.map +1 -1
- package/dest/avm/opcodes/memory.js +40 -53
- package/dest/avm/opcodes/misc.d.ts.map +1 -1
- package/dest/avm/opcodes/misc.js +6 -5
- package/dest/avm/opcodes/multi_scalar_mul.d.ts.map +1 -1
- package/dest/avm/opcodes/multi_scalar_mul.js +17 -16
- package/dest/avm/opcodes/storage.d.ts.map +1 -1
- package/dest/avm/opcodes/storage.js +11 -9
- package/dest/avm/serialization/bytecode_serialization.d.ts.map +1 -1
- package/dest/avm/serialization/bytecode_serialization.js +5 -25
- package/dest/avm/serialization/instruction_serialization.d.ts +40 -54
- package/dest/avm/serialization/instruction_serialization.d.ts.map +1 -1
- package/dest/avm/serialization/instruction_serialization.js +41 -57
- package/dest/avm/test_utils.d.ts +8 -8
- package/dest/avm/test_utils.d.ts.map +1 -1
- package/dest/avm/test_utils.js +15 -15
- package/dest/client/client_execution_context.d.ts +6 -30
- package/dest/client/client_execution_context.d.ts.map +1 -1
- package/dest/client/client_execution_context.js +31 -53
- package/dest/client/index.d.ts +0 -1
- package/dest/client/index.d.ts.map +1 -1
- package/dest/client/index.js +1 -2
- package/dest/client/private_execution.d.ts +2 -2
- package/dest/client/private_execution.d.ts.map +1 -1
- package/dest/client/private_execution.js +5 -19
- package/dest/client/simulator.d.ts +3 -4
- package/dest/client/simulator.d.ts.map +1 -1
- package/dest/client/simulator.js +1 -1
- package/dest/client/test_utils.d.ts.map +1 -1
- package/dest/client/test_utils.js +12 -6
- package/dest/client/unconstrained_execution.d.ts +2 -2
- package/dest/client/unconstrained_execution.d.ts.map +1 -1
- package/dest/client/unconstrained_execution.js +3 -3
- package/dest/common/debug_fn_name.d.ts +4 -0
- package/dest/common/debug_fn_name.d.ts.map +1 -0
- package/dest/common/debug_fn_name.js +15 -0
- package/dest/common/index.d.ts +0 -1
- package/dest/common/index.d.ts.map +1 -1
- package/dest/common/index.js +1 -2
- package/dest/public/enqueued_call_simulator.d.ts +43 -0
- package/dest/public/enqueued_call_simulator.d.ts.map +1 -0
- package/dest/public/enqueued_call_simulator.js +154 -0
- package/dest/public/enqueued_calls_processor.d.ts +44 -0
- package/dest/public/enqueued_calls_processor.d.ts.map +1 -0
- package/dest/public/enqueued_calls_processor.js +218 -0
- package/dest/public/execution.d.ts +10 -8
- package/dest/public/execution.d.ts.map +1 -1
- package/dest/public/execution.js +15 -8
- package/dest/public/executor.d.ts +4 -7
- package/dest/public/executor.d.ts.map +1 -1
- package/dest/public/executor.js +10 -14
- package/dest/public/hints_builder.d.ts +4 -3
- package/dest/public/hints_builder.d.ts.map +1 -1
- package/dest/public/hints_builder.js +5 -5
- package/dest/public/index.d.ts +3 -2
- package/dest/public/index.d.ts.map +1 -1
- package/dest/public/index.js +3 -3
- package/dest/public/public_db_sources.d.ts +17 -24
- package/dest/public/public_db_sources.d.ts.map +1 -1
- package/dest/public/public_db_sources.js +58 -65
- package/dest/public/public_kernel.d.ts +3 -9
- package/dest/public/public_kernel.d.ts.map +1 -1
- package/dest/public/public_kernel.js +13 -40
- package/dest/public/public_kernel_circuit_simulator.d.ts +5 -11
- package/dest/public/public_kernel_circuit_simulator.d.ts.map +1 -1
- package/dest/public/public_kernel_tail_simulator.d.ts +19 -0
- package/dest/public/public_kernel_tail_simulator.d.ts.map +1 -0
- package/dest/public/public_kernel_tail_simulator.js +45 -0
- package/dest/public/public_processor.d.ts +9 -5
- package/dest/public/public_processor.d.ts.map +1 -1
- package/dest/public/public_processor.js +34 -48
- package/dest/public/public_processor_metrics.d.ts +3 -3
- package/dest/public/public_processor_metrics.d.ts.map +1 -1
- package/dest/public/public_processor_metrics.js +1 -1
- package/dest/public/side_effect_errors.d.ts +4 -0
- package/dest/public/side_effect_errors.d.ts.map +1 -0
- package/dest/public/side_effect_errors.js +7 -0
- package/dest/public/side_effect_trace.d.ts +2 -2
- package/dest/public/side_effect_trace.d.ts.map +1 -1
- package/dest/public/side_effect_trace.js +56 -35
- package/dest/public/side_effect_trace_interface.d.ts +1 -1
- package/dest/public/side_effect_trace_interface.d.ts.map +1 -1
- package/package.json +12 -9
- package/src/acvm/oracle/oracle.ts +7 -85
- package/src/acvm/oracle/typed_oracle.ts +2 -31
- package/src/avm/avm_execution_environment.ts +2 -24
- package/src/avm/avm_gas.ts +30 -115
- package/src/avm/avm_machine_state.ts +5 -5
- package/src/avm/avm_memory_types.ts +41 -14
- package/src/avm/avm_simulator.ts +4 -5
- package/src/avm/bytecode_utils.ts +2 -17
- package/src/avm/errors.ts +11 -3
- package/src/avm/fixtures/index.ts +13 -46
- package/src/avm/journal/index.ts +0 -1
- package/src/avm/journal/journal.ts +22 -22
- package/src/avm/opcodes/accrued_substate.ts +36 -44
- package/src/avm/opcodes/addressing_mode.ts +27 -24
- package/src/avm/opcodes/arithmetic.ts +5 -7
- package/src/avm/opcodes/bitwise.ts +23 -20
- package/src/avm/opcodes/commitment.ts +6 -7
- package/src/avm/opcodes/comparators.ts +7 -9
- package/src/avm/opcodes/contract.ts +5 -7
- package/src/avm/opcodes/control_flow.ts +5 -5
- package/src/avm/opcodes/conversion.ts +19 -12
- package/src/avm/opcodes/ec_add.ts +14 -16
- package/src/avm/opcodes/environment_getters.ts +73 -99
- package/src/avm/opcodes/external_calls.ts +27 -20
- package/src/avm/opcodes/hashing.ts +52 -52
- package/src/avm/opcodes/index.ts +3 -0
- package/src/avm/opcodes/instruction.ts +3 -10
- package/src/avm/opcodes/instruction_impl.ts +0 -62
- package/src/avm/opcodes/memory.ts +37 -67
- package/src/avm/opcodes/misc.ts +5 -7
- package/src/avm/opcodes/multi_scalar_mul.ts +16 -19
- package/src/avm/opcodes/storage.ts +10 -8
- package/src/avm/serialization/bytecode_serialization.ts +11 -35
- package/src/avm/serialization/instruction_serialization.ts +3 -19
- package/src/avm/test_utils.ts +20 -16
- package/src/client/client_execution_context.ts +40 -96
- package/src/client/index.ts +0 -1
- package/src/client/private_execution.ts +9 -9
- package/src/client/simulator.ts +2 -3
- package/src/client/test_utils.ts +36 -9
- package/src/client/unconstrained_execution.ts +3 -8
- package/src/common/debug_fn_name.ts +22 -0
- package/src/common/index.ts +0 -1
- package/src/public/enqueued_call_simulator.ts +335 -0
- package/src/public/enqueued_calls_processor.ts +388 -0
- package/src/public/execution.ts +16 -12
- package/src/public/executor.ts +8 -22
- package/src/public/hints_builder.ts +21 -21
- package/src/public/index.ts +3 -2
- package/src/public/public_db_sources.ts +62 -67
- package/src/public/public_kernel.ts +15 -49
- package/src/public/public_kernel_circuit_simulator.ts +6 -10
- package/src/public/public_kernel_tail_simulator.ts +106 -0
- package/src/public/public_processor.ts +65 -86
- package/src/public/public_processor_metrics.ts +3 -3
- package/src/public/side_effect_errors.ts +6 -0
- package/src/public/side_effect_trace.ts +77 -39
- package/src/public/side_effect_trace_interface.ts +2 -2
- package/dest/avm/journal/host_storage.d.ts +0 -13
- package/dest/avm/journal/host_storage.d.ts.map +0 -1
- package/dest/avm/journal/host_storage.js +0 -13
- package/dest/avm/opcodes/context_getters.d.ts +0 -15
- package/dest/avm/opcodes/context_getters.d.ts.map +0 -1
- package/dest/avm/opcodes/context_getters.js +0 -19
- package/dest/client/execution_result.d.ts +0 -104
- package/dest/client/execution_result.d.ts.map +0 -1
- package/dest/client/execution_result.js +0 -136
- package/dest/common/return_values.d.ts +0 -11
- package/dest/common/return_values.d.ts.map +0 -1
- package/dest/common/return_values.js +0 -13
- package/dest/public/abstract_phase_manager.d.ts +0 -93
- package/dest/public/abstract_phase_manager.d.ts.map +0 -1
- package/dest/public/abstract_phase_manager.js +0 -285
- package/dest/public/app_logic_phase_manager.d.ts +0 -25
- package/dest/public/app_logic_phase_manager.d.ts.map +0 -1
- package/dest/public/app_logic_phase_manager.js +0 -52
- package/dest/public/phase_manager_factory.d.ts +0 -18
- package/dest/public/phase_manager_factory.d.ts.map +0 -1
- package/dest/public/phase_manager_factory.js +0 -56
- package/dest/public/setup_phase_manager.d.ts +0 -25
- package/dest/public/setup_phase_manager.d.ts.map +0 -1
- package/dest/public/setup_phase_manager.js +0 -39
- package/dest/public/tail_phase_manager.d.ts +0 -23
- package/dest/public/tail_phase_manager.d.ts.map +0 -1
- package/dest/public/tail_phase_manager.js +0 -50
- package/dest/public/teardown_phase_manager.d.ts +0 -27
- package/dest/public/teardown_phase_manager.d.ts.map +0 -1
- package/dest/public/teardown_phase_manager.js +0 -56
- package/src/avm/journal/host_storage.ts +0 -14
- package/src/avm/opcodes/context_getters.ts +0 -23
- package/src/client/execution_result.ts +0 -228
- package/src/common/return_values.ts +0 -18
- package/src/public/abstract_phase_manager.ts +0 -522
- package/src/public/app_logic_phase_manager.ts +0 -81
- package/src/public/phase_manager_factory.ts +0 -126
- package/src/public/setup_phase_manager.ts +0 -61
- package/src/public/tail_phase_manager.ts +0 -127
- package/src/public/teardown_phase_manager.ts +0 -86
package/src/avm/avm_gas.ts
CHANGED
|
@@ -53,10 +53,10 @@ function makeCost(l2Gas: number, daGas: number): Gas {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
/** Dimensions of gas usage: L1, L2, and DA. */
|
|
56
|
-
export const
|
|
56
|
+
export const GAS_DIMENSIONS = ['l2Gas', 'daGas'] as const;
|
|
57
57
|
|
|
58
58
|
/** Base gas costs for each instruction. Additional gas cost may be added on top due to memory or storage accesses, etc. */
|
|
59
|
-
const
|
|
59
|
+
const BASE_GAS_COSTS: Record<Opcode, Gas> = {
|
|
60
60
|
[Opcode.ADD_8]: makeCost(c.AVM_ADD_BASE_L2_GAS, 0),
|
|
61
61
|
[Opcode.ADD_16]: makeCost(c.AVM_ADD_BASE_L2_GAS, 0),
|
|
62
62
|
[Opcode.SUB_8]: makeCost(c.AVM_SUB_BASE_L2_GAS, 0),
|
|
@@ -87,20 +87,8 @@ const BaseGasCosts: Record<Opcode, Gas> = {
|
|
|
87
87
|
[Opcode.SHR_16]: makeCost(c.AVM_SHR_BASE_L2_GAS, 0),
|
|
88
88
|
[Opcode.CAST_8]: makeCost(c.AVM_CAST_BASE_L2_GAS, 0),
|
|
89
89
|
[Opcode.CAST_16]: makeCost(c.AVM_CAST_BASE_L2_GAS, 0),
|
|
90
|
-
[Opcode.
|
|
91
|
-
[Opcode.STORAGEADDRESS]: makeCost(c.AVM_STORAGEADDRESS_BASE_L2_GAS, 0),
|
|
92
|
-
[Opcode.SENDER]: makeCost(c.AVM_SENDER_BASE_L2_GAS, 0),
|
|
93
|
-
[Opcode.FEEPERL2GAS]: makeCost(c.AVM_FEEPERL2GAS_BASE_L2_GAS, 0),
|
|
94
|
-
[Opcode.FEEPERDAGAS]: makeCost(c.AVM_FEEPERDAGAS_BASE_L2_GAS, 0),
|
|
95
|
-
[Opcode.TRANSACTIONFEE]: makeCost(c.AVM_TRANSACTIONFEE_BASE_L2_GAS, 0),
|
|
96
|
-
[Opcode.FUNCTIONSELECTOR]: makeCost(c.AVM_FUNCTIONSELECTOR_BASE_L2_GAS, 0),
|
|
97
|
-
[Opcode.CHAINID]: makeCost(c.AVM_CHAINID_BASE_L2_GAS, 0),
|
|
98
|
-
[Opcode.VERSION]: makeCost(c.AVM_VERSION_BASE_L2_GAS, 0),
|
|
99
|
-
[Opcode.BLOCKNUMBER]: makeCost(c.AVM_BLOCKNUMBER_BASE_L2_GAS, 0),
|
|
100
|
-
[Opcode.TIMESTAMP]: makeCost(c.AVM_TIMESTAMP_BASE_L2_GAS, 0),
|
|
90
|
+
[Opcode.GETENVVAR_16]: makeCost(c.AVM_GETENVVAR_BASE_L2_GAS, 0),
|
|
101
91
|
[Opcode.CALLDATACOPY]: makeCost(c.AVM_CALLDATACOPY_BASE_L2_GAS, 0),
|
|
102
|
-
[Opcode.L2GASLEFT]: makeCost(c.AVM_L2GASLEFT_BASE_L2_GAS, 0),
|
|
103
|
-
[Opcode.DAGASLEFT]: makeCost(c.AVM_DAGASLEFT_BASE_L2_GAS, 0),
|
|
104
92
|
[Opcode.JUMP_16]: makeCost(c.AVM_JUMP_BASE_L2_GAS, 0),
|
|
105
93
|
[Opcode.JUMPI_16]: makeCost(c.AVM_JUMPI_BASE_L2_GAS, 0),
|
|
106
94
|
[Opcode.INTERNALCALL]: makeCost(c.AVM_INTERNALCALL_BASE_L2_GAS, 0),
|
|
@@ -113,16 +101,15 @@ const BaseGasCosts: Record<Opcode, Gas> = {
|
|
|
113
101
|
[Opcode.SET_FF]: makeCost(c.AVM_SET_BASE_L2_GAS, 0),
|
|
114
102
|
[Opcode.MOV_8]: makeCost(c.AVM_MOV_BASE_L2_GAS, 0),
|
|
115
103
|
[Opcode.MOV_16]: makeCost(c.AVM_MOV_BASE_L2_GAS, 0),
|
|
116
|
-
[Opcode.CMOV]: makeCost(c.AVM_CMOV_BASE_L2_GAS, 0),
|
|
117
104
|
[Opcode.SLOAD]: makeCost(c.AVM_SLOAD_BASE_L2_GAS, 0),
|
|
118
|
-
[Opcode.SSTORE]: makeCost(c.AVM_SSTORE_BASE_L2_GAS,
|
|
105
|
+
[Opcode.SSTORE]: makeCost(c.AVM_SSTORE_BASE_L2_GAS, c.AVM_SSTORE_BASE_DA_GAS),
|
|
119
106
|
[Opcode.NOTEHASHEXISTS]: makeCost(c.AVM_NOTEHASHEXISTS_BASE_L2_GAS, 0),
|
|
120
|
-
[Opcode.EMITNOTEHASH]: makeCost(c.AVM_EMITNOTEHASH_BASE_L2_GAS,
|
|
107
|
+
[Opcode.EMITNOTEHASH]: makeCost(c.AVM_EMITNOTEHASH_BASE_L2_GAS, c.AVM_EMITNOTEHASH_BASE_DA_GAS),
|
|
121
108
|
[Opcode.NULLIFIEREXISTS]: makeCost(c.AVM_NULLIFIEREXISTS_BASE_L2_GAS, 0),
|
|
122
|
-
[Opcode.EMITNULLIFIER]: makeCost(c.AVM_EMITNULLIFIER_BASE_L2_GAS,
|
|
109
|
+
[Opcode.EMITNULLIFIER]: makeCost(c.AVM_EMITNULLIFIER_BASE_L2_GAS, c.AVM_EMITNULLIFIER_BASE_DA_GAS),
|
|
123
110
|
[Opcode.L1TOL2MSGEXISTS]: makeCost(c.AVM_L1TOL2MSGEXISTS_BASE_L2_GAS, 0),
|
|
124
111
|
[Opcode.EMITUNENCRYPTEDLOG]: makeCost(c.AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS, 0),
|
|
125
|
-
[Opcode.SENDL2TOL1MSG]: makeCost(c.AVM_SENDL2TOL1MSG_BASE_L2_GAS,
|
|
112
|
+
[Opcode.SENDL2TOL1MSG]: makeCost(c.AVM_SENDL2TOL1MSG_BASE_L2_GAS, c.AVM_SENDL2TOL1MSG_BASE_DA_GAS),
|
|
126
113
|
[Opcode.GETCONTRACTINSTANCE]: makeCost(c.AVM_GETCONTRACTINSTANCE_BASE_L2_GAS, 0),
|
|
127
114
|
[Opcode.CALL]: makeCost(c.AVM_CALL_BASE_L2_GAS, 0),
|
|
128
115
|
[Opcode.STATICCALL]: makeCost(c.AVM_STATICCALL_BASE_L2_GAS, 0),
|
|
@@ -133,110 +120,37 @@ const BaseGasCosts: Record<Opcode, Gas> = {
|
|
|
133
120
|
[Opcode.DEBUGLOG]: makeCost(c.AVM_DEBUGLOG_BASE_L2_GAS, 0),
|
|
134
121
|
[Opcode.KECCAK]: makeCost(c.AVM_KECCAK_BASE_L2_GAS, 0),
|
|
135
122
|
[Opcode.POSEIDON2]: makeCost(c.AVM_POSEIDON2_BASE_L2_GAS, 0),
|
|
136
|
-
[Opcode.
|
|
123
|
+
[Opcode.SHA256COMPRESSION]: makeCost(c.AVM_SHA256COMPRESSION_BASE_L2_GAS, 0),
|
|
124
|
+
[Opcode.KECCAKF1600]: makeCost(c.AVM_KECCAKF1600_BASE_L2_GAS, 0),
|
|
137
125
|
[Opcode.PEDERSEN]: makeCost(c.AVM_PEDERSEN_BASE_L2_GAS, 0),
|
|
138
126
|
[Opcode.ECADD]: makeCost(c.AVM_ECADD_BASE_L2_GAS, 0),
|
|
139
127
|
[Opcode.MSM]: makeCost(c.AVM_MSM_BASE_L2_GAS, 0),
|
|
140
128
|
[Opcode.PEDERSENCOMMITMENT]: makeCost(c.AVM_PEDERSENCOMMITMENT_BASE_L2_GAS, 0),
|
|
141
129
|
[Opcode.TORADIXLE]: makeCost(c.AVM_TORADIXLE_BASE_L2_GAS, 0),
|
|
142
|
-
[Opcode.SHA256COMPRESSION]: makeCost(c.AVM_SHA256COMPRESSION_BASE_L2_GAS, 0),
|
|
143
|
-
[Opcode.KECCAKF1600]: makeCost(c.AVM_KECCAKF1600_BASE_L2_GAS, 0),
|
|
144
130
|
};
|
|
145
131
|
|
|
146
|
-
const
|
|
147
|
-
[Opcode.
|
|
148
|
-
[Opcode.
|
|
149
|
-
[Opcode.
|
|
150
|
-
[Opcode.
|
|
151
|
-
[Opcode.
|
|
152
|
-
[Opcode.
|
|
153
|
-
[Opcode.
|
|
154
|
-
[Opcode.
|
|
155
|
-
[Opcode.
|
|
156
|
-
[Opcode.
|
|
157
|
-
[Opcode.
|
|
158
|
-
[Opcode.
|
|
159
|
-
|
|
160
|
-
[Opcode.LT_16]: makeCost(c.AVM_LT_DYN_L2_GAS, 0),
|
|
161
|
-
[Opcode.LTE_8]: makeCost(c.AVM_LTE_DYN_L2_GAS, 0),
|
|
162
|
-
[Opcode.LTE_16]: makeCost(c.AVM_LTE_DYN_L2_GAS, 0),
|
|
163
|
-
[Opcode.AND_8]: makeCost(c.AVM_AND_DYN_L2_GAS, 0),
|
|
164
|
-
[Opcode.AND_16]: makeCost(c.AVM_AND_DYN_L2_GAS, 0),
|
|
165
|
-
[Opcode.OR_8]: makeCost(c.AVM_OR_DYN_L2_GAS, 0),
|
|
166
|
-
[Opcode.OR_16]: makeCost(c.AVM_OR_DYN_L2_GAS, 0),
|
|
167
|
-
[Opcode.XOR_8]: makeCost(c.AVM_XOR_DYN_L2_GAS, 0),
|
|
168
|
-
[Opcode.XOR_16]: makeCost(c.AVM_XOR_DYN_L2_GAS, 0),
|
|
169
|
-
[Opcode.NOT_8]: makeCost(c.AVM_NOT_DYN_L2_GAS, 0),
|
|
170
|
-
[Opcode.NOT_16]: makeCost(c.AVM_NOT_DYN_L2_GAS, 0),
|
|
171
|
-
[Opcode.SHL_8]: makeCost(c.AVM_SHL_DYN_L2_GAS, 0),
|
|
172
|
-
[Opcode.SHL_16]: makeCost(c.AVM_SHL_DYN_L2_GAS, 0),
|
|
173
|
-
[Opcode.SHR_8]: makeCost(c.AVM_SHR_DYN_L2_GAS, 0),
|
|
174
|
-
[Opcode.SHR_16]: makeCost(c.AVM_SHR_DYN_L2_GAS, 0),
|
|
175
|
-
[Opcode.CAST_8]: makeCost(c.AVM_CAST_DYN_L2_GAS, 0),
|
|
176
|
-
[Opcode.CAST_16]: makeCost(c.AVM_CAST_DYN_L2_GAS, 0),
|
|
177
|
-
[Opcode.ADDRESS]: makeCost(c.AVM_ADDRESS_DYN_L2_GAS, 0),
|
|
178
|
-
[Opcode.STORAGEADDRESS]: makeCost(c.AVM_STORAGEADDRESS_DYN_L2_GAS, 0),
|
|
179
|
-
[Opcode.SENDER]: makeCost(c.AVM_SENDER_DYN_L2_GAS, 0),
|
|
180
|
-
[Opcode.FEEPERL2GAS]: makeCost(c.AVM_FEEPERL2GAS_DYN_L2_GAS, 0),
|
|
181
|
-
[Opcode.FEEPERDAGAS]: makeCost(c.AVM_FEEPERDAGAS_DYN_L2_GAS, 0),
|
|
182
|
-
[Opcode.TRANSACTIONFEE]: makeCost(c.AVM_TRANSACTIONFEE_DYN_L2_GAS, 0),
|
|
183
|
-
[Opcode.FUNCTIONSELECTOR]: makeCost(c.AVM_FUNCTIONSELECTOR_DYN_L2_GAS, 0),
|
|
184
|
-
[Opcode.CHAINID]: makeCost(c.AVM_CHAINID_DYN_L2_GAS, 0),
|
|
185
|
-
[Opcode.VERSION]: makeCost(c.AVM_VERSION_DYN_L2_GAS, 0),
|
|
186
|
-
[Opcode.BLOCKNUMBER]: makeCost(c.AVM_BLOCKNUMBER_DYN_L2_GAS, 0),
|
|
187
|
-
[Opcode.TIMESTAMP]: makeCost(c.AVM_TIMESTAMP_DYN_L2_GAS, 0),
|
|
188
|
-
[Opcode.CALLDATACOPY]: makeCost(c.AVM_CALLDATACOPY_DYN_L2_GAS, 0),
|
|
189
|
-
[Opcode.L2GASLEFT]: makeCost(c.AVM_L2GASLEFT_DYN_L2_GAS, 0),
|
|
190
|
-
[Opcode.DAGASLEFT]: makeCost(c.AVM_DAGASLEFT_DYN_L2_GAS, 0),
|
|
191
|
-
[Opcode.JUMP_16]: makeCost(c.AVM_JUMP_DYN_L2_GAS, 0),
|
|
192
|
-
[Opcode.JUMPI_16]: makeCost(c.AVM_JUMPI_DYN_L2_GAS, 0),
|
|
193
|
-
[Opcode.INTERNALCALL]: makeCost(c.AVM_INTERNALCALL_DYN_L2_GAS, 0),
|
|
194
|
-
[Opcode.INTERNALRETURN]: makeCost(c.AVM_INTERNALRETURN_DYN_L2_GAS, 0),
|
|
195
|
-
[Opcode.SET_8]: makeCost(c.AVM_SET_DYN_L2_GAS, 0),
|
|
196
|
-
[Opcode.SET_16]: makeCost(c.AVM_SET_DYN_L2_GAS, 0),
|
|
197
|
-
[Opcode.SET_32]: makeCost(c.AVM_SET_DYN_L2_GAS, 0),
|
|
198
|
-
[Opcode.SET_64]: makeCost(c.AVM_SET_DYN_L2_GAS, 0),
|
|
199
|
-
[Opcode.SET_128]: makeCost(c.AVM_SET_DYN_L2_GAS, 0),
|
|
200
|
-
[Opcode.SET_FF]: makeCost(c.AVM_SET_DYN_L2_GAS, 0),
|
|
201
|
-
[Opcode.MOV_8]: makeCost(c.AVM_MOV_DYN_L2_GAS, 0),
|
|
202
|
-
[Opcode.MOV_16]: makeCost(c.AVM_MOV_DYN_L2_GAS, 0),
|
|
203
|
-
[Opcode.CMOV]: makeCost(c.AVM_CMOV_DYN_L2_GAS, 0),
|
|
204
|
-
[Opcode.SLOAD]: makeCost(c.AVM_SLOAD_DYN_L2_GAS, 0),
|
|
205
|
-
[Opcode.SSTORE]: makeCost(c.AVM_SSTORE_DYN_L2_GAS, 0),
|
|
206
|
-
[Opcode.NOTEHASHEXISTS]: makeCost(c.AVM_NOTEHASHEXISTS_DYN_L2_GAS, 0),
|
|
207
|
-
[Opcode.EMITNOTEHASH]: makeCost(c.AVM_EMITNOTEHASH_DYN_L2_GAS, 0),
|
|
208
|
-
[Opcode.NULLIFIEREXISTS]: makeCost(c.AVM_NULLIFIEREXISTS_DYN_L2_GAS, 0),
|
|
209
|
-
[Opcode.EMITNULLIFIER]: makeCost(c.AVM_EMITNULLIFIER_DYN_L2_GAS, 0),
|
|
210
|
-
[Opcode.L1TOL2MSGEXISTS]: makeCost(c.AVM_L1TOL2MSGEXISTS_DYN_L2_GAS, 0),
|
|
211
|
-
[Opcode.EMITUNENCRYPTEDLOG]: makeCost(c.AVM_EMITUNENCRYPTEDLOG_DYN_L2_GAS, 0),
|
|
212
|
-
[Opcode.SENDL2TOL1MSG]: makeCost(c.AVM_SENDL2TOL1MSG_DYN_L2_GAS, 0),
|
|
213
|
-
[Opcode.GETCONTRACTINSTANCE]: makeCost(c.AVM_GETCONTRACTINSTANCE_DYN_L2_GAS, 0),
|
|
214
|
-
[Opcode.CALL]: makeCost(c.AVM_CALL_DYN_L2_GAS, 0),
|
|
215
|
-
[Opcode.STATICCALL]: makeCost(c.AVM_STATICCALL_DYN_L2_GAS, 0),
|
|
216
|
-
[Opcode.DELEGATECALL]: makeCost(c.AVM_DELEGATECALL_DYN_L2_GAS, 0),
|
|
217
|
-
[Opcode.RETURN]: makeCost(c.AVM_RETURN_DYN_L2_GAS, 0),
|
|
218
|
-
[Opcode.REVERT_8]: makeCost(c.AVM_REVERT_DYN_L2_GAS, 0),
|
|
219
|
-
[Opcode.REVERT_16]: makeCost(c.AVM_REVERT_DYN_L2_GAS, 0),
|
|
220
|
-
[Opcode.DEBUGLOG]: makeCost(c.AVM_DEBUGLOG_DYN_L2_GAS, 0),
|
|
221
|
-
[Opcode.KECCAK]: makeCost(c.AVM_KECCAK_DYN_L2_GAS, 0),
|
|
222
|
-
[Opcode.POSEIDON2]: makeCost(c.AVM_POSEIDON2_DYN_L2_GAS, 0),
|
|
223
|
-
[Opcode.SHA256]: makeCost(c.AVM_SHA256_DYN_L2_GAS, 0),
|
|
224
|
-
[Opcode.PEDERSEN]: makeCost(c.AVM_PEDERSEN_DYN_L2_GAS, 0),
|
|
225
|
-
[Opcode.ECADD]: makeCost(c.AVM_ECADD_DYN_L2_GAS, 0),
|
|
226
|
-
[Opcode.MSM]: makeCost(c.AVM_MSM_DYN_L2_GAS, 0),
|
|
227
|
-
[Opcode.PEDERSENCOMMITMENT]: makeCost(c.AVM_PEDERSENCOMMITMENT_DYN_L2_GAS, 0),
|
|
228
|
-
[Opcode.TORADIXLE]: makeCost(c.AVM_TORADIXLE_DYN_L2_GAS, 0),
|
|
229
|
-
[Opcode.SHA256COMPRESSION]: makeCost(c.AVM_SHA256COMPRESSION_DYN_L2_GAS, 0),
|
|
230
|
-
[Opcode.KECCAKF1600]: makeCost(c.AVM_KECCAKF1600_DYN_L2_GAS, 0),
|
|
231
|
-
};
|
|
132
|
+
const DYNAMIC_GAS_COSTS = new Map<Opcode, Gas>([
|
|
133
|
+
[Opcode.CALLDATACOPY, makeCost(c.AVM_CALLDATACOPY_DYN_L2_GAS, 0)],
|
|
134
|
+
[Opcode.EMITUNENCRYPTEDLOG, makeCost(c.AVM_EMITUNENCRYPTEDLOG_DYN_L2_GAS, c.AVM_EMITUNENCRYPTEDLOG_DYN_DA_GAS)],
|
|
135
|
+
[Opcode.CALL, makeCost(c.AVM_CALL_DYN_L2_GAS, 0)],
|
|
136
|
+
[Opcode.STATICCALL, makeCost(c.AVM_STATICCALL_DYN_L2_GAS, 0)],
|
|
137
|
+
[Opcode.DELEGATECALL, makeCost(c.AVM_DELEGATECALL_DYN_L2_GAS, 0)],
|
|
138
|
+
[Opcode.RETURN, makeCost(c.AVM_RETURN_DYN_L2_GAS, 0)],
|
|
139
|
+
[Opcode.REVERT_8, makeCost(c.AVM_REVERT_DYN_L2_GAS, 0)],
|
|
140
|
+
[Opcode.REVERT_16, makeCost(c.AVM_REVERT_DYN_L2_GAS, 0)],
|
|
141
|
+
[Opcode.PEDERSEN, makeCost(c.AVM_PEDERSEN_DYN_L2_GAS, 0)],
|
|
142
|
+
[Opcode.MSM, makeCost(c.AVM_MSM_DYN_L2_GAS, 0)],
|
|
143
|
+
[Opcode.PEDERSENCOMMITMENT, makeCost(c.AVM_PEDERSENCOMMITMENT_DYN_L2_GAS, 0)],
|
|
144
|
+
[Opcode.TORADIXLE, makeCost(c.AVM_TORADIXLE_DYN_L2_GAS, 0)],
|
|
145
|
+
]);
|
|
232
146
|
|
|
233
147
|
/** Returns the fixed base gas cost for a given opcode. */
|
|
234
148
|
export function getBaseGasCost(opcode: Opcode): Gas {
|
|
235
|
-
return
|
|
149
|
+
return BASE_GAS_COSTS[opcode];
|
|
236
150
|
}
|
|
237
151
|
|
|
238
152
|
export function getDynamicGasCost(opcode: Opcode): Gas {
|
|
239
|
-
return
|
|
153
|
+
return DYNAMIC_GAS_COSTS.has(opcode) ? DYNAMIC_GAS_COSTS.get(opcode)! : makeCost(0, 0);
|
|
240
154
|
}
|
|
241
155
|
|
|
242
156
|
/** Returns the gas cost associated with the memory operations performed. */
|
|
@@ -244,14 +158,14 @@ export function getMemoryGasCost(args: { reads?: number; writes?: number; indire
|
|
|
244
158
|
const { reads, writes, indirect } = args;
|
|
245
159
|
const indirectCount = Addressing.fromWire(indirect ?? 0).count(AddressingMode.INDIRECT);
|
|
246
160
|
const l2MemoryGasCost =
|
|
247
|
-
(reads ?? 0) *
|
|
248
|
-
(writes ?? 0) *
|
|
249
|
-
indirectCount *
|
|
161
|
+
(reads ?? 0) * GAS_COST_CONSTANTS.MEMORY_READ +
|
|
162
|
+
(writes ?? 0) * GAS_COST_CONSTANTS.MEMORY_WRITE +
|
|
163
|
+
indirectCount * GAS_COST_CONSTANTS.MEMORY_INDIRECT_READ_PENALTY;
|
|
250
164
|
return makeGas({ l2Gas: l2MemoryGasCost });
|
|
251
165
|
}
|
|
252
166
|
|
|
253
167
|
/** Constants used in base cost calculations. */
|
|
254
|
-
export const
|
|
168
|
+
export const GAS_COST_CONSTANTS = {
|
|
255
169
|
MEMORY_READ: 10,
|
|
256
170
|
MEMORY_INDIRECT_READ_PENALTY: 10,
|
|
257
171
|
MEMORY_WRITE: 100,
|
|
@@ -265,6 +179,7 @@ export function getGasCostForTypeTag(tag: TypeTag, baseCost: Gas) {
|
|
|
265
179
|
/** Returns a multiplier based on the size of the type represented by the tag. Throws on uninitialized or invalid. */
|
|
266
180
|
function getGasCostMultiplierFromTypeTag(tag: TypeTag) {
|
|
267
181
|
switch (tag) {
|
|
182
|
+
case TypeTag.UINT1: // same as u8
|
|
268
183
|
case TypeTag.UINT8:
|
|
269
184
|
return 1;
|
|
270
185
|
case TypeTag.UINT16:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Fr } from '@aztec/circuits.js';
|
|
2
2
|
|
|
3
|
-
import { type Gas
|
|
3
|
+
import { GAS_DIMENSIONS, type Gas } from './avm_gas.js';
|
|
4
4
|
import { TaggedMemory } from './avm_memory_types.js';
|
|
5
5
|
import { OutOfGasError } from './errors.js';
|
|
6
6
|
|
|
@@ -67,7 +67,7 @@ export class AvmMachineState {
|
|
|
67
67
|
*/
|
|
68
68
|
public consumeGas(gasCost: Partial<Gas>) {
|
|
69
69
|
// Assert there is enough gas on every dimension.
|
|
70
|
-
const outOfGasDimensions =
|
|
70
|
+
const outOfGasDimensions = GAS_DIMENSIONS.filter(
|
|
71
71
|
dimension => this[`${dimension}Left`] - (gasCost[dimension] ?? 0) < 0,
|
|
72
72
|
);
|
|
73
73
|
// If not, trigger an exceptional halt.
|
|
@@ -77,14 +77,14 @@ export class AvmMachineState {
|
|
|
77
77
|
throw new OutOfGasError(outOfGasDimensions);
|
|
78
78
|
}
|
|
79
79
|
// Otherwise, charge the corresponding gas
|
|
80
|
-
for (const dimension of
|
|
80
|
+
for (const dimension of GAS_DIMENSIONS) {
|
|
81
81
|
this[`${dimension}Left`] -= gasCost[dimension] ?? 0;
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
/** Increases the gas left by the amounts specified. */
|
|
86
86
|
public refundGas(gasRefund: Partial<Gas>) {
|
|
87
|
-
for (const dimension of
|
|
87
|
+
for (const dimension of GAS_DIMENSIONS) {
|
|
88
88
|
this[`${dimension}Left`] += gasRefund[dimension] ?? 0;
|
|
89
89
|
}
|
|
90
90
|
}
|
|
@@ -133,7 +133,7 @@ export class AvmMachineState {
|
|
|
133
133
|
* Flag an exceptional halt. Clears gas left and sets the reverted flag. No output data.
|
|
134
134
|
*/
|
|
135
135
|
private exceptionalHalt() {
|
|
136
|
-
|
|
136
|
+
GAS_DIMENSIONS.forEach(dimension => (this[`${dimension}Left`] = 0));
|
|
137
137
|
this.reverted = true;
|
|
138
138
|
this.halted = true;
|
|
139
139
|
}
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MEM_TAG_FF,
|
|
3
|
+
MEM_TAG_U1,
|
|
4
|
+
MEM_TAG_U8,
|
|
5
|
+
MEM_TAG_U16,
|
|
6
|
+
MEM_TAG_U32,
|
|
7
|
+
MEM_TAG_U64,
|
|
8
|
+
MEM_TAG_U128,
|
|
9
|
+
} from '@aztec/circuits.js';
|
|
1
10
|
import { toBufferBE } from '@aztec/foundation/bigint-buffer';
|
|
2
11
|
import { Fr } from '@aztec/foundation/fields';
|
|
3
12
|
import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';
|
|
@@ -128,6 +137,9 @@ function UnsignedIntegerClassFactory(bits: number) {
|
|
|
128
137
|
}
|
|
129
138
|
|
|
130
139
|
public toBuffer(): Buffer {
|
|
140
|
+
if (bits < 8) {
|
|
141
|
+
return toBufferBE(this.n, 1);
|
|
142
|
+
}
|
|
131
143
|
return toBufferBE(this.n, bits / 8);
|
|
132
144
|
}
|
|
133
145
|
};
|
|
@@ -136,6 +148,7 @@ function UnsignedIntegerClassFactory(bits: number) {
|
|
|
136
148
|
// Now we can create the classes for each unsigned integer type.
|
|
137
149
|
// We extend instead of just assigning so that the class has the right name.
|
|
138
150
|
// Otherwise they are all called "NewUintClass".
|
|
151
|
+
export class Uint1 extends UnsignedIntegerClassFactory(1) {}
|
|
139
152
|
export class Uint8 extends UnsignedIntegerClassFactory(8) {}
|
|
140
153
|
export class Uint16 extends UnsignedIntegerClassFactory(16) {}
|
|
141
154
|
export class Uint32 extends UnsignedIntegerClassFactory(32) {}
|
|
@@ -196,12 +209,13 @@ export class Field extends MemoryValue {
|
|
|
196
209
|
|
|
197
210
|
export enum TypeTag {
|
|
198
211
|
UNINITIALIZED,
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
212
|
+
UINT1 = MEM_TAG_U1,
|
|
213
|
+
UINT8 = MEM_TAG_U8,
|
|
214
|
+
UINT16 = MEM_TAG_U16,
|
|
215
|
+
UINT32 = MEM_TAG_U32,
|
|
216
|
+
UINT64 = MEM_TAG_U64,
|
|
217
|
+
UINT128 = MEM_TAG_U128,
|
|
218
|
+
FIELD = MEM_TAG_FF,
|
|
205
219
|
INVALID,
|
|
206
220
|
}
|
|
207
221
|
|
|
@@ -302,7 +316,9 @@ export class TaggedMemory implements TaggedMemoryInterface {
|
|
|
302
316
|
}
|
|
303
317
|
|
|
304
318
|
public static checkIsIntegralTag(tag: TypeTag) {
|
|
305
|
-
if (
|
|
319
|
+
if (
|
|
320
|
+
![TypeTag.UINT1, TypeTag.UINT8, TypeTag.UINT16, TypeTag.UINT32, TypeTag.UINT64, TypeTag.UINT128].includes(tag)
|
|
321
|
+
) {
|
|
306
322
|
throw TagCheckError.forTag(TypeTag[tag], 'integral');
|
|
307
323
|
}
|
|
308
324
|
}
|
|
@@ -332,8 +348,8 @@ export class TaggedMemory implements TaggedMemoryInterface {
|
|
|
332
348
|
|
|
333
349
|
if (v === undefined) {
|
|
334
350
|
tag = TypeTag.UNINITIALIZED;
|
|
335
|
-
} else if (v instanceof
|
|
336
|
-
tag = TypeTag.
|
|
351
|
+
} else if (v instanceof Uint1) {
|
|
352
|
+
tag = TypeTag.UINT1;
|
|
337
353
|
} else if (v instanceof Uint8) {
|
|
338
354
|
tag = TypeTag.UINT8;
|
|
339
355
|
} else if (v instanceof Uint16) {
|
|
@@ -344,6 +360,8 @@ export class TaggedMemory implements TaggedMemoryInterface {
|
|
|
344
360
|
tag = TypeTag.UINT64;
|
|
345
361
|
} else if (v instanceof Uint128) {
|
|
346
362
|
tag = TypeTag.UINT128;
|
|
363
|
+
} else if (v instanceof Field) {
|
|
364
|
+
tag = TypeTag.FIELD;
|
|
347
365
|
}
|
|
348
366
|
|
|
349
367
|
return tag;
|
|
@@ -353,6 +371,8 @@ export class TaggedMemory implements TaggedMemoryInterface {
|
|
|
353
371
|
public static buildFromTagTruncating(v: bigint | number, tag: TypeTag): MemoryValue {
|
|
354
372
|
v = BigInt(v);
|
|
355
373
|
switch (tag) {
|
|
374
|
+
case TypeTag.UINT1:
|
|
375
|
+
return new Uint1(v & 1n);
|
|
356
376
|
case TypeTag.UINT8:
|
|
357
377
|
return new Uint8(v & ((1n << 8n) - 1n));
|
|
358
378
|
case TypeTag.UINT16:
|
|
@@ -373,6 +393,8 @@ export class TaggedMemory implements TaggedMemoryInterface {
|
|
|
373
393
|
// Does not truncate. Type constructor will check that it fits.
|
|
374
394
|
public static buildFromTagOrDie(v: bigint | number, tag: TypeTag): MemoryValue {
|
|
375
395
|
switch (tag) {
|
|
396
|
+
case TypeTag.UINT1:
|
|
397
|
+
return new Uint1(v);
|
|
376
398
|
case TypeTag.UINT8:
|
|
377
399
|
return new Uint8(v);
|
|
378
400
|
case TypeTag.UINT16:
|
|
@@ -391,7 +413,7 @@ export class TaggedMemory implements TaggedMemoryInterface {
|
|
|
391
413
|
}
|
|
392
414
|
|
|
393
415
|
/** No-op. Implemented here for compatibility with the MeteredTaggedMemory. */
|
|
394
|
-
public assert(_operations: Partial<MemoryOperations & {
|
|
416
|
+
public assert(_operations: Partial<MemoryOperations & { addressing: Addressing }>) {}
|
|
395
417
|
}
|
|
396
418
|
|
|
397
419
|
/** Tagged memory wrapper with metering for each memory read and write operation. */
|
|
@@ -413,10 +435,15 @@ export class MeteredTaggedMemory implements TaggedMemoryInterface {
|
|
|
413
435
|
* Asserts that the exact number of memory operations have been performed.
|
|
414
436
|
* Indirect represents the flags for indirect accesses: each bit set to one counts as an extra read.
|
|
415
437
|
*/
|
|
416
|
-
public assert(operations: Partial<MemoryOperations & {
|
|
417
|
-
const {
|
|
418
|
-
|
|
419
|
-
|
|
438
|
+
public assert(operations: Partial<MemoryOperations & { addressing: Addressing }>) {
|
|
439
|
+
const {
|
|
440
|
+
reads: expectedReads,
|
|
441
|
+
writes: expectedWrites,
|
|
442
|
+
addressing,
|
|
443
|
+
} = { reads: 0, writes: 0, addressing: new Addressing([]), ...operations };
|
|
444
|
+
|
|
445
|
+
const totalExpectedReads =
|
|
446
|
+
expectedReads + addressing.count(AddressingMode.INDIRECT) + addressing.count(AddressingMode.RELATIVE);
|
|
420
447
|
const { reads: actualReads, writes: actualWrites } = this.reset();
|
|
421
448
|
if (actualReads !== totalExpectedReads) {
|
|
422
449
|
throw new InstructionExecutionError(
|
package/src/avm/avm_simulator.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { strict as assert } from 'assert';
|
|
|
4
4
|
|
|
5
5
|
import type { AvmContext } from './avm_context.js';
|
|
6
6
|
import { AvmContractCallResult } from './avm_contract_call_result.js';
|
|
7
|
-
import {
|
|
7
|
+
import { isAvmBytecode } from './bytecode_utils.js';
|
|
8
8
|
import {
|
|
9
9
|
AvmExecutionError,
|
|
10
10
|
InvalidProgramCounterError,
|
|
@@ -53,11 +53,10 @@ export class AvmSimulator {
|
|
|
53
53
|
* This method is useful for testing and debugging.
|
|
54
54
|
*/
|
|
55
55
|
public async executeBytecode(bytecode: Buffer): Promise<AvmContractCallResult> {
|
|
56
|
-
|
|
57
|
-
assert(await isAvmBytecode(decompressedBytecode), "AVM simulator can't execute non-AVM bytecode");
|
|
56
|
+
assert(isAvmBytecode(bytecode), "AVM simulator can't execute non-AVM bytecode");
|
|
58
57
|
|
|
59
|
-
this.bytecode =
|
|
60
|
-
return await this.executeInstructions(decodeFromBytecode(
|
|
58
|
+
this.bytecode = bytecode;
|
|
59
|
+
return await this.executeInstructions(decodeFromBytecode(bytecode));
|
|
61
60
|
}
|
|
62
61
|
|
|
63
62
|
/**
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { promisify } from 'util';
|
|
2
|
-
import { gunzip } from 'zlib';
|
|
3
|
-
|
|
4
1
|
import { Opcode } from './serialization/instruction_serialization.js';
|
|
5
2
|
|
|
6
3
|
const AVM_MAGIC_SUFFIX = Buffer.from([
|
|
@@ -14,19 +11,7 @@ export function markBytecodeAsAvm(bytecode: Buffer): Buffer {
|
|
|
14
11
|
return Buffer.concat([bytecode, AVM_MAGIC_SUFFIX]);
|
|
15
12
|
}
|
|
16
13
|
|
|
17
|
-
|
|
18
|
-
export async function decompressBytecodeIfCompressed(bytecode: Buffer): Promise<Buffer> {
|
|
19
|
-
try {
|
|
20
|
-
return await promisify(gunzip)(bytecode);
|
|
21
|
-
} catch {
|
|
22
|
-
// If the bytecode is not compressed, the gunzip call will throw an error
|
|
23
|
-
// In this case, we assume the bytecode is not compressed and continue.
|
|
24
|
-
return Promise.resolve(bytecode);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export async function isAvmBytecode(bytecode: Buffer): Promise<boolean> {
|
|
29
|
-
const decompressedBytecode = await decompressBytecodeIfCompressed(bytecode);
|
|
14
|
+
export function isAvmBytecode(bytecode: Buffer): boolean {
|
|
30
15
|
const magicSize = AVM_MAGIC_SUFFIX.length;
|
|
31
|
-
return
|
|
16
|
+
return bytecode.subarray(-magicSize).equals(AVM_MAGIC_SUFFIX);
|
|
32
17
|
}
|
package/src/avm/errors.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type FailingFunction, type NoirCallStack } from '@aztec/circuit-types';
|
|
2
|
-
import { type AztecAddress,
|
|
2
|
+
import { type AztecAddress, Fr, FunctionSelector, PUBLIC_DISPATCH_SELECTOR } from '@aztec/circuits.js';
|
|
3
3
|
|
|
4
4
|
import { ExecutionError } from '../common/errors.js';
|
|
5
5
|
import { type AvmContext } from './avm_context.js';
|
|
@@ -109,13 +109,21 @@ export class AvmRevertReason extends ExecutionError {
|
|
|
109
109
|
* @param nestedError - the error that caused this one (if this is not the root-cause itself)
|
|
110
110
|
*/
|
|
111
111
|
function createRevertReason(message: string, context: AvmContext, nestedError?: AvmRevertReason): AvmRevertReason {
|
|
112
|
+
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/8985): Properly fix this.
|
|
113
|
+
// If the function selector is the public dispatch selector, we need to extract the actual function selector from the calldata.
|
|
114
|
+
// We should remove this because the AVM (or public protocol) shouldn't be aware of the public dispatch calling convention.
|
|
115
|
+
let functionSelector = context.environment.functionSelector;
|
|
116
|
+
const internalCallStack = context.machineState.internalCallStack;
|
|
117
|
+
if (functionSelector.toField().equals(new Fr(PUBLIC_DISPATCH_SELECTOR)) && context.environment.calldata.length > 0) {
|
|
118
|
+
functionSelector = FunctionSelector.fromField(context.environment.calldata[0]);
|
|
119
|
+
}
|
|
112
120
|
return new AvmRevertReason(
|
|
113
121
|
message,
|
|
114
122
|
/*failingFunction=*/ {
|
|
115
123
|
contractAddress: context.environment.address,
|
|
116
|
-
functionSelector:
|
|
124
|
+
functionSelector: functionSelector,
|
|
117
125
|
},
|
|
118
|
-
/*noirCallStack=*/ [...
|
|
126
|
+
/*noirCallStack=*/ [...internalCallStack, context.machineState.pc].map(pc => `0.${pc}`),
|
|
119
127
|
/*options=*/ { cause: nestedError },
|
|
120
128
|
);
|
|
121
129
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isNoirCallStackUnresolved } from '@aztec/circuit-types';
|
|
2
|
-
import { GasFees, GlobalVariables
|
|
2
|
+
import { GasFees, GlobalVariables } from '@aztec/circuits.js';
|
|
3
3
|
import { FunctionSelector, getFunctionDebugMetadata } from '@aztec/foundation/abi';
|
|
4
4
|
import { AztecAddress } from '@aztec/foundation/aztec-address';
|
|
5
5
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
@@ -10,20 +10,13 @@ import { strict as assert } from 'assert';
|
|
|
10
10
|
import { mock } from 'jest-mock-extended';
|
|
11
11
|
import merge from 'lodash.merge';
|
|
12
12
|
|
|
13
|
-
import {
|
|
14
|
-
type CommitmentsDB,
|
|
15
|
-
type PublicContractsDB,
|
|
16
|
-
type PublicStateDB,
|
|
17
|
-
resolveAssertionMessage,
|
|
18
|
-
traverseCauseChain,
|
|
19
|
-
} from '../../index.js';
|
|
13
|
+
import { type WorldStateDB, resolveAssertionMessage, traverseCauseChain } from '../../index.js';
|
|
20
14
|
import { type PublicSideEffectTraceInterface } from '../../public/side_effect_trace_interface.js';
|
|
21
15
|
import { AvmContext } from '../avm_context.js';
|
|
22
|
-
import {
|
|
16
|
+
import { AvmExecutionEnvironment } from '../avm_execution_environment.js';
|
|
23
17
|
import { AvmMachineState } from '../avm_machine_state.js';
|
|
24
|
-
import { Field, Uint8, Uint64 } from '../avm_memory_types.js';
|
|
18
|
+
import { Field, Uint8, Uint32, Uint64 } from '../avm_memory_types.js';
|
|
25
19
|
import { type AvmRevertReason } from '../errors.js';
|
|
26
|
-
import { HostStorage } from '../journal/host_storage.js';
|
|
27
20
|
import { AvmPersistableStateManager } from '../journal/journal.js';
|
|
28
21
|
import { NullifierManager } from '../journal/nullifiers.js';
|
|
29
22
|
import { PublicStorage } from '../journal/public_storage.js';
|
|
@@ -43,32 +36,19 @@ export function initContext(overrides?: {
|
|
|
43
36
|
);
|
|
44
37
|
}
|
|
45
38
|
|
|
46
|
-
/** Creates an empty host storage with mocked dbs. */
|
|
47
|
-
export function initHostStorage(overrides?: {
|
|
48
|
-
publicDb?: PublicStateDB;
|
|
49
|
-
contractsDb?: PublicContractsDB;
|
|
50
|
-
commitmentsDb?: CommitmentsDB;
|
|
51
|
-
}): HostStorage {
|
|
52
|
-
return new HostStorage(
|
|
53
|
-
overrides?.publicDb || mock<PublicStateDB>(),
|
|
54
|
-
overrides?.contractsDb || mock<PublicContractsDB>(),
|
|
55
|
-
overrides?.commitmentsDb || mock<CommitmentsDB>(),
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
39
|
/** Creates an empty state manager with mocked host storage. */
|
|
60
40
|
export function initPersistableStateManager(overrides?: {
|
|
61
|
-
|
|
41
|
+
worldStateDB?: WorldStateDB;
|
|
62
42
|
trace?: PublicSideEffectTraceInterface;
|
|
63
43
|
publicStorage?: PublicStorage;
|
|
64
44
|
nullifiers?: NullifierManager;
|
|
65
45
|
}): AvmPersistableStateManager {
|
|
66
|
-
const
|
|
46
|
+
const worldStateDB = overrides?.worldStateDB || mock<WorldStateDB>();
|
|
67
47
|
return new AvmPersistableStateManager(
|
|
68
|
-
|
|
48
|
+
worldStateDB,
|
|
69
49
|
overrides?.trace || mock<PublicSideEffectTraceInterface>(),
|
|
70
|
-
overrides?.publicStorage || new PublicStorage(
|
|
71
|
-
overrides?.nullifiers || new NullifierManager(
|
|
50
|
+
overrides?.publicStorage || new PublicStorage(worldStateDB),
|
|
51
|
+
overrides?.nullifiers || new NullifierManager(worldStateDB),
|
|
72
52
|
);
|
|
73
53
|
}
|
|
74
54
|
|
|
@@ -83,7 +63,6 @@ export function initExecutionEnvironment(overrides?: Partial<AvmExecutionEnviron
|
|
|
83
63
|
overrides?.functionSelector ?? FunctionSelector.empty(),
|
|
84
64
|
overrides?.contractCallDepth ?? Fr.zero(),
|
|
85
65
|
overrides?.transactionFee ?? Fr.zero(),
|
|
86
|
-
overrides?.header ?? Header.empty(),
|
|
87
66
|
overrides?.globals ?? GlobalVariables.empty(),
|
|
88
67
|
overrides?.isStaticCall ?? false,
|
|
89
68
|
overrides?.isDelegateCall ?? false,
|
|
@@ -124,26 +103,14 @@ export function allSameExcept(original: any, overrides: any): any {
|
|
|
124
103
|
return merge({}, original, overrides);
|
|
125
104
|
}
|
|
126
105
|
|
|
127
|
-
/**
|
|
128
|
-
* Adjust the user index to account for the AvmContextInputs size.
|
|
129
|
-
* This is a hack for testing, and should go away once AvmContextInputs themselves go away.
|
|
130
|
-
*/
|
|
131
|
-
export function adjustCalldataIndex(userIndex: number): number {
|
|
132
|
-
return userIndex + AvmContextInputs.SIZE;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export function anyAvmContextInputs() {
|
|
136
|
-
const tv = [];
|
|
137
|
-
for (let i = 0; i < AvmContextInputs.SIZE; i++) {
|
|
138
|
-
tv.push(expect.any(Fr));
|
|
139
|
-
}
|
|
140
|
-
return tv;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
106
|
export function randomMemoryBytes(length: number): Uint8[] {
|
|
144
107
|
return [...Array(length)].map(_ => new Uint8(Math.floor(Math.random() * 255)));
|
|
145
108
|
}
|
|
146
109
|
|
|
110
|
+
export function randomMemoryUint32s(length: number): Uint32[] {
|
|
111
|
+
return [...Array(length)].map(_ => new Uint32(Math.floor(Math.random() * 255)));
|
|
112
|
+
}
|
|
113
|
+
|
|
147
114
|
export function randomMemoryUint64s(length: number): Uint64[] {
|
|
148
115
|
return [...Array(length)].map(_ => new Uint64(Math.floor(Math.random() * 255)));
|
|
149
116
|
}
|
package/src/avm/journal/index.ts
CHANGED