From fb0012b7db1c717c1202cf6ba190c4d2fde5e318 Mon Sep 17 00:00:00 2001 From: dbanks12 Date: Wed, 18 Sep 2024 18:23:59 +0000 Subject: [PATCH] chore: add some gas premiums for avm side effects --- .../barretenberg/vm/avm/trace/fixed_gas.cpp | 146 +++++----- .../src/barretenberg/vm/avm/trace/trace.cpp | 26 +- .../src/barretenberg/vm/aztec_constants.hpp | 200 ++++++-------- .../src/core/libraries/ConstantsGen.sol | 13 +- .../crates/types/src/constants.nr | 253 ++++++++---------- yarn-project/circuits.js/src/constants.gen.ts | 213 ++++++--------- yarn-project/simulator/src/avm/avm_gas.ts | 126 ++------- .../simulator/src/avm/avm_machine_state.ts | 10 +- .../simulator/src/avm/opcodes/commitment.ts | 4 +- .../simulator/src/avm/opcodes/hashing.ts | 13 +- .../src/avm/opcodes/multi_scalar_mul.ts | 8 +- 11 files changed, 431 insertions(+), 581 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/vm/avm/trace/fixed_gas.cpp b/barretenberg/cpp/src/barretenberg/vm/avm/trace/fixed_gas.cpp index cf34e1d46919..741b733ad286 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm/trace/fixed_gas.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm/trace/fixed_gas.cpp @@ -18,93 +18,95 @@ constexpr auto make_cost(uint16_t l2_base, uint16_t da_base, uint16_t l2_dyn, ui } const std::unordered_map GAS_COST_TABLE = { - { OpCode::ADD_8, make_cost(AVM_ADD_BASE_L2_GAS, 0, AVM_ADD_DYN_L2_GAS, 0) }, - { OpCode::ADD_16, make_cost(AVM_ADD_BASE_L2_GAS, 0, AVM_ADD_DYN_L2_GAS, 0) }, - { OpCode::SUB_8, make_cost(AVM_SUB_BASE_L2_GAS, 0, AVM_SUB_DYN_L2_GAS, 0) }, - { OpCode::SUB_16, make_cost(AVM_SUB_BASE_L2_GAS, 0, AVM_SUB_DYN_L2_GAS, 0) }, - { OpCode::MUL_8, make_cost(AVM_MUL_BASE_L2_GAS, 0, AVM_MUL_DYN_L2_GAS, 0) }, - { OpCode::MUL_16, make_cost(AVM_MUL_BASE_L2_GAS, 0, AVM_MUL_DYN_L2_GAS, 0) }, - { OpCode::DIV_8, make_cost(AVM_DIV_BASE_L2_GAS, 0, AVM_DIV_DYN_L2_GAS, 0) }, - { OpCode::DIV_16, make_cost(AVM_DIV_BASE_L2_GAS, 0, AVM_DIV_DYN_L2_GAS, 0) }, - { OpCode::FDIV_8, make_cost(AVM_FDIV_BASE_L2_GAS, 0, AVM_FDIV_DYN_L2_GAS, 0) }, - { OpCode::FDIV_16, make_cost(AVM_FDIV_BASE_L2_GAS, 0, AVM_FDIV_DYN_L2_GAS, 0) }, - { OpCode::EQ_8, make_cost(AVM_EQ_BASE_L2_GAS, 0, AVM_EQ_DYN_L2_GAS, 0) }, - { OpCode::EQ_16, make_cost(AVM_EQ_BASE_L2_GAS, 0, AVM_EQ_DYN_L2_GAS, 0) }, - { OpCode::LT_8, make_cost(AVM_LT_BASE_L2_GAS, 0, AVM_LT_DYN_L2_GAS, 0) }, - { OpCode::LT_16, make_cost(AVM_LT_BASE_L2_GAS, 0, AVM_LT_DYN_L2_GAS, 0) }, - { OpCode::LTE_8, make_cost(AVM_LTE_BASE_L2_GAS, 0, AVM_LTE_DYN_L2_GAS, 0) }, - { OpCode::LTE_16, make_cost(AVM_LTE_BASE_L2_GAS, 0, AVM_LTE_DYN_L2_GAS, 0) }, - { OpCode::AND_8, make_cost(AVM_AND_BASE_L2_GAS, 0, AVM_AND_DYN_L2_GAS, 0) }, - { OpCode::AND_16, make_cost(AVM_AND_BASE_L2_GAS, 0, AVM_AND_DYN_L2_GAS, 0) }, - { OpCode::OR_8, make_cost(AVM_OR_BASE_L2_GAS, 0, AVM_OR_DYN_L2_GAS, 0) }, - { OpCode::OR_16, make_cost(AVM_OR_BASE_L2_GAS, 0, AVM_OR_DYN_L2_GAS, 0) }, - { OpCode::XOR_8, make_cost(AVM_XOR_BASE_L2_GAS, 0, AVM_XOR_DYN_L2_GAS, 0) }, - { OpCode::XOR_16, make_cost(AVM_XOR_BASE_L2_GAS, 0, AVM_XOR_DYN_L2_GAS, 0) }, - { OpCode::NOT_8, make_cost(AVM_NOT_BASE_L2_GAS, 0, AVM_NOT_DYN_L2_GAS, 0) }, - { OpCode::NOT_16, make_cost(AVM_NOT_BASE_L2_GAS, 0, AVM_NOT_DYN_L2_GAS, 0) }, - { OpCode::SHL_8, make_cost(AVM_SHL_BASE_L2_GAS, 0, AVM_SHL_DYN_L2_GAS, 0) }, - { OpCode::SHL_16, make_cost(AVM_SHL_BASE_L2_GAS, 0, AVM_SHL_DYN_L2_GAS, 0) }, - { OpCode::SHR_8, make_cost(AVM_SHR_BASE_L2_GAS, 0, AVM_SHR_DYN_L2_GAS, 0) }, - { OpCode::SHR_16, make_cost(AVM_SHR_BASE_L2_GAS, 0, AVM_SHR_DYN_L2_GAS, 0) }, - { OpCode::CAST_8, make_cost(AVM_CAST_BASE_L2_GAS, 0, AVM_CAST_DYN_L2_GAS, 0) }, - { OpCode::CAST_16, make_cost(AVM_CAST_BASE_L2_GAS, 0, AVM_CAST_DYN_L2_GAS, 0) }, - { OpCode::ADDRESS, make_cost(AVM_ADDRESS_BASE_L2_GAS, 0, AVM_ADDRESS_DYN_L2_GAS, 0) }, - { OpCode::STORAGEADDRESS, make_cost(AVM_STORAGEADDRESS_BASE_L2_GAS, 0, AVM_STORAGEADDRESS_DYN_L2_GAS, 0) }, - { OpCode::SENDER, make_cost(AVM_SENDER_BASE_L2_GAS, 0, AVM_SENDER_DYN_L2_GAS, 0) }, - { OpCode::FUNCTIONSELECTOR, make_cost(AVM_FUNCTIONSELECTOR_BASE_L2_GAS, 0, AVM_FUNCTIONSELECTOR_DYN_L2_GAS, 0) }, - { OpCode::TRANSACTIONFEE, make_cost(AVM_TRANSACTIONFEE_BASE_L2_GAS, 0, AVM_TRANSACTIONFEE_DYN_L2_GAS, 0) }, - { OpCode::CHAINID, make_cost(AVM_CHAINID_BASE_L2_GAS, 0, AVM_CHAINID_DYN_L2_GAS, 0) }, - { OpCode::VERSION, make_cost(AVM_VERSION_BASE_L2_GAS, 0, AVM_VERSION_DYN_L2_GAS, 0) }, - { OpCode::BLOCKNUMBER, make_cost(AVM_BLOCKNUMBER_BASE_L2_GAS, 0, AVM_BLOCKNUMBER_DYN_L2_GAS, 0) }, - { OpCode::TIMESTAMP, make_cost(AVM_TIMESTAMP_BASE_L2_GAS, 0, AVM_TIMESTAMP_DYN_L2_GAS, 0) }, - { OpCode::FEEPERL2GAS, make_cost(AVM_FEEPERL2GAS_BASE_L2_GAS, 0, AVM_FEEPERL2GAS_DYN_L2_GAS, 0) }, - { OpCode::FEEPERDAGAS, make_cost(AVM_FEEPERDAGAS_BASE_L2_GAS, 0, AVM_FEEPERDAGAS_DYN_L2_GAS, 0) }, + { OpCode::ADD_8, make_cost(AVM_ADD_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::ADD_16, make_cost(AVM_ADD_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SUB_8, make_cost(AVM_SUB_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SUB_16, make_cost(AVM_SUB_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::MUL_8, make_cost(AVM_MUL_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::MUL_16, make_cost(AVM_MUL_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::DIV_8, make_cost(AVM_DIV_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::DIV_16, make_cost(AVM_DIV_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::FDIV_8, make_cost(AVM_FDIV_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::FDIV_16, make_cost(AVM_FDIV_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::EQ_8, make_cost(AVM_EQ_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::EQ_16, make_cost(AVM_EQ_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::LT_8, make_cost(AVM_LT_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::LT_16, make_cost(AVM_LT_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::LTE_8, make_cost(AVM_LTE_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::LTE_16, make_cost(AVM_LTE_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::AND_8, make_cost(AVM_AND_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::AND_16, make_cost(AVM_AND_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::OR_8, make_cost(AVM_OR_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::OR_16, make_cost(AVM_OR_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::XOR_8, make_cost(AVM_XOR_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::XOR_16, make_cost(AVM_XOR_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::NOT_8, make_cost(AVM_NOT_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::NOT_16, make_cost(AVM_NOT_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SHL_8, make_cost(AVM_SHL_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SHL_16, make_cost(AVM_SHL_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SHR_8, make_cost(AVM_SHR_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SHR_16, make_cost(AVM_SHR_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::CAST_8, make_cost(AVM_CAST_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::CAST_16, make_cost(AVM_CAST_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::ADDRESS, make_cost(AVM_ADDRESS_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::STORAGEADDRESS, make_cost(AVM_STORAGEADDRESS_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SENDER, make_cost(AVM_SENDER_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::FUNCTIONSELECTOR, make_cost(AVM_FUNCTIONSELECTOR_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::TRANSACTIONFEE, make_cost(AVM_TRANSACTIONFEE_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::CHAINID, make_cost(AVM_CHAINID_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::VERSION, make_cost(AVM_VERSION_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::BLOCKNUMBER, make_cost(AVM_BLOCKNUMBER_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::TIMESTAMP, make_cost(AVM_TIMESTAMP_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::FEEPERL2GAS, make_cost(AVM_FEEPERL2GAS_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::FEEPERDAGAS, make_cost(AVM_FEEPERDAGAS_BASE_L2_GAS, 0, 0, 0) }, { OpCode::CALLDATACOPY, make_cost(AVM_CALLDATACOPY_BASE_L2_GAS, 0, AVM_CALLDATACOPY_DYN_L2_GAS, 0) }, - { OpCode::L2GASLEFT, make_cost(AVM_L2GASLEFT_BASE_L2_GAS, 0, AVM_L2GASLEFT_DYN_L2_GAS, 0) }, - { OpCode::DAGASLEFT, make_cost(AVM_DAGASLEFT_BASE_L2_GAS, 0, AVM_DAGASLEFT_DYN_L2_GAS, 0) }, - { OpCode::JUMP_16, make_cost(AVM_JUMP_BASE_L2_GAS, 0, AVM_JUMP_DYN_L2_GAS, 0) }, - { OpCode::JUMPI_16, make_cost(AVM_JUMPI_BASE_L2_GAS, 0, AVM_JUMPI_DYN_L2_GAS, 0) }, - { OpCode::INTERNALCALL, make_cost(AVM_INTERNALCALL_BASE_L2_GAS, 0, AVM_INTERNALCALL_DYN_L2_GAS, 0) }, - { OpCode::INTERNALRETURN, make_cost(AVM_INTERNALRETURN_BASE_L2_GAS, 0, AVM_INTERNALRETURN_DYN_L2_GAS, 0) }, - { OpCode::SET_8, make_cost(AVM_SET_BASE_L2_GAS, 0, AVM_SET_DYN_L2_GAS, 0) }, - { OpCode::SET_16, make_cost(AVM_SET_BASE_L2_GAS, 0, AVM_SET_DYN_L2_GAS, 0) }, - { OpCode::SET_32, make_cost(AVM_SET_BASE_L2_GAS, 0, AVM_SET_DYN_L2_GAS, 0) }, - { OpCode::SET_64, make_cost(AVM_SET_BASE_L2_GAS, 0, AVM_SET_DYN_L2_GAS, 0) }, - { OpCode::SET_128, make_cost(AVM_SET_BASE_L2_GAS, 0, AVM_SET_DYN_L2_GAS, 0) }, - { OpCode::SET_FF, make_cost(AVM_SET_BASE_L2_GAS, 0, AVM_SET_DYN_L2_GAS, 0) }, - { OpCode::MOV_8, make_cost(AVM_MOV_BASE_L2_GAS, 0, AVM_MOV_DYN_L2_GAS, 0) }, - { OpCode::MOV_16, make_cost(AVM_MOV_BASE_L2_GAS, 0, AVM_MOV_DYN_L2_GAS, 0) }, - { OpCode::CMOV, make_cost(AVM_CMOV_BASE_L2_GAS, 0, AVM_CMOV_DYN_L2_GAS, 0) }, - { OpCode::SLOAD, make_cost(AVM_SLOAD_BASE_L2_GAS, 0, AVM_SLOAD_DYN_L2_GAS, 0) }, - { OpCode::SSTORE, make_cost(AVM_SSTORE_BASE_L2_GAS, 0, AVM_SSTORE_DYN_L2_GAS, 0) }, - { OpCode::NOTEHASHEXISTS, make_cost(AVM_NOTEHASHEXISTS_BASE_L2_GAS, 0, AVM_NOTEHASHEXISTS_DYN_L2_GAS, 0) }, - { OpCode::EMITNOTEHASH, make_cost(AVM_EMITNOTEHASH_BASE_L2_GAS, 0, AVM_EMITNOTEHASH_DYN_L2_GAS, 0) }, - { OpCode::NULLIFIEREXISTS, make_cost(AVM_NULLIFIEREXISTS_BASE_L2_GAS, 0, AVM_NULLIFIEREXISTS_DYN_L2_GAS, 0) }, - { OpCode::EMITNULLIFIER, make_cost(AVM_EMITNULLIFIER_BASE_L2_GAS, 0, AVM_EMITNULLIFIER_DYN_L2_GAS, 0) }, - { OpCode::L1TOL2MSGEXISTS, make_cost(AVM_L1TOL2MSGEXISTS_BASE_L2_GAS, 0, AVM_L1TOL2MSGEXISTS_DYN_L2_GAS, 0) }, - { OpCode::GETCONTRACTINSTANCE, - make_cost(AVM_GETCONTRACTINSTANCE_BASE_L2_GAS, 0, AVM_GETCONTRACTINSTANCE_DYN_L2_GAS, 0) }, + { OpCode::L2GASLEFT, make_cost(AVM_L2GASLEFT_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::DAGASLEFT, make_cost(AVM_DAGASLEFT_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::JUMP_16, make_cost(AVM_JUMP_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::JUMPI_16, make_cost(AVM_JUMPI_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::INTERNALCALL, make_cost(AVM_INTERNALCALL_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::INTERNALRETURN, make_cost(AVM_INTERNALRETURN_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SET_8, make_cost(AVM_SET_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SET_16, make_cost(AVM_SET_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SET_32, make_cost(AVM_SET_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SET_64, make_cost(AVM_SET_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SET_128, make_cost(AVM_SET_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SET_FF, make_cost(AVM_SET_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::MOV_8, make_cost(AVM_MOV_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::MOV_16, make_cost(AVM_MOV_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::CMOV, make_cost(AVM_CMOV_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SLOAD, make_cost(AVM_SLOAD_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::SSTORE, make_cost(AVM_SSTORE_BASE_L2_GAS, AVM_SSTORE_BASE_DA_GAS, 0, 0) }, + { OpCode::NOTEHASHEXISTS, make_cost(AVM_NOTEHASHEXISTS_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::EMITNOTEHASH, make_cost(AVM_EMITNOTEHASH_BASE_L2_GAS, AVM_EMITNOTEHASH_BASE_DA_GAS, 0, 0) }, + { OpCode::NULLIFIEREXISTS, make_cost(AVM_NULLIFIEREXISTS_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::EMITNULLIFIER, make_cost(AVM_EMITNULLIFIER_BASE_L2_GAS, AVM_EMITNULLIFIER_BASE_DA_GAS, 0, 0) }, + { OpCode::L1TOL2MSGEXISTS, make_cost(AVM_L1TOL2MSGEXISTS_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::GETCONTRACTINSTANCE, make_cost(AVM_GETCONTRACTINSTANCE_BASE_L2_GAS, 0, 0, 0) }, { OpCode::EMITUNENCRYPTEDLOG, - make_cost(AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS, 0, AVM_EMITUNENCRYPTEDLOG_DYN_L2_GAS, 0) }, - { OpCode::SENDL2TOL1MSG, make_cost(AVM_SENDL2TOL1MSG_BASE_L2_GAS, 0, AVM_SENDL2TOL1MSG_DYN_L2_GAS, 0) }, + make_cost(AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS, + 0, + AVM_EMITUNENCRYPTEDLOG_DYN_L2_GAS, + AVM_EMITUNENCRYPTEDLOG_DYN_DA_GAS) }, + { OpCode::SENDL2TOL1MSG, make_cost(AVM_SENDL2TOL1MSG_BASE_L2_GAS, AVM_SENDL2TOL1MSG_BASE_DA_GAS, 0, 0) }, { OpCode::CALL, make_cost(AVM_CALL_BASE_L2_GAS, 0, AVM_CALL_DYN_L2_GAS, 0) }, { OpCode::STATICCALL, make_cost(AVM_STATICCALL_BASE_L2_GAS, 0, AVM_STATICCALL_DYN_L2_GAS, 0) }, { OpCode::DELEGATECALL, make_cost(AVM_DELEGATECALL_BASE_L2_GAS, 0, AVM_DELEGATECALL_DYN_L2_GAS, 0) }, { OpCode::RETURN, make_cost(AVM_RETURN_BASE_L2_GAS, 0, AVM_RETURN_DYN_L2_GAS, 0) }, { OpCode::REVERT_8, make_cost(AVM_REVERT_BASE_L2_GAS, 0, AVM_REVERT_DYN_L2_GAS, 0) }, { OpCode::REVERT_16, make_cost(AVM_REVERT_BASE_L2_GAS, 0, AVM_REVERT_DYN_L2_GAS, 0) }, - { OpCode::DEBUGLOG, make_cost(AVM_DEBUGLOG_BASE_L2_GAS, 0, AVM_DEBUGLOG_DYN_L2_GAS, 0) }, + { OpCode::DEBUGLOG, make_cost(AVM_DEBUGLOG_BASE_L2_GAS, 0, 0, 0) }, { OpCode::KECCAK, make_cost(AVM_KECCAK_BASE_L2_GAS, 0, AVM_KECCAK_DYN_L2_GAS, 0) }, - { OpCode::POSEIDON2, make_cost(AVM_POSEIDON2_BASE_L2_GAS, 0, AVM_POSEIDON2_DYN_L2_GAS, 0) }, + { OpCode::POSEIDON2, make_cost(AVM_POSEIDON2_BASE_L2_GAS, 0, 0, 0) }, { OpCode::SHA256, make_cost(AVM_SHA256_BASE_L2_GAS, 0, AVM_SHA256_DYN_L2_GAS, 0) }, { OpCode::PEDERSEN, make_cost(AVM_PEDERSEN_BASE_L2_GAS, 0, AVM_PEDERSEN_DYN_L2_GAS, 0) }, - { OpCode::ECADD, make_cost(AVM_ECADD_BASE_L2_GAS, 0, AVM_ECADD_DYN_L2_GAS, 0) }, + { OpCode::ECADD, make_cost(AVM_ECADD_BASE_L2_GAS, 0, 0, 0) }, { OpCode::MSM, make_cost(AVM_MSM_BASE_L2_GAS, 0, AVM_MSM_DYN_L2_GAS, 0) }, { OpCode::PEDERSENCOMMITMENT, make_cost(AVM_PEDERSENCOMMITMENT_BASE_L2_GAS, 0, AVM_PEDERSENCOMMITMENT_DYN_L2_GAS, 0) }, { OpCode::TORADIXLE, make_cost(AVM_TORADIXLE_BASE_L2_GAS, 0, AVM_TORADIXLE_DYN_L2_GAS, 0) }, - { OpCode::SHA256COMPRESSION, make_cost(AVM_SHA256COMPRESSION_BASE_L2_GAS, 0, AVM_SHA256COMPRESSION_DYN_L2_GAS, 0) }, - { OpCode::KECCAKF1600, make_cost(AVM_KECCAKF1600_BASE_L2_GAS, 0, AVM_KECCAKF1600_DYN_L2_GAS, 0) }, + { OpCode::SHA256COMPRESSION, make_cost(AVM_SHA256COMPRESSION_BASE_L2_GAS, 0, 0, 0) }, + { OpCode::KECCAKF1600, make_cost(AVM_KECCAKF1600_BASE_L2_GAS, 0, 0, 0) }, }; } // namespace diff --git a/barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.cpp b/barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.cpp index 8d493b33e827..2e07ca12e6a8 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.cpp @@ -2807,13 +2807,13 @@ void AvmTraceBuilder::op_keccak(uint8_t indirect, auto [resolved_output_offset, resolved_input_offset, resolved_input_size_offset] = unpack_indirects<3>(indirect, { output_offset, input_offset, input_size_offset }); - // Constrain gas cost - gas_trace_builder.constrain_gas(clk, OpCode::KECCAK); - // Read the input length first auto input_length_read = constrained_read_from_memory( call_ptr, clk, resolved_input_size_offset, AvmMemoryTag::U32, AvmMemoryTag::U0, IntermRegister::IB); + // Constrain gas cost + gas_trace_builder.constrain_gas(clk, OpCode::KECCAK, static_cast(input_length_read.val)); + // Store the clock time that we will use to line up the gadget later auto keccak_op_clk = clk; main_trace.push_back(Row{ @@ -2999,11 +2999,12 @@ void AvmTraceBuilder::op_sha256(uint8_t indirect, auto [resolved_output_offset, resolved_input_offset, resolved_input_size_offset] = unpack_indirects<3>(indirect, { output_offset, input_offset, input_size_offset }); - gas_trace_builder.constrain_gas(clk, OpCode::SHA256); - auto input_length_read = constrained_read_from_memory( call_ptr, clk, resolved_input_size_offset, AvmMemoryTag::U32, AvmMemoryTag::U0, IntermRegister::IB); + // Constrain gas cost + gas_trace_builder.constrain_gas(clk, OpCode::SHA256, static_cast(input_length_read.val)); + // Store the clock time that we will use to line up the gadget later auto sha256_op_clk = clk; main_trace.push_back(Row{ @@ -3062,7 +3063,8 @@ void AvmTraceBuilder::op_pedersen_hash(uint8_t indirect, auto gen_ctx_read = unconstrained_read_from_memory(resolved_gen_ctx_offset); // Constrain gas cost - gas_trace_builder.constrain_gas(clk, OpCode::PEDERSEN); + // TODO(dbanks12): need tag check u32 here on input size + gas_trace_builder.constrain_gas(clk, OpCode::PEDERSEN, static_cast(input_size_read)); // We read the input and output addresses in one row as they should contain FF elements main_trace.push_back(Row{ @@ -3080,8 +3082,8 @@ void AvmTraceBuilder::op_pedersen_hash(uint8_t indirect, }); std::vector inputs; - read_slice_from_memory(resolved_input_offset, uint32_t(input_size_read), inputs); - FF output = pedersen_trace_builder.pedersen_hash(inputs, uint32_t(gen_ctx_read), clk); + read_slice_from_memory(resolved_input_offset, static_cast(input_size_read), inputs); + FF output = pedersen_trace_builder.pedersen_hash(inputs, static_cast(gen_ctx_read), clk); write_slice_to_memory(resolved_output_offset, AvmMemoryTag::FF, std::vector{ output }); } @@ -3224,7 +3226,9 @@ void AvmTraceBuilder::op_variable_msm(uint8_t indirect, .main_tag_err = FF(0), }); - gas_trace_builder.constrain_gas(clk, OpCode::MSM); + // TODO(dbanks12): length needs to fit into u32 here or it will certainly + // run out of gas. Casting/truncating here is not secure. + gas_trace_builder.constrain_gas(clk, OpCode::MSM, static_cast(points_length)); // Write the result back to memory [x, y, inf] with tags [FF, FF, U8] AddressWithMode output_offset_direct = @@ -3262,7 +3266,9 @@ void AvmTraceBuilder::op_pedersen_commit(uint8_t indirect, .main_tag_err = FF(0), }); - gas_trace_builder.constrain_gas(clk, OpCode::PEDERSENCOMMITMENT); + // TODO(dbanks12): length needs to fit into u32 here or it will certainly + // run out of gas. Casting/truncating here is not secure. + gas_trace_builder.constrain_gas(clk, OpCode::PEDERSENCOMMITMENT, static_cast(input_length_read)); // Write the result back to memory [x, y, inf] with tags [FF, FF, U8] AddressWithMode output_offset_direct = diff --git a/barretenberg/cpp/src/barretenberg/vm/aztec_constants.hpp b/barretenberg/cpp/src/barretenberg/vm/aztec_constants.hpp index 83f246fea5c1..643848b0a87e 100644 --- a/barretenberg/cpp/src/barretenberg/vm/aztec_constants.hpp +++ b/barretenberg/cpp/src/barretenberg/vm/aztec_constants.hpp @@ -66,129 +66,83 @@ #define START_EMIT_NULLIFIER_WRITE_OFFSET 144 #define START_EMIT_L2_TO_L1_MSG_WRITE_OFFSET 160 #define START_EMIT_UNENCRYPTED_LOG_WRITE_OFFSET 162 -#define AVM_DEFAULT_BASE_GAS_L2 100 -#define AVM_DEFAULT_DYN_GAS_L2 0 -#define AVM_ADD_BASE_L2_GAS 320 -#define AVM_SUB_BASE_L2_GAS 320 -#define AVM_MUL_BASE_L2_GAS 330 -#define AVM_DIV_BASE_L2_GAS 430 -#define AVM_FDIV_BASE_L2_GAS 320 -#define AVM_EQ_BASE_L2_GAS 320 -#define AVM_LT_BASE_L2_GAS 640 -#define AVM_LTE_BASE_L2_GAS 640 -#define AVM_AND_BASE_L2_GAS 330 -#define AVM_OR_BASE_L2_GAS 330 -#define AVM_XOR_BASE_L2_GAS 320 -#define AVM_NOT_BASE_L2_GAS 270 -#define AVM_SHL_BASE_L2_GAS 320 -#define AVM_SHR_BASE_L2_GAS 320 -#define AVM_CAST_BASE_L2_GAS 300 -#define AVM_ADDRESS_BASE_L2_GAS 200 -#define AVM_STORAGEADDRESS_BASE_L2_GAS 200 +#define AVM_ADD_BASE_L2_GAS 32 +#define AVM_SUB_BASE_L2_GAS 32 +#define AVM_MUL_BASE_L2_GAS 33 +#define AVM_DIV_BASE_L2_GAS 43 +#define AVM_FDIV_BASE_L2_GAS 32 +#define AVM_EQ_BASE_L2_GAS 32 +#define AVM_LT_BASE_L2_GAS 64 +#define AVM_LTE_BASE_L2_GAS 64 +#define AVM_AND_BASE_L2_GAS 33 +#define AVM_OR_BASE_L2_GAS 33 +#define AVM_XOR_BASE_L2_GAS 33 +#define AVM_NOT_BASE_L2_GAS 27 +#define AVM_SHL_BASE_L2_GAS 32 +#define AVM_SHR_BASE_L2_GAS 32 +#define AVM_CAST_BASE_L2_GAS 30 +#define AVM_ADDRESS_BASE_L2_GAS 20 +#define AVM_STORAGEADDRESS_BASE_L2_GAS 20 #define AVM_SENDER_BASE_L2_GAS 200 -#define AVM_FUNCTIONSELECTOR_BASE_L2_GAS 200 +#define AVM_FUNCTIONSELECTOR_BASE_L2_GAS 20 #define AVM_TRANSACTIONFEE_BASE_L2_GAS 200 -#define AVM_CHAINID_BASE_L2_GAS 200 -#define AVM_VERSION_BASE_L2_GAS 200 -#define AVM_BLOCKNUMBER_BASE_L2_GAS 200 -#define AVM_TIMESTAMP_BASE_L2_GAS 200 -#define AVM_FEEPERL2GAS_BASE_L2_GAS 200 -#define AVM_FEEPERDAGAS_BASE_L2_GAS 200 -#define AVM_CALLDATACOPY_BASE_L2_GAS 290 -#define AVM_L2GASLEFT_BASE_L2_GAS 180 -#define AVM_DAGASLEFT_BASE_L2_GAS 180 -#define AVM_JUMP_BASE_L2_GAS 120 -#define AVM_JUMPI_BASE_L2_GAS 180 -#define AVM_INTERNALCALL_BASE_L2_GAS 180 -#define AVM_INTERNALRETURN_BASE_L2_GAS 180 -#define AVM_SET_BASE_L2_GAS 180 -#define AVM_MOV_BASE_L2_GAS 230 -#define AVM_CMOV_BASE_L2_GAS 340 -#define AVM_SLOAD_BASE_L2_GAS 180 -#define AVM_SSTORE_BASE_L2_GAS 180 -#define AVM_NOTEHASHEXISTS_BASE_L2_GAS 260 -#define AVM_EMITNOTEHASH_BASE_L2_GAS 260 -#define AVM_NULLIFIEREXISTS_BASE_L2_GAS 260 -#define AVM_EMITNULLIFIER_BASE_L2_GAS 260 -#define AVM_L1TOL2MSGEXISTS_BASE_L2_GAS 260 -#define AVM_GETCONTRACTINSTANCE_BASE_L2_GAS 480 -#define AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS 180 -#define AVM_SENDL2TOL1MSG_BASE_L2_GAS 260 -#define AVM_CALL_BASE_L2_GAS 450 -#define AVM_STATICCALL_BASE_L2_GAS 450 -#define AVM_DELEGATECALL_BASE_L2_GAS 0 -#define AVM_RETURN_BASE_L2_GAS 140 -#define AVM_REVERT_BASE_L2_GAS 140 -#define AVM_DEBUGLOG_BASE_L2_GAS 100 +#define AVM_CHAINID_BASE_L2_GAS 20 +#define AVM_VERSION_BASE_L2_GAS 20 +#define AVM_BLOCKNUMBER_BASE_L2_GAS 20 +#define AVM_TIMESTAMP_BASE_L2_GAS 20 +#define AVM_FEEPERL2GAS_BASE_L2_GAS 20 +#define AVM_FEEPERDAGAS_BASE_L2_GAS 20 +#define AVM_CALLDATACOPY_BASE_L2_GAS 29 +#define AVM_L2GASLEFT_BASE_L2_GAS 18 +#define AVM_DAGASLEFT_BASE_L2_GAS 18 +#define AVM_JUMP_BASE_L2_GAS 12 +#define AVM_JUMPI_BASE_L2_GAS 18 +#define AVM_INTERNALCALL_BASE_L2_GAS 18 +#define AVM_INTERNALRETURN_BASE_L2_GAS 18 +#define AVM_SET_BASE_L2_GAS 18 +#define AVM_MOV_BASE_L2_GAS 23 +#define AVM_CMOV_BASE_L2_GAS 34 +#define AVM_SLOAD_BASE_L2_GAS 1218 +#define AVM_SSTORE_BASE_L2_GAS 2642 +#define AVM_NOTEHASHEXISTS_BASE_L2_GAS 986 +#define AVM_EMITNOTEHASH_BASE_L2_GAS 2330 +#define AVM_NULLIFIEREXISTS_BASE_L2_GAS 1226 +#define AVM_EMITNULLIFIER_BASE_L2_GAS 2650 +#define AVM_L1TOL2MSGEXISTS_BASE_L2_GAS 506 +#define AVM_GETCONTRACTINSTANCE_BASE_L2_GAS 1248 +#define AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS 18 +#define AVM_SENDL2TOL1MSG_BASE_L2_GAS 226 +#define AVM_CALL_BASE_L2_GAS 2445 +#define AVM_STATICCALL_BASE_L2_GAS 2445 +#define AVM_DELEGATECALL_BASE_L2_GAS 2445 +#define AVM_RETURN_BASE_L2_GAS 28 +#define AVM_REVERT_BASE_L2_GAS 28 +#define AVM_DEBUGLOG_BASE_L2_GAS 10 #define AVM_KECCAK_BASE_L2_GAS 3000 -#define AVM_POSEIDON2_BASE_L2_GAS 670 -#define AVM_SHA256_BASE_L2_GAS 3000 -#define AVM_PEDERSEN_BASE_L2_GAS 3000 -#define AVM_ECADD_BASE_L2_GAS 900 -#define AVM_MSM_BASE_L2_GAS 900 -#define AVM_PEDERSENCOMMITMENT_BASE_L2_GAS 3000 -#define AVM_TORADIXLE_BASE_L2_GAS 200 -#define AVM_SHA256COMPRESSION_BASE_L2_GAS 3000 -#define AVM_KECCAKF1600_BASE_L2_GAS 3000 -#define AVM_ADD_DYN_L2_GAS 0 -#define AVM_SUB_DYN_L2_GAS 0 -#define AVM_MUL_DYN_L2_GAS 0 -#define AVM_DIV_DYN_L2_GAS 0 -#define AVM_FDIV_DYN_L2_GAS 0 -#define AVM_EQ_DYN_L2_GAS 0 -#define AVM_LT_DYN_L2_GAS 0 -#define AVM_LTE_DYN_L2_GAS 0 -#define AVM_AND_DYN_L2_GAS 0 -#define AVM_OR_DYN_L2_GAS 0 -#define AVM_XOR_DYN_L2_GAS 0 -#define AVM_NOT_DYN_L2_GAS 0 -#define AVM_SHL_DYN_L2_GAS 0 -#define AVM_SHR_DYN_L2_GAS 0 -#define AVM_CAST_DYN_L2_GAS 0 -#define AVM_ADDRESS_DYN_L2_GAS 0 -#define AVM_STORAGEADDRESS_DYN_L2_GAS 0 -#define AVM_SENDER_DYN_L2_GAS 0 -#define AVM_FUNCTIONSELECTOR_DYN_L2_GAS 0 -#define AVM_TRANSACTIONFEE_DYN_L2_GAS 0 -#define AVM_CHAINID_DYN_L2_GAS 0 -#define AVM_VERSION_DYN_L2_GAS 0 -#define AVM_BLOCKNUMBER_DYN_L2_GAS 0 -#define AVM_TIMESTAMP_DYN_L2_GAS 0 -#define AVM_FEEPERL2GAS_DYN_L2_GAS 0 -#define AVM_FEEPERDAGAS_DYN_L2_GAS 0 -#define AVM_CALLDATACOPY_DYN_L2_GAS 50 -#define AVM_L2GASLEFT_DYN_L2_GAS 0 -#define AVM_DAGASLEFT_DYN_L2_GAS 0 -#define AVM_JUMP_DYN_L2_GAS 0 -#define AVM_JUMPI_DYN_L2_GAS 0 -#define AVM_INTERNALCALL_DYN_L2_GAS 0 -#define AVM_INTERNALRETURN_DYN_L2_GAS 0 -#define AVM_SET_DYN_L2_GAS 0 -#define AVM_MOV_DYN_L2_GAS 0 -#define AVM_CMOV_DYN_L2_GAS 0 -#define AVM_SLOAD_DYN_L2_GAS 180 -#define AVM_SSTORE_DYN_L2_GAS 180 -#define AVM_NOTEHASHEXISTS_DYN_L2_GAS 0 -#define AVM_EMITNOTEHASH_DYN_L2_GAS 0 -#define AVM_NULLIFIEREXISTS_DYN_L2_GAS 0 -#define AVM_EMITNULLIFIER_DYN_L2_GAS 0 -#define AVM_L1TOL2MSGEXISTS_DYN_L2_GAS 0 -#define AVM_GETCONTRACTINSTANCE_DYN_L2_GAS 0 -#define AVM_EMITUNENCRYPTEDLOG_DYN_L2_GAS 180 -#define AVM_SENDL2TOL1MSG_DYN_L2_GAS 0 -#define AVM_CALL_DYN_L2_GAS 50 -#define AVM_STATICCALL_DYN_L2_GAS 50 -#define AVM_DELEGATECALL_DYN_L2_GAS 50 -#define AVM_RETURN_DYN_L2_GAS 50 -#define AVM_REVERT_DYN_L2_GAS 50 -#define AVM_DEBUGLOG_DYN_L2_GAS 0 -#define AVM_KECCAK_DYN_L2_GAS 0 -#define AVM_POSEIDON2_DYN_L2_GAS 0 -#define AVM_SHA256_DYN_L2_GAS 0 -#define AVM_PEDERSEN_DYN_L2_GAS 0 -#define AVM_ECADD_DYN_L2_GAS 0 -#define AVM_MSM_DYN_L2_GAS 0 -#define AVM_PEDERSENCOMMITMENT_DYN_L2_GAS 0 -#define AVM_TORADIXLE_DYN_L2_GAS 200 -#define AVM_SHA256COMPRESSION_DYN_L2_GAS 0 -#define AVM_KECCAKF1600_DYN_L2_GAS 0 +#define AVM_POSEIDON2_BASE_L2_GAS 78 +#define AVM_SHA256_BASE_L2_GAS 2610 +#define AVM_PEDERSEN_BASE_L2_GAS 1000 +#define AVM_ECADD_BASE_L2_GAS 62 +#define AVM_MSM_BASE_L2_GAS 1000 +#define AVM_PEDERSENCOMMITMENT_BASE_L2_GAS 1000 +#define AVM_TORADIXLE_BASE_L2_GAS 46 +#define AVM_SHA256COMPRESSION_BASE_L2_GAS 261 +#define AVM_KECCAKF1600_BASE_L2_GAS 300 +#define AVM_CALLDATACOPY_DYN_L2_GAS 6 +#define AVM_EMITUNENCRYPTEDLOG_DYN_L2_GAS 146 +#define AVM_CALL_DYN_L2_GAS 4 +#define AVM_STATICCALL_DYN_L2_GAS 4 +#define AVM_DELEGATECALL_DYN_L2_GAS 4 +#define AVM_RETURN_DYN_L2_GAS 6 +#define AVM_REVERT_DYN_L2_GAS 6 +#define AVM_KECCAK_DYN_L2_GAS 100 +#define AVM_SHA256_DYN_L2_GAS 100 +#define AVM_PEDERSEN_DYN_L2_GAS 100 +#define AVM_PEDERSENCOMMITMENT_DYN_L2_GAS 100 +#define AVM_MSM_DYN_L2_GAS 100 +#define AVM_TORADIXLE_DYN_L2_GAS 20 +#define AVM_SSTORE_BASE_DA_GAS 512 +#define AVM_EMITNOTEHASH_BASE_DA_GAS 512 +#define AVM_EMITNULLIFIER_BASE_DA_GAS 512 +#define AVM_SENDL2TOL1MSG_BASE_DA_GAS 512 +#define AVM_EMITUNENCRYPTEDLOG_DYN_DA_GAS 512 diff --git a/l1-contracts/src/core/libraries/ConstantsGen.sol b/l1-contracts/src/core/libraries/ConstantsGen.sol index de90972406b1..616f372fe3dc 100644 --- a/l1-contracts/src/core/libraries/ConstantsGen.sol +++ b/l1-contracts/src/core/libraries/ConstantsGen.sol @@ -127,9 +127,18 @@ library Constants { uint256 internal constant FIXED_DA_GAS = 512; uint256 internal constant FIXED_L2_GAS = 512; uint256 internal constant FIXED_AVM_STARTUP_L2_GAS = 1024; + uint256 internal constant L2_GAS_DISTRIBUTED_STORAGE_PREMIUM = 1024; + uint256 internal constant L2_GAS_PER_READ_MERKLE_HASH = 30; + uint256 internal constant L2_GAS_PER_WRITE_MERKLE_HASH = 40; + uint256 internal constant L2_GAS_PER_PUBLIC_DATA_UPDATE = 2624; + uint256 internal constant L2_GAS_PER_NOTE_HASH = 2304; + uint256 internal constant L2_GAS_PER_NULLIFIER = 2624; + uint256 internal constant L2_GAS_PER_PUBLIC_DATA_READ = 1200; + uint256 internal constant L2_GAS_PER_NOTE_HASH_READ_REQUEST = 960; + uint256 internal constant L2_GAS_PER_NULLIFIER_READ_REQUEST = 1200; + uint256 internal constant L2_GAS_PER_L1_TO_L2_MSG_READ_REQUEST = 480; uint256 internal constant L2_GAS_PER_LOG_BYTE = 4; - uint256 internal constant L2_GAS_PER_NOTE_HASH = 32; - uint256 internal constant L2_GAS_PER_NULLIFIER = 64; + uint256 internal constant L2_GAS_PER_L2_TO_L1_MSG = 200; uint256 internal constant CANONICAL_AUTH_REGISTRY_ADDRESS = 19361441716519463065948254497947932755739298943049449145365332870925554042208; uint256 internal constant DEPLOYER_CONTRACT_ADDRESS = diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr index 31f090e60882..be7b9c281277 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -178,14 +178,36 @@ global FIXED_DA_GAS: u32 = 512; // pays for fixed tx costs like validation, and updating state roots global FIXED_L2_GAS: u32 = 512; // base cost for a single public call -global FIXED_AVM_STARTUP_L2_GAS : u32= 1024; -// pays for hashing/validating logs +global FIXED_AVM_STARTUP_L2_GAS: u32 = 1024; + +// Some tree insertions incur an additional cost associated with +// the new database entry to be stored by all network participants. +global L2_GAS_DISTRIBUTED_STORAGE_PREMIUM: u32 = 1024; + +// Kernel and/or rollup circuits perform a hash per tree-level for insertions and reads. +// All network participants need to perform tree insertions. +// Not _all_ network participants need to perform membership checks, so they're cheaper. +global L2_GAS_PER_READ_MERKLE_HASH: u32 = 30; +global L2_GAS_PER_WRITE_MERKLE_HASH: u32 = 40; + +// Gas for tree insertions and associated storage +global L2_GAS_PER_PUBLIC_DATA_UPDATE: u32 = L2_GAS_DISTRIBUTED_STORAGE_PREMIUM + (PUBLIC_DATA_TREE_HEIGHT * L2_GAS_PER_WRITE_MERKLE_HASH); +global L2_GAS_PER_NOTE_HASH: u32 = L2_GAS_DISTRIBUTED_STORAGE_PREMIUM + (NOTE_HASH_TREE_HEIGHT * L2_GAS_PER_WRITE_MERKLE_HASH); +// 2x because insertion into indexed tree requires a low-leaf membership check and a standard insertion +global L2_GAS_PER_NULLIFIER: u32 = L2_GAS_DISTRIBUTED_STORAGE_PREMIUM + (2 * NULLIFIER_TREE_HEIGHT * L2_GAS_PER_WRITE_MERKLE_HASH); + +// Gas for tree read requests +global L2_GAS_PER_PUBLIC_DATA_READ: u32 = PUBLIC_DATA_TREE_HEIGHT * L2_GAS_PER_READ_MERKLE_HASH; +global L2_GAS_PER_NOTE_HASH_READ_REQUEST: u32 = NOTE_HASH_TREE_HEIGHT * L2_GAS_PER_READ_MERKLE_HASH; +// 2x because non-membership checks are really 2 membership checks +global L2_GAS_PER_NULLIFIER_READ_REQUEST: u32 = 2 * NULLIFIER_TREE_HEIGHT * L2_GAS_PER_READ_MERKLE_HASH; +global L2_GAS_PER_L1_TO_L2_MSG_READ_REQUEST: u32 = L1_TO_L2_MSG_TREE_HEIGHT * L2_GAS_PER_READ_MERKLE_HASH; + +// Gas for hashing and validating logs global L2_GAS_PER_LOG_BYTE: u32 = 4; -// pays for tree insertions. -global L2_GAS_PER_NOTE_HASH: u32 = 32; -// nullifiers are more expensive to insert/validate: -// they are inserted into an indexed tree and must be checked for duplicates -global L2_GAS_PER_NULLIFIER: u32 = 64; + +// Gas for writing message to L1 portal +global L2_GAS_PER_L2_TO_L1_MSG: u32 = 200; // CANONICAL CONTRACT ADDRESSES global CANONICAL_AUTH_REGISTRY_ADDRESS = AztecAddress::from_field(0x2ace300b02ca5ab0a25052b1e852913a47292096997ca09f758c0e3624e84560); @@ -410,134 +432,95 @@ global START_EMIT_UNENCRYPTED_LOG_WRITE_OFFSET: u32 = START_EMIT_L2_TO_L1_MSG_WR * GAS COSTS FOR AVM OPCODES * They are used in TS and C++ */ -global AVM_DEFAULT_BASE_GAS_L2: u16 = 100; -global AVM_DEFAULT_DYN_GAS_L2: u16 = 0; -// Base GAS. -global AVM_ADD_BASE_L2_GAS: u16 = 320; -global AVM_SUB_BASE_L2_GAS: u16 = 320; -global AVM_MUL_BASE_L2_GAS: u16 = 330; -global AVM_DIV_BASE_L2_GAS: u16 = 430; -global AVM_FDIV_BASE_L2_GAS: u16 = 320; -global AVM_EQ_BASE_L2_GAS: u16 = 320; -global AVM_LT_BASE_L2_GAS: u16 = 640; -global AVM_LTE_BASE_L2_GAS: u16 = 640; -global AVM_AND_BASE_L2_GAS: u16 = 330; -global AVM_OR_BASE_L2_GAS: u16 = 330; -global AVM_XOR_BASE_L2_GAS: u16 = 320; -global AVM_NOT_BASE_L2_GAS: u16 = 270; -global AVM_SHL_BASE_L2_GAS: u16 = 320; -global AVM_SHR_BASE_L2_GAS: u16 = 320; -global AVM_CAST_BASE_L2_GAS: u16 = 300; -global AVM_ADDRESS_BASE_L2_GAS: u16 = 200; -global AVM_STORAGEADDRESS_BASE_L2_GAS: u16 = 200; -global AVM_SENDER_BASE_L2_GAS: u16 = 200; -global AVM_FUNCTIONSELECTOR_BASE_L2_GAS: u16 = 200; -global AVM_TRANSACTIONFEE_BASE_L2_GAS: u16 = 200; -global AVM_CHAINID_BASE_L2_GAS: u16 = 200; -global AVM_VERSION_BASE_L2_GAS: u16 = 200; -global AVM_BLOCKNUMBER_BASE_L2_GAS: u16 = 200; -global AVM_TIMESTAMP_BASE_L2_GAS: u16 = 200; -global AVM_FEEPERL2GAS_BASE_L2_GAS: u16 = 200; -global AVM_FEEPERDAGAS_BASE_L2_GAS: u16 = 200; -global AVM_CALLDATACOPY_BASE_L2_GAS: u16 = 290; -global AVM_L2GASLEFT_BASE_L2_GAS: u16 = 180; -global AVM_DAGASLEFT_BASE_L2_GAS: u16 = 180; -global AVM_JUMP_BASE_L2_GAS: u16 = 120; -global AVM_JUMPI_BASE_L2_GAS: u16 = 180; -global AVM_INTERNALCALL_BASE_L2_GAS: u16 = 180; -global AVM_INTERNALRETURN_BASE_L2_GAS: u16 = 180; -global AVM_SET_BASE_L2_GAS: u16 = 180; -global AVM_MOV_BASE_L2_GAS: u16 = 230; -global AVM_CMOV_BASE_L2_GAS: u16 = 340; -global AVM_SLOAD_BASE_L2_GAS: u16 = 180; -global AVM_SSTORE_BASE_L2_GAS: u16 = 180; -global AVM_NOTEHASHEXISTS_BASE_L2_GAS: u16 = 260; -global AVM_EMITNOTEHASH_BASE_L2_GAS: u16 = 260; -global AVM_NULLIFIEREXISTS_BASE_L2_GAS: u16 = 260; -global AVM_EMITNULLIFIER_BASE_L2_GAS: u16 = 260; -global AVM_L1TOL2MSGEXISTS_BASE_L2_GAS: u16 = 260; -global AVM_GETCONTRACTINSTANCE_BASE_L2_GAS: u16 = 480; -global AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS: u16 = 180; -global AVM_SENDL2TOL1MSG_BASE_L2_GAS: u16 = 260; -global AVM_CALL_BASE_L2_GAS: u16 = 450; -global AVM_STATICCALL_BASE_L2_GAS: u16 = 450; -global AVM_DELEGATECALL_BASE_L2_GAS: u16 = 0; -global AVM_RETURN_BASE_L2_GAS: u16 = 140; -global AVM_REVERT_BASE_L2_GAS: u16 = 140; -global AVM_DEBUGLOG_BASE_L2_GAS: u16 = 100; -global AVM_KECCAK_BASE_L2_GAS: u16 = 3000; -global AVM_POSEIDON2_BASE_L2_GAS: u16 = 670; -global AVM_SHA256_BASE_L2_GAS: u16 = 3000; -global AVM_PEDERSEN_BASE_L2_GAS: u16 = 3000; -global AVM_ECADD_BASE_L2_GAS: u16 = 900; -global AVM_MSM_BASE_L2_GAS: u16 = 900; -global AVM_PEDERSENCOMMITMENT_BASE_L2_GAS: u16 = 3000; -global AVM_TORADIXLE_BASE_L2_GAS: u16 = 200; -global AVM_SHA256COMPRESSION_BASE_L2_GAS: u16 = 3000; -global AVM_KECCAKF1600_BASE_L2_GAS: u16 = 3000; -// Dynamic GAS -global AVM_ADD_DYN_L2_GAS: u16 = 0; -global AVM_SUB_DYN_L2_GAS: u16 = 0; -global AVM_MUL_DYN_L2_GAS: u16 = 0; -global AVM_DIV_DYN_L2_GAS: u16 = 0; -global AVM_FDIV_DYN_L2_GAS: u16 = 0; -global AVM_EQ_DYN_L2_GAS: u16 = 0; -global AVM_LT_DYN_L2_GAS: u16 = 0; -global AVM_LTE_DYN_L2_GAS: u16 = 0; -global AVM_AND_DYN_L2_GAS: u16 = 0; -global AVM_OR_DYN_L2_GAS: u16 = 0; -global AVM_XOR_DYN_L2_GAS: u16 = 0; -global AVM_NOT_DYN_L2_GAS: u16 = 0; -global AVM_SHL_DYN_L2_GAS: u16 = 0; -global AVM_SHR_DYN_L2_GAS: u16 = 0; -global AVM_CAST_DYN_L2_GAS: u16 = 0; -global AVM_ADDRESS_DYN_L2_GAS: u16 = 0; -global AVM_STORAGEADDRESS_DYN_L2_GAS: u16 = 0; -global AVM_SENDER_DYN_L2_GAS: u16 = 0; -global AVM_FUNCTIONSELECTOR_DYN_L2_GAS: u16 = 0; -global AVM_TRANSACTIONFEE_DYN_L2_GAS: u16 = 0; -global AVM_CHAINID_DYN_L2_GAS: u16 = 0; -global AVM_VERSION_DYN_L2_GAS: u16 = 0; -global AVM_BLOCKNUMBER_DYN_L2_GAS: u16 = 0; -global AVM_TIMESTAMP_DYN_L2_GAS: u16 = 0; -global AVM_FEEPERL2GAS_DYN_L2_GAS: u16 = 0; -global AVM_FEEPERDAGAS_DYN_L2_GAS: u16 = 0; -global AVM_CALLDATACOPY_DYN_L2_GAS: u16 = 50; -global AVM_L2GASLEFT_DYN_L2_GAS: u16 = 0; -global AVM_DAGASLEFT_DYN_L2_GAS: u16 = 0; -global AVM_JUMP_DYN_L2_GAS: u16 = 0; -global AVM_JUMPI_DYN_L2_GAS: u16 = 0; -global AVM_INTERNALCALL_DYN_L2_GAS: u16 = 0; -global AVM_INTERNALRETURN_DYN_L2_GAS: u16 = 0; -global AVM_SET_DYN_L2_GAS: u16 = 0; -global AVM_MOV_DYN_L2_GAS: u16 = 0; -global AVM_CMOV_DYN_L2_GAS: u16 = 0; -global AVM_SLOAD_DYN_L2_GAS: u16 = 180; -global AVM_SSTORE_DYN_L2_GAS: u16 = 180; -global AVM_NOTEHASHEXISTS_DYN_L2_GAS: u16 = 0; -global AVM_EMITNOTEHASH_DYN_L2_GAS: u16 = 0; -global AVM_NULLIFIEREXISTS_DYN_L2_GAS: u16 = 0; -global AVM_EMITNULLIFIER_DYN_L2_GAS: u16 = 0; -global AVM_L1TOL2MSGEXISTS_DYN_L2_GAS: u16 = 0; -global AVM_GETCONTRACTINSTANCE_DYN_L2_GAS: u16 = 0; -global AVM_EMITUNENCRYPTEDLOG_DYN_L2_GAS: u16 = 180; -global AVM_SENDL2TOL1MSG_DYN_L2_GAS: u16 = 0; -global AVM_CALL_DYN_L2_GAS: u16 = 50; -global AVM_STATICCALL_DYN_L2_GAS: u16 = 50; -global AVM_DELEGATECALL_DYN_L2_GAS: u16 = 50; -global AVM_RETURN_DYN_L2_GAS: u16 = 50; -global AVM_REVERT_DYN_L2_GAS: u16 = 50; -global AVM_DEBUGLOG_DYN_L2_GAS: u16 = 0; -global AVM_KECCAK_DYN_L2_GAS: u16 = 0; -global AVM_POSEIDON2_DYN_L2_GAS: u16 = 0; -global AVM_SHA256_DYN_L2_GAS: u16 = 0; -global AVM_PEDERSEN_DYN_L2_GAS: u16 = 0; -global AVM_ECADD_DYN_L2_GAS: u16 = 0; -global AVM_MSM_DYN_L2_GAS: u16 = 0; -global AVM_PEDERSENCOMMITMENT_DYN_L2_GAS: u16 = 0; -global AVM_TORADIXLE_DYN_L2_GAS: u16 = 200; -global AVM_SHA256COMPRESSION_DYN_L2_GAS: u16 = 0; -global AVM_KECCAKF1600_DYN_L2_GAS: u16 = 0; +// Base L2 GAS +// Note: magic numbers here are derived from each op's AVM circuit trace area +global AVM_ADD_BASE_L2_GAS: u32 = 32; +global AVM_SUB_BASE_L2_GAS: u32 = 32; +global AVM_MUL_BASE_L2_GAS: u32 = 33; +global AVM_DIV_BASE_L2_GAS: u32 = 43; +global AVM_FDIV_BASE_L2_GAS: u32 = 32; +global AVM_EQ_BASE_L2_GAS: u32 = 32; +global AVM_LT_BASE_L2_GAS: u32 = 64; +global AVM_LTE_BASE_L2_GAS: u32 = 64; +global AVM_AND_BASE_L2_GAS: u32 = 33; +global AVM_OR_BASE_L2_GAS: u32 = 33; +global AVM_XOR_BASE_L2_GAS: u32 = 33; +global AVM_NOT_BASE_L2_GAS: u32 = 27; +global AVM_SHL_BASE_L2_GAS: u32 = 32; +global AVM_SHR_BASE_L2_GAS: u32 = 32; +global AVM_CAST_BASE_L2_GAS: u32 = 30; +global AVM_ADDRESS_BASE_L2_GAS: u32 = 20; +global AVM_STORAGEADDRESS_BASE_L2_GAS: u32 = 20; +global AVM_SENDER_BASE_L2_GAS: u32 = 200; +global AVM_FUNCTIONSELECTOR_BASE_L2_GAS: u32 = 20; +global AVM_TRANSACTIONFEE_BASE_L2_GAS: u32 = 200; +global AVM_CHAINID_BASE_L2_GAS: u32 = 20; +global AVM_VERSION_BASE_L2_GAS: u32 = 20; +global AVM_BLOCKNUMBER_BASE_L2_GAS: u32 = 20; +global AVM_TIMESTAMP_BASE_L2_GAS: u32 = 20; +global AVM_FEEPERL2GAS_BASE_L2_GAS: u32 = 20; +global AVM_FEEPERDAGAS_BASE_L2_GAS: u32 = 20; +global AVM_CALLDATACOPY_BASE_L2_GAS: u32 = 29; +global AVM_L2GASLEFT_BASE_L2_GAS: u32 = 18; +global AVM_DAGASLEFT_BASE_L2_GAS: u32 = 18; +global AVM_JUMP_BASE_L2_GAS: u32 = 12; +global AVM_JUMPI_BASE_L2_GAS: u32 = 18; +global AVM_INTERNALCALL_BASE_L2_GAS: u32 = 18; +global AVM_INTERNALRETURN_BASE_L2_GAS: u32 = 18; +global AVM_SET_BASE_L2_GAS: u32 = 18; +global AVM_MOV_BASE_L2_GAS: u32 = 23; +global AVM_CMOV_BASE_L2_GAS: u32 = 34; +global AVM_SLOAD_BASE_L2_GAS: u32 = 18 + L2_GAS_PER_PUBLIC_DATA_READ; +global AVM_SSTORE_BASE_L2_GAS: u32 = 18 + L2_GAS_PER_PUBLIC_DATA_UPDATE; +global AVM_NOTEHASHEXISTS_BASE_L2_GAS: u32 = 26 + L2_GAS_PER_NOTE_HASH_READ_REQUEST; +global AVM_EMITNOTEHASH_BASE_L2_GAS: u32 = 26 + L2_GAS_PER_NOTE_HASH; +global AVM_NULLIFIEREXISTS_BASE_L2_GAS: u32 = 26 + L2_GAS_PER_NULLIFIER_READ_REQUEST; +global AVM_EMITNULLIFIER_BASE_L2_GAS: u32 = 26 + L2_GAS_PER_NULLIFIER; +global AVM_L1TOL2MSGEXISTS_BASE_L2_GAS: u32 = 26 + L2_GAS_PER_L1_TO_L2_MSG_READ_REQUEST; +global AVM_GETCONTRACTINSTANCE_BASE_L2_GAS: u32 = 48 + L2_GAS_PER_NULLIFIER_READ_REQUEST; // does a nullifier check +global AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS: u32 = 18; +global AVM_SENDL2TOL1MSG_BASE_L2_GAS: u32 = 26 + L2_GAS_PER_L2_TO_L1_MSG; +// On CALL, AVM performs nullifier checks for contract address & contract class ID +global AVM_CALL_BASE_L2_GAS: u32 = 45 + (2 * L2_GAS_PER_NULLIFIER_READ_REQUEST); +global AVM_STATICCALL_BASE_L2_GAS: u32 = 45 + (2 * L2_GAS_PER_NULLIFIER_READ_REQUEST); +global AVM_DELEGATECALL_BASE_L2_GAS: u32 = 45 + (2 * L2_GAS_PER_NULLIFIER_READ_REQUEST); +global AVM_RETURN_BASE_L2_GAS: u32 = 28; +global AVM_REVERT_BASE_L2_GAS: u32 = 28; +global AVM_DEBUGLOG_BASE_L2_GAS: u32 = 10; +global AVM_KECCAK_BASE_L2_GAS: u32 = 3000; +global AVM_POSEIDON2_BASE_L2_GAS: u32 = 78; +global AVM_SHA256_BASE_L2_GAS: u32 = 2610; +global AVM_PEDERSEN_BASE_L2_GAS: u32 = 1000; +global AVM_ECADD_BASE_L2_GAS: u32 = 62; +global AVM_MSM_BASE_L2_GAS: u32 = 1000; +global AVM_PEDERSENCOMMITMENT_BASE_L2_GAS: u32 = 1000; +global AVM_TORADIXLE_BASE_L2_GAS: u32 = 46; +global AVM_SHA256COMPRESSION_BASE_L2_GAS: u32 = 261; +global AVM_KECCAKF1600_BASE_L2_GAS: u32 = 300; +// Dynamic L2 GAS +global AVM_CALLDATACOPY_DYN_L2_GAS: u32 = 6; +// a single increment here corresponds to an entire additional field (hence x32 bytes per field) +global AVM_EMITUNENCRYPTEDLOG_DYN_L2_GAS: u32 = 18 + (DA_BYTES_PER_FIELD * L2_GAS_PER_LOG_BYTE); +global AVM_CALL_DYN_L2_GAS: u32 = 4; +global AVM_STATICCALL_DYN_L2_GAS: u32 = 4; +global AVM_DELEGATECALL_DYN_L2_GAS: u32 = 4; +global AVM_RETURN_DYN_L2_GAS: u32 = 6; +global AVM_REVERT_DYN_L2_GAS: u32 = 6; +global AVM_KECCAK_DYN_L2_GAS: u32 = 100; +global AVM_SHA256_DYN_L2_GAS: u32 = 100; +global AVM_PEDERSEN_DYN_L2_GAS: u32 = 100; +global AVM_PEDERSENCOMMITMENT_DYN_L2_GAS: u32 = 100; +global AVM_MSM_DYN_L2_GAS: u32 = 100; +global AVM_TORADIXLE_DYN_L2_GAS: u32 = 20; + +// Base DA Gas +global AVM_SSTORE_BASE_DA_GAS: u32 = DA_BYTES_PER_FIELD * DA_GAS_PER_BYTE; +global AVM_EMITNOTEHASH_BASE_DA_GAS: u32 = DA_BYTES_PER_FIELD * DA_GAS_PER_BYTE; +global AVM_EMITNULLIFIER_BASE_DA_GAS: u32 = DA_BYTES_PER_FIELD * DA_GAS_PER_BYTE; +global AVM_SENDL2TOL1MSG_BASE_DA_GAS: u32 = DA_BYTES_PER_FIELD * DA_GAS_PER_BYTE; +// Dynamic DA Gas +// a single increment here corresponds to an entire additional field (hence x32 bytes per field) +global AVM_EMITUNENCRYPTEDLOG_DYN_DA_GAS: u32 = DA_BYTES_PER_FIELD * DA_GAS_PER_BYTE; // Constants related to proof type of a recursive proof verification. // Keep following constants in sync with the enum acir_format::PROOF_TYPE in recursion_constraint.hpp diff --git a/yarn-project/circuits.js/src/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index 112ed2455438..64f2820c01ad 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -112,9 +112,18 @@ export const DA_GAS_PER_BYTE = 16; export const FIXED_DA_GAS = 512; export const FIXED_L2_GAS = 512; export const FIXED_AVM_STARTUP_L2_GAS = 1024; +export const L2_GAS_DISTRIBUTED_STORAGE_PREMIUM = 1024; +export const L2_GAS_PER_READ_MERKLE_HASH = 30; +export const L2_GAS_PER_WRITE_MERKLE_HASH = 40; +export const L2_GAS_PER_PUBLIC_DATA_UPDATE = 2624; +export const L2_GAS_PER_NOTE_HASH = 2304; +export const L2_GAS_PER_NULLIFIER = 2624; +export const L2_GAS_PER_PUBLIC_DATA_READ = 1200; +export const L2_GAS_PER_NOTE_HASH_READ_REQUEST = 960; +export const L2_GAS_PER_NULLIFIER_READ_REQUEST = 1200; +export const L2_GAS_PER_L1_TO_L2_MSG_READ_REQUEST = 480; export const L2_GAS_PER_LOG_BYTE = 4; -export const L2_GAS_PER_NOTE_HASH = 32; -export const L2_GAS_PER_NULLIFIER = 64; +export const L2_GAS_PER_L2_TO_L1_MSG = 200; export const CANONICAL_AUTH_REGISTRY_ADDRESS = 19361441716519463065948254497947932755739298943049449145365332870925554042208n; export const DEPLOYER_CONTRACT_ADDRESS = 17119407406465801909352274670277571579675739451008438338071219340964365249977n; @@ -235,132 +244,86 @@ export const START_EMIT_NOTE_HASH_WRITE_OFFSET = 128; export const START_EMIT_NULLIFIER_WRITE_OFFSET = 144; export const START_EMIT_L2_TO_L1_MSG_WRITE_OFFSET = 160; export const START_EMIT_UNENCRYPTED_LOG_WRITE_OFFSET = 162; -export const AVM_DEFAULT_BASE_GAS_L2 = 100; -export const AVM_DEFAULT_DYN_GAS_L2 = 0; -export const AVM_ADD_BASE_L2_GAS = 320; -export const AVM_SUB_BASE_L2_GAS = 320; -export const AVM_MUL_BASE_L2_GAS = 330; -export const AVM_DIV_BASE_L2_GAS = 430; -export const AVM_FDIV_BASE_L2_GAS = 320; -export const AVM_EQ_BASE_L2_GAS = 320; -export const AVM_LT_BASE_L2_GAS = 640; -export const AVM_LTE_BASE_L2_GAS = 640; -export const AVM_AND_BASE_L2_GAS = 330; -export const AVM_OR_BASE_L2_GAS = 330; -export const AVM_XOR_BASE_L2_GAS = 320; -export const AVM_NOT_BASE_L2_GAS = 270; -export const AVM_SHL_BASE_L2_GAS = 320; -export const AVM_SHR_BASE_L2_GAS = 320; -export const AVM_CAST_BASE_L2_GAS = 300; -export const AVM_ADDRESS_BASE_L2_GAS = 200; -export const AVM_STORAGEADDRESS_BASE_L2_GAS = 200; +export const AVM_ADD_BASE_L2_GAS = 32; +export const AVM_SUB_BASE_L2_GAS = 32; +export const AVM_MUL_BASE_L2_GAS = 33; +export const AVM_DIV_BASE_L2_GAS = 43; +export const AVM_FDIV_BASE_L2_GAS = 32; +export const AVM_EQ_BASE_L2_GAS = 32; +export const AVM_LT_BASE_L2_GAS = 64; +export const AVM_LTE_BASE_L2_GAS = 64; +export const AVM_AND_BASE_L2_GAS = 33; +export const AVM_OR_BASE_L2_GAS = 33; +export const AVM_XOR_BASE_L2_GAS = 33; +export const AVM_NOT_BASE_L2_GAS = 27; +export const AVM_SHL_BASE_L2_GAS = 32; +export const AVM_SHR_BASE_L2_GAS = 32; +export const AVM_CAST_BASE_L2_GAS = 30; +export const AVM_ADDRESS_BASE_L2_GAS = 20; +export const AVM_STORAGEADDRESS_BASE_L2_GAS = 20; export const AVM_SENDER_BASE_L2_GAS = 200; -export const AVM_FUNCTIONSELECTOR_BASE_L2_GAS = 200; +export const AVM_FUNCTIONSELECTOR_BASE_L2_GAS = 20; export const AVM_TRANSACTIONFEE_BASE_L2_GAS = 200; -export const AVM_CHAINID_BASE_L2_GAS = 200; -export const AVM_VERSION_BASE_L2_GAS = 200; -export const AVM_BLOCKNUMBER_BASE_L2_GAS = 200; -export const AVM_TIMESTAMP_BASE_L2_GAS = 200; -export const AVM_FEEPERL2GAS_BASE_L2_GAS = 200; -export const AVM_FEEPERDAGAS_BASE_L2_GAS = 200; -export const AVM_CALLDATACOPY_BASE_L2_GAS = 290; -export const AVM_L2GASLEFT_BASE_L2_GAS = 180; -export const AVM_DAGASLEFT_BASE_L2_GAS = 180; -export const AVM_JUMP_BASE_L2_GAS = 120; -export const AVM_JUMPI_BASE_L2_GAS = 180; -export const AVM_INTERNALCALL_BASE_L2_GAS = 180; -export const AVM_INTERNALRETURN_BASE_L2_GAS = 180; -export const AVM_SET_BASE_L2_GAS = 180; -export const AVM_MOV_BASE_L2_GAS = 230; -export const AVM_CMOV_BASE_L2_GAS = 340; -export const AVM_SLOAD_BASE_L2_GAS = 180; -export const AVM_SSTORE_BASE_L2_GAS = 180; -export const AVM_NOTEHASHEXISTS_BASE_L2_GAS = 260; -export const AVM_EMITNOTEHASH_BASE_L2_GAS = 260; -export const AVM_NULLIFIEREXISTS_BASE_L2_GAS = 260; -export const AVM_EMITNULLIFIER_BASE_L2_GAS = 260; -export const AVM_L1TOL2MSGEXISTS_BASE_L2_GAS = 260; -export const AVM_GETCONTRACTINSTANCE_BASE_L2_GAS = 480; -export const AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS = 180; -export const AVM_SENDL2TOL1MSG_BASE_L2_GAS = 260; -export const AVM_CALL_BASE_L2_GAS = 450; -export const AVM_STATICCALL_BASE_L2_GAS = 450; -export const AVM_DELEGATECALL_BASE_L2_GAS = 0; -export const AVM_RETURN_BASE_L2_GAS = 140; -export const AVM_REVERT_BASE_L2_GAS = 140; -export const AVM_DEBUGLOG_BASE_L2_GAS = 100; +export const AVM_CHAINID_BASE_L2_GAS = 20; +export const AVM_VERSION_BASE_L2_GAS = 20; +export const AVM_BLOCKNUMBER_BASE_L2_GAS = 20; +export const AVM_TIMESTAMP_BASE_L2_GAS = 20; +export const AVM_FEEPERL2GAS_BASE_L2_GAS = 20; +export const AVM_FEEPERDAGAS_BASE_L2_GAS = 20; +export const AVM_CALLDATACOPY_BASE_L2_GAS = 29; +export const AVM_L2GASLEFT_BASE_L2_GAS = 18; +export const AVM_DAGASLEFT_BASE_L2_GAS = 18; +export const AVM_JUMP_BASE_L2_GAS = 12; +export const AVM_JUMPI_BASE_L2_GAS = 18; +export const AVM_INTERNALCALL_BASE_L2_GAS = 18; +export const AVM_INTERNALRETURN_BASE_L2_GAS = 18; +export const AVM_SET_BASE_L2_GAS = 18; +export const AVM_MOV_BASE_L2_GAS = 23; +export const AVM_CMOV_BASE_L2_GAS = 34; +export const AVM_SLOAD_BASE_L2_GAS = 1218; +export const AVM_SSTORE_BASE_L2_GAS = 2642; +export const AVM_NOTEHASHEXISTS_BASE_L2_GAS = 986; +export const AVM_EMITNOTEHASH_BASE_L2_GAS = 2330; +export const AVM_NULLIFIEREXISTS_BASE_L2_GAS = 1226; +export const AVM_EMITNULLIFIER_BASE_L2_GAS = 2650; +export const AVM_L1TOL2MSGEXISTS_BASE_L2_GAS = 506; +export const AVM_GETCONTRACTINSTANCE_BASE_L2_GAS = 1248; +export const AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS = 18; +export const AVM_SENDL2TOL1MSG_BASE_L2_GAS = 226; +export const AVM_CALL_BASE_L2_GAS = 2445; +export const AVM_STATICCALL_BASE_L2_GAS = 2445; +export const AVM_DELEGATECALL_BASE_L2_GAS = 2445; +export const AVM_RETURN_BASE_L2_GAS = 28; +export const AVM_REVERT_BASE_L2_GAS = 28; +export const AVM_DEBUGLOG_BASE_L2_GAS = 10; export const AVM_KECCAK_BASE_L2_GAS = 3000; -export const AVM_POSEIDON2_BASE_L2_GAS = 670; -export const AVM_SHA256_BASE_L2_GAS = 3000; -export const AVM_PEDERSEN_BASE_L2_GAS = 3000; -export const AVM_ECADD_BASE_L2_GAS = 900; -export const AVM_MSM_BASE_L2_GAS = 900; -export const AVM_PEDERSENCOMMITMENT_BASE_L2_GAS = 3000; -export const AVM_TORADIXLE_BASE_L2_GAS = 200; -export const AVM_SHA256COMPRESSION_BASE_L2_GAS = 3000; -export const AVM_KECCAKF1600_BASE_L2_GAS = 3000; -export const AVM_ADD_DYN_L2_GAS = 0; -export const AVM_SUB_DYN_L2_GAS = 0; -export const AVM_MUL_DYN_L2_GAS = 0; -export const AVM_DIV_DYN_L2_GAS = 0; -export const AVM_FDIV_DYN_L2_GAS = 0; -export const AVM_EQ_DYN_L2_GAS = 0; -export const AVM_LT_DYN_L2_GAS = 0; -export const AVM_LTE_DYN_L2_GAS = 0; -export const AVM_AND_DYN_L2_GAS = 0; -export const AVM_OR_DYN_L2_GAS = 0; -export const AVM_XOR_DYN_L2_GAS = 0; -export const AVM_NOT_DYN_L2_GAS = 0; -export const AVM_SHL_DYN_L2_GAS = 0; -export const AVM_SHR_DYN_L2_GAS = 0; -export const AVM_CAST_DYN_L2_GAS = 0; -export const AVM_ADDRESS_DYN_L2_GAS = 0; -export const AVM_STORAGEADDRESS_DYN_L2_GAS = 0; -export const AVM_SENDER_DYN_L2_GAS = 0; -export const AVM_FUNCTIONSELECTOR_DYN_L2_GAS = 0; -export const AVM_TRANSACTIONFEE_DYN_L2_GAS = 0; -export const AVM_CHAINID_DYN_L2_GAS = 0; -export const AVM_VERSION_DYN_L2_GAS = 0; -export const AVM_BLOCKNUMBER_DYN_L2_GAS = 0; -export const AVM_TIMESTAMP_DYN_L2_GAS = 0; -export const AVM_FEEPERL2GAS_DYN_L2_GAS = 0; -export const AVM_FEEPERDAGAS_DYN_L2_GAS = 0; -export const AVM_CALLDATACOPY_DYN_L2_GAS = 50; -export const AVM_L2GASLEFT_DYN_L2_GAS = 0; -export const AVM_DAGASLEFT_DYN_L2_GAS = 0; -export const AVM_JUMP_DYN_L2_GAS = 0; -export const AVM_JUMPI_DYN_L2_GAS = 0; -export const AVM_INTERNALCALL_DYN_L2_GAS = 0; -export const AVM_INTERNALRETURN_DYN_L2_GAS = 0; -export const AVM_SET_DYN_L2_GAS = 0; -export const AVM_MOV_DYN_L2_GAS = 0; -export const AVM_CMOV_DYN_L2_GAS = 0; -export const AVM_SLOAD_DYN_L2_GAS = 180; -export const AVM_SSTORE_DYN_L2_GAS = 180; -export const AVM_NOTEHASHEXISTS_DYN_L2_GAS = 0; -export const AVM_EMITNOTEHASH_DYN_L2_GAS = 0; -export const AVM_NULLIFIEREXISTS_DYN_L2_GAS = 0; -export const AVM_EMITNULLIFIER_DYN_L2_GAS = 0; -export const AVM_L1TOL2MSGEXISTS_DYN_L2_GAS = 0; -export const AVM_GETCONTRACTINSTANCE_DYN_L2_GAS = 0; -export const AVM_EMITUNENCRYPTEDLOG_DYN_L2_GAS = 180; -export const AVM_SENDL2TOL1MSG_DYN_L2_GAS = 0; -export const AVM_CALL_DYN_L2_GAS = 50; -export const AVM_STATICCALL_DYN_L2_GAS = 50; -export const AVM_DELEGATECALL_DYN_L2_GAS = 50; -export const AVM_RETURN_DYN_L2_GAS = 50; -export const AVM_REVERT_DYN_L2_GAS = 50; -export const AVM_DEBUGLOG_DYN_L2_GAS = 0; -export const AVM_KECCAK_DYN_L2_GAS = 0; -export const AVM_POSEIDON2_DYN_L2_GAS = 0; -export const AVM_SHA256_DYN_L2_GAS = 0; -export const AVM_PEDERSEN_DYN_L2_GAS = 0; -export const AVM_ECADD_DYN_L2_GAS = 0; -export const AVM_MSM_DYN_L2_GAS = 0; -export const AVM_PEDERSENCOMMITMENT_DYN_L2_GAS = 0; -export const AVM_TORADIXLE_DYN_L2_GAS = 200; -export const AVM_SHA256COMPRESSION_DYN_L2_GAS = 0; -export const AVM_KECCAKF1600_DYN_L2_GAS = 0; +export const AVM_POSEIDON2_BASE_L2_GAS = 78; +export const AVM_SHA256_BASE_L2_GAS = 2610; +export const AVM_PEDERSEN_BASE_L2_GAS = 1000; +export const AVM_ECADD_BASE_L2_GAS = 62; +export const AVM_MSM_BASE_L2_GAS = 1000; +export const AVM_PEDERSENCOMMITMENT_BASE_L2_GAS = 1000; +export const AVM_TORADIXLE_BASE_L2_GAS = 46; +export const AVM_SHA256COMPRESSION_BASE_L2_GAS = 261; +export const AVM_KECCAKF1600_BASE_L2_GAS = 300; +export const AVM_CALLDATACOPY_DYN_L2_GAS = 6; +export const AVM_EMITUNENCRYPTEDLOG_DYN_L2_GAS = 146; +export const AVM_CALL_DYN_L2_GAS = 4; +export const AVM_STATICCALL_DYN_L2_GAS = 4; +export const AVM_DELEGATECALL_DYN_L2_GAS = 4; +export const AVM_RETURN_DYN_L2_GAS = 6; +export const AVM_REVERT_DYN_L2_GAS = 6; +export const AVM_KECCAK_DYN_L2_GAS = 100; +export const AVM_SHA256_DYN_L2_GAS = 100; +export const AVM_PEDERSEN_DYN_L2_GAS = 100; +export const AVM_PEDERSENCOMMITMENT_DYN_L2_GAS = 100; +export const AVM_MSM_DYN_L2_GAS = 100; +export const AVM_TORADIXLE_DYN_L2_GAS = 20; +export const AVM_SSTORE_BASE_DA_GAS = 512; +export const AVM_EMITNOTEHASH_BASE_DA_GAS = 512; +export const AVM_EMITNULLIFIER_BASE_DA_GAS = 512; +export const AVM_SENDL2TOL1MSG_BASE_DA_GAS = 512; +export const AVM_EMITUNENCRYPTEDLOG_DYN_DA_GAS = 512; export const PROOF_TYPE_PLONK = 0; export const PROOF_TYPE_HONK = 1; export const PROOF_TYPE_OINK = 2; diff --git a/yarn-project/simulator/src/avm/avm_gas.ts b/yarn-project/simulator/src/avm/avm_gas.ts index b0c3c79e3c8c..d9510998c5f3 100644 --- a/yarn-project/simulator/src/avm/avm_gas.ts +++ b/yarn-project/simulator/src/avm/avm_gas.ts @@ -53,10 +53,10 @@ function makeCost(l2Gas: number, daGas: number): Gas { } /** Dimensions of gas usage: L1, L2, and DA. */ -export const GasDimensions = ['l2Gas', 'daGas'] as const; +export const GAS_DIMENSIONS = ['l2Gas', 'daGas'] as const; /** Base gas costs for each instruction. Additional gas cost may be added on top due to memory or storage accesses, etc. */ -const BaseGasCosts: Record = { +const BASE_GAS_COSTS: Record = { [Opcode.ADD_8]: makeCost(c.AVM_ADD_BASE_L2_GAS, 0), [Opcode.ADD_16]: makeCost(c.AVM_ADD_BASE_L2_GAS, 0), [Opcode.SUB_8]: makeCost(c.AVM_SUB_BASE_L2_GAS, 0), @@ -115,14 +115,14 @@ const BaseGasCosts: Record = { [Opcode.MOV_16]: makeCost(c.AVM_MOV_BASE_L2_GAS, 0), [Opcode.CMOV]: makeCost(c.AVM_CMOV_BASE_L2_GAS, 0), [Opcode.SLOAD]: makeCost(c.AVM_SLOAD_BASE_L2_GAS, 0), - [Opcode.SSTORE]: makeCost(c.AVM_SSTORE_BASE_L2_GAS, 0), + [Opcode.SSTORE]: makeCost(c.AVM_SSTORE_BASE_L2_GAS, c.AVM_SSTORE_BASE_DA_GAS), [Opcode.NOTEHASHEXISTS]: makeCost(c.AVM_NOTEHASHEXISTS_BASE_L2_GAS, 0), - [Opcode.EMITNOTEHASH]: makeCost(c.AVM_EMITNOTEHASH_BASE_L2_GAS, 0), + [Opcode.EMITNOTEHASH]: makeCost(c.AVM_EMITNOTEHASH_BASE_L2_GAS, c.AVM_EMITNOTEHASH_BASE_DA_GAS), [Opcode.NULLIFIEREXISTS]: makeCost(c.AVM_NULLIFIEREXISTS_BASE_L2_GAS, 0), - [Opcode.EMITNULLIFIER]: makeCost(c.AVM_EMITNULLIFIER_BASE_L2_GAS, 0), + [Opcode.EMITNULLIFIER]: makeCost(c.AVM_EMITNULLIFIER_BASE_L2_GAS, c.AVM_EMITNULLIFIER_BASE_DA_GAS), [Opcode.L1TOL2MSGEXISTS]: makeCost(c.AVM_L1TOL2MSGEXISTS_BASE_L2_GAS, 0), [Opcode.EMITUNENCRYPTEDLOG]: makeCost(c.AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS, 0), - [Opcode.SENDL2TOL1MSG]: makeCost(c.AVM_SENDL2TOL1MSG_BASE_L2_GAS, 0), + [Opcode.SENDL2TOL1MSG]: makeCost(c.AVM_SENDL2TOL1MSG_BASE_L2_GAS, c.AVM_SENDL2TOL1MSG_BASE_DA_GAS), [Opcode.GETCONTRACTINSTANCE]: makeCost(c.AVM_GETCONTRACTINSTANCE_BASE_L2_GAS, 0), [Opcode.CALL]: makeCost(c.AVM_CALL_BASE_L2_GAS, 0), [Opcode.STATICCALL]: makeCost(c.AVM_STATICCALL_BASE_L2_GAS, 0), @@ -143,100 +143,30 @@ const BaseGasCosts: Record = { [Opcode.KECCAKF1600]: makeCost(c.AVM_KECCAKF1600_BASE_L2_GAS, 0), }; -const DynamicGasCosts: Record = { - [Opcode.ADD_8]: makeCost(c.AVM_ADD_DYN_L2_GAS, 0), - [Opcode.ADD_16]: makeCost(c.AVM_ADD_DYN_L2_GAS, 0), - [Opcode.SUB_8]: makeCost(c.AVM_SUB_DYN_L2_GAS, 0), - [Opcode.SUB_16]: makeCost(c.AVM_SUB_DYN_L2_GAS, 0), - [Opcode.MUL_8]: makeCost(c.AVM_MUL_DYN_L2_GAS, 0), - [Opcode.MUL_16]: makeCost(c.AVM_MUL_DYN_L2_GAS, 0), - [Opcode.DIV_8]: makeCost(c.AVM_DIV_DYN_L2_GAS, 0), - [Opcode.DIV_16]: makeCost(c.AVM_DIV_DYN_L2_GAS, 0), - [Opcode.FDIV_8]: makeCost(c.AVM_FDIV_DYN_L2_GAS, 0), - [Opcode.FDIV_16]: makeCost(c.AVM_FDIV_DYN_L2_GAS, 0), - [Opcode.EQ_8]: makeCost(c.AVM_EQ_DYN_L2_GAS, 0), - [Opcode.EQ_16]: makeCost(c.AVM_EQ_DYN_L2_GAS, 0), - [Opcode.LT_8]: makeCost(c.AVM_LT_DYN_L2_GAS, 0), - [Opcode.LT_16]: makeCost(c.AVM_LT_DYN_L2_GAS, 0), - [Opcode.LTE_8]: makeCost(c.AVM_LTE_DYN_L2_GAS, 0), - [Opcode.LTE_16]: makeCost(c.AVM_LTE_DYN_L2_GAS, 0), - [Opcode.AND_8]: makeCost(c.AVM_AND_DYN_L2_GAS, 0), - [Opcode.AND_16]: makeCost(c.AVM_AND_DYN_L2_GAS, 0), - [Opcode.OR_8]: makeCost(c.AVM_OR_DYN_L2_GAS, 0), - [Opcode.OR_16]: makeCost(c.AVM_OR_DYN_L2_GAS, 0), - [Opcode.XOR_8]: makeCost(c.AVM_XOR_DYN_L2_GAS, 0), - [Opcode.XOR_16]: makeCost(c.AVM_XOR_DYN_L2_GAS, 0), - [Opcode.NOT_8]: makeCost(c.AVM_NOT_DYN_L2_GAS, 0), - [Opcode.NOT_16]: makeCost(c.AVM_NOT_DYN_L2_GAS, 0), - [Opcode.SHL_8]: makeCost(c.AVM_SHL_DYN_L2_GAS, 0), - [Opcode.SHL_16]: makeCost(c.AVM_SHL_DYN_L2_GAS, 0), - [Opcode.SHR_8]: makeCost(c.AVM_SHR_DYN_L2_GAS, 0), - [Opcode.SHR_16]: makeCost(c.AVM_SHR_DYN_L2_GAS, 0), - [Opcode.CAST_8]: makeCost(c.AVM_CAST_DYN_L2_GAS, 0), - [Opcode.CAST_16]: makeCost(c.AVM_CAST_DYN_L2_GAS, 0), - [Opcode.ADDRESS]: makeCost(c.AVM_ADDRESS_DYN_L2_GAS, 0), - [Opcode.STORAGEADDRESS]: makeCost(c.AVM_STORAGEADDRESS_DYN_L2_GAS, 0), - [Opcode.SENDER]: makeCost(c.AVM_SENDER_DYN_L2_GAS, 0), - [Opcode.FEEPERL2GAS]: makeCost(c.AVM_FEEPERL2GAS_DYN_L2_GAS, 0), - [Opcode.FEEPERDAGAS]: makeCost(c.AVM_FEEPERDAGAS_DYN_L2_GAS, 0), - [Opcode.TRANSACTIONFEE]: makeCost(c.AVM_TRANSACTIONFEE_DYN_L2_GAS, 0), - [Opcode.FUNCTIONSELECTOR]: makeCost(c.AVM_FUNCTIONSELECTOR_DYN_L2_GAS, 0), - [Opcode.CHAINID]: makeCost(c.AVM_CHAINID_DYN_L2_GAS, 0), - [Opcode.VERSION]: makeCost(c.AVM_VERSION_DYN_L2_GAS, 0), - [Opcode.BLOCKNUMBER]: makeCost(c.AVM_BLOCKNUMBER_DYN_L2_GAS, 0), - [Opcode.TIMESTAMP]: makeCost(c.AVM_TIMESTAMP_DYN_L2_GAS, 0), - [Opcode.CALLDATACOPY]: makeCost(c.AVM_CALLDATACOPY_DYN_L2_GAS, 0), - [Opcode.L2GASLEFT]: makeCost(c.AVM_L2GASLEFT_DYN_L2_GAS, 0), - [Opcode.DAGASLEFT]: makeCost(c.AVM_DAGASLEFT_DYN_L2_GAS, 0), - [Opcode.JUMP_16]: makeCost(c.AVM_JUMP_DYN_L2_GAS, 0), - [Opcode.JUMPI_16]: makeCost(c.AVM_JUMPI_DYN_L2_GAS, 0), - [Opcode.INTERNALCALL]: makeCost(c.AVM_INTERNALCALL_DYN_L2_GAS, 0), - [Opcode.INTERNALRETURN]: makeCost(c.AVM_INTERNALRETURN_DYN_L2_GAS, 0), - [Opcode.SET_8]: makeCost(c.AVM_SET_DYN_L2_GAS, 0), - [Opcode.SET_16]: makeCost(c.AVM_SET_DYN_L2_GAS, 0), - [Opcode.SET_32]: makeCost(c.AVM_SET_DYN_L2_GAS, 0), - [Opcode.SET_64]: makeCost(c.AVM_SET_DYN_L2_GAS, 0), - [Opcode.SET_128]: makeCost(c.AVM_SET_DYN_L2_GAS, 0), - [Opcode.SET_FF]: makeCost(c.AVM_SET_DYN_L2_GAS, 0), - [Opcode.MOV_8]: makeCost(c.AVM_MOV_DYN_L2_GAS, 0), - [Opcode.MOV_16]: makeCost(c.AVM_MOV_DYN_L2_GAS, 0), - [Opcode.CMOV]: makeCost(c.AVM_CMOV_DYN_L2_GAS, 0), - [Opcode.SLOAD]: makeCost(c.AVM_SLOAD_DYN_L2_GAS, 0), - [Opcode.SSTORE]: makeCost(c.AVM_SSTORE_DYN_L2_GAS, 0), - [Opcode.NOTEHASHEXISTS]: makeCost(c.AVM_NOTEHASHEXISTS_DYN_L2_GAS, 0), - [Opcode.EMITNOTEHASH]: makeCost(c.AVM_EMITNOTEHASH_DYN_L2_GAS, 0), - [Opcode.NULLIFIEREXISTS]: makeCost(c.AVM_NULLIFIEREXISTS_DYN_L2_GAS, 0), - [Opcode.EMITNULLIFIER]: makeCost(c.AVM_EMITNULLIFIER_DYN_L2_GAS, 0), - [Opcode.L1TOL2MSGEXISTS]: makeCost(c.AVM_L1TOL2MSGEXISTS_DYN_L2_GAS, 0), - [Opcode.EMITUNENCRYPTEDLOG]: makeCost(c.AVM_EMITUNENCRYPTEDLOG_DYN_L2_GAS, 0), - [Opcode.SENDL2TOL1MSG]: makeCost(c.AVM_SENDL2TOL1MSG_DYN_L2_GAS, 0), - [Opcode.GETCONTRACTINSTANCE]: makeCost(c.AVM_GETCONTRACTINSTANCE_DYN_L2_GAS, 0), - [Opcode.CALL]: makeCost(c.AVM_CALL_DYN_L2_GAS, 0), - [Opcode.STATICCALL]: makeCost(c.AVM_STATICCALL_DYN_L2_GAS, 0), - [Opcode.DELEGATECALL]: makeCost(c.AVM_DELEGATECALL_DYN_L2_GAS, 0), - [Opcode.RETURN]: makeCost(c.AVM_RETURN_DYN_L2_GAS, 0), - [Opcode.REVERT_8]: makeCost(c.AVM_REVERT_DYN_L2_GAS, 0), - [Opcode.REVERT_16]: makeCost(c.AVM_REVERT_DYN_L2_GAS, 0), - [Opcode.DEBUGLOG]: makeCost(c.AVM_DEBUGLOG_DYN_L2_GAS, 0), - [Opcode.KECCAK]: makeCost(c.AVM_KECCAK_DYN_L2_GAS, 0), - [Opcode.POSEIDON2]: makeCost(c.AVM_POSEIDON2_DYN_L2_GAS, 0), - [Opcode.SHA256]: makeCost(c.AVM_SHA256_DYN_L2_GAS, 0), - [Opcode.PEDERSEN]: makeCost(c.AVM_PEDERSEN_DYN_L2_GAS, 0), - [Opcode.ECADD]: makeCost(c.AVM_ECADD_DYN_L2_GAS, 0), - [Opcode.MSM]: makeCost(c.AVM_MSM_DYN_L2_GAS, 0), - [Opcode.PEDERSENCOMMITMENT]: makeCost(c.AVM_PEDERSENCOMMITMENT_DYN_L2_GAS, 0), - [Opcode.TORADIXLE]: makeCost(c.AVM_TORADIXLE_DYN_L2_GAS, 0), - [Opcode.SHA256COMPRESSION]: makeCost(c.AVM_SHA256COMPRESSION_DYN_L2_GAS, 0), - [Opcode.KECCAKF1600]: makeCost(c.AVM_KECCAKF1600_DYN_L2_GAS, 0), -}; +const DYNAMIC_GAS_COSTS = new Map([ + [Opcode.CALLDATACOPY, makeCost(c.AVM_CALLDATACOPY_DYN_L2_GAS, 0)], + [Opcode.EMITUNENCRYPTEDLOG, makeCost(c.AVM_EMITUNENCRYPTEDLOG_DYN_L2_GAS, c.AVM_EMITUNENCRYPTEDLOG_DYN_DA_GAS)], + [Opcode.CALL, makeCost(c.AVM_CALL_DYN_L2_GAS, 0)], + [Opcode.STATICCALL, makeCost(c.AVM_STATICCALL_DYN_L2_GAS, 0)], + [Opcode.DELEGATECALL, makeCost(c.AVM_DELEGATECALL_DYN_L2_GAS, 0)], + [Opcode.RETURN, makeCost(c.AVM_RETURN_DYN_L2_GAS, 0)], + [Opcode.REVERT_8, makeCost(c.AVM_REVERT_DYN_L2_GAS, 0)], + [Opcode.REVERT_16, makeCost(c.AVM_REVERT_DYN_L2_GAS, 0)], + [Opcode.KECCAK, makeCost(c.AVM_KECCAK_DYN_L2_GAS, 0)], + [Opcode.SHA256, makeCost(c.AVM_SHA256_DYN_L2_GAS, 0)], + [Opcode.PEDERSEN, makeCost(c.AVM_PEDERSEN_DYN_L2_GAS, 0)], + [Opcode.MSM, makeCost(c.AVM_MSM_DYN_L2_GAS, 0)], + [Opcode.PEDERSENCOMMITMENT, makeCost(c.AVM_PEDERSENCOMMITMENT_DYN_L2_GAS, 0)], + [Opcode.TORADIXLE, makeCost(c.AVM_TORADIXLE_DYN_L2_GAS, 0)], +]); /** Returns the fixed base gas cost for a given opcode. */ export function getBaseGasCost(opcode: Opcode): Gas { - return BaseGasCosts[opcode]; + return BASE_GAS_COSTS[opcode]; } export function getDynamicGasCost(opcode: Opcode): Gas { - return DynamicGasCosts[opcode]; + return DYNAMIC_GAS_COSTS.has(opcode) ? DYNAMIC_GAS_COSTS.get(opcode)! : makeCost(0, 0); } /** Returns the gas cost associated with the memory operations performed. */ @@ -244,14 +174,14 @@ export function getMemoryGasCost(args: { reads?: number; writes?: number; indire const { reads, writes, indirect } = args; const indirectCount = Addressing.fromWire(indirect ?? 0).count(AddressingMode.INDIRECT); const l2MemoryGasCost = - (reads ?? 0) * GasCostConstants.MEMORY_READ + - (writes ?? 0) * GasCostConstants.MEMORY_WRITE + - indirectCount * GasCostConstants.MEMORY_INDIRECT_READ_PENALTY; + (reads ?? 0) * GAS_COST_CONSTANTS.MEMORY_READ + + (writes ?? 0) * GAS_COST_CONSTANTS.MEMORY_WRITE + + indirectCount * GAS_COST_CONSTANTS.MEMORY_INDIRECT_READ_PENALTY; return makeGas({ l2Gas: l2MemoryGasCost }); } /** Constants used in base cost calculations. */ -export const GasCostConstants = { +export const GAS_COST_CONSTANTS = { MEMORY_READ: 10, MEMORY_INDIRECT_READ_PENALTY: 10, MEMORY_WRITE: 100, diff --git a/yarn-project/simulator/src/avm/avm_machine_state.ts b/yarn-project/simulator/src/avm/avm_machine_state.ts index 740f607bafb4..abb13bc085ed 100644 --- a/yarn-project/simulator/src/avm/avm_machine_state.ts +++ b/yarn-project/simulator/src/avm/avm_machine_state.ts @@ -1,6 +1,6 @@ import { type Fr } from '@aztec/circuits.js'; -import { type Gas, GasDimensions } from './avm_gas.js'; +import { type Gas, GAS_DIMENSIONS } from './avm_gas.js'; import { TaggedMemory } from './avm_memory_types.js'; import { OutOfGasError } from './errors.js'; @@ -67,7 +67,7 @@ export class AvmMachineState { */ public consumeGas(gasCost: Partial) { // Assert there is enough gas on every dimension. - const outOfGasDimensions = GasDimensions.filter( + const outOfGasDimensions = GAS_DIMENSIONS.filter( dimension => this[`${dimension}Left`] - (gasCost[dimension] ?? 0) < 0, ); // If not, trigger an exceptional halt. @@ -77,14 +77,14 @@ export class AvmMachineState { throw new OutOfGasError(outOfGasDimensions); } // Otherwise, charge the corresponding gas - for (const dimension of GasDimensions) { + for (const dimension of GAS_DIMENSIONS) { this[`${dimension}Left`] -= gasCost[dimension] ?? 0; } } /** Increases the gas left by the amounts specified. */ public refundGas(gasRefund: Partial) { - for (const dimension of GasDimensions) { + for (const dimension of GAS_DIMENSIONS) { this[`${dimension}Left`] += gasRefund[dimension] ?? 0; } } @@ -133,7 +133,7 @@ export class AvmMachineState { * Flag an exceptional halt. Clears gas left and sets the reverted flag. No output data. */ private exceptionalHalt() { - GasDimensions.forEach(dimension => (this[`${dimension}Left`] = 0)); + GAS_DIMENSIONS.forEach(dimension => (this[`${dimension}Left`] = 0)); this.reverted = true; this.halted = true; } diff --git a/yarn-project/simulator/src/avm/opcodes/commitment.ts b/yarn-project/simulator/src/avm/opcodes/commitment.ts index 6c065781ded8..e130c86a1f3f 100644 --- a/yarn-project/simulator/src/avm/opcodes/commitment.ts +++ b/yarn-project/simulator/src/avm/opcodes/commitment.ts @@ -46,8 +46,8 @@ export class PedersenCommitment extends Instruction { const generatorIndex = memory.get(genIndexOffset).toNumber(); memory.checkTag(TypeTag.UINT32, genIndexOffset); - const memoryOperations = { reads: inputSize + 2, writes: 3, indirect: this.indirect }; - context.machineState.consumeGas(this.gasCost(memoryOperations)); + const memoryOperations = { reads: 2, writes: 3, indirect: this.indirect }; + context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: inputSize })); const inputBuffer: Buffer[] = inputs.map(input => input.toBuffer()); // TODO: Add the generate index to the pedersenCommit function diff --git a/yarn-project/simulator/src/avm/opcodes/hashing.ts b/yarn-project/simulator/src/avm/opcodes/hashing.ts index 6a70b7e6a6b2..fd54ec686f56 100644 --- a/yarn-project/simulator/src/avm/opcodes/hashing.ts +++ b/yarn-project/simulator/src/avm/opcodes/hashing.ts @@ -79,8 +79,8 @@ export class Keccak extends Instruction { ); memory.checkTag(TypeTag.UINT32, messageSizeOffset); const messageSize = memory.get(messageSizeOffset).toNumber(); - const memoryOperations = { reads: messageSize + 1, writes: 32, indirect: this.indirect }; - context.machineState.consumeGas(this.gasCost(memoryOperations)); + const memoryOperations = { reads: 1, writes: 32, indirect: this.indirect }; + context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: messageSize })); memory.checkTagsRange(TypeTag.UINT8, messageOffset, messageSize); @@ -176,8 +176,9 @@ export class Sha256 extends Instruction { ); memory.checkTag(TypeTag.UINT32, messageSizeOffset); const messageSize = memory.get(messageSizeOffset).toNumber(); - const memoryOperations = { reads: messageSize + 1, writes: 32, indirect: this.indirect }; - context.machineState.consumeGas(this.gasCost(memoryOperations)); + const memoryOperations = { reads: 1, writes: 32, indirect: this.indirect }; + context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: messageSize })); + memory.checkTagsRange(TypeTag.UINT8, messageOffset, messageSize); const messageData = Buffer.concat(memory.getSlice(messageOffset, messageSize).map(word => word.toBuffer())); @@ -230,8 +231,8 @@ export class Pedersen extends Instruction { memory.checkTag(TypeTag.UINT32, messageSizeOffset); const hashData = memory.getSlice(messageOffset, messageSize); - const memoryOperations = { reads: messageSize + 2, writes: 1, indirect: this.indirect }; - context.machineState.consumeGas(this.gasCost(memoryOperations)); + const memoryOperations = { reads: 2, writes: 1, indirect: this.indirect }; + context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: messageSize })); memory.checkTagsRange(TypeTag.FIELD, messageOffset, messageSize); diff --git a/yarn-project/simulator/src/avm/opcodes/multi_scalar_mul.ts b/yarn-project/simulator/src/avm/opcodes/multi_scalar_mul.ts index 3ac9223c1859..2de458e067b5 100644 --- a/yarn-project/simulator/src/avm/opcodes/multi_scalar_mul.ts +++ b/yarn-project/simulator/src/avm/opcodes/multi_scalar_mul.ts @@ -46,7 +46,9 @@ export class MultiScalarMul extends Instruction { memory.checkTag(TypeTag.UINT32, this.pointsLengthOffset); // Get the size of the unrolled (x, y , inf) points vector const pointsReadLength = memory.get(this.pointsLengthOffset).toNumber(); - assert(pointsReadLength % 3 === 0, 'Points vector offset should be a multiple of 3'); + if (pointsReadLength % 3 !== 0) { + throw new InstructionExecutionError(`Points vector offset should be a multiple of 3, was ${pointsReadLength}`); + } // Divide by 3 since each point is represented as a triplet to get the number of points const numPoints = pointsReadLength / 3; // The tag for each triplet will be (Field, Field, Uint8) @@ -64,11 +66,11 @@ export class MultiScalarMul extends Instruction { const scalarReadLength = numPoints * 2; // Consume gas prior to performing work const memoryOperations = { - reads: 1 + pointsReadLength + scalarReadLength /* points and scalars */, + reads: 1, writes: 3 /* output triplet */, indirect: this.indirect, }; - context.machineState.consumeGas(this.gasCost(memoryOperations)); + context.machineState.consumeGas(this.gasCost({ ...memoryOperations, dynMultiplier: pointsReadLength })); // Get the unrolled scalar (lo & hi) representing the scalars const scalarsVector = memory.getSlice(scalarsOffset, scalarReadLength); memory.checkTagsRange(TypeTag.FIELD, scalarsOffset, scalarReadLength);