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

Commit

Permalink
clean up blockctx variants
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Jun 19, 2023
1 parent 498c502 commit 7b1dc94
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 126 deletions.
15 changes: 4 additions & 11 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,13 @@ mod sstore;
mod stop;
mod swap;

use self::sha3::Sha3Gadget;
use self::{block_ctx::BlockCtxGadget, sha3::Sha3Gadget};
use add_sub::AddSubGadget;
use addmod::AddModGadget;
use address::AddressGadget;
use balance::BalanceGadget;
use begin_tx::BeginTxGadget;
use bitwise::BitwiseGadget;
use block_ctx::{BlockCtxU160Gadget, BlockCtxU256Gadget, BlockCtxU64Gadget};
use blockhash::BlockHashGadget;
use byte::ByteGadget;
use calldatacopy::CallDataCopyGadget;
Expand Down Expand Up @@ -281,9 +280,7 @@ pub struct ExecutionConfig<F> {
stop_gadget: Box<StopGadget<F>>,
swap_gadget: Box<SwapGadget<F>>,
blockhash_gadget: Box<BlockHashGadget<F>>,
block_ctx_u64_gadget: Box<BlockCtxU64Gadget<F>>,
block_ctx_u160_gadget: Box<BlockCtxU160Gadget<F>>,
block_ctx_u256_gadget: Box<BlockCtxU256Gadget<F>>,
block_ctx_gadget: Box<BlockCtxGadget<F>>,
// error gadgets
error_oog_call: Box<ErrorOOGCallGadget<F>>,
error_oog_constant: Box<ErrorOOGConstantGadget<F>>,
Expand Down Expand Up @@ -548,9 +545,7 @@ impl<F: Field> ExecutionConfig<F> {
sstore_gadget: configure_gadget!(),
stop_gadget: configure_gadget!(),
swap_gadget: configure_gadget!(),
block_ctx_u64_gadget: configure_gadget!(),
block_ctx_u160_gadget: configure_gadget!(),
block_ctx_u256_gadget: configure_gadget!(),
block_ctx_gadget: configure_gadget!(),
// error gadgets
error_oog_constant: configure_gadget!(),
error_oog_static_memory_gadget: configure_gadget!(),
Expand Down Expand Up @@ -1216,9 +1211,7 @@ impl<F: Field> ExecutionConfig<F> {
ExecutionState::SAR => assign_exec_step!(self.sar_gadget),
ExecutionState::SCMP => assign_exec_step!(self.signed_comparator_gadget),
ExecutionState::SDIV_SMOD => assign_exec_step!(self.sdiv_smod_gadget),
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),
ExecutionState::BLOCKCTX => assign_exec_step!(self.block_ctx_gadget),
ExecutionState::BLOCKHASH => assign_exec_step!(self.blockhash_gadget),
ExecutionState::SELFBALANCE => assign_exec_step!(self.selfbalance_gadget),
// dummy gadgets
Expand Down
116 changes: 12 additions & 104 deletions zkevm-circuits/src/evm_circuit/execution/block_ctx.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
param::{N_BYTES_ACCOUNT_ADDRESS, N_BYTES_U64, N_BYTES_WORD},
step::ExecutionState,
util::{
common_gadget::SameContextGadget,
Expand All @@ -20,17 +18,22 @@ use bus_mapping::evm::OpcodeId;
use eth_types::Field;
use halo2_proofs::plonk::Error;

use super::ExecutionGadget;

#[derive(Clone, Debug)]
pub(crate) struct BlockCtxGadget<F, const N_BYTES: usize> {
pub(crate) struct BlockCtxGadget<F> {
same_context: SameContextGadget<F>,
value: WordCell<F>,
}

impl<F: Field, const N_BYTES: usize> BlockCtxGadget<F, N_BYTES> {
fn construct(cb: &mut EVMConstraintBuilder<F>) -> Self {
let value = cb.query_word_unchecked(); // block lookup below
impl<F: Field> ExecutionGadget<F> for BlockCtxGadget<F> {
const NAME: &'static str = "BlockCTX";

const EXECUTION_STATE: ExecutionState = ExecutionState::BLOCKCTX;

fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let value = cb.query_word_unchecked(); // block table lookup below

// Push the const generic parameter N_BYTES value to the stack
cb.stack_push_word(value.to_word());

// Get op's FieldTag
Expand All @@ -57,99 +60,6 @@ impl<F: Field, const N_BYTES: usize> BlockCtxGadget<F, N_BYTES> {
value,
}
}
}

#[derive(Clone, Debug)]
pub(crate) struct BlockCtxU64Gadget<F> {
value_u64: BlockCtxGadget<F, N_BYTES_U64>,
}

impl<F: Field> ExecutionGadget<F> for BlockCtxU64Gadget<F> {
const NAME: &'static str = "BlockCTXU64";

const EXECUTION_STATE: ExecutionState = ExecutionState::BLOCKCTXU64;

fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let value_u64 = BlockCtxGadget::construct(cb);

Self { value_u64 }
}

fn assign_exec_step(
&self,
region: &mut CachedRegion<'_, '_, F>,
offset: usize,
block: &Block<F>,
_: &Transaction,
_: &Call,
step: &ExecStep,
) -> Result<(), Error> {
self.value_u64
.same_context
.assign_exec_step(region, offset, step)?;

let value = block.get_rws(step, 0).stack_value();

self.value_u64
.value
.assign_u64(region, offset, u64::try_from(value).unwrap())?;

Ok(())
}
}

#[derive(Clone, Debug)]
pub(crate) struct BlockCtxU160Gadget<F> {
value_u160: BlockCtxGadget<F, N_BYTES_ACCOUNT_ADDRESS>,
}

impl<F: Field> ExecutionGadget<F> for BlockCtxU160Gadget<F> {
const NAME: &'static str = "BlockCTXU160";

const EXECUTION_STATE: ExecutionState = ExecutionState::BLOCKCTXU160;

fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let value_u160 = BlockCtxGadget::construct(cb);

Self { value_u160 }
}

fn assign_exec_step(
&self,
region: &mut CachedRegion<'_, '_, F>,
offset: usize,
block: &Block<F>,
_: &Transaction,
_: &Call,
step: &ExecStep,
) -> Result<(), Error> {
self.value_u160
.same_context
.assign_exec_step(region, offset, step)?;

let value = block.get_rws(step, 0).stack_value();

self.value_u160.value.assign_u256(region, offset, value)?;

Ok(())
}
}

#[derive(Clone, Debug)]
pub(crate) struct BlockCtxU256Gadget<F> {
value_u256: BlockCtxGadget<F, N_BYTES_WORD>,
}

impl<F: Field> ExecutionGadget<F> for BlockCtxU256Gadget<F> {
const NAME: &'static str = "BLOCKCTXU256";

const EXECUTION_STATE: ExecutionState = ExecutionState::BLOCKCTXU256;

fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let value_u256 = BlockCtxGadget::construct(cb);

Self { value_u256 }
}

fn assign_exec_step(
&self,
Expand All @@ -160,13 +70,11 @@ impl<F: Field> ExecutionGadget<F> for BlockCtxU256Gadget<F> {
_: &Call,
step: &ExecStep,
) -> Result<(), Error> {
self.value_u256
.same_context
.assign_exec_step(region, offset, step)?;
self.same_context.assign_exec_step(region, offset, step)?;

let value = block.get_rws(step, 0).stack_value();

self.value_u256.value.assign_u256(region, offset, value)?;
self.value.assign_u256(region, offset, value)?;

Ok(())
}
Expand Down
26 changes: 15 additions & 11 deletions zkevm-circuits/src/evm_circuit/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ pub enum ExecutionState {
RETURNDATACOPY,
EXTCODEHASH,
BLOCKHASH,
BLOCKCTXU64, // TIMESTAMP, NUMBER, GASLIMIT
BLOCKCTXU160, // COINBASE
BLOCKCTXU256, // DIFFICULTY, BASEFEE
BLOCKCTX, // TIMESTAMP, NUMBER, GASLIMIT, COINBASE, DIFFICULTY, BASEFEE
CHAINID,
SELFBALANCE,
POP,
Expand Down Expand Up @@ -239,11 +237,12 @@ impl From<&ExecStep> for ExecutionState {
OpcodeId::EXTCODEHASH => ExecutionState::EXTCODEHASH,
OpcodeId::EXTCODESIZE => ExecutionState::EXTCODESIZE,
OpcodeId::BLOCKHASH => ExecutionState::BLOCKHASH,
OpcodeId::TIMESTAMP | OpcodeId::NUMBER | OpcodeId::GASLIMIT => {
ExecutionState::BLOCKCTXU64
}
OpcodeId::COINBASE => ExecutionState::BLOCKCTXU160,
OpcodeId::DIFFICULTY | OpcodeId::BASEFEE => ExecutionState::BLOCKCTXU256,
OpcodeId::TIMESTAMP
| OpcodeId::NUMBER
| OpcodeId::GASLIMIT
| OpcodeId::COINBASE
| OpcodeId::DIFFICULTY
| OpcodeId::BASEFEE => ExecutionState::BLOCKCTX,
OpcodeId::GAS => ExecutionState::GAS,
OpcodeId::SAR => ExecutionState::SAR,
OpcodeId::SELFBALANCE => ExecutionState::SELFBALANCE,
Expand Down Expand Up @@ -379,9 +378,14 @@ impl ExecutionState {
Self::RETURNDATACOPY => vec![OpcodeId::RETURNDATACOPY],
Self::EXTCODEHASH => vec![OpcodeId::EXTCODEHASH],
Self::BLOCKHASH => vec![OpcodeId::BLOCKHASH],
Self::BLOCKCTXU64 => vec![OpcodeId::TIMESTAMP, OpcodeId::NUMBER, OpcodeId::GASLIMIT],
Self::BLOCKCTXU160 => vec![OpcodeId::COINBASE],
Self::BLOCKCTXU256 => vec![OpcodeId::DIFFICULTY, OpcodeId::BASEFEE],
Self::BLOCKCTX => vec![
OpcodeId::TIMESTAMP,
OpcodeId::NUMBER,
OpcodeId::GASLIMIT,
OpcodeId::COINBASE,
OpcodeId::DIFFICULTY,
OpcodeId::BASEFEE,
],
Self::CHAINID => vec![OpcodeId::CHAINID],
Self::SELFBALANCE => vec![OpcodeId::SELFBALANCE],
Self::POP => vec![OpcodeId::POP],
Expand Down

0 comments on commit 7b1dc94

Please sign in to comment.