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

Commit

Permalink
rewrite logic to support both chronological/by address rw_table padding
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Feb 21, 2024
1 parent 31bf030 commit ab43142
Show file tree
Hide file tree
Showing 22 changed files with 238 additions and 153 deletions.
3 changes: 2 additions & 1 deletion bus-mapping/src/circuit_input_builder/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct ChunkContext {
/// Index of current chunk, start from 0
pub idx: usize,
/// Used to track the inner chunk counter in every operation in the chunk.
/// it will be reset for every new chunk.
/// Contains the next available value.
pub rwc: RWCounter,
/// Number of chunks
Expand All @@ -41,7 +42,7 @@ pub struct ChunkContext {
pub initial_copy_index: usize,
///
pub end_copy_index: usize,
/// Druing dry run, chuncking is desabled
/// Druing dry run, chuncking is disabled
pub enable: bool,
}

Expand Down
2 changes: 1 addition & 1 deletion circuit-benchmarks/src/copy_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ mod tests {
.handle_block(&block.eth_block, &block.geth_traces)
.unwrap();
let block = block_convert(&builder).unwrap();
let chunk = chunk_convert(&builder, 0).unwrap();
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);
assert_eq!(block.copy_events.len(), copy_event_num);
(block, chunk)
}
Expand Down
2 changes: 1 addition & 1 deletion circuit-benchmarks/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod evm_circ_benches {
.unwrap();

let block = block_convert(&builder).unwrap();
let chunk = chunk_convert(&builder, 0).unwrap();
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);

let circuit = TestEvmCircuit::<Fr>::new(block, chunk);
let mut rng = XorShiftRng::from_seed([
Expand Down
7 changes: 3 additions & 4 deletions circuit-benchmarks/src/exp_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ mod tests {
.new_circuit_input_builder()
.handle_block(&block.eth_block, &block.geth_traces)
.unwrap();
(
block_convert(&builder).unwrap(),
chunk_convert(&builder, 0).unwrap(),
)
let block = block_convert(&builder).unwrap();
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);
(block, chunk)
}
}
11 changes: 5 additions & 6 deletions integration-tests/src/integration_test_circuits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ impl<C: SubCircuit<Fr> + Circuit<Fr>> IntegrationTest<C> {
block_tag,
);
let mut block = block_convert(&builder).unwrap();
let chunk = chunk_convert(&builder, 0).unwrap();
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);
block.randomness = Fr::from(TEST_MOCK_RANDOMNESS);
let circuit = C::new_from_block(&block, &chunk);
let instance = circuit.instance();
Expand All @@ -441,7 +441,7 @@ impl<C: SubCircuit<Fr> + Circuit<Fr>> IntegrationTest<C> {
);

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

Expand Down Expand Up @@ -515,10 +515,9 @@ fn new_empty_block_chunk() -> (Block<Fr>, Chunk<Fr>) {
.new_circuit_input_builder()
.handle_block(&block.eth_block, &block.geth_traces)
.unwrap();
(
block_convert(&builder).unwrap(),
chunk_convert(&builder, 0).unwrap(),
)
let block = block_convert(&builder).unwrap();
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);
(block, chunk)
}

fn get_general_params(degree: u32) -> ParamsKZG<Bn256> {
Expand Down
17 changes: 5 additions & 12 deletions testool/src/statetest/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ use std::{collections::HashMap, str::FromStr};
use thiserror::Error;
use zkevm_circuits::{
super_circuit::SuperCircuit,
<<<<<<< HEAD
test_util::CircuitTestBuilder,
witness::{Block, Chunk},
=======
test_util::{CircuitTestBuilder, CircuitTestError},
witness::Block,
>>>>>>> main
witness::{Block, Chunk},
};

#[derive(PartialEq, Eq, Error, Debug)]
Expand Down Expand Up @@ -354,12 +349,11 @@ 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(&builder, 0).unwrap();
zkevm_circuits::evm_circuit::witness::chunk_convert(&block, &builder)
.unwrap()
.remove(0);

<<<<<<< HEAD
CircuitTestBuilder::<1, 1>::new_from_block(block, chunk).run();
=======
CircuitTestBuilder::<1, 1>::new_from_block(block)
CircuitTestBuilder::<1, 1>::new_from_block(block, chunk)
.run_with_result()
.map_err(|err| match err {
CircuitTestError::VerificationFailed { reasons, .. } => {
Expand All @@ -373,7 +367,6 @@ pub fn run_test(
found: err.to_string(),
},
})?;
>>>>>>> main
} else {
geth_data.sign(&wallets);

Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/copy_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,8 @@ impl<F: Field> SubCircuit<F> for CopyCircuit<F> {
max_calldata: chunk.fixed_param.max_calldata,
txs: block.txs.clone(),
max_rws: chunk.fixed_param.max_rws,
rws: chunk.rws.clone(),
prev_chunk_last_rw: chunk.prev_chunk_last_rw,
rws: chunk.chrono_rws.clone(),
prev_chunk_last_rw: chunk.prev_chunk_last_chrono_rw,
bytecodes: block.bytecodes.clone(),
},
)
Expand Down
24 changes: 12 additions & 12 deletions zkevm-circuits/src/copy_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub fn test_copy_circuit_from_block<F: Field>(
max_calldata: chunk.fixed_param.max_calldata,
txs: block.txs,
max_rws: chunk.fixed_param.max_rws,
rws: chunk.rws,
prev_chunk_last_rw: chunk.prev_chunk_last_rw,
rws: chunk.chrono_rws,
prev_chunk_last_rw: chunk.prev_chunk_last_chrono_rw,
bytecodes: block.bytecodes,
},
)
Expand Down Expand Up @@ -180,39 +180,39 @@ fn gen_tx_log_data() -> CircuitInputBuilder<FixedCParams> {
fn copy_circuit_valid_calldatacopy() {
let builder = gen_calldatacopy_data();
let block = block_convert::<Fr>(&builder).unwrap();
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
assert_eq!(test_copy_circuit_from_block(14, block, chunk), Ok(()));
}

#[test]
fn copy_circuit_valid_codecopy() {
let builder = gen_codecopy_data();
let block = block_convert::<Fr>(&builder).unwrap();
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
assert_eq!(test_copy_circuit_from_block(10, block, chunk), Ok(()));
}

#[test]
fn copy_circuit_valid_extcodecopy() {
let builder = gen_extcodecopy_data();
let block = block_convert::<Fr>(&builder).unwrap();
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
assert_eq!(test_copy_circuit_from_block(14, block, chunk), Ok(()));
}

#[test]
fn copy_circuit_valid_sha3() {
let builder = gen_sha3_data();
let block = block_convert::<Fr>(&builder).unwrap();
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
assert_eq!(test_copy_circuit_from_block(14, block, chunk), Ok(()));
}

#[test]
fn copy_circuit_valid_tx_log() {
let builder = gen_tx_log_data();
let block = block_convert::<Fr>(&builder).unwrap();
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
assert_eq!(test_copy_circuit_from_block(10, block, chunk), Ok(()));
}

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

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

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

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

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

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

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

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

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

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

assert_error_matches(
test_copy_circuit_from_block(10, block, chunk),
Expand Down
16 changes: 8 additions & 8 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ impl<F: Field> SubCircuit<F> for EvmCircuit<F> {
.assign_block(layouter, block, chunk, challenges)?;

let (rw_rows_padding, _) = RwMap::table_assignments_padding(
&chunk.rws.table_assignments(true),
&chunk.chrono_rws.table_assignments(true),
chunk.fixed_param.max_rws,
chunk.prev_chunk_last_rw,
chunk.prev_chunk_last_chrono_rw,
);
let (
alpha_cell,
Expand All @@ -353,10 +353,10 @@ impl<F: Field> SubCircuit<F> for EvmCircuit<F> {
&mut region,
// pass non-padding rws to `load_with_region` since it will be padding
// inside
&chunk.rws.table_assignments(true),
&chunk.chrono_rws.table_assignments(true),
// align with state circuit to padding to same max_rws
chunk.fixed_param.max_rws,
chunk.prev_chunk_last_rw,
chunk.prev_chunk_last_chrono_rw,
)?;
let permutation_cells = config.rw_permutation_config.assign(
&mut region,
Expand Down Expand Up @@ -565,7 +565,7 @@ impl<F: Field> Circuit<F> for EvmCircuit<F> {
chunk.fixed_param.max_txs,
chunk.fixed_param.max_calldata,
)?;
chunk.rws.check_rw_counter_sanity();
chunk.chrono_rws.check_rw_counter_sanity();
config
.bytecode_table
.load(&mut layouter, block.bytecodes.clone())?;
Expand Down Expand Up @@ -698,7 +698,7 @@ mod evm_circuit_stats {
.handle_block(&block.eth_block, &block.geth_traces)
.unwrap();
let block = block_convert::<Fr>(&builder).unwrap();
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
let k = block.get_test_degree(&chunk);
let circuit = EvmCircuit::<Fr>::get_test_circuit_from_block(block, chunk);
let instance = circuit.instance_extend_chunk_ctx();
Expand All @@ -723,7 +723,7 @@ mod evm_circuit_stats {
.handle_block(&block.eth_block, &block.geth_traces)
.unwrap();
let block = block_convert::<Fr>(&builder).unwrap();
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
let k = block.get_test_degree(&chunk);

let circuit = EvmCircuit::<Fr>::get_test_circuit_from_block(block, chunk);
Expand All @@ -746,7 +746,7 @@ mod evm_circuit_stats {
.handle_block(&block.eth_block, &block.geth_traces)
.unwrap();
let block = block_convert::<Fr>(&builder).unwrap();
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
let k = block.get_test_degree(&chunk);
let circuit = EvmCircuit::<Fr>::get_test_circuit_from_block(block, chunk);
let instance = circuit.instance_extend_chunk_ctx();
Expand Down
5 changes: 3 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/end_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ impl<F: Field> ExecutionGadget<F> for EndChunkGadget<F> {
const EXECUTION_STATE: ExecutionState = ExecutionState::EndChunk;

fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
// State transition
// State transition on non-last evm step
// TODO/FIXME make EndChunk must be in last evm step and remove below constraint
cb.not_step_last(|cb| {
// Propagate all the way down.
cb.require_step_state_transition(StepStateTransition::same());
Expand Down Expand Up @@ -102,7 +103,7 @@ mod test {
// }
println!(
"=> FIXME is fixed? {:?}",
chunk.rws.0.get_mut(&Target::Start)
chunk.chrono_rws.0.get_mut(&Target::Start)
);
}))
.run_dynamic_chunk(4, 2);
Expand Down
8 changes: 4 additions & 4 deletions zkevm-circuits/src/exp_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ fn test_ok(base: Word, exponent: Word, k: Option<u32>) {
let code = gen_code_single(base, exponent);
let builder = gen_data(code, false);
let block = block_convert::<Fr>(&builder).unwrap();
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
test_exp_circuit(k.unwrap_or(18), block, chunk);
}

fn test_ok_multiple(args: Vec<(Word, Word)>) {
let code = gen_code_multiple(args);
let builder = gen_data(code, false);
let block = block_convert::<Fr>(&builder).unwrap();
let chunk = chunk_convert::<Fr>(&builder, 0).unwrap();
let chunk = chunk_convert::<Fr>(&block, &builder).unwrap().remove(0);
test_exp_circuit(20, block, chunk);
}

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

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

Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/pi_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn test_1tx_1maxtx() {

block.sign(&wallets);
let block = block_convert(&builder).unwrap();
let chunk = chunk_convert(&builder, 0).unwrap();
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);
// MAX_TXS, MAX_TXS align with `CircuitsParams`
let circuit = PiCircuit::<Fr>::new_from_block(&block, &chunk);
let public_inputs = circuit.instance();
Expand Down Expand Up @@ -230,7 +230,7 @@ fn test_1wd_1wdmax() {
.unwrap();

let block = block_convert(&builder).unwrap();
let chunk = chunk_convert(&builder, 0).unwrap();
let chunk = chunk_convert(&block, &builder).unwrap().remove(0);
// MAX_TXS, MAX_TXS align with `CircuitsParams`
let circuit = PiCircuit::<Fr>::new_from_block(&block, &chunk);
let public_inputs = circuit.instance();
Expand Down
8 changes: 4 additions & 4 deletions zkevm-circuits/src/state_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ pub struct StateCircuit<F: Field> {
impl<F: Field> StateCircuit<F> {
/// make a new state circuit from an RwMap
pub fn new(chunk: &Chunk<F>) -> Self {
let rows = chunk.rws.table_assignments(false); // address sorted
let rows = chunk.by_address_rws.table_assignments(false); // address sorted
let updates = MptUpdates::mock_from(&rows);
Self {
rows,
Expand All @@ -483,8 +483,8 @@ impl<F: Field> StateCircuit<F> {
overrides: HashMap::new(),
permu_alpha: chunk.permu_alpha,
permu_gamma: chunk.permu_gamma,
rw_fingerprints: chunk.rw_fingerprints.clone(),
prev_chunk_last_rw: chunk.prev_chunk_last_rw,
rw_fingerprints: chunk.by_address_rw_fingerprints.clone(),
prev_chunk_last_rw: chunk.prev_chunk_last_chrono_rw,
_marker: PhantomData::default(),
}
}
Expand All @@ -506,7 +506,7 @@ impl<F: Field> SubCircuit<F> for StateCircuit<F> {
/// Return the minimum number of rows required to prove the block
fn min_num_rows_block(_block: &witness::Block<F>, chunk: &Chunk<F>) -> (usize, usize) {
(
chunk.rws.0.values().flatten().count() + 1,
chunk.chrono_rws.0.values().flatten().count() + 1,
chunk.fixed_param.max_rws,
)
}
Expand Down
Loading

0 comments on commit ab43142

Please sign in to comment.