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

This commit is contained in:
Marek Kotewicz
2020-02-05 22:55:38 +01:00
committed by GitHub
parent a77972bf9a
commit 9621bd34a4
21 changed files with 104 additions and 74 deletions

View File

@@ -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"]

View File

@@ -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

View File

@@ -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() {

View File

@@ -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};

View File

@@ -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};

View File

@@ -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};

View File

@@ -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> {

View File

@@ -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,

View File

@@ -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"))]

View File

@@ -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>;

View File

@@ -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,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::<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();

View File

@@ -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();

View File

@@ -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;

View File

@@ -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;

View File

@@ -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>

View File

@@ -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();

View File

@@ -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<G: Group, ConstraintF: Field>:
#[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::<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();

View File

@@ -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;

View File

@@ -17,7 +17,7 @@ use algebra::{
},
fields::{fp12_2over3over2::Fp12, BitIterator},
};
use std::marker::PhantomData;
use core::marker::PhantomData;
pub mod bls12_377;

View File

@@ -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<PairingE: PairingEngine, ConstraintF: Field> {
#[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::<Fq>::new();

View File

@@ -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<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> {
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 @@ impl<ConstraintF: Field> TestConstraintSystem<ConstraintF> {
}
}
#[allow(unused_variables)]
pub fn print_named_objects(&self) {
for &(_, _, _, ref name) in &self.constraints {
println!("{}", name);