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

Commit

Permalink
feat: challenge API in evm circuit (#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
adria0 authored Jan 12, 2023
1 parent 9007367 commit 6d12683
Show file tree
Hide file tree
Showing 86 changed files with 989 additions and 936 deletions.
7 changes: 3 additions & 4 deletions integration-tests/src/integration_test_circuits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::collections::HashMap;
use std::sync::Mutex;
use zkevm_circuits::bytecode_circuit::bytecode_unroller::BytecodeCircuit;
use zkevm_circuits::copy_circuit::CopyCircuit;
use zkevm_circuits::evm_circuit::test::{get_test_degree, get_test_instance};
use zkevm_circuits::evm_circuit::test::get_test_degree;
use zkevm_circuits::evm_circuit::{test::get_test_cicuit_from_block, witness::block_convert};
use zkevm_circuits::state_circuit::StateCircuit;
use zkevm_circuits::super_circuit::SuperCircuit;
Expand Down Expand Up @@ -245,13 +245,12 @@ pub async fn test_evm_circuit_block(block_num: u64, actual: bool) {
let block = block_convert(&builder.block, &builder.code_db).unwrap();

let degree = get_test_degree(&block);
let instance = get_test_instance(&block);
let circuit = get_test_cicuit_from_block(block);

if actual {
test_actual(degree, circuit, instance, None);
test_actual(degree, circuit, vec![], None);
} else {
test_mock(degree, &circuit, instance);
test_mock(degree, &circuit, vec![]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/copy_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {
cb.require_equal(
"rows[2].value == rows[0].value * r + rows[1].value",
meta.query_advice(value, Rotation(2)),
meta.query_advice(value, Rotation::cur()) * challenges.evm_word()
meta.query_advice(value, Rotation::cur()) * challenges.keccak_input()
+ meta.query_advice(value, Rotation::next()),
);

Expand Down
79 changes: 36 additions & 43 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ pub struct EvmCircuitConfig<F> {

/// Circuit configuration arguments
pub struct EvmCircuitConfigArgs<F: Field> {
/// Power of randomness
pub power_of_randomness: [Expression<F>; 31],
/// Challenge
pub challenges: Challenges<Expression<F>>,
/// TxTable
pub tx_table: TxTable,
/// RwTable
Expand All @@ -68,7 +68,7 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
fn new(
meta: &mut ConstraintSystem<F>,
Self::ConfigArgs {
power_of_randomness,
challenges,
tx_table,
rw_table,
bytecode_table,
Expand All @@ -82,7 +82,7 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
let byte_table = [(); 1].map(|_| meta.fixed_column());
let execution = Box::new(ExecutionConfig::configure(
meta,
power_of_randomness,
challenges,
&fixed_table,
&byte_table,
&tx_table,
Expand Down Expand Up @@ -233,14 +233,14 @@ impl<F: Field> SubCircuit<F> for EvmCircuit<F> {
fn synthesize_sub(
&self,
config: &Self::Config,
_challenges: &Challenges<Value<F>>,
challenges: &Challenges<Value<F>>,
layouter: &mut impl Layouter<F>,
) -> Result<(), Error> {
let block = self.block.as_ref().unwrap();

config.load_fixed_table(layouter, self.fixed_table_tags.clone())?;
config.load_byte_table(layouter)?;
config.execution.assign_block(layouter, block)
config.execution.assign_block(layouter, block, challenges)
}
}

Expand Down Expand Up @@ -274,14 +274,14 @@ pub mod test {
evm_circuit::{witness::Block, EvmCircuitConfig},
exp_circuit::OFFSET_INCREMENT,
table::{BlockTable, BytecodeTable, CopyTable, ExpTable, KeccakTable, RwTable, TxTable},
util::{power_of_randomness_from_instance, Challenges},
util::Challenges,
witness::block_convert,
};
use bus_mapping::{circuit_input_builder::CircuitsParams, mock::BlockData};
use eth_types::{geth_types::GethData, Field, Word};
use halo2_proofs::halo2curves::bn256::Fr;
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
circuit::{Layouter, SimpleFloorPlanner},
dev::{MockProver, VerifyFailure},
plonk::{Circuit, ConstraintSystem, Error},
};
Expand Down Expand Up @@ -311,7 +311,7 @@ pub mod test {
}

impl<F: Field> Circuit<F> for EvmCircuit<F> {
type Config = EvmCircuitConfig<F>;
type Config = (EvmCircuitConfig<F>, Challenges);
type FloorPlanner = SimpleFloorPlanner;

fn without_witnesses(&self) -> Self {
Expand All @@ -327,20 +327,24 @@ pub mod test {
let copy_table = CopyTable::construct(meta, q_copy_table);
let keccak_table = KeccakTable::construct(meta);
let exp_table = ExpTable::construct(meta);

let power_of_randomness = power_of_randomness_from_instance(meta);
EvmCircuitConfig::new(
meta,
EvmCircuitConfigArgs {
power_of_randomness,
tx_table,
rw_table,
bytecode_table,
block_table,
copy_table,
keccak_table,
exp_table,
},
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,
},
),
challenges,
)
}

Expand All @@ -350,10 +354,9 @@ pub mod test {
mut layouter: impl Layouter<F>,
) -> Result<(), Error> {
let block = self.block.as_ref().unwrap();
let challenges = Challenges::mock(
Value::known(block.randomness),
Value::known(block.randomness),
);

let (config, challenges) = config;
let challenges = challenges.values(&mut layouter);

config.tx_table.load(
&mut layouter,
Expand All @@ -366,14 +369,14 @@ pub mod test {
&mut layouter,
&block.rws.table_assignments(),
block.circuits_params.max_rws,
Value::known(block.randomness),
challenges.evm_word(),
)?;
config
.bytecode_table
.load(&mut layouter, block.bytecodes.values(), &challenges)?;
config
.block_table
.load(&mut layouter, &block.context, block.randomness)?;
.load(&mut layouter, &block.context, challenges.evm_word())?;
config.copy_table.load(&mut layouter, block, &challenges)?;
config
.keccak_table
Expand All @@ -388,13 +391,13 @@ pub mod test {
pub fn get_num_rows_required(block: &Block<F>) -> usize {
let mut cs = ConstraintSystem::default();
let config = EvmCircuit::<F>::configure(&mut cs);
config.get_num_rows_required(block)
config.0.get_num_rows_required(block)
}

pub fn get_active_rows(block: &Block<F>) -> (Vec<usize>, Vec<usize>) {
let mut cs = ConstraintSystem::default();
let config = EvmCircuit::<F>::configure(&mut cs);
config.get_active_rows(block)
config.0.get_active_rows(block)
}
}

Expand Down Expand Up @@ -482,23 +485,13 @@ pub mod test {
EvmCircuit::<F>::new_dev(block, fixed_table_tags)
}

pub fn get_test_instance<F: Field>(block: &Block<F>) -> Vec<Vec<F>> {
let k = get_test_degree(block);

(1..32)
.map(|exp| vec![block.randomness.pow(&[exp, 0, 0, 0]); (1 << k) - 64])
.collect()
}

pub fn run_test_circuit<F: Field>(block: Block<F>) -> Result<(), Vec<VerifyFailure>> {
let k = get_test_degree(&block);

let (active_gate_rows, active_lookup_rows) = EvmCircuit::<F>::get_active_rows(&block);

let power_of_randomness = get_test_instance(&block);

let circuit = get_test_cicuit_from_block(block);
let prover = MockProver::<F>::run(k, &circuit, power_of_randomness).unwrap();
let prover = MockProver::<F>::run(k, &circuit, vec![]).unwrap();
prover.verify_at_rows_par(active_gate_rows.into_iter(), active_lookup_rows.into_iter())
}
}
Expand Down Expand Up @@ -559,7 +552,7 @@ mod evm_circuit_stats {

let mut implemented_states = Vec::new();
for state in ExecutionState::iter() {
let height = circuit.execution.get_step_height_option(state);
let height = circuit.0.execution.get_step_height_option(state);
if let Some(h) = height {
implemented_states.push((state, h));
}
Expand Down
Loading

0 comments on commit 6d12683

Please sign in to comment.