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

Commit

Permalink
reduce super circuit degree to 9 to reduce extend_k from 4 to 3 (#927)
Browse files Browse the repository at this point in the history
* reduce super circuit degree to 9

* refactor super circuit
  • Loading branch information
lispc authored Nov 29, 2022
1 parent 1de7903 commit 43da558
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 53 deletions.
10 changes: 9 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,15 @@ impl<F: Field> ExecutionConfig<F> {
next: Option<(&Transaction, &Call, &ExecStep)>,
power_of_randomness: [F; 31],
) -> Result<(), Error> {
if !matches!(step.execution_state, ExecutionState::EndBlock) {
log::trace!(
"assign_exec_step offset: {} state {:?} step: {:?} call: {:?}",
offset,
step.execution_state,
step,
call
);
}
// Make the region large enough for the current step and the next step.
// The next step's next step may also be accessed, so make the region large
// enough for 3 steps.
Expand Down Expand Up @@ -945,7 +954,6 @@ impl<F: Field> ExecutionConfig<F> {
call: &Call,
step: &ExecStep,
) -> Result<(), Error> {
log::trace!("assign_exec_step offset:{} step:{:?}", offset, step);
self.step
.assign_exec_step(region, offset, block, call, step)?;

Expand Down
40 changes: 20 additions & 20 deletions zkevm-circuits/src/state_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use random_linear_combination::{Chip as RlcChip, Config as RlcConfig, Queries as
use std::collections::HashMap;
use std::{iter::once, marker::PhantomData};

use self::constraint_builder::{MptUpdateTableQueries, RwTableQueries};
use self::{
constraint_builder::{MptUpdateTableQueries, RwTableQueries},
lexicographic_ordering::LimbIndex,
};

const N_LIMBS_RW_COUNTER: usize = 2;
const N_LIMBS_ACCOUNT_ADDRESS: usize = 10;
Expand All @@ -51,6 +54,7 @@ pub struct StateCircuitConfig<F> {
* the MPT, for others, it is 0. */
state_root: Column<Advice>,
lexicographic_ordering: LexicographicOrderingConfig,
not_first_access: Column<Advice>,
lookups: LookupsConfig,
power_of_randomness: [Expression<F>; N_BYTES_WORD - 1],
}
Expand Down Expand Up @@ -104,6 +108,7 @@ impl<F: Field> StateCircuitConfig<F> {
initial_value,
state_root,
lexicographic_ordering,
not_first_access: meta.advice_column(),
lookups,
power_of_randomness: challenges.evm_word_powers_of_randomness(),
rw_table: *rw_table,
Expand Down Expand Up @@ -194,13 +199,21 @@ impl<F: Field> StateCircuitConfig<F> {
}

if let Some(prev_row) = prev_row {
let is_first_access = self
let index = self
.lexicographic_ordering
.assign(region, offset, row, prev_row)?;
let is_first_access =
!matches!(index, LimbIndex::RwCounter0 | LimbIndex::RwCounter1);

region.assign_advice(
|| "not_first_access",
self.not_first_access,
offset,
|| Value::known(if is_first_access { F::zero() } else { F::one() }),
)?;

if is_first_access {
// If previous row was a last access, we need to update the state root.

state_root = randomness
.zip(state_root)
.map(|(randomness, mut state_root)| {
Expand Down Expand Up @@ -458,23 +471,10 @@ fn queries<F: Field>(meta: &mut VirtualCells<'_, F>, c: &StateCircuitConfig<F>)
initial_value_prev: meta.query_advice(c.initial_value, Rotation::prev()),
lookups: LookupsQueries::new(meta, c.lookups),
power_of_randomness: c.power_of_randomness.clone(),
// this isn't binary! only 0 if most significant 4 bits are all 1.
first_access: 4.expr()
- meta.query_advice(first_different_limb.bits[0], Rotation::cur())
- meta.query_advice(first_different_limb.bits[1], Rotation::cur())
- meta.query_advice(first_different_limb.bits[2], Rotation::cur())
- meta.query_advice(first_different_limb.bits[3], Rotation::cur()),
// 1 if first_different_limb is in the rw counter, 0 otherwise (i.e. any of the 4 most
// significant bits are 0)
not_first_access: meta.query_advice(first_different_limb.bits[0], Rotation::cur())
* meta.query_advice(first_different_limb.bits[1], Rotation::cur())
* meta.query_advice(first_different_limb.bits[2], Rotation::cur())
* meta.query_advice(first_different_limb.bits[3], Rotation::cur()),
last_access: 1.expr()
- meta.query_advice(first_different_limb.bits[0], Rotation::next())
* meta.query_advice(first_different_limb.bits[1], Rotation::next())
* meta.query_advice(first_different_limb.bits[2], Rotation::next())
* meta.query_advice(first_different_limb.bits[3], Rotation::next()),
first_different_limb: [0, 1, 2, 3]
.map(|idx| meta.query_advice(first_different_limb.bits[idx], Rotation::cur())),
not_first_access: meta.query_advice(c.not_first_access, Rotation::cur()),
last_access: 1.expr() - meta.query_advice(c.not_first_access, Rotation::next()),
state_root: meta.query_advice(c.state_root, Rotation::cur()),
state_root_prev: meta.query_advice(c.state_root, Rotation::prev()),
}
Expand Down
15 changes: 13 additions & 2 deletions zkevm-circuits/src/state_circuit/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct Queries<F: Field> {
pub initial_value_prev: Expression<F>,
pub lookups: LookupsQueries<F>,
pub power_of_randomness: [Expression<F>; N_BYTES_WORD - 1],
pub first_access: Expression<F>,
pub first_different_limb: [Expression<F>; 4],
pub not_first_access: Expression<F>,
pub last_access: Expression<F>,
pub state_root: Expression<F>,
Expand Down Expand Up @@ -136,6 +136,17 @@ impl<F: Field> ConstraintBuilder<F> {
// tag value in RwTableTag range is enforced in BinaryNumberChip
self.require_boolean("is_write is boolean", q.is_write());

// 1 if first_different_limb is in the rw counter, 0 otherwise (i.e. any of the
// 4 most significant bits are 0)
self.require_equal(
"not_first_access when first 16 limbs are same",
q.not_first_access.clone(),
q.first_different_limb[0].clone()
* q.first_different_limb[1].clone()
* q.first_different_limb[2].clone()
* q.first_different_limb[3].clone(),
);

// When at least one of the keys (tag, id, address, field_tag, or storage_key)
// in the current row differs from the previous row.
self.condition(q.first_access(), |cb| {
Expand Down Expand Up @@ -485,7 +496,7 @@ impl<F: Field> Queries<F> {
}

fn first_access(&self) -> Expression<F> {
self.first_access.clone()
not::expr(self.not_first_access.clone())
}

fn address_change(&self) -> Expression<F> {
Expand Down
7 changes: 2 additions & 5 deletions zkevm-circuits/src/state_circuit/lexicographic_ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl Config {
offset: usize,
cur: &Rw,
prev: &Rw,
) -> Result<bool, Error> {
) -> Result<LimbIndex, Error> {
region.assign_fixed(
|| "upper_limb_difference",
self.selector,
Expand Down Expand Up @@ -228,10 +228,7 @@ impl Config {
|| Value::known(limb_difference.invert().unwrap()),
)?;

Ok(!matches!(
index,
LimbIndex::RwCounter0 | LimbIndex::RwCounter1
))
Ok(index)
}
}

Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/state_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn test_state_circuit_ok(
fn degree() {
let mut meta = ConstraintSystem::<Fr>::default();
StateCircuit::<Fr>::configure(&mut meta);
assert_eq!(meta.degree(), 12);
assert_eq!(meta.degree(), 9);
}

#[test]
Expand Down
57 changes: 33 additions & 24 deletions zkevm-circuits/src/super_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,40 +206,40 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MAX_RWS: u
StateCircuitConfig::configure(meta, &rw_table, &mpt_table, challenges.clone());
let pi_circuit = PiCircuitConfig::new(meta, block_table.clone(), tx_table.clone());

let copy_circuit = CopyCircuit::configure(
meta,
&tx_table,
&rw_table,
&bytecode_table,
copy_table,
q_copy_table,
power_of_randomness[0].clone(),
);
let tx_circuit = TxCircuitConfig::new(
meta,
tx_table.clone(),
keccak_table.clone(),
challenges.clone(),
);
let bytecode_circuit =
BytecodeConfig::configure(meta, bytecode_table.clone(), keccak_table, challenges);
let exp_circuit = ExpCircuitConfig::configure(meta, exp_table);
Self::Config {
tx_table: tx_table.clone(),
tx_table,
rw_table,
mpt_table,
bytecode_table: bytecode_table.clone(),
bytecode_table,
block_table,
copy_table,
exp_table,
evm_circuit,
state_circuit,
copy_circuit: CopyCircuit::configure(
meta,
&tx_table,
&rw_table,
&bytecode_table,
copy_table,
q_copy_table,
power_of_randomness[0].clone(),
),
tx_circuit: TxCircuitConfig::new(
meta,
tx_table,
keccak_table.clone(),
challenges.clone(),
),
bytecode_circuit: BytecodeConfig::configure(
meta,
bytecode_table,
keccak_table,
challenges,
),
copy_circuit,
tx_circuit,
bytecode_circuit,
keccak_circuit,
pi_circuit,
exp_circuit: ExpCircuitConfig::configure(meta, exp_table),
exp_circuit,
}
}

Expand Down Expand Up @@ -448,6 +448,15 @@ mod super_circuit_tests {

use eth_types::{address, bytecode, geth_types::GethData, Word};

#[test]
fn super_circuit_degree() {
let mut cs = ConstraintSystem::<Fr>::default();
SuperCircuit::<_, 1, 32, 256>::configure(&mut cs);
log::info!("super circuit degree: {}", cs.degree());
log::info!("super circuit minimum_rows: {}", cs.minimum_rows());
assert!(cs.degree() <= 9);
}

// High memory usage test. Run in serial with:
// `cargo test [...] serial_ -- --ignored --test-threads 1`
#[ignore]
Expand Down

0 comments on commit 43da558

Please sign in to comment.