Skip to content

Commit

Permalink
Cleanup RNG usage after merging dalek-cryptography#57.
Browse files Browse the repository at this point in the history
  • Loading branch information
isislovecruft committed Dec 22, 2018
1 parent b9f078a commit 80ae5d0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 50 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ hex = "^0.3"
sha2 = "^0.8"
bincode = "^0.9"
criterion = "0.2"
rand_chacha = "0.1.0"

[[bench]]
name = "ed25519_benchmarks"
Expand Down
2 changes: 1 addition & 1 deletion benches/ed25519_benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod ed25519_benches {
use ed25519_dalek::Signature;
use ed25519_dalek::verify_batch;
use rand::thread_rng;
use rand::ThreadRng;
use rand::rngs::ThreadRng;
use sha2::Sha512;

fn sign(c: &mut Criterion) {
Expand Down
33 changes: 17 additions & 16 deletions src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl SecretKey {
/// # fn main() {
/// #
/// use rand::Rng;
/// use rand::OsRng;
/// use rand::rngs::OsRng;
/// use sha2::Sha512;
/// use ed25519_dalek::PublicKey;
/// use ed25519_dalek::SecretKey;
Expand All @@ -287,21 +287,19 @@ impl SecretKey {
///
/// ```
/// # extern crate rand;
/// # extern crate rand_chacha;
/// # extern crate sha2;
/// # extern crate ed25519_dalek;
/// #
/// # fn main() {
/// #
/// # use rand::Rng;
/// # use rand_chacha::ChaChaRng;
/// # use rand::SeedableRng;
/// # use rand::thread_rng;
/// # use sha2::Sha512;
/// # use ed25519_dalek::PublicKey;
/// # use ed25519_dalek::SecretKey;
/// # 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 public_key: PublicKey = PublicKey::from_secret::<Sha512>(&secret_key);
Expand Down Expand Up @@ -417,7 +415,8 @@ impl<'a> From<&'a SecretKey> for ExpandedSecretKey {
/// # #[cfg(all(feature = "std", feature = "sha2"))]
/// # fn main() {
/// #
/// use rand::{Rng, OsRng};
/// use rand::Rng;
/// use rand::rngs::OsRng;
/// use sha2::Sha512;
/// use ed25519_dalek::{SecretKey, ExpandedSecretKey};
///
Expand Down Expand Up @@ -453,7 +452,8 @@ impl ExpandedSecretKey {
/// # #[cfg(all(feature = "sha2", feature = "std"))]
/// # fn main() {
/// #
/// use rand::{Rng, OsRng};
/// use rand::Rng;
/// use rand::rngs::OsRng;
/// use sha2::Sha512;
/// use ed25519_dalek::{SecretKey, ExpandedSecretKey};
///
Expand Down Expand Up @@ -494,7 +494,8 @@ impl ExpandedSecretKey {
/// # #[cfg(all(feature = "sha2", feature = "std"))]
/// # 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::SignatureError;
///
Expand Down Expand Up @@ -544,7 +545,8 @@ impl ExpandedSecretKey {
/// # #[cfg(all(feature = "std", feature = "sha2"))]
/// # fn main() {
/// #
/// use rand::{Rng, OsRng};
/// use rand::Rng;
/// use rand::rngs::OsRng;
/// use sha2::Sha512;
/// use ed25519_dalek::{SecretKey, ExpandedSecretKey};
///
Expand Down Expand Up @@ -927,7 +929,8 @@ impl From<ExpandedSecretKey> for PublicKey {
/// * `messages` is a slice of byte slices, one per signed message.
/// * `signatures` is a slice of `Signature`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
///
Expand Down Expand Up @@ -1393,8 +1396,6 @@ mod test {
use std::string::String;
use std::vec::Vec;
use rand::thread_rng;
use rand_chacha::ChaChaRng;
use rand::SeedableRng;
use rand::rngs::ThreadRng;
use hex::FromHex;
use sha2::Sha512;
Expand Down Expand Up @@ -1428,15 +1429,15 @@ mod test {

#[test]
fn sign_verify() { // TestSignVerify
let mut csprng: ChaChaRng;
let mut csprng: ThreadRng;
let keypair: Keypair;
let good_sig: Signature;
let bad_sig: Signature;

let good: &[u8] = "test 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);
good_sig = keypair.sign::<Sha512>(&good);
bad_sig = keypair.sign::<Sha512>(&bad);
Expand Down Expand Up @@ -1530,7 +1531,7 @@ mod test {

#[test]
fn ed25519ph_sign_verify() {
let mut csprng: ChaChaRng;
let mut csprng: ThreadRng;
let keypair: Keypair;
let good_sig: Signature;
let bad_sig: Signature;
Expand All @@ -1553,7 +1554,7 @@ mod test {

let context: &[u8] = b"testing testing 1 2 3";

csprng = ChaChaRng::from_seed([0u8; 32]);
csprng = thread_rng();
keypair = Keypair::generate::<Sha512, _>(&mut csprng);
good_sig = keypair.sign_prehashed::<Sha512>(prehashed_good1, Some(context));
bad_sig = keypair.sign_prehashed::<Sha512>(prehashed_bad1, Some(context));
Expand Down
51 changes: 19 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
//! use ed25519_dalek::Signature;
//!
//! 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")))]
Expand All @@ -44,17 +44,15 @@
//!
//! ```
//! # extern crate rand;
//! # extern crate rand_chacha;
//! # extern crate sha2;
//! # extern crate ed25519_dalek;
//! # fn main() {
//! # use rand::Rng;
//! # use rand_chacha::ChaChaRng;
//! # use rand::SeedableRng;
//! # use rand::thread_rng;
//! # use sha2::Sha512;
//! # use ed25519_dalek::Keypair;
//! # 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 message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! let signature: Signature = keypair.sign::<Sha512>(message);
Expand All @@ -68,15 +66,13 @@
//! # extern crate rand;
//! # extern crate sha2;
//! # extern crate ed25519_dalek;
//! # extern crate rand_chacha;
//! # fn main() {
//! # use rand::Rng;
//! # use rand_chacha::ChaChaRng;
//! # use rand::SeedableRng;
//! # use rand::thread_rng;
//! # use sha2::Sha512;
//! # use ed25519_dalek::Keypair;
//! # 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 message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! # let signature: Signature = keypair.sign::<Sha512>(message);
Expand All @@ -91,16 +87,14 @@
//! # extern crate rand;
//! # extern crate sha2;
//! # extern crate ed25519_dalek;
//! # extern crate rand_chacha;
//! # fn main() {
//! # use rand::Rng;
//! # use rand_chacha::ChaChaRng;
//! # use rand::SeedableRng;
//! # use rand::thread_rng;
//! # use sha2::Sha512;
//! # use ed25519_dalek::Keypair;
//! # use ed25519_dalek::Signature;
//! 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 message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! # let signature: Signature = keypair.sign::<Sha512>(message);
Expand All @@ -122,14 +116,13 @@
//! # extern crate rand;
//! # extern crate sha2;
//! # extern crate ed25519_dalek;
//! # extern crate rand_chacha;
//! # fn main() {
//! # use rand::{Rng, SeedableRng};
//! # use rand_chacha::ChaChaRng;
//! # use rand::Rng;
//! # use rand::thread_rng;
//! # use sha2::Sha512;
//! # use ed25519_dalek::{Keypair, Signature, PublicKey};
//! 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 message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! # let signature: Signature = keypair.sign::<Sha512>(message);
Expand All @@ -147,15 +140,14 @@
//! ```
//! # extern crate rand;
//! # extern crate sha2;
//! # extern crate rand_chacha;
//! # extern crate ed25519_dalek;
//! # use rand::{Rng, SeedableRng};
//! # use rand_chacha::ChaChaRng;
//! # use rand::Rng;
//! # use rand::thread_rng;
//! # use sha2::Sha512;
//! # use ed25519_dalek::{Keypair, Signature, PublicKey, SecretKey, SignatureError};
//! # use ed25519_dalek::{PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, KEYPAIR_LENGTH, SIGNATURE_LENGTH};
//! # 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 message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! # let signature_orig: Signature = keypair_orig.sign::<Sha512>(message);
Expand Down Expand Up @@ -193,20 +185,19 @@
//! # extern crate rand;
//! # extern crate sha2;
//! # extern crate ed25519_dalek;
//! # extern crate rand_chacha;
//! # #[cfg(feature = "serde")]
//! extern crate serde;
//! # #[cfg(feature = "serde")]
//! extern crate bincode;
//!
//! # #[cfg(feature = "serde")]
//! # fn main() {
//! # use rand::{Rng, SeedableRng};
//! # use rand_chacha::ChaChaRng;
//! # use rand::Rng;
//! # use rand::thread_rng;
//! # use sha2::Sha512;
//! # use ed25519_dalek::{Keypair, Signature, PublicKey};
//! 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 message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! # let signature: Signature = keypair.sign::<Sha512>(message);
Expand All @@ -227,22 +218,21 @@
//! # extern crate rand;
//! # extern crate sha2;
//! # extern crate ed25519_dalek;
//! # extern crate rand_chacha;
//! # #[cfg(feature = "serde")]
//! # extern crate serde;
//! # #[cfg(feature = "serde")]
//! # extern crate bincode;
//! #
//! # #[cfg(feature = "serde")]
//! # fn main() {
//! # use rand::{Rng, SeedableRng};
//! # use rand_chacha::ChaChaRng;
//! # use rand::Rng;
//! # use rand::thread_rng;
//! # use sha2::Sha512;
//! # use ed25519_dalek::{Keypair, Signature, PublicKey};
//! # use bincode::{serialize, Infinite};
//! 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 message: &[u8] = "This is a test of the tsunami alert system.".as_bytes();
//! # let signature: Signature = keypair.sign::<Sha512>(message);
Expand Down Expand Up @@ -283,9 +273,6 @@ extern crate sha2;
#[cfg(test)]
extern crate hex;

#[cfg(test)]
extern crate rand_chacha;

#[cfg(feature = "serde")]
extern crate serde;

Expand Down

0 comments on commit 80ae5d0

Please sign in to comment.