Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EVM-Equivalence-Yul] Charge for zkEVM contracts decommits #428

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 25 additions & 39 deletions system-contracts/contracts/EvmInterpreterFunctions.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -592,12 +592,9 @@ function _popEVMFrame() {
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 @@ -615,11 +612,12 @@ function getEVMGas() -> evmGas {
evmGas := div(sub(_gas, requiredGas), GAS_DIVISOR())
}

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

function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft{
Expand Down Expand Up @@ -671,14 +669,9 @@ function performStaticCall(oldSp,evmGasLeft) -> extraCost, sp {
retOffset, sp := popStackItem(sp)
retSize, sp := popStackItem(sp)

checkMemOverflow(add(add(argsOffset, argsSize), MEM_OFFSET_INNER()))
checkMemOverflow(add(add(retOffset, retSize), MEM_OFFSET_INNER()))

extraCost := 0
if iszero(warmAddress(addr)) {
extraCost := 2500
}

switch warmAddress(addr)
case 0 { extraCost := 2600 }
default { extraCost := 100 }
{
let maxExpand := add(retOffset, retSize)
switch lt(maxExpand,add(argsOffset, argsSize)) // Check if this makes sense
Expand Down Expand Up @@ -709,7 +702,7 @@ function performStaticCall(oldSp,evmGasLeft) -> extraCost, sp {

// zkEVM native
if iszero(_isEVM(addr)) {
gasToPass := _getZkEVMGas(gasToPass)
gasToPass := _getZkEVMGas(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 @@ -743,23 +736,22 @@ function performCall(oldSp, evmGasLeft, isStatic) -> extraCost, sp {
retOffset, sp := popStackItem(sp)
retSize, sp := popStackItem(sp)


// static_gas = 0
// dynamic_gas = memory_expansion_cost + code_execution_cost + address_access_cost + positive_value_cost + value_to_empty_account_cost
// code_execution_cost is the cost of the called code execution (limited by the gas parameter).
// If address is warm, then address_access_cost is 100, otherwise it is 2600. See section access sets.
// If value is not 0, then positive_value_cost is 9000. In this case there is also a call stipend that is given to make sure that a basic fallback function can be called. 2300 is thus removed from the cost, and also added to the gas input.
// If value is not 0 and the address given points to an empty account, then value_to_empty_account_cost is 25000. An account is empty if its balance is 0, its nonce is 0 and it has no code.


extraCost := 0
if iszero(warmAddress(addr)) {
extraCost := 2500
}

switch warmAddress(addr)
case 0 { extraCost := 2600 }
default { extraCost := 100 }
if gt(value, 0) {
extraCost := add(extraCost,6700)
gasToPass := add(gasToPass,2300)
}

if and(isAddrEmpty(addr), gt(value, 0)) {
extraCost := add(extraCost,25000)
}
Expand All @@ -778,9 +770,8 @@ function performCall(oldSp, evmGasLeft, isStatic) -> extraCost, sp {

argsOffset := add(argsOffset,MEM_OFFSET_INNER())
retOffset := add(retOffset,MEM_OFFSET_INNER())

checkMemOverflow(add(argsOffset, argsSize))
checkMemOverflow(add(retOffset, retSize))
checkMemOverflow(argsOffset)
checkMemOverflow(retOffset)

let frameGasLeft
let success
Expand Down Expand Up @@ -809,7 +800,7 @@ function performCall(oldSp, evmGasLeft, isStatic) -> extraCost, sp {

// zkEVM native
if and(iszero(_isEVM(addr)), iszero(isStatic)) {
gasToPass := _getZkEVMGas(gasToPass)
gasToPass := _getZkEVMGas(addr)
let zkevmGasBefore := gas()
success := call(gasToPass, addr, value, argsOffset, argsSize, retOffset, retSize)
_saveReturndataAfterZkEVMCall()
Expand Down Expand Up @@ -838,18 +829,13 @@ function delegateCall(oldSp, oldIsStatic, evmGasLeft) -> sp, isStatic, extraCost
retOffset, sp := popStackItem(sp)
retSize, sp := popStackItem(sp)

checkMemOverflow(add(add(argsOffset, argsSize), MEM_OFFSET_INNER()))
checkMemOverflow(add(add(retOffset, retSize), MEM_OFFSET_INNER()))

if iszero(_isEVM(addr)) {
revert(0, 0)
}

extraCost := 0
if iszero(warmAddress(addr)) {
extraCost := 2500
}

switch warmAddress(addr)
case 0 { extraCost := 2600 }
default { extraCost := 100 }
{
let maxExpand := add(retOffset, retSize)
switch lt(maxExpand,add(argsOffset, argsSize))
Expand Down Expand Up @@ -942,7 +928,7 @@ function _performStaticCall(

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

Expand Down
Loading