Skip to content

Commit

Permalink
opcode DIV and MOD (privacy-scaling-explorations#362)
Browse files Browse the repository at this point in the history
* merge div/mod and mul into one gadget.

* rename and tweak

* tweak

* rename exec state

* address comments

Co-authored-by: SwordArt0820 <[email protected]>
  • Loading branch information
icemelon and SwordArt0820 authored Apr 5, 2022
1 parent 19cc589 commit 5328fdf
Show file tree
Hide file tree
Showing 8 changed files with 545 additions and 422 deletions.
180 changes: 82 additions & 98 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use halo2_proofs::{
};
use std::{collections::HashMap, iter};

mod add;
mod add_sub;
mod begin_tx;
mod bitwise;
mod byte;
Expand All @@ -43,7 +43,7 @@ mod jumpi;
mod memory;
mod memory_copy;
mod msize;
mod mul;
mod mul_div_mod;
mod number;
mod origin;
mod pc;
Expand All @@ -58,7 +58,7 @@ mod stop;
mod swap;
mod timestamp;

use add::AddGadget;
use add_sub::AddSubGadget;
use begin_tx::BeginTxGadget;
use bitwise::BitwiseGadget;
use byte::ByteGadget;
Expand All @@ -84,7 +84,7 @@ use jumpi::JumpiGadget;
use memory::MemoryGadget;
use memory_copy::CopyToMemoryGadget;
use msize::MsizeGadget;
use mul::MulGadget;
use mul_div_mod::MulDivModGadget;
use number::NumberGadget;
use origin::OriginGadget;
use pc::PcGadget;
Expand Down Expand Up @@ -124,46 +124,49 @@ pub(crate) struct ExecutionConfig<F> {
q_step_last: Selector,
step: Step<F>,
presets_map: HashMap<ExecutionState, Vec<Preset<F>>>,
add_gadget: AddGadget<F>,
mul_gadget: MulGadget<F>,
bitwise_gadget: BitwiseGadget<F>,
// internal state gadgets
begin_tx_gadget: BeginTxGadget<F>,
copy_to_memory_gadget: CopyToMemoryGadget<F>,
end_block_gadget: EndBlockGadget<F>,
end_tx_gadget: EndTxGadget<F>,
// opcode gadgets
add_sub_gadget: AddSubGadget<F>,
bitwise_gadget: BitwiseGadget<F>,
byte_gadget: ByteGadget<F>,
call_gadget: CallGadget<F>,
call_value_gadget: CallValueGadget<F>,
calldatacopy_gadget: CallDataCopyGadget<F>,
calldataload_gadget: CallDataLoadGadget<F>,
calldatasize_gadget: CallDataSizeGadget<F>,
origin_gadget: OriginGadget<F>,
caller_gadget: CallerGadget<F>,
call_value_gadget: CallValueGadget<F>,
call_gadget: CallGadget<F>,
coinbase_gadget: CoinbaseGadget<F>,
comparator_gadget: ComparatorGadget<F>,
dup_gadget: DupGadget<F>,
end_block_gadget: EndBlockGadget<F>,
end_tx_gadget: EndTxGadget<F>,
error_oog_static_memory_gadget: ErrorOOGStaticMemoryGadget<F>,
extcodehash_gadget: ExtcodehashGadget<F>,
gas_gadget: GasGadget<F>,
gasprice_gadget: GasPriceGadget<F>,
iszero_gadget: IsZeroGadget<F>,
jump_gadget: JumpGadget<F>,
jumpdest_gadget: JumpdestGadget<F>,
jumpi_gadget: JumpiGadget<F>,
gasprice_gadget: GasPriceGadget<F>,
gas_gadget: GasGadget<F>,
memory_gadget: MemoryGadget<F>,
copy_to_memory_gadget: CopyToMemoryGadget<F>,
msize_gadget: MsizeGadget<F>,
mul_div_mod_gadget: MulDivModGadget<F>,
number_gadget: NumberGadget<F>,
origin_gadget: OriginGadget<F>,
pc_gadget: PcGadget<F>,
pop_gadget: PopGadget<F>,
push_gadget: PushGadget<F>,
selfbalance_gadget: SelfbalanceGadget<F>,
signed_comparator_gadget: SignedComparatorGadget<F>,
signextend_gadget: SignextendGadget<F>,
sload_gadget: SloadGadget<F>,
sstore_gadget: SstoreGadget<F>,
stop_gadget: StopGadget<F>,
swap_gadget: SwapGadget<F>,
msize_gadget: MsizeGadget<F>,
coinbase_gadget: CoinbaseGadget<F>,
timestamp_gadget: TimestampGadget<F>,
selfbalance_gadget: SelfbalanceGadget<F>,
number_gadget: NumberGadget<F>,
sload_gadget: SloadGadget<F>,
sstore_gadget: SstoreGadget<F>,
extcodehash_gadget: ExtcodehashGadget<F>,
iszero_gadget: IsZeroGadget<F>,
// error gadgets
error_oog_static_memory_gadget: ErrorOOGStaticMemoryGadget<F>,
}

impl<F: Field> ExecutionConfig<F> {
Expand Down Expand Up @@ -344,46 +347,50 @@ impl<F: Field> ExecutionConfig<F> {
q_step,
q_step_first,
q_step_last,
add_gadget: configure_gadget!(),
mul_gadget: configure_gadget!(),
bitwise_gadget: configure_gadget!(),
// internal states
begin_tx_gadget: configure_gadget!(),
copy_to_memory_gadget: configure_gadget!(),
end_block_gadget: configure_gadget!(),
end_tx_gadget: configure_gadget!(),
// opcode gadgets
add_sub_gadget: configure_gadget!(),
bitwise_gadget: configure_gadget!(),
byte_gadget: configure_gadget!(),
call_gadget: configure_gadget!(),
call_value_gadget: configure_gadget!(),
calldatacopy_gadget: configure_gadget!(),
calldataload_gadget: configure_gadget!(),
calldatasize_gadget: configure_gadget!(),
origin_gadget: configure_gadget!(),
caller_gadget: configure_gadget!(),
call_value_gadget: configure_gadget!(),
call_gadget: configure_gadget!(),
coinbase_gadget: configure_gadget!(),
comparator_gadget: configure_gadget!(),
dup_gadget: configure_gadget!(),
end_block_gadget: configure_gadget!(),
end_tx_gadget: configure_gadget!(),
error_oog_static_memory_gadget: configure_gadget!(),
extcodehash_gadget: configure_gadget!(),
gas_gadget: configure_gadget!(),
gasprice_gadget: configure_gadget!(),
iszero_gadget: configure_gadget!(),
jump_gadget: configure_gadget!(),
jumpdest_gadget: configure_gadget!(),
jumpi_gadget: configure_gadget!(),
gas_gadget: configure_gadget!(),
gasprice_gadget: configure_gadget!(),
memory_gadget: configure_gadget!(),
copy_to_memory_gadget: configure_gadget!(),
msize_gadget: configure_gadget!(),
mul_div_mod_gadget: configure_gadget!(),
number_gadget: configure_gadget!(),
origin_gadget: configure_gadget!(),
pc_gadget: configure_gadget!(),
pop_gadget: configure_gadget!(),
push_gadget: configure_gadget!(),
selfbalance_gadget: configure_gadget!(),
signed_comparator_gadget: configure_gadget!(),
signextend_gadget: configure_gadget!(),
sload_gadget: configure_gadget!(),
sstore_gadget: configure_gadget!(),
stop_gadget: configure_gadget!(),
swap_gadget: configure_gadget!(),
msize_gadget: configure_gadget!(),
coinbase_gadget: configure_gadget!(),
timestamp_gadget: configure_gadget!(),
number_gadget: configure_gadget!(),
sload_gadget: configure_gadget!(),
sstore_gadget: configure_gadget!(),
extcodehash_gadget: configure_gadget!(),
iszero_gadget: configure_gadget!(),
// error gadgets
error_oog_static_memory_gadget: configure_gadget!(),
// step and presets
step: step_curr,
presets_map,
};
Expand Down Expand Up @@ -611,74 +618,51 @@ impl<F: Field> ExecutionConfig<F> {
}

match step.execution_state {
// internal states
ExecutionState::BeginTx => assign_exec_step!(self.begin_tx_gadget),
ExecutionState::CopyToMemory => assign_exec_step!(self.copy_to_memory_gadget),
ExecutionState::EndTx => assign_exec_step!(self.end_tx_gadget),
ExecutionState::EndBlock => {
assign_exec_step!(self.end_block_gadget)
}
ExecutionState::STOP => assign_exec_step!(self.stop_gadget),
ExecutionState::ADD => assign_exec_step!(self.add_gadget),
ExecutionState::MUL => assign_exec_step!(self.mul_gadget),
ExecutionState::EndBlock => assign_exec_step!(self.end_block_gadget),
// opcode
ExecutionState::ADD_SUB => assign_exec_step!(self.add_sub_gadget),
ExecutionState::BITWISE => assign_exec_step!(self.bitwise_gadget),
ExecutionState::SIGNEXTEND => {
assign_exec_step!(self.signextend_gadget)
}
ExecutionState::CMP => assign_exec_step!(self.comparator_gadget),
ExecutionState::SCMP => {
assign_exec_step!(self.signed_comparator_gadget)
}
ExecutionState::BYTE => assign_exec_step!(self.byte_gadget),
ExecutionState::POP => assign_exec_step!(self.pop_gadget),
ExecutionState::MEMORY => assign_exec_step!(self.memory_gadget),
ExecutionState::PC => assign_exec_step!(self.pc_gadget),
ExecutionState::MSIZE => assign_exec_step!(self.msize_gadget),
ExecutionState::JUMP => assign_exec_step!(self.jump_gadget),
ExecutionState::JUMPI => assign_exec_step!(self.jumpi_gadget),
ExecutionState::JUMPDEST => {
assign_exec_step!(self.jumpdest_gadget)
}
ExecutionState::CALL => assign_exec_step!(self.call_gadget),
ExecutionState::CALLDATACOPY => assign_exec_step!(self.calldatacopy_gadget),
ExecutionState::CALLDATALOAD => assign_exec_step!(self.calldataload_gadget),
ExecutionState::CALLDATASIZE => assign_exec_step!(self.calldatasize_gadget),
ExecutionState::CALLER => assign_exec_step!(self.caller_gadget),
ExecutionState::CALLVALUE => assign_exec_step!(self.call_value_gadget),
ExecutionState::COINBASE => assign_exec_step!(self.coinbase_gadget),
ExecutionState::CMP => assign_exec_step!(self.comparator_gadget),
ExecutionState::DUP => assign_exec_step!(self.dup_gadget),
ExecutionState::EXTCODEHASH => assign_exec_step!(self.extcodehash_gadget),
ExecutionState::GAS => assign_exec_step!(self.gas_gadget),
ExecutionState::GASPRICE => assign_exec_step!(self.gasprice_gadget),
ExecutionState::PUSH => assign_exec_step!(self.push_gadget),
ExecutionState::DUP => assign_exec_step!(self.dup_gadget),
ExecutionState::SWAP => assign_exec_step!(self.swap_gadget),
ExecutionState::ISZERO => assign_exec_step!(self.iszero_gadget),
ExecutionState::JUMP => assign_exec_step!(self.jump_gadget),
ExecutionState::JUMPDEST => assign_exec_step!(self.jumpdest_gadget),
ExecutionState::JUMPI => assign_exec_step!(self.jumpi_gadget),
ExecutionState::MEMORY => assign_exec_step!(self.memory_gadget),
ExecutionState::MSIZE => assign_exec_step!(self.msize_gadget),
ExecutionState::MUL_DIV_MOD => assign_exec_step!(self.mul_div_mod_gadget),
ExecutionState::NUMBER => assign_exec_step!(self.number_gadget),
ExecutionState::ORIGIN => assign_exec_step!(self.origin_gadget),
ExecutionState::CALLER => assign_exec_step!(self.caller_gadget),
ExecutionState::CALLVALUE => {
assign_exec_step!(self.call_value_gadget)
}
ExecutionState::COINBASE => assign_exec_step!(self.coinbase_gadget),
ExecutionState::TIMESTAMP => {
assign_exec_step!(self.timestamp_gadget)
}
ExecutionState::NUMBER => {
assign_exec_step!(self.number_gadget)
}
ExecutionState::PC => assign_exec_step!(self.pc_gadget),
ExecutionState::POP => assign_exec_step!(self.pop_gadget),
ExecutionState::PUSH => assign_exec_step!(self.push_gadget),
ExecutionState::SCMP => assign_exec_step!(self.signed_comparator_gadget),
ExecutionState::SELFBALANCE => assign_exec_step!(self.selfbalance_gadget),
ExecutionState::SIGNEXTEND => assign_exec_step!(self.signextend_gadget),
ExecutionState::SLOAD => assign_exec_step!(self.sload_gadget),
ExecutionState::SSTORE => assign_exec_step!(self.sstore_gadget),
ExecutionState::CALLDATACOPY => {
assign_exec_step!(self.calldatacopy_gadget)
}
ExecutionState::EXTCODEHASH => {
assign_exec_step!(self.extcodehash_gadget)
}
ExecutionState::CopyToMemory => {
assign_exec_step!(self.copy_to_memory_gadget)
}
ExecutionState::CALLDATALOAD => {
assign_exec_step!(self.calldataload_gadget)
}
ExecutionState::STOP => assign_exec_step!(self.stop_gadget),
ExecutionState::SWAP => assign_exec_step!(self.swap_gadget),
ExecutionState::TIMESTAMP => assign_exec_step!(self.timestamp_gadget),
// errors
ExecutionState::ErrorOutOfGasStaticMemoryExpansion => {
assign_exec_step!(self.error_oog_static_memory_gadget)
}
ExecutionState::CALLDATASIZE => {
assign_exec_step!(self.calldatasize_gadget)
}
ExecutionState::ISZERO => assign_exec_step!(self.iszero_gadget),
ExecutionState::CALL => {
assign_exec_step!(self.call_gadget)
}
_ => unimplemented!(),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ use halo2_proofs::{circuit::Region, plonk::Error};
// when it's SUB, we annotate stack as [c, b, ...] and [a, ...].
// Then we verify if a + b is equal to c.
#[derive(Clone, Debug)]
pub(crate) struct AddGadget<F> {
pub(crate) struct AddSubGadget<F> {
same_context: SameContextGadget<F>,
add_words: AddWordsGadget<F, 2, false>,
is_sub: PairSelectGadget<F>,
}

impl<F: Field> ExecutionGadget<F> for AddGadget<F> {
impl<F: Field> ExecutionGadget<F> for AddSubGadget<F> {
const NAME: &'static str = "ADD";

const EXECUTION_STATE: ExecutionState = ExecutionState::ADD;
const EXECUTION_STATE: ExecutionState = ExecutionState::ADD_SUB;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();
Expand Down
Loading

0 comments on commit 5328fdf

Please sign in to comment.