Migrate bellman to rand 0.5

This commit is contained in:
Jack Grigg
2019-07-10 19:40:20 -04:00
parent a7e22b3550
commit 4606a0cefb
10 changed files with 53 additions and 50 deletions

3
Cargo.lock generated
View File

@@ -61,7 +61,8 @@ dependencies = [
"group 0.1.0", "group 0.1.0",
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pairing 0.14.2", "pairing 0.14.2",
"rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]

View File

@@ -9,7 +9,7 @@ repository = "https://github.com/ebfull/bellman"
version = "0.1.0" version = "0.1.0"
[dependencies] [dependencies]
rand = "0.4" rand_core = "0.3"
bit-vec = "0.4.4" bit-vec = "0.4.4"
ff = { path = "../ff" } ff = { path = "../ff" }
futures = "0.1" futures = "0.1"
@@ -20,6 +20,9 @@ crossbeam = { version = "0.3", optional = true }
pairing = { path = "../pairing", optional = true } pairing = { path = "../pairing", optional = true }
byteorder = "1" byteorder = "1"
[dev-dependencies]
rand = "0.5"
[features] [features]
groth16 = ["pairing"] groth16 = ["pairing"]
multicore = ["futures-cpupool", "crossbeam", "num_cpus"] multicore = ["futures-cpupool", "crossbeam", "num_cpus"]

View File

@@ -375,16 +375,16 @@ fn parallel_fft<E: ScalarEngine, T: Group<E>>(
#[test] #[test]
fn polynomial_arith() { fn polynomial_arith() {
use pairing::bls12_381::Bls12; 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(); let worker = Worker::new();
for coeffs_a in 0..70 { for coeffs_a in 0..70 {
for coeffs_b 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 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::rand(rng))).collect(); let mut b: Vec<_> = (0..coeffs_b).map(|_| Scalar::<E>(E::Fr::random(rng))).collect();
// naive evaluation // naive evaluation
let mut naive = vec![Scalar(E::Fr::zero()); coeffs_a + coeffs_b]; let mut naive = vec![Scalar(E::Fr::zero()); coeffs_a + coeffs_b];
@@ -423,9 +423,9 @@ fn polynomial_arith() {
#[test] #[test]
fn fft_composition() { fn fft_composition() {
use pairing::bls12_381::Bls12; 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(); let worker = Worker::new();
@@ -434,7 +434,7 @@ fn fft_composition() {
let mut v = vec![]; let mut v = vec![];
for _ in 0..coeffs { 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(); let mut domain = EvaluationDomain::from_coeffs(v.clone()).unwrap();
@@ -462,10 +462,10 @@ fn fft_composition() {
#[test] #[test]
fn parallel_fft_consistency() { fn parallel_fft_consistency() {
use pairing::bls12_381::Bls12; use pairing::bls12_381::Bls12;
use rand::{self, Rand}; use rand_core::RngCore;
use std::cmp::min; 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(); let worker = Worker::new();
@@ -473,7 +473,7 @@ fn parallel_fft_consistency() {
for log_d in 0..10 { for log_d in 0..10 {
let d = 1 << log_d; 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 v1 = EvaluationDomain::from_coeffs(v1).unwrap();
let mut v2 = EvaluationDomain::from_coeffs(v1.coeffs.clone()).unwrap(); let mut v2 = EvaluationDomain::from_coeffs(v1.coeffs.clone()).unwrap();

View File

@@ -1,4 +1,4 @@
use rand::Rng; use rand_core::RngCore;
use std::sync::Arc; use std::sync::Arc;
@@ -35,15 +35,15 @@ pub fn generate_random_parameters<E, C, R>(
circuit: C, circuit: C,
rng: &mut R rng: &mut R
) -> Result<Parameters<E>, SynthesisError> ) -> 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 g1 = E::G1::random(rng);
let g2 = rng.gen(); let g2 = E::G2::random(rng);
let alpha = rng.gen(); let alpha = E::Fr::random(rng);
let beta = rng.gen(); let beta = E::Fr::random(rng);
let gamma = rng.gen(); let gamma = E::Fr::random(rng);
let delta = rng.gen(); let delta = E::Fr::random(rng);
let tau = rng.gen(); let tau = E::Fr::random(rng);
generate_parameters::<E, C>( generate_parameters::<E, C>(
circuit, circuit,

View File

@@ -487,7 +487,7 @@ mod test_with_bls12_381 {
use {Circuit, SynthesisError, ConstraintSystem}; use {Circuit, SynthesisError, ConstraintSystem};
use ff::Field; use ff::Field;
use rand::{Rand, thread_rng}; use rand::{thread_rng};
use pairing::bls12_381::{Bls12, Fr}; use pairing::bls12_381::{Bls12, Fr};
#[test] #[test]
@@ -547,8 +547,8 @@ mod test_with_bls12_381 {
let pvk = prepare_verifying_key::<Bls12>(&params.vk); let pvk = prepare_verifying_key::<Bls12>(&params.vk);
for _ in 0..100 { for _ in 0..100 {
let a = Fr::rand(rng); let a = Fr::random(rng);
let b = Fr::rand(rng); let b = Fr::random(rng);
let mut c = a; let mut c = a;
c.mul_assign(&b); c.mul_assign(&b);

View File

@@ -1,4 +1,4 @@
use rand::Rng; use rand_core::RngCore;
use std::sync::Arc; use std::sync::Arc;
@@ -189,10 +189,10 @@ pub fn create_random_proof<E, C, R, P: ParameterSource<E>>(
params: P, params: P,
rng: &mut R rng: &mut R
) -> Result<Proof<E>, SynthesisError> ) -> 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 r = E::Fr::random(rng);
let s = rng.gen(); let s = E::Fr::random(rng);
create_proof::<E, C, P>(circuit, params, r, s) create_proof::<E, C, P>(circuit, params, r, s)
} }

View File

@@ -6,7 +6,7 @@ use pairing::{Engine, PairingCurveAffine};
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fmt; use std::fmt;
use rand::{Rand, Rng}; use rand_core::RngCore;
use std::num::Wrapping; use std::num::Wrapping;
const MODULUS_R: Wrapping<u32> = Wrapping(64513); const MODULUS_R: Wrapping<u32> = Wrapping(64513);
@@ -20,13 +20,11 @@ impl fmt::Display for Fr {
} }
} }
impl Rand for Fr { impl Field for Fr {
fn rand<R: Rng>(rng: &mut R) -> Self { fn random<R: RngCore>(rng: &mut R) -> Self {
Fr(Wrapping(rng.gen()) % MODULUS_R) Fr(Wrapping(rng.next_u32()) % MODULUS_R)
}
} }
impl Field for Fr {
fn zero() -> Self { fn zero() -> Self {
Fr(Wrapping(0)) Fr(Wrapping(0))
} }
@@ -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 { impl fmt::Display for FrRepr {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", (self.0)[0]) write!(f, "{}", (self.0)[0])
@@ -300,6 +292,10 @@ impl CurveProjective for Fr {
type Scalar = Fr; type Scalar = Fr;
type Engine = DummyEngine; type Engine = DummyEngine;
fn random<R: RngCore>(rng: &mut R) -> Self {
<Fr as Field>::random(rng)
}
fn zero() -> Self { fn zero() -> Self {
<Fr as Field>::zero() <Fr as Field>::zero()
} }

View File

@@ -2,7 +2,7 @@ extern crate ff;
extern crate group; extern crate group;
#[cfg(feature = "pairing")] #[cfg(feature = "pairing")]
extern crate pairing; extern crate pairing;
extern crate rand; extern crate rand_core;
extern crate futures; extern crate futures;
extern crate bit_vec; extern crate bit_vec;
@@ -15,6 +15,9 @@ extern crate futures_cpupool;
#[cfg(feature = "multicore")] #[cfg(feature = "multicore")]
extern crate num_cpus; extern crate num_cpus;
#[cfg(test)]
extern crate rand;
pub mod multicore; pub mod multicore;
mod multiexp; mod multiexp;
pub mod domain; pub mod domain;

View File

@@ -274,14 +274,14 @@ fn test_with_bls12() {
acc acc
} }
use rand::{self, Rand}; use rand;
use pairing::{bls12_381::Bls12, Engine}; use pairing::{bls12_381::Bls12, Engine};
const SAMPLES: usize = 1 << 14; const SAMPLES: usize = 1 << 14;
let rng = &mut rand::thread_rng(); let rng = &mut rand::thread_rng();
let v = Arc::new((0..SAMPLES).map(|_| <Bls12 as ScalarEngine>::Fr::rand(rng).into_repr()).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::rand(rng).into_affine()).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()); let naive = naive_multiexp(g.clone(), v.clone());

View File

@@ -4,13 +4,13 @@ extern crate pairing;
extern crate rand; extern crate rand;
// For randomness (during paramgen and proof generation) // For randomness (during paramgen and proof generation)
use rand::{thread_rng, Rng}; use rand::thread_rng;
// For benchmarking // For benchmarking
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
// Bring in some tools for using pairing-friendly curves // Bring in some tools for using pairing-friendly curves
use ff::Field; use ff::{Field, ScalarEngine};
use pairing::Engine; use pairing::Engine;
// We're going to use the BLS12-381 pairing-friendly elliptic curve. // We're going to use the BLS12-381 pairing-friendly elliptic curve.
@@ -172,7 +172,7 @@ fn test_mimc() {
let rng = &mut thread_rng(); let rng = &mut thread_rng();
// Generate the MiMC round constants // 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..."); println!("Creating parameters...");
@@ -203,8 +203,8 @@ fn test_mimc() {
for _ in 0..SAMPLES { for _ in 0..SAMPLES {
// Generate a random preimage and compute the image // Generate a random preimage and compute the image
let xl = rng.gen(); let xl = <Bls12 as ScalarEngine>::Fr::random(rng);
let xr = rng.gen(); let xr = <Bls12 as ScalarEngine>::Fr::random(rng);
let image = mimc::<Bls12>(xl, xr, &constants); let image = mimc::<Bls12>(xl, xr, &constants);
proof_vec.truncate(0); proof_vec.truncate(0);