forked from privacy-scaling-explorations/zkevm-circuits
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement circuit for
ErrorOutOfGasMemoryCopy
state (privacy-scalin…
…g-explorations#1288) ### Description Spec Markdown PR privacy-scaling-explorations/zkevm-specs#397 #### Summary 1. Merge OOG error `ExtCodeCopy` into `MemoryCopy`. 2. Implement bus-mapping and circuit for this OOG error of `CALLDATACOPY`, `CODECOPY`, `EXTCODECOPY` and `RETURNDATACOPY` opcodes. 3. OOG error executions of `CALLDATACOPY`, `CODECOPY` and `RETURNDATACOPY` are same. 4. `EXTCODECOPY` has extra `1` stack pop, and constant gas costs are different for cold (2600) and warm (100) accounts according to EIP-2929 (could reference [go-etherum gasExtCodeCopyEIP2929 function](https://github.com/ethereum/go-ethereum/blob/master/core/vm/operations_acl.go#L116)). 5. Fix `memory_copier_gas_cost` function to not calculate memory expansion for zero copy size in gas utils of `eth-types`. ### Issue Link Close privacy-scaling-explorations#1266 ### Type of change - [X] New feature (non-breaking change which adds functionality) ### How Has This Been Tested? Add new unit-test cases for this error state.
- Loading branch information
1 parent
3429f83
commit e55ba06
Showing
7 changed files
with
499 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::{ | ||
circuit_input_builder::{CircuitInputStateRef, ExecStep}, | ||
error::{ExecError, OogError}, | ||
evm::Opcode, | ||
operation::{CallContextField, TxAccessListAccountOp, RW}, | ||
Error, | ||
}; | ||
use eth_types::{evm_types::OpcodeId, GethExecStep, ToAddress}; | ||
|
||
/// Placeholder structure used to implement [`Opcode`] trait over it | ||
/// corresponding to the | ||
/// [`OogError::MemoryCopy`](crate::error::OogError::MemoryCopy). | ||
#[derive(Clone, Copy, Debug)] | ||
pub(crate) struct OOGMemoryCopy; | ||
|
||
impl Opcode for OOGMemoryCopy { | ||
fn gen_associated_ops( | ||
state: &mut CircuitInputStateRef, | ||
geth_steps: &[GethExecStep], | ||
) -> Result<Vec<ExecStep>, Error> { | ||
let geth_step = &geth_steps[0]; | ||
debug_assert!([ | ||
OpcodeId::CALLDATACOPY, | ||
OpcodeId::CODECOPY, | ||
OpcodeId::EXTCODECOPY, | ||
OpcodeId::RETURNDATACOPY | ||
] | ||
.contains(&geth_step.op)); | ||
|
||
let mut exec_step = state.new_step(geth_step)?; | ||
exec_step.error = Some(ExecError::OutOfGas(OogError::MemoryCopy)); | ||
|
||
let is_extcodecopy = geth_step.op == OpcodeId::EXTCODECOPY; | ||
|
||
// According to EIP-2929, EXTCODECOPY constant gas cost is different for cold | ||
// and warm accounts. | ||
if is_extcodecopy { | ||
state.call_context_read( | ||
&mut exec_step, | ||
state.call()?.call_id, | ||
CallContextField::TxId, | ||
state.tx_ctx.id().into(), | ||
); | ||
|
||
let external_address = geth_step.stack.last()?.to_address(); | ||
let is_warm = state.sdb.check_account_in_access_list(&external_address); | ||
state.push_op( | ||
&mut exec_step, | ||
RW::READ, | ||
TxAccessListAccountOp { | ||
tx_id: state.tx_ctx.id(), | ||
address: external_address, | ||
is_warm, | ||
is_warm_prev: is_warm, | ||
}, | ||
); | ||
} | ||
|
||
// Each of CALLDATACOPY, CODECOPY and RETURNDATACOPY has 3 stack read values. | ||
// But EXTCODECOPY has 4. It has an extra stack pop for external address. | ||
let stack_read_num = if is_extcodecopy { 4 } else { 3 }; | ||
for i in 0..stack_read_num { | ||
state.stack_read( | ||
&mut exec_step, | ||
geth_step.stack.nth_last_filled(i), | ||
geth_step.stack.nth_last(i)?, | ||
)?; | ||
} | ||
|
||
state.handle_return(&mut exec_step, geth_steps, true)?; | ||
Ok(vec![exec_step]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.