Migrate sapling-crypto to rand_core 0.4

This commit is contained in:
Jack Grigg 2019-07-12 23:54:59 -04:00
parent 83e1af104e
commit 60d344a0a7
No known key found for this signature in database
GPG Key ID: 9E8255172BBF9898
18 changed files with 115 additions and 79 deletions

3
Cargo.lock generated
View File

@ -534,7 +534,8 @@ dependencies = [
"ff 0.4.0",
"hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"pairing 0.14.2",
"rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

View File

@ -17,10 +17,11 @@ bellman = { path = "../bellman" }
blake2b_simd = "0.5"
blake2s_simd = "0.5"
ff = { path = "../ff" }
rand = "0.5"
rand_core = "0.4"
digest = "0.7"
byteorder = "1"
[dev-dependencies]
hex-literal = "0.1"
rand_xorshift = "0.1"
sha2 = "0.8"

View File

@ -1,8 +1,9 @@
extern crate ff;
extern crate sapling_crypto;
extern crate bellman;
extern crate rand;
extern crate pairing;
extern crate rand_core;
extern crate rand_xorshift;
use ff::Field;
use std::time::{Duration, Instant};
@ -20,7 +21,8 @@ use sapling_crypto::primitives::{
ValueCommitment
};
use bellman::groth16::*;
use rand::{XorShiftRng, SeedableRng, Rng, RngCore};
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
use pairing::bls12_381::{Bls12, Fr};
const TREE_DEPTH: usize = 32;
@ -86,7 +88,7 @@ fn main() {
}
let commitment_randomness = fs::Fs::random(rng);
let auth_path = vec![Some((Fr::random(rng), rng.gen())); TREE_DEPTH];
let auth_path = vec![Some((Fr::random(rng), rng.next_u32() % 2 != 0)); TREE_DEPTH];
let ar = fs::Fs::random(rng);
let anchor = Fr::random(rng);

View File

@ -321,8 +321,10 @@ pub fn blake2s<E: Engine, CS: ConstraintSystem<E>>(
#[cfg(test)]
mod test {
use blake2s_simd::Params as Blake2sParams;
use rand::{XorShiftRng, SeedableRng, Rng};
use pairing::bls12_381::{Bls12};
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
use ::circuit::boolean::{Boolean, AllocatedBit};
use ::circuit::test::TestConstraintSystem;
use super::blake2s;
@ -371,7 +373,7 @@ mod test {
0xe5,
]);
let input_bits: Vec<_> = (0..512)
.map(|_| Boolean::constant(rng.gen()))
.map(|_| Boolean::constant(rng.next_u32() % 2 != 0))
.chain((0..512)
.map(|i| AllocatedBit::alloc(cs.namespace(|| format!("input bit {}", i)), Some(true)).unwrap().into()))
.collect();
@ -387,7 +389,7 @@ mod test {
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc,
0xe5,
]);
let input_bits: Vec<_> = (0..512).map(|_| Boolean::constant(rng.gen())).collect();
let input_bits: Vec<_> = (0..512).map(|_| Boolean::constant(rng.next_u32() % 2 != 0)).collect();
blake2s(&mut cs, &input_bits, b"12345678").unwrap();
assert_eq!(cs.num_constraints(), 0);
}
@ -403,7 +405,7 @@ mod test {
{
let mut h = Blake2sParams::new().hash_length(32).personal(b"12345678").to_state();
let data: Vec<u8> = (0..input_len).map(|_| rng.gen()).collect();
let data: Vec<u8> = (0..input_len).map(|_| rng.next_u32() as u8).collect();
h.update(&data);

View File

@ -748,9 +748,11 @@ impl<E: JubjubEngine> MontgomeryPoint<E> {
#[cfg(test)]
mod test {
use bellman::{ConstraintSystem};
use rand::{XorShiftRng, SeedableRng, Rng};
use ff::{BitIterator, Field, PrimeField};
use pairing::bls12_381::{Bls12, Fr};
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
use ::circuit::test::*;
use ::jubjub::{
montgomery,
@ -1001,10 +1003,10 @@ mod test {
y: num_y0
};
let mut should_we_select = rng.gen();
let mut should_we_select = rng.next_u32() % 2 != 0;
// Conditionally allocate
let mut b = if rng.gen() {
let mut b = if rng.next_u32() % 2 != 0 {
Boolean::from(AllocatedBit::alloc(
cs.namespace(|| "condition"),
Some(should_we_select)
@ -1014,7 +1016,7 @@ mod test {
};
// Conditionally negate
if rng.gen() {
if rng.next_u32() % 2 != 0 {
b = b.not();
should_we_select = !should_we_select;
}
@ -1163,7 +1165,7 @@ mod test {
for _ in 0..100 {
let p1 = loop {
let x = Fr::random(rng);
let s: bool = rng.gen();
let s: bool = rng.next_u32() % 2 != 0;
if let Some(p) = montgomery::Point::<Bls12, _>::get_for_x(x, s, params) {
break p;
@ -1172,7 +1174,7 @@ mod test {
let p2 = loop {
let x = Fr::random(rng);
let s: bool = rng.gen();
let s: bool = rng.next_u32() % 2 != 0;
if let Some(p) = montgomery::Point::<Bls12, _>::get_for_x(x, s, params) {
break p;

View File

@ -196,11 +196,12 @@ pub fn lookup3_xy_with_conditional_negation<E: Engine, CS>(
#[cfg(test)]
mod test {
use rand::{SeedableRng, Rng, XorShiftRng};
use super::*;
use ::circuit::test::*;
use ::circuit::boolean::{Boolean, AllocatedBit};
use pairing::bls12_381::{Bls12, Fr};
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
#[test]
fn test_lookup3_xy() {
@ -212,17 +213,17 @@ mod test {
for _ in 0..100 {
let mut cs = TestConstraintSystem::<Bls12>::new();
let a_val = rng.gen();
let a_val = rng.next_u32() % 2 != 0;
let a = Boolean::from(
AllocatedBit::alloc(cs.namespace(|| "a"), Some(a_val)).unwrap()
);
let b_val = rng.gen();
let b_val = rng.next_u32() % 2 != 0;
let b = Boolean::from(
AllocatedBit::alloc(cs.namespace(|| "b"), Some(b_val)).unwrap()
);
let c_val = rng.gen();
let c_val = rng.next_u32() % 2 != 0;
let c = Boolean::from(
AllocatedBit::alloc(cs.namespace(|| "c"), Some(c_val)).unwrap()
);
@ -255,17 +256,17 @@ mod test {
for _ in 0..100 {
let mut cs = TestConstraintSystem::<Bls12>::new();
let a_val = rng.gen();
let a_val = rng.next_u32() % 2 != 0;
let a = Boolean::from(
AllocatedBit::alloc(cs.namespace(|| "a"), Some(a_val)).unwrap()
);
let b_val = rng.gen();
let b_val = rng.next_u32() % 2 != 0;
let b = Boolean::from(
AllocatedBit::alloc(cs.namespace(|| "b"), Some(b_val)).unwrap()
);
let c_val = rng.gen();
let c_val = rng.next_u32() % 2 != 0;
let c = Boolean::from(
AllocatedBit::alloc(cs.namespace(|| "c"), Some(c_val)).unwrap()
);

View File

@ -80,9 +80,11 @@ pub fn compute_multipacking<E: Engine>(
#[test]
fn test_multipacking() {
use rand::{SeedableRng, Rng, XorShiftRng};
use bellman::{ConstraintSystem};
use pairing::bls12_381::{Bls12};
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
use ::circuit::test::*;
use super::boolean::{AllocatedBit, Boolean};
@ -94,7 +96,7 @@ fn test_multipacking() {
for num_bits in 0..1500 {
let mut cs = TestConstraintSystem::<Bls12>::new();
let bits: Vec<bool> = (0..num_bits).map(|_| rng.gen()).collect();
let bits: Vec<bool> = (0..num_bits).map(|_| rng.next_u32() % 2 != 0).collect();
let circuit_bits = bits.iter().enumerate()
.map(|(i, &b)| {

View File

@ -455,10 +455,12 @@ impl<E: Engine> Num<E> {
#[cfg(test)]
mod test {
use rand::{SeedableRng, XorShiftRng};
use bellman::{ConstraintSystem};
use ff::{BitIterator, Field, PrimeField};
use pairing::bls12_381::{Bls12, Fr};
use rand_core::SeedableRng;
use rand_xorshift::XorShiftRng;
use ::circuit::test::*;
use super::{AllocatedNum, Boolean};

View File

@ -112,12 +112,13 @@ pub fn pedersen_hash<E: JubjubEngine, CS>(
#[cfg(test)]
mod test {
use rand::{SeedableRng, Rng, XorShiftRng};
use super::*;
use ::circuit::test::*;
use ::circuit::boolean::{Boolean, AllocatedBit};
use ff::PrimeField;
use pairing::bls12_381::{Bls12, Fr};
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
#[test]
fn test_pedersen_hash_constraints() {
@ -128,7 +129,7 @@ mod test {
let params = &JubjubBls12::new();
let mut cs = TestConstraintSystem::<Bls12>::new();
let input: Vec<bool> = (0..(Fr::NUM_BITS * 2)).map(|_| rng.gen()).collect();
let input: Vec<bool> = (0..(Fr::NUM_BITS * 2)).map(|_| rng.next_u32() % 2 != 0).collect();
let input_bools: Vec<Boolean> = input.iter().enumerate().map(|(i, b)| {
Boolean::from(
@ -157,7 +158,7 @@ mod test {
for length in 0..751 {
for _ in 0..5 {
let mut input: Vec<bool> = (0..length).map(|_| rng.gen()).collect();
let mut input: Vec<bool> = (0..length).map(|_| rng.next_u32() % 2 != 0).collect();
let mut cs = TestConstraintSystem::<Bls12>::new();

View File

@ -600,7 +600,9 @@ impl<'a, E: JubjubEngine> Circuit<E> for Output<'a, E> {
fn test_input_circuit_with_bls12_381() {
use ff::{BitIterator, Field};
use pairing::bls12_381::*;
use rand::{SeedableRng, Rng, RngCore, XorShiftRng};
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
use ::circuit::test::*;
use jubjub::{JubjubBls12, fs, edwards};
@ -614,7 +616,7 @@ fn test_input_circuit_with_bls12_381() {
for _ in 0..10 {
let value_commitment = ValueCommitment {
value: rng.gen(),
value: rng.next_u64(),
randomness: fs::Fs::random(rng),
};
@ -649,7 +651,7 @@ fn test_input_circuit_with_bls12_381() {
let g_d = payment_address.diversifier.g_d(params).unwrap();
let commitment_randomness = fs::Fs::random(rng);
let auth_path = vec![Some((Fr::random(rng), rng.gen())); tree_depth];
let auth_path = vec![Some((Fr::random(rng), rng.next_u32() % 2 != 0)); tree_depth];
let ar = fs::Fs::random(rng);
{
@ -739,7 +741,8 @@ fn test_input_circuit_with_bls12_381() {
fn test_output_circuit_with_bls12_381() {
use ff::Field;
use pairing::bls12_381::*;
use rand::{SeedableRng, Rng, RngCore, XorShiftRng};
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
use ::circuit::test::*;
use jubjub::{JubjubBls12, fs, edwards};
@ -751,7 +754,7 @@ fn test_output_circuit_with_bls12_381() {
for _ in 0..100 {
let value_commitment = ValueCommitment {
value: rng.gen(),
value: rng.next_u64(),
randomness: fs::Fs::random(rng),
};

View File

@ -308,7 +308,8 @@ mod test {
use circuit::boolean::AllocatedBit;
use pairing::bls12_381::Bls12;
use circuit::test::TestConstraintSystem;
use rand::{XorShiftRng, SeedableRng, Rng};
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
#[test]
fn test_blank_hash() {
@ -353,7 +354,7 @@ mod test {
Boolean::from(
AllocatedBit::alloc(
cs.namespace(|| format!("input bit {}", i)),
Some(rng.gen())
Some(rng.next_u32() % 2 != 0)
).unwrap()
)
}).collect();
@ -380,7 +381,7 @@ mod test {
for input_len in (0..32).chain((32..256).filter(|a| a % 8 == 0))
{
let mut h = Sha256::new();
let data: Vec<u8> = (0..input_len).map(|_| rng.gen()).collect();
let data: Vec<u8> = (0..input_len).map(|_| rng.next_u32() as u8).collect();
h.input(&data);
let hash_result = h.result();

View File

@ -409,7 +409,6 @@ impl UInt32 {
#[cfg(test)]
mod test {
use rand::{XorShiftRng, SeedableRng, Rng};
use ::circuit::boolean::{Boolean};
use super::{UInt32};
use ff::Field;
@ -417,6 +416,8 @@ mod test {
use ::circuit::test::*;
use bellman::{ConstraintSystem};
use circuit::multieq::MultiEq;
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
#[test]
fn test_uint32_from_bits_be() {
@ -426,7 +427,7 @@ mod test {
]);
for _ in 0..1000 {
let mut v = (0..32).map(|_| Boolean::constant(rng.gen())).collect::<Vec<_>>();
let mut v = (0..32).map(|_| Boolean::constant(rng.next_u32() % 2 != 0)).collect::<Vec<_>>();
let b = UInt32::from_bits_be(&v);
@ -460,7 +461,7 @@ mod test {
]);
for _ in 0..1000 {
let mut v = (0..32).map(|_| Boolean::constant(rng.gen())).collect::<Vec<_>>();
let mut v = (0..32).map(|_| Boolean::constant(rng.next_u32() % 2 != 0)).collect::<Vec<_>>();
let b = UInt32::from_bits(&v);
@ -496,9 +497,9 @@ mod test {
for _ in 0..1000 {
let mut cs = TestConstraintSystem::<Bls12>::new();
let a: u32 = rng.gen();
let b: u32 = rng.gen();
let c: u32 = rng.gen();
let a = rng.next_u32();
let b = rng.next_u32();
let c = rng.next_u32();
let mut expected = a ^ b ^ c;
@ -541,9 +542,9 @@ mod test {
for _ in 0..1000 {
let mut cs = TestConstraintSystem::<Bls12>::new();
let a: u32 = rng.gen();
let b: u32 = rng.gen();
let c: u32 = rng.gen();
let a = rng.next_u32();
let b = rng.next_u32();
let c = rng.next_u32();
let a_bit = UInt32::constant(a);
let b_bit = UInt32::constant(b);
@ -583,10 +584,10 @@ mod test {
for _ in 0..1000 {
let mut cs = TestConstraintSystem::<Bls12>::new();
let a: u32 = rng.gen();
let b: u32 = rng.gen();
let c: u32 = rng.gen();
let d: u32 = rng.gen();
let a = rng.next_u32();
let b = rng.next_u32();
let c = rng.next_u32();
let d = rng.next_u32();
let mut expected = (a ^ b).wrapping_add(c).wrapping_add(d);
@ -640,7 +641,7 @@ mod test {
0xe5,
]);
let mut num = rng.gen();
let mut num = rng.next_u32();
let a = UInt32::constant(num);
@ -675,7 +676,7 @@ mod test {
for _ in 0..50 {
for i in 0..60 {
let num = rng.gen();
let num = rng.next_u32();
let a = UInt32::constant(num).shr(i);
let b = UInt32::constant(num.wrapping_shr(i as u32));
@ -699,9 +700,9 @@ mod test {
for _ in 0..1000 {
let mut cs = TestConstraintSystem::<Bls12>::new();
let a: u32 = rng.gen();
let b: u32 = rng.gen();
let c: u32 = rng.gen();
let a = rng.next_u32();
let b = rng.next_u32();
let c = rng.next_u32();
let mut expected = (a & b) ^ (a & c) ^ (b & c);
@ -743,9 +744,9 @@ mod test {
for _ in 0..1000 {
let mut cs = TestConstraintSystem::<Bls12>::new();
let a: u32 = rng.gen();
let b: u32 = rng.gen();
let c: u32 = rng.gen();
let a = rng.next_u32();
let b = rng.next_u32();
let c = rng.next_u32();
let mut expected = (a & b) ^ ((!a) & c);

View File

@ -8,9 +8,7 @@ use super::{
montgomery
};
use rand::{
Rng
};
use rand_core::RngCore;
use std::marker::PhantomData;
@ -185,12 +183,13 @@ impl<E: JubjubEngine> Point<E, Unknown> {
convert_subgroup(&tmp)
}
pub fn rand<R: Rng>(rng: &mut R, params: &E::Params) -> Self
pub fn rand<R: RngCore>(rng: &mut R, params: &E::Params) -> Self
{
loop {
let y = E::Fr::random(rng);
let sign = rng.next_u32() % 2 != 0;
if let Some(p) = Self::get_for_y(y, rng.gen(), params) {
if let Some(p) = Self::get_for_y(y, sign, params) {
return p;
}
}

View File

@ -4,7 +4,7 @@ use ff::{
LegendreSymbol::{self, *},
PrimeField, PrimeFieldDecodingError, PrimeFieldRepr, SqrtField,
};
use rand::RngCore;
use rand_core::RngCore;
use super::ToUniform;
@ -620,7 +620,9 @@ fn test_neg_one() {
}
#[cfg(test)]
use rand::{SeedableRng, XorShiftRng};
use rand_core::SeedableRng;
#[cfg(test)]
use rand_xorshift::XorShiftRng;
#[test]
fn test_fs_repr_ordering() {

View File

@ -8,9 +8,7 @@ use super::{
edwards
};
use rand::{
Rng
};
use rand_core::RngCore;
use std::marker::PhantomData;
@ -101,12 +99,13 @@ impl<E: JubjubEngine> Point<E, Unknown> {
convert_subgroup(&tmp)
}
pub fn rand<R: Rng>(rng: &mut R, params: &E::Params) -> Self
pub fn rand<R: RngCore>(rng: &mut R, params: &E::Params) -> Self
{
loop {
let x = E::Fr::random(rng);
let sign = rng.next_u32() % 2 != 0;
match Self::get_for_x(x, rng.gen(), params) {
match Self::get_for_x(x, sign, params) {
Some(p) => {
return p
},

View File

@ -14,7 +14,8 @@ use ff::{
LegendreSymbol
};
use rand::{RngCore, XorShiftRng, SeedableRng};
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
pub fn test_suite<E: JubjubEngine>(params: &E::Params) {
test_back_and_forth::<E>(params);

View File

@ -4,13 +4,16 @@ extern crate blake2b_simd;
extern crate blake2s_simd;
extern crate digest;
extern crate ff;
extern crate rand;
extern crate rand_core;
extern crate byteorder;
#[cfg(test)]
#[macro_use]
extern crate hex_literal;
#[cfg(test)]
extern crate rand_xorshift;
#[cfg(test)]
extern crate sha2;

View File

@ -2,7 +2,7 @@
//! See section 5.4.6 of the Sapling protocol specification.
use ff::{Field, PrimeField, PrimeFieldRepr};
use rand::{Rng};
use rand_core::RngCore;
use std::io::{self, Read, Write};
use jubjub::{FixedGenerators, JubjubEngine, JubjubParams, Unknown, edwards::Point};
@ -71,7 +71,7 @@ impl<E: JubjubEngine> PrivateKey<E> {
write_scalar::<E, W>(&self.0, writer)
}
pub fn sign<R: Rng>(
pub fn sign<R: RngCore>(
&self,
msg: &[u8],
rng: &mut R,
@ -163,7 +163,7 @@ pub struct BatchEntry<'a, E: JubjubEngine> {
// TODO: #82: This is a naive implementation currently,
// and doesn't use multiexp.
pub fn batch_verify<'a, E: JubjubEngine, R: Rng>(
pub fn batch_verify<'a, E: JubjubEngine, R: RngCore>(
rng: &mut R,
batch: &[BatchEntry<'a, E>],
p_g: FixedGenerators,
@ -206,7 +206,8 @@ pub fn batch_verify<'a, E: JubjubEngine, R: Rng>(
#[cfg(test)]
mod tests {
use pairing::bls12_381::Bls12;
use rand::thread_rng;
use rand_core::SeedableRng;
use rand_xorshift::XorShiftRng;
use jubjub::{JubjubBls12, fs::Fs, edwards};
@ -214,7 +215,10 @@ mod tests {
#[test]
fn test_batch_verify() {
let rng = &mut thread_rng();
let rng = &mut XorShiftRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc,
0xe5,
]);
let params = &JubjubBls12::new();
let p_g = FixedGenerators::SpendingKeyGenerator;
@ -244,7 +248,10 @@ mod tests {
#[test]
fn cofactor_check() {
let rng = &mut thread_rng();
let rng = &mut XorShiftRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc,
0xe5,
]);
let params = &JubjubBls12::new();
let zero = edwards::Point::zero();
let p_g = FixedGenerators::SpendingKeyGenerator;
@ -276,7 +283,10 @@ mod tests {
#[test]
fn round_trip_serialization() {
let rng = &mut thread_rng();
let rng = &mut XorShiftRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc,
0xe5,
]);
let p_g = FixedGenerators::SpendingKeyGenerator;
let params = &JubjubBls12::new();
@ -309,7 +319,10 @@ mod tests {
#[test]
fn random_signatures() {
let rng = &mut thread_rng();
let rng = &mut XorShiftRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc,
0xe5,
]);
let p_g = FixedGenerators::SpendingKeyGenerator;
let params = &JubjubBls12::new();