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

Commit

Permalink
stack error (underflow & overflow) circuit (#873)
Browse files Browse the repository at this point in the history
* draft stack error circuit

* add opcode stack table and constraint

* add tests

* add internal call error test

* merge to latest

* remvoe comment lines

* fix ci

* Empty-Commit

* fix merging issue

* Apply suggestions from code review

Co-authored-by: Miha Stopar <[email protected]>

* small updates

* small updates per review

Co-authored-by: Miha Stopar <[email protected]>
  • Loading branch information
DreamWuGit and miha-stopar authored Dec 20, 2022
1 parent 9400c62 commit 3da1888
Show file tree
Hide file tree
Showing 8 changed files with 547 additions and 57 deletions.
156 changes: 156 additions & 0 deletions eth-types/src/evm_types/opcode_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,162 @@ impl OpcodeId {
}
}

/// Returns the constant min & stack pointer of `OpcodeId`
pub const fn valid_stack_ptr_range(&self) -> (u32, u32) {
match self {
// `min_stack_pointer` 0 means stack overflow never happen, for example, `OpcodeId::ADD`
// can only encounter underflow error, but never encounter overflow error.
// `max_stack_pointer` means max stack poniter for op code normally run. for example,
// `OpcodeId::ADD` 's max stack pointer is 1022, when actual sp > 1022, will
// encounter underflow error.
OpcodeId::STOP => (0, 1024),
OpcodeId::ADD => (0, 1022),
OpcodeId::MUL => (0, 1022),
OpcodeId::SUB => (0, 1022),
OpcodeId::DIV => (0, 1022),
OpcodeId::SDIV => (0, 1022),
OpcodeId::MOD => (0, 1022),
OpcodeId::SMOD => (0, 1022),
OpcodeId::ADDMOD => (0, 1021),
OpcodeId::MULMOD => (0, 1021),
OpcodeId::EXP => (0, 1022),
OpcodeId::SIGNEXTEND => (0, 1022),
OpcodeId::LT => (0, 1022),
OpcodeId::GT => (0, 1022),
OpcodeId::SLT => (0, 1022),
OpcodeId::SGT => (0, 1022),
OpcodeId::EQ => (0, 1022),
OpcodeId::ISZERO => (0, 1022),
OpcodeId::AND => (0, 1022),
OpcodeId::OR => (0, 1022),
OpcodeId::XOR => (0, 1022),
OpcodeId::NOT => (0, 1023),
OpcodeId::BYTE => (0, 1022),
OpcodeId::SHL => (0, 1022),
OpcodeId::SHR => (0, 1022),
OpcodeId::SAR => (0, 1022),
OpcodeId::SHA3 => (0, 1022),
OpcodeId::ADDRESS => (1, 1024),
OpcodeId::BALANCE => (0, 1023),
OpcodeId::ORIGIN => (1, 1024),
OpcodeId::CALLER => (1, 1024),
OpcodeId::CALLVALUE => (1, 1024),
OpcodeId::CALLDATALOAD => (0, 1023),
OpcodeId::CALLDATASIZE => (1, 1024),
OpcodeId::CALLDATACOPY => (0, 1021),
OpcodeId::CODESIZE => (1, 1024),
OpcodeId::CODECOPY => (0, 1021),
OpcodeId::GASPRICE => (1, 1024),
OpcodeId::EXTCODESIZE => (0, 1023),
OpcodeId::EXTCODECOPY => (0, 1020),
OpcodeId::RETURNDATASIZE => (1, 1024),
OpcodeId::RETURNDATACOPY => (0, 1021),
OpcodeId::EXTCODEHASH => (1, 1024),
OpcodeId::BLOCKHASH => (0, 1023),
OpcodeId::COINBASE => (1, 1024),

OpcodeId::TIMESTAMP => (1, 1024),
OpcodeId::NUMBER => (1, 1024),
OpcodeId::DIFFICULTY => (1, 1024),
OpcodeId::GASLIMIT => (1, 1024),
OpcodeId::CHAINID => (1, 1024),
OpcodeId::SELFBALANCE => (1, 1024),
OpcodeId::BASEFEE => (1, 1024),
OpcodeId::POP => (0, 1023),
OpcodeId::MLOAD => (0, 1023),
OpcodeId::MSTORE => (0, 1022),
OpcodeId::MSTORE8 => (0, 1022),
OpcodeId::SLOAD => (0, 1023),
OpcodeId::SSTORE => (0, 1022),
OpcodeId::JUMP => (0, 1023),
OpcodeId::JUMPI => (0, 1022),
OpcodeId::PC => (1, 1024),
OpcodeId::MSIZE => (1, 1024),
OpcodeId::GAS => (1, 1024),
OpcodeId::JUMPDEST => (0, 1024),
OpcodeId::PUSH1 => (1, 1024),
OpcodeId::PUSH2 => (1, 1024),
OpcodeId::PUSH3 => (1, 1024),
OpcodeId::PUSH4 => (1, 1024),
OpcodeId::PUSH5 => (1, 1024),
OpcodeId::PUSH6 => (1, 1024),
OpcodeId::PUSH7 => (1, 1024),
OpcodeId::PUSH8 => (1, 1024),
OpcodeId::PUSH9 => (1, 1024),
OpcodeId::PUSH10 => (1, 1024),
OpcodeId::PUSH11 => (1, 1024),
OpcodeId::PUSH12 => (1, 1024),
OpcodeId::PUSH13 => (1, 1024),
OpcodeId::PUSH14 => (1, 1024),
OpcodeId::PUSH15 => (1, 1024),
OpcodeId::PUSH16 => (1, 1024),
OpcodeId::PUSH17 => (1, 1024),
OpcodeId::PUSH18 => (1, 1024),
OpcodeId::PUSH19 => (1, 1024),
OpcodeId::PUSH20 => (1, 1024),
OpcodeId::PUSH21 => (1, 1024),
OpcodeId::PUSH22 => (1, 1024),
OpcodeId::PUSH23 => (1, 1024),
OpcodeId::PUSH24 => (1, 1024),
OpcodeId::PUSH25 => (1, 1024),
OpcodeId::PUSH26 => (1, 1024),
OpcodeId::PUSH27 => (1, 1024),
OpcodeId::PUSH28 => (1, 1024),
OpcodeId::PUSH29 => (1, 1024),
OpcodeId::PUSH30 => (1, 1024),
OpcodeId::PUSH31 => (1, 1024),
OpcodeId::PUSH32 => (1, 1024),
OpcodeId::DUP1 => (1, 1023),
OpcodeId::DUP2 => (1, 1022),
OpcodeId::DUP3 => (1, 1021),
OpcodeId::DUP4 => (1, 1020),
OpcodeId::DUP5 => (1, 1019),
OpcodeId::DUP6 => (1, 1018),
OpcodeId::DUP7 => (1, 1017),
OpcodeId::DUP8 => (1, 1016),
OpcodeId::DUP9 => (1, 1015),
OpcodeId::DUP10 => (1, 1014),
OpcodeId::DUP11 => (1, 1013),
OpcodeId::DUP12 => (1, 1012),
OpcodeId::DUP13 => (1, 1011),
OpcodeId::DUP14 => (1, 1010),
OpcodeId::DUP15 => (1, 1009),
OpcodeId::DUP16 => (1, 1008),
OpcodeId::SWAP1 => (0, 1022),
OpcodeId::SWAP2 => (0, 1021),
OpcodeId::SWAP3 => (0, 1020),
OpcodeId::SWAP4 => (0, 1019),
OpcodeId::SWAP5 => (0, 1018),
OpcodeId::SWAP6 => (0, 1017),
OpcodeId::SWAP7 => (0, 1016),
OpcodeId::SWAP8 => (0, 1015),
OpcodeId::SWAP9 => (0, 1014),

OpcodeId::SWAP10 => (0, 1013),
OpcodeId::SWAP11 => (0, 1012),
OpcodeId::SWAP12 => (0, 1011),
OpcodeId::SWAP13 => (0, 1010),
OpcodeId::SWAP14 => (0, 1009),
OpcodeId::SWAP15 => (0, 1008),
OpcodeId::SWAP16 => (0, 1007),
OpcodeId::LOG0 => (0, 1022),
OpcodeId::LOG1 => (0, 1021),
OpcodeId::LOG2 => (0, 1020),
OpcodeId::LOG3 => (0, 1019),
OpcodeId::LOG4 => (0, 1018),
OpcodeId::CREATE => (0, 1021),
OpcodeId::CALL => (0, 1017),
OpcodeId::CALLCODE => (0, 1017),
OpcodeId::RETURN => (0, 1022),
OpcodeId::DELEGATECALL => (0, 1018),
OpcodeId::CREATE2 => (0, 1020),
OpcodeId::STATICCALL => (0, 1018),
OpcodeId::REVERT => (0, 1022),
OpcodeId::SELFDESTRUCT => (0, 1023),
_ => (0, 0),
}
}

/// Returns `true` if the `OpcodeId` has memory access
pub const fn has_memory_access(&self) -> bool {
matches!(
Expand Down
16 changes: 7 additions & 9 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ mod error_invalid_jump;
mod error_oog_call;
mod error_oog_constant;
mod error_oog_static_memory;
mod error_stack;
mod exp;
mod extcodehash;
mod gas;
Expand Down Expand Up @@ -112,6 +113,7 @@ use end_tx::EndTxGadget;
use error_invalid_jump::ErrorInvalidJumpGadget;
use error_oog_call::ErrorOOGCallGadget;
use error_oog_constant::ErrorOOGConstantGadget;
use error_stack::ErrorStackGadget;
use exp::ExponentiationGadget;
use extcodehash::ExtcodehashGadget;
use gas::GasGadget;
Expand Down Expand Up @@ -250,8 +252,7 @@ pub(crate) struct ExecutionConfig<F> {
error_oog_constant: ErrorOOGConstantGadget<F>,
error_oog_static_memory_gadget:
DummyGadget<F, 0, 0, { ExecutionState::ErrorOutOfGasStaticMemoryExpansion }>,
error_stack_overflow: DummyGadget<F, 0, 0, { ExecutionState::ErrorStackOverflow }>,
error_stack_underflow: DummyGadget<F, 0, 0, { ExecutionState::ErrorStackUnderflow }>,
error_stack: ErrorStackGadget<F>,
error_oog_dynamic_memory_gadget:
DummyGadget<F, 0, 0, { ExecutionState::ErrorOutOfGasDynamicMemoryExpansion }>,
error_oog_log: DummyGadget<F, 0, 0, { ExecutionState::ErrorOutOfGasLOG }>,
Expand Down Expand Up @@ -486,8 +487,7 @@ impl<F: Field> ExecutionConfig<F> {
// error gadgets
error_oog_constant: configure_gadget!(),
error_oog_static_memory_gadget: configure_gadget!(),
error_stack_overflow: configure_gadget!(),
error_stack_underflow: configure_gadget!(),
error_stack: configure_gadget!(),
error_oog_dynamic_memory_gadget: configure_gadget!(),
error_oog_log: configure_gadget!(),
error_oog_sload: configure_gadget!(),
Expand Down Expand Up @@ -1085,12 +1085,10 @@ impl<F: Field> ExecutionConfig<F> {
ExecutionState::ErrorOutOfGasCodeStore => {
assign_exec_step!(self.error_oog_code_store)
}
ExecutionState::ErrorStackOverflow => {
assign_exec_step!(self.error_stack_overflow)
}
ExecutionState::ErrorStackUnderflow => {
assign_exec_step!(self.error_stack_underflow)
ExecutionState::ErrorStack => {
assign_exec_step!(self.error_stack)
}

ExecutionState::ErrorInsufficientBalance => {
assign_exec_step!(self.error_insufficient_balance)
}
Expand Down
Loading

0 comments on commit 3da1888

Please sign in to comment.