Skip to content

Commit

Permalink
Migrate bellman to rand 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Jul 18, 2019
1 parent bfa9aaf commit 533d586
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 48 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repository = "https://github.com/ebfull/bellman"
version = "0.1.0"

[dependencies]
rand = "0.4"
rand_core = "0.3"
bit-vec = "0.4.4"
ff = { path = "../ff" }
futures = "0.1"
Expand All @@ -20,6 +20,9 @@ crossbeam = { version = "0.3", optional = true }
pairing = { path = "../pairing", optional = true }
byteorder = "1"

[dev-dependencies]
rand = "0.5"

[features]
groth16 = ["pairing"]
multicore = ["futures-cpupool", "crossbeam", "num_cpus"]
Expand Down
20 changes: 10 additions & 10 deletions 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 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 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 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 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 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 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 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

0 comments on commit 533d586

Please sign in to comment.