Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 4491513

Browse files
committed
Enable BlockCtxU256Gadget for BASEFEE opcode in scroll feature.
1 parent b02de5d commit 4491513

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

zkevm-circuits/src/evm_circuit.rs

-4
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,7 @@ impl<F: Field> EvmCircuit<F> {
283283
}
284284
}
285285

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

293289
impl<F: Field> SubCircuit<F> for EvmCircuit<F> {

zkevm-circuits/src/evm_circuit/execution.rs

+10
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ use address::AddressGadget;
146146
use balance::BalanceGadget;
147147
use begin_tx::BeginTxGadget;
148148
use bitwise::BitwiseGadget;
149+
#[cfg(feature = "scroll")]
150+
use block_ctx::DifficultyGadget;
149151
use block_ctx::{BlockCtxU160Gadget, BlockCtxU256Gadget, BlockCtxU64Gadget};
150152
use blockhash::BlockHashGadget;
151153
use byte::ByteGadget;
@@ -328,6 +330,8 @@ pub(crate) struct ExecutionConfig<F> {
328330
block_ctx_u64_gadget: Box<BlockCtxU64Gadget<F>>,
329331
block_ctx_u160_gadget: Box<BlockCtxU160Gadget<F>>,
330332
block_ctx_u256_gadget: Box<BlockCtxU256Gadget<F>>,
333+
#[cfg(feature = "scroll")]
334+
difficulty_gadget: Box<DifficultyGadget<F>>,
331335
// error gadgets
332336
error_oog_call: Box<ErrorOOGCallGadget<F>>,
333337
error_oog_precompile: Box<ErrorOOGPrecompileGadget<F>>,
@@ -606,6 +610,8 @@ impl<F: Field> ExecutionConfig<F> {
606610
block_ctx_u64_gadget: configure_gadget!(),
607611
block_ctx_u160_gadget: configure_gadget!(),
608612
block_ctx_u256_gadget: configure_gadget!(),
613+
#[cfg(feature = "scroll")]
614+
difficulty_gadget: configure_gadget!(),
609615
// error gadgets
610616
error_oog_constant: configure_gadget!(),
611617
error_oog_static_memory_gadget: configure_gadget!(),
@@ -1439,6 +1445,10 @@ impl<F: Field> ExecutionConfig<F> {
14391445
ExecutionState::BLOCKCTXU64 => assign_exec_step!(self.block_ctx_u64_gadget),
14401446
ExecutionState::BLOCKCTXU160 => assign_exec_step!(self.block_ctx_u160_gadget),
14411447
ExecutionState::BLOCKCTXU256 => assign_exec_step!(self.block_ctx_u256_gadget),
1448+
ExecutionState::DIFFICULTY => {
1449+
#[cfg(feature = "scroll")]
1450+
assign_exec_step!(self.difficulty_gadget)
1451+
}
14421452
ExecutionState::BLOCKHASH => assign_exec_step!(self.blockhash_gadget),
14431453
ExecutionState::SELFBALANCE => assign_exec_step!(self.selfbalance_gadget),
14441454
ExecutionState::CREATE => assign_exec_step!(self.create_gadget),

zkevm-circuits/src/evm_circuit/execution/block_ctx.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,16 @@ impl<F: Field> ExecutionGadget<F> for BlockCtxU160Gadget<F> {
147147
}
148148
}
149149

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

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

@@ -174,6 +177,11 @@ impl<F: Field> ExecutionGadget<F> for BlockCtxU256Gadget<F> {
174177
_: &Call,
175178
step: &ExecStep,
176179
) -> Result<(), Error> {
180+
if cfg!(feature = "scroll") {
181+
panic!("BASEFEE is disabled by scroll for now");
182+
}
183+
log::debug!("BlockCtxU256Gadget assign for {:?}", step.opcode);
184+
177185
self.value_u256
178186
.same_context
179187
.assign_exec_step(region, offset, step)?;
@@ -188,17 +196,20 @@ impl<F: Field> ExecutionGadget<F> for BlockCtxU256Gadget<F> {
188196
}
189197
}
190198

199+
#[cfg(feature = "scroll")]
191200
#[derive(Clone, Debug)]
192-
pub(crate) struct DifficulityGadget<F> {
201+
pub(crate) struct DifficultyGadget<F> {
193202
same_context: SameContextGadget<F>,
194203
}
195204

196-
impl<F: Field> ExecutionGadget<F> for DifficulityGadget<F> {
197-
const NAME: &'static str = "DIFFICULITY";
205+
#[cfg(feature = "scroll")]
206+
impl<F: Field> ExecutionGadget<F> for DifficultyGadget<F> {
207+
const NAME: &'static str = "DIFFICULTY";
198208

199-
const EXECUTION_STATE: ExecutionState = ExecutionState::BLOCKCTXU256;
209+
const EXECUTION_STATE: ExecutionState = ExecutionState::DIFFICULTY;
200210

201211
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
212+
// Always returns 0 for scroll.
202213
cb.stack_push(0.expr());
203214
let opcode = cb.query_cell();
204215
// State transition
@@ -223,17 +234,10 @@ impl<F: Field> ExecutionGadget<F> for DifficulityGadget<F> {
223234
_: &Call,
224235
step: &ExecStep,
225236
) -> Result<(), Error> {
226-
self.same_context.assign_exec_step(region, offset, step)?;
227-
228-
Ok(())
237+
self.same_context.assign_exec_step(region, offset, step)
229238
}
230239
}
231240

232-
// notice in scroll ,the BASEFEE is invalid and difficulity has been
233-
// changed
234-
#[cfg(feature = "scroll")]
235-
pub(crate) use DifficulityGadget as BlockCtxU256Gadget;
236-
237241
#[cfg(test)]
238242
mod test {
239243
use crate::test_util::CircuitTestBuilder;

zkevm-circuits/src/evm_circuit/step.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ pub enum ExecutionState {
8080
BLOCKHASH,
8181
BLOCKCTXU64, // TIMESTAMP, NUMBER, GASLIMIT
8282
BLOCKCTXU160, // COINBASE
83-
BLOCKCTXU256, // DIFFICULTY, BASEFEE
83+
BLOCKCTXU256, // BASEFEE
84+
DIFFICULTY, // DIFFICULTY
8485
CHAINID,
8586
SELFBALANCE,
8687
POP,
@@ -270,11 +271,13 @@ impl ExecutionState {
270271
Self::BLOCKCTXU160 => vec![OpcodeId::COINBASE],
271272
Self::BLOCKCTXU256 => {
272273
if cfg!(feature = "scroll") {
273-
vec![OpcodeId::DIFFICULTY]
274+
vec![OpcodeId::BASEFEE]
274275
} else {
275276
vec![OpcodeId::DIFFICULTY, OpcodeId::BASEFEE]
276277
}
277278
}
279+
#[cfg(feature = "scroll")]
280+
Self::DIFFICULTY => vec![OpcodeId::DIFFICULTY],
278281
Self::CHAINID => vec![OpcodeId::CHAINID],
279282
Self::SELFBALANCE => vec![OpcodeId::SELFBALANCE],
280283
Self::POP => vec![OpcodeId::POP],

zkevm-circuits/src/witness/step.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,14 @@ impl From<&circuit_input_builder::ExecStep> for ExecutionState {
187187
ExecutionState::BLOCKCTXU64
188188
}
189189
OpcodeId::COINBASE => ExecutionState::BLOCKCTXU160,
190-
OpcodeId::DIFFICULTY | OpcodeId::BASEFEE => ExecutionState::BLOCKCTXU256,
190+
OpcodeId::BASEFEE => ExecutionState::BLOCKCTXU256,
191+
OpcodeId::DIFFICULTY => {
192+
if cfg!(feature = "scroll") {
193+
ExecutionState::DIFFICULTY
194+
} else {
195+
ExecutionState::BLOCKCTXU256
196+
}
197+
}
191198
OpcodeId::GAS => ExecutionState::GAS,
192199
OpcodeId::SAR => ExecutionState::SAR,
193200
OpcodeId::SELFBALANCE => ExecutionState::SELFBALANCE,

0 commit comments

Comments
 (0)