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

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ChihChengLiang committed Jan 10, 2024
1 parent 835dc79 commit 684b449
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 36 deletions.
28 changes: 25 additions & 3 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ use std::{
pub use transaction::{Transaction, TransactionContext};
pub use withdrawal::{Withdrawal, WithdrawalContext};


/// Runtime Config
///
/// Default to mainnet block
#[derive(Debug, Clone, Copy)]
pub struct FeatureConfig {
zero_difficulty: bool,
Expand All @@ -52,6 +53,17 @@ pub struct FeatureConfig {
invalid_tx: bool,
}

impl Default for FeatureConfig {
fn default() -> Self {
Self {
zero_difficulty: true,
free_first_tx: false,
enable_eip1559: true,
invalid_tx: false,
}
}
}

/// Circuit Setup Parameters
#[derive(Debug, Clone, Copy)]
pub struct FixedCParams {
Expand Down Expand Up @@ -160,18 +172,26 @@ pub struct CircuitInputBuilder<C: CircuitsParams> {
pub circuits_params: C,
/// Block Context
pub block_ctx: BlockContext,
pub feature_config: FeatureConfig,
}

impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
/// Create a new CircuitInputBuilder from the given `eth_block` and
/// `constants`.
pub fn new(sdb: StateDB, code_db: CodeDB, block: Block, params: C) -> Self {
pub fn new(
sdb: StateDB,
code_db: CodeDB,
block: Block,
params: C,
feature_config: FeatureConfig,
) -> Self {
Self {
sdb,
code_db,
block,
circuits_params: params,
block_ctx: BlockContext::new(),
feature_config,
}
}

Expand Down Expand Up @@ -283,13 +303,15 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
let end_tx_step =
gen_associated_steps(&mut self.state_ref(&mut tx, &mut tx_ctx), ExecState::EndTx)?;
tx.steps_mut().push(end_tx_step);
} else {
} else if self.feature_config.invalid_tx {
// Generate InvalidTx step
let invalid_tx_step = gen_associated_steps(
&mut self.state_ref(&mut tx, &mut tx_ctx),
ExecState::InvalidTx,
)?;
tx.steps_mut().push(invalid_tx_step);
}else {
panic!("invalid tx support not enabled")
}

self.sdb.commit_tx();
Expand Down
74 changes: 42 additions & 32 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub struct EvmCircuitConfigArgs<F: Field> {
pub u8_table: UXTable<8>,
/// U16Table
pub u16_table: UXTable<16>,
pub feature_config: FeatureConfig,
}

impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
Expand All @@ -93,6 +94,7 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
exp_table,
u8_table,
u16_table,
feature_config,
}: Self::ConfigArgs,
) -> Self {
let fixed_table = [(); 4].map(|_| meta.fixed_column());
Expand All @@ -109,6 +111,7 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
&copy_table,
&keccak_table,
&exp_table,
feature_config,
));

u8_table.annotate_columns(meta);
Expand Down Expand Up @@ -356,44 +359,51 @@ pub(crate) mod cached {
impl<F: Field> Circuit<F> for EvmCircuit<F> {
type Config = (EvmCircuitConfig<F>, Challenges);
type FloorPlanner = SimpleFloorPlanner;
type Params = ();
type Params = FeatureConfig;

fn without_witnesses(&self) -> Self {
Self::default()
}

fn configure(meta: &mut ConstraintSystem<F>) -> Self::Config {
let tx_table = TxTable::construct(meta);
let rw_table = RwTable::construct(meta);
let bytecode_table = BytecodeTable::construct(meta);
let block_table = BlockTable::construct(meta);
let q_copy_table = meta.fixed_column();
let copy_table = CopyTable::construct(meta, q_copy_table);
let keccak_table = KeccakTable::construct(meta);
let exp_table = ExpTable::construct(meta);
let u8_table = UXTable::construct(meta);
let u16_table = UXTable::construct(meta);
let challenges = Challenges::construct(meta);
let challenges_expr = challenges.exprs(meta);
fn configure_with_params(
meta: &mut ConstraintSystem<F>,
params: Self::Params,
) -> Self::Config {
let tx_table = TxTable::construct(meta);
let rw_table = RwTable::construct(meta);
let bytecode_table = BytecodeTable::construct(meta);
let block_table = BlockTable::construct(meta);
let q_copy_table = meta.fixed_column();
let copy_table = CopyTable::construct(meta, q_copy_table);
let keccak_table = KeccakTable::construct(meta);
let exp_table = ExpTable::construct(meta);
let u8_table = UXTable::construct(meta);
let u16_table = UXTable::construct(meta);
let challenges = Challenges::construct(meta);
let challenges_expr = challenges.exprs(meta);

(
EvmCircuitConfig::new(
meta,
EvmCircuitConfigArgs {
challenges: challenges_expr,
tx_table,
rw_table,
bytecode_table,
block_table,
copy_table,
keccak_table,
exp_table,
u8_table,
u16_table,
},
),
challenges,
)
}

(
EvmCircuitConfig::new(
meta,
EvmCircuitConfigArgs {
challenges: challenges_expr,
tx_table,
rw_table,
bytecode_table,
block_table,
copy_table,
keccak_table,
exp_table,
u8_table,
u16_table,
},
),
challenges,
)
fn configure(_meta: &mut ConstraintSystem<F>) -> Self::Config {
unreachable!()
}

fn synthesize(
Expand Down
6 changes: 6 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ impl<F: Field> ExecutionConfig<F> {
copy_table: &dyn LookupTable<F>,
keccak_table: &dyn LookupTable<F>,
exp_table: &dyn LookupTable<F>,
feature_config: FeatureConfig,
) -> Self {
let mut instrument = Instrument::default();
let q_usable = meta.complex_selector();
Expand Down Expand Up @@ -395,6 +396,7 @@ impl<F: Field> ExecutionConfig<F> {

let execution_state_selector_constraints = step_curr.state.execution_state.configure();

// TODO: Change logic depending on feature_config.invalid_tx
// NEW: Enabled, this will break hand crafted tests, maybe we can remove them?
let first_step_check = {
let begin_tx_invalid_tx_end_block_selector = step_curr.execution_state_selector([
Expand Down Expand Up @@ -504,6 +506,7 @@ impl<F: Field> ExecutionConfig<F> {
&mut stored_expressions_map,
&mut debug_expressions_map,
&mut instrument,
feature_config.clone(),
))
})()
};
Expand Down Expand Up @@ -654,6 +657,7 @@ impl<F: Field> ExecutionConfig<F> {
stored_expressions_map: &mut HashMap<ExecutionState, Vec<StoredExpression<F>>>,
debug_expressions_map: &mut HashMap<ExecutionState, Vec<(String, Expression<F>)>>,
instrument: &mut Instrument,
feature_config: FeatureConfig,
) -> G {
// Configure the gadget with the max height first so we can find out the actual
// height
Expand All @@ -665,6 +669,7 @@ impl<F: Field> ExecutionConfig<F> {
dummy_step_next,
challenges,
G::EXECUTION_STATE,
feature_config,
);
G::configure(&mut cb);
let (_, _, height, _) = cb.build();
Expand Down Expand Up @@ -782,6 +787,7 @@ impl<F: Field> ExecutionConfig<F> {
let q_step = meta.query_advice(q_step, Rotation::cur());
let q_step_last = meta.query_selector(q_step_last);

// TODO: diable InvalidTx
// ExecutionState transition should be correct.
iter::empty()
.chain(
Expand Down
3 changes: 3 additions & 0 deletions zkevm-circuits/src/evm_circuit/util/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ pub(crate) struct EVMConstraintBuilder<'a, F: Field> {
stored_expressions: Vec<StoredExpression<F>>,
pub(crate) debug_expressions: Vec<(String, Expression<F>)>,
meta: &'a mut ConstraintSystem<F>,
feature_config: FeatureConfig,
}

impl<'a, F: Field> ConstrainBuilderCommon<F> for EVMConstraintBuilder<'a, F> {
Expand All @@ -337,6 +338,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
next: Step<F>,
challenges: &'a Challenges<Expression<F>>,
execution_state: ExecutionState,
feature_config: FeatureConfig,
) -> Self {
Self {
curr,
Expand All @@ -358,6 +360,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
stored_expressions: Vec::new(),
meta,
debug_expressions: Vec::new(),
feature_config,
}
}

Expand Down
4 changes: 3 additions & 1 deletion zkevm-circuits/src/witness/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
util::{log2_ceil, word, SubCircuit},
};
use bus_mapping::{
circuit_input_builder::{self, CopyEvent, ExpEvent, FixedCParams, Withdrawal},
circuit_input_builder::{self, CopyEvent, ExpEvent, FixedCParams, Withdrawal, FeatureConfig},
state_db::CodeDB,
Error,
};
Expand Down Expand Up @@ -43,6 +43,7 @@ pub struct Block<F> {
pub exp_circuit_pad_to: usize,
/// Circuit Setup Parameters
pub circuits_params: FixedCParams,
pub feature_config: FeatureConfig,
/// Inputs to the SHA3 opcode
pub sha3_inputs: Vec<Vec<u8>>,
/// State root of the previous block
Expand Down Expand Up @@ -288,6 +289,7 @@ pub fn block_convert<F: Field>(
exp_events: block.exp_events.clone(),
sha3_inputs: block.sha3_inputs.clone(),
circuits_params: builder.circuits_params,
feature_config: builder.feature_config,
exp_circuit_pad_to: <usize>::default(),
prev_state_root: block.prev_state_root,
keccak_inputs: circuit_input_builder::keccak_inputs(block, code_db)?,
Expand Down

0 comments on commit 684b449

Please sign in to comment.