Skip to content

Commit

Permalink
Charge for zkEVM Contract Decommits
Browse files Browse the repository at this point in the history
  • Loading branch information
jrchatruc committed May 20, 2024
1 parent 19b81f7 commit 3eb9691
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
24 changes: 12 additions & 12 deletions system-contracts/contracts/EvmInterpreterFunctions.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -752,11 +752,9 @@ function GAS_DIVISOR() -> gas_div { gas_div := 5 }
function EVM_GAS_STIPEND() -> gas_stipend { gas_stipend := shl(30, 1) } // 1 << 30
function OVERHEAD() -> overhead { overhead := 2000 }

function GAS_CONSTANTS() -> divisor, stipend, overhead {
divisor := GAS_DIVISOR()
stipend := EVM_GAS_STIPEND()
overhead := OVERHEAD()
}
// From precompiles/CodeOracle
function DECOMMIT_COST_PER_WORD() -> cost { cost := 4 }
function UINT32_MAX() -> ret { ret := 4294967295 } // 2^32 - 1

function _calcEVMGas(_zkevmGas) -> calczkevmGas {
calczkevmGas := div(_zkevmGas, GAS_DIVISOR())
Expand All @@ -774,11 +772,13 @@ function getEVMGas() -> evmGas {
evmGas := div(sub(_gas, requiredGas), GAS_DIVISOR())
}

function _getZkEVMGas(_evmGas) -> zkevmGas {
/*
TODO: refine the formula, especially with regard to decommitment costs
*/
function _getZkEVMGas(_evmGas, addr) -> zkevmGas {
zkevmGas := mul(_evmGas, GAS_DIVISOR())
let byteSize := extcodesize(addr)
zkevmGas := add(zkevmGas, mul(byteSize, DECOMMIT_COST_PER_WORD()))
if gt(zkevmGas, UINT32_MAX()) {
zkevmGas := UINT32_MAX()
}
}

function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft{
Expand Down Expand Up @@ -861,7 +861,7 @@ function performStaticCall(oldSp,evmGasLeft) -> extraCost, sp {

// zkEVM native
if iszero(_isEVM(addr)) {
gasToPass := _getZkEVMGas(gasToPass)
gasToPass := _getZkEVMGas(gasToPass, addr)
let zkevmGasBefore := gas()
success := staticcall(gasToPass, addr, add(MEM_OFFSET_INNER(), argsOffset), argsSize, add(MEM_OFFSET_INNER(), retOffset), retSize)
_saveReturndataAfterZkEVMCall()
Expand Down Expand Up @@ -967,7 +967,7 @@ function performCall(oldSp, evmGasLeft, isStatic) -> extraCost, sp {

// zkEVM native
if and(iszero(_isEVM(addr)), iszero(isStatic)) {
gasToPass := _getZkEVMGas(gasToPass)
gasToPass := _getZkEVMGas(gasToPass, addr)
let zkevmGasBefore := gas()
success := call(gasToPass, addr, value, argsOffset, argsSize, retOffset, retSize)
_saveReturndataAfterZkEVMCall()
Expand Down Expand Up @@ -1095,7 +1095,7 @@ function _performStaticCall(

// zkEVM native
if iszero(_calleeIsEVM) {
_calleeGas := _getZkEVMGas(_calleeGas)
_calleeGas := _getZkEVMGas(_calleeGas, _callee)
let zkevmGasBefore := gas()
success := staticcall(_calleeGas, _callee, _inputOffset, _inputLen, _outputOffset, _outputLen)

Expand Down
48 changes: 24 additions & 24 deletions system-contracts/contracts/EvmInterpreterPreprocessed.yul
Original file line number Diff line number Diff line change
Expand Up @@ -826,11 +826,9 @@ object "EVMInterpreter" {
function EVM_GAS_STIPEND() -> gas_stipend { gas_stipend := shl(30, 1) } // 1 << 30
function OVERHEAD() -> overhead { overhead := 2000 }

function GAS_CONSTANTS() -> divisor, stipend, overhead {
divisor := GAS_DIVISOR()
stipend := EVM_GAS_STIPEND()
overhead := OVERHEAD()
}
// From precompiles/CodeOracle
function DECOMMIT_COST_PER_WORD() -> cost { cost := 4 }
function UINT32_MAX() -> ret { ret := 4294967295 } // 2^32 - 1

function _calcEVMGas(_zkevmGas) -> calczkevmGas {
calczkevmGas := div(_zkevmGas, GAS_DIVISOR())
Expand All @@ -848,11 +846,13 @@ object "EVMInterpreter" {
evmGas := div(sub(_gas, requiredGas), GAS_DIVISOR())
}

function _getZkEVMGas(_evmGas) -> zkevmGas {
/*
TODO: refine the formula, especially with regard to decommitment costs
*/
function _getZkEVMGas(_evmGas, addr) -> zkevmGas {
zkevmGas := mul(_evmGas, GAS_DIVISOR())
let byteSize := extcodesize(addr)
zkevmGas := add(zkevmGas, mul(byteSize, DECOMMIT_COST_PER_WORD()))
if gt(zkevmGas, UINT32_MAX()) {
zkevmGas := UINT32_MAX()
}
}

function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft{
Expand Down Expand Up @@ -935,7 +935,7 @@ object "EVMInterpreter" {

// zkEVM native
if iszero(_isEVM(addr)) {
gasToPass := _getZkEVMGas(gasToPass)
gasToPass := _getZkEVMGas(gasToPass, addr)
let zkevmGasBefore := gas()
success := staticcall(gasToPass, addr, add(MEM_OFFSET_INNER(), argsOffset), argsSize, add(MEM_OFFSET_INNER(), retOffset), retSize)
_saveReturndataAfterZkEVMCall()
Expand Down Expand Up @@ -1041,7 +1041,7 @@ object "EVMInterpreter" {

// zkEVM native
if and(iszero(_isEVM(addr)), iszero(isStatic)) {
gasToPass := _getZkEVMGas(gasToPass)
gasToPass := _getZkEVMGas(gasToPass, addr)
let zkevmGasBefore := gas()
success := call(gasToPass, addr, value, argsOffset, argsSize, retOffset, retSize)
_saveReturndataAfterZkEVMCall()
Expand Down Expand Up @@ -1169,7 +1169,7 @@ object "EVMInterpreter" {

// zkEVM native
if iszero(_calleeIsEVM) {
_calleeGas := _getZkEVMGas(_calleeGas)
_calleeGas := _getZkEVMGas(_calleeGas, _callee)
let zkevmGasBefore := gas()
success := staticcall(_calleeGas, _callee, _inputOffset, _inputLen, _outputOffset, _outputLen)

Expand Down Expand Up @@ -3402,11 +3402,9 @@ object "EVMInterpreter" {
function EVM_GAS_STIPEND() -> gas_stipend { gas_stipend := shl(30, 1) } // 1 << 30
function OVERHEAD() -> overhead { overhead := 2000 }

function GAS_CONSTANTS() -> divisor, stipend, overhead {
divisor := GAS_DIVISOR()
stipend := EVM_GAS_STIPEND()
overhead := OVERHEAD()
}
// From precompiles/CodeOracle
function DECOMMIT_COST_PER_WORD() -> cost { cost := 4 }
function UINT32_MAX() -> ret { ret := 4294967295 } // 2^32 - 1

function _calcEVMGas(_zkevmGas) -> calczkevmGas {
calczkevmGas := div(_zkevmGas, GAS_DIVISOR())
Expand All @@ -3424,11 +3422,13 @@ object "EVMInterpreter" {
evmGas := div(sub(_gas, requiredGas), GAS_DIVISOR())
}

function _getZkEVMGas(_evmGas) -> zkevmGas {
/*
TODO: refine the formula, especially with regard to decommitment costs
*/
function _getZkEVMGas(_evmGas, addr) -> zkevmGas {
zkevmGas := mul(_evmGas, GAS_DIVISOR())
let byteSize := extcodesize(addr)
zkevmGas := add(zkevmGas, mul(byteSize, DECOMMIT_COST_PER_WORD()))
if gt(zkevmGas, UINT32_MAX()) {
zkevmGas := UINT32_MAX()
}
}

function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft{
Expand Down Expand Up @@ -3511,7 +3511,7 @@ object "EVMInterpreter" {

// zkEVM native
if iszero(_isEVM(addr)) {
gasToPass := _getZkEVMGas(gasToPass)
gasToPass := _getZkEVMGas(gasToPass, addr)
let zkevmGasBefore := gas()
success := staticcall(gasToPass, addr, add(MEM_OFFSET_INNER(), argsOffset), argsSize, add(MEM_OFFSET_INNER(), retOffset), retSize)
_saveReturndataAfterZkEVMCall()
Expand Down Expand Up @@ -3617,7 +3617,7 @@ object "EVMInterpreter" {

// zkEVM native
if and(iszero(_isEVM(addr)), iszero(isStatic)) {
gasToPass := _getZkEVMGas(gasToPass)
gasToPass := _getZkEVMGas(gasToPass, addr)
let zkevmGasBefore := gas()
success := call(gasToPass, addr, value, argsOffset, argsSize, retOffset, retSize)
_saveReturndataAfterZkEVMCall()
Expand Down Expand Up @@ -3745,7 +3745,7 @@ object "EVMInterpreter" {

// zkEVM native
if iszero(_calleeIsEVM) {
_calleeGas := _getZkEVMGas(_calleeGas)
_calleeGas := _getZkEVMGas(_calleeGas, _callee)
let zkevmGasBefore := gas()
success := staticcall(_calleeGas, _callee, _inputOffset, _inputLen, _outputOffset, _outputLen)

Expand Down

0 comments on commit 3eb9691

Please sign in to comment.