From 8f7adec0d940e6e260d4abb14d2c09e3b6662ce6 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sat, 13 Jul 2019 00:16:54 -0400 Subject: [PATCH] Migrate zcash_primitives to rand_core 0.4 --- Cargo.lock | 3 +- zcash_primitives/Cargo.toml | 3 +- zcash_primitives/src/lib.rs | 3 +- zcash_primitives/src/merkle_tree.rs | 4 +- zcash_primitives/src/note_encryption.rs | 58 +++++++++++------------ zcash_primitives/src/sapling.rs | 2 +- zcash_primitives/src/transaction/tests.rs | 4 +- 7 files changed, 40 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c36e99..e435c6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -621,7 +621,8 @@ dependencies = [ "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.0 (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_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "sapling-crypto 0.0.1", "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/zcash_primitives/Cargo.toml b/zcash_primitives/Cargo.toml index d82b3bc..10c6d00 100644 --- a/zcash_primitives/Cargo.toml +++ b/zcash_primitives/Cargo.toml @@ -15,6 +15,7 @@ fpe = "0.1" hex = "0.3" lazy_static = "1" pairing = { path = "../pairing" } -rand = "0.5" +rand_core = "0.4" +rand_os = "0.1" sapling-crypto = { path = "../sapling-crypto" } sha2 = "0.8" diff --git a/zcash_primitives/src/lib.rs b/zcash_primitives/src/lib.rs index 70bd8fa..90d69e6 100644 --- a/zcash_primitives/src/lib.rs +++ b/zcash_primitives/src/lib.rs @@ -9,7 +9,8 @@ extern crate ff; extern crate fpe; extern crate hex; extern crate pairing; -extern crate rand; +extern crate rand_core; +extern crate rand_os; extern crate sapling_crypto; extern crate sha2; diff --git a/zcash_primitives/src/merkle_tree.rs b/zcash_primitives/src/merkle_tree.rs index 3b94bd9..a692073 100644 --- a/zcash_primitives/src/merkle_tree.rs +++ b/zcash_primitives/src/merkle_tree.rs @@ -202,12 +202,12 @@ impl CommitmentTree { /// ``` /// extern crate ff; /// extern crate pairing; -/// extern crate rand; +/// extern crate rand_os; /// extern crate zcash_primitives; /// /// use ff::{Field, PrimeField}; /// use pairing::bls12_381::Fr; -/// use rand::OsRng; +/// use rand_os::OsRng; /// use zcash_primitives::{ /// merkle_tree::{CommitmentTree, IncrementalWitness}, /// sapling::Node, diff --git a/zcash_primitives/src/note_encryption.rs b/zcash_primitives/src/note_encryption.rs index b1b483f..728818f 100644 --- a/zcash_primitives/src/note_encryption.rs +++ b/zcash_primitives/src/note_encryption.rs @@ -5,7 +5,8 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use crypto_api_chachapoly::{ChaCha20Ietf, ChachaPolyIetf}; use ff::{PrimeField, PrimeFieldRepr}; use pairing::bls12_381::{Bls12, Fr}; -use rand::{OsRng, Rng}; +use rand_core::RngCore; +use rand_os::OsRng; use sapling_crypto::{ jubjub::{ edwards, @@ -138,9 +139,7 @@ fn generate_esk() -> Fs { // create random 64 byte buffer let mut rng = OsRng::new().expect("should be able to construct RNG"); let mut buffer = [0u8; 64]; - for i in 0..buffer.len() { - buffer[i] = rng.gen(); - } + rng.fill_bytes(&mut buffer); // reduce to uniform value Fs::to_uniform(&buffer[..]) @@ -213,12 +212,12 @@ fn prf_ock( /// ``` /// extern crate ff; /// extern crate pairing; -/// extern crate rand; +/// extern crate rand_os; /// extern crate sapling_crypto; /// /// use ff::Field; /// use pairing::bls12_381::Bls12; -/// use rand::OsRng; +/// use rand_os::OsRng; /// use sapling_crypto::{ /// jubjub::fs::Fs, /// primitives::{Diversifier, PaymentAddress, ValueCommitment}, @@ -562,7 +561,8 @@ mod tests { use crypto_api_chachapoly::ChachaPolyIetf; use ff::{Field, PrimeField, PrimeFieldRepr}; use pairing::bls12_381::{Bls12, Fr, FrRepr}; - use rand::{thread_rng, RngCore}; + use rand_core::RngCore; + use rand_os::OsRng; use sapling_crypto::{ jubjub::{ edwards, @@ -848,7 +848,7 @@ mod tests { #[test] fn decryption_with_invalid_ivk() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (_, _, _, cmu, epk, enc_ciphertext, _) = random_enc_ciphertext(&mut rng); @@ -860,7 +860,7 @@ mod tests { #[test] fn decryption_with_invalid_epk() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (_, ivk, _, cmu, _, enc_ciphertext, _) = random_enc_ciphertext(&mut rng); @@ -877,7 +877,7 @@ mod tests { #[test] fn decryption_with_invalid_cmu() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (_, ivk, _, _, epk, enc_ciphertext, _) = random_enc_ciphertext(&mut rng); @@ -889,7 +889,7 @@ mod tests { #[test] fn decryption_with_invalid_tag() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (_, ivk, _, cmu, epk, mut enc_ciphertext, _) = random_enc_ciphertext(&mut rng); @@ -902,7 +902,7 @@ mod tests { #[test] fn decryption_with_invalid_version_byte() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, ivk, cv, cmu, epk, mut enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -924,7 +924,7 @@ mod tests { #[test] fn decryption_with_invalid_diversifier() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, ivk, cv, cmu, epk, mut enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -946,7 +946,7 @@ mod tests { #[test] fn decryption_with_incorrect_diversifier() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, ivk, cv, cmu, epk, mut enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -968,7 +968,7 @@ mod tests { #[test] fn compact_decryption_with_invalid_ivk() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (_, _, _, cmu, epk, enc_ciphertext, _) = random_enc_ciphertext(&mut rng); @@ -985,7 +985,7 @@ mod tests { #[test] fn compact_decryption_with_invalid_epk() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (_, ivk, _, cmu, _, enc_ciphertext, _) = random_enc_ciphertext(&mut rng); @@ -1002,7 +1002,7 @@ mod tests { #[test] fn compact_decryption_with_invalid_cmu() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (_, ivk, _, _, epk, enc_ciphertext, _) = random_enc_ciphertext(&mut rng); @@ -1019,7 +1019,7 @@ mod tests { #[test] fn compact_decryption_with_invalid_version_byte() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, ivk, cv, cmu, epk, mut enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -1046,7 +1046,7 @@ mod tests { #[test] fn compact_decryption_with_invalid_diversifier() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, ivk, cv, cmu, epk, mut enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -1073,7 +1073,7 @@ mod tests { #[test] fn compact_decryption_with_incorrect_diversifier() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, ivk, cv, cmu, epk, mut enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -1100,7 +1100,7 @@ mod tests { #[test] fn recovery_with_invalid_ovk() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (mut ovk, _, cv, cmu, epk, enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -1114,7 +1114,7 @@ mod tests { #[test] fn recovery_with_invalid_cv() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, _, _, cmu, epk, enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -1133,7 +1133,7 @@ mod tests { #[test] fn recovery_with_invalid_cmu() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, _, cv, _, epk, enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -1152,7 +1152,7 @@ mod tests { #[test] fn recovery_with_invalid_epk() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, _, cv, cmu, _, enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -1171,7 +1171,7 @@ mod tests { #[test] fn recovery_with_invalid_enc_tag() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, _, cv, cmu, epk, mut enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -1185,7 +1185,7 @@ mod tests { #[test] fn recovery_with_invalid_out_tag() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, _, cv, cmu, epk, enc_ciphertext, mut out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -1199,7 +1199,7 @@ mod tests { #[test] fn recovery_with_invalid_version_byte() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, _, cv, cmu, epk, mut enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -1221,7 +1221,7 @@ mod tests { #[test] fn recovery_with_invalid_diversifier() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, _, cv, cmu, epk, mut enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); @@ -1243,7 +1243,7 @@ mod tests { #[test] fn recovery_with_incorrect_diversifier() { - let mut rng = thread_rng(); + let mut rng = OsRng::new().expect("should be able to construct RNG"); let (ovk, _, cv, cmu, epk, mut enc_ciphertext, out_ciphertext) = random_enc_ciphertext(&mut rng); diff --git a/zcash_primitives/src/sapling.rs b/zcash_primitives/src/sapling.rs index 0ee808c..7f2b6f2 100644 --- a/zcash_primitives/src/sapling.rs +++ b/zcash_primitives/src/sapling.rs @@ -2,7 +2,7 @@ use ff::{BitIterator, PrimeField, PrimeFieldRepr}; use pairing::bls12_381::{Bls12, Fr, FrRepr}; -use rand::OsRng; +use rand_os::OsRng; use sapling_crypto::{ jubjub::{fs::Fs, FixedGenerators, JubjubBls12}, pedersen_hash::{pedersen_hash, Personalization}, diff --git a/zcash_primitives/src/transaction/tests.rs b/zcash_primitives/src/transaction/tests.rs index 7ef691f..81f8e21 100644 --- a/zcash_primitives/src/transaction/tests.rs +++ b/zcash_primitives/src/transaction/tests.rs @@ -1,6 +1,6 @@ use ff::Field; use pairing::bls12_381::Bls12; -use rand::thread_rng; +use rand_os::OsRng; use sapling_crypto::{ jubjub::{fs::Fs, FixedGenerators}, redjubjub::PrivateKey, @@ -197,7 +197,7 @@ fn tx_write_rejects_unexpected_binding_sig() { // Fails with an unexpected binding signature { - let rng = &mut thread_rng(); + let rng = &mut OsRng::new().expect("should be able to construct RNG"); let sk = PrivateKey::(Fs::random(rng)); let sig = sk.sign( b"Foo bar",