Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
implement oog sha3 error state (#1558)
Browse files Browse the repository at this point in the history
### Description

implement oog sha3 error state

### Issue Link



### Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update


This PR contains:
- refactor memory address gadget to handle overflow condition
- add oog sha3 gadget
- add tests
  • Loading branch information
DreamWuGit authored Aug 15, 2023
1 parent b36da2a commit 5d01150
Show file tree
Hide file tree
Showing 21 changed files with 580 additions and 29 deletions.
3 changes: 3 additions & 0 deletions bus-mapping/src/evm/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ fn fn_gen_error_state_associated_ops(error: &ExecError) -> Option<FnGenAssociate
ExecError::OutOfGas(OogError::AccountAccess) => {
Some(ErrorOOGAccountAccess::gen_associated_ops)
}
ExecError::OutOfGas(OogError::Sha3) => {
Some(StackOnlyOpcode::<2, 0, true>::gen_associated_ops)
}
ExecError::StackOverflow => Some(ErrorSimple::gen_associated_ops),
ExecError::StackUnderflow => Some(ErrorSimple::gen_associated_ops),
// call & callcode can encounter InsufficientBalance error, Use pop-7 generic CallOpcode
Expand Down
17 changes: 15 additions & 2 deletions bus-mapping/src/evm/opcodes/stackonlyop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ use eth_types::GethExecStep;
/// - N = 2: BinaryOpcode
/// - N = 3: TernaryOpcode
#[derive(Debug, Copy, Clone)]
pub(crate) struct StackOnlyOpcode<const N_POP: usize, const N_PUSH: usize>;
pub(crate) struct StackOnlyOpcode<
const N_POP: usize,
const N_PUSH: usize,
const IS_ERR: bool = { false },
>;

impl<const N_POP: usize, const N_PUSH: usize> Opcode for StackOnlyOpcode<N_POP, N_PUSH> {
impl<const N_POP: usize, const N_PUSH: usize, const IS_ERR: bool> Opcode
for StackOnlyOpcode<N_POP, N_PUSH, IS_ERR>
{
fn gen_associated_ops(
state: &mut CircuitInputStateRef,
geth_steps: &[GethExecStep],
Expand All @@ -39,6 +45,13 @@ impl<const N_POP: usize, const N_PUSH: usize> Opcode for StackOnlyOpcode<N_POP,
)?;
}

if IS_ERR {
let next_step = geth_steps.get(1);
exec_step.error = state.get_step_err(geth_step, next_step).unwrap();

state.handle_return(&mut exec_step, geth_steps, true)?;
}

Ok(vec![exec_step])
}
}
Expand Down
5 changes: 5 additions & 0 deletions eth-types/src/evm_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ pub const MAX_REFUND_QUOTIENT_OF_GAS_USED: usize = 5;
/// Gas stipend when CALL or CALLCODE is attached with value.
pub const GAS_STIPEND_CALL_WITH_VALUE: u64 = 2300;

/// This constant ((2^32 - 1) * 32) is the highest number that can be used without overflowing the
/// square operation of gas calculation.
/// <https://github.com/ethereum/go-ethereum/blob/e6b6a8b738069ad0579f6798ee59fde93ed13b43/core/vm/gas_table.go#L38>
pub const MAX_EXPANDED_MEMORY_ADDRESS: u64 = 0x1FFFFFFFE0;

/// Defines the gas consumption.
pub struct GasCost;

Expand Down
3 changes: 3 additions & 0 deletions mock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub use sha3::Sha3CodeGen;
pub use test_ctx::TestContext;
pub use transaction::{AddrOrWallet, MockTransaction, CORRECT_MOCK_TXS};

/// Mock block gas limit
pub const MOCK_BLOCK_GAS_LIMIT: u64 = 10_000_000_000_000_000;

lazy_static! {
/// Mock 1 ETH
pub static ref MOCK_1_ETH: Word = eth(1);
Expand Down
4 changes: 3 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ mod error_oog_constant;
mod error_oog_exp;
mod error_oog_log;
mod error_oog_memory_copy;
mod error_oog_sha3;
mod error_oog_sload_sstore;
mod error_oog_static_memory;
mod error_return_data_oo_bound;
Expand Down Expand Up @@ -152,6 +153,7 @@ use error_oog_constant::ErrorOOGConstantGadget;
use error_oog_exp::ErrorOOGExpGadget;
use error_oog_log::ErrorOOGLogGadget;
use error_oog_memory_copy::ErrorOOGMemoryCopyGadget;
use error_oog_sha3::ErrorOOGSha3Gadget;
use error_oog_sload_sstore::ErrorOOGSloadSstoreGadget;
use error_return_data_oo_bound::ErrorReturnDataOutOfBoundGadget;
use error_stack::ErrorStackGadget;
Expand Down Expand Up @@ -305,8 +307,8 @@ pub struct ExecutionConfig<F> {
error_oog_dynamic_memory_gadget:
Box<DummyGadget<F, 0, 0, { ExecutionState::ErrorOutOfGasDynamicMemoryExpansion }>>,
error_oog_log: Box<ErrorOOGLogGadget<F>>,
error_oog_sha3: Box<ErrorOOGSha3Gadget<F>>,
error_oog_account_access: Box<ErrorOOGAccountAccessGadget<F>>,
error_oog_sha3: Box<DummyGadget<F, 0, 0, { ExecutionState::ErrorOutOfGasSHA3 }>>,
error_oog_ext_codecopy: Box<DummyGadget<F, 0, 0, { ExecutionState::ErrorOutOfGasEXTCODECOPY }>>,
error_oog_create2: Box<DummyGadget<F, 0, 0, { ExecutionState::ErrorOutOfGasCREATE2 }>>,
error_oog_self_destruct:
Expand Down
5 changes: 4 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/calldatacopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use crate::{
ConstrainBuilderCommon, EVMConstraintBuilder, StepStateTransition,
Transition::{Delta, To},
},
memory_gadget::{MemoryAddressGadget, MemoryCopierGasGadget, MemoryExpansionGadget},
memory_gadget::{
CommonMemoryAddressGadget, MemoryAddressGadget, MemoryCopierGasGadget,
MemoryExpansionGadget,
},
not, select, CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/evm_circuit/execution/callop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
math_gadget::{
ConstantDivisionGadget, IsZeroGadget, LtGadget, LtWordGadget, MinMaxGadget,
},
memory_gadget::CommonMemoryAddressGadget,
not, or, select, CachedRegion, Cell,
},
},
Expand Down
5 changes: 4 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/codecopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use crate::{
constraint_builder::{
ConstrainBuilderCommon, EVMConstraintBuilder, StepStateTransition, Transition,
},
memory_gadget::{MemoryAddressGadget, MemoryCopierGasGadget, MemoryExpansionGadget},
memory_gadget::{
CommonMemoryAddressGadget, MemoryAddressGadget, MemoryCopierGasGadget,
MemoryExpansionGadget,
},
not, select, CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
Expand Down
4 changes: 3 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use crate::{
ConstantDivisionGadget, ContractCreateGadget, IsZeroGadget, IsZeroWordGadget,
LtGadget, LtWordGadget,
},
memory_gadget::{MemoryAddressGadget, MemoryExpansionGadget},
memory_gadget::{
CommonMemoryAddressGadget, MemoryAddressGadget, MemoryExpansionGadget,
},
not, AccountAddress, CachedRegion, Cell, Word, WordExpr,
},
witness::{Block, Call, ExecStep, Transaction},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
common_gadget::CommonErrorGadget,
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
math_gadget::LtGadget,
memory_gadget::MemoryAddressGadget,
memory_gadget::{CommonMemoryAddressGadget, MemoryAddressGadget},
CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
common_gadget::CommonErrorGadget,
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
math_gadget::IsEqualGadget,
memory_gadget::MemoryAddressGadget,
memory_gadget::{CommonMemoryAddressGadget, MemoryAddressGadget},
CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
Expand Down
4 changes: 3 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/error_oog_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use crate::{
common_gadget::CommonErrorGadget,
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
math_gadget::LtGadget,
memory_gadget::{MemoryAddressGadget, MemoryExpansionGadget},
memory_gadget::{
CommonMemoryAddressGadget, MemoryAddressGadget, MemoryExpansionGadget,
},
CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use crate::{
common_gadget::CommonErrorGadget,
constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder},
math_gadget::{IsZeroGadget, LtGadget},
memory_gadget::{MemoryAddressGadget, MemoryCopierGasGadget, MemoryExpansionGadget},
memory_gadget::{
CommonMemoryAddressGadget, MemoryAddressGadget, MemoryCopierGasGadget,
MemoryExpansionGadget,
},
select, AccountAddress, CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
Expand Down
Loading

0 comments on commit 5d01150

Please sign in to comment.