-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(test-shape-cs): TestShapeCS * refactor(test-shape-cs): bellpepper instead of bellpepper-core * refactor(test-shape-cs): debug on test cs * refactor(test-shape-cs): fix clippy & fmt * feat(test-shape-cs): added ShapeCS
- Loading branch information
1 parent
dcf98f6
commit 1afbae9
Showing
5 changed files
with
429 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
target | ||
Cargo.lock | ||
|
||
# IDE config files | ||
.vscode | ||
.idea |
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,5 +1,7 @@ | ||
pub use bellpepper_core::{Comparable, Constraint}; | ||
|
||
pub mod bench_cs; | ||
pub mod metric_cs; | ||
pub mod shape_cs; | ||
pub mod test_shape_cs; | ||
pub mod witness_cs; | ||
|
||
pub use bellpepper_core::{Comparable, Constraint}; |
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,106 @@ | ||
//! Support for generating R1CS shape. | ||
|
||
use bellpepper_core::{ConstraintSystem, Index, LinearCombination, SynthesisError, Variable}; | ||
use ff::PrimeField; | ||
|
||
/// `ShapeCSConstraint` represent a constraint in a `ShapeCS`. | ||
pub type ShapeCSConstraint<Scalar> = ( | ||
LinearCombination<Scalar>, | ||
LinearCombination<Scalar>, | ||
LinearCombination<Scalar>, | ||
); | ||
|
||
/// `ShapeCS` is a `ConstraintSystem` for creating `R1CSShape`s for a circuit. | ||
#[derive(Debug)] | ||
pub struct ShapeCS<Scalar: PrimeField> { | ||
/// All constraints added to the `ShapeCS`. | ||
pub constraints: Vec<ShapeCSConstraint<Scalar>>, | ||
inputs: usize, | ||
aux: usize, | ||
} | ||
|
||
impl<Scalar: PrimeField> ShapeCS<Scalar> { | ||
/// Create a new, default `ShapeCS`, | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
/// Returns the number of constraints defined for this `ShapeCS`. | ||
pub fn num_constraints(&self) -> usize { | ||
self.constraints.len() | ||
} | ||
|
||
/// Returns the number of inputs defined for this `ShapeCS`. | ||
pub fn num_inputs(&self) -> usize { | ||
self.inputs | ||
} | ||
|
||
/// Returns the number of aux inputs defined for this `ShapeCS`. | ||
pub fn num_aux(&self) -> usize { | ||
self.aux | ||
} | ||
} | ||
|
||
impl<Scalar: PrimeField> Default for ShapeCS<Scalar> { | ||
fn default() -> Self { | ||
Self { | ||
constraints: vec![], | ||
inputs: 1, | ||
aux: 0, | ||
} | ||
} | ||
} | ||
|
||
impl<Scalar: PrimeField> ConstraintSystem<Scalar> for ShapeCS<Scalar> { | ||
type Root = Self; | ||
|
||
fn alloc<F, A, AR>(&mut self, _annotation: A, _f: F) -> Result<Variable, SynthesisError> | ||
where | ||
F: FnOnce() -> Result<Scalar, SynthesisError>, | ||
A: FnOnce() -> AR, | ||
AR: Into<String>, | ||
{ | ||
self.aux += 1; | ||
|
||
Ok(Variable::new_unchecked(Index::Aux(self.aux - 1))) | ||
} | ||
|
||
fn alloc_input<F, A, AR>(&mut self, _annotation: A, _f: F) -> Result<Variable, SynthesisError> | ||
where | ||
F: FnOnce() -> Result<Scalar, SynthesisError>, | ||
A: FnOnce() -> AR, | ||
AR: Into<String>, | ||
{ | ||
self.inputs += 1; | ||
|
||
Ok(Variable::new_unchecked(Index::Input(self.inputs - 1))) | ||
} | ||
|
||
fn enforce<A, AR, LA, LB, LC>(&mut self, _annotation: A, a: LA, b: LB, c: LC) | ||
where | ||
A: FnOnce() -> AR, | ||
AR: Into<String>, | ||
LA: FnOnce(LinearCombination<Scalar>) -> LinearCombination<Scalar>, | ||
LB: FnOnce(LinearCombination<Scalar>) -> LinearCombination<Scalar>, | ||
LC: FnOnce(LinearCombination<Scalar>) -> LinearCombination<Scalar>, | ||
{ | ||
let a = a(LinearCombination::zero()); | ||
let b = b(LinearCombination::zero()); | ||
let c = c(LinearCombination::zero()); | ||
|
||
self.constraints.push((a, b, c)); | ||
} | ||
|
||
fn push_namespace<NR, N>(&mut self, _name_fn: N) | ||
where | ||
NR: Into<String>, | ||
N: FnOnce() -> NR, | ||
{ | ||
} | ||
|
||
fn pop_namespace(&mut self) {} | ||
|
||
fn get_root(&mut self) -> &mut Self::Root { | ||
self | ||
} | ||
} |
Oops, something went wrong.