Skip to content

Commit

Permalink
Moving TestShapeCS (#91)
Browse files Browse the repository at this point in the history
* 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
tchataigner authored Mar 21, 2024
1 parent dcf98f6 commit 1afbae9
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
target
Cargo.lock

# IDE config files
.vscode
.idea
3 changes: 2 additions & 1 deletion crates/bellpepper-core/src/util_cs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::LinearCombination;
use ff::PrimeField;

use crate::LinearCombination;

pub mod test_cs;

pub type Constraint<Scalar> = (
Expand Down
6 changes: 4 additions & 2 deletions crates/bellpepper/src/util_cs/mod.rs
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};
106 changes: 106 additions & 0 deletions crates/bellpepper/src/util_cs/shape_cs.rs
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
}
}
Loading

0 comments on commit 1afbae9

Please sign in to comment.