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

Commit 16dad5e

Browse files
committed
[wip] testing intermediate chunk
1 parent 8aaaa0d commit 16dad5e

File tree

12 files changed

+271
-213
lines changed

12 files changed

+271
-213
lines changed

bus-mapping/src/circuit_input_builder.rs

+41-8
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,15 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
357357
// Optain the first op of the next GethExecStep, for fixed case also lookahead
358358
let (mut cib, mut tx, mut tx_ctx) = (self.clone(), tx, tx_ctx);
359359
let mut cib_ref = cib.state_ref(&mut tx, &mut tx_ctx);
360+
println!("aa before self.block_ctx.rwc.0 {}", self.block_ctx.rwc.0);
360361
let next_ops = if let Some((i, step)) = next_geth_step {
361362
log::trace!("chunk at {}th opcode {:?} ", i, step.op);
362363
gen_associated_ops(&step.op, &mut cib_ref, &geth_trace.struct_logs[i..])?.remove(0)
363364
} else {
364365
log::trace!("chunk at EndTx");
365366
gen_associated_steps(&mut cib_ref, ExecState::EndTx)?
366367
};
368+
println!("aa before self.block_ctx.rwc.0 {}", self.block_ctx.rwc.0);
367369

368370
let last_copy = self.block.copy_events.len();
369371
// Generate EndChunk and proceed to the next if it's not the last chunk
@@ -421,6 +423,12 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
421423
step.op,
422424
self.chunk_rws()
423425
);
426+
println!(
427+
"handle {}th opcode {:?} rws = {:?}",
428+
i,
429+
step.op,
430+
self.chunk_rws()
431+
);
424432
let exec_steps = gen_associated_ops(
425433
&step.op,
426434
&mut self.state_ref(&mut tx, &mut tx_ctx),
@@ -431,7 +439,8 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
431439
.map(|step| tx.calls().get(step.call_index).unwrap().clone());
432440
tx.steps_mut().extend(exec_steps);
433441
}
434-
442+
println!("before end_tx rws = {:?}", self.chunk_rws());
443+
println!("before end_tx block rws = {:?}", self.block_ctx.rwc.0);
435444
// Peek the end_tx_step
436445
self.check_and_chunk(
437446
geth_trace,
@@ -445,6 +454,8 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
445454
let end_tx_step =
446455
gen_associated_steps(&mut self.state_ref(&mut tx, &mut tx_ctx), ExecState::EndTx)?;
447456
tx.steps_mut().push(end_tx_step.clone());
457+
println!("after end_tx rws = {:?}", self.chunk_rws());
458+
println!("after end_tx block rws = {:?}", self.block_ctx.rwc.0);
448459
(end_tx_step, last_call)
449460
} else if self.feature_config.invalid_tx {
450461
// chunk before hands to avoid chunk between [InvalidTx, BeginTx)
@@ -466,8 +477,7 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
466477
Ok(res)
467478
}
468479

469-
// TODO Fix this, for current logic on processing `call` is incorrect
470-
// TODO re-design `gen_chunk_associated_steps` to separate RW
480+
// generate chunk related steps
471481
fn gen_chunk_associated_steps(&mut self, step: &mut ExecStep, rw: RW) {
472482
let STEP_STATE_LEN = 10;
473483
let mut dummy_tx = Transaction::default();
@@ -560,15 +570,20 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
560570
fn set_begin_chunk(&mut self, first_step: &ExecStep) {
561571
let mut begin_chunk = first_step.clone();
562572
begin_chunk.exec_state = ExecState::BeginChunk;
573+
begin_chunk.bus_mapping_instance = Vec::new();
563574
self.gen_chunk_associated_steps(&mut begin_chunk, RW::READ);
564575
self.chunks[self.chunk_ctx.idx].begin_chunk = Some(begin_chunk);
565576
}
566577

567578
fn set_end_chunk(&mut self, next_step: &ExecStep) {
579+
println!("before self.block_ctx.rwc.0 {}", self.block_ctx.rwc.0);
580+
println!("next step {:?}", next_step);
568581
let mut end_chunk = next_step.clone();
569582
end_chunk.exec_state = ExecState::EndChunk;
583+
end_chunk.bus_mapping_instance = Vec::new();
570584
self.gen_chunk_associated_steps(&mut end_chunk, RW::WRITE);
571585
self.gen_chunk_padding(&mut end_chunk);
586+
println!("after self.block_ctx.rwc.0 {}", self.block_ctx.rwc.0);
572587
self.chunks[self.chunk_ctx.idx].end_chunk = Some(end_chunk);
573588
}
574589

@@ -710,7 +725,11 @@ impl CircuitInputBuilder<FixedCParams> {
710725
println!("--------------{:?}", self.circuits_params);
711726
// accumulates gas across all txs in the block
712727
let (last_step, last_call) = self.begin_handle_block(eth_block, geth_traces)?;
713-
let last_step = last_step.unwrap_or_default();
728+
let mut dummy_next_step = last_step.unwrap_or_default();
729+
(0..dummy_next_step.rw_indices_len()).for_each(|_| {
730+
dummy_next_step.rwc.inc_pre();
731+
dummy_next_step.rwc_inner_chunk.inc_pre();
732+
});
714733

715734
assert!(self.circuits_params.max_rws().is_some());
716735

@@ -734,16 +753,29 @@ impl CircuitInputBuilder<FixedCParams> {
734753
last_call.clone(),
735754
);
736755
} else {
737-
// it doent matter what step was put to set_end_chunk/set_begin_chunk on no-used
738-
// chunks before end_block. Just need to make sure it's step lookup is consistency.
739-
self.set_end_chunk(&last_step);
756+
// since there is no next step, we cook dummy next step from last step to reuse
757+
// existing field while update its `rwc`.
758+
println!("for end_chunk dummy_next_step {:?}", dummy_next_step);
759+
self.set_end_chunk(&dummy_next_step);
760+
dummy_next_step.rwc = self.block_ctx.rwc;
740761
self.commit_chunk_ctx(
741762
true,
742763
eth_block.transactions.len(),
743764
last_copy,
744765
last_call.clone(),
745766
);
746-
self.set_begin_chunk(&last_step);
767+
dummy_next_step.rwc_inner_chunk = self.chunk_ctx.rwc;
768+
println!("for begin_chunk dummy_next_step {:?}", dummy_next_step);
769+
self.set_begin_chunk(&dummy_next_step);
770+
dummy_next_step.rwc = self.block_ctx.rwc;
771+
dummy_next_step.rwc_inner_chunk = self.chunk_ctx.rwc;
772+
println!("for end_block dummy_next_step {:?}", dummy_next_step);
773+
self.block.end_block = dummy_next_step.clone();
774+
self.cur_chunk_mut().padding = {
775+
let mut padding = dummy_next_step.clone();
776+
padding.exec_state = ExecState::Padding;
777+
Some(padding)
778+
};
747779
}
748780
Ok::<(), Error>(())
749781
})?;
@@ -773,6 +805,7 @@ impl CircuitInputBuilder<FixedCParams> {
773805
fn set_end_block(&mut self) -> Result<(), Error> {
774806
let mut end_block = self.block.end_block.clone();
775807
end_block.rwc = self.block_ctx.rwc;
808+
end_block.exec_state = ExecState::EndBlock;
776809
end_block.rwc_inner_chunk = self.chunk_ctx.rwc;
777810

778811
let mut dummy_tx = Transaction::default();

testool/src/statetest/executor.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,9 @@ pub fn run_test(
348348

349349
let block: Block<Fr> =
350350
zkevm_circuits::evm_circuit::witness::block_convert(&builder).unwrap();
351-
let chunk: Chunk<Fr> =
352-
zkevm_circuits::evm_circuit::witness::chunk_convert(&block, &builder)
353-
.unwrap()
354-
.remove(0);
355-
356-
CircuitTestBuilder::<1, 1>::new_from_block(block, chunk)
351+
let chunks: Vec<Chunk<Fr>> =
352+
zkevm_circuits::evm_circuit::witness::chunk_convert(&block, &builder).unwrap();
353+
CircuitTestBuilder::<1, 1>::new_from_block(block, chunks)
357354
.run_with_result()
358355
.map_err(|err| match err {
359356
CircuitTestError::VerificationFailed { reasons, .. } => {

zkevm-circuits/src/evm_circuit.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,9 @@ mod evm_circuit_stats {
638638
TestContext::<0, 0>::new(None, |_| {}, |_, _| {}, |b, _| b).unwrap(),
639639
)
640640
.block_modifier(Box::new(|_block, chunk| {
641-
chunk.fixed_param.max_evm_rows = (1 << 18) - 100
641+
chunk
642+
.iter_mut()
643+
.for_each(|chunk| chunk.fixed_param.max_evm_rows = (1 << 18) - 100);
642644
}))
643645
.run();
644646
}

zkevm-circuits/src/evm_circuit/execution.rs

+25
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,10 @@ impl<F: Field> ExecutionConfig<F> {
815815

816816
let step_curr_rw_counter = cb.curr.state.rw_counter.clone();
817817
let step_curr_rw_counter_offset = cb.rw_counter_offset();
818+
if execution_state == ExecutionState::BeginChunk {
819+
cb.debug_expression("step_curr_rw_counter.expr()", step_curr_rw_counter.expr());
820+
}
821+
818822
let debug_expressions = cb.debug_expressions.clone();
819823

820824
// Extract feature config here before cb is built.
@@ -1228,6 +1232,11 @@ impl<F: Field> ExecutionConfig<F> {
12281232
if next.is_none() {
12291233
break;
12301234
}
1235+
println!(
1236+
"in assignment. ExecStep {:?} rwc {:?} rwc inner {:?}",
1237+
cur.2.exec_state, cur.2.rwc, cur.2.rwc_inner_chunk
1238+
);
1239+
12311240
second_last_real_step = Some(cur);
12321241
// record offset of current step before assignment
12331242
second_last_real_step_offset = offset;
@@ -1252,6 +1261,7 @@ impl<F: Field> ExecutionConfig<F> {
12521261

12531262
// part3: assign end chunk or end block
12541263
if let Some(end_chunk) = &chunk.end_chunk {
1264+
println!("assigning end chunk");
12551265
debug_assert_eq!(ExecutionState::EndChunk.get_step_height(), 1);
12561266
offset = assign_padding_or_step(
12571267
(&dummy_tx, &last_call, end_chunk),
@@ -1263,6 +1273,7 @@ impl<F: Field> ExecutionConfig<F> {
12631273
next_step_after_real_step = Some(end_chunk.clone());
12641274
}
12651275
} else {
1276+
println!("assigning end block");
12661277
assert!(
12671278
chunk.chunk_context.is_last_chunk(),
12681279
"If not end_chunk, must be end_block at last chunk"
@@ -1421,6 +1432,13 @@ impl<F: Field> ExecutionConfig<F> {
14211432
step,
14221433
call
14231434
);
1435+
println!(
1436+
"assign_exec_step offset: {} state {:?} step: {:?} call: {:?}",
1437+
offset,
1438+
step.execution_state(),
1439+
step,
1440+
call
1441+
);
14241442
}
14251443
// Make the region large enough for the current step and the next step.
14261444
// The next step's next step may also be accessed, so make the region large
@@ -1438,6 +1456,13 @@ impl<F: Field> ExecutionConfig<F> {
14381456
// so their witness values need to be known to be able
14391457
// to correctly calculate the intermediate value.
14401458
if let Some(next_step) = next_step {
1459+
println!(
1460+
"assign_exec_step nextstep offset: {} state {:?} step: {:?} call: {:?}",
1461+
offset,
1462+
next_step.2.execution_state(),
1463+
next_step.2,
1464+
next_step.1
1465+
);
14411466
self.assign_exec_step_int(
14421467
region,
14431468
offset + height,

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
evm_circuit::{
55
step::ExecutionState,
66
util::{
7-
constraint_builder::{EVMConstraintBuilder, StepStateTransition},
7+
constraint_builder::{EVMConstraintBuilder, StepStateTransition, Transition::Delta},
88
CachedRegion,
99
},
1010
witness::{Block, Call, Chunk, ExecStep, Transaction},
@@ -29,7 +29,10 @@ impl<F: Field> ExecutionGadget<F> for BeginChunkGadget<F> {
2929
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
3030
// state lookup
3131
cb.step_state_lookup(0.expr());
32-
let step_state_transition = StepStateTransition::same();
32+
let step_state_transition = StepStateTransition {
33+
rw_counter: Delta(cb.rw_counter_offset()),
34+
..StepStateTransition::same()
35+
};
3336
cb.require_step_state_transition(step_state_transition);
3437
Self {
3538
_marker: PhantomData {},

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ mod test {
174174
// finish required tests using this witness block
175175
CircuitTestBuilder::<2, 1>::new_from_test_ctx(ctx)
176176
.block_modifier(Box::new(move |_block, chunk| {
177-
chunk.fixed_param.max_evm_rows = evm_circuit_pad_to
177+
chunk
178+
.iter_mut()
179+
.for_each(|chunk| chunk.fixed_param.max_evm_rows = evm_circuit_pad_to);
178180
}))
179181
.run();
180182
}

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

+15-13
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ impl<F: Field> ExecutionGadget<F> for EndChunkGadget<F> {
6060
_: &Call,
6161
step: &ExecStep,
6262
) -> Result<(), Error> {
63+
let rwc_before_padding = step.bus_mapping_instance.len()
64+
- 1 // start op
65+
- 2; // 2 padding op
6366
self.rw_table_padding_gadget.assign_exec_step(
6467
region,
6568
offset,
6669
block,
6770
chunk,
68-
(step.rwc_inner_chunk.0 - 1 + step.bus_mapping_instance.len()) as u64,
71+
(step.rwc_inner_chunk.0 - 1 + rwc_before_padding) as u64,
6972
step,
7073
)?;
7174
Ok(())
@@ -167,18 +170,17 @@ mod test {
167170
|block, _tx| block.number(0xcafeu64),
168171
)
169172
.unwrap();
170-
(0..6).for_each(|chunk_id| {
171-
CircuitTestBuilder::new_from_test_ctx(test_ctx.clone())
172-
.params({
173-
FixedCParams {
174-
total_chunks: 6,
175-
max_rws: 90,
176-
max_txs: 2,
177-
..Default::default()
178-
}
179-
})
180-
.run_chunk(chunk_id);
181-
});
173+
CircuitTestBuilder::new_from_test_ctx(test_ctx.clone())
174+
.params({
175+
FixedCParams {
176+
total_chunks: 2,
177+
max_rws: 180,
178+
max_txs: 2,
179+
..Default::default()
180+
}
181+
})
182+
.run_multiple_chunks_with_result(Some(2))
183+
.unwrap();
182184
}
183185

184186
#[test]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ mod test {
453453
max_txs: 5,
454454
..Default::default()
455455
})
456-
.build_block(0, 1)
456+
.build_block(None)
457457
.unwrap();
458458

459459
block.rws.0[&Target::CallContext]

zkevm-circuits/src/evm_circuit/step.rs

+5
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,11 @@ impl<F: Field> Step<F> {
850850
offset,
851851
Value::known(F::from(step.rwc_inner_chunk.into())),
852852
)?;
853+
println!(
854+
"execstate {:?} self.state.call_id {:?}",
855+
step.execution_state(),
856+
F::from(call.call_id as u64)
857+
);
853858
self.state
854859
.call_id
855860
.assign(region, offset, Value::known(F::from(call.call_id as u64)))?;

zkevm-circuits/src/evm_circuit/util/common_gadget.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,7 @@ impl<F: Field> RwTablePaddingGadget<F> {
13361336
1.expr(),
13371337
max_rws.expr() - inner_rws_before_padding.expr(),
13381338
);
1339+
13391340
cb.condition(is_end_padding_exist.expr(), |cb| {
13401341
cb.rw_table_padding_lookup(inner_rws_before_padding.expr() + 1.expr());
13411342
cb.rw_table_padding_lookup(max_rws.expr() - 1.expr());

0 commit comments

Comments
 (0)