Skip to content

Commit

Permalink
Add generate method to SecretKey and Keypair.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvc94ch committed Apr 15, 2021
1 parent 925eb9e commit 3a54d0e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 86 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ features = ["nightly", "batch"]
[dependencies]
curve25519-dalek = { version = "3", default-features = false }
ed25519 = { version = "1", default-features = false }
merlin = { version = "2", default-features = false, optional = true }
rand = { version = "0.7", default-features = false, optional = true }
rand_core = { version = "0.5", default-features = false, optional = true }
merlin = { version = "3.0.0", default-features = false, optional = true }
rand = { version = "0.8.3", default-features = false, optional = true }
rand_core = { version = "0.6.2", default-features = false, optional = true }
serde_crate = { package = "serde", version = "1.0", default-features = false, optional = true }
serde_bytes = { version = "0.11", optional = true }
sha2 = { version = "0.9", default-features = false }
zeroize = { version = "1", default-features = false, features = ["zeroize_derive"] }

[dev-dependencies]
hex = "^0.4"
bincode = "1.0"
bincode = "1.3.3"
serde_json = "1.0"
criterion = "0.3"
rand = "0.7"
rand = "0.8.3"
serde_crate = { package = "serde", version = "1.0", features = ["derive"] }
toml = { version = "0.5" }
toml = "0.5.8"

[[bench]]
name = "ed25519_benchmarks"
Expand Down
45 changes: 29 additions & 16 deletions src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! ed25519 keypairs.
#[cfg(feature = "rand")]
use rand::{CryptoRng, RngCore};
use rand::{rngs::OsRng, CryptoRng, RngCore};

#[cfg(feature = "serde")]
use serde::de::Error as SerdeError;
Expand Down Expand Up @@ -90,6 +90,28 @@ impl Keypair {
Ok(Keypair{ secret: secret, public: public })
}

/// Generate an ed25519 keypair.
///
/// # Example
///
/// ```
/// extern crate ed25519_dalek;
///
/// # #[cfg(feature = "std")]
/// # fn main() {
/// #
/// use ed25519_dalek::Keypair;
/// let keypair = Keypair::generate();
/// # }
/// #
/// # #[cfg(not(feature = "std"))]
/// # fn main() { }
/// ```
#[cfg(feature = "rand")]
pub fn generate() -> Keypair {
Self::generate_with_rng(&mut OsRng {})
}

/// Generate an ed25519 keypair.
///
/// # Example
Expand All @@ -106,7 +128,7 @@ impl Keypair {
/// use ed25519_dalek::Signature;
///
/// let mut csprng = OsRng{};
/// let keypair: Keypair = Keypair::generate(&mut csprng);
/// let keypair: Keypair = Keypair::generate_with_rng(&mut csprng);
///
/// # }
/// #
Expand All @@ -124,11 +146,11 @@ impl Keypair {
/// which is available with `use sha2::Sha512` as in the example above.
/// Other suitable hash functions include Keccak-512 and Blake2b-512.
#[cfg(feature = "rand")]
pub fn generate<R>(csprng: &mut R) -> Keypair
pub fn generate_with_rng<R>(csprng: &mut R) -> Keypair
where
R: CryptoRng + RngCore,
{
let sk: SecretKey = SecretKey::generate(csprng);
let sk: SecretKey = SecretKey::generate_with_rng(csprng);
let pk: PublicKey = (&sk).into();

Keypair{ public: pk, secret: sk }
Expand All @@ -154,18 +176,15 @@ impl Keypair {
///
/// ```
/// extern crate ed25519_dalek;
/// extern crate rand;
///
/// use ed25519_dalek::Digest;
/// use ed25519_dalek::Keypair;
/// use ed25519_dalek::Sha512;
/// use ed25519_dalek::Signature;
/// use rand::rngs::OsRng;
///
/// # #[cfg(feature = "std")]
/// # fn main() {
/// let mut csprng = OsRng{};
/// let keypair: Keypair = Keypair::generate(&mut csprng);
/// let keypair: Keypair = Keypair::generate();
/// let message: &[u8] = b"All I want is to pet all of the dogs.";
///
/// // Create a hash digest object which we'll feed the message into:
Expand Down Expand Up @@ -201,18 +220,15 @@ impl Keypair {
///
/// ```
/// # extern crate ed25519_dalek;
/// # extern crate rand;
/// #
/// # use ed25519_dalek::Digest;
/// # use ed25519_dalek::Keypair;
/// # use ed25519_dalek::Signature;
/// # use ed25519_dalek::SignatureError;
/// # use ed25519_dalek::Sha512;
/// # use rand::rngs::OsRng;
/// #
/// # fn do_test() -> Result<Signature, SignatureError> {
/// # let mut csprng = OsRng{};
/// # let keypair: Keypair = Keypair::generate(&mut csprng);
/// # let keypair: Keypair = Keypair::generate();
/// # let message: &[u8] = b"All I want is to pet all of the dogs.";
/// # let mut prehashed: Sha512 = Sha512::new();
/// # prehashed.update(message);
Expand Down Expand Up @@ -278,18 +294,15 @@ impl Keypair {
///
/// ```
/// extern crate ed25519_dalek;
/// extern crate rand;
///
/// use ed25519_dalek::Digest;
/// use ed25519_dalek::Keypair;
/// use ed25519_dalek::Signature;
/// use ed25519_dalek::SignatureError;
/// use ed25519_dalek::Sha512;
/// use rand::rngs::OsRng;
///
/// # fn do_test() -> Result<(), SignatureError> {
/// let mut csprng = OsRng{};
/// let keypair: Keypair = Keypair::generate(&mut csprng);
/// let keypair: Keypair = Keypair::generate();
/// let message: &[u8] = b"All I want is to pet all of the dogs.";
///
/// let mut prehashed: Sha512 = Sha512::new();
Expand Down
40 changes: 8 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
//! the operating system's builtin PRNG:
//!
//! ```
//! extern crate rand;
//! extern crate ed25519_dalek;
//!
//! # #[cfg(feature = "std")]
//! # fn main() {
//! use rand::rngs::OsRng;
//! use ed25519_dalek::Keypair;
//! use ed25519_dalek::Signature;
//!
//! let mut csprng = OsRng{};
//! let keypair: Keypair = Keypair::generate(&mut csprng);
//! let keypair: Keypair = Keypair::generate();
//! # }
//! #
//! # #[cfg(not(feature = "std"))]
Expand All @@ -39,13 +36,10 @@
//! We can now use this `keypair` to sign a message:
//!
//! ```
//! # extern crate rand;
//! # extern crate ed25519_dalek;
//! # fn main() {
//! # use rand::rngs::OsRng;
//! # use ed25519_dalek::Keypair;
//! # let mut csprng = OsRng{};
//! # let keypair: Keypair = Keypair::generate(&mut csprng);
//! # let keypair: Keypair = Keypair::generate();
//! use ed25519_dalek::{Signature, Signer};
//! let message: &[u8] = b"This is a test of the tsunami alert system.";
//! let signature: Signature = keypair.sign(message);
Expand All @@ -56,13 +50,10 @@
//! that `message`:
//!
//! ```
//! # extern crate rand;
//! # extern crate ed25519_dalek;
//! # fn main() {
//! # use rand::rngs::OsRng;
//! # use ed25519_dalek::{Keypair, Signature, Signer};
//! # let mut csprng = OsRng{};
//! # let keypair: Keypair = Keypair::generate(&mut csprng);
//! # let keypair: Keypair = Keypair::generate();
//! # let message: &[u8] = b"This is a test of the tsunami alert system.";
//! # let signature: Signature = keypair.sign(message);
//! use ed25519_dalek::Verifier;
Expand All @@ -74,16 +65,13 @@
//! verify this signature:
//!
//! ```
//! # extern crate rand;
//! # extern crate ed25519_dalek;
//! # fn main() {
//! # use rand::rngs::OsRng;
//! # use ed25519_dalek::Keypair;
//! # use ed25519_dalek::Signature;
//! # use ed25519_dalek::Signer;
//! use ed25519_dalek::{PublicKey, Verifier};
//! # let mut csprng = OsRng{};
//! # let keypair: Keypair = Keypair::generate(&mut csprng);
//! # let keypair: Keypair = Keypair::generate();
//! # let message: &[u8] = b"This is a test of the tsunami alert system.";
//! # let signature: Signature = keypair.sign(message);
//!
Expand All @@ -101,14 +89,11 @@
//! verify your signatures!)
//!
//! ```
//! # extern crate rand;
//! # extern crate ed25519_dalek;
//! # fn main() {
//! # use rand::rngs::OsRng;
//! # use ed25519_dalek::{Keypair, Signature, Signer, PublicKey};
//! use ed25519_dalek::{PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, KEYPAIR_LENGTH, SIGNATURE_LENGTH};
//! # let mut csprng = OsRng{};
//! # let keypair: Keypair = Keypair::generate(&mut csprng);
//! # let keypair: Keypair = Keypair::generate();
//! # let message: &[u8] = b"This is a test of the tsunami alert system.";
//! # let signature: Signature = keypair.sign(message);
//! # let public_key: PublicKey = keypair.public;
Expand All @@ -123,15 +108,12 @@
//! And similarly, decoded from bytes with `::from_bytes()`:
//!
//! ```
//! # extern crate rand;
//! # extern crate ed25519_dalek;
//! # use std::convert::TryFrom;
//! # use rand::rngs::OsRng;
//! # use ed25519_dalek::{Keypair, Signature, Signer, 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 = OsRng{};
//! # let keypair_orig: Keypair = Keypair::generate(&mut csprng);
//! # let keypair_orig: Keypair = Keypair::generate();
//! # let message: &[u8] = b"This is a test of the tsunami alert system.";
//! # let signature_orig: Signature = keypair_orig.sign(message);
//! # let public_key_bytes: [u8; PUBLIC_KEY_LENGTH] = keypair_orig.public.to_bytes();
Expand Down Expand Up @@ -165,7 +147,6 @@
//! For example, using [bincode](https://github.com/TyOverby/bincode):
//!
//! ```
//! # extern crate rand;
//! # extern crate ed25519_dalek;
//! # #[cfg(feature = "serde")]
//! # extern crate serde_crate as serde;
Expand All @@ -174,11 +155,9 @@
//!
//! # #[cfg(feature = "serde")]
//! # fn main() {
//! # use rand::rngs::OsRng;
//! # use ed25519_dalek::{Keypair, Signature, Signer, Verifier, PublicKey};
//! use bincode::serialize;
//! # let mut csprng = OsRng{};
//! # let keypair: Keypair = Keypair::generate(&mut csprng);
//! # let keypair: Keypair = Keypair::generate();
//! # let message: &[u8] = b"This is a test of the tsunami alert system.";
//! # let signature: Signature = keypair.sign(message);
//! # let public_key: PublicKey = keypair.public;
Expand All @@ -195,7 +174,6 @@
//! recipient may deserialise them and verify:
//!
//! ```
//! # extern crate rand;
//! # extern crate ed25519_dalek;
//! # #[cfg(feature = "serde")]
//! # extern crate serde_crate as serde;
Expand All @@ -204,13 +182,11 @@
//! #
//! # #[cfg(feature = "serde")]
//! # fn main() {
//! # use rand::rngs::OsRng;
//! # use ed25519_dalek::{Keypair, Signature, Signer, Verifier, PublicKey};
//! # use bincode::serialize;
//! use bincode::deserialize;
//!
//! # let mut csprng = OsRng{};
//! # let keypair: Keypair = Keypair::generate(&mut csprng);
//! # let keypair: Keypair = Keypair::generate();
//! let message: &[u8] = b"This is a test of the tsunami alert system.";
//! # let signature: Signature = keypair.sign(message);
//! # let public_key: PublicKey = keypair.public;
Expand Down
Loading

0 comments on commit 3a54d0e

Please sign in to comment.