From 0a507b0a5f31579520ccb346ae957768a91b9811 Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Tue, 17 Sep 2019 01:35:23 -0700 Subject: [PATCH] Upgrade ff-fft, gm17, and r1cs-std --- r1cs-std/Cargo.toml | 1 + r1cs-std/src/bits/boolean.rs | 6 ++++-- r1cs-std/src/bits/uint32.rs | 13 +++++++------ r1cs-std/src/bits/uint8.rs | 7 ++++--- r1cs-std/src/fields/mod.rs | 13 +++++++------ .../curves/short_weierstrass/bls12/bls12_377.rs | 6 ++++-- r1cs-std/src/groups/curves/twisted_edwards/test.rs | 10 +++++----- 7 files changed, 32 insertions(+), 24 deletions(-) diff --git a/r1cs-std/Cargo.toml b/r1cs-std/Cargo.toml index e793f1a..a797398 100644 --- a/r1cs-std/Cargo.toml +++ b/r1cs-std/Cargo.toml @@ -29,3 +29,4 @@ radix_trie = "0.1" [dev-dependencies] rand = { version = "0.7" } +rand_xorshift = { version = "0.2" } diff --git a/r1cs-std/src/bits/boolean.rs b/r1cs-std/src/bits/boolean.rs index e455c3d..ed92527 100644 --- a/r1cs-std/src/bits/boolean.rs +++ b/r1cs-std/src/bits/boolean.rs @@ -739,7 +739,9 @@ mod test { prelude::* }; use algebra::{fields::bls12_381::Fr, BitIterator, Field, PrimeField}; - use rand::{Rand, SeedableRng, XorShiftRng}; + use algebra::UniformRand; +use rand::SeedableRng; +use rand_xorshift::XorShiftRng; use r1cs_core::ConstraintSystem; use std::str::FromStr; @@ -1736,7 +1738,7 @@ mod test { assert!(!cs.is_satisfied()); } - let mut rng = XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for _ in 0..1000 { let r = Fr::rand(&mut rng); diff --git a/r1cs-std/src/bits/uint32.rs b/r1cs-std/src/bits/uint32.rs index 7a3b3dc..855a171 100644 --- a/r1cs-std/src/bits/uint32.rs +++ b/r1cs-std/src/bits/uint32.rs @@ -340,12 +340,13 @@ mod test { use super::UInt32; use crate::{bits::boolean::Boolean, test_constraint_system::TestConstraintSystem}; use algebra::fields::{bls12_381::Fr, Field}; - use rand::{Rng, SeedableRng, XorShiftRng}; + use rand::{Rng, SeedableRng}; + use rand_xorshift::XorShiftRng; use r1cs_core::ConstraintSystem; #[test] fn test_uint32_from_bits() { - let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0653]); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for _ in 0..1000 { let v = (0..32) @@ -377,7 +378,7 @@ mod test { #[test] fn test_uint32_xor() { - let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0653]); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for _ in 0..1000 { let mut cs = TestConstraintSystem::::new(); @@ -419,7 +420,7 @@ mod test { #[test] fn test_uint32_addmany_constants() { - let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for _ in 0..1000 { let mut cs = TestConstraintSystem::::new(); @@ -454,7 +455,7 @@ mod test { #[test] fn test_uint32_addmany() { - let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for _ in 0..1000 { let mut cs = TestConstraintSystem::::new(); @@ -505,7 +506,7 @@ mod test { #[test] fn test_uint32_rotr() { - let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); let mut num = rng.gen(); diff --git a/r1cs-std/src/bits/uint8.rs b/r1cs-std/src/bits/uint8.rs index 25ddaf8..5a46dff 100644 --- a/r1cs-std/src/bits/uint8.rs +++ b/r1cs-std/src/bits/uint8.rs @@ -294,7 +294,8 @@ mod test { use super::UInt8; use crate::{prelude::*, test_constraint_system::TestConstraintSystem}; use algebra::fields::bls12_381::Fr; - use rand::{Rng, SeedableRng, XorShiftRng}; + use rand::{Rng, SeedableRng}; + use rand_xorshift::XorShiftRng; use r1cs_core::ConstraintSystem; #[test] @@ -323,7 +324,7 @@ mod test { #[test] fn test_uint8_from_bits() { - let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0653]); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for _ in 0..1000 { let v = (0..8) @@ -355,7 +356,7 @@ mod test { #[test] fn test_uint8_xor() { - let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0653]); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for _ in 0..1000 { let mut cs = TestConstraintSystem::::new(); diff --git a/r1cs-std/src/fields/mod.rs b/r1cs-std/src/fields/mod.rs index c3b5313..f03bc9a 100644 --- a/r1cs-std/src/fields/mod.rs +++ b/r1cs-std/src/fields/mod.rs @@ -213,10 +213,11 @@ pub trait FieldGadget: #[cfg(test)] mod test { - use rand::{self, thread_rng, Rand, SeedableRng, XorShiftRng}; + use rand::{self, thread_rng, SeedableRng}; + use rand_xorshift::XorShiftRng; use crate::{prelude::*, test_constraint_system::TestConstraintSystem}; - use algebra::{fields::Field, BitIterator}; + use algebra::{UniformRand, Field, BitIterator}; use r1cs_core::ConstraintSystem; fn field_test, CS: ConstraintSystem>( @@ -388,7 +389,7 @@ mod test { // a * a * a = a^3 let mut constants = [FE::zero(); 4]; for c in &mut constants { - *c = rand::random(); + *c = UniformRand::rand(&mut thread_rng()); println!("Current c[i]: {:?}", c); } let bits = [Boolean::constant(false), Boolean::constant(true)]; @@ -396,7 +397,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 = rand::random(); + let negone: FE = UniformRand::rand(&mut thread_rng()); let n = F::alloc(&mut cs.ns(|| "alloc new var"), || Ok(negone)).unwrap(); let _ = n.to_bytes(&mut cs.ns(|| "ToBytes")).unwrap(); @@ -412,7 +413,7 @@ mod test { mut cs: CS, maxpower: usize, ) { - let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); for i in 0..(maxpower + 1) { let mut a = FE::rand(&mut rng); let mut a_gadget = F::alloc(cs.ns(|| format!("a_gadget_{:?}", i)), || Ok(a)).unwrap(); @@ -432,7 +433,7 @@ mod test { let mut cs = TestConstraintSystem::::new(); - let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0653]); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); 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 eb3b0ba..3239102 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 @@ -96,8 +96,10 @@ mod test { #[test] fn bls12_g1_gadget_test() { - use rand::{Rand, SeedableRng, XorShiftRng}; - let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); + use algebra::UniformRand; + use rand::SeedableRng; + use rand_xorshift::XorShiftRng; + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); let mut cs = TestConstraintSystem::::new(); diff --git a/r1cs-std/src/groups/curves/twisted_edwards/test.rs b/r1cs-std/src/groups/curves/twisted_edwards/test.rs index c959092..8b7c523 100644 --- a/r1cs-std/src/groups/curves/twisted_edwards/test.rs +++ b/r1cs-std/src/groups/curves/twisted_edwards/test.rs @@ -1,10 +1,10 @@ -use rand; +use rand::thread_rng; use crate::{prelude::*, groups::test::group_test}; use algebra::{ curves::{models::TEModelParameters, twisted_edwards_extended::GroupAffine as TEAffine}, - BitIterator, Group, PrimeField, Field, + BitIterator, Group, PrimeField, Field, UniformRand, }; use r1cs_core::ConstraintSystem; @@ -16,8 +16,8 @@ where GG: GroupGadget, ConstraintF, Value = TEAffine

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

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

= rand::random(); + let a: TEAffine

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

= UniformRand::rand(&mut thread_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 +29,7 @@ where ); // Check mul_bits - let scalar: as Group>::ScalarField = rand::random(); + let scalar: as Group>::ScalarField = UniformRand::rand(&mut thread_rng()); let native_result = a.mul(&scalar); let mut scalar: Vec = BitIterator::new(scalar.into_repr()).collect();