Skip to content

Commit

Permalink
inline single trait requirements; fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
srinathsetty committed Nov 16, 2023
1 parent 00b126a commit ad73659
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 132 deletions.
10 changes: 2 additions & 8 deletions benches/compressed-snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,21 +227,15 @@ struct NonTrivialCircuit<F: PrimeField> {
_p: PhantomData<F>,
}

impl<F> NonTrivialCircuit<F>
where
F: PrimeField,
{
impl<F: PrimeField> NonTrivialCircuit<F> {
pub fn new(num_cons: usize) -> Self {
Self {
num_cons,
_p: PhantomData,
}
}
}
impl<F> StepCircuit<F> for NonTrivialCircuit<F>
where
F: PrimeField,
{
impl<F: PrimeField> StepCircuit<F> for NonTrivialCircuit<F> {
fn arity(&self) -> usize {
1
}
Expand Down
10 changes: 2 additions & 8 deletions benches/compute-digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,15 @@ struct NonTrivialCircuit<F: PrimeField> {
_p: PhantomData<F>,
}

impl<F> NonTrivialCircuit<F>
where
F: PrimeField,
{
impl<F: PrimeField> NonTrivialCircuit<F> {
pub fn new(num_cons: usize) -> Self {
Self {
num_cons,
_p: PhantomData,
}
}
}
impl<F> StepCircuit<F> for NonTrivialCircuit<F>
where
F: PrimeField,
{
impl<F: PrimeField> StepCircuit<F> for NonTrivialCircuit<F> {
fn arity(&self) -> usize {
1
}
Expand Down
10 changes: 2 additions & 8 deletions benches/recursive-snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,15 @@ struct NonTrivialCircuit<F: PrimeField> {
_p: PhantomData<F>,
}

impl<F> NonTrivialCircuit<F>
where
F: PrimeField,
{
impl<F: PrimeField> NonTrivialCircuit<F> {
pub fn new(num_cons: usize) -> Self {
Self {
num_cons,
_p: PhantomData,
}
}
}
impl<F> StepCircuit<F> for NonTrivialCircuit<F>
where
F: PrimeField,
{
impl<F: PrimeField> StepCircuit<F> for NonTrivialCircuit<F> {
fn arity(&self) -> usize {
1
}
Expand Down
5 changes: 1 addition & 4 deletions examples/minroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ struct MinRootCircuit<F: PrimeField> {
seq: Vec<MinRootIteration<F>>,
}

impl<F> StepCircuit<F> for MinRootCircuit<F>
where
F: PrimeField,
{
impl<F: PrimeField> StepCircuit<F> for MinRootCircuit<F> {
fn arity(&self) -> usize {
2
}
Expand Down
5 changes: 1 addition & 4 deletions src/bellpepper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ mod tests {
);
}

fn test_alloc_bit_with<E>()
where
E: Engine,
{
fn test_alloc_bit_with<E: Engine>() {
// First create the shape
let mut cs: ShapeCS<E> = ShapeCS::new();
synthesize_alloc_bit(&mut cs);
Expand Down
10 changes: 2 additions & 8 deletions src/bellpepper/test_shape_cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ impl Ord for OrderedVariable {
}

/// `TestShapeCS` is a `ConstraintSystem` for creating `R1CSShape`s for a circuit.
pub struct TestShapeCS<E: Engine>
where
E::Scalar: PrimeField + Field,
{
pub struct TestShapeCS<E: Engine> {
named_objects: HashMap<String, NamedObject>,
current_namespace: Vec<String>,
/// All constraints added to the `TestShapeCS`.
Expand Down Expand Up @@ -216,10 +213,7 @@ where
}
}

impl<E: Engine> Default for TestShapeCS<E>
where
E::Scalar: PrimeField,
{
impl<E: Engine> Default for TestShapeCS<E> {
fn default() -> Self {
let mut map = HashMap::new();
map.insert("ONE".into(), NamedObject::Var(TestShapeCS::<E>::one()));
Expand Down
52 changes: 17 additions & 35 deletions src/gadgets/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ use ff::{Field, PrimeField};

/// `AllocatedPoint` provides an elliptic curve abstraction inside a circuit.
#[derive(Clone)]
pub struct AllocatedPoint<E>
where
E: Engine,
{
pub struct AllocatedPoint<E: Engine> {
pub(crate) x: AllocatedNum<E::Base>,
pub(crate) y: AllocatedNum<E::Base>,
pub(crate) is_infinity: AllocatedNum<E::Base>,
Expand All @@ -33,13 +30,10 @@ where
{
/// Allocates a new point on the curve using coordinates provided by `coords`.
/// If coords = None, it allocates the default infinity point
pub fn alloc<CS>(
pub fn alloc<CS: ConstraintSystem<E::Base>>(
mut cs: CS,
coords: Option<(E::Base, E::Base, bool)>,
) -> Result<Self, SynthesisError>
where
CS: ConstraintSystem<E::Base>,
{
) -> Result<Self, SynthesisError> {
let x = AllocatedNum::alloc(cs.namespace(|| "x"), || {
Ok(coords.map_or(E::Base::ZERO, |c| c.0))
})?;
Expand Down Expand Up @@ -110,10 +104,7 @@ where
}

/// Allocates a default point on the curve, set to the identity point.
pub fn default<CS>(mut cs: CS) -> Result<Self, SynthesisError>
where
CS: ConstraintSystem<E::Base>,
{
pub fn default<CS: ConstraintSystem<E::Base>>(mut cs: CS) -> Result<Self, SynthesisError> {
let zero = alloc_zero(cs.namespace(|| "zero"));
let one = alloc_one(cs.namespace(|| "one"));

Expand Down Expand Up @@ -368,7 +359,7 @@ where
/// Doubles the supplied point.
pub fn double<CS: ConstraintSystem<E::Base>>(&self, mut cs: CS) -> Result<Self, SynthesisError> {
//*************************************************************/
// lambda = (E::Base::from(3) * self.x * self.x + E::A())
// lambda = (E::Base::from(3) * self.x * self.x + E::GE::A())
// * (E::Base::from(2)) * self.y).invert().unwrap();
/*************************************************************/

Expand All @@ -385,7 +376,7 @@ where

let tmp = select_one_or_num2(cs.namespace(|| "tmp"), &tmp_actual, &self.is_infinity)?;

// Now compute lambda as (E::Base::from(3) * self.x * self.x + E::A()) * tmp_inv
// Now compute lambda as (E::Base::from(3) * self.x * self.x + E::GE::A()) * tmp_inv

let prod_1 = AllocatedNum::alloc(cs.namespace(|| "alloc prod 1"), || {
Ok(E::Base::from(3) * self.x.get_value().get()? * self.x.get_value().get()?)
Expand Down Expand Up @@ -601,10 +592,7 @@ where

#[derive(Clone)]
/// `AllocatedPoint` but one that is guaranteed to be not infinity
pub struct AllocatedPointNonInfinity<E>
where
E: Engine,
{
pub struct AllocatedPointNonInfinity<E: Engine> {
x: AllocatedNum<E::Base>,
y: AllocatedNum<E::Base>,
}
Expand All @@ -619,10 +607,10 @@ where
}

/// Allocates a new point on the curve using coordinates provided by `coords`.
pub fn alloc<CS>(mut cs: CS, coords: Option<(E::Base, E::Base)>) -> Result<Self, SynthesisError>
where
CS: ConstraintSystem<E::Base>,
{
pub fn alloc<CS: ConstraintSystem<E::Base>>(
mut cs: CS,
coords: Option<(E::Base, E::Base)>,
) -> Result<Self, SynthesisError> {
let x = AllocatedNum::alloc(cs.namespace(|| "x"), || {
coords.map_or(Err(SynthesisError::AssignmentMissing), |c| Ok(c.0))
})?;
Expand Down Expand Up @@ -721,10 +709,10 @@ where
}

/// doubles the point; since this is called with a point not at infinity, it is guaranteed to be not infinity
pub fn double_incomplete<CS>(&self, mut cs: CS) -> Result<Self, SynthesisError>
where
CS: ConstraintSystem<E::Base>,
{
pub fn double_incomplete<CS: ConstraintSystem<E::Base>>(
&self,
mut cs: CS,
) -> Result<Self, SynthesisError> {
// lambda = (3 x^2 + a) / 2 * y

let x_sq = self.x.square(cs.namespace(|| "x_sq"))?;
Expand Down Expand Up @@ -811,19 +799,13 @@ mod tests {
use rand::rngs::OsRng;

#[derive(Debug, Clone)]
pub struct Point<E>
where
E: Engine,
{
pub struct Point<E: Engine> {
x: E::Base,
y: E::Base,
is_infinity: bool,
}

impl<E> Point<E>
where
E: Engine,
{
impl<E: Engine> Point<E> {
pub fn new(x: E::Base, y: E::Base, is_infinity: bool) -> Self {
Self { x, y, is_infinity }
}
Expand Down
8 changes: 4 additions & 4 deletions src/gadgets/nonnative/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ pub struct Bitvector<Scalar: PrimeField> {
impl<Scalar: PrimeField> Bit<Scalar> {
/// Allocate a variable in the constraint system which can only be a
/// boolean value.
pub fn alloc<CS>(mut cs: CS, value: Option<bool>) -> Result<Self, SynthesisError>
where
CS: ConstraintSystem<Scalar>,
{
pub fn alloc<CS: ConstraintSystem<Scalar>>(
mut cs: CS,
value: Option<bool>,
) -> Result<Self, SynthesisError> {
let var = cs.alloc(
|| "boolean",
|| {
Expand Down
20 changes: 5 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ where
let F_arity_primary = c_primary.arity();
let F_arity_secondary = c_secondary.arity();

// ro_consts_circuit_primary are parameterized by G2 because the type alias uses G2::Base = E1::Scalar
// ro_consts_circuit_primary are parameterized by E2 because the type alias uses E2::Base = E1::Scalar
let ro_consts_circuit_primary: ROConstantsCircuit<E2> = ROConstantsCircuit::<E2>::default();
let ro_consts_circuit_secondary: ROConstantsCircuit<E1> = ROConstantsCircuit::<E1>::default();

Expand Down Expand Up @@ -865,8 +865,7 @@ mod tests {
traits::{evaluation::EvaluationEngineTrait, snark::default_ck_hint},
};
use ::bellpepper_core::{num::AllocatedNum, ConstraintSystem, SynthesisError};
use core::fmt::Write;
use core::marker::PhantomData;
use core::{fmt::Write, marker::PhantomData};
use ff::PrimeField;
use traits::circuit::TrivialCircuit;

Expand All @@ -879,10 +878,7 @@ mod tests {
_p: PhantomData<F>,
}

impl<F> StepCircuit<F> for CubicCircuit<F>
where
F: PrimeField,
{
impl<F: PrimeField> StepCircuit<F> for CubicCircuit<F> {
fn arity(&self) -> usize {
1
}
Expand Down Expand Up @@ -919,10 +915,7 @@ mod tests {
}
}

impl<F> CubicCircuit<F>
where
F: PrimeField,
{
impl<F: PrimeField> CubicCircuit<F> {
fn output(&self, z: &[F]) -> Vec<F> {
vec![z[0] * z[0] * z[0] + z[0] + F::from(5u64)]
}
Expand Down Expand Up @@ -1355,10 +1348,7 @@ mod tests {
y: F,
}

impl<F> FifthRootCheckingCircuit<F>
where
F: PrimeField,
{
impl<F: PrimeField> FifthRootCheckingCircuit<F> {
fn new(num_steps: usize) -> (Vec<F>, Vec<Self>) {
let mut powers = Vec::new();
let rng = &mut rand::rngs::OsRng;
Expand Down
22 changes: 8 additions & 14 deletions src/nifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,14 @@ impl<E: Engine> NIFS<E> {
mod tests {
use super::*;
use crate::{
bellpepper::{
r1cs::{NovaShape, NovaWitness},
solver::SatisfyingAssignment,
test_shape_cs::TestShapeCS,
},
provider::{bn256_grumpkin::Bn256Engine, pasta::PallasEngine, secp_secq::Secp256k1Engine},
r1cs::SparseMatrix,
r1cs::R1CS,
traits::snark::default_ck_hint,
traits::Engine,
r1cs::{SparseMatrix, R1CS},
traits::{snark::default_ck_hint, Engine},
};
use ::bellpepper_core::{num::AllocatedNum, ConstraintSystem, SynthesisError};
use ff::{Field, PrimeField};
Expand Down Expand Up @@ -157,16 +160,7 @@ mod tests {
Ok(())
}

fn test_tiny_r1cs_bellpepper_with<E>()
where
E: Engine,
{
use crate::bellpepper::{
r1cs::{NovaShape, NovaWitness},
solver::SatisfyingAssignment,
test_shape_cs::TestShapeCS,
};

fn test_tiny_r1cs_bellpepper_with<E: Engine>() {
// First create the shape
let mut cs: TestShapeCS<E> = TestShapeCS::new();
let _ = synthesize_tiny_r1cs_bellpepper(&mut cs, None);
Expand Down
5 changes: 1 addition & 4 deletions src/provider/ipa_pc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ where
}
}

fn inner_product<T>(a: &[T], b: &[T]) -> T
where
T: Field + Send + Sync,
{
fn inner_product<T: Field + Send + Sync>(a: &[T], b: &[T]) -> T {
assert_eq!(a.len(), b.len());
(0..a.len())
.into_par_iter()
Expand Down
12 changes: 3 additions & 9 deletions src/provider/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ where

/// A Poseidon-based RO gadget to use inside the verifier circuit.
#[derive(Serialize, Deserialize)]
pub struct PoseidonROCircuit<Scalar>
where
Scalar: PrimeField,
{
pub struct PoseidonROCircuit<Scalar: PrimeField> {
// Internal state
state: Vec<AllocatedNum<Scalar>>,
constants: PoseidonConstantsCircuit<Scalar>,
Expand Down Expand Up @@ -140,14 +137,11 @@ where
}

/// Compute a challenge by hashing the current state
fn squeeze<CS>(
fn squeeze<CS: ConstraintSystem<Scalar>>(
&mut self,
mut cs: CS,
num_bits: usize,
) -> Result<Vec<AllocatedBit>, SynthesisError>
where
CS: ConstraintSystem<Scalar>,
{
) -> Result<Vec<AllocatedBit>, SynthesisError> {
// check if we have squeezed already
assert!(!self.squeezed, "Cannot squeeze again after squeezing");
self.squeezed = true;
Expand Down
Loading

0 comments on commit ad73659

Please sign in to comment.