Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 2773896

Browse files
authored
Refactor: make witness gen of tx circuit more easier to maintain (#800)
* refactor witness gen of tx circuit * import tx_value_cells from tx circuit into pi_circuit
1 parent 7834cc6 commit 2773896

File tree

7 files changed

+632
-705
lines changed

7 files changed

+632
-705
lines changed

zkevm-circuits/src/pi_circuit.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod param;
77
#[cfg(any(feature = "test", test, feature = "test-circuits"))]
88
mod test;
99

10-
use std::{collections::BTreeMap, iter, marker::PhantomData, str::FromStr};
10+
use std::{cell::RefCell, collections::BTreeMap, iter, marker::PhantomData, str::FromStr};
1111

1212
use crate::{evm_circuit::util::constraint_builder::ConstrainBuilderCommon, table::KeccakTable};
1313
use bus_mapping::circuit_input_builder::get_dummy_tx_hash;
@@ -653,6 +653,7 @@ impl<F: Field> PiCircuitConfig<F> {
653653
region: &mut Region<'_, F>,
654654
public_data: &PublicData,
655655
block_value_cells: &[AssignedCell<F, F>],
656+
tx_value_cells: &[AssignedCell<F, F>],
656657
challenges: &Challenges<Value<F>>,
657658
) -> Result<(PiHashExport<F>, Connections<F>), Error> {
658659
let block_values = &public_data.block_ctxs;
@@ -817,11 +818,7 @@ impl<F: Field> PiCircuitConfig<F> {
817818
for (i, tx_hash_cell) in tx_copy_cells.into_iter().enumerate() {
818819
region.constrain_equal(
819820
tx_hash_cell.cell(),
820-
Cell {
821-
region_index: RegionIndex(1), // FIXME: this is not safe
822-
row_offset: i * TX_LEN + TX_HASH_OFFSET,
823-
column: self.tx_table.value.into(),
824-
},
821+
tx_value_cells[i * TX_LEN + TX_HASH_OFFSET - 1].cell(),
825822
)?;
826823
}
827824

@@ -1476,7 +1473,8 @@ pub struct PiCircuit<F: Field> {
14761473

14771474
_marker: PhantomData<F>,
14781475

1479-
connections: std::cell::RefCell<Option<Connections<F>>>,
1476+
connections: RefCell<Option<Connections<F>>>,
1477+
tx_value_cells: RefCell<Option<Vec<AssignedCell<F, F>>>>,
14801478
}
14811479

14821480
impl<F: Field> PiCircuit<F> {
@@ -1503,6 +1501,7 @@ impl<F: Field> PiCircuit<F> {
15031501
max_inner_blocks,
15041502
_marker: PhantomData,
15051503
connections: Default::default(),
1504+
tx_value_cells: RefCell::new(None),
15061505
}
15071506
}
15081507

@@ -1511,6 +1510,11 @@ impl<F: Field> PiCircuit<F> {
15111510
&self.public_data.transactions
15121511
}
15131512

1513+
/// Import tx value cells from Tx circuit
1514+
pub fn import_tx_values(&self, values: Vec<AssignedCell<F, F>>) {
1515+
*self.tx_value_cells.borrow_mut() = Some(values);
1516+
}
1517+
15141518
/// Connect the exportings from other circuit when we are in super circuit
15151519
pub fn connect_export(
15161520
&self,
@@ -1634,6 +1638,11 @@ impl<F: Field> SubCircuit<F> for PiCircuit<F> {
16341638
config.block_table.annotate_columns_in_region(&mut region);
16351639

16361640
// assign block table
1641+
let tx_value_cells = self
1642+
.tx_value_cells
1643+
.borrow()
1644+
.clone()
1645+
.expect("tx_value_cells must have been set");
16371646
let block_value_cells = config.assign_block_table(
16381647
&mut region,
16391648
&self.public_data,
@@ -1645,6 +1654,7 @@ impl<F: Field> SubCircuit<F> for PiCircuit<F> {
16451654
&mut region,
16461655
&self.public_data,
16471656
&block_value_cells,
1657+
&tx_value_cells,
16481658
challenges,
16491659
)?;
16501660

zkevm-circuits/src/pi_circuit/dev.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MAX_INNER_
9090
let challenges = challenges.values(&layouter);
9191

9292
// assign tx table
93-
config.tx_table.load(
93+
let tx_value_cells = config.tx_table.load(
9494
&mut layouter,
9595
&self.0.public_data.transactions,
9696
self.0.max_txs,
@@ -108,6 +108,7 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MAX_INNER_
108108
.keccak_table
109109
.dev_load(&mut layouter, vec![&data_bytes, &pi_bytes], &challenges)?;
110110

111+
self.0.import_tx_values(tx_value_cells);
111112
self.0.synthesize_sub(&config, &challenges, &mut layouter)?;
112113

113114
Ok(())

zkevm-circuits/src/super_circuit.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,9 @@ impl<
652652
self.evm_circuit
653653
.synthesize_sub(&config.evm_circuit, challenges, layouter)?;
654654

655+
self.pi_circuit
656+
.import_tx_values(self.tx_circuit.value_cells.borrow().clone().unwrap());
657+
655658
self.pi_circuit
656659
.synthesize_sub(&config.pi_circuit, challenges, layouter)?;
657660

@@ -710,17 +713,6 @@ impl<
710713
) -> Result<(), Error> {
711714
let challenges = challenges.values(&layouter);
712715

713-
let block = self.evm_circuit.block.as_ref().unwrap();
714-
715-
config.tx_table.load(
716-
&mut layouter,
717-
&block.txs,
718-
block.circuits_params.max_txs,
719-
block.circuits_params.max_calldata,
720-
block.chain_id,
721-
&challenges,
722-
)?;
723-
724716
config.u8_table.load(&mut layouter)?;
725717
config.u16_table.load(&mut layouter)?;
726718

zkevm-circuits/src/table.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl TxTable {
230230
max_calldata: usize,
231231
chain_id: u64,
232232
challenges: &Challenges<Value<F>>,
233-
) -> Result<(), Error> {
233+
) -> Result<Vec<AssignedCell<F, F>>, Error> {
234234
assert!(
235235
txs.len() <= max_txs,
236236
"txs.len() <= max_txs: txs.len()={}, max_txs={}",
@@ -251,14 +251,19 @@ impl TxTable {
251251
tag: &Column<Fixed>,
252252
row: &[Value<F>; 4],
253253
msg: &str,
254-
) -> Result<(), Error> {
254+
) -> Result<AssignedCell<F, F>, Error> {
255+
let mut value_cell = None;
255256
for (index, column) in advice_columns.iter().enumerate() {
256-
region.assign_advice(
257+
let cell = region.assign_advice(
257258
|| format!("tx table {msg} row {offset}"),
258259
*column,
259260
offset,
260261
|| row[if index > 0 { index + 1 } else { index }],
261262
)?;
263+
// tx_id, index, value
264+
if index == 2 {
265+
value_cell = Some(cell);
266+
}
262267
}
263268
region.assign_fixed(
264269
|| format!("tx table q_enable row {offset}"),
@@ -272,13 +277,14 @@ impl TxTable {
272277
offset,
273278
|| row[1],
274279
)?;
275-
Ok(())
280+
Ok(value_cell.unwrap())
276281
}
277282

278283
layouter.assign_region(
279284
|| "tx table",
280285
|mut region| {
281286
let mut offset = 0;
287+
let mut tx_value_cells = vec![];
282288
let advice_columns = [self.tx_id, self.index, self.value];
283289
assign_row(
284290
&mut region,
@@ -312,15 +318,15 @@ impl TxTable {
312318
let tx_data = tx.table_assignments_fixed(*challenges);
313319
let tx_calldata = tx.table_assignments_dyn(*challenges);
314320
for row in tx_data {
315-
assign_row(
321+
tx_value_cells.push(assign_row(
316322
&mut region,
317323
offset,
318324
self.q_enable,
319325
&advice_columns,
320326
&self.tag,
321327
&row,
322328
"",
323-
)?;
329+
)?);
324330
offset += 1;
325331
}
326332
calldata_assignments.extend(tx_calldata.iter());
@@ -338,7 +344,7 @@ impl TxTable {
338344
)?;
339345
offset += 1;
340346
}
341-
Ok(())
347+
Ok(tx_value_cells)
342348
},
343349
)
344350
}

0 commit comments

Comments
 (0)