r1cs-std works with no-std (#92)
jump to
@@ -22,11 +22,14 @@
################################# 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 crate::{ boolean::{AllocatedBit, Boolean}, prelude::*, - Assignment, + Assignment, Vec, }; /// Represents an interpretation of 32 `Boolean` objects as an@@ -343,7 +343,7 @@
#[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 @@
#[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 @@ ConstraintVar::{self, *},
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 @@ Fp2Parameters,
}, 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 @@ Field, Fp2Parameters,
}, 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 @@ }
#[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,7 +417,7 @@
// 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)];@@ -426,7 +425,7 @@ 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 @@ use algebra::fields::jubjub::fq::Fq;
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 @@ use algebra::fields::edwards_bls12::fq::Fq;
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 @@ let bit = AllocatedBit::alloc(&mut cs.ns(|| "bool"), || Ok(true))
.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 @@ let bit = AllocatedBit::alloc(&mut cs.ns(|| "bool"), || Ok(true))
.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 @@ #[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 @@ SWModelParameters,
}, 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 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 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 @@ P: TEModelParameters,
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 @@ gadget_b,
); // 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 @@ let bit = AllocatedBit::alloc(&mut cs.ns(|| "bool"), || Ok(true))
.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();
@@ -2,7 +2,7 @@ use crate::prelude::*;
use algebra::{Field, Group}; use r1cs_core::{ConstraintSystem, SynthesisError}; -use std::{borrow::Borrow, fmt::Debug}; +use core::{borrow::Borrow, fmt::Debug}; pub mod curves;@@ -163,12 +163,12 @@ }
#[cfg(test)] mod test { - use algebra::Field; + use algebra::{test_rng, Field}; use r1cs_core::ConstraintSystem; + use rand::Rng; use crate::{prelude::*, test_constraint_system::TestConstraintSystem}; use algebra::groups::Group; - use rand; pub(crate) fn group_test< ConstraintF: Field,@@ -226,8 +226,9 @@ use algebra::{curves::jubjub::JubJubProjective, fields::jubjub::fq::Fq};
let mut cs = TestConstraintSystem::<Fq>::new(); - let a: JubJubProjective = rand::random(); - let b: JubJubProjective = rand::random(); + let mut rng = test_rng(); + let a: JubJubProjective = rng.gen(); + let b: JubJubProjective = rng.gen(); let a = JubJubGadget::alloc(&mut cs.ns(|| "generate_a"), || Ok(a)).unwrap(); let b = JubJubGadget::alloc(&mut cs.ns(|| "generate_b"), || Ok(b)).unwrap();
@@ -1,3 +1,4 @@
+#![cfg_attr(not(feature = "std"), no_std)] #![deny( unused_import_braces, unused_qualifications,@@ -33,11 +34,31 @@ unsafe_code
)] #![forbid(unsafe_code)] +#[cfg(all(test, not(feature = "std")))] +#[macro_use] +extern crate std; + +#[cfg(not(feature = "std"))] +extern crate alloc as ralloc; + #[macro_use] extern crate algebra; #[macro_use] extern crate derivative; + +/// used by test_constraint_system +#[cfg(not(feature = "std"))] +macro_rules! println { + () => {}; + ($($arg: tt)*) => {}; +} + +#[cfg(not(feature = "std"))] +use ralloc::{collections::BTreeMap, string::String, vec::Vec}; + +#[cfg(feature = "std")] +use std::{collections::BTreeMap, string::String, vec::Vec}; pub mod test_constraint_system;
@@ -17,7 +17,7 @@ PairingCurve,
}, fields::{fp12_2over3over2::Fp12, BitIterator}, }; -use std::marker::PhantomData; +use core::marker::PhantomData; pub mod bls12_377;
@@ -1,7 +1,7 @@
use crate::prelude::*; use algebra::{Field, PairingEngine}; +use core::fmt::Debug; use r1cs_core::{ConstraintSystem, SynthesisError}; -use std::fmt::Debug; pub mod bls12; pub use self::bls12::bls12_377;@@ -57,8 +57,7 @@ }
#[cfg(test)] mod test { - // use rand; - use crate::test_constraint_system::TestConstraintSystem; + use crate::{test_constraint_system::TestConstraintSystem, Vec}; use algebra::{BitIterator, Field, One}; use r1cs_core::ConstraintSystem;@@ -79,7 +78,7 @@ pairing::PairingGadget as _,
prelude::*, }; use algebra::curves::bls12_377::{Bls12_377, G1Projective, G2Projective}; - use std::ops::Mul; + use core::ops::Mul; let mut cs = TestConstraintSystem::<Fq>::new();
@@ -1,7 +1,6 @@
+use crate::{BTreeMap, String, Vec}; use algebra::Field; use r1cs_core::{ConstraintSystem, Index, LinearCombination, SynthesisError, Variable}; - -use radix_trie::Trie; #[derive(Debug)] enum NamedObject {@@ -12,7 +11,7 @@ }
/// Constraint system for testing purposes. pub struct TestConstraintSystem<ConstraintF: Field> { - named_objects: Trie<String, NamedObject>, + named_objects: BTreeMap<String, NamedObject>, current_namespace: Vec<String>, pub constraints: Vec<( LinearCombination<ConstraintF>,@@ -48,7 +47,7 @@ }
impl<ConstraintF: Field> TestConstraintSystem<ConstraintF> { pub fn new() -> TestConstraintSystem<ConstraintF> { - let mut map = Trie::new(); + let mut map = BTreeMap::new(); map.insert( "ONE".into(), NamedObject::Var(TestConstraintSystem::<ConstraintF>::one()),@@ -63,6 +62,7 @@ aux: vec![],
} } + #[allow(unused_variables)] pub fn print_named_objects(&self) { for &(_, _, _, ref name) in &self.constraints { println!("{}", name);