diff --git a/src/zkevm_specs/evm_circuit/execution/begin_tx.py b/src/zkevm_specs/evm_circuit/execution/begin_tx.py index 2429b7d14..4fb364bd6 100644 --- a/src/zkevm_specs/evm_circuit/execution/begin_tx.py +++ b/src/zkevm_specs/evm_circuit/execution/begin_tx.py @@ -1,7 +1,9 @@ +from zkevm_specs.util.param import GAS_COST_INITCODE_WORD from ...util import ( GAS_COST_TX, GAS_COST_CREATION_TX, EMPTY_CODE_HASH, + N_BYTES_U64, FQ, Word, WordOrValue, @@ -67,7 +69,13 @@ def begin_tx(instruction: Instruction): # G_transaction + # sum([G_accesslistaddress + G_accessliststorage * len(TA[j]) for j in len(TA)]) tx_calldata_gas_cost = instruction.tx_context_lookup(tx_id, TxContextFieldTag.CallDataGasCost) - tx_cost_gas = GAS_COST_CREATION_TX if tx_is_create == 1 else GAS_COST_TX + tx_cost_gas = GAS_COST_TX + if tx_is_create.expr() == FQ(1): + len_words, _ = instruction.constant_divmod( + tx_call_data_length + FQ(31), FQ(32), N_BYTES_U64 + ) + tx_cost_gas = GAS_COST_CREATION_TX + len_words * GAS_COST_INITCODE_WORD + # TODO: Handle gas cost of tx level access list (EIP 2930) tx_accesslist_gas = instruction.tx_context_lookup(tx_id, TxContextFieldTag.AccessListGasCost) tx_intrinsic_gas = tx_calldata_gas_cost.expr() + tx_cost_gas + tx_accesslist_gas.expr() diff --git a/src/zkevm_specs/evm_circuit/execution/create.py b/src/zkevm_specs/evm_circuit/execution/create.py index c1570765c..89cd2fc57 100644 --- a/src/zkevm_specs/evm_circuit/execution/create.py +++ b/src/zkevm_specs/evm_circuit/execution/create.py @@ -65,10 +65,9 @@ def create(instruction: Instruction): # CREATE2 = gas cost of CREATE + GAS_COST_COPY_SHA3 * word_len # byte_code is only available in `return_revert`, # so the last part (GAS_COST_CODE_DEPOSIT * len(byte_code)) won't be calculated here - gas_left = instruction.curr.gas_left - gas_cost = GAS_COST_CREATE + memory_expansion_gas_cost word_len, _ = instruction.constant_divmod(size + FQ(31), FQ(32), N_BYTES_MEMORY_WORD_SIZE) - gas_cost += word_len * GAS_COST_INITCODE_WORD + gas_left = instruction.curr.gas_left + gas_cost = GAS_COST_CREATE + memory_expansion_gas_cost + word_len * GAS_COST_INITCODE_WORD if is_create2 == FQ(1): gas_cost += GAS_COST_COPY_SHA3 * word_len gas_available = gas_left - gas_cost diff --git a/tests/evm/test_begin_tx.py b/tests/evm/test_begin_tx.py index 6352988c7..b88ec1cbb 100644 --- a/tests/evm/test_begin_tx.py +++ b/tests/evm/test_begin_tx.py @@ -186,7 +186,7 @@ def gen_bytecode(is_return: bool, offset: int, has_init_code: bool) -> Bytecode: Transaction( caller_address=0xFE, callee_address=None, - gas=100000, # TODO(amb) make more precise gas + gas=53000, ), CALLEE_WITH_NOTHING, True, @@ -196,7 +196,7 @@ def gen_bytecode(is_return: bool, offset: int, has_init_code: bool) -> Bytecode: Transaction( caller_address=0xFE, callee_address=None, - gas=100000, # TODO(amb) make more precise gas + gas=53000, value=1, ), CALLEE_WITH_NOTHING, @@ -207,7 +207,7 @@ def gen_bytecode(is_return: bool, offset: int, has_init_code: bool) -> Bytecode: Transaction( caller_address=0xFE, callee_address=None, - gas=53580, # TODO(amb) make more precise gas + gas=53584, call_data=bytes(gen_bytecode(True, 0, True).code), ), CALLEE_WITH_NOTHING, @@ -218,7 +218,7 @@ def gen_bytecode(is_return: bool, offset: int, has_init_code: bool) -> Bytecode: Transaction( caller_address=0xFE, callee_address=None, - gas=53580, + gas=53584, value=1, call_data=bytes(gen_bytecode(True, 0, True).code), ), @@ -230,7 +230,7 @@ def gen_bytecode(is_return: bool, offset: int, has_init_code: bool) -> Bytecode: Transaction( caller_address=0xFE, callee_address=None, - gas=53580, + gas=53584, value=1, call_data=bytes(gen_bytecode(False, 0, True).code), ),