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

Commit

Permalink
allow zero limb diff in state_circuit lexicoordering
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Feb 23, 2024
1 parent 03d00ec commit cd6c843
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 217 deletions.
17 changes: 9 additions & 8 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ impl CircuitInputBuilder<FixedCParams> {
&mut self,
eth_block: &EthBlock,
geth_traces: &[eth_types::GethExecTrace],
) -> Result<(ExecStep, Option<Call>), Error> {
) -> Result<(Option<ExecStep>, Option<Call>), Error> {
assert!(
self.circuits_params.max_rws().unwrap_or_default() > self.rws_reserve(),
"Fixed max_rws not enough for rws reserve"
Expand All @@ -687,13 +687,17 @@ impl CircuitInputBuilder<FixedCParams> {
tx_id == eth_block.transactions.len(),
tx_id as u64,
)
.map(|(exec_step, last_call)| (Some(exec_step), last_call))
})
.collect::<Result<Vec<(ExecStep, Option<Call>)>, _>>()?;
let res = res.remove(res.len() - 1);
.collect::<Result<Vec<(Option<ExecStep>, Option<Call>)>, _>>()?;
// set eth_block
self.block.eth_block = eth_block.clone();
self.set_value_ops_call_context_rwc_eor();
Ok(res)
if !res.is_empty() {
Ok(res.remove(res.len() - 1))
} else {
Ok((None, None))
}
}

/// Handle a block by handling each transaction to generate all the
Expand All @@ -706,6 +710,7 @@ 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();

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

Expand Down Expand Up @@ -748,10 +753,6 @@ impl CircuitInputBuilder<FixedCParams> {
used_chunks <= self.circuits_params.total_chunks(),
"Used more chunks than given total_chunks"
);

self.chunks.iter().enumerate().for_each(|(id, chunk)| {
println!("chunk {}th ctx {:?}", id, chunk.ctx);
});
assert!(
self.chunks.len() == self.chunk_ctx.idx + 1,
"number of chunks {} mis-match with chunk_ctx id {}",
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl<F: Field> EvmCircuit<F> {
}

/// Compute the public inputs for this circuit.
fn instance_extend_chunk_ctx(&self) -> Vec<Vec<F>> {
pub fn instance_extend_chunk_ctx(&self) -> Vec<Vec<F>> {
let chunk = self.chunk.as_ref().unwrap();

let (rw_table_chunked_index, rw_table_total_chunks) =
Expand Down
48 changes: 9 additions & 39 deletions zkevm-circuits/src/evm_circuit/execution/end_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ mod test {
let builder = BlockData::new_from_geth_data_with_params(
block.clone(),
FixedCParams {
total_chunks: 6,
total_chunks: 4,
max_rws: 64,
max_txs: 2,
..Default::default()
Expand All @@ -128,43 +128,13 @@ mod test {
.unwrap();
let block = block_convert::<Fr>(&builder).unwrap();
let chunks = chunk_convert(&block, &builder).unwrap();
println!("num of chunk {:?}", chunks.len());
chunks.iter().enumerate().for_each(|(idx, chunk)| {
println!(
"{}th chunk by_address_rw_fingerprints {:?}, chrono_rw_fingerprints {:?} ",
idx, chunk.by_address_rw_fingerprints, chunk.chrono_rw_fingerprints,
);
});
}

#[test]
fn test_all_chunks_ok() {
let bytecode = bytecode! {
PUSH1(0x0) // retLength
PUSH1(0x0) // retOffset
PUSH1(0x0) // argsLength
PUSH1(0x0) // argsOffset
PUSH1(0x0) // value
PUSH32(0x10_0000) // addr
PUSH32(0x10_0000) // gas
CALL
PUSH2(0xaa)
};
CircuitTestBuilder::new_from_test_ctx(
TestContext::<2, 1>::simple_ctx_with_bytecode(bytecode).unwrap(),
)
.block_modifier(Box::new(move |_block, chunk| {
// TODO FIXME padding start as a workaround. The practical should be last chunk last row
// rws
// if let Some(a) = chunk.rws.0.get_mut(&Target::Start) {
// a.push(Rw::Start { rw_counter: 1 });
// }
println!(
"=> FIXME is fixed? {:?}",
chunk.chrono_rws.0.get_mut(&Target::Start)
);
}))
.run_dynamic_chunk(4, 2);
// assert last fingerprint acc are equal
if let Some(last_chunk) = chunks.last() {
assert_eq!(
last_chunk.by_address_rw_fingerprints.mul_acc,
last_chunk.chrono_rw_fingerprints.mul_acc
)
}
}

#[test]
Expand Down Expand Up @@ -202,7 +172,7 @@ mod test {
.params({
FixedCParams {
total_chunks: 6,
max_rws: 64,
max_rws: 90,
max_txs: 2,
..Default::default()
}
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/state_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ impl<F: Field> StateCircuit<F> {
permu_alpha: chunk.permu_alpha,
permu_gamma: chunk.permu_gamma,
rw_fingerprints: chunk.by_address_rw_fingerprints.clone(),
prev_chunk_last_rw: chunk.prev_chunk_last_chrono_rw,
prev_chunk_last_rw: chunk.prev_chunk_last_by_address_rw,
_marker: PhantomData::default(),
}
}
Expand Down
27 changes: 10 additions & 17 deletions zkevm-circuits/src/state_circuit/lexicographic_ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub struct Config {
pub(crate) selector: Column<Fixed>,
pub first_different_limb: BinaryNumberConfig<LimbIndex, 5>,
limb_difference: Column<Advice>,
limb_difference_inverse: Column<Advice>,
// limb_difference_inverse: Column<Advice>,
}

impl Config {
Expand All @@ -112,26 +112,26 @@ impl Config {
let selector = meta.fixed_column();
let first_different_limb = BinaryNumberChip::configure(meta, selector, None);
let limb_difference = meta.advice_column();
let limb_difference_inverse = meta.advice_column();
// let limb_difference_inverse = meta.advice_column();

let config = Config {
selector,
first_different_limb,
limb_difference,
limb_difference_inverse,
// limb_difference_inverse,
};

lookup.range_check_u16(meta, "limb_difference fits into u16", |meta| {
meta.query_advice(limb_difference, Rotation::cur())
});

meta.create_gate("limb_difference is not zero", |meta| {
let selector = meta.query_fixed(selector, Rotation::cur());
let limb_difference = meta.query_advice(limb_difference, Rotation::cur());
let limb_difference_inverse =
meta.query_advice(limb_difference_inverse, Rotation::cur());
vec![selector * (1.expr() - limb_difference * limb_difference_inverse)]
});
// meta.create_gate("limb_difference is not zero", |meta| {
// let selector = meta.query_fixed(selector, Rotation::cur());
// let limb_difference = meta.query_advice(limb_difference, Rotation::cur());
// let limb_difference_inverse =
// meta.query_advice(limb_difference_inverse, Rotation::cur());
// vec![selector * (1.expr() - limb_difference * limb_difference_inverse)]
// });

meta.create_gate(
"limb differences before first_different_limb are all 0",
Expand Down Expand Up @@ -221,12 +221,6 @@ impl Config {
offset,
|| Value::known(limb_difference),
)?;
region.assign_advice(
|| "limb_difference_inverse",
self.limb_difference_inverse,
offset,
|| Value::known(limb_difference.invert().unwrap()),
)?;

Ok(index)
}
Expand All @@ -235,7 +229,6 @@ impl Config {
pub fn annotate_columns_in_region<F: Field>(&self, region: &mut Region<F>, prefix: &str) {
[
(self.limb_difference, "LO_limb_difference"),
(self.limb_difference_inverse, "LO_limb_difference_inverse"),
]
.iter()
.for_each(|(col, ann)| region.name_column(|| format!("{}_{}", prefix, ann), *col));
Expand Down
33 changes: 24 additions & 9 deletions zkevm-circuits/src/state_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ fn state_circuit_unusable_rows() {
)
}

fn new_chunk_from_rw_map<F: Field>(rws: &RwMap, padding_start_rw: Option<Rw>) -> Chunk<F> {
let (alpha, gamma) = get_permutation_randomness();
let mut chunk = Chunk {
by_address_rws: rws.clone(),
..Default::default()
};

let rw_fingerprints = get_permutation_fingerprint_of_rwmap(
&chunk.by_address_rws,
chunk.fixed_param.max_rws,
alpha,
gamma,
F::from(1),
false,
padding_start_rw,
);
chunk.by_address_rw_fingerprints = rw_fingerprints;
chunk
}

fn test_state_circuit_ok(
memory_ops: Vec<Operation<MemoryOp>>,
stack_ops: Vec<Operation<StackOp>>,
Expand All @@ -45,7 +65,7 @@ fn test_state_circuit_ok(
storage: storage_ops,
..Default::default()
});
let chunk = Chunk::new_from_rw_map(&rw_map, None, None);
let chunk = new_chunk_from_rw_map(&rw_map, None);

let circuit = StateCircuit::<Fr>::new(&chunk);
let instance = circuit.instance();
Expand All @@ -69,7 +89,7 @@ fn verifying_key_independent_of_rw_length() {

let no_rows = StateCircuit::<Fr>::new(&chunk);

chunk = Chunk::new_from_rw_map(
chunk = new_chunk_from_rw_map(
&RwMap::from(&OperationContainer {
memory: vec![Operation::new(
RWCounter::from(1),
Expand All @@ -80,7 +100,6 @@ fn verifying_key_independent_of_rw_length() {
..Default::default()
}),
None,
None,
);
let one_row = StateCircuit::<Fr>::new(&chunk);

Expand Down Expand Up @@ -948,11 +967,7 @@ fn variadic_size_check() {
},
];
// let rw_map: RwMap = rows.clone().into();
let circuit = StateCircuit::new(&Chunk::new_from_rw_map(
&RwMap::from(rows.clone()),
None,
None,
));
let circuit = StateCircuit::new(&new_chunk_from_rw_map(&RwMap::from(rows.clone()), None));
let power_of_randomness = circuit.instance();
let prover1 = MockProver::<Fr>::run(17, &circuit, power_of_randomness).unwrap();

Expand All @@ -973,7 +988,7 @@ fn variadic_size_check() {
},
]);

let circuit = StateCircuit::new(&Chunk::new_from_rw_map(&rows.into(), None, None));
let circuit = StateCircuit::new(&new_chunk_from_rw_map(&rows.into(), None));
let power_of_randomness = circuit.instance();
let prover2 = MockProver::<Fr>::run(17, &circuit, power_of_randomness).unwrap();

Expand Down
24 changes: 15 additions & 9 deletions zkevm-circuits/src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ const NUM_BLINDING_ROWS: usize = 64;
///
/// CircuitTestBuilder::new_from_test_ctx(ctx)
/// .block_modifier(Box::new(|block, chunk| chunk.fixed_param.max_evm_rows = (1 << 18) - 100))
/// .state_checks(Box::new(|prover, evm_rows, lookup_rows| assert!(prover.verify_at_rows_par(evm_rows.iter().cloned(), lookup_rows.iter().cloned()).is_err())))
/// .run();
/// ```
pub struct CircuitTestBuilder<const NACC: usize, const NTX: usize> {
Expand Down Expand Up @@ -327,12 +326,14 @@ impl<const NACC: usize, const NTX: usize> CircuitTestBuilder<NACC, NTX> {
"Total chunks unmatched with fixed param"
);
BlockData::new_from_geth_data_with_params(block.clone(), fixed_param)
.new_circuit_input_builder()
.new_circuit_input_builder_with_feature(
self.feature_config.unwrap_or_default(),
)
.handle_block(&block.eth_block, &block.geth_traces)
.unwrap()
}
None => BlockData::new_from_geth_data_chunked(block.clone(), total_chunk)
.new_circuit_input_builder()
.new_circuit_input_builder_with_feature(self.feature_config.unwrap_or_default())
.handle_block(&block.eth_block, &block.geth_traces)
.unwrap(),
};
Expand All @@ -352,12 +353,11 @@ impl<const NACC: usize, const NTX: usize> CircuitTestBuilder<NACC, NTX> {

// Build a witness block from trace result.
let mut block = crate::witness::block_convert(&builder).unwrap();

let mut chunk = crate::witness::chunk_convert(&block, &builder)
.unwrap()
.remove(chunk_index);

println!("fingerprints = {:?}", chunk.chrono_rw_fingerprints);

for modifier_fn in self.block_modifiers {
modifier_fn.as_ref()(&mut block, &mut chunk);
}
Expand All @@ -374,10 +374,16 @@ impl<const NACC: usize, const NTX: usize> CircuitTestBuilder<NACC, NTX> {
let (_active_gate_rows, _active_lookup_rows) =
EvmCircuit::<Fr>::get_active_rows(&block, &chunk);

let circuit =
EvmCircuitCached::get_test_circuit_from_block(block.clone(), chunk.clone());
let instance = circuit.instance();
let _prover = MockProver::<Fr>::run(k, &circuit, instance).unwrap();
let _prover = if block.feature_config.is_mainnet() {
let circuit =
EvmCircuitCached::get_test_circuit_from_block(block.clone(), chunk.clone());
let instance = circuit.instance();
MockProver::<Fr>::run(k, &circuit, instance)
} else {
let circuit = EvmCircuit::get_test_circuit_from_block(block.clone(), chunk.clone());
let instance = circuit.instance_extend_chunk_ctx();
MockProver::<Fr>::run(k, &circuit, instance)
};

// self.evm_checks.as_ref()(prover, &active_gate_rows, &active_lookup_rows)
}
Expand Down
11 changes: 8 additions & 3 deletions zkevm-circuits/src/witness/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,14 @@ pub fn block_convert<F: Field>(
.chunks
.iter()
.fold(BTreeMap::new(), |mut map, chunk| {
assert!(chunk.ctx.rwc.0.saturating_sub(1) <= builder.circuits_params.max_rws);
// [chunk.ctx.rwc.0, builder.circuits_params.max_rws + 1)
(chunk.ctx.rwc.0..builder.circuits_params.max_rws + 1).for_each(|padding_rw_counter| {
assert!(
chunk.ctx.rwc.0.saturating_sub(1) <= builder.circuits_params.max_rws,
"max_rws size {} must larger than chunk rws size {}",
builder.circuits_params.max_rws,
chunk.ctx.rwc.0.saturating_sub(1),
);
// [chunk.ctx.rwc.0, builder.circuits_params.max_rws)
(chunk.ctx.rwc.0..builder.circuits_params.max_rws).for_each(|padding_rw_counter| {
*map.entry(padding_rw_counter).or_insert(0) += 1;
});
map
Expand Down
Loading

0 comments on commit cd6c843

Please sign in to comment.