From 1a88e645c6c8709c4dfdb1489da0ea6026ec1364 Mon Sep 17 00:00:00 2001 From: Javier Chatruc Date: Wed, 3 Jul 2024 16:06:05 -0300 Subject: [PATCH] Properly charge cost per word and substract it instead of adding --- .../contracts/EvmInterpreterFunctions.template.yul | 7 ++++++- .../contracts/EvmInterpreterPreprocessed.yul | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/system-contracts/contracts/EvmInterpreterFunctions.template.yul b/system-contracts/contracts/EvmInterpreterFunctions.template.yul index d054a8180..239d1171f 100644 --- a/system-contracts/contracts/EvmInterpreterFunctions.template.yul +++ b/system-contracts/contracts/EvmInterpreterFunctions.template.yul @@ -777,7 +777,12 @@ function getEVMGas() -> evmGas { function _getZkEVMGas(_evmGas, addr) -> zkevmGas { zkevmGas := mul(_evmGas, GAS_DIVISOR()) let byteSize := extcodesize(addr) - zkevmGas := add(zkevmGas, mul(byteSize, DECOMMIT_COST_PER_WORD())) + let should_ceil := mod(byteSize, 32) + if gt(should_ceil, 0) { + byteSize := add(byteSize, sub(32, should_ceil)) + } + let decommitGasCost := mul(div(byteSize,32), DECOMMIT_COST_PER_WORD()) + zkevmGas := sub(zkevmGas, decommitGasCost) if gt(zkevmGas, UINT32_MAX()) { zkevmGas := UINT32_MAX() } diff --git a/system-contracts/contracts/EvmInterpreterPreprocessed.yul b/system-contracts/contracts/EvmInterpreterPreprocessed.yul index 4f997b4bf..dddc81c28 100644 --- a/system-contracts/contracts/EvmInterpreterPreprocessed.yul +++ b/system-contracts/contracts/EvmInterpreterPreprocessed.yul @@ -851,7 +851,12 @@ object "EVMInterpreter" { function _getZkEVMGas(_evmGas, addr) -> zkevmGas { zkevmGas := mul(_evmGas, GAS_DIVISOR()) let byteSize := extcodesize(addr) - zkevmGas := add(zkevmGas, mul(byteSize, DECOMMIT_COST_PER_WORD())) + let should_ceil := mod(byteSize, 32) + if gt(should_ceil, 0) { + byteSize := add(byteSize, sub(32, should_ceil)) + } + let decommitGasCost := mul(div(byteSize,32), DECOMMIT_COST_PER_WORD()) + zkevmGas := sub(zkevmGas, decommitGasCost) if gt(zkevmGas, UINT32_MAX()) { zkevmGas := UINT32_MAX() } @@ -3474,7 +3479,12 @@ object "EVMInterpreter" { function _getZkEVMGas(_evmGas, addr) -> zkevmGas { zkevmGas := mul(_evmGas, GAS_DIVISOR()) let byteSize := extcodesize(addr) - zkevmGas := add(zkevmGas, mul(byteSize, DECOMMIT_COST_PER_WORD())) + let should_ceil := mod(byteSize, 32) + if gt(should_ceil, 0) { + byteSize := add(byteSize, sub(32, should_ceil)) + } + let decommitGasCost := mul(div(byteSize,32), DECOMMIT_COST_PER_WORD()) + zkevmGas := sub(zkevmGas, decommitGasCost) if gt(zkevmGas, UINT32_MAX()) { zkevmGas := UINT32_MAX() }