Skip to content

Commit

Permalink
[Feat]Induce copy constraint for phase2 cell (scroll-tech#492)
Browse files Browse the repository at this point in the history
* induce phase2 copy cell

* fmt

---------

Co-authored-by: ho <[email protected]>
  • Loading branch information
noel2004 and ho authored Apr 26, 2023
1 parent c656b97 commit ac70bd2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
3 changes: 3 additions & 0 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ mod evm_circuit_stats {
evm_circuit::{
param::{
LOOKUP_CONFIG, N_BYTE_LOOKUPS, N_COPY_COLUMNS, N_PHASE1_COLUMNS, N_PHASE2_COLUMNS,
N_PHASE2_COPY_COLUMNS,
},
step::ExecutionState,
EvmCircuit,
Expand Down Expand Up @@ -598,6 +599,8 @@ mod evm_circuit_stats {
N_PHASE2_COLUMNS,
storage_perm,
N_COPY_COLUMNS,
storage_perm_2,
N_PHASE2_COPY_COLUMNS,
byte_lookup,
N_BYTE_LOOKUPS,
fixed_table,
Expand Down
2 changes: 2 additions & 0 deletions zkevm-circuits/src/evm_circuit/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub(crate) const N_PHASE1_COLUMNS: usize =

// Number of copy columns
pub(crate) const N_COPY_COLUMNS: usize = 2;
// Number of copy columns for phase2
pub(crate) const N_PHASE2_COPY_COLUMNS: usize = 1;

pub(crate) const N_BYTE_LOOKUPS: usize = 24;

Expand Down
27 changes: 23 additions & 4 deletions zkevm-circuits/src/evm_circuit/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
evm_circuit::{
param::{
LOOKUP_CONFIG, N_BYTES_MEMORY_ADDRESS, N_BYTE_LOOKUPS, N_COPY_COLUMNS, N_PHASE2_COLUMNS,
LOOKUP_CONFIG, N_BYTES_MEMORY_ADDRESS, N_BYTE_LOOKUPS, N_COPY_COLUMNS,
N_PHASE2_COLUMNS, N_PHASE2_COPY_COLUMNS,
},
table::Table,
},
Expand Down Expand Up @@ -289,6 +290,7 @@ pub(crate) enum CellType {
StoragePhase1,
StoragePhase2,
StoragePermutation,
StoragePermutationPhase2,
LookupByte,
Lookup(Table),
}
Expand Down Expand Up @@ -378,8 +380,15 @@ impl<F: FieldExt> CellManager<F> {
}
}

// Mark columns used for copy constraints on phase2
for _ in 0..N_PHASE2_COPY_COLUMNS {
meta.enable_equality(advices[column_idx]);
columns[column_idx].cell_type = CellType::StoragePermutationPhase2;
column_idx += 1;
}

// Mark columns used for Phase2 constraints
for _ in 0..N_PHASE2_COLUMNS {
for _ in N_PHASE2_COPY_COLUMNS..N_PHASE2_COLUMNS {
columns[column_idx].cell_type = CellType::StoragePhase2;
column_idx += 1;
}
Expand Down Expand Up @@ -430,16 +439,26 @@ impl<F: FieldExt> CellManager<F> {
best_height = column.height;
}
}
// Replace a CellType::Storage by CellType::StoragePermutation if the later has
// better height
// Replace a CellType::Storage by CellType::StoragePermutation (phase 1 or phase 2) if the
// later has better height
if cell_type == CellType::StoragePhase1 {
for column in self.columns.iter() {
if column.cell_type == CellType::StoragePermutation && column.height < best_height {
best_index = Some(column.index);
best_height = column.height;
}
}
} else if cell_type == CellType::StoragePhase2 {
for column in self.columns.iter() {
if column.cell_type == CellType::StoragePermutationPhase2
&& column.height < best_height
{
best_index = Some(column.index);
best_height = column.height;
}
}
}

match best_index {
Some(index) => index,
// If we reach this case, it means that all the columns of cell_type have assignments
Expand Down
4 changes: 4 additions & 0 deletions zkevm-circuits/src/evm_circuit/util/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,10 @@ impl<'a, F: Field> ConstraintBuilder<'a, F> {
self.query_cell_with_type(CellType::StoragePermutation)
}

pub(crate) fn query_copy_cell_phase2(&mut self) -> Cell<F> {
self.query_cell_with_type(CellType::StoragePermutationPhase2)
}

pub(crate) fn query_cell_with_type(&mut self, cell_type: CellType) -> Cell<F> {
self.query_cells(cell_type, 1).first().unwrap().clone()
}
Expand Down
4 changes: 4 additions & 0 deletions zkevm-circuits/src/evm_circuit/util/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ impl Instrument {
CellType::StoragePermutation => {
report.storage_perm = data_entry;
}
CellType::StoragePermutationPhase2 => {
report.storage_perm_2 = data_entry;
}
CellType::LookupByte => {
report.byte_lookup = data_entry;
}
Expand Down Expand Up @@ -116,6 +119,7 @@ pub(crate) struct ExecStateReport {
pub(crate) storage_1: StateReportRow,
pub(crate) storage_2: StateReportRow,
pub(crate) storage_perm: StateReportRow,
pub(crate) storage_perm_2: StateReportRow,
pub(crate) byte_lookup: StateReportRow,
pub(crate) fixed_table: StateReportRow,
pub(crate) tx_table: StateReportRow,
Expand Down

0 comments on commit ac70bd2

Please sign in to comment.