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

Commit

Permalink
[wip] testing intermediate chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Feb 28, 2024
1 parent 8aaaa0d commit 16dad5e
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 213 deletions.
49 changes: 41 additions & 8 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,15 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
// Optain the first op of the next GethExecStep, for fixed case also lookahead
let (mut cib, mut tx, mut tx_ctx) = (self.clone(), tx, tx_ctx);
let mut cib_ref = cib.state_ref(&mut tx, &mut tx_ctx);
println!("aa before self.block_ctx.rwc.0 {}", self.block_ctx.rwc.0);
let next_ops = if let Some((i, step)) = next_geth_step {
log::trace!("chunk at {}th opcode {:?} ", i, step.op);
gen_associated_ops(&step.op, &mut cib_ref, &geth_trace.struct_logs[i..])?.remove(0)
} else {
log::trace!("chunk at EndTx");
gen_associated_steps(&mut cib_ref, ExecState::EndTx)?
};
println!("aa before self.block_ctx.rwc.0 {}", self.block_ctx.rwc.0);

let last_copy = self.block.copy_events.len();
// Generate EndChunk and proceed to the next if it's not the last chunk
Expand Down Expand Up @@ -421,6 +423,12 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
step.op,
self.chunk_rws()
);
println!(
"handle {}th opcode {:?} rws = {:?}",
i,
step.op,
self.chunk_rws()
);
let exec_steps = gen_associated_ops(
&step.op,
&mut self.state_ref(&mut tx, &mut tx_ctx),
Expand All @@ -431,7 +439,8 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
.map(|step| tx.calls().get(step.call_index).unwrap().clone());
tx.steps_mut().extend(exec_steps);
}

println!("before end_tx rws = {:?}", self.chunk_rws());
println!("before end_tx block rws = {:?}", self.block_ctx.rwc.0);
// Peek the end_tx_step
self.check_and_chunk(
geth_trace,
Expand All @@ -445,6 +454,8 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
let end_tx_step =
gen_associated_steps(&mut self.state_ref(&mut tx, &mut tx_ctx), ExecState::EndTx)?;
tx.steps_mut().push(end_tx_step.clone());
println!("after end_tx rws = {:?}", self.chunk_rws());
println!("after end_tx block rws = {:?}", self.block_ctx.rwc.0);
(end_tx_step, last_call)
} else if self.feature_config.invalid_tx {
// chunk before hands to avoid chunk between [InvalidTx, BeginTx)
Expand All @@ -466,8 +477,7 @@ impl<'a, C: CircuitsParams> CircuitInputBuilder<C> {
Ok(res)
}

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

fn set_end_chunk(&mut self, next_step: &ExecStep) {
println!("before self.block_ctx.rwc.0 {}", self.block_ctx.rwc.0);
println!("next step {:?}", next_step);
let mut end_chunk = next_step.clone();
end_chunk.exec_state = ExecState::EndChunk;
end_chunk.bus_mapping_instance = Vec::new();
self.gen_chunk_associated_steps(&mut end_chunk, RW::WRITE);
self.gen_chunk_padding(&mut end_chunk);
println!("after self.block_ctx.rwc.0 {}", self.block_ctx.rwc.0);
self.chunks[self.chunk_ctx.idx].end_chunk = Some(end_chunk);
}

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

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

Expand All @@ -734,16 +753,29 @@ impl CircuitInputBuilder<FixedCParams> {
last_call.clone(),
);
} else {
// it doent matter what step was put to set_end_chunk/set_begin_chunk on no-used
// chunks before end_block. Just need to make sure it's step lookup is consistency.
self.set_end_chunk(&last_step);
// since there is no next step, we cook dummy next step from last step to reuse
// existing field while update its `rwc`.
println!("for end_chunk dummy_next_step {:?}", dummy_next_step);
self.set_end_chunk(&dummy_next_step);
dummy_next_step.rwc = self.block_ctx.rwc;
self.commit_chunk_ctx(
true,
eth_block.transactions.len(),
last_copy,
last_call.clone(),
);
self.set_begin_chunk(&last_step);
dummy_next_step.rwc_inner_chunk = self.chunk_ctx.rwc;
println!("for begin_chunk dummy_next_step {:?}", dummy_next_step);
self.set_begin_chunk(&dummy_next_step);
dummy_next_step.rwc = self.block_ctx.rwc;
dummy_next_step.rwc_inner_chunk = self.chunk_ctx.rwc;
println!("for end_block dummy_next_step {:?}", dummy_next_step);
self.block.end_block = dummy_next_step.clone();
self.cur_chunk_mut().padding = {
let mut padding = dummy_next_step.clone();
padding.exec_state = ExecState::Padding;
Some(padding)
};
}
Ok::<(), Error>(())
})?;
Expand Down Expand Up @@ -773,6 +805,7 @@ impl CircuitInputBuilder<FixedCParams> {
fn set_end_block(&mut self) -> Result<(), Error> {
let mut end_block = self.block.end_block.clone();
end_block.rwc = self.block_ctx.rwc;
end_block.exec_state = ExecState::EndBlock;
end_block.rwc_inner_chunk = self.chunk_ctx.rwc;

let mut dummy_tx = Transaction::default();
Expand Down
9 changes: 3 additions & 6 deletions testool/src/statetest/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,9 @@ pub fn run_test(

let block: Block<Fr> =
zkevm_circuits::evm_circuit::witness::block_convert(&builder).unwrap();
let chunk: Chunk<Fr> =
zkevm_circuits::evm_circuit::witness::chunk_convert(&block, &builder)
.unwrap()
.remove(0);

CircuitTestBuilder::<1, 1>::new_from_block(block, chunk)
let chunks: Vec<Chunk<Fr>> =
zkevm_circuits::evm_circuit::witness::chunk_convert(&block, &builder).unwrap();
CircuitTestBuilder::<1, 1>::new_from_block(block, chunks)
.run_with_result()
.map_err(|err| match err {
CircuitTestError::VerificationFailed { reasons, .. } => {
Expand Down
4 changes: 3 additions & 1 deletion zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,9 @@ mod evm_circuit_stats {
TestContext::<0, 0>::new(None, |_| {}, |_, _| {}, |b, _| b).unwrap(),
)
.block_modifier(Box::new(|_block, chunk| {
chunk.fixed_param.max_evm_rows = (1 << 18) - 100
chunk
.iter_mut()
.for_each(|chunk| chunk.fixed_param.max_evm_rows = (1 << 18) - 100);
}))
.run();
}
Expand Down
25 changes: 25 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,10 @@ impl<F: Field> ExecutionConfig<F> {

let step_curr_rw_counter = cb.curr.state.rw_counter.clone();
let step_curr_rw_counter_offset = cb.rw_counter_offset();
if execution_state == ExecutionState::BeginChunk {
cb.debug_expression("step_curr_rw_counter.expr()", step_curr_rw_counter.expr());
}

let debug_expressions = cb.debug_expressions.clone();

// Extract feature config here before cb is built.
Expand Down Expand Up @@ -1228,6 +1232,11 @@ impl<F: Field> ExecutionConfig<F> {
if next.is_none() {
break;
}
println!(
"in assignment. ExecStep {:?} rwc {:?} rwc inner {:?}",
cur.2.exec_state, cur.2.rwc, cur.2.rwc_inner_chunk
);

second_last_real_step = Some(cur);
// record offset of current step before assignment
second_last_real_step_offset = offset;
Expand All @@ -1252,6 +1261,7 @@ impl<F: Field> ExecutionConfig<F> {

// part3: assign end chunk or end block
if let Some(end_chunk) = &chunk.end_chunk {
println!("assigning end chunk");
debug_assert_eq!(ExecutionState::EndChunk.get_step_height(), 1);
offset = assign_padding_or_step(
(&dummy_tx, &last_call, end_chunk),
Expand All @@ -1263,6 +1273,7 @@ impl<F: Field> ExecutionConfig<F> {
next_step_after_real_step = Some(end_chunk.clone());
}
} else {
println!("assigning end block");
assert!(
chunk.chunk_context.is_last_chunk(),
"If not end_chunk, must be end_block at last chunk"
Expand Down Expand Up @@ -1421,6 +1432,13 @@ impl<F: Field> ExecutionConfig<F> {
step,
call
);
println!(
"assign_exec_step offset: {} state {:?} step: {:?} call: {:?}",
offset,
step.execution_state(),
step,
call
);
}
// Make the region large enough for the current step and the next step.
// The next step's next step may also be accessed, so make the region large
Expand All @@ -1438,6 +1456,13 @@ impl<F: Field> ExecutionConfig<F> {
// so their witness values need to be known to be able
// to correctly calculate the intermediate value.
if let Some(next_step) = next_step {
println!(
"assign_exec_step nextstep offset: {} state {:?} step: {:?} call: {:?}",
offset,
next_step.2.execution_state(),
next_step.2,
next_step.1
);
self.assign_exec_step_int(
region,
offset + height,
Expand Down
7 changes: 5 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/begin_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
evm_circuit::{
step::ExecutionState,
util::{
constraint_builder::{EVMConstraintBuilder, StepStateTransition},
constraint_builder::{EVMConstraintBuilder, StepStateTransition, Transition::Delta},
CachedRegion,
},
witness::{Block, Call, Chunk, ExecStep, Transaction},
Expand All @@ -29,7 +29,10 @@ impl<F: Field> ExecutionGadget<F> for BeginChunkGadget<F> {
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
// state lookup
cb.step_state_lookup(0.expr());
let step_state_transition = StepStateTransition::same();
let step_state_transition = StepStateTransition {
rw_counter: Delta(cb.rw_counter_offset()),
..StepStateTransition::same()
};
cb.require_step_state_transition(step_state_transition);
Self {
_marker: PhantomData {},
Expand Down
4 changes: 3 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/end_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ mod test {
// finish required tests using this witness block
CircuitTestBuilder::<2, 1>::new_from_test_ctx(ctx)
.block_modifier(Box::new(move |_block, chunk| {
chunk.fixed_param.max_evm_rows = evm_circuit_pad_to
chunk
.iter_mut()
.for_each(|chunk| chunk.fixed_param.max_evm_rows = evm_circuit_pad_to);
}))
.run();
}
Expand Down
28 changes: 15 additions & 13 deletions zkevm-circuits/src/evm_circuit/execution/end_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ impl<F: Field> ExecutionGadget<F> for EndChunkGadget<F> {
_: &Call,
step: &ExecStep,
) -> Result<(), Error> {
let rwc_before_padding = step.bus_mapping_instance.len()
- 1 // start op
- 2; // 2 padding op
self.rw_table_padding_gadget.assign_exec_step(
region,
offset,
block,
chunk,
(step.rwc_inner_chunk.0 - 1 + step.bus_mapping_instance.len()) as u64,
(step.rwc_inner_chunk.0 - 1 + rwc_before_padding) as u64,
step,
)?;
Ok(())
Expand Down Expand Up @@ -167,18 +170,17 @@ mod test {
|block, _tx| block.number(0xcafeu64),
)
.unwrap();
(0..6).for_each(|chunk_id| {
CircuitTestBuilder::new_from_test_ctx(test_ctx.clone())
.params({
FixedCParams {
total_chunks: 6,
max_rws: 90,
max_txs: 2,
..Default::default()
}
})
.run_chunk(chunk_id);
});
CircuitTestBuilder::new_from_test_ctx(test_ctx.clone())
.params({
FixedCParams {
total_chunks: 2,
max_rws: 180,
max_txs: 2,
..Default::default()
}
})
.run_multiple_chunks_with_result(Some(2))
.unwrap();
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ mod test {
max_txs: 5,
..Default::default()
})
.build_block(0, 1)
.build_block(None)
.unwrap();

block.rws.0[&Target::CallContext]
Expand Down
5 changes: 5 additions & 0 deletions zkevm-circuits/src/evm_circuit/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,11 @@ impl<F: Field> Step<F> {
offset,
Value::known(F::from(step.rwc_inner_chunk.into())),
)?;
println!(
"execstate {:?} self.state.call_id {:?}",
step.execution_state(),
F::from(call.call_id as u64)
);
self.state
.call_id
.assign(region, offset, Value::known(F::from(call.call_id as u64)))?;
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/evm_circuit/util/common_gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,7 @@ impl<F: Field> RwTablePaddingGadget<F> {
1.expr(),
max_rws.expr() - inner_rws_before_padding.expr(),
);

cb.condition(is_end_padding_exist.expr(), |cb| {
cb.rw_table_padding_lookup(inner_rws_before_padding.expr() + 1.expr());
cb.rw_table_padding_lookup(max_rws.expr() - 1.expr());
Expand Down
Loading

0 comments on commit 16dad5e

Please sign in to comment.