Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade rand crate dependency to 0.7 #91

Merged
merged 15 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 105 additions & 104 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion bellman/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ repository = "https://github.com/ebfull/bellman"
version = "0.1.0"

[dependencies]
rand = "0.4"
bit-vec = "0.4.4"
ff = { path = "../ff" }
futures = "0.1"
Expand All @@ -18,8 +17,12 @@ group = { path = "../group" }
num_cpus = { version = "1", optional = true }
crossbeam = { version = "0.3", optional = true }
pairing = { path = "../pairing", optional = true }
rand_core = "0.5"
byteorder = "1"

[dev-dependencies]
rand = "0.7"

[features]
groth16 = ["pairing"]
multicore = ["futures-cpupool", "crossbeam", "num_cpus"]
Expand Down
20 changes: 10 additions & 10 deletions bellman/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,16 @@ fn parallel_fft<E: ScalarEngine, T: Group<E>>(
#[test]
fn polynomial_arith() {
use pairing::bls12_381::Bls12;
use rand::{self, Rand};
use rand_core::RngCore;

fn test_mul<E: ScalarEngine, R: rand::Rng>(rng: &mut R)
fn test_mul<E: ScalarEngine, R: RngCore>(rng: &mut R)
{
let worker = Worker::new();

for coeffs_a in 0..70 {
for coeffs_b in 0..70 {
let mut a: Vec<_> = (0..coeffs_a).map(|_| Scalar::<E>(E::Fr::rand(rng))).collect();
let mut b: Vec<_> = (0..coeffs_b).map(|_| Scalar::<E>(E::Fr::rand(rng))).collect();
let mut a: Vec<_> = (0..coeffs_a).map(|_| Scalar::<E>(E::Fr::random(rng))).collect();
let mut b: Vec<_> = (0..coeffs_b).map(|_| Scalar::<E>(E::Fr::random(rng))).collect();

// naive evaluation
let mut naive = vec![Scalar(E::Fr::zero()); coeffs_a + coeffs_b];
Expand Down Expand Up @@ -423,9 +423,9 @@ fn polynomial_arith() {
#[test]
fn fft_composition() {
use pairing::bls12_381::Bls12;
use rand;
use rand_core::RngCore;

fn test_comp<E: ScalarEngine, R: rand::Rng>(rng: &mut R)
fn test_comp<E: ScalarEngine, R: RngCore>(rng: &mut R)
{
let worker = Worker::new();

Expand All @@ -434,7 +434,7 @@ fn fft_composition() {

let mut v = vec![];
for _ in 0..coeffs {
v.push(Scalar::<E>(rng.gen()));
v.push(Scalar::<E>(E::Fr::random(rng)));
}

let mut domain = EvaluationDomain::from_coeffs(v.clone()).unwrap();
Expand Down Expand Up @@ -462,18 +462,18 @@ fn fft_composition() {
#[test]
fn parallel_fft_consistency() {
use pairing::bls12_381::Bls12;
use rand::{self, Rand};
use rand_core::RngCore;
use std::cmp::min;

fn test_consistency<E: ScalarEngine, R: rand::Rng>(rng: &mut R)
fn test_consistency<E: ScalarEngine, R: RngCore>(rng: &mut R)
{
let worker = Worker::new();

for _ in 0..5 {
for log_d in 0..10 {
let d = 1 << log_d;

let v1 = (0..d).map(|_| Scalar::<E>(E::Fr::rand(rng))).collect::<Vec<_>>();
let v1 = (0..d).map(|_| Scalar::<E>(E::Fr::random(rng))).collect::<Vec<_>>();
let mut v1 = EvaluationDomain::from_coeffs(v1).unwrap();
let mut v2 = EvaluationDomain::from_coeffs(v1.coeffs.clone()).unwrap();

Expand Down
18 changes: 9 additions & 9 deletions bellman/src/groth16/generator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rand::Rng;
use rand_core::RngCore;

use std::sync::Arc;

Expand Down Expand Up @@ -35,15 +35,15 @@ pub fn generate_random_parameters<E, C, R>(
circuit: C,
rng: &mut R
) -> Result<Parameters<E>, SynthesisError>
where E: Engine, C: Circuit<E>, R: Rng
where E: Engine, C: Circuit<E>, R: RngCore
{
let g1 = rng.gen();
let g2 = rng.gen();
let alpha = rng.gen();
let beta = rng.gen();
let gamma = rng.gen();
let delta = rng.gen();
let tau = rng.gen();
let g1 = E::G1::random(rng);
let g2 = E::G2::random(rng);
let alpha = E::Fr::random(rng);
let beta = E::Fr::random(rng);
let gamma = E::Fr::random(rng);
let delta = E::Fr::random(rng);
let tau = E::Fr::random(rng);

generate_parameters::<E, C>(
circuit,
Expand Down
6 changes: 3 additions & 3 deletions bellman/src/groth16/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ mod test_with_bls12_381 {
use {Circuit, SynthesisError, ConstraintSystem};

use ff::Field;
use rand::{Rand, thread_rng};
use rand::{thread_rng};
use pairing::bls12_381::{Bls12, Fr};

#[test]
Expand Down Expand Up @@ -547,8 +547,8 @@ mod test_with_bls12_381 {
let pvk = prepare_verifying_key::<Bls12>(&params.vk);

for _ in 0..100 {
let a = Fr::rand(rng);
let b = Fr::rand(rng);
let a = Fr::random(rng);
let b = Fr::random(rng);
let mut c = a;
c.mul_assign(&b);

Expand Down
8 changes: 4 additions & 4 deletions bellman/src/groth16/prover.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rand::Rng;
use rand_core::RngCore;

use std::sync::Arc;

Expand Down Expand Up @@ -189,10 +189,10 @@ pub fn create_random_proof<E, C, R, P: ParameterSource<E>>(
params: P,
rng: &mut R
) -> Result<Proof<E>, SynthesisError>
where E: Engine, C: Circuit<E>, R: Rng
where E: Engine, C: Circuit<E>, R: RngCore
{
let r = rng.gen();
let s = rng.gen();
let r = E::Fr::random(rng);
let s = E::Fr::random(rng);

create_proof::<E, C, P>(circuit, params, r, s)
}
Expand Down
20 changes: 8 additions & 12 deletions bellman/src/groth16/tests/dummy_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use pairing::{Engine, PairingCurveAffine};

use std::cmp::Ordering;
use std::fmt;
use rand::{Rand, Rng};
use rand_core::RngCore;
use std::num::Wrapping;

const MODULUS_R: Wrapping<u32> = Wrapping(64513);
Expand All @@ -20,13 +20,11 @@ impl fmt::Display for Fr {
}
}

impl Rand for Fr {
fn rand<R: Rng>(rng: &mut R) -> Self {
Fr(Wrapping(rng.gen()) % MODULUS_R)
impl Field for Fr {
fn random<R: RngCore>(rng: &mut R) -> Self {
Fr(Wrapping(rng.next_u32()) % MODULUS_R)
}
}

impl Field for Fr {
fn zero() -> Self {
Fr(Wrapping(0))
}
Expand Down Expand Up @@ -145,12 +143,6 @@ impl PartialOrd for FrRepr {
}
}

impl Rand for FrRepr {
fn rand<R: Rng>(rng: &mut R) -> Self {
FrRepr([rng.gen()])
}
}

impl fmt::Display for FrRepr {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", (self.0)[0])
Expand Down Expand Up @@ -300,6 +292,10 @@ impl CurveProjective for Fr {
type Scalar = Fr;
type Engine = DummyEngine;

fn random<R: RngCore>(rng: &mut R) -> Self {
<Fr as Field>::random(rng)
}

fn zero() -> Self {
<Fr as Field>::zero()
}
Expand Down
5 changes: 4 additions & 1 deletion bellman/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate ff;
extern crate group;
#[cfg(feature = "pairing")]
extern crate pairing;
extern crate rand;
extern crate rand_core;

extern crate futures;
extern crate bit_vec;
Expand All @@ -15,6 +15,9 @@ extern crate futures_cpupool;
#[cfg(feature = "multicore")]
extern crate num_cpus;

#[cfg(test)]
extern crate rand;

pub mod multicore;
mod multiexp;
pub mod domain;
Expand Down
6 changes: 3 additions & 3 deletions bellman/src/multiexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,14 @@ fn test_with_bls12() {
acc
}

use rand::{self, Rand};
use rand;
use pairing::{bls12_381::Bls12, Engine};

const SAMPLES: usize = 1 << 14;

let rng = &mut rand::thread_rng();
let v = Arc::new((0..SAMPLES).map(|_| <Bls12 as ScalarEngine>::Fr::rand(rng).into_repr()).collect::<Vec<_>>());
let g = Arc::new((0..SAMPLES).map(|_| <Bls12 as Engine>::G1::rand(rng).into_affine()).collect::<Vec<_>>());
let v = Arc::new((0..SAMPLES).map(|_| <Bls12 as ScalarEngine>::Fr::random(rng).into_repr()).collect::<Vec<_>>());
let g = Arc::new((0..SAMPLES).map(|_| <Bls12 as Engine>::G1::random(rng).into_affine()).collect::<Vec<_>>());

let naive = naive_multiexp(g.clone(), v.clone());

Expand Down
10 changes: 5 additions & 5 deletions bellman/tests/mimc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ extern crate pairing;
extern crate rand;

// For randomness (during paramgen and proof generation)
use rand::{thread_rng, Rng};
use rand::thread_rng;

// For benchmarking
use std::time::{Duration, Instant};

// Bring in some tools for using pairing-friendly curves
use ff::Field;
use ff::{Field, ScalarEngine};
use pairing::Engine;

// We're going to use the BLS12-381 pairing-friendly elliptic curve.
Expand Down Expand Up @@ -172,7 +172,7 @@ fn test_mimc() {
let rng = &mut thread_rng();

// Generate the MiMC round constants
let constants = (0..MIMC_ROUNDS).map(|_| rng.gen()).collect::<Vec<_>>();
let constants = (0..MIMC_ROUNDS).map(|_| <Bls12 as ScalarEngine>::Fr::random(rng)).collect::<Vec<_>>();

println!("Creating parameters...");

Expand Down Expand Up @@ -203,8 +203,8 @@ fn test_mimc() {

for _ in 0..SAMPLES {
// Generate a random preimage and compute the image
let xl = rng.gen();
let xr = rng.gen();
let xl = <Bls12 as ScalarEngine>::Fr::random(rng);
let xr = <Bls12 as ScalarEngine>::Fr::random(rng);
let image = mimc::<Bls12>(xl, xr, &constants);

proof_vec.truncate(0);
Expand Down
2 changes: 1 addition & 1 deletion ff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ repository = "https://github.com/ebfull/ff"

[dependencies]
byteorder = "1"
rand = "0.4"
ff_derive = { version = "0.3.0", path = "ff_derive", optional = true }
rand_core = "0.5"

[features]
default = []
Expand Down
43 changes: 20 additions & 23 deletions ff/ff_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,6 @@ fn prime_field_repr_impl(repr: &syn::Ident, limbs: usize) -> proc_macro2::TokenS
}
}

impl ::rand::Rand for #repr {
#[inline(always)]
fn rand<R: ::rand::Rng>(rng: &mut R) -> Self {
#repr(rng.gen())
}
}

impl ::std::fmt::Display for #repr {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
try!(write!(f, "0x"));
Expand Down Expand Up @@ -839,22 +832,6 @@ fn prime_field_impl(
}
}

impl ::rand::Rand for #name {
/// Computes a uniformly random element using rejection sampling.
fn rand<R: ::rand::Rng>(rng: &mut R) -> Self {
loop {
let mut tmp = #name(#repr::rand(rng));

// Mask away the unused bits at the beginning.
tmp.0.as_mut()[#top_limb_index] &= 0xffffffffffffffff >> REPR_SHAVE_BITS;

if tmp.is_valid() {
return tmp
}
}
}
}

impl From<#name> for #repr {
fn from(e: #name) -> #repr {
e.into_repr()
Expand Down Expand Up @@ -904,6 +881,26 @@ fn prime_field_impl(
}

impl ::ff::Field for #name {
/// Computes a uniformly random element using rejection sampling.
fn random<R: ::rand_core::RngCore>(rng: &mut R) -> Self {
loop {
let mut tmp = {
let mut repr = [0u64; #limbs];
for i in 0..#limbs {
repr[i] = rng.next_u64();
}
#name(#repr(repr))
};

// Mask away the unused most-significant bits.
tmp.0.as_mut()[#top_limb_index] &= 0xffffffffffffffff >> REPR_SHAVE_BITS;

if tmp.is_valid() {
return tmp
}
}
}

#[inline]
fn zero() -> Self {
#name(#repr::from(0))
Expand Down
9 changes: 6 additions & 3 deletions ff/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(unused_imports)]

extern crate byteorder;
extern crate rand;
extern crate rand_core;

#[cfg(feature = "derive")]
#[macro_use]
Expand All @@ -10,14 +10,18 @@ extern crate ff_derive;
#[cfg(feature = "derive")]
pub use ff_derive::*;

use rand_core::RngCore;
use std::error::Error;
use std::fmt;
use std::io::{self, Read, Write};

/// This trait represents an element of a field.
pub trait Field:
Sized + Eq + Copy + Clone + Send + Sync + fmt::Debug + fmt::Display + 'static + rand::Rand
Sized + Eq + Copy + Clone + Send + Sync + fmt::Debug + fmt::Display + 'static
{
/// Returns an element chosen uniformly at random using a user-provided RNG.
fn random<R: RngCore>(rng: &mut R) -> Self;

/// Returns the zero element of the field, the additive identity.
fn zero() -> Self;

Expand Down Expand Up @@ -100,7 +104,6 @@ pub trait PrimeFieldRepr:
+ fmt::Debug
+ fmt::Display
+ 'static
+ rand::Rand
+ AsRef<[u64]>
+ AsMut<[u64]>
+ From<u64>
Expand Down
3 changes: 2 additions & 1 deletion group/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ repository = "https://github.com/ebfull/group"

[dependencies]
ff = { path = "../ff" }
rand = "0.4"
rand = "0.7"
rand_xorshift = "0.2"
Loading