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

Commit

Permalink
skip rw memory lookup when copy lookup happens
Browse files Browse the repository at this point in the history
  • Loading branch information
KimiWu123 committed Jun 19, 2023
1 parent 8867ee0 commit b875da0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
21 changes: 19 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1357,14 +1357,26 @@ impl<F: Field> ExecutionConfig<F> {
// challenges not ready
return;
}

let mut copy_lookup_included = false;
let mut assigned_rw_values = Vec::new();
for (name, v) in assigned_stored_expressions {
// println!("{:?}", name.clone());
if name.starts_with("rw lookup ")
&& !v.is_zero_vartime()
&& !assigned_rw_values.contains(&(name.clone(), *v))
{
assigned_rw_values.push((name.clone(), *v));
}
// If any `copy lookup` with Memory tag in opcode execution,
// block.get_rws() contains memory operations but
// assigned_stored_expressions only has a `copy lookup` expression without
// any rw lookup with Memory tag.
// So, we need to filter out Memory operation in get_rws_for_check_rw_lookup
// if having any `copy lookup` happens.
if name.starts_with("copy lookup") {
copy_lookup_included = true;
}
}

let rlc_assignments: BTreeSet<_> = (0..step.rw_indices_len())
Expand Down Expand Up @@ -1398,6 +1410,7 @@ impl<F: Field> ExecutionConfig<F> {
);
}
let mut rev_count = 0;
let mut acc_offset = 0;
for (idx, assigned_rw_value) in assigned_rw_values.iter().enumerate() {
let is_rev = if assigned_rw_value.0.contains(" with reversion") {
rev_count += 1;
Expand All @@ -1411,15 +1424,19 @@ impl<F: Field> ExecutionConfig<F> {
rev_count,
step.reversible_write_counter_delta
);

// In the EVM Circuit, reversion rw lookups are assigned after their
// corresponding rw lookup, but in the bus-mapping they are
// generated at the end of the step.
let idx = if is_rev {
step.rw_indices_len() - rev_count
} else {
idx - rev_count
idx - rev_count + acc_offset
};
let rw = block.get_rws(step, idx);
let (rw, offset) = block.get_rws_for_check_rw_lookup(step, idx, copy_lookup_included);
// let rw = block.get_rws(step, idx);
// let offset = 0;
acc_offset += offset;
let table_assignments = rw.table_assignment_aux(evm_randomness);
let rlc = table_assignments.rlc(lookup_randomness);
if rlc != assigned_rw_value.1 {
Expand Down
30 changes: 30 additions & 0 deletions zkevm-circuits/src/witness/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
};
use bus_mapping::{
circuit_input_builder::{self, CircuitsParams, CopyEvent, ExpEvent},
operation::Target,
Error,
};
use eth_types::{Address, Field, ToLittleEndian, ToScalar, Word};
Expand Down Expand Up @@ -73,6 +74,35 @@ impl<F: Field> Block<F> {
self.rws[step.rw_index(index)]
}

/// Get a read-write record for `check_rw_lookup`
pub(crate) fn get_rws_for_check_rw_lookup(
&self,
step: &ExecStep,
index: usize,
skip_memory_rw: bool,
) -> (Rw, usize) {
if skip_memory_rw == false {
(self.get_rws(step, index), 0)
} else {
let mut idx = index;
let mut rw_index = step.rw_index(idx);
loop {
if idx == step.rw_indices_len() - 1 {
idx = index;
rw_index = step.rw_index(idx);
break;
}
if rw_index.0 == Target::Memory {
idx += 1;
rw_index = step.rw_index(idx);
} else {
break;
}
}
(self.rws[rw_index], idx - index)
}
}

/// Obtains the expected Circuit degree needed in order to be able to test
/// the EvmCircuit with this block without needing to configure the
/// `ConstraintSystem`.
Expand Down

0 comments on commit b875da0

Please sign in to comment.