Browse Source

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

master
Marek Kotewicz 4 years ago
committed by GitHub
parent
commit
9621bd34a4
21 changed files with 104 additions and 74 deletions
  1. +8
    -5
      r1cs-std/Cargo.toml
  2. +2
    -1
      r1cs-std/src/alloc.rs
  3. +3
    -3
      r1cs-std/src/bits/boolean.rs
  4. +4
    -1
      r1cs-std/src/bits/mod.rs
  5. +2
    -2
      r1cs-std/src/bits/uint32.rs
  6. +3
    -3
      r1cs-std/src/bits/uint8.rs
  7. +2
    -2
      r1cs-std/src/fields/fp.rs
  8. +2
    -2
      r1cs-std/src/fields/fp12.rs
  9. +2
    -2
      r1cs-std/src/fields/fp2.rs
  10. +2
    -2
      r1cs-std/src/fields/fp6_3over2.rs
  11. +8
    -9
      r1cs-std/src/fields/mod.rs
  12. +12
    -9
      r1cs-std/src/groups/curves/short_weierstrass/bls12/bls12_377.rs
  13. +2
    -1
      r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs
  14. +2
    -2
      r1cs-std/src/groups/curves/short_weierstrass/mod.rs
  15. +6
    -6
      r1cs-std/src/groups/curves/twisted_edwards/mod.rs
  16. +9
    -10
      r1cs-std/src/groups/curves/twisted_edwards/test.rs
  17. +6
    -5
      r1cs-std/src/groups/mod.rs
  18. +21
    -0
      r1cs-std/src/lib.rs
  19. +1
    -1
      r1cs-std/src/pairing/bls12/mod.rs
  20. +3
    -4
      r1cs-std/src/pairing/mod.rs
  21. +4
    -4
      r1cs-std/src/test_constraint_system.rs

+ 8
- 5
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"]

+ 2
- 1
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<V, ConstraintF: Field>
where

+ 3
- 3
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() {

+ 4
- 1
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};

+ 2
- 2
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};

+ 3
- 3
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};

+ 2
- 2
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<F: PrimeField> {

+ 2
- 2
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<P, ConstraintF> = super::fp2::Fp2Gadget<
<<P as Fp12Parameters>::Fp6Params as Fp6Parameters>::Fp2Params,

+ 2
- 2
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"))]

+ 2
- 2
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<P, ConstraintF> =
super::fp2::Fp2Gadget<<P as Fp6Parameters>::Fp2Params, ConstraintF>;

+ 8
- 9
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::<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
- 9
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::<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();

+ 2
- 1
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;

+ 2
- 2
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;

+ 6
- 6
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<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>

+ 9
- 10
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<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();

+ 6
- 5
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::<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();

+ 21
- 0
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;

+ 1
- 1
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;

+ 3
- 4
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::<Fq>::new();

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

Loading…
Cancel
Save