diff --git a/r1cs-std/Cargo.toml b/r1cs-std/Cargo.toml index a797398..b23d90d 100644 --- a/r1cs-std/Cargo.toml +++ b/r1cs-std/Cargo.toml @@ -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"] diff --git a/r1cs-std/src/alloc.rs b/r1cs-std/src/alloc.rs index 13f3d02..139820e 100644 --- a/r1cs-std/src/alloc.rs +++ b/r1cs-std/src/alloc.rs @@ -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 where diff --git a/r1cs-std/src/bits/boolean.rs b/r1cs-std/src/bits/boolean.rs index 3a8a820..c5c0453 100644 --- a/r1cs-std/src/bits/boolean.rs +++ b/r1cs-std/src/bits/boolean.rs @@ -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() { diff --git a/r1cs-std/src/bits/mod.rs b/r1cs-std/src/bits/mod.rs index e859d18..bcaa0a9 100644 --- a/r1cs-std/src/bits/mod.rs +++ b/r1cs-std/src/bits/mod.rs @@ -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}; diff --git a/r1cs-std/src/bits/uint32.rs b/r1cs-std/src/bits/uint32.rs index 4f1fb2c..806ad13 100644 --- a/r1cs-std/src/bits/uint32.rs +++ b/r1cs-std/src/bits/uint32.rs @@ -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 ConditionalEqGadget 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}; diff --git a/r1cs-std/src/bits/uint8.rs b/r1cs-std/src/bits/uint8.rs index 5ca819a..e355cf6 100644 --- a/r1cs-std/src/bits/uint8.rs +++ b/r1cs-std/src/bits/uint8.rs @@ -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 AllocGadget 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}; diff --git a/r1cs-std/src/fields/fp.rs b/r1cs-std/src/fields/fp.rs index fdc685f..85c4d1c 100644 --- a/r1cs-std/src/fields/fp.rs +++ b/r1cs-std/src/fields/fp.rs @@ -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 { diff --git a/r1cs-std/src/fields/fp12.rs b/r1cs-std/src/fields/fp12.rs index ffe89a6..de6d9e6 100644 --- a/r1cs-std/src/fields/fp12.rs +++ b/r1cs-std/src/fields/fp12.rs @@ -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 = super::fp2::Fp2Gadget< <

::Fp6Params as Fp6Parameters>::Fp2Params, diff --git a/r1cs-std/src/fields/fp2.rs b/r1cs-std/src/fields/fp2.rs index 4b4cb15..3ba5fca 100644 --- a/r1cs-std/src/fields/fp2.rs +++ b/r1cs-std/src/fields/fp2.rs @@ -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"))] diff --git a/r1cs-std/src/fields/fp6_3over2.rs b/r1cs-std/src/fields/fp6_3over2.rs index 3a6f93d..9da0f5d 100644 --- a/r1cs-std/src/fields/fp6_3over2.rs +++ b/r1cs-std/src/fields/fp6_3over2.rs @@ -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 = super::fp2::Fp2Gadget<

::Fp2Params, ConstraintF>; diff --git a/r1cs-std/src/fields/mod.rs b/r1cs-std/src/fields/mod.rs index cb789e1..bcd6839 100644 --- a/r1cs-std/src/fields/mod.rs +++ b/r1cs-std/src/fields/mod.rs @@ -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: #[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 @@ 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)]; @@ -426,7 +425,7 @@ mod test { 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::::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::::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(); diff --git a/r1cs-std/src/groups/curves/short_weierstrass/bls12/bls12_377.rs b/r1cs-std/src/groups/curves/short_weierstrass/bls12/bls12_377.rs index 4984362..dee30d3 100644 --- a/r1cs-std/src/groups/curves/short_weierstrass/bls12/bls12_377.rs +++ b/r1cs-std/src/groups/curves/short_weierstrass/bls12/bls12_377.rs @@ -12,14 +12,14 @@ pub type G2PreparedGadget = Bls12G2PreparedGadget; #[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::::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(); diff --git a/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs b/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs index eb7b184..0304bb4 100644 --- a/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs +++ b/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs @@ -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; diff --git a/r1cs-std/src/groups/curves/short_weierstrass/mod.rs b/r1cs-std/src/groups/curves/short_weierstrass/mod.rs index 69163a9..f6f4a1f 100644 --- a/r1cs-std/src/groups/curves/short_weierstrass/mod.rs +++ b/r1cs-std/src/groups/curves/short_weierstrass/mod.rs @@ -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; diff --git a/r1cs-std/src/groups/curves/twisted_edwards/mod.rs b/r1cs-std/src/groups/curves/twisted_edwards/mod.rs index 0a495d3..9fa8545 100644 --- a/r1cs-std/src/groups/curves/twisted_edwards/mod.rs +++ b/r1cs-std/src/groups/curves/twisted_edwards/mod.rs @@ -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> MontgomeryAffineGadget @@ -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 GroupGadget, ConstraintF> for AffineGadget 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 GroupGadget, ConstraintF> for AffineGadget diff --git a/r1cs-std/src/groups/curves/twisted_edwards/test.rs b/r1cs-std/src/groups/curves/twisted_edwards/test.rs index 8720746..ea76c92 100644 --- a/r1cs-std/src/groups/curves/twisted_edwards/test.rs +++ b/r1cs-std/src/groups/curves/twisted_edwards/test.rs @@ -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, ConstraintF, Value = TEAffine

>, CS: ConstraintSystem, { - let a: TEAffine

= UniformRand::rand(&mut thread_rng()); - let b: TEAffine

= UniformRand::rand(&mut thread_rng()); + let a: TEAffine

= UniformRand::rand(&mut test_rng()); + let b: TEAffine

= 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: as Group>::ScalarField = UniformRand::rand(&mut thread_rng()); + let scalar: as Group>::ScalarField = UniformRand::rand(&mut test_rng()); let native_result = a.mul(&scalar); let mut scalar: Vec = BitIterator::new(scalar.into_repr()).collect(); @@ -57,8 +55,9 @@ where .unwrap() .into(); - let a: TEAffine

= rand::random(); - let b: TEAffine

= rand::random(); + let mut rng = test_rng(); + let a: TEAffine

= rng.gen(); + let b: TEAffine

= 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(); diff --git a/r1cs-std/src/groups/mod.rs b/r1cs-std/src/groups/mod.rs index 01f2902..58207f8 100644 --- a/r1cs-std/src/groups/mod.rs +++ b/r1cs-std/src/groups/mod.rs @@ -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 @@ pub trait GroupGadget: #[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 @@ mod test { let mut cs = TestConstraintSystem::::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(); diff --git a/r1cs-std/src/lib.rs b/r1cs-std/src/lib.rs index 3425fa1..f6cf442 100644 --- a/r1cs-std/src/lib.rs +++ b/r1cs-std/src/lib.rs @@ -1,3 +1,4 @@ +#![cfg_attr(not(feature = "std"), no_std)] #![deny( unused_import_braces, unused_qualifications, @@ -33,12 +34,32 @@ )] #![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; pub mod bits; diff --git a/r1cs-std/src/pairing/bls12/mod.rs b/r1cs-std/src/pairing/bls12/mod.rs index 421e2c2..0c03694 100644 --- a/r1cs-std/src/pairing/bls12/mod.rs +++ b/r1cs-std/src/pairing/bls12/mod.rs @@ -17,7 +17,7 @@ use algebra::{ }, fields::{fp12_2over3over2::Fp12, BitIterator}, }; -use std::marker::PhantomData; +use core::marker::PhantomData; pub mod bls12_377; diff --git a/r1cs-std/src/pairing/mod.rs b/r1cs-std/src/pairing/mod.rs index 5ff8473..1f5fd09 100644 --- a/r1cs-std/src/pairing/mod.rs +++ b/r1cs-std/src/pairing/mod.rs @@ -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 @@ pub trait PairingGadget { #[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 @@ mod test { prelude::*, }; use algebra::curves::bls12_377::{Bls12_377, G1Projective, G2Projective}; - use std::ops::Mul; + use core::ops::Mul; let mut cs = TestConstraintSystem::::new(); diff --git a/r1cs-std/src/test_constraint_system.rs b/r1cs-std/src/test_constraint_system.rs index f1a98c2..3fb955f 100644 --- a/r1cs-std/src/test_constraint_system.rs +++ b/r1cs-std/src/test_constraint_system.rs @@ -1,8 +1,7 @@ +use crate::{BTreeMap, String, Vec}; use algebra::Field; use r1cs_core::{ConstraintSystem, Index, LinearCombination, SynthesisError, Variable}; -use radix_trie::Trie; - #[derive(Debug)] enum NamedObject { Constraint(usize), @@ -12,7 +11,7 @@ enum NamedObject { /// Constraint system for testing purposes. pub struct TestConstraintSystem { - named_objects: Trie, + named_objects: BTreeMap, current_namespace: Vec, pub constraints: Vec<( LinearCombination, @@ -48,7 +47,7 @@ impl TestConstraintSystem { impl TestConstraintSystem { pub fn new() -> TestConstraintSystem { - let mut map = Trie::new(); + let mut map = BTreeMap::new(); map.insert( "ONE".into(), NamedObject::Var(TestConstraintSystem::::one()), @@ -63,6 +62,7 @@ impl TestConstraintSystem { } } + #[allow(unused_variables)] pub fn print_named_objects(&self) { for &(_, _, _, ref name) in &self.constraints { println!("{}", name);