Skip to content

Commit

Permalink
fix advice lookup (scroll-tech#471)
Browse files Browse the repository at this point in the history
* fix poseidon advice lookup

* add the 1.expr for poseidon lookup inputs

* fix mpt advice lookup

* fix rw advice lookup

* fix copy advice lookup

* fix exp advice lookup

* fix rlp advice lookup

* exp table

* fix tx advice lookup

* fixing tx circuit

* change block table tag column to fixed

* fix
  • Loading branch information
lispc authored Apr 17, 2023
1 parent ad1dadd commit d3922e1
Show file tree
Hide file tree
Showing 15 changed files with 398 additions and 141 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions gadgets/src/binary_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::util::{and, not, Expr};
use eth_types::Field;
use halo2_proofs::{
circuit::{Region, Value},
plonk::{Advice, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells},
plonk::{Advice, Any, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells},
poly::Rotation,
};
use std::{collections::BTreeSet, marker::PhantomData};
Expand Down Expand Up @@ -132,7 +132,7 @@ where
pub fn configure(
meta: &mut ConstraintSystem<F>,
selector: Column<Fixed>,
value: Option<Column<Advice>>,
value: Option<Column<Any>>,
) -> BinaryNumberConfig<T, N> {
let bits = [0; N].map(|_| meta.advice_column());
bits.map(|bit| {
Expand All @@ -154,7 +154,7 @@ where
vec![
selector
* (config.value(Rotation::cur())(meta)
- meta.query_advice(value, Rotation::cur())),
- meta.query_any(value, Rotation::cur())),
]
});
}
Expand Down
55 changes: 33 additions & 22 deletions zkevm-circuits/src/bytecode_circuit/circuit/to_poseidon_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use halo2_proofs::{
plonk::{Advice, Column, ConstraintSystem, Error, Expression, VirtualCells},
poly::Rotation,
};
use itertools::Itertools;
use log::trace;
use mpt_zktrie::hash::HASHABLE_DOMAIN_SPEC;
use std::vec;
Expand Down Expand Up @@ -287,10 +288,21 @@ impl<F: Field, const BYTES_IN_FIELD: usize> ToHashBlockCircuitConfig<F, BYTES_IN
// ]))
// });

let pick_hash_tbl_cols = |inp_i: usize| {
let cols =
<PoseidonTable as crate::table::LookupTable<F>>::advice_columns(&poseidon_table);
[cols[0], cols[inp_i + 1], cols[cols.len() - 2]]
let pick_hash_tbl_cols = |meta: &mut VirtualCells<F>, inp_i: usize| {
debug_assert_eq!(PoseidonTable::INPUT_WIDTH, 2);
[
meta.query_fixed(poseidon_table.q_enable, Rotation::cur()),
meta.query_advice(poseidon_table.hash_id, Rotation::cur()),
meta.query_advice(
match inp_i {
0 => poseidon_table.input0,
1 => poseidon_table.input1,
_ => unreachable!("valid poseidon input index"),
},
Rotation::cur(),
),
meta.query_advice(poseidon_table.control, Rotation::cur()),
]
};

// we use a special selection exp for only 2 indexs
Expand All @@ -314,20 +326,20 @@ impl<F: Field, const BYTES_IN_FIELD: usize> ToHashBlockCircuitConfig<F, BYTES_IN
meta.query_advice(is_field_border, Rotation::cur()),
field_selector(meta)[i].clone(),
]);
let mut constraints = Vec::new(); // vec![(
// enable.clone(),
// meta.query_advice(keccak_table.is_enabled, Rotation::cur()),
// )];
let lookup_columns = [
let mut constraints = Vec::new();

let lookup_inputs = [
1.expr(),
meta.query_advice(code_hash, Rotation::cur()),
meta.query_advice(field_input, Rotation::cur()),
meta.query_advice(control_length, Rotation::cur()) * domain_spec_factor.clone(),
];
for (l_col, tbl_col) in lookup_columns.into_iter().zip(pick_hash_tbl_cols(i)) {
constraints.push((
enable.clone() * l_col,
meta.query_advice(tbl_col, Rotation::cur()),
))

for (input_expr, table_expr) in lookup_inputs
.into_iter()
.zip_eq(pick_hash_tbl_cols(meta, i))
{
constraints.push((enable.clone() * input_expr, table_expr))
}
constraints
});
Expand All @@ -344,18 +356,17 @@ impl<F: Field, const BYTES_IN_FIELD: usize> ToHashBlockCircuitConfig<F, BYTES_IN
2.expr() - meta.query_advice(field_index, Rotation::cur()),
]);
let mut constraints = Vec::new();
for (l_exp, tbl_col) in [
let lookup_inputs = [
1.expr(),
meta.query_advice(code_hash, Rotation::cur()),
0.expr(),
meta.query_advice(control_length, Rotation::cur()) * domain_spec_factor,
]
.into_iter()
.zip(pick_hash_tbl_cols(1))
];
for (input_expr, table_expr) in lookup_inputs
.into_iter()
.zip_eq(pick_hash_tbl_cols(meta, 1))
{
constraints.push((
enable.clone() * l_exp,
meta.query_advice(tbl_col, Rotation::cur()),
))
constraints.push((enable.clone() * input_expr, table_expr))
}
constraints
});
Expand Down
10 changes: 10 additions & 0 deletions zkevm-circuits/src/copy_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {
* tag.value_equals(CopyDataType::Memory, Rotation::cur())(meta)
* not::expr(meta.query_advice(is_pad, Rotation::cur()));
vec![
1.expr(),
meta.query_advice(rw_counter, Rotation::cur()),
not::expr(meta.query_selector(q_step)),
RwTableTag::Memory.expr(),
Expand All @@ -369,6 +370,7 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {
let cond = meta.query_fixed(q_enable, Rotation::cur())
* tag.value_equals(CopyDataType::TxLog, Rotation::cur())(meta);
vec![
1.expr(),
meta.query_advice(rw_counter, Rotation::cur()),
1.expr(),
RwTableTag::TxLog.expr(),
Expand Down Expand Up @@ -410,6 +412,7 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {
* tag.value_equals(CopyDataType::TxCalldata, Rotation::cur())(meta)
* not::expr(meta.query_advice(is_pad, Rotation::cur()));
vec![
1.expr(),
meta.query_advice(id, Rotation::cur()),
TxContextFieldTag::CallData.expr(),
meta.query_advice(addr, Rotation::cur()),
Expand Down Expand Up @@ -456,6 +459,13 @@ impl<F: Field> CopyCircuitConfig<F> {
{
let is_read = step_idx % 2 == 0;

region.assign_fixed(
|| format!("q_enable at row: {}", offset),
self.q_enable,
*offset,
|| Value::known(F::one()),
)?;

// Copy table assignments
for (&column, &(value, label)) in
<CopyTable as LookupTable<F>>::advice_columns(&self.copy_table)
Expand Down
8 changes: 5 additions & 3 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,14 @@ impl<F: Field> Circuit<F> for EvmCircuit<F> {
.dev_load(&mut layouter, block.bytecodes.values(), &challenges)?;
config
.block_table
.load(&mut layouter, &block.context, &block.txs, 1, &challenges)?;
config.copy_table.load(&mut layouter, block, &challenges)?;
.dev_load(&mut layouter, &block.context, &block.txs, 1, &challenges)?;
config
.copy_table
.dev_load(&mut layouter, block, &challenges)?;
config
.keccak_table
.dev_load(&mut layouter, &block.sha3_inputs, &challenges)?;
config.exp_table.load(&mut layouter, block)?;
config.exp_table.dev_load(&mut layouter, block)?;

self.synthesize_sub(&config, &challenges, &mut layouter)
}
Expand Down
11 changes: 10 additions & 1 deletion zkevm-circuits/src/evm_circuit/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,21 @@ impl<F: Field> Lookup<F> {
field_tag,
index,
value,
} => vec![id.clone(), field_tag.clone(), index.clone(), value.clone()],
} => vec![
1.expr(),
id.clone(),
field_tag.clone(),
index.clone(),
value.clone(),
],
Self::Rw {
counter,
is_write,
tag,
values,
} => {
vec![
1.expr(),
counter.clone(),
is_write.clone(),
tag.clone(),
Expand Down Expand Up @@ -369,6 +376,7 @@ impl<F: Field> Lookup<F> {
rw_counter,
rwc_inc,
} => vec![
1.expr(),
is_first.clone(),
src_id.clone(),
src_tag.clone(),
Expand Down Expand Up @@ -400,6 +408,7 @@ impl<F: Field> Lookup<F> {
exponent_lo_hi,
exponentiation_lo_hi,
} => vec![
1.expr(), // q_enable
1.expr(), // is_step
identifier.clone(),
is_last.clone(),
Expand Down
25 changes: 15 additions & 10 deletions zkevm-circuits/src/exp_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use gadgets::{
};
use halo2_proofs::{
circuit::{Layouter, Region, Value},
plonk::{ConstraintSystem, Error, Selector},
plonk::{Column, ConstraintSystem, Error, Fixed},
poly::Rotation,
};
use param::*;
Expand All @@ -32,7 +32,7 @@ use std::{marker::PhantomData, ops::Add};
#[derive(Clone, Debug)]
pub struct ExpCircuitConfig<F> {
/// Whether the row is enabled.
pub q_usable: Selector,
pub q_enable: Column<Fixed>,
/// The Exponentiation circuit's table.
pub exp_table: ExpTable,
/// Multiplication gadget for verification of each step.
Expand All @@ -46,16 +46,16 @@ impl<F: Field> SubCircuitConfig<F> for ExpCircuitConfig<F> {

/// Return a new ExpCircuitConfig
fn new(meta: &mut ConstraintSystem<F>, exp_table: Self::ConfigArgs) -> Self {
let q_usable = meta.complex_selector();
let q_enable = exp_table.q_enable;
let mul_gadget = MulAddChip::configure(meta, |meta| {
and::expr([
meta.query_selector(q_usable),
meta.query_fixed(q_enable, Rotation::cur()),
meta.query_fixed(exp_table.is_step, Rotation::cur()),
])
});
let parity_check = MulAddChip::configure(meta, |meta| {
and::expr([
meta.query_selector(q_usable),
meta.query_fixed(q_enable, Rotation::cur()),
meta.query_fixed(exp_table.is_step, Rotation::cur()),
])
});
Expand Down Expand Up @@ -105,7 +105,7 @@ impl<F: Field> SubCircuitConfig<F> for ExpCircuitConfig<F> {
);

cb.gate(and::expr([
meta.query_selector(q_usable),
meta.query_fixed(q_enable, Rotation::cur()),
meta.query_fixed(exp_table.is_step, Rotation::cur()),
not::expr(meta.query_advice(exp_table.is_last, Rotation::cur())),
]))
Expand All @@ -126,7 +126,7 @@ impl<F: Field> SubCircuitConfig<F> for ExpCircuitConfig<F> {
meta.query_advice(exp_table.is_last, Rotation::cur()),
);

cb.gate(meta.query_selector(q_usable))
cb.gate(meta.query_fixed(q_enable, Rotation::cur()))
});

meta.create_gate("verify all steps", |meta| {
Expand Down Expand Up @@ -267,15 +267,15 @@ impl<F: Field> SubCircuitConfig<F> for ExpCircuitConfig<F> {
});

cb.gate(and::expr([
meta.query_selector(q_usable),
meta.query_fixed(q_enable, Rotation::cur()),
meta.query_fixed(exp_table.is_step, Rotation::cur()),
]))
});

exp_table.annotate_columns(meta);

Self {
q_usable,
q_enable,
exp_table,
mul_gadget,
parity_check,
Expand Down Expand Up @@ -411,7 +411,12 @@ impl<F: Field> ExpCircuitConfig<F> {
let (exponent_div2, remainder) = exponent.div_mod(two);

for i in 0..OFFSET_INCREMENT {
self.q_usable.enable(region, offset + i)?;
region.assign_fixed(
|| "assign q_enable",
self.q_enable,
offset + i,
|| Value::known(F::one()),
)?;
}
mul_chip.assign(region, offset, [step.a, step.b, U256::zero(), step.d])?;
parity_check_chip.assign(region, offset, [two, exponent_div2, remainder, *exponent])?;
Expand Down
29 changes: 24 additions & 5 deletions zkevm-circuits/src/mpt_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,31 @@ impl<F: Field> SubCircuitConfig<F> for MptCircuitConfig {
challenges,
}: Self::ConfigArgs,
) -> Self {
let conf = EthTrieConfig::configure_sub(
meta,
mpt_table.0,
poseidon_table.0,
challenges.evm_word(),
let poseidon_table = (
poseidon_table.q_enable,
[
poseidon_table.hash_id,
poseidon_table.input0,
poseidon_table.input1,
poseidon_table.control,
poseidon_table.heading_mark,
],
);
let mpt_table = (
mpt_table.q_enable,
[
mpt_table.address,
mpt_table.storage_key,
mpt_table.proof_type,
mpt_table.new_root,
mpt_table.old_root,
mpt_table.new_value,
mpt_table.old_value,
],
);

let conf =
EthTrieConfig::configure_sub(meta, mpt_table, poseidon_table, challenges.evm_word());
Self(conf)
}
}
Expand Down
15 changes: 11 additions & 4 deletions zkevm-circuits/src/pi_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ impl<F: Field> SubCircuitConfig<F> for PiCircuitConfig<F> {

let q_block_tag = meta.fixed_column();
let cum_num_txs = meta.advice_column();
let block_tag_bits = BinaryNumberChip::configure(meta, q_block_tag, Some(block_table.tag));
let block_tag_bits =
BinaryNumberChip::configure(meta, q_block_tag, Some(block_table.tag.into()));

meta.enable_equality(constant);
meta.enable_equality(rpi_bytes);
Expand Down Expand Up @@ -463,7 +464,7 @@ impl<F: Field> SubCircuitConfig<F> for PiCircuitConfig<F> {
let cum_num_txs_cur = meta.query_advice(cum_num_txs, Rotation::cur());
let cum_num_txs_next = meta.query_advice(cum_num_txs, Rotation::next());
let is_num_txs_field = block_tag_bits.value_equals(BlockContextFieldTag::NumTxs, Rotation::cur())(meta);
let block_tag = meta.query_advice(block_table.tag, Rotation::cur());
let block_tag = meta.query_fixed(block_table.tag, Rotation::cur());
let tag_bits = block_tag_bits.value(Rotation::cur())(meta);

let num_txs = select::expr(
Expand Down Expand Up @@ -1166,12 +1167,18 @@ impl<F: Field> PiCircuitConfig<F> {
.into_iter()
.zip(tag.iter())
{
for (column, value) in block_table_columns.iter().zip_eq(row) {
region.assign_fixed(
|| format!("block table row {}", offset),
self.block_table.tag,
offset,
|| row[0],
)?;
for (column, value) in block_table_columns.iter().zip_eq(&row[1..]) {
let cell = region.assign_advice(
|| format!("block table row {}", offset),
*column,
offset,
|| value,
|| *value,
)?;
if *column == self.block_table.value {
block_value_cells.push(cell);
Expand Down
Loading

0 comments on commit d3922e1

Please sign in to comment.