Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: word hi lo refactor stage2 #1069

Merged
merged 36 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d4582e8
pi circuit: add rpi_rlc_acc_word and enable keccak lookup
DreamWuGit Dec 20, 2023
9f5ed47
fix pi equality constraint error
DreamWuGit Dec 21, 2023
7deac01
pi enable state_root check between local_conn&state_roots
DreamWuGit Dec 21, 2023
923fcf7
update tx table value to word
DreamWuGit Dec 21, 2023
4104454
update tx circuit per tx table change
DreamWuGit Dec 21, 2023
122eccb
tx circuit update assign_fixed_rows
DreamWuGit Dec 22, 2023
529fec2
update value_is_zero_chip & tx values
DreamWuGit Jan 1, 2024
7376d2f
sig table add msg_hash_word
DreamWuGit Jan 3, 2024
1db703b
disable several yaml tests temporarily
DreamWuGit Jan 3, 2024
38ebd13
fix table assignment
DreamWuGit Jan 3, 2024
5d629ec
update evm circuit to support tx table word lookup
DreamWuGit Jan 4, 2024
d7ab770
try remove msg_hash_rlc in sig circuit
DreamWuGit Jan 4, 2024
56340de
update u8 with uxtable
DreamWuGit Jan 5, 2024
91e3ae2
update less than/comparator to use uxtable
DreamWuGit Jan 7, 2024
43170a0
enable yaml tests
DreamWuGit Jan 8, 2024
21124b2
update u16 with uxtable
DreamWuGit Jan 8, 2024
2703666
clippy fixes
DreamWuGit Jan 9, 2024
e6b12d9
cargo fmt update
DreamWuGit Jan 9, 2024
56b623a
fix test serial_test_simple_pi
DreamWuGit Jan 10, 2024
97cc537
fix value_is_zero_hi_chip &keep gas_price rlc
DreamWuGit Jan 11, 2024
99d92d6
enable test_aggregation_circuit
DreamWuGit Jan 16, 2024
2fa0ec7
fix sig table lookup test error
DreamWuGit Jan 16, 2024
a70809a
fix hash tag lookup into RLP table condition
DreamWuGit Jan 16, 2024
abd493a
fix tx circuit keccak lookup for txhash & txsignhash
DreamWuGit Jan 17, 2024
b7512bd
chagne tx gas price, address to all use lo part
DreamWuGit Jan 17, 2024
214166b
decrease tx circuit degree to 9
DreamWuGit Jan 21, 2024
4def001
disable test_aggregation_circuit to test other issue
DreamWuGit Jan 21, 2024
2e7ad46
fix aggregation test
DreamWuGit Jan 22, 2024
fda8607
add tx_value_evm_rlc column targeting for rlc table lookup
DreamWuGit Jan 22, 2024
7cf08fa
fix constrait: is_none is true => value == 0
DreamWuGit Jan 22, 2024
a552888
fix tx hash constraint
DreamWuGit Jan 22, 2024
ef17e4a
add keccak word and some clean up
DreamWuGit Jan 22, 2024
2535f94
fix super circuit lookup keccak(rpi)
DreamWuGit Jan 23, 2024
86c7819
fix super circuit: state root copy constraints
DreamWuGit Jan 23, 2024
41854dc
fix aggregatio test by removing last two word cells
DreamWuGit Jan 23, 2024
545390f
fix clippy
DreamWuGit Jan 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion aggregator/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ pub(crate) fn extract_hash_cells(
keccak_packed_multi::get_input_bytes_col_idx_in_cell_manager()
+ <KeccakTable as LookupTable<Fr>>::columns(&keccak_config.keccak_table)
.len()
- 1;
- 1
- 2; // exclude last word limbs' columns
for (offset, keccak_row) in witness.iter().enumerate() {
let row = keccak_config.set_row(&mut region, offset, keccak_row)?;

Expand Down
2 changes: 0 additions & 2 deletions aggregator/src/tests/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use crate::{
tests::mock_chunk::MockChunkCircuit, ChunkHash,
};

//TODO: renable this test after pi/tx circuit supooort word hi lo feature.
#[ignore = "renable after all circuits support word hi-lo done"]
#[test]
fn test_aggregation_circuit() {
env_logger::init();
Expand Down
4 changes: 2 additions & 2 deletions gadgets/src/comparator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use eth_types::Field;
use halo2_proofs::{
circuit::{Chip, Region, Value},
plonk::{ConstraintSystem, Error, Expression, TableColumn, VirtualCells},
plonk::{Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells},
poly::Rotation,
};

Expand Down Expand Up @@ -64,7 +64,7 @@ impl<F: Field, const N_BYTES: usize> ComparatorChip<F, N_BYTES> {
q_enable: impl FnOnce(&mut VirtualCells<F>) -> Expression<F> + Clone,
lhs: impl FnOnce(&mut VirtualCells<F>) -> Expression<F> + Clone,
rhs: impl FnOnce(&mut VirtualCells<F>) -> Expression<F> + Clone,
u8_table: TableColumn,
u8_table: Column<Fixed>,
) -> ComparatorConfig<F, N_BYTES> {
let lt_config =
LtChip::configure(meta, q_enable.clone(), lhs.clone(), rhs.clone(), u8_table);
Expand Down
24 changes: 14 additions & 10 deletions gadgets/src/less_than.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use eth_types::Field;
use halo2_proofs::{
circuit::{Chip, Region, Value},
plonk::{Advice, Column, ConstraintSystem, Error, Expression, TableColumn, VirtualCells},
plonk::{Advice, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells},
poly::Rotation,
};

Expand Down Expand Up @@ -37,7 +37,9 @@ pub struct LtConfig<F, const N_BYTES: usize> {
/// Denotes the bytes representation of the difference between lhs and rhs.
pub diff: [Column<Advice>; N_BYTES],
/// Denotes the range within which each byte should lie.
pub u8_table: TableColumn,
//pub u8_table: TableColumn,
pub u8_table: Column<Fixed>,

/// Denotes the range within which both lhs and rhs lie.
pub range: F,
}
Expand Down Expand Up @@ -68,7 +70,8 @@ impl<F: Field, const N_BYTES: usize> LtChip<F, N_BYTES> {
q_enable: impl FnOnce(&mut VirtualCells<'_, F>) -> Expression<F> + Clone,
lhs: impl FnOnce(&mut VirtualCells<F>) -> Expression<F>,
rhs: impl FnOnce(&mut VirtualCells<F>) -> Expression<F>,
u8_table: TableColumn,
//u8_table: TableColumn,
u8_table: Column<Fixed>,
) -> LtConfig<F, N_BYTES> {
let lt = meta.advice_column();
let diff = [(); N_BYTES].map(|_| meta.advice_column());
Expand All @@ -94,11 +97,11 @@ impl<F: Field, const N_BYTES: usize> LtChip<F, N_BYTES> {
});

for cell_column in diff {
meta.lookup("range check for u8", |meta| {
meta.lookup_any("range check for u8", |meta| {
let q_enable = q_enable.clone()(meta);
vec![(
q_enable * meta.query_advice(cell_column, Rotation::cur()),
u8_table,
meta.query_fixed(u8_table, Rotation::cur()),
)]
});
}
Expand Down Expand Up @@ -156,11 +159,12 @@ impl<F: Field, const N_BYTES: usize> LtInstruction<F> for LtChip<F, N_BYTES> {
) -> Result<(), Error> {
const RANGE: usize = u8::MAX as usize;

layouter.assign_table(
//layouter.assign_table(
layouter.assign_region(
|| "load u8 range check table",
|mut table| {
|mut region| {
for i in 0..=RANGE {
table.assign_cell(
region.assign_fixed(
|| "assign cell in fixed column",
self.config.u8_table,
i,
Expand Down Expand Up @@ -265,7 +269,7 @@ mod test {
let q_enable = meta.complex_selector();
let value = meta.advice_column();
let check = meta.advice_column();
let u8_table = meta.lookup_table_column();
let u8_table = meta.fixed_column();

let lt = LtChip::configure(
meta,
Expand Down Expand Up @@ -389,7 +393,7 @@ mod test {
let q_enable = meta.complex_selector();
let (value_a, value_b) = (meta.advice_column(), meta.advice_column());
let check = meta.advice_column();
let u16_table = meta.lookup_table_column();
let u16_table = meta.fixed_column();

let lt = LtChip::configure(
meta,
Expand Down
23 changes: 14 additions & 9 deletions gadgets/src/mul_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use eth_types::{Field, Word, ToU16LittleEndian};
use halo2_proofs::{
circuit::{Region, Value},
plonk::{Advice, Column, ConstraintSystem, Error, Expression, TableColumn, VirtualCells},
plonk::{Advice, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells},
poly::Rotation,
};

Expand All @@ -46,7 +46,8 @@ pub struct MulAddConfig<F> {
/// Sum of the parts higher than 256-bit in the product.
pub overflow: Expression<F>,
/// Lookup table for LtChips and carry_lo/hi.
pub u16_table: TableColumn,
// pub u16_table: TableColumn,
pub u16_table: Column<Fixed>,
/// Range check of a, b which needs to be in [0, 2^64)
pub range_check_64: UIntRangeCheckChip<F, { UIntRangeCheckChip::SIZE_U64 }, 8>,
/// Range check of c, d which needs to be in [0, 2^128)
Expand Down Expand Up @@ -118,7 +119,8 @@ impl<F: Field> MulAddChip<F> {
pub fn configure(
meta: &mut ConstraintSystem<F>,
q_enable: impl FnOnce(&mut VirtualCells<'_, F>) -> Expression<F> + Clone,
u16_table: TableColumn,
//u16_table: TableColumn,
u16_table: Column<Fixed>,
) -> MulAddConfig<F> {
let col0 = meta.advice_column();
let col1 = meta.advice_column();
Expand Down Expand Up @@ -157,9 +159,10 @@ impl<F: Field> MulAddChip<F> {
carry_cols.append(&mut carry_hi_cols.clone());

for (col, rot) in carry_cols.into_iter() {
meta.lookup("mul carry range check lo/hi lookup u16", |meta| {
meta.lookup_any("mul carry range check lo/hi lookup u16", |meta| {
let q_enable = q_enable.clone()(meta);
vec![(q_enable * meta.query_advice(col, Rotation(rot)), u16_table)]
let u16_expr = meta.query_fixed(u16_table, Rotation::cur());
vec![(q_enable * meta.query_advice(col, Rotation(rot)), u16_expr)]
});
}
}
Expand Down Expand Up @@ -506,7 +509,8 @@ mod test {

fn configure(meta: &mut halo2_proofs::plonk::ConstraintSystem<F>) -> Self::Config {
let q_enable = meta.complex_selector();
let u16_table = meta.lookup_table_column();
//let u16_table = meta.lookup_table_column();
let u16_table = meta.fixed_column();
let mul_config =
MulAddChip::configure(meta, |meta| meta.query_selector(q_enable), u16_table);
Self::Config {
Expand All @@ -522,11 +526,12 @@ mod test {
) -> Result<(), halo2_proofs::plonk::Error> {
let chip = MulAddChip::construct(config.mul_config);

layouter.assign_table(
//layouter.assign_table(
layouter.assign_region(
|| "u16 table",
|mut table| {
|mut region| {
for i in 0..=65535 {
table.assign_cell(
region.assign_fixed(
|| format!("u16 table row {i}"),
chip.config.u16_table,
i,
Expand Down
13 changes: 8 additions & 5 deletions gadgets/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::util::Expr;
use eth_types::Field;
use halo2_proofs::{
circuit::{Chip, Region, Value},
plonk::{Advice, Column, ConstraintSystem, Error, Expression, TableColumn, VirtualCells},
plonk::{Advice, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells},
poly::Rotation,
};

Expand All @@ -36,7 +36,8 @@ pub struct UIntRangeCheckConfig<F, const N_2BYTE: usize, const N_EXPR: usize> {
/// Denotes the little-endian representation of expression in u16.
pub u16_repr: [Column<Advice>; N_2BYTE],
/// Denotes the u16 lookup table.
pub u16_table: TableColumn,
//pub u16_table: TableColumn,
pub u16_table: Column<Fixed>,
_marker: std::marker::PhantomData<F>,
}

Expand All @@ -61,7 +62,8 @@ impl<F: Field, const N_2BYTE: usize, const N_EXPR: usize> UIntRangeCheckChip<F,
meta: &mut ConstraintSystem<F>,
q_enable: impl FnOnce(&mut VirtualCells<F>) -> Expression<F> + Clone,
expressions: impl FnOnce(&mut VirtualCells<F>) -> [Expression<F>; N_EXPR],
u16_table: TableColumn,
//u16_table: TableColumn,
u16_table: Column<Fixed>,
) -> UIntRangeCheckConfig<F, N_2BYTE, N_EXPR> {
let u16_repr = [(); N_2BYTE].map(|_| meta.advice_column());

Expand All @@ -83,9 +85,10 @@ impl<F: Field, const N_2BYTE: usize, const N_EXPR: usize> UIntRangeCheckChip<F,
});

for column in u16_repr {
meta.lookup(concat!("u16 cell range check"), |meta| {
meta.lookup_any(concat!("u16 cell range check"), |meta| {
let cell = meta.query_advice(column, Rotation::cur());
vec![(cell, u16_table)]
let u16_expr = meta.query_fixed(u16_table, Rotation::cur());
vec![(cell, u16_expr)]
});
}

Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/calldataload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ impl<F: Field> ExecutionGadget<F> for CallDataLoadGadget<F> {
src_id.expr(),
TxContextFieldTag::CallData,
Some(src_addr.expr() + idx.expr()),
//Word::from_lo_unchecked(buffer_reader.byte(idx)),
buffer_reader.byte(idx),
Word::from_lo_unchecked(buffer_reader.byte(idx)),
// buffer_reader.byte(idx),
);
},
);
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/end_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ impl<F: Field> ExecutionGadget<F> for EndBlockGadget<F> {
total_txs.expr() + 1.expr(),
TxContextFieldTag::CallerAddress,
None,
//Word::zero(),
0.expr(),
Word::zero(),
// 0.expr(),
);
// Since every tx lookup done in the EVM circuit must succeed
// and uses a unique tx_id, we know that at
Expand Down
6 changes: 3 additions & 3 deletions zkevm-circuits/src/evm_circuit/execution/end_inner_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
witness::{Block, Call, ExecStep, Transaction},
},
table::{BlockContextFieldTag, TxFieldTag::BlockNumber},
//util::word::Word,
util::word::Word,
};
use eth_types::Field;
use gadgets::util::{not, Expr};
Expand Down Expand Up @@ -72,8 +72,8 @@ impl<F: Field> ExecutionGadget<F> for EndInnerBlockGadget<F> {
last_tx_id.expr(),
BlockNumber,
None,
// Word::from_lo_unchecked(cb.curr.state.block_number.expr()),
cb.curr.state.block_number.expr(),
Word::from_lo_unchecked(cb.curr.state.block_number.expr()),
// cb.curr.state.block_number.expr(),
);
});

Expand Down
23 changes: 12 additions & 11 deletions zkevm-circuits/src/evm_circuit/execution/gasprice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
table::{CallContextFieldTag, TxContextFieldTag},
util::{
word::{WordCell, WordExpr},
word::{Word, Word32Cell, WordExpr},
Expr,
},
};
Expand All @@ -22,9 +22,8 @@ use halo2_proofs::{circuit::Value, plonk::Error};
#[derive(Clone, Debug)]
pub(crate) struct GasPriceGadget<F> {
tx_id: Cell<F>,
gas_price: WordCell<F>,
// TODO: remove gas_price_rlc in word hi lo stage2 (txtable to word)
gas_price_rlc: Cell<F>,
gas_price: Word32Cell<F>,
// gas_price_rlc: Cell<F>,
same_context: SameContextGadget<F>,
}

Expand All @@ -35,8 +34,9 @@ impl<F: Field> ExecutionGadget<F> for GasPriceGadget<F> {

fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
// Query gasprice value
let gas_price = cb.query_word_unchecked();
let gas_price_rlc = cb.query_cell();
let gas_price = cb.query_word32();
// let gas_price_rlc = cb.query_cell();
let gas_price_rlc = cb.word_rlc(gas_price.limbs.clone().map(|l| l.expr()));

// Lookup in call_ctx the TxId
let tx_id = cb.call_context(None, CallContextFieldTag::TxId);
Expand All @@ -45,8 +45,9 @@ impl<F: Field> ExecutionGadget<F> for GasPriceGadget<F> {
tx_id.expr(),
TxContextFieldTag::GasPrice,
None,
//gas_price.to_word(),
gas_price_rlc.expr(),
// gas_price.to_word(),
// gas_price_rlc.expr(),
Word::from_lo_unchecked(gas_price_rlc.expr()),
);

// Push the value to the stack
Expand All @@ -66,7 +67,7 @@ impl<F: Field> ExecutionGadget<F> for GasPriceGadget<F> {
Self {
tx_id,
gas_price,
gas_price_rlc,
//gas_price_rlc,
same_context,
}
}
Expand All @@ -85,8 +86,8 @@ impl<F: Field> ExecutionGadget<F> for GasPriceGadget<F> {
self.tx_id
.assign(region, offset, Value::known(F::from(tx.id as u64)))?;

self.gas_price_rlc
.assign(region, offset, region.word_rlc(gas_price))?;
// self.gas_price_rlc
// .assign(region, offset, region.word_rlc(gas_price))?;

self.gas_price.assign_u256(region, offset, gas_price)?;

Expand Down
13 changes: 9 additions & 4 deletions zkevm-circuits/src/evm_circuit/execution/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use crate::{
witness::{Block, Call, ExecStep, Transaction},
},
table::{CallContextFieldTag, TxContextFieldTag},
util::{word::WordExpr, Expr},
util::{
word::{Word, WordExpr},
Expr,
},
};
use bus_mapping::evm::OpcodeId;
use eth_types::Field;
Expand All @@ -30,16 +33,18 @@ impl<F: Field> ExecutionGadget<F> for OriginGadget<F> {

fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let origin = cb.query_account_address();
// let address_rlc = cb.word_rlc(origin.limbs.clone().map(|l| l.expr()));

// Lookup in call_ctx the TxId
let tx_id = cb.call_context(None, CallContextFieldTag::TxId);
let address_value = from_bytes::expr(&origin.limbs);
// Lookup rw_table -> call_context with tx origin address
cb.tx_context_lookup(
tx_id.expr(),
TxContextFieldTag::CallerAddress,
None, // None because unrelated to calldata
//origin.to_word(), used in word hi lo stage2
from_bytes::expr(&origin.limbs),
//origin.to_word(),
Word::from_lo_unchecked(address_value),
);

// Push the value to the stack
Expand Down Expand Up @@ -78,7 +83,7 @@ impl<F: Field> ExecutionGadget<F> for OriginGadget<F> {
self.tx_id
.assign(region, offset, Value::known(F::from(tx.id as u64)))?;

// Assign Origin addr RLC.
// Assign Origin addr word.
self.origin.assign_u256(region, offset, origin)?;

// Assign SameContextGadget witnesses.
Expand Down
10 changes: 5 additions & 5 deletions zkevm-circuits/src/evm_circuit/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ pub(crate) enum Lookup<F> {
/// field_tag is Calldata, otherwise should be set to 0.
index: Expression<F>,
/// Value of the field.
// value: Word<Expression<F>>, change to word in stage2
value: Expression<F>,
value: Word<Expression<F>>,
// value: Expression<F>,
},
/// Lookup to read-write table, which contains read-write access records of
/// time-aware data.
Expand Down Expand Up @@ -403,9 +403,9 @@ impl<F: Field> Lookup<F> {
id.clone(),
field_tag.clone(),
index.clone(),
// value.lo(),
// value.hi(),
value.clone(),
value.lo(),
value.hi(),
// value.clone(),
],
Self::Rw {
counter,
Expand Down
Loading