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

Commit 0284a4c

Browse files
committed
rewrite logic to support both chronological/by address rw_table padding
1 parent 31bf030 commit 0284a4c

File tree

22 files changed

+228
-148
lines changed

22 files changed

+228
-148
lines changed

Diff for: bus-mapping/src/circuit_input_builder/chunk.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct ChunkContext {
2525
/// Index of current chunk, start from 0
2626
pub idx: usize,
2727
/// Used to track the inner chunk counter in every operation in the chunk.
28+
/// it will be reset for every new chunk.
2829
/// Contains the next available value.
2930
pub rwc: RWCounter,
3031
/// Number of chunks

Diff for: circuit-benchmarks/src/copy_circuit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ mod tests {
156156
.handle_block(&block.eth_block, &block.geth_traces)
157157
.unwrap();
158158
let block = block_convert(&builder).unwrap();
159-
let chunk = chunk_convert(&builder, 0).unwrap();
159+
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);
160160
assert_eq!(block.copy_events.len(), copy_event_num);
161161
(block, chunk)
162162
}

Diff for: circuit-benchmarks/src/evm_circuit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ mod evm_circ_benches {
5454
.unwrap();
5555

5656
let block = block_convert(&builder).unwrap();
57-
let chunk = chunk_convert(&builder, 0).unwrap();
57+
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);
5858

5959
let circuit = TestEvmCircuit::<Fr>::new(block, chunk);
6060
let mut rng = XorShiftRng::from_seed([

Diff for: circuit-benchmarks/src/exp_circuit.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ mod tests {
149149
.new_circuit_input_builder()
150150
.handle_block(&block.eth_block, &block.geth_traces)
151151
.unwrap();
152-
(
153-
block_convert(&builder).unwrap(),
154-
chunk_convert(&builder, 0).unwrap(),
155-
)
152+
let block = block_convert(&builder).unwrap();
153+
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);
154+
(block, chunk)
156155
}
157156
}

Diff for: integration-tests/src/integration_test_circuits.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ impl<C: SubCircuit<Fr> + Circuit<Fr>> IntegrationTest<C> {
424424
block_tag,
425425
);
426426
let mut block = block_convert(&builder).unwrap();
427-
let chunk = chunk_convert(&builder, 0).unwrap();
427+
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);
428428
block.randomness = Fr::from(TEST_MOCK_RANDOMNESS);
429429
let circuit = C::new_from_block(&block, &chunk);
430430
let instance = circuit.instance();
@@ -441,7 +441,7 @@ impl<C: SubCircuit<Fr> + Circuit<Fr>> IntegrationTest<C> {
441441
);
442442

443443
// get chronological_rwtable and byaddr_rwtable columns index
444-
let mut cs = ConstraintSystem::<<Bn256 as Engine>::Scalar>::default();
444+
let mut cs = ConstraintSystem::<<Bn256 as Engine>::Fr>::default();
445445
let config = SuperCircuit::configure(&mut cs);
446446
let rwtable_columns = config.get_rwtable_columns();
447447

@@ -515,10 +515,9 @@ fn new_empty_block_chunk() -> (Block<Fr>, Chunk<Fr>) {
515515
.new_circuit_input_builder()
516516
.handle_block(&block.eth_block, &block.geth_traces)
517517
.unwrap();
518-
(
519-
block_convert(&builder).unwrap(),
520-
chunk_convert(&builder, 0).unwrap(),
521-
)
518+
let block = block_convert(&builder).unwrap();
519+
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);
520+
(block, chunk)
522521
}
523522

524523
fn get_general_params(degree: u32) -> ParamsKZG<Bn256> {

Diff for: testool/src/statetest/executor.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@ use std::{collections::HashMap, str::FromStr};
1616
use thiserror::Error;
1717
use zkevm_circuits::{
1818
super_circuit::SuperCircuit,
19-
<<<<<<< HEAD
20-
test_util::CircuitTestBuilder,
21-
witness::{Block, Chunk},
22-
=======
2319
test_util::{CircuitTestBuilder, CircuitTestError},
24-
witness::Block,
25-
>>>>>>> main
20+
witness::{Block, Chunk},
2621
};
2722

2823
#[derive(PartialEq, Eq, Error, Debug)]
@@ -354,12 +349,11 @@ pub fn run_test(
354349
let block: Block<Fr> =
355350
zkevm_circuits::evm_circuit::witness::block_convert(&builder).unwrap();
356351
let chunk: Chunk<Fr> =
357-
zkevm_circuits::evm_circuit::witness::chunk_convert(&builder, 0).unwrap();
352+
zkevm_circuits::evm_circuit::witness::chunk_convert(&block, &builder)
353+
.unwrap()
354+
.remove(0);
358355

359-
<<<<<<< HEAD
360-
CircuitTestBuilder::<1, 1>::new_from_block(block, chunk).run();
361-
=======
362-
CircuitTestBuilder::<1, 1>::new_from_block(block)
356+
CircuitTestBuilder::<1, 1>::new_from_block(block, chunk)
363357
.run_with_result()
364358
.map_err(|err| match err {
365359
CircuitTestError::VerificationFailed { reasons, .. } => {
@@ -373,7 +367,6 @@ pub fn run_test(
373367
found: err.to_string(),
374368
},
375369
})?;
376-
>>>>>>> main
377370
} else {
378371
geth_data.sign(&wallets);
379372

Diff for: zkevm-circuits/src/copy_circuit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -859,8 +859,8 @@ impl<F: Field> SubCircuit<F> for CopyCircuit<F> {
859859
max_calldata: chunk.fixed_param.max_calldata,
860860
txs: block.txs.clone(),
861861
max_rws: chunk.fixed_param.max_rws,
862-
rws: chunk.rws.clone(),
863-
prev_chunk_last_rw: chunk.prev_chunk_last_rw,
862+
rws: chunk.chrono_rws.clone(),
863+
prev_chunk_last_rw: chunk.prev_chunk_last_chrono_rw,
864864
bytecodes: block.bytecodes.clone(),
865865
},
866866
)

Diff for: zkevm-circuits/src/copy_circuit/test.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ pub fn test_copy_circuit_from_block<F: Field>(
5858
max_calldata: chunk.fixed_param.max_calldata,
5959
txs: block.txs,
6060
max_rws: chunk.fixed_param.max_rws,
61-
rws: chunk.rws,
62-
prev_chunk_last_rw: chunk.prev_chunk_last_rw,
61+
rws: chunk.chrono_rws,
62+
prev_chunk_last_rw: chunk.prev_chunk_last_chrono_rw,
6363
bytecodes: block.bytecodes,
6464
},
6565
)
@@ -180,39 +180,39 @@ fn gen_tx_log_data() -> CircuitInputBuilder<FixedCParams> {
180180
fn copy_circuit_valid_calldatacopy() {
181181
let builder = gen_calldatacopy_data();
182182
let block = block_convert::<Fr>(&builder).unwrap();
183-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
183+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
184184
assert_eq!(test_copy_circuit_from_block(14, block, chunk), Ok(()));
185185
}
186186

187187
#[test]
188188
fn copy_circuit_valid_codecopy() {
189189
let builder = gen_codecopy_data();
190190
let block = block_convert::<Fr>(&builder).unwrap();
191-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
191+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
192192
assert_eq!(test_copy_circuit_from_block(10, block, chunk), Ok(()));
193193
}
194194

195195
#[test]
196196
fn copy_circuit_valid_extcodecopy() {
197197
let builder = gen_extcodecopy_data();
198198
let block = block_convert::<Fr>(&builder).unwrap();
199-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
199+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
200200
assert_eq!(test_copy_circuit_from_block(14, block, chunk), Ok(()));
201201
}
202202

203203
#[test]
204204
fn copy_circuit_valid_sha3() {
205205
let builder = gen_sha3_data();
206206
let block = block_convert::<Fr>(&builder).unwrap();
207-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
207+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
208208
assert_eq!(test_copy_circuit_from_block(14, block, chunk), Ok(()));
209209
}
210210

211211
#[test]
212212
fn copy_circuit_valid_tx_log() {
213213
let builder = gen_tx_log_data();
214214
let block = block_convert::<Fr>(&builder).unwrap();
215-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
215+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
216216
assert_eq!(test_copy_circuit_from_block(10, block, chunk), Ok(()));
217217
}
218218

@@ -225,7 +225,7 @@ fn copy_circuit_invalid_calldatacopy() {
225225
builder.block.copy_events[0].bytes[0].0.wrapping_add(1);
226226

227227
let block = block_convert::<Fr>(&builder).unwrap();
228-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
228+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
229229

230230
assert_error_matches(
231231
test_copy_circuit_from_block(14, block, chunk),
@@ -242,7 +242,7 @@ fn copy_circuit_invalid_codecopy() {
242242
builder.block.copy_events[0].bytes[0].0.wrapping_add(1);
243243

244244
let block = block_convert::<Fr>(&builder).unwrap();
245-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
245+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
246246

247247
assert_error_matches(
248248
test_copy_circuit_from_block(10, block, chunk),
@@ -259,7 +259,7 @@ fn copy_circuit_invalid_extcodecopy() {
259259
builder.block.copy_events[0].bytes[0].0.wrapping_add(1);
260260

261261
let block = block_convert::<Fr>(&builder).unwrap();
262-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
262+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
263263

264264
assert_error_matches(
265265
test_copy_circuit_from_block(14, block, chunk),
@@ -276,7 +276,7 @@ fn copy_circuit_invalid_sha3() {
276276
builder.block.copy_events[0].bytes[0].0.wrapping_add(1);
277277

278278
let block = block_convert::<Fr>(&builder).unwrap();
279-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
279+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
280280

281281
assert_error_matches(
282282
test_copy_circuit_from_block(14, block, chunk),
@@ -293,7 +293,7 @@ fn copy_circuit_invalid_tx_log() {
293293
builder.block.copy_events[0].bytes[0].0.wrapping_add(1);
294294

295295
let block = block_convert::<Fr>(&builder).unwrap();
296-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
296+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
297297

298298
assert_error_matches(
299299
test_copy_circuit_from_block(10, block, chunk),

Diff for: zkevm-circuits/src/evm_circuit.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,9 @@ impl<F: Field> SubCircuit<F> for EvmCircuit<F> {
334334
.assign_block(layouter, block, chunk, challenges)?;
335335

336336
let (rw_rows_padding, _) = RwMap::table_assignments_padding(
337-
&chunk.rws.table_assignments(true),
337+
&chunk.chrono_rws.table_assignments(true),
338338
chunk.fixed_param.max_rws,
339-
chunk.prev_chunk_last_rw,
339+
chunk.prev_chunk_last_chrono_rw,
340340
);
341341
let (
342342
alpha_cell,
@@ -353,10 +353,10 @@ impl<F: Field> SubCircuit<F> for EvmCircuit<F> {
353353
&mut region,
354354
// pass non-padding rws to `load_with_region` since it will be padding
355355
// inside
356-
&chunk.rws.table_assignments(true),
356+
&chunk.chrono_rws.table_assignments(true),
357357
// align with state circuit to padding to same max_rws
358358
chunk.fixed_param.max_rws,
359-
chunk.prev_chunk_last_rw,
359+
chunk.prev_chunk_last_chrono_rw,
360360
)?;
361361
let permutation_cells = config.rw_permutation_config.assign(
362362
&mut region,
@@ -565,7 +565,7 @@ impl<F: Field> Circuit<F> for EvmCircuit<F> {
565565
chunk.fixed_param.max_txs,
566566
chunk.fixed_param.max_calldata,
567567
)?;
568-
chunk.rws.check_rw_counter_sanity();
568+
chunk.chrono_rws.check_rw_counter_sanity();
569569
config
570570
.bytecode_table
571571
.load(&mut layouter, block.bytecodes.clone())?;
@@ -698,7 +698,7 @@ mod evm_circuit_stats {
698698
.handle_block(&block.eth_block, &block.geth_traces)
699699
.unwrap();
700700
let block = block_convert::<Fr>(&builder).unwrap();
701-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
701+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
702702
let k = block.get_test_degree(&chunk);
703703
let circuit = EvmCircuit::<Fr>::get_test_circuit_from_block(block, chunk);
704704
let instance = circuit.instance_extend_chunk_ctx();
@@ -723,7 +723,7 @@ mod evm_circuit_stats {
723723
.handle_block(&block.eth_block, &block.geth_traces)
724724
.unwrap();
725725
let block = block_convert::<Fr>(&builder).unwrap();
726-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
726+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
727727
let k = block.get_test_degree(&chunk);
728728

729729
let circuit = EvmCircuit::<Fr>::get_test_circuit_from_block(block, chunk);
@@ -746,7 +746,7 @@ mod evm_circuit_stats {
746746
.handle_block(&block.eth_block, &block.geth_traces)
747747
.unwrap();
748748
let block = block_convert::<Fr>(&builder).unwrap();
749-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
749+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
750750
let k = block.get_test_degree(&chunk);
751751
let circuit = EvmCircuit::<Fr>::get_test_circuit_from_block(block, chunk);
752752
let instance = circuit.instance_extend_chunk_ctx();

Diff for: zkevm-circuits/src/evm_circuit/execution/end_chunk.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ impl<F: Field> ExecutionGadget<F> for EndChunkGadget<F> {
2929
const EXECUTION_STATE: ExecutionState = ExecutionState::EndChunk;
3030

3131
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
32-
// State transition
32+
// State transition on non-last evm step
33+
// TODO/FIXME make EndChunk must be in last evm step and remove below constraint
3334
cb.not_step_last(|cb| {
3435
// Propagate all the way down.
3536
cb.require_step_state_transition(StepStateTransition::same());
@@ -102,7 +103,7 @@ mod test {
102103
// }
103104
println!(
104105
"=> FIXME is fixed? {:?}",
105-
chunk.rws.0.get_mut(&Target::Start)
106+
chunk.chrono_rws.0.get_mut(&Target::Start)
106107
);
107108
}))
108109
.run_dynamic_chunk(4, 2);

Diff for: zkevm-circuits/src/exp_circuit/test.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ fn test_ok(base: Word, exponent: Word, k: Option<u32>) {
6767
let code = gen_code_single(base, exponent);
6868
let builder = gen_data(code, false);
6969
let block = block_convert::<Fr>(&builder).unwrap();
70-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
70+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
7171
test_exp_circuit(k.unwrap_or(18), block, chunk);
7272
}
7373

7474
fn test_ok_multiple(args: Vec<(Word, Word)>) {
7575
let code = gen_code_multiple(args);
7676
let builder = gen_data(code, false);
7777
let block = block_convert::<Fr>(&builder).unwrap();
78-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
78+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
7979
test_exp_circuit(20, block, chunk);
8080
}
8181

@@ -125,7 +125,7 @@ fn variadic_size_check() {
125125
.handle_block(&block.eth_block, &block.geth_traces)
126126
.unwrap();
127127
let block = block_convert::<Fr>(&builder).unwrap();
128-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
128+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
129129
let circuit = ExpCircuit::<Fr>::new(block.exp_events, chunk.fixed_param.max_exp_steps);
130130
let prover1 = MockProver::<Fr>::run(k, &circuit, vec![]).unwrap();
131131

@@ -141,7 +141,7 @@ fn variadic_size_check() {
141141
};
142142
let builder = gen_data(code, true);
143143
let block = block_convert::<Fr>(&builder).unwrap();
144-
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
144+
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
145145
let circuit = ExpCircuit::<Fr>::new(block.exp_events, chunk.fixed_param.max_exp_steps);
146146
let prover2 = MockProver::<Fr>::run(k, &circuit, vec![]).unwrap();
147147

Diff for: zkevm-circuits/src/pi_circuit/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fn test_1tx_1maxtx() {
149149

150150
block.sign(&wallets);
151151
let block = block_convert(&builder).unwrap();
152-
let chunk = chunk_convert(&builder, 0).unwrap();
152+
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);
153153
// MAX_TXS, MAX_TXS align with `CircuitsParams`
154154
let circuit = PiCircuit::<Fr>::new_from_block(&block, &chunk);
155155
let public_inputs = circuit.instance();
@@ -230,7 +230,7 @@ fn test_1wd_1wdmax() {
230230
.unwrap();
231231

232232
let block = block_convert(&builder).unwrap();
233-
let chunk = chunk_convert(&builder, 0).unwrap();
233+
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);
234234
// MAX_TXS, MAX_TXS align with `CircuitsParams`
235235
let circuit = PiCircuit::<Fr>::new_from_block(&block, &chunk);
236236
let public_inputs = circuit.instance();

Diff for: zkevm-circuits/src/state_circuit.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ pub struct StateCircuit<F: Field> {
471471
impl<F: Field> StateCircuit<F> {
472472
/// make a new state circuit from an RwMap
473473
pub fn new(chunk: &Chunk<F>) -> Self {
474-
let rows = chunk.rws.table_assignments(false); // address sorted
474+
let rows = chunk.by_address_rws.table_assignments(false); // address sorted
475475
let updates = MptUpdates::mock_from(&rows);
476476
Self {
477477
rows,
@@ -483,8 +483,8 @@ impl<F: Field> StateCircuit<F> {
483483
overrides: HashMap::new(),
484484
permu_alpha: chunk.permu_alpha,
485485
permu_gamma: chunk.permu_gamma,
486-
rw_fingerprints: chunk.rw_fingerprints.clone(),
487-
prev_chunk_last_rw: chunk.prev_chunk_last_rw,
486+
rw_fingerprints: chunk.by_address_rw_fingerprints.clone(),
487+
prev_chunk_last_rw: chunk.prev_chunk_last_chrono_rw,
488488
_marker: PhantomData::default(),
489489
}
490490
}
@@ -506,7 +506,7 @@ impl<F: Field> SubCircuit<F> for StateCircuit<F> {
506506
/// Return the minimum number of rows required to prove the block
507507
fn min_num_rows_block(_block: &witness::Block<F>, chunk: &Chunk<F>) -> (usize, usize) {
508508
(
509-
chunk.rws.0.values().flatten().count() + 1,
509+
chunk.chrono_rws.0.values().flatten().count() + 1,
510510
chunk.fixed_param.max_rws,
511511
)
512512
}

Diff for: zkevm-circuits/src/state_circuit/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ fn prover(rows: Vec<Rw>, overrides: HashMap<(AdviceColumn, isize), Fr>) -> MockP
10121012
let rw_rows: Vec<witness::RwRow<Value<Fr>>> =
10131013
rw_overrides_skip_first_padding(&rw_rows, &overrides);
10141014
let rwtable_fingerprints =
1015-
get_permutation_fingerprint_of_rwrowvec(&rw_rows, N_ROWS, Fr::ONE, Fr::ONE, Fr::ONE);
1015+
get_permutation_fingerprint_of_rwrowvec(&rw_rows, N_ROWS, Fr::ONE, Fr::ONE, Fr::ONE, None);
10161016
let row_padding_and_overridess = rw_rows.to2dvec();
10171017

10181018
let updates = MptUpdates::mock_from(&rows);

0 commit comments

Comments
 (0)