Cleanup RNG usage after merging #57.

This commit is contained in:
Isis Lovecruft 2018-12-22 12:20:59 +00:00
parent b9f078af16
commit 80ae5d0683
No known key found for this signature in database
GPG key ID: AB41313533E8E812
4 changed files with 37 additions and 50 deletions

View file

@ -43,7 +43,6 @@ hex = "^0.3"
sha2 = "^0.8" sha2 = "^0.8"
bincode = "^0.9" bincode = "^0.9"
criterion = "0.2" criterion = "0.2"
rand_chacha = "0.1.0"
[[bench]] [[bench]]
name = "ed25519_benchmarks" name = "ed25519_benchmarks"

View file

@ -23,7 +23,7 @@ mod ed25519_benches {
use ed25519_dalek::Signature; use ed25519_dalek::Signature;
use ed25519_dalek::verify_batch; use ed25519_dalek::verify_batch;
use rand::thread_rng; use rand::thread_rng;
use rand::ThreadRng; use rand::rngs::ThreadRng;
use sha2::Sha512; use sha2::Sha512;
fn sign(c: &mut Criterion) { fn sign(c: &mut Criterion) {

View file

@ -267,7 +267,7 @@ impl SecretKey {
/// # fn main() { /// # fn main() {
/// # /// #
/// use rand::Rng; /// use rand::Rng;
/// use rand::OsRng; /// use rand::rngs::OsRng;
/// use sha2::Sha512; /// use sha2::Sha512;
/// use ed25519_dalek::PublicKey; /// use ed25519_dalek::PublicKey;
/// use ed25519_dalek::SecretKey; /// use ed25519_dalek::SecretKey;
@ -287,21 +287,19 @@ impl SecretKey {
/// ///
/// ``` /// ```
/// # extern crate rand; /// # extern crate rand;
/// # extern crate rand_chacha;
/// # extern crate sha2; /// # extern crate sha2;
/// # extern crate ed25519_dalek; /// # extern crate ed25519_dalek;
/// # /// #
/// # fn main() { /// # fn main() {
/// # /// #
/// # use rand::Rng; /// # use rand::Rng;
/// # use rand_chacha::ChaChaRng; /// # use rand::thread_rng;
/// # use rand::SeedableRng;
/// # use sha2::Sha512; /// # use sha2::Sha512;
/// # use ed25519_dalek::PublicKey; /// # use ed25519_dalek::PublicKey;
/// # use ed25519_dalek::SecretKey; /// # use ed25519_dalek::SecretKey;
/// # use ed25519_dalek::Signature; /// # use ed25519_dalek::Signature;
/// # /// #
/// # let mut csprng: ChaChaRng = ChaChaRng::from_seed([0u8; 32]); /// # let mut csprng = thread_rng();
/// # let secret_key: SecretKey = SecretKey::generate(&mut csprng); /// # let secret_key: SecretKey = SecretKey::generate(&mut csprng);
/// ///
/// let public_key: PublicKey = PublicKey::from_secret::<Sha512>(&secret_key); /// let public_key: PublicKey = PublicKey::from_secret::<Sha512>(&secret_key);
@ -417,7 +415,8 @@ impl<'a> From<&'a SecretKey> for ExpandedSecretKey {
/// # #[cfg(all(feature = "std", feature = "sha2"))] /// # #[cfg(all(feature = "std", feature = "sha2"))]
/// # fn main() { /// # fn main() {
/// # /// #
/// use rand::{Rng, OsRng}; /// use rand::Rng;
/// use rand::rngs::OsRng;
/// use sha2::Sha512; /// use sha2::Sha512;
/// use ed25519_dalek::{SecretKey, ExpandedSecretKey}; /// use ed25519_dalek::{SecretKey, ExpandedSecretKey};
/// ///
@ -453,7 +452,8 @@ impl ExpandedSecretKey {
/// # #[cfg(all(feature = "sha2", feature = "std"))] /// # #[cfg(all(feature = "sha2", feature = "std"))]
/// # fn main() { /// # fn main() {
/// # /// #
/// use rand::{Rng, OsRng}; /// use rand::Rng;
/// use rand::rngs::OsRng;
/// use sha2::Sha512; /// use sha2::Sha512;
/// use ed25519_dalek::{SecretKey, ExpandedSecretKey}; /// use ed25519_dalek::{SecretKey, ExpandedSecretKey};
/// ///
@ -494,7 +494,8 @@ impl ExpandedSecretKey {
/// # #[cfg(all(feature = "sha2", feature = "std"))] /// # #[cfg(all(feature = "sha2", feature = "std"))]
/// # fn do_test() -> Result<ExpandedSecretKey, SignatureError> { /// # fn do_test() -> Result<ExpandedSecretKey, SignatureError> {
/// # /// #
/// use rand::{Rng, OsRng}; /// use rand::Rng;
/// use rand::rngs::OsRng;
/// use ed25519_dalek::{SecretKey, ExpandedSecretKey}; /// use ed25519_dalek::{SecretKey, ExpandedSecretKey};
/// use ed25519_dalek::SignatureError; /// use ed25519_dalek::SignatureError;
/// ///
@ -544,7 +545,8 @@ impl ExpandedSecretKey {
/// # #[cfg(all(feature = "std", feature = "sha2"))] /// # #[cfg(all(feature = "std", feature = "sha2"))]
/// # fn main() { /// # fn main() {
/// # /// #
/// use rand::{Rng, OsRng}; /// use rand::Rng;
/// use rand::rngs::OsRng;
/// use sha2::Sha512; /// use sha2::Sha512;
/// use ed25519_dalek::{SecretKey, ExpandedSecretKey}; /// use ed25519_dalek::{SecretKey, ExpandedSecretKey};
/// ///
@ -927,7 +929,8 @@ impl From<ExpandedSecretKey> for PublicKey {
/// * `messages` is a slice of byte slices, one per signed message. /// * `messages` is a slice of byte slices, one per signed message.
/// * `signatures` is a slice of `Signature`s. /// * `signatures` is a slice of `Signature`s.
/// * `public_keys` is a slice of `PublicKey`s. /// * `public_keys` is a slice of `PublicKey`s.
/// * `csprng` is an implementation of `Rng + CryptoRng`, such as `rand::ThreadRng`. /// * `csprng` is an implementation of `Rng + CryptoRng`, such as
/// `rand::rngs::ThreadRng`.
/// ///
/// # Panics /// # Panics
/// ///
@ -1393,8 +1396,6 @@ mod test {
use std::string::String; use std::string::String;
use std::vec::Vec; use std::vec::Vec;
use rand::thread_rng; use rand::thread_rng;
use rand_chacha::ChaChaRng;
use rand::SeedableRng;
use rand::rngs::ThreadRng; use rand::rngs::ThreadRng;
use hex::FromHex; use hex::FromHex;
use sha2::Sha512; use sha2::Sha512;
@ -1428,7 +1429,7 @@ mod test {
#[test] #[test]
fn sign_verify() { // TestSignVerify fn sign_verify() { // TestSignVerify
let mut csprng: ChaChaRng; let mut csprng: ThreadRng;
let keypair: Keypair; let keypair: Keypair;
let good_sig: Signature; let good_sig: Signature;
let bad_sig: Signature; let bad_sig: Signature;
@ -1436,7 +1437,7 @@ mod test {
let good: &[u8] = "test message".as_bytes(); let good: &[u8] = "test message".as_bytes();
let bad: &[u8] = "wrong message".as_bytes(); let bad: &[u8] = "wrong message".as_bytes();
csprng = ChaChaRng::from_seed([0u8; 32]); csprng = thread_rng();
keypair = Keypair::generate::<Sha512, _>(&mut csprng); keypair = Keypair::generate::<Sha512, _>(&mut csprng);
good_sig = keypair.sign::<Sha512>(&good); good_sig = keypair.sign::<Sha512>(&good);
bad_sig = keypair.sign::<Sha512>(&bad); bad_sig = keypair.sign::<Sha512>(&bad);
@ -1530,7 +1531,7 @@ mod test {
#[test] #[test]
fn ed25519ph_sign_verify() { fn ed25519ph_sign_verify() {
let mut csprng: ChaChaRng; let mut csprng: ThreadRng;
let keypair: Keypair; let keypair: Keypair;
let good_sig: Signature; let good_sig: Signature;
let bad_sig: Signature; let bad_sig: Signature;
@ -1553,7 +1554,7 @@ mod test {
let context: &[u8] = b"testing testing 1 2 3"; let context: &[u8] = b"testing testing 1 2 3";
csprng = ChaChaRng::from_seed([0u8; 32]); csprng = thread_rng();
keypair = Keypair::generate::<Sha512, _>(&mut csprng); keypair = Keypair::generate::<Sha512, _>(&mut csprng);
good_sig = keypair.sign_prehashed::<Sha512>(prehashed_good1, Some(context)); good_sig = keypair.sign_prehashed::<Sha512>(prehashed_good1, Some(context));
bad_sig = keypair.sign_prehashed::<Sha512>(prehashed_bad1, Some(context)); bad_sig = keypair.sign_prehashed::<Sha512>(prehashed_bad1, Some(context));

View file

@ -33,7 +33,7 @@
//! use ed25519_dalek::Signature; //! use ed25519_dalek::Signature;
//! //!
//! let mut csprng: OsRng = OsRng::new().unwrap(); //! let mut csprng: OsRng = OsRng::new().unwrap();
//! let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng); //! let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng); // The `_` can be the type of `csprng`
//! # } //! # }
//! # //! #
//! # #[cfg(any(not(feature = "std"), not(feature = "sha2")))] //! # #[cfg(any(not(feature = "std"), not(feature = "sha2")))]
@ -44,17 +44,15 @@
//! //!
//! ``` //! ```
//! # extern crate rand; //! # extern crate rand;
//! # extern crate rand_chacha;
//! # extern crate sha2; //! # extern crate sha2;
//! # extern crate ed25519_dalek; //! # extern crate ed25519_dalek;
//! # fn main() { //! # fn main() {
//! # use rand::Rng; //! # use rand::Rng;
//! # use rand_chacha::ChaChaRng; //! # use rand::thread_rng;
//! # use rand::SeedableRng;
//! # use sha2::Sha512; //! # use sha2::Sha512;
//! # use ed25519_dalek::Keypair; //! # use ed25519_dalek::Keypair;
//! # use ed25519_dalek::Signature; //! # use ed25519_dalek::Signature;
//! # let mut csprng: ChaChaRng = ChaChaRng::from_seed([0u8; 32]); //! # let mut csprng = thread_rng();
//! # let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng); //! # let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng);
//! let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes(); //! let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! let signature: Signature = keypair.sign::<Sha512>(message); //! let signature: Signature = keypair.sign::<Sha512>(message);
@ -68,15 +66,13 @@
//! # extern crate rand; //! # extern crate rand;
//! # extern crate sha2; //! # extern crate sha2;
//! # extern crate ed25519_dalek; //! # extern crate ed25519_dalek;
//! # extern crate rand_chacha;
//! # fn main() { //! # fn main() {
//! # use rand::Rng; //! # use rand::Rng;
//! # use rand_chacha::ChaChaRng; //! # use rand::thread_rng;
//! # use rand::SeedableRng;
//! # use sha2::Sha512; //! # use sha2::Sha512;
//! # use ed25519_dalek::Keypair; //! # use ed25519_dalek::Keypair;
//! # use ed25519_dalek::Signature; //! # use ed25519_dalek::Signature;
//! # let mut csprng: ChaChaRng = ChaChaRng::from_seed([0u8; 32]); //! # let mut csprng = thread_rng();
//! # let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng); //! # let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng);
//! # let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes(); //! # let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! # let signature: Signature = keypair.sign::<Sha512>(message); //! # let signature: Signature = keypair.sign::<Sha512>(message);
@ -91,16 +87,14 @@
//! # extern crate rand; //! # extern crate rand;
//! # extern crate sha2; //! # extern crate sha2;
//! # extern crate ed25519_dalek; //! # extern crate ed25519_dalek;
//! # extern crate rand_chacha;
//! # fn main() { //! # fn main() {
//! # use rand::Rng; //! # use rand::Rng;
//! # use rand_chacha::ChaChaRng; //! # use rand::thread_rng;
//! # use rand::SeedableRng;
//! # use sha2::Sha512; //! # use sha2::Sha512;
//! # use ed25519_dalek::Keypair; //! # use ed25519_dalek::Keypair;
//! # use ed25519_dalek::Signature; //! # use ed25519_dalek::Signature;
//! use ed25519_dalek::PublicKey; //! use ed25519_dalek::PublicKey;
//! # let mut csprng: ChaChaRng = ChaChaRng::from_seed([0u8; 32]); //! # let mut csprng = thread_rng();
//! # let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng); //! # let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng);
//! # let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes(); //! # let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! # let signature: Signature = keypair.sign::<Sha512>(message); //! # let signature: Signature = keypair.sign::<Sha512>(message);
@ -122,14 +116,13 @@
//! # extern crate rand; //! # extern crate rand;
//! # extern crate sha2; //! # extern crate sha2;
//! # extern crate ed25519_dalek; //! # extern crate ed25519_dalek;
//! # extern crate rand_chacha;
//! # fn main() { //! # fn main() {
//! # use rand::{Rng, SeedableRng}; //! # use rand::Rng;
//! # use rand_chacha::ChaChaRng; //! # use rand::thread_rng;
//! # use sha2::Sha512; //! # use sha2::Sha512;
//! # use ed25519_dalek::{Keypair, Signature, PublicKey}; //! # use ed25519_dalek::{Keypair, Signature, PublicKey};
//! use ed25519_dalek::{PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, KEYPAIR_LENGTH, SIGNATURE_LENGTH}; //! use ed25519_dalek::{PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, KEYPAIR_LENGTH, SIGNATURE_LENGTH};
//! # let mut csprng: ChaChaRng = ChaChaRng::from_seed([0u8; 32]); //! # let mut csprng = thread_rng();
//! # let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng); //! # let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng);
//! # let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes(); //! # let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! # let signature: Signature = keypair.sign::<Sha512>(message); //! # let signature: Signature = keypair.sign::<Sha512>(message);
@ -147,15 +140,14 @@
//! ``` //! ```
//! # extern crate rand; //! # extern crate rand;
//! # extern crate sha2; //! # extern crate sha2;
//! # extern crate rand_chacha;
//! # extern crate ed25519_dalek; //! # extern crate ed25519_dalek;
//! # use rand::{Rng, SeedableRng}; //! # use rand::Rng;
//! # use rand_chacha::ChaChaRng; //! # use rand::thread_rng;
//! # use sha2::Sha512; //! # use sha2::Sha512;
//! # use ed25519_dalek::{Keypair, Signature, PublicKey, SecretKey, SignatureError}; //! # use ed25519_dalek::{Keypair, Signature, PublicKey, SecretKey, SignatureError};
//! # use ed25519_dalek::{PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, KEYPAIR_LENGTH, SIGNATURE_LENGTH}; //! # use ed25519_dalek::{PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, KEYPAIR_LENGTH, SIGNATURE_LENGTH};
//! # fn do_test() -> Result<(SecretKey, PublicKey, Keypair, Signature), SignatureError> { //! # fn do_test() -> Result<(SecretKey, PublicKey, Keypair, Signature), SignatureError> {
//! # let mut csprng: ChaChaRng = ChaChaRng::from_seed([0u8; 32]); //! # let mut csprng = thread_rng();
//! # let keypair_orig: Keypair = Keypair::generate::<Sha512, _>(&mut csprng); //! # let keypair_orig: Keypair = Keypair::generate::<Sha512, _>(&mut csprng);
//! # let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes(); //! # let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! # let signature_orig: Signature = keypair_orig.sign::<Sha512>(message); //! # let signature_orig: Signature = keypair_orig.sign::<Sha512>(message);
@ -193,7 +185,6 @@
//! # extern crate rand; //! # extern crate rand;
//! # extern crate sha2; //! # extern crate sha2;
//! # extern crate ed25519_dalek; //! # extern crate ed25519_dalek;
//! # extern crate rand_chacha;
//! # #[cfg(feature = "serde")] //! # #[cfg(feature = "serde")]
//! extern crate serde; //! extern crate serde;
//! # #[cfg(feature = "serde")] //! # #[cfg(feature = "serde")]
@ -201,12 +192,12 @@
//! //!
//! # #[cfg(feature = "serde")] //! # #[cfg(feature = "serde")]
//! # fn main() { //! # fn main() {
//! # use rand::{Rng, SeedableRng}; //! # use rand::Rng;
//! # use rand_chacha::ChaChaRng; //! # use rand::thread_rng;
//! # use sha2::Sha512; //! # use sha2::Sha512;
//! # use ed25519_dalek::{Keypair, Signature, PublicKey}; //! # use ed25519_dalek::{Keypair, Signature, PublicKey};
//! use bincode::{serialize, Infinite}; //! use bincode::{serialize, Infinite};
//! # let mut csprng: ChaChaRng = ChaChaRng::from_seed([0u8; 32]); //! # let mut csprng = thread_rng();
//! # let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng); //! # let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng);
//! # let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes(); //! # let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! # let signature: Signature = keypair.sign::<Sha512>(message); //! # let signature: Signature = keypair.sign::<Sha512>(message);
@ -227,7 +218,6 @@
//! # extern crate rand; //! # extern crate rand;
//! # extern crate sha2; //! # extern crate sha2;
//! # extern crate ed25519_dalek; //! # extern crate ed25519_dalek;
//! # extern crate rand_chacha;
//! # #[cfg(feature = "serde")] //! # #[cfg(feature = "serde")]
//! # extern crate serde; //! # extern crate serde;
//! # #[cfg(feature = "serde")] //! # #[cfg(feature = "serde")]
@ -235,14 +225,14 @@
//! # //! #
//! # #[cfg(feature = "serde")] //! # #[cfg(feature = "serde")]
//! # fn main() { //! # fn main() {
//! # use rand::{Rng, SeedableRng}; //! # use rand::Rng;
//! # use rand_chacha::ChaChaRng; //! # use rand::thread_rng;
//! # use sha2::Sha512; //! # use sha2::Sha512;
//! # use ed25519_dalek::{Keypair, Signature, PublicKey}; //! # use ed25519_dalek::{Keypair, Signature, PublicKey};
//! # use bincode::{serialize, Infinite}; //! # use bincode::{serialize, Infinite};
//! use bincode::{deserialize}; //! use bincode::{deserialize};
//! //!
//! # let mut csprng: ChaChaRng = ChaChaRng::from_seed([0u8; 32]); //! # let mut csprng = thread_rng();
//! # let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng); //! # let keypair: Keypair = Keypair::generate::<Sha512, _>(&mut csprng);
//! let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes(); //! let message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! # let signature: Signature = keypair.sign::<Sha512>(message); //! # let signature: Signature = keypair.sign::<Sha512>(message);
@ -283,9 +273,6 @@ extern crate sha2;
#[cfg(test)] #[cfg(test)]
extern crate hex; extern crate hex;
#[cfg(test)]
extern crate rand_chacha;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
extern crate serde; extern crate serde;