-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ZKR-4188-Fixed-Permutations-SMT-Solver (#56)
ZKR-4188-Fixed-Permutations-SMT-Solver
- Loading branch information
1 parent
a21dcdc
commit ec10164
Showing
28 changed files
with
3,259 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod bit_decomposition; | ||
pub mod copy_constraint; | ||
pub mod lookup_circuits; | ||
pub mod simple; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod mul; | ||
pub mod no_selector; | ||
pub mod steps; | ||
pub mod steps_with_fixed; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
use group::ff::Field; | ||
use pse_halo2_proofs::circuit::{AssignedCell, Layouter, SimpleFloorPlanner, Value}; | ||
use pse_halo2_proofs::plonk::{ | ||
Advice, Circuit, Column, ConstraintSystem, Error, Fixed, Instance, Selector, | ||
}; | ||
use pse_halo2_proofs::poly::Rotation; | ||
use std::marker::PhantomData; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct MulConfig { | ||
pub col_fixed: Column<Fixed>, | ||
pub col_a: Column<Advice>, | ||
pub col_b: Column<Advice>, | ||
pub col_c: Column<Advice>, | ||
pub selector: Selector, | ||
pub instance: Column<Instance>, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
struct MulChip<F: Field> { | ||
config: MulConfig, | ||
_marker: PhantomData<F>, | ||
} | ||
|
||
impl<F: Field> MulChip<F> { | ||
pub fn construct(config: MulConfig) -> Self { | ||
Self { | ||
config, | ||
_marker: PhantomData, | ||
} | ||
} | ||
|
||
pub fn configure(meta: &mut ConstraintSystem<F>) -> MulConfig { | ||
let col_fixed = meta.fixed_column(); | ||
let col_a = meta.advice_column(); | ||
let col_b = meta.advice_column(); | ||
let col_c = meta.advice_column(); | ||
let selector = meta.selector(); | ||
let instance = meta.instance_column(); | ||
|
||
meta.enable_constant(col_fixed); | ||
meta.enable_equality(col_a); | ||
meta.enable_equality(col_b); | ||
meta.enable_equality(col_c); | ||
meta.enable_equality(instance); | ||
|
||
// computes c = -a^2 | ||
meta.create_gate("mul", |meta| { | ||
// | ||
// col_fixed | col_a | col_b | col_c | selector | ||
// f a b c s | ||
// | ||
let s = meta.query_selector(selector); | ||
let f = meta.query_fixed(col_fixed, Rotation::cur()); | ||
let a = meta.query_advice(col_a, Rotation::cur()); | ||
let b = meta.query_advice(col_b, Rotation::cur()); | ||
let c = meta.query_advice(col_c, Rotation::cur()); | ||
|
||
vec![s.clone() * (f * a.clone() - b.clone()), s * (a * b - c)] | ||
}); | ||
|
||
MulConfig { | ||
col_fixed, | ||
col_a, | ||
col_b, | ||
col_c, | ||
selector, | ||
instance, | ||
} | ||
} | ||
|
||
#[allow(clippy::type_complexity)] | ||
pub fn assign_first_row( | ||
&self, | ||
mut layouter: impl Layouter<F>, | ||
) -> Result<AssignedCell<F, F>, Error> { | ||
layouter.assign_region( | ||
|| "first row", | ||
|mut region| { | ||
self.config.selector.enable(&mut region, 0)?; | ||
|
||
let fixed_cell = region.assign_fixed( | ||
|| "-1", | ||
self.config.col_fixed, | ||
0, | ||
|| -> Value<F> { Value::known(-F::ONE) }, | ||
)?; | ||
|
||
let a_cell = region.assign_advice_from_instance( | ||
|| "a", | ||
self.config.instance, | ||
0, | ||
self.config.col_a, | ||
0, | ||
)?; | ||
|
||
let b_cell = region.assign_advice( | ||
|| "-1 * a", | ||
self.config.col_b, | ||
0, | ||
|| a_cell.value().copied() * fixed_cell.value(), | ||
)?; | ||
|
||
let c_cell = region.assign_advice( | ||
|| "a * b", | ||
self.config.col_c, | ||
0, | ||
|| a_cell.value().copied() * b_cell.value(), | ||
)?; | ||
|
||
Ok(c_cell) | ||
}, | ||
) | ||
} | ||
|
||
pub fn expose_public( | ||
&self, | ||
mut layouter: impl Layouter<F>, | ||
cell: &AssignedCell<F, F>, | ||
row: usize, | ||
) -> Result<(), Error> { | ||
layouter.constrain_instance(cell.cell(), self.config.instance, row) | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct MulCircuit<F>(pub PhantomData<F>); | ||
|
||
impl<F: Field> Circuit<F> for MulCircuit<F> { | ||
type Config = MulConfig; | ||
type FloorPlanner = SimpleFloorPlanner; | ||
|
||
fn without_witnesses(&self) -> Self { | ||
Self::default() | ||
} | ||
|
||
fn configure(meta: &mut ConstraintSystem<F>) -> Self::Config { | ||
MulChip::configure(meta) | ||
} | ||
|
||
fn synthesize( | ||
&self, | ||
config: Self::Config, | ||
mut layouter: impl Layouter<F>, | ||
) -> Result<(), Error> { | ||
let chip = MulChip::construct(config); | ||
|
||
let prev_c = chip.assign_first_row(layouter.namespace(|| "first row"))?; | ||
|
||
chip.expose_public(layouter.namespace(|| "out"), &prev_c, 1)?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
use group::ff::Field; | ||
use pse_halo2_proofs::circuit::{AssignedCell, Layouter, SimpleFloorPlanner, Value}; | ||
use pse_halo2_proofs::plonk::{Advice, Circuit, Column, ConstraintSystem, Error, Fixed, Instance}; | ||
use pse_halo2_proofs::poly::Rotation; | ||
use std::marker::PhantomData; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct MulConfig { | ||
pub col_fixed: Column<Fixed>, | ||
pub col_a: Column<Advice>, | ||
pub col_b: Column<Advice>, | ||
pub col_c: Column<Advice>, | ||
pub instance: Column<Instance>, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
struct MulChip<F: Field> { | ||
config: MulConfig, | ||
_marker: PhantomData<F>, | ||
} | ||
|
||
impl<F: Field> MulChip<F> { | ||
pub fn construct(config: MulConfig) -> Self { | ||
Self { | ||
config, | ||
_marker: PhantomData, | ||
} | ||
} | ||
|
||
pub fn configure(meta: &mut ConstraintSystem<F>) -> MulConfig { | ||
let col_fixed = meta.fixed_column(); | ||
let col_a = meta.advice_column(); | ||
let col_b = meta.advice_column(); | ||
let col_c = meta.advice_column(); | ||
let instance = meta.instance_column(); | ||
|
||
meta.enable_constant(col_fixed); | ||
meta.enable_equality(col_a); | ||
meta.enable_equality(col_b); | ||
meta.enable_equality(col_c); | ||
meta.enable_equality(instance); | ||
|
||
// computes c = -a^2 | ||
meta.create_gate("mul", |meta| { | ||
// | ||
// col_fixed | col_a | col_b | col_c | ||
// f a b c | ||
// | ||
let f = meta.query_fixed(col_fixed, Rotation::cur()); | ||
let a = meta.query_advice(col_a, Rotation::cur()); | ||
let b = meta.query_advice(col_b, Rotation::cur()); | ||
let c = meta.query_advice(col_c, Rotation::cur()); | ||
|
||
vec![(f * a.clone() - b.clone()), (a * b - c)] | ||
}); | ||
|
||
MulConfig { | ||
col_fixed, | ||
col_a, | ||
col_b, | ||
col_c, | ||
instance, | ||
} | ||
} | ||
|
||
#[allow(clippy::type_complexity)] | ||
pub fn assign_first_row( | ||
&self, | ||
mut layouter: impl Layouter<F>, | ||
) -> Result<AssignedCell<F, F>, Error> { | ||
layouter.assign_region( | ||
|| "first row", | ||
|mut region| { | ||
let fixed_cell = region.assign_fixed( | ||
|| "-1", | ||
self.config.col_fixed, | ||
0, | ||
|| -> Value<F> { Value::known(-F::ONE) }, | ||
)?; | ||
|
||
let a_cell = region.assign_advice_from_instance( | ||
|| "a", | ||
self.config.instance, | ||
0, | ||
self.config.col_a, | ||
0, | ||
)?; | ||
|
||
let b_cell = region.assign_advice( | ||
|| "-1 * a", | ||
self.config.col_b, | ||
0, | ||
|| a_cell.value().copied() * fixed_cell.value(), | ||
)?; | ||
|
||
let c_cell = region.assign_advice( | ||
|| "a * b", | ||
self.config.col_c, | ||
0, | ||
|| a_cell.value().copied() * b_cell.value(), | ||
)?; | ||
|
||
Ok(c_cell) | ||
}, | ||
) | ||
} | ||
|
||
pub fn expose_public( | ||
&self, | ||
mut layouter: impl Layouter<F>, | ||
cell: &AssignedCell<F, F>, | ||
row: usize, | ||
) -> Result<(), Error> { | ||
layouter.constrain_instance(cell.cell(), self.config.instance, row) | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct MulCircuit<F>(pub PhantomData<F>); | ||
|
||
impl<F: Field> Circuit<F> for MulCircuit<F> { | ||
type Config = MulConfig; | ||
type FloorPlanner = SimpleFloorPlanner; | ||
|
||
fn without_witnesses(&self) -> Self { | ||
Self::default() | ||
} | ||
|
||
fn configure(meta: &mut ConstraintSystem<F>) -> Self::Config { | ||
MulChip::configure(meta) | ||
} | ||
|
||
fn synthesize( | ||
&self, | ||
config: Self::Config, | ||
mut layouter: impl Layouter<F>, | ||
) -> Result<(), Error> { | ||
let chip = MulChip::construct(config); | ||
|
||
let prev_c = chip.assign_first_row(layouter.namespace(|| "first row"))?; | ||
|
||
chip.expose_public(layouter.namespace(|| "out"), &prev_c, 1)?; | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.