Edition fixups: remove extern crate
, add idioms lint (#231)
Rust editions 2018+ do not require `extern crate` except for linking `alloc` and `std`.
This commit is contained in:
parent
44512a3e9c
commit
01ad6305f2
@ -48,13 +48,13 @@ required-features = ["batch"]
|
||||
|
||||
[features]
|
||||
default = ["std", "rand"]
|
||||
std = ["curve25519-dalek/std", "ed25519/std", "serde_crate/std", "sha2/std", "rand/std"]
|
||||
std = ["alloc", "curve25519-dalek/std", "ed25519/std", "serde_crate/std", "sha2/std", "rand/std"]
|
||||
alloc = ["curve25519-dalek/alloc", "rand/alloc", "zeroize/alloc"]
|
||||
nightly = ["curve25519-dalek/nightly"]
|
||||
serde = ["serde_crate", "serde_bytes", "ed25519/serde"]
|
||||
batch = ["merlin", "rand/std"]
|
||||
batch = ["alloc", "merlin", "rand/std"]
|
||||
# This feature enables deterministic batch verification.
|
||||
batch_deterministic = ["merlin", "rand", "rand_core"]
|
||||
batch_deterministic = ["alloc", "merlin", "rand", "rand_core"]
|
||||
asm = ["sha2/asm"]
|
||||
# This features turns off stricter checking for scalar malleability in signatures
|
||||
legacy_compatibility = []
|
||||
|
@ -7,12 +7,7 @@
|
||||
// Authors:
|
||||
// - isis agora lovecruft <isis@patternsinthevoid.net>
|
||||
|
||||
#[macro_use]
|
||||
extern crate criterion;
|
||||
extern crate ed25519_dalek;
|
||||
extern crate rand;
|
||||
|
||||
use criterion::Criterion;
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
|
||||
mod ed25519_benches {
|
||||
use super::*;
|
||||
|
57
src/batch.rs
57
src/batch.rs
@ -9,12 +9,7 @@
|
||||
|
||||
//! Batch signature verification.
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
extern crate alloc;
|
||||
#[cfg(feature = "alloc")]
|
||||
use alloc::vec::Vec;
|
||||
#[cfg(all(not(feature = "alloc"), feature = "std"))]
|
||||
use std::vec::Vec;
|
||||
|
||||
use core::convert::TryFrom;
|
||||
use core::iter::once;
|
||||
@ -29,9 +24,9 @@ pub use curve25519_dalek::digest::Digest;
|
||||
|
||||
use merlin::Transcript;
|
||||
|
||||
use rand::Rng;
|
||||
#[cfg(all(feature = "batch", not(feature = "batch_deterministic")))]
|
||||
use rand::thread_rng;
|
||||
use rand::Rng;
|
||||
#[cfg(all(not(feature = "batch"), feature = "batch_deterministic"))]
|
||||
use rand_core;
|
||||
|
||||
@ -101,7 +96,7 @@ impl rand_core::RngCore for ZeroRng {
|
||||
/// STROBE state based on external randomness, we're doing an
|
||||
/// `ENC_{state}(00000000000000000000000000000000)` operation, which is
|
||||
/// identical to the STROBE `MAC` operation.
|
||||
fn fill_bytes(&mut self, _dest: &mut [u8]) { }
|
||||
fn fill_bytes(&mut self, _dest: &mut [u8]) {}
|
||||
|
||||
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
|
||||
self.fill_bytes(dest);
|
||||
@ -193,9 +188,6 @@ fn zero_rng() -> ZeroRng {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// extern crate ed25519_dalek;
|
||||
/// extern crate rand;
|
||||
///
|
||||
/// use ed25519_dalek::verify_batch;
|
||||
/// use ed25519_dalek::Keypair;
|
||||
/// use ed25519_dalek::PublicKey;
|
||||
@ -215,24 +207,26 @@ fn zero_rng() -> ZeroRng {
|
||||
/// assert!(result.is_ok());
|
||||
/// # }
|
||||
/// ```
|
||||
#[cfg(all(any(feature = "batch", feature = "batch_deterministic"),
|
||||
any(feature = "alloc", feature = "std")))]
|
||||
#[allow(non_snake_case)]
|
||||
pub fn verify_batch(
|
||||
messages: &[&[u8]],
|
||||
signatures: &[ed25519::Signature],
|
||||
public_keys: &[PublicKey],
|
||||
) -> Result<(), SignatureError>
|
||||
{
|
||||
) -> Result<(), SignatureError> {
|
||||
// Return an Error if any of the vectors were not the same size as the others.
|
||||
if signatures.len() != messages.len() ||
|
||||
signatures.len() != public_keys.len() ||
|
||||
public_keys.len() != messages.len() {
|
||||
return Err(InternalError::ArrayLengthError{
|
||||
name_a: "signatures", length_a: signatures.len(),
|
||||
name_b: "messages", length_b: messages.len(),
|
||||
name_c: "public_keys", length_c: public_keys.len(),
|
||||
}.into());
|
||||
if signatures.len() != messages.len()
|
||||
|| signatures.len() != public_keys.len()
|
||||
|| public_keys.len() != messages.len()
|
||||
{
|
||||
return Err(InternalError::ArrayLengthError {
|
||||
name_a: "signatures",
|
||||
length_a: signatures.len(),
|
||||
name_b: "messages",
|
||||
length_b: messages.len(),
|
||||
name_c: "public_keys",
|
||||
length_c: public_keys.len(),
|
||||
}
|
||||
.into());
|
||||
}
|
||||
|
||||
// Convert all signatures to `InternalSignature`
|
||||
@ -242,13 +236,15 @@ pub fn verify_batch(
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
// Compute H(R || A || M) for each (signature, public_key, message) triplet
|
||||
let hrams: Vec<Scalar> = (0..signatures.len()).map(|i| {
|
||||
let mut h: Sha512 = Sha512::default();
|
||||
h.update(signatures[i].R.as_bytes());
|
||||
h.update(public_keys[i].as_bytes());
|
||||
h.update(&messages[i]);
|
||||
Scalar::from_hash(h)
|
||||
}).collect();
|
||||
let hrams: Vec<Scalar> = (0..signatures.len())
|
||||
.map(|i| {
|
||||
let mut h: Sha512 = Sha512::default();
|
||||
h.update(signatures[i].R.as_bytes());
|
||||
h.update(public_keys[i].as_bytes());
|
||||
h.update(&messages[i]);
|
||||
Scalar::from_hash(h)
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Collect the message lengths and the scalar portions of the signatures,
|
||||
// and add them into the transcript.
|
||||
@ -295,7 +291,8 @@ pub fn verify_batch(
|
||||
let id = EdwardsPoint::optional_multiscalar_mul(
|
||||
once(-B_coefficient).chain(zs.iter().cloned()).chain(zhrams),
|
||||
B.chain(Rs).chain(As),
|
||||
).ok_or(InternalError::VerifyError)?;
|
||||
)
|
||||
.ok_or(InternalError::VerifyError)?;
|
||||
|
||||
if id.is_identity() {
|
||||
Ok(())
|
||||
|
@ -28,4 +28,5 @@ const EXPANDED_SECRET_KEY_KEY_LENGTH: usize = 32;
|
||||
const EXPANDED_SECRET_KEY_NONCE_LENGTH: usize = 32;
|
||||
|
||||
/// The length of an "expanded" ed25519 key, `ExpandedSecretKey`, in bytes.
|
||||
pub const EXPANDED_SECRET_KEY_LENGTH: usize = EXPANDED_SECRET_KEY_KEY_LENGTH + EXPANDED_SECRET_KEY_NONCE_LENGTH;
|
||||
pub const EXPANDED_SECRET_KEY_LENGTH: usize =
|
||||
EXPANDED_SECRET_KEY_KEY_LENGTH + EXPANDED_SECRET_KEY_NONCE_LENGTH;
|
||||
|
@ -39,9 +39,14 @@ pub(crate) enum InternalError {
|
||||
/// Two arrays did not match in size, making the called signature
|
||||
/// verification method impossible.
|
||||
#[cfg(any(feature = "batch", feature = "batch_deterministic"))]
|
||||
ArrayLengthError{ name_a: &'static str, length_a: usize,
|
||||
name_b: &'static str, length_b: usize,
|
||||
name_c: &'static str, length_c: usize, },
|
||||
ArrayLengthError {
|
||||
name_a: &'static str,
|
||||
length_a: usize,
|
||||
name_b: &'static str,
|
||||
length_b: usize,
|
||||
name_c: &'static str,
|
||||
length_c: usize,
|
||||
},
|
||||
/// An ed25519ph signature can only take up to 255 octets of context.
|
||||
PrehashedContextLengthError,
|
||||
/// A mismatched (public, secret) key pair.
|
||||
@ -51,29 +56,37 @@ pub(crate) enum InternalError {
|
||||
impl Display for InternalError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
InternalError::PointDecompressionError
|
||||
=> write!(f, "Cannot decompress Edwards point"),
|
||||
InternalError::ScalarFormatError
|
||||
=> write!(f, "Cannot use scalar with high-bit set"),
|
||||
InternalError::BytesLengthError{ name: n, length: l}
|
||||
=> write!(f, "{} must be {} bytes in length", n, l),
|
||||
InternalError::VerifyError
|
||||
=> write!(f, "Verification equation was not satisfied"),
|
||||
InternalError::PointDecompressionError => write!(f, "Cannot decompress Edwards point"),
|
||||
InternalError::ScalarFormatError => write!(f, "Cannot use scalar with high-bit set"),
|
||||
InternalError::BytesLengthError { name: n, length: l } => {
|
||||
write!(f, "{} must be {} bytes in length", n, l)
|
||||
}
|
||||
InternalError::VerifyError => write!(f, "Verification equation was not satisfied"),
|
||||
#[cfg(any(feature = "batch", feature = "batch_deterministic"))]
|
||||
InternalError::ArrayLengthError{ name_a: na, length_a: la,
|
||||
name_b: nb, length_b: lb,
|
||||
name_c: nc, length_c: lc, }
|
||||
=> write!(f, "Arrays must be the same length: {} has length {},
|
||||
{} has length {}, {} has length {}.", na, la, nb, lb, nc, lc),
|
||||
InternalError::PrehashedContextLengthError
|
||||
=> write!(f, "An ed25519ph signature can only take up to 255 octets of context"),
|
||||
InternalError::ArrayLengthError {
|
||||
name_a: na,
|
||||
length_a: la,
|
||||
name_b: nb,
|
||||
length_b: lb,
|
||||
name_c: nc,
|
||||
length_c: lc,
|
||||
} => write!(
|
||||
f,
|
||||
"Arrays must be the same length: {} has length {},
|
||||
{} has length {}, {} has length {}.",
|
||||
na, la, nb, lb, nc, lc
|
||||
),
|
||||
InternalError::PrehashedContextLengthError => write!(
|
||||
f,
|
||||
"An ed25519ph signature can only take up to 255 octets of context"
|
||||
),
|
||||
InternalError::MismatchedKeypairError => write!(f, "Mismatched Keypair detected"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Error for InternalError { }
|
||||
impl Error for InternalError {}
|
||||
|
||||
/// Errors which may occur while processing signatures and keypairs.
|
||||
///
|
||||
|
@ -114,9 +114,6 @@ impl Keypair {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// extern crate rand;
|
||||
/// extern crate ed25519_dalek;
|
||||
///
|
||||
/// # #[cfg(feature = "std")]
|
||||
/// # fn main() {
|
||||
///
|
||||
@ -175,9 +172,6 @@ impl Keypair {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// extern crate ed25519_dalek;
|
||||
/// extern crate rand;
|
||||
///
|
||||
/// use ed25519_dalek::Digest;
|
||||
/// use ed25519_dalek::Keypair;
|
||||
/// use ed25519_dalek::Sha512;
|
||||
@ -222,9 +216,6 @@ impl Keypair {
|
||||
/// your own!):
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate ed25519_dalek;
|
||||
/// # extern crate rand;
|
||||
/// #
|
||||
/// # use ed25519_dalek::Digest;
|
||||
/// # use ed25519_dalek::Keypair;
|
||||
/// # use ed25519_dalek::Signature;
|
||||
@ -300,9 +291,6 @@ impl Keypair {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// extern crate ed25519_dalek;
|
||||
/// extern crate rand;
|
||||
///
|
||||
/// use ed25519_dalek::Digest;
|
||||
/// use ed25519_dalek::Keypair;
|
||||
/// use ed25519_dalek::Signature;
|
||||
|
51
src/lib.rs
51
src/lib.rs
@ -19,9 +19,6 @@
|
||||
//! the operating system's builtin PRNG:
|
||||
//!
|
||||
//! ```
|
||||
//! extern crate rand;
|
||||
//! extern crate ed25519_dalek;
|
||||
//!
|
||||
//! # #[cfg(feature = "std")]
|
||||
//! # fn main() {
|
||||
//! use rand::rngs::OsRng;
|
||||
@ -39,8 +36,6 @@
|
||||
//! 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;
|
||||
@ -56,8 +51,6 @@
|
||||
//! that `message`:
|
||||
//!
|
||||
//! ```
|
||||
//! # extern crate rand;
|
||||
//! # extern crate ed25519_dalek;
|
||||
//! # fn main() {
|
||||
//! # use rand::rngs::OsRng;
|
||||
//! # use ed25519_dalek::{Keypair, Signature, Signer};
|
||||
@ -74,8 +67,6 @@
|
||||
//! verify this signature:
|
||||
//!
|
||||
//! ```
|
||||
//! # extern crate rand;
|
||||
//! # extern crate ed25519_dalek;
|
||||
//! # fn main() {
|
||||
//! # use rand::rngs::OsRng;
|
||||
//! # use ed25519_dalek::Keypair;
|
||||
@ -101,8 +92,6 @@
|
||||
//! verify your signatures!)
|
||||
//!
|
||||
//! ```
|
||||
//! # extern crate rand;
|
||||
//! # extern crate ed25519_dalek;
|
||||
//! # fn main() {
|
||||
//! # use rand::rngs::OsRng;
|
||||
//! # use ed25519_dalek::{Keypair, Signature, Signer, PublicKey};
|
||||
@ -122,8 +111,6 @@
|
||||
//! And similarly, decoded from bytes with `::from_bytes()`:
|
||||
//!
|
||||
//! ```
|
||||
//! # extern crate rand;
|
||||
//! # extern crate ed25519_dalek;
|
||||
//! # use std::convert::TryFrom;
|
||||
//! # use rand::rngs::OsRng;
|
||||
//! # use std::convert::TryInto;
|
||||
@ -165,13 +152,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;
|
||||
//! # #[cfg(feature = "serde")]
|
||||
//! # extern crate bincode;
|
||||
//!
|
||||
//! # #[cfg(feature = "serde")]
|
||||
//! # fn main() {
|
||||
//! # use rand::rngs::OsRng;
|
||||
@ -195,13 +175,6 @@
|
||||
//! recipient may deserialise them and verify:
|
||||
//!
|
||||
//! ```
|
||||
//! # extern crate rand;
|
||||
//! # extern crate ed25519_dalek;
|
||||
//! # #[cfg(feature = "serde")]
|
||||
//! # extern crate serde_crate as serde;
|
||||
//! # #[cfg(feature = "serde")]
|
||||
//! # extern crate bincode;
|
||||
//! #
|
||||
//! # #[cfg(feature = "serde")]
|
||||
//! # fn main() {
|
||||
//! # use rand::rngs::OsRng;
|
||||
@ -232,40 +205,34 @@
|
||||
//! ```
|
||||
|
||||
#![no_std]
|
||||
#![warn(future_incompatible)]
|
||||
#![warn(future_incompatible, rust_2018_idioms)]
|
||||
#![deny(missing_docs)] // refuse to compile if documentation is missing
|
||||
#![cfg_attr(not(test), forbid(unsafe_code))]
|
||||
|
||||
#[cfg(any(feature = "batch", feature = "batch_deterministic"))]
|
||||
extern crate alloc;
|
||||
|
||||
#[cfg(any(feature = "std", test))]
|
||||
#[macro_use]
|
||||
extern crate std;
|
||||
|
||||
pub extern crate ed25519;
|
||||
|
||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||
extern crate alloc;
|
||||
extern crate curve25519_dalek;
|
||||
#[cfg(all(any(feature = "batch", feature = "batch_deterministic"), any(feature = "std", feature = "alloc")))]
|
||||
extern crate merlin;
|
||||
#[cfg(any(feature = "batch", feature = "std", feature = "alloc", test))]
|
||||
extern crate rand;
|
||||
#[cfg(feature = "serde")]
|
||||
extern crate serde_crate as serde;
|
||||
extern crate sha2;
|
||||
extern crate zeroize;
|
||||
|
||||
#[cfg(all(any(feature = "batch", feature = "batch_deterministic"), any(feature = "std", feature = "alloc")))]
|
||||
pub use ed25519;
|
||||
|
||||
#[cfg(any(feature = "batch", feature = "batch_deterministic"))]
|
||||
mod batch;
|
||||
mod constants;
|
||||
mod keypair;
|
||||
mod errors;
|
||||
mod keypair;
|
||||
mod public;
|
||||
mod secret;
|
||||
mod signature;
|
||||
|
||||
pub use curve25519_dalek::digest::Digest;
|
||||
|
||||
#[cfg(all(any(feature = "batch", feature = "batch_deterministic"), any(feature = "std", feature = "alloc")))]
|
||||
#[cfg(any(feature = "batch", feature = "batch_deterministic"))]
|
||||
pub use crate::batch::*;
|
||||
pub use crate::constants::*;
|
||||
pub use crate::errors::*;
|
||||
|
@ -28,7 +28,7 @@ use serde::de::Error as SerdeError;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
#[cfg(feature = "serde")]
|
||||
use serde_bytes::{Bytes as SerdeBytes, ByteBuf as SerdeByteBuf};
|
||||
use serde_bytes::{ByteBuf as SerdeByteBuf, Bytes as SerdeBytes};
|
||||
|
||||
use crate::constants::*;
|
||||
use crate::errors::*;
|
||||
@ -100,8 +100,6 @@ impl PublicKey {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate ed25519_dalek;
|
||||
/// #
|
||||
/// use ed25519_dalek::PublicKey;
|
||||
/// use ed25519_dalek::PUBLIC_KEY_LENGTH;
|
||||
/// use ed25519_dalek::SignatureError;
|
||||
@ -131,7 +129,8 @@ impl PublicKey {
|
||||
return Err(InternalError::BytesLengthError {
|
||||
name: "PublicKey",
|
||||
length: PUBLIC_KEY_LENGTH,
|
||||
}.into());
|
||||
}
|
||||
.into());
|
||||
}
|
||||
let mut bits: [u8; 32] = [0u8; 32];
|
||||
bits.copy_from_slice(&bytes[..32]);
|
||||
@ -195,7 +194,10 @@ impl PublicKey {
|
||||
let k: Scalar;
|
||||
|
||||
let ctx: &[u8] = context.unwrap_or(b"");
|
||||
debug_assert!(ctx.len() <= 255, "The context must not be longer than 255 octets.");
|
||||
debug_assert!(
|
||||
ctx.len() <= 255,
|
||||
"The context must not be longer than 255 octets."
|
||||
);
|
||||
|
||||
let minus_A: EdwardsPoint = -self.1;
|
||||
|
||||
@ -284,8 +286,7 @@ impl PublicKey {
|
||||
&self,
|
||||
message: &[u8],
|
||||
signature: &ed25519::Signature,
|
||||
) -> Result<(), SignatureError>
|
||||
{
|
||||
) -> Result<(), SignatureError> {
|
||||
let signature = InternalSignature::try_from(signature)?;
|
||||
|
||||
let mut h: Sha512 = Sha512::new();
|
||||
@ -326,12 +327,7 @@ impl Verifier<ed25519::Signature> for PublicKey {
|
||||
///
|
||||
/// Returns `Ok(())` if the signature is valid, and `Err` otherwise.
|
||||
#[allow(non_snake_case)]
|
||||
fn verify(
|
||||
&self,
|
||||
message: &[u8],
|
||||
signature: &ed25519::Signature
|
||||
) -> Result<(), SignatureError>
|
||||
{
|
||||
fn verify(&self, message: &[u8], signature: &ed25519::Signature) -> Result<(), SignatureError> {
|
||||
let signature = InternalSignature::try_from(signature)?;
|
||||
|
||||
let mut h: Sha512 = Sha512::new();
|
||||
|
@ -27,7 +27,7 @@ use serde::de::Error as SerdeError;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
#[cfg(feature = "serde")]
|
||||
use serde_bytes::{Bytes as SerdeBytes, ByteBuf as SerdeByteBuf};
|
||||
use serde_bytes::{ByteBuf as SerdeByteBuf, Bytes as SerdeBytes};
|
||||
|
||||
use zeroize::Zeroize;
|
||||
|
||||
@ -78,8 +78,6 @@ impl SecretKey {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate ed25519_dalek;
|
||||
/// #
|
||||
/// use ed25519_dalek::SecretKey;
|
||||
/// use ed25519_dalek::SECRET_KEY_LENGTH;
|
||||
/// use ed25519_dalek::SignatureError;
|
||||
@ -112,7 +110,8 @@ impl SecretKey {
|
||||
return Err(InternalError::BytesLengthError {
|
||||
name: "SecretKey",
|
||||
length: SECRET_KEY_LENGTH,
|
||||
}.into());
|
||||
}
|
||||
.into());
|
||||
}
|
||||
let mut bits: [u8; 32] = [0u8; 32];
|
||||
bits.copy_from_slice(&bytes[..32]);
|
||||
@ -125,9 +124,6 @@ impl SecretKey {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// extern crate rand;
|
||||
/// extern crate ed25519_dalek;
|
||||
///
|
||||
/// # #[cfg(feature = "std")]
|
||||
/// # fn main() {
|
||||
/// #
|
||||
@ -147,9 +143,6 @@ impl SecretKey {
|
||||
/// Afterwards, you can generate the corresponding public:
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate rand;
|
||||
/// # extern crate ed25519_dalek;
|
||||
/// #
|
||||
/// # fn main() {
|
||||
/// #
|
||||
/// # use rand::rngs::OsRng;
|
||||
@ -257,10 +250,6 @@ impl<'a> From<&'a SecretKey> for ExpandedSecretKey {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```ignore
|
||||
/// # extern crate rand;
|
||||
/// # extern crate sha2;
|
||||
/// # extern crate ed25519_dalek;
|
||||
/// #
|
||||
/// # fn main() {
|
||||
/// #
|
||||
/// use rand::rngs::OsRng;
|
||||
@ -273,7 +262,7 @@ impl<'a> From<&'a SecretKey> for ExpandedSecretKey {
|
||||
/// ```
|
||||
fn from(secret_key: &'a SecretKey) -> ExpandedSecretKey {
|
||||
let mut h: Sha512 = Sha512::default();
|
||||
let mut hash: [u8; 64] = [0u8; 64];
|
||||
let mut hash: [u8; 64] = [0u8; 64];
|
||||
let mut lower: [u8; 32] = [0u8; 32];
|
||||
let mut upper: [u8; 32] = [0u8; 32];
|
||||
|
||||
@ -283,11 +272,14 @@ impl<'a> From<&'a SecretKey> for ExpandedSecretKey {
|
||||
lower.copy_from_slice(&hash[00..32]);
|
||||
upper.copy_from_slice(&hash[32..64]);
|
||||
|
||||
lower[0] &= 248;
|
||||
lower[31] &= 63;
|
||||
lower[31] |= 64;
|
||||
lower[0] &= 248;
|
||||
lower[31] &= 63;
|
||||
lower[31] |= 64;
|
||||
|
||||
ExpandedSecretKey{ key: Scalar::from_bits(lower), nonce: upper, }
|
||||
ExpandedSecretKey {
|
||||
key: Scalar::from_bits(lower),
|
||||
nonce: upper,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -358,7 +350,9 @@ impl ExpandedSecretKey {
|
||||
let ctx: &[u8] = context.unwrap_or(b""); // By default, the context is an empty string.
|
||||
|
||||
if ctx.len() > 255 {
|
||||
return Err(SignatureError::from(InternalError::PrehashedContextLengthError));
|
||||
return Err(SignatureError::from(
|
||||
InternalError::PrehashedContextLengthError,
|
||||
));
|
||||
}
|
||||
|
||||
let ctx_len: u8 = ctx.len() as u8;
|
||||
@ -413,7 +407,8 @@ mod test {
|
||||
fn secret_key_zeroize_on_drop() {
|
||||
let secret_ptr: *const u8;
|
||||
|
||||
{ // scope for the secret to ensure it's been dropped
|
||||
{
|
||||
// scope for the secret to ensure it's been dropped
|
||||
let secret = SecretKey::from_bytes(&[0x15u8; 32][..]).unwrap();
|
||||
|
||||
secret_ptr = secret.0.as_ptr();
|
||||
|
@ -91,7 +91,7 @@ fn check_scalar(bytes: [u8; 32]) -> Result<Scalar, SignatureError> {
|
||||
//
|
||||
// This succeed-fast trick should succeed for roughly half of all scalars.
|
||||
if bytes[31] & 240 == 0 {
|
||||
return Ok(Scalar::from_bits(bytes))
|
||||
return Ok(Scalar::from_bits(bytes));
|
||||
}
|
||||
|
||||
match Scalar::from_canonical_bytes(bytes) {
|
||||
@ -167,7 +167,8 @@ impl InternalSignature {
|
||||
return Err(InternalError::BytesLengthError {
|
||||
name: "Signature",
|
||||
length: SIGNATURE_LENGTH,
|
||||
}.into());
|
||||
}
|
||||
.into());
|
||||
}
|
||||
let mut lower: [u8; 32] = [0u8; 32];
|
||||
let mut upper: [u8; 32] = [0u8; 32];
|
||||
@ -178,7 +179,7 @@ impl InternalSignature {
|
||||
let s: Scalar;
|
||||
|
||||
match check_scalar(upper) {
|
||||
Ok(x) => s = x,
|
||||
Ok(x) => s = x,
|
||||
Err(x) => return Err(x),
|
||||
}
|
||||
|
||||
|
194
tests/ed25519.rs
194
tests/ed25519.rs
@ -9,16 +9,7 @@
|
||||
|
||||
//! Integration tests for ed25519-dalek.
|
||||
|
||||
#[cfg(all(test, feature = "serde"))]
|
||||
extern crate bincode;
|
||||
extern crate ed25519_dalek;
|
||||
extern crate hex;
|
||||
extern crate sha2;
|
||||
extern crate rand;
|
||||
#[cfg(all(test, feature = "serde"))]
|
||||
extern crate serde_crate;
|
||||
#[cfg(all(test, feature = "serde"))]
|
||||
extern crate toml;
|
||||
use curve25519_dalek;
|
||||
|
||||
use ed25519_dalek::*;
|
||||
|
||||
@ -32,9 +23,9 @@ mod vectors {
|
||||
use sha2::{digest::Digest, Sha512};
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use std::io::BufReader;
|
||||
use std::io::BufRead;
|
||||
use std::fs::File;
|
||||
use std::io::BufRead;
|
||||
use std::io::BufReader;
|
||||
|
||||
use super::*;
|
||||
|
||||
@ -42,15 +33,18 @@ mod vectors {
|
||||
// package. It is a selection of test cases from
|
||||
// http://ed25519.cr.yp.to/python/sign.input
|
||||
#[test]
|
||||
fn against_reference_implementation() { // TestGolden
|
||||
fn against_reference_implementation() {
|
||||
// TestGolden
|
||||
let mut line: String;
|
||||
let mut lineno: usize = 0;
|
||||
|
||||
let f = File::open("TESTVECTORS");
|
||||
if f.is_err() {
|
||||
println!("This test is only available when the code has been cloned \
|
||||
from the git repository, since the TESTVECTORS file is large \
|
||||
and is therefore not included within the distributed crate.");
|
||||
println!(
|
||||
"This test is only available when the code has been cloned \
|
||||
from the git repository, since the TESTVECTORS file is large \
|
||||
and is therefore not included within the distributed crate."
|
||||
);
|
||||
panic!();
|
||||
}
|
||||
let file = BufReader::new(f.unwrap());
|
||||
@ -73,14 +67,17 @@ mod vectors {
|
||||
let keypair: Keypair = Keypair::from(secret);
|
||||
assert_eq!(expected_public, keypair.public_key());
|
||||
|
||||
// The signatures in the test vectors also include the message
|
||||
// at the end, but we just want R and S.
|
||||
// The signatures in the test vectors also include the message
|
||||
// at the end, but we just want R and S.
|
||||
let sig1: Signature = Signature::from_bytes(&sig_bytes[..64]).unwrap();
|
||||
let sig2: Signature = keypair.sign(&msg_bytes);
|
||||
|
||||
assert!(sig1 == sig2, "Signature bytes not equal on line {}", lineno);
|
||||
assert!(keypair.verify(&msg_bytes, &sig2).is_ok(),
|
||||
"Signature verification failed on line {}", lineno);
|
||||
assert!(
|
||||
keypair.verify(&msg_bytes, &sig2).is_ok(),
|
||||
"Signature verification failed on line {}",
|
||||
lineno
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -112,11 +109,19 @@ mod vectors {
|
||||
|
||||
let sig2: Signature = keypair.sign_prehashed(prehash_for_signing, None).unwrap();
|
||||
|
||||
assert!(sig1 == sig2,
|
||||
"Original signature from test vectors doesn't equal signature produced:\
|
||||
\noriginal:\n{:?}\nproduced:\n{:?}", sig1, sig2);
|
||||
assert!(keypair.verify_prehashed(prehash_for_verifying, None, &sig2).is_ok(),
|
||||
"Could not verify ed25519ph signature!");
|
||||
assert!(
|
||||
sig1 == sig2,
|
||||
"Original signature from test vectors doesn't equal signature produced:\
|
||||
\noriginal:\n{:?}\nproduced:\n{:?}",
|
||||
sig1,
|
||||
sig2
|
||||
);
|
||||
assert!(
|
||||
keypair
|
||||
.verify_prehashed(prehash_for_verifying, None, &sig2)
|
||||
.is_ok(),
|
||||
"Could not verify ed25519ph signature!"
|
||||
);
|
||||
}
|
||||
|
||||
// Taken from curve25519_dalek::constants::EIGHT_TORSION[4]
|
||||
@ -197,38 +202,45 @@ mod integrations {
|
||||
use rand::rngs::OsRng;
|
||||
|
||||
#[test]
|
||||
fn sign_verify() { // TestSignVerify
|
||||
fn sign_verify() {
|
||||
// TestSignVerify
|
||||
let keypair: Keypair;
|
||||
let good_sig: Signature;
|
||||
let bad_sig: Signature;
|
||||
let bad_sig: Signature;
|
||||
|
||||
let good: &[u8] = "test message".as_bytes();
|
||||
let bad: &[u8] = "wrong message".as_bytes();
|
||||
let bad: &[u8] = "wrong message".as_bytes();
|
||||
|
||||
let mut csprng = OsRng{};
|
||||
let mut csprng = OsRng {};
|
||||
|
||||
keypair = Keypair::generate(&mut csprng);
|
||||
keypair = Keypair::generate(&mut csprng);
|
||||
good_sig = keypair.sign(&good);
|
||||
bad_sig = keypair.sign(&bad);
|
||||
bad_sig = keypair.sign(&bad);
|
||||
|
||||
assert!(keypair.verify(&good, &good_sig).is_ok(),
|
||||
"Verification of a valid signature failed!");
|
||||
assert!(keypair.verify(&good, &bad_sig).is_err(),
|
||||
"Verification of a signature on a different message passed!");
|
||||
assert!(keypair.verify(&bad, &good_sig).is_err(),
|
||||
"Verification of a signature on a different message passed!");
|
||||
assert!(
|
||||
keypair.verify(&good, &good_sig).is_ok(),
|
||||
"Verification of a valid signature failed!"
|
||||
);
|
||||
assert!(
|
||||
keypair.verify(&good, &bad_sig).is_err(),
|
||||
"Verification of a signature on a different message passed!"
|
||||
);
|
||||
assert!(
|
||||
keypair.verify(&bad, &good_sig).is_err(),
|
||||
"Verification of a signature on a different message passed!"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ed25519ph_sign_verify() {
|
||||
let keypair: Keypair;
|
||||
let good_sig: Signature;
|
||||
let bad_sig: Signature;
|
||||
let bad_sig: Signature;
|
||||
|
||||
let good: &[u8] = b"test message";
|
||||
let bad: &[u8] = b"wrong message";
|
||||
let bad: &[u8] = b"wrong message";
|
||||
|
||||
let mut csprng = OsRng{};
|
||||
let mut csprng = OsRng;
|
||||
|
||||
// ugh… there's no `impl Copy for Sha512`… i hope we can all agree these are the same hashes
|
||||
let mut prehashed_good1: Sha512 = Sha512::default();
|
||||
@ -245,16 +257,32 @@ mod integrations {
|
||||
|
||||
let context: &[u8] = b"testing testing 1 2 3";
|
||||
|
||||
keypair = Keypair::generate(&mut csprng);
|
||||
good_sig = keypair.sign_prehashed(prehashed_good1, Some(context)).unwrap();
|
||||
bad_sig = keypair.sign_prehashed(prehashed_bad1, Some(context)).unwrap();
|
||||
keypair = Keypair::generate(&mut csprng);
|
||||
good_sig = keypair
|
||||
.sign_prehashed(prehashed_good1, Some(context))
|
||||
.unwrap();
|
||||
bad_sig = keypair
|
||||
.sign_prehashed(prehashed_bad1, Some(context))
|
||||
.unwrap();
|
||||
|
||||
assert!(keypair.verify_prehashed(prehashed_good2, Some(context), &good_sig).is_ok(),
|
||||
"Verification of a valid signature failed!");
|
||||
assert!(keypair.verify_prehashed(prehashed_good3, Some(context), &bad_sig).is_err(),
|
||||
"Verification of a signature on a different message passed!");
|
||||
assert!(keypair.verify_prehashed(prehashed_bad2, Some(context), &good_sig).is_err(),
|
||||
"Verification of a signature on a different message passed!");
|
||||
assert!(
|
||||
keypair
|
||||
.verify_prehashed(prehashed_good2, Some(context), &good_sig)
|
||||
.is_ok(),
|
||||
"Verification of a valid signature failed!"
|
||||
);
|
||||
assert!(
|
||||
keypair
|
||||
.verify_prehashed(prehashed_good3, Some(context), &bad_sig)
|
||||
.is_err(),
|
||||
"Verification of a signature on a different message passed!"
|
||||
);
|
||||
assert!(
|
||||
keypair
|
||||
.verify_prehashed(prehashed_bad2, Some(context), &good_sig)
|
||||
.is_err(),
|
||||
"Verification of a signature on a different message passed!"
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(feature = "batch")]
|
||||
@ -268,7 +296,7 @@ mod integrations {
|
||||
b"Fuck dumbin' it down, spit ice, skip jewellery: Molotov cocktails on me like accessories.",
|
||||
b"Hey, I never cared about your bucks, so if I run up with a mask on, probably got a gas can too.",
|
||||
b"And I'm not here to fill 'er up. Nope, we came to riot, here to incite, we don't want any of your stuff.", ];
|
||||
let mut csprng = OsRng{};
|
||||
let mut csprng = OsRng;
|
||||
let mut keypairs: Vec<Keypair> = Vec::new();
|
||||
let mut signatures: Vec<Signature> = Vec::new();
|
||||
|
||||
@ -289,7 +317,7 @@ mod integrations {
|
||||
#[derive(Debug, serde_crate::Serialize, serde_crate::Deserialize)]
|
||||
#[serde(crate = "serde_crate")]
|
||||
struct Demo {
|
||||
keypair: Keypair
|
||||
keypair: Keypair,
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "serde"))]
|
||||
@ -300,37 +328,29 @@ mod serialisation {
|
||||
static BINCODE_INT_LENGTH: usize = 8;
|
||||
|
||||
static PUBLIC_KEY_BYTES: [u8; PUBLIC_KEY_LENGTH] = [
|
||||
130, 039, 155, 015, 062, 076, 188, 063,
|
||||
124, 122, 026, 251, 233, 253, 225, 220,
|
||||
014, 041, 166, 120, 108, 035, 254, 077,
|
||||
160, 083, 172, 058, 219, 042, 086, 120, ];
|
||||
130, 039, 155, 015, 062, 076, 188, 063, 124, 122, 026, 251, 233, 253, 225, 220, 014, 041,
|
||||
166, 120, 108, 035, 254, 077, 160, 083, 172, 058, 219, 042, 086, 120,
|
||||
];
|
||||
|
||||
static SECRET_KEY_BYTES: [u8; SECRET_KEY_LENGTH] = [
|
||||
062, 070, 027, 163, 092, 182, 011, 003,
|
||||
077, 234, 098, 004, 011, 127, 079, 228,
|
||||
243, 187, 150, 073, 201, 137, 076, 022,
|
||||
085, 251, 152, 002, 241, 042, 072, 054, ];
|
||||
062, 070, 027, 163, 092, 182, 011, 003, 077, 234, 098, 004, 011, 127, 079, 228, 243, 187,
|
||||
150, 073, 201, 137, 076, 022, 085, 251, 152, 002, 241, 042, 072, 054,
|
||||
];
|
||||
|
||||
/// Signature with the above keypair of a blank message.
|
||||
static SIGNATURE_BYTES: [u8; SIGNATURE_LENGTH] = [
|
||||
010, 126, 151, 143, 157, 064, 047, 001,
|
||||
196, 140, 179, 058, 226, 152, 018, 102,
|
||||
160, 123, 080, 016, 210, 086, 196, 028,
|
||||
053, 231, 012, 157, 169, 019, 158, 063,
|
||||
045, 154, 238, 007, 053, 185, 227, 229,
|
||||
079, 108, 213, 080, 124, 252, 084, 167,
|
||||
216, 085, 134, 144, 129, 149, 041, 081,
|
||||
063, 120, 126, 100, 092, 059, 050, 011, ];
|
||||
010, 126, 151, 143, 157, 064, 047, 001, 196, 140, 179, 058, 226, 152, 018, 102, 160, 123,
|
||||
080, 016, 210, 086, 196, 028, 053, 231, 012, 157, 169, 019, 158, 063, 045, 154, 238, 007,
|
||||
053, 185, 227, 229, 079, 108, 213, 080, 124, 252, 084, 167, 216, 085, 134, 144, 129, 149,
|
||||
041, 081, 063, 120, 126, 100, 092, 059, 050, 011,
|
||||
];
|
||||
|
||||
static KEYPAIR_BYTES: [u8; KEYPAIR_LENGTH] = [
|
||||
239, 085, 017, 235, 167, 103, 034, 062,
|
||||
007, 010, 032, 146, 113, 039, 096, 174,
|
||||
003, 219, 232, 166, 240, 121, 167, 013,
|
||||
098, 238, 122, 116, 193, 114, 215, 213,
|
||||
175, 181, 075, 166, 224, 164, 140, 146,
|
||||
053, 120, 010, 037, 104, 094, 136, 225,
|
||||
249, 102, 171, 160, 097, 132, 015, 071,
|
||||
035, 056, 000, 074, 130, 168, 225, 071, ];
|
||||
239, 085, 017, 235, 167, 103, 034, 062, 007, 010, 032, 146, 113, 039, 096, 174, 003, 219,
|
||||
232, 166, 240, 121, 167, 013, 098, 238, 122, 116, 193, 114, 215, 213, 175, 181, 075, 166,
|
||||
224, 164, 140, 146, 053, 120, 010, 037, 104, 094, 136, 225, 249, 102, 171, 160, 097, 132,
|
||||
015, 071, 035, 056, 000, 074, 130, 168, 225, 071,
|
||||
];
|
||||
|
||||
#[test]
|
||||
fn serialize_deserialize_signature_bincode() {
|
||||
@ -356,7 +376,10 @@ mod serialisation {
|
||||
let encoded_public_key: Vec<u8> = bincode::serialize(&public_key).unwrap();
|
||||
let decoded_public_key: PublicKey = bincode::deserialize(&encoded_public_key).unwrap();
|
||||
|
||||
assert_eq!(&PUBLIC_KEY_BYTES[..], &encoded_public_key[encoded_public_key.len() - PUBLIC_KEY_LENGTH..]);
|
||||
assert_eq!(
|
||||
&PUBLIC_KEY_BYTES[..],
|
||||
&encoded_public_key[encoded_public_key.len() - PUBLIC_KEY_LENGTH..]
|
||||
);
|
||||
assert_eq!(public_key, decoded_public_key);
|
||||
}
|
||||
|
||||
@ -415,7 +438,9 @@ mod serialisation {
|
||||
|
||||
#[test]
|
||||
fn serialize_deserialize_keypair_toml() {
|
||||
let demo = Demo { keypair: Keypair::from_bytes(&KEYPAIR_BYTES).unwrap() };
|
||||
let demo = Demo {
|
||||
keypair: Keypair::from_bytes(&KEYPAIR_BYTES).unwrap(),
|
||||
};
|
||||
|
||||
println!("\n\nWrite to toml");
|
||||
let demo_toml = toml::to_string(&demo).unwrap();
|
||||
@ -427,13 +452,19 @@ mod serialisation {
|
||||
#[test]
|
||||
fn serialize_public_key_size() {
|
||||
let public_key: PublicKey = PublicKey::from_bytes(&PUBLIC_KEY_BYTES).unwrap();
|
||||
assert_eq!(bincode::serialized_size(&public_key).unwrap() as usize, BINCODE_INT_LENGTH + PUBLIC_KEY_LENGTH);
|
||||
assert_eq!(
|
||||
bincode::serialized_size(&public_key).unwrap() as usize,
|
||||
BINCODE_INT_LENGTH + PUBLIC_KEY_LENGTH
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize_signature_size() {
|
||||
let signature: Signature = Signature::from_bytes(&SIGNATURE_BYTES).unwrap();
|
||||
assert_eq!(bincode::serialized_size(&signature).unwrap() as usize, SIGNATURE_LENGTH);
|
||||
assert_eq!(
|
||||
bincode::serialized_size(&signature).unwrap() as usize,
|
||||
SIGNATURE_LENGTH
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -448,6 +479,9 @@ mod serialisation {
|
||||
#[test]
|
||||
fn serialize_keypair_size() {
|
||||
let keypair = Keypair::from_bytes(&KEYPAIR_BYTES).unwrap();
|
||||
assert_eq!(bincode::serialized_size(&keypair).unwrap() as usize, BINCODE_INT_LENGTH + KEYPAIR_LENGTH);
|
||||
assert_eq!(
|
||||
bincode::serialized_size(&keypair).unwrap() as usize,
|
||||
BINCODE_INT_LENGTH + KEYPAIR_LENGTH
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user