Skip to content

Commit 727c20f

Browse files
authored
feat: word hi lo refactor stage2 (#1069)
* pi circuit: add rpi_rlc_acc_word and enable keccak lookup * fix pi equality constraint error * pi enable state_root check between local_conn&state_roots * update tx table value to word * update tx circuit per tx table change * tx circuit update assign_fixed_rows * update value_is_zero_chip & tx values * sig table add msg_hash_word * disable several yaml tests temporarily * fix table assignment * update evm circuit to support tx table word lookup * try remove msg_hash_rlc in sig circuit * update u8 with uxtable * update less than/comparator to use uxtable * enable yaml tests * update u16 with uxtable * clippy fixes * cargo fmt update * fix test serial_test_simple_pi * fix value_is_zero_hi_chip &keep gas_price rlc * enable test_aggregation_circuit * fix sig table lookup test error * fix hash tag lookup into RLP table condition * fix tx circuit keccak lookup for txhash & txsignhash * chagne tx gas price, address to all use lo part * decrease tx circuit degree to 9 * disable test_aggregation_circuit to test other issue * fix aggregation test * add tx_value_evm_rlc column targeting for rlc table lookup * fix constrait: is_none is true => value == 0 * fix tx hash constraint * add keccak word and some clean up * fix super circuit lookup keccak(rpi) * fix super circuit: state root copy constraints * fix aggregatio test by removing last two word cells * fix clippy
1 parent 52426de commit 727c20f

26 files changed

+886
-380
lines changed

aggregator/src/core.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ pub(crate) fn extract_hash_cells(
284284
keccak_packed_multi::get_input_bytes_col_idx_in_cell_manager()
285285
+ <KeccakTable as LookupTable<Fr>>::columns(&keccak_config.keccak_table)
286286
.len()
287-
- 1;
287+
- 1
288+
- 2; // exclude last word limbs' columns
288289
for (offset, keccak_row) in witness.iter().enumerate() {
289290
let row = keccak_config.set_row(&mut region, offset, keccak_row)?;
290291

aggregator/src/tests/aggregation.rs

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use crate::{
1111
tests::mock_chunk::MockChunkCircuit, ChunkHash,
1212
};
1313

14-
//TODO: renable this test after pi/tx circuit supooort word hi lo feature.
15-
#[ignore = "renable after all circuits support word hi-lo done"]
1614
#[test]
1715
fn test_aggregation_circuit() {
1816
env_logger::init();

gadgets/src/comparator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use eth_types::Field;
55
use halo2_proofs::{
66
circuit::{Chip, Region, Value},
7-
plonk::{ConstraintSystem, Error, Expression, TableColumn, VirtualCells},
7+
plonk::{Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells},
88
poly::Rotation,
99
};
1010

@@ -64,7 +64,7 @@ impl<F: Field, const N_BYTES: usize> ComparatorChip<F, N_BYTES> {
6464
q_enable: impl FnOnce(&mut VirtualCells<F>) -> Expression<F> + Clone,
6565
lhs: impl FnOnce(&mut VirtualCells<F>) -> Expression<F> + Clone,
6666
rhs: impl FnOnce(&mut VirtualCells<F>) -> Expression<F> + Clone,
67-
u8_table: TableColumn,
67+
u8_table: Column<Fixed>,
6868
) -> ComparatorConfig<F, N_BYTES> {
6969
let lt_config =
7070
LtChip::configure(meta, q_enable.clone(), lhs.clone(), rhs.clone(), u8_table);

gadgets/src/less_than.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use eth_types::Field;
44
use halo2_proofs::{
55
circuit::{Chip, Region, Value},
6-
plonk::{Advice, Column, ConstraintSystem, Error, Expression, TableColumn, VirtualCells},
6+
plonk::{Advice, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells},
77
poly::Rotation,
88
};
99

@@ -37,7 +37,9 @@ pub struct LtConfig<F, const N_BYTES: usize> {
3737
/// Denotes the bytes representation of the difference between lhs and rhs.
3838
pub diff: [Column<Advice>; N_BYTES],
3939
/// Denotes the range within which each byte should lie.
40-
pub u8_table: TableColumn,
40+
//pub u8_table: TableColumn,
41+
pub u8_table: Column<Fixed>,
42+
4143
/// Denotes the range within which both lhs and rhs lie.
4244
pub range: F,
4345
}
@@ -68,7 +70,8 @@ impl<F: Field, const N_BYTES: usize> LtChip<F, N_BYTES> {
6870
q_enable: impl FnOnce(&mut VirtualCells<'_, F>) -> Expression<F> + Clone,
6971
lhs: impl FnOnce(&mut VirtualCells<F>) -> Expression<F>,
7072
rhs: impl FnOnce(&mut VirtualCells<F>) -> Expression<F>,
71-
u8_table: TableColumn,
73+
//u8_table: TableColumn,
74+
u8_table: Column<Fixed>,
7275
) -> LtConfig<F, N_BYTES> {
7376
let lt = meta.advice_column();
7477
let diff = [(); N_BYTES].map(|_| meta.advice_column());
@@ -94,11 +97,11 @@ impl<F: Field, const N_BYTES: usize> LtChip<F, N_BYTES> {
9497
});
9598

9699
for cell_column in diff {
97-
meta.lookup("range check for u8", |meta| {
100+
meta.lookup_any("range check for u8", |meta| {
98101
let q_enable = q_enable.clone()(meta);
99102
vec![(
100103
q_enable * meta.query_advice(cell_column, Rotation::cur()),
101-
u8_table,
104+
meta.query_fixed(u8_table, Rotation::cur()),
102105
)]
103106
});
104107
}
@@ -156,11 +159,12 @@ impl<F: Field, const N_BYTES: usize> LtInstruction<F> for LtChip<F, N_BYTES> {
156159
) -> Result<(), Error> {
157160
const RANGE: usize = u8::MAX as usize;
158161

159-
layouter.assign_table(
162+
//layouter.assign_table(
163+
layouter.assign_region(
160164
|| "load u8 range check table",
161-
|mut table| {
165+
|mut region| {
162166
for i in 0..=RANGE {
163-
table.assign_cell(
167+
region.assign_fixed(
164168
|| "assign cell in fixed column",
165169
self.config.u8_table,
166170
i,
@@ -265,7 +269,7 @@ mod test {
265269
let q_enable = meta.complex_selector();
266270
let value = meta.advice_column();
267271
let check = meta.advice_column();
268-
let u8_table = meta.lookup_table_column();
272+
let u8_table = meta.fixed_column();
269273

270274
let lt = LtChip::configure(
271275
meta,
@@ -389,7 +393,7 @@ mod test {
389393
let q_enable = meta.complex_selector();
390394
let (value_a, value_b) = (meta.advice_column(), meta.advice_column());
391395
let check = meta.advice_column();
392-
let u16_table = meta.lookup_table_column();
396+
let u16_table = meta.fixed_column();
393397

394398
let lt = LtChip::configure(
395399
meta,

gadgets/src/mul_add.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use eth_types::{Field, Word, ToU16LittleEndian};
2020
use halo2_proofs::{
2121
circuit::{Region, Value},
22-
plonk::{Advice, Column, ConstraintSystem, Error, Expression, TableColumn, VirtualCells},
22+
plonk::{Advice, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells},
2323
poly::Rotation,
2424
};
2525

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

159161
for (col, rot) in carry_cols.into_iter() {
160-
meta.lookup("mul carry range check lo/hi lookup u16", |meta| {
162+
meta.lookup_any("mul carry range check lo/hi lookup u16", |meta| {
161163
let q_enable = q_enable.clone()(meta);
162-
vec![(q_enable * meta.query_advice(col, Rotation(rot)), u16_table)]
164+
let u16_expr = meta.query_fixed(u16_table, Rotation::cur());
165+
vec![(q_enable * meta.query_advice(col, Rotation(rot)), u16_expr)]
163166
});
164167
}
165168
}
@@ -506,7 +509,8 @@ mod test {
506509

507510
fn configure(meta: &mut halo2_proofs::plonk::ConstraintSystem<F>) -> Self::Config {
508511
let q_enable = meta.complex_selector();
509-
let u16_table = meta.lookup_table_column();
512+
//let u16_table = meta.lookup_table_column();
513+
let u16_table = meta.fixed_column();
510514
let mul_config =
511515
MulAddChip::configure(meta, |meta| meta.query_selector(q_enable), u16_table);
512516
Self::Config {
@@ -522,11 +526,12 @@ mod test {
522526
) -> Result<(), halo2_proofs::plonk::Error> {
523527
let chip = MulAddChip::construct(config.mul_config);
524528

525-
layouter.assign_table(
529+
//layouter.assign_table(
530+
layouter.assign_region(
526531
|| "u16 table",
527-
|mut table| {
532+
|mut region| {
528533
for i in 0..=65535 {
529-
table.assign_cell(
534+
region.assign_fixed(
530535
|| format!("u16 table row {i}"),
531536
chip.config.u16_table,
532537
i,

gadgets/src/range.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::util::Expr;
1010
use eth_types::Field;
1111
use halo2_proofs::{
1212
circuit::{Chip, Region, Value},
13-
plonk::{Advice, Column, ConstraintSystem, Error, Expression, TableColumn, VirtualCells},
13+
plonk::{Advice, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells},
1414
poly::Rotation,
1515
};
1616

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

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

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

8587
for column in u16_repr {
86-
meta.lookup(concat!("u16 cell range check"), |meta| {
88+
meta.lookup_any(concat!("u16 cell range check"), |meta| {
8789
let cell = meta.query_advice(column, Rotation::cur());
88-
vec![(cell, u16_table)]
90+
let u16_expr = meta.query_fixed(u16_table, Rotation::cur());
91+
vec![(cell, u16_expr)]
8992
});
9093
}
9194

zkevm-circuits/src/evm_circuit/execution/calldataload.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ impl<F: Field> ExecutionGadget<F> for CallDataLoadGadget<F> {
157157
src_id.expr(),
158158
TxContextFieldTag::CallData,
159159
Some(src_addr.expr() + idx.expr()),
160-
//Word::from_lo_unchecked(buffer_reader.byte(idx)),
161-
buffer_reader.byte(idx),
160+
Word::from_lo_unchecked(buffer_reader.byte(idx)),
161+
// buffer_reader.byte(idx),
162162
);
163163
},
164164
);

zkevm-circuits/src/evm_circuit/execution/end_block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ impl<F: Field> ExecutionGadget<F> for EndBlockGadget<F> {
121121
total_txs.expr() + 1.expr(),
122122
TxContextFieldTag::CallerAddress,
123123
None,
124-
//Word::zero(),
125-
0.expr(),
124+
Word::zero(),
125+
// 0.expr(),
126126
);
127127
// Since every tx lookup done in the EVM circuit must succeed
128128
// and uses a unique tx_id, we know that at

zkevm-circuits/src/evm_circuit/execution/end_inner_block.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
witness::{Block, Call, ExecStep, Transaction},
1111
},
1212
table::{BlockContextFieldTag, TxFieldTag::BlockNumber},
13-
//util::word::Word,
13+
util::word::Word,
1414
};
1515
use eth_types::Field;
1616
use gadgets::util::{not, Expr};
@@ -72,8 +72,8 @@ impl<F: Field> ExecutionGadget<F> for EndInnerBlockGadget<F> {
7272
last_tx_id.expr(),
7373
BlockNumber,
7474
None,
75-
// Word::from_lo_unchecked(cb.curr.state.block_number.expr()),
76-
cb.curr.state.block_number.expr(),
75+
Word::from_lo_unchecked(cb.curr.state.block_number.expr()),
76+
// cb.curr.state.block_number.expr(),
7777
);
7878
});
7979

zkevm-circuits/src/evm_circuit/execution/gasprice.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
},
1212
table::{CallContextFieldTag, TxContextFieldTag},
1313
util::{
14-
word::{WordCell, WordExpr},
14+
word::{Word, Word32Cell, WordExpr},
1515
Expr,
1616
},
1717
};
@@ -22,9 +22,8 @@ use halo2_proofs::{circuit::Value, plonk::Error};
2222
#[derive(Clone, Debug)]
2323
pub(crate) struct GasPriceGadget<F> {
2424
tx_id: Cell<F>,
25-
gas_price: WordCell<F>,
26-
// TODO: remove gas_price_rlc in word hi lo stage2 (txtable to word)
27-
gas_price_rlc: Cell<F>,
25+
gas_price: Word32Cell<F>,
26+
// gas_price_rlc: Cell<F>,
2827
same_context: SameContextGadget<F>,
2928
}
3029

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

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

4141
// Lookup in call_ctx the TxId
4242
let tx_id = cb.call_context(None, CallContextFieldTag::TxId);
@@ -45,8 +45,9 @@ impl<F: Field> ExecutionGadget<F> for GasPriceGadget<F> {
4545
tx_id.expr(),
4646
TxContextFieldTag::GasPrice,
4747
None,
48-
//gas_price.to_word(),
49-
gas_price_rlc.expr(),
48+
// gas_price.to_word(),
49+
// gas_price_rlc.expr(),
50+
Word::from_lo_unchecked(gas_price_rlc.expr()),
5051
);
5152

5253
// Push the value to the stack
@@ -66,7 +67,7 @@ impl<F: Field> ExecutionGadget<F> for GasPriceGadget<F> {
6667
Self {
6768
tx_id,
6869
gas_price,
69-
gas_price_rlc,
70+
//gas_price_rlc,
7071
same_context,
7172
}
7273
}
@@ -85,8 +86,8 @@ impl<F: Field> ExecutionGadget<F> for GasPriceGadget<F> {
8586
self.tx_id
8687
.assign(region, offset, Value::known(F::from(tx.id as u64)))?;
8788

88-
self.gas_price_rlc
89-
.assign(region, offset, region.word_rlc(gas_price))?;
89+
// self.gas_price_rlc
90+
// .assign(region, offset, region.word_rlc(gas_price))?;
9091

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

zkevm-circuits/src/evm_circuit/execution/origin.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use crate::{
1010
witness::{Block, Call, ExecStep, Transaction},
1111
},
1212
table::{CallContextFieldTag, TxContextFieldTag},
13-
util::{word::WordExpr, Expr},
13+
util::{
14+
word::{Word, WordExpr},
15+
Expr,
16+
},
1417
};
1518
use bus_mapping::evm::OpcodeId;
1619
use eth_types::Field;
@@ -30,16 +33,18 @@ impl<F: Field> ExecutionGadget<F> for OriginGadget<F> {
3033

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

3438
// Lookup in call_ctx the TxId
3539
let tx_id = cb.call_context(None, CallContextFieldTag::TxId);
40+
let address_value = from_bytes::expr(&origin.limbs);
3641
// Lookup rw_table -> call_context with tx origin address
3742
cb.tx_context_lookup(
3843
tx_id.expr(),
3944
TxContextFieldTag::CallerAddress,
4045
None, // None because unrelated to calldata
41-
//origin.to_word(), used in word hi lo stage2
42-
from_bytes::expr(&origin.limbs),
46+
//origin.to_word(),
47+
Word::from_lo_unchecked(address_value),
4348
);
4449

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

81-
// Assign Origin addr RLC.
86+
// Assign Origin addr word.
8287
self.origin.assign_u256(region, offset, origin)?;
8388

8489
// Assign SameContextGadget witnesses.

zkevm-circuits/src/evm_circuit/table.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ pub(crate) enum Lookup<F> {
229229
/// field_tag is Calldata, otherwise should be set to 0.
230230
index: Expression<F>,
231231
/// Value of the field.
232-
// value: Word<Expression<F>>, change to word in stage2
233-
value: Expression<F>,
232+
value: Word<Expression<F>>,
233+
// value: Expression<F>,
234234
},
235235
/// Lookup to read-write table, which contains read-write access records of
236236
/// time-aware data.
@@ -403,9 +403,9 @@ impl<F: Field> Lookup<F> {
403403
id.clone(),
404404
field_tag.clone(),
405405
index.clone(),
406-
// value.lo(),
407-
// value.hi(),
408-
value.clone(),
406+
value.lo(),
407+
value.hi(),
408+
// value.clone(),
409409
],
410410
Self::Rw {
411411
counter,

0 commit comments

Comments
 (0)