Skip to content
Permalink
Browse files

r1cs-std works with no-std (#92)

  • Loading branch information
debris committed Feb 5, 2020
1 parent 317fc33 commit cb4ae26fe48859c9b8becd9725a3209f17e5d368
@@ -60,6 +60,11 @@ matrix:
- cargo build -p r1cs-core --no-default-features --target thumbv6m-none-eabi
- cargo check --examples -p r1cs-core --no-default-features --target thumbv6m-none-eabi
- cd ..
# test r1cs-std
- cd r1cs-std
- cargo build -p r1cs-std --no-default-features --target thumbv6m-none-eabi
- cargo check --examples -p r1cs-std --no-default-features --target thumbv6m-none-eabi
- cd ..
# test ff-fft
- cd ff-fft
- cargo build -p ff-fft --no-default-features --target thumbv6m-none-eabi
@@ -22,11 +22,14 @@ edition = "2018"
################################# Dependencies ################################

[dependencies]
algebra = { path = "../algebra" }
r1cs-core = { path = "../r1cs-core" }
derivative = "1"
radix_trie = "0.1"
algebra = { path = "../algebra", default-features = false }
r1cs-core = { path = "../r1cs-core", default-features = false }
derivative = { version = "1", features = ["use_core"] }

[dev-dependencies]
rand = { version = "0.7" }
rand = { version = "0.7", default-features = false }
rand_xorshift = { version = "0.2" }

[features]
default = ["std"]
std = ["algebra/std", "r1cs-core/std"]
@@ -1,6 +1,7 @@
use crate::Vec;
use algebra::Field;
use core::borrow::Borrow;
use r1cs_core::{ConstraintSystem, SynthesisError};
use std::borrow::Borrow;

pub trait AllocGadget<V, ConstraintF: Field>
where
@@ -1,8 +1,8 @@
use algebra::{BitIterator, Field, FpParameters, PrimeField};

use crate::{prelude::*, Assignment};
use crate::{prelude::*, Assignment, Vec};
use core::borrow::Borrow;
use r1cs_core::{ConstraintSystem, ConstraintVar, LinearCombination, SynthesisError, Variable};
use std::borrow::Borrow;

/// Represents a variable in the constraint system which is guaranteed
/// to be either zero or one.
@@ -835,10 +835,10 @@ mod test {
use super::{AllocatedBit, Boolean};
use crate::{prelude::*, test_constraint_system::TestConstraintSystem};
use algebra::{fields::bls12_381::Fr, BitIterator, Field, One, PrimeField, UniformRand, Zero};
use core::str::FromStr;
use r1cs_core::ConstraintSystem;
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
use std::str::FromStr;

#[test]
fn test_boolean_to_byte() {
@@ -1,4 +1,7 @@
use crate::bits::{boolean::Boolean, uint8::UInt8};
use crate::{
bits::{boolean::Boolean, uint8::UInt8},
Vec,
};
use algebra::Field;
use r1cs_core::{ConstraintSystem, SynthesisError};

@@ -5,7 +5,7 @@ use r1cs_core::{ConstraintSystem, LinearCombination, SynthesisError};
use crate::{
boolean::{AllocatedBit, Boolean},
prelude::*,
Assignment,
Assignment, Vec,
};

/// Represents an interpretation of 32 `Boolean` objects as an
@@ -343,7 +343,7 @@ impl<ConstraintF: Field> ConditionalEqGadget<ConstraintF> for UInt32 {
#[cfg(test)]
mod test {
use super::UInt32;
use crate::{bits::boolean::Boolean, test_constraint_system::TestConstraintSystem};
use crate::{bits::boolean::Boolean, test_constraint_system::TestConstraintSystem, Vec};
use algebra::{fields::bls12_381::Fr, One, Zero};
use r1cs_core::ConstraintSystem;
use rand::{Rng, SeedableRng};
@@ -2,8 +2,8 @@ use algebra::{Field, FpParameters, PrimeField, ToConstraintField};

use r1cs_core::{ConstraintSystem, SynthesisError};

use crate::{boolean::AllocatedBit, fields::fp::FpGadget, prelude::*, Assignment};
use std::borrow::Borrow;
use crate::{boolean::AllocatedBit, fields::fp::FpGadget, prelude::*, Assignment, Vec};
use core::borrow::Borrow;

/// Represents an interpretation of 8 `Boolean` objects as an
/// unsigned integer.
@@ -297,7 +297,7 @@ impl<ConstraintF: Field> AllocGadget<u8, ConstraintF> for UInt8 {
#[cfg(test)]
mod test {
use super::UInt8;
use crate::{prelude::*, test_constraint_system::TestConstraintSystem};
use crate::{prelude::*, test_constraint_system::TestConstraintSystem, Vec};
use algebra::fields::bls12_381::Fr;
use r1cs_core::ConstraintSystem;
use rand::{Rng, SeedableRng};
@@ -5,9 +5,9 @@ use r1cs_core::{
LinearCombination, SynthesisError,
};

use std::borrow::Borrow;
use core::borrow::Borrow;

use crate::{boolean::AllocatedBit, prelude::*, Assignment};
use crate::{boolean::AllocatedBit, prelude::*, Assignment, Vec};

#[derive(Debug)]
pub struct FpGadget<F: PrimeField> {
@@ -8,9 +8,9 @@ use algebra::{
},
BitIterator, Field, One, PrimeField,
};
use std::{borrow::Borrow, marker::PhantomData};
use core::{borrow::Borrow, marker::PhantomData};

use crate::{prelude::*, Assignment};
use crate::{prelude::*, Assignment, Vec};

type Fp2Gadget<P, ConstraintF> = super::fp2::Fp2Gadget<
<<P as Fp12Parameters>::Fp6Params as Fp6Parameters>::Fp2Params,
@@ -2,10 +2,10 @@ use algebra::{
fields::{Fp2, Fp2Parameters},
Field, PrimeField,
};
use core::{borrow::Borrow, marker::PhantomData};
use r1cs_core::{ConstraintSystem, ConstraintVar, SynthesisError};
use std::{borrow::Borrow, marker::PhantomData};

use crate::{fields::fp::FpGadget, prelude::*, Assignment};
use crate::{fields::fp::FpGadget, prelude::*, Assignment, Vec};

#[derive(Derivative)]
#[derivative(Debug(bound = "P: Fp2Parameters, ConstraintF: PrimeField"))]
@@ -5,10 +5,10 @@ use algebra::{
},
PrimeField,
};
use core::{borrow::Borrow, marker::PhantomData};
use r1cs_core::{ConstraintSystem, ConstraintVar, SynthesisError};
use std::{borrow::Borrow, marker::PhantomData};

use crate::{prelude::*, Assignment};
use crate::{prelude::*, Assignment, Vec};

type Fp2Gadget<P, ConstraintF> =
super::fp2::Fp2Gadget<<P as Fp6Parameters>::Fp2Params, ConstraintF>;
@@ -1,7 +1,6 @@
// use std::ops::{Mul, MulAssign};
use algebra::Field;
use core::fmt::Debug;
use r1cs_core::{ConstraintSystem, SynthesisError};
use std::fmt::Debug;

use crate::prelude::*;

@@ -237,11 +236,11 @@ pub trait FieldGadget<F: Field, ConstraintF: Field>:

#[cfg(test)]
mod test {
use rand::{self, thread_rng, SeedableRng};
use rand::{self, SeedableRng};
use rand_xorshift::XorShiftRng;

use crate::{prelude::*, test_constraint_system::TestConstraintSystem};
use algebra::{BitIterator, Field, UniformRand};
use crate::{prelude::*, test_constraint_system::TestConstraintSystem, Vec};
use algebra::{test_rng, BitIterator, Field, UniformRand};
use r1cs_core::ConstraintSystem;

fn field_test<
@@ -418,15 +417,15 @@ mod test {
// a * a * a = a^3
let mut constants = [FE::zero(); 4];
for c in &mut constants {
*c = UniformRand::rand(&mut thread_rng());
*c = UniformRand::rand(&mut test_rng());
println!("Current c[i]: {:?}", c);
}
let bits = [Boolean::constant(false), Boolean::constant(true)];
let lookup_result =
F::two_bit_lookup(cs.ns(|| "Lookup"), &bits, constants.as_ref()).unwrap();
assert_eq!(lookup_result.get_value().unwrap(), constants[2]);

let negone: FE = UniformRand::rand(&mut thread_rng());
let negone: FE = UniformRand::rand(&mut test_rng());

let n = F::alloc(&mut cs.ns(|| "alloc new var"), || Ok(negone)).unwrap();
let _ = n.to_bytes(&mut cs.ns(|| "ToBytes")).unwrap();
@@ -525,7 +524,7 @@ mod test {

let mut cs = TestConstraintSystem::<Fq>::new();

let mut rng = thread_rng();
let mut rng = test_rng();

let a = FqGadget::alloc(&mut cs.ns(|| "generate_a"), || Ok(Fq::rand(&mut rng))).unwrap();
let b = FqGadget::alloc(&mut cs.ns(|| "generate_b"), || Ok(Fq::rand(&mut rng))).unwrap();
@@ -543,7 +542,7 @@ mod test {

let mut cs = TestConstraintSystem::<Fq>::new();

let mut rng = thread_rng();
let mut rng = test_rng();

let a = FqGadget::alloc(&mut cs.ns(|| "generate_a"), || Ok(Fq::rand(&mut rng))).unwrap();
let b = FqGadget::alloc(&mut cs.ns(|| "generate_b"), || Ok(Fq::rand(&mut rng))).unwrap();
@@ -12,14 +12,14 @@ pub type G2PreparedGadget = Bls12G2PreparedGadget<Bls12_377Parameters>;

#[cfg(test)]
mod test {
use rand;
use rand::Rng;

use super::{G1Gadget, G2Gadget};
use crate::{prelude::*, test_constraint_system::TestConstraintSystem};
use crate::{prelude::*, test_constraint_system::TestConstraintSystem, Vec};
use algebra::{
curves::bls12_377::{G1Projective as G1, G2Projective as G2},
fields::bls12_377::{Fq, Fr},
AffineCurve, BitIterator, PrimeField, ProjectiveCurve,
test_rng, AffineCurve, BitIterator, PrimeField, ProjectiveCurve,
};
use r1cs_core::ConstraintSystem;

@@ -33,8 +33,9 @@ mod test {
.unwrap()
.into();

let a: G1 = rand::random();
let b: G1 = rand::random();
let mut rng = test_rng();
let a: G1 = rng.gen();
let b: G1 = rng.gen();
let gadget_a = G1Gadget::alloc(&mut cs.ns(|| "a"), || Ok(a)).unwrap();
let gadget_b = G1Gadget::alloc(&mut cs.ns(|| "b"), || Ok(b)).unwrap();
let alloc_cost = cs.num_constraints();
@@ -65,8 +66,9 @@ mod test {
.unwrap()
.into();

let a: G2 = rand::random();
let b: G2 = rand::random();
let mut rng = test_rng();
let a: G2 = rng.gen();
let b: G2 = rng.gen();
let gadget_a = G2Gadget::alloc(&mut cs.ns(|| "a"), || Ok(a)).unwrap();
let gadget_b = G2Gadget::alloc(&mut cs.ns(|| "b"), || Ok(b)).unwrap();
let alloc_cost = cs.num_constraints();
@@ -164,8 +166,9 @@ mod test {
fn bls12_g2_gadget_test() {
let mut cs = TestConstraintSystem::<Fq>::new();

let a: G2 = rand::random();
let b: G2 = rand::random();
let mut rng = test_rng();
let a: G2 = rng.gen();
let b: G2 = rng.gen();
let a_affine = a.into_affine();
let b_affine = b.into_affine();

@@ -9,9 +9,10 @@ use crate::{
fields::{fp::FpGadget, fp2::Fp2Gadget, FieldGadget},
groups::curves::short_weierstrass::AffineGadget,
prelude::*,
Vec,
};

use std::fmt::Debug;
use core::fmt::Debug;

pub mod bls12_377;

@@ -5,10 +5,10 @@ use algebra::{
},
AffineCurve, BitIterator, Field, One, PrimeField, ProjectiveCurve, Zero,
};
use core::{borrow::Borrow, marker::PhantomData, ops::Neg};
use r1cs_core::{ConstraintSystem, SynthesisError};
use std::{borrow::Borrow, marker::PhantomData, ops::Neg};

use crate::{prelude::*, Assignment};
use crate::{prelude::*, Assignment, Vec};

pub mod bls12;

@@ -8,9 +8,9 @@ use algebra::{

use r1cs_core::{ConstraintSystem, SynthesisError};

use crate::prelude::*;
use crate::{prelude::*, Vec};

use std::{borrow::Borrow, marker::PhantomData};
use core::{borrow::Borrow, marker::PhantomData};

pub mod edwards_bls12;
pub mod edwards_sw6;
@@ -39,7 +39,7 @@ mod montgomery_affine_impl {
use super::*;
use crate::Assignment;
use algebra::{twisted_edwards_extended::GroupAffine, Field};
use std::ops::{AddAssign, MulAssign, SubAssign};
use core::ops::{AddAssign, MulAssign, SubAssign};

impl<P: TEModelParameters, ConstraintF: Field, F: FieldGadget<P::BaseField, ConstraintF>>
MontgomeryAffineGadget<P, ConstraintF, F>
@@ -264,7 +264,7 @@ mod affine_impl {
use super::*;
use crate::Assignment;
use algebra::{curves::AffineCurve, Field, PrimeField};
use std::ops::Neg;
use core::ops::Neg;

impl<P, ConstraintF, F> GroupGadget<TEAffine<P>, ConstraintF> for AffineGadget<P, ConstraintF, F>
where
@@ -662,12 +662,12 @@ mod affine_impl {

mod projective_impl {
use super::*;
use crate::Assignment;
use crate::{Assignment, Vec};
use algebra::{
curves::twisted_edwards_extended::GroupProjective as TEProjective, AffineCurve, Field,
PrimeField, ProjectiveCurve,
};
use std::ops::Neg;
use core::ops::Neg;

impl<P, ConstraintF, F> GroupGadget<TEProjective<P>, ConstraintF>
for AffineGadget<P, ConstraintF, F>
@@ -1,11 +1,9 @@
use rand::thread_rng;

use crate::{groups::test::group_test, prelude::*};

use crate::{groups::test::group_test, prelude::*, Vec};
use algebra::{
curves::{models::TEModelParameters, twisted_edwards_extended::GroupAffine as TEAffine},
BitIterator, Field, Group, PrimeField, UniformRand,
test_rng, BitIterator, Field, Group, PrimeField, UniformRand,
};
use rand::Rng;

use r1cs_core::ConstraintSystem;

@@ -16,8 +14,8 @@ where
GG: GroupGadget<TEAffine<P>, ConstraintF, Value = TEAffine<P>>,
CS: ConstraintSystem<ConstraintF>,
{
let a: TEAffine<P> = UniformRand::rand(&mut thread_rng());
let b: TEAffine<P> = UniformRand::rand(&mut thread_rng());
let a: TEAffine<P> = UniformRand::rand(&mut test_rng());
let b: TEAffine<P> = UniformRand::rand(&mut test_rng());
let gadget_a = GG::alloc(&mut cs.ns(|| "a"), || Ok(a)).unwrap();
let gadget_b = GG::alloc(&mut cs.ns(|| "b"), || Ok(b)).unwrap();
assert_eq!(gadget_a.get_value().unwrap(), a);
@@ -29,7 +27,7 @@ where
);

// Check mul_bits
let scalar: <TEAffine<P> as Group>::ScalarField = UniformRand::rand(&mut thread_rng());
let scalar: <TEAffine<P> as Group>::ScalarField = UniformRand::rand(&mut test_rng());
let native_result = a.mul(&scalar);

let mut scalar: Vec<bool> = BitIterator::new(scalar.into_repr()).collect();
@@ -57,8 +55,9 @@ where
.unwrap()
.into();

let a: TEAffine<P> = rand::random();
let b: TEAffine<P> = rand::random();
let mut rng = test_rng();
let a: TEAffine<P> = rng.gen();
let b: TEAffine<P> = rng.gen();
let gadget_a = GG::alloc(&mut cs.ns(|| "a"), || Ok(a)).unwrap();
let gadget_b = GG::alloc(&mut cs.ns(|| "b"), || Ok(b)).unwrap();
let alloc_cost = cs.num_constraints();

0 comments on commit cb4ae26

Please sign in to comment.
You can’t perform that action at this time.