Skip to content

Commit

Permalink
fix: only enable circuit for BASEFEE opcode (without opcode mapping) (
Browse files Browse the repository at this point in the history
scroll-tech#1033)

* Enable `BlockCtxU256Gadget` for `BASEFEE` opcode in `scroll` feature.

* Fix to restrict `DIFFICULTY` execution state only for scroll (to avoid soundness).
  • Loading branch information
silathdiir authored Nov 22, 2023
1 parent 0c3b39f commit abb7564
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
4 changes: 0 additions & 4 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,7 @@ impl<F: Field> EvmCircuit<F> {
}
}

// the diff is from the num of valid opcodes: self-destruct?
#[cfg(not(feature = "scroll"))]
const FIXED_TABLE_ROWS_NO_BITWISE: usize = 3647;
#[cfg(feature = "scroll")]
const FIXED_TABLE_ROWS_NO_BITWISE: usize = 3646;
const FIXED_TABLE_ROWS: usize = FIXED_TABLE_ROWS_NO_BITWISE + 3 * 65536;

impl<F: Field> SubCircuit<F> for EvmCircuit<F> {
Expand Down
8 changes: 8 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ use address::AddressGadget;
use balance::BalanceGadget;
use begin_tx::BeginTxGadget;
use bitwise::BitwiseGadget;
#[cfg(feature = "scroll")]
use block_ctx::DifficultyGadget;
use block_ctx::{BlockCtxU160Gadget, BlockCtxU256Gadget, BlockCtxU64Gadget};
use blockhash::BlockHashGadget;
use byte::ByteGadget;
Expand Down Expand Up @@ -328,6 +330,8 @@ pub(crate) struct ExecutionConfig<F> {
block_ctx_u64_gadget: Box<BlockCtxU64Gadget<F>>,
block_ctx_u160_gadget: Box<BlockCtxU160Gadget<F>>,
block_ctx_u256_gadget: Box<BlockCtxU256Gadget<F>>,
#[cfg(feature = "scroll")]
difficulty_gadget: Box<DifficultyGadget<F>>,
// error gadgets
error_oog_call: Box<ErrorOOGCallGadget<F>>,
error_oog_precompile: Box<ErrorOOGPrecompileGadget<F>>,
Expand Down Expand Up @@ -606,6 +610,8 @@ impl<F: Field> ExecutionConfig<F> {
block_ctx_u64_gadget: configure_gadget!(),
block_ctx_u160_gadget: configure_gadget!(),
block_ctx_u256_gadget: configure_gadget!(),
#[cfg(feature = "scroll")]
difficulty_gadget: configure_gadget!(),
// error gadgets
error_oog_constant: configure_gadget!(),
error_oog_static_memory_gadget: configure_gadget!(),
Expand Down Expand Up @@ -1439,6 +1445,8 @@ impl<F: Field> ExecutionConfig<F> {
ExecutionState::BLOCKCTXU64 => assign_exec_step!(self.block_ctx_u64_gadget),
ExecutionState::BLOCKCTXU160 => assign_exec_step!(self.block_ctx_u160_gadget),
ExecutionState::BLOCKCTXU256 => assign_exec_step!(self.block_ctx_u256_gadget),
#[cfg(feature = "scroll")]
ExecutionState::DIFFICULTY => assign_exec_step!(self.difficulty_gadget),
ExecutionState::BLOCKHASH => assign_exec_step!(self.blockhash_gadget),
ExecutionState::SELFBALANCE => assign_exec_step!(self.selfbalance_gadget),
ExecutionState::CREATE => assign_exec_step!(self.create_gadget),
Expand Down
32 changes: 18 additions & 14 deletions zkevm-circuits/src/evm_circuit/execution/block_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,16 @@ impl<F: Field> ExecutionGadget<F> for BlockCtxU160Gadget<F> {
}
}

#[cfg(not(feature = "scroll"))]
// TODO:
// This gadget is used for `BASEFEE` opcode. With current `scroll` feature, it's
// disabled by l2geth and converted to an invalid opcode.
// <https://github.com/scroll-tech/zkevm-circuits/blob/develop/eth-types/src/evm_types/opcode_ids.rs#L1062>
// So need to test it after `BASEFEE` opcode is enabled in scroll l2geth.
#[derive(Clone, Debug)]
pub(crate) struct BlockCtxU256Gadget<F> {
value_u256: BlockCtxGadget<F, N_BYTES_WORD>,
}

#[cfg(not(feature = "scroll"))]
impl<F: Field> ExecutionGadget<F> for BlockCtxU256Gadget<F> {
const NAME: &'static str = "BLOCKCTXU256";

Expand All @@ -174,6 +177,11 @@ impl<F: Field> ExecutionGadget<F> for BlockCtxU256Gadget<F> {
_: &Call,
step: &ExecStep,
) -> Result<(), Error> {
if cfg!(feature = "scroll") {
panic!("BASEFEE is disabled by scroll for now");
}
log::debug!("BlockCtxU256Gadget assign for {:?}", step.opcode);

self.value_u256
.same_context
.assign_exec_step(region, offset, step)?;
Expand All @@ -188,17 +196,20 @@ impl<F: Field> ExecutionGadget<F> for BlockCtxU256Gadget<F> {
}
}

#[cfg(feature = "scroll")]
#[derive(Clone, Debug)]
pub(crate) struct DifficulityGadget<F> {
pub(crate) struct DifficultyGadget<F> {
same_context: SameContextGadget<F>,
}

impl<F: Field> ExecutionGadget<F> for DifficulityGadget<F> {
const NAME: &'static str = "DIFFICULITY";
#[cfg(feature = "scroll")]
impl<F: Field> ExecutionGadget<F> for DifficultyGadget<F> {
const NAME: &'static str = "DIFFICULTY";

const EXECUTION_STATE: ExecutionState = ExecutionState::BLOCKCTXU256;
const EXECUTION_STATE: ExecutionState = ExecutionState::DIFFICULTY;

fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
// Always returns 0 for scroll.
cb.stack_push(0.expr());
let opcode = cb.query_cell();
// State transition
Expand All @@ -223,17 +234,10 @@ impl<F: Field> ExecutionGadget<F> for DifficulityGadget<F> {
_: &Call,
step: &ExecStep,
) -> Result<(), Error> {
self.same_context.assign_exec_step(region, offset, step)?;

Ok(())
self.same_context.assign_exec_step(region, offset, step)
}
}

// notice in scroll ,the BASEFEE is invalid and difficulity has been
// changed
#[cfg(feature = "scroll")]
pub(crate) use DifficulityGadget as BlockCtxU256Gadget;

#[cfg(test)]
mod test {
use crate::test_util::CircuitTestBuilder;
Expand Down
8 changes: 6 additions & 2 deletions zkevm-circuits/src/evm_circuit/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ pub enum ExecutionState {
BLOCKHASH,
BLOCKCTXU64, // TIMESTAMP, NUMBER, GASLIMIT
BLOCKCTXU160, // COINBASE
BLOCKCTXU256, // DIFFICULTY, BASEFEE
BLOCKCTXU256, // BASEFEE, DIFFICULTY (for non-scroll)
#[cfg(feature = "scroll")]
DIFFICULTY, // DIFFICULTY
CHAINID,
SELFBALANCE,
POP,
Expand Down Expand Up @@ -270,11 +272,13 @@ impl ExecutionState {
Self::BLOCKCTXU160 => vec![OpcodeId::COINBASE],
Self::BLOCKCTXU256 => {
if cfg!(feature = "scroll") {
vec![OpcodeId::DIFFICULTY]
vec![OpcodeId::BASEFEE]
} else {
vec![OpcodeId::DIFFICULTY, OpcodeId::BASEFEE]
}
}
#[cfg(feature = "scroll")]
Self::DIFFICULTY => vec![OpcodeId::DIFFICULTY],
Self::CHAINID => vec![OpcodeId::CHAINID],
Self::SELFBALANCE => vec![OpcodeId::SELFBALANCE],
Self::POP => vec![OpcodeId::POP],
Expand Down
6 changes: 5 additions & 1 deletion zkevm-circuits/src/witness/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ impl From<&circuit_input_builder::ExecStep> for ExecutionState {
ExecutionState::BLOCKCTXU64
}
OpcodeId::COINBASE => ExecutionState::BLOCKCTXU160,
OpcodeId::DIFFICULTY | OpcodeId::BASEFEE => ExecutionState::BLOCKCTXU256,
OpcodeId::BASEFEE => ExecutionState::BLOCKCTXU256,
#[cfg(not(feature = "scroll"))]
OpcodeId::DIFFICULTY => ExecutionState::BLOCKCTXU256,
#[cfg(feature = "scroll")]
OpcodeId::DIFFICULTY => ExecutionState::DIFFICULTY,
OpcodeId::GAS => ExecutionState::GAS,
OpcodeId::SAR => ExecutionState::SAR,
OpcodeId::SELFBALANCE => ExecutionState::SELFBALANCE,
Expand Down

0 comments on commit abb7564

Please sign in to comment.