Skip to content

Commit

Permalink
Extract copy constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
pnyda committed Sep 13, 2024
1 parent ed9c454 commit e9afe8e
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions halo2_proofs/src/ccs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ use crate::plonk::{Advice, Any, Assigned, Assignment, Column, Error, Fixed, Inst
pub struct CellDumper<F: Field> {
pub k: u32,

pub copy_constraints: Vec<(
Any, // column_type
usize, // column_index
usize, // row_index
Any, // column_type
usize, // column_index
usize, // row_index
)>,
// instance[column_index][row_index] == cell_value
pub instance: Vec<Vec<Value<F>>>,
// fixed[column_index][row_index] == cell_value
Expand Down Expand Up @@ -122,18 +130,23 @@ impl<F: Field> Assignment<F> for CellDumper<F> {

fn copy(
&mut self,
_left_column: Column<Any>,
left_column: Column<Any>,
left_row: usize,
_right_column: Column<Any>,
right_column: Column<Any>,
right_row: usize,
) -> Result<(), Error> {
if !self.usable_rows.contains(&left_row) || !self.usable_rows.contains(&right_row) {
return Err(Error::not_enough_rows_available(self.k));
}

// TODO
// self.permutation.copy(left_column, left_row, right_column, right_row)
// permutation::keygen::Assembly is a private module so we can't expose it.
self.copy_constraints.push((
*left_column.column_type(),
left_column.index(),
left_row,
*right_column.column_type(),
right_column.index(),
right_row,
));

Ok(())
}
Expand Down Expand Up @@ -179,7 +192,7 @@ mod tests {
use super::CellDumper;
use crate::circuit::{Layouter, SimpleFloorPlanner, Value};
use crate::plonk::{
Advice, Circuit, Column, ConstraintSystem, Error, Fixed, FloorPlanner, Selector,
Advice, Any, Circuit, Column, ConstraintSystem, Error, Fixed, FloorPlanner, Selector,
};
use pasta_curves::Fp;

Expand Down Expand Up @@ -216,6 +229,8 @@ mod tests {
layouter.assign_region(
|| "region",
|mut region| {
// to test assignment extractions

config.s.enable(&mut region, 0)?;
region.assign_fixed(|| "", config.a, 0, || Value::known(Fp::from(123)))?;
region.assign_advice(|| "", config.b, 0, || Value::known(Fp::from(321)))?;
Expand All @@ -227,6 +242,18 @@ mod tests {
region.assign_fixed(|| "", config.a, 2, || Value::known(Fp::from(789)))?;
region.assign_advice(|| "", config.b, 2, || Value::known(Fp::from(987)))?;

// to test copy constraint extractions

let above =
region.assign_advice(|| "", config.b, 3, || Value::known(Fp::from(111)))?;
let below =
region.assign_advice(|| "", config.b, 4, || Value::known(Fp::from(111)))?;
region.constrain_equal(above.cell(), below.cell())?;

let left =
region.assign_fixed(|| "", config.a, 3, || Value::known(Fp::from(111)))?;
region.constrain_equal(left.cell(), above.cell())?;

Ok(())
},
)?;
Expand All @@ -248,6 +275,7 @@ mod tests {
fixed: vec![vec![None; n]; meta.num_fixed_columns],
advice: vec![vec![None; n]; meta.num_advice_columns],
selectors: vec![vec![false; n]; meta.num_selectors],
copy_constraints: Vec::new(),
usable_rows: 0..(n - meta.blinding_factors() - 1), // Why -1?
};

Expand All @@ -271,6 +299,15 @@ mod tests {
assert_eq!(cell_dumper.fixed[0][2], Some(Fp::from(789)));
assert_eq!(cell_dumper.advice[0][2], Some(Fp::from(987)));

assert_eq!(
cell_dumper.copy_constraints[0],
(Any::Advice, 0, 3, Any::Advice, 0, 4)
);
assert_eq!(
cell_dumper.copy_constraints[1],
(Any::Fixed, 0, 3, Any::Advice, 0, 3)
);

Ok(())
}
}

0 comments on commit e9afe8e

Please sign in to comment.