diff --git a/utils/buffer/src/buffer_pool/mod.rs b/utils/buffer/src/buffer_pool/mod.rs index c172f2ee2..4f8e5e8db 100644 --- a/utils/buffer/src/buffer_pool/mod.rs +++ b/utils/buffer/src/buffer_pool/mod.rs @@ -232,7 +232,8 @@ impl InnerMemory { match self.raw_len { 0 => self.raw_offset = offset, _ => { - self.pool.copy_within(self.raw_offset..self.raw_offset+self.raw_len, offset); + self.pool + .copy_within(self.raw_offset..self.raw_offset + self.raw_len, offset); self.raw_offset = offset; } } diff --git a/vendor/ed25519-dalek/benches/ed25519_benchmarks.rs b/vendor/ed25519-dalek/benches/ed25519_benchmarks.rs index 45dce3570..fc86dc05d 100644 --- a/vendor/ed25519-dalek/benches/ed25519_benchmarks.rs +++ b/vendor/ed25519-dalek/benches/ed25519_benchmarks.rs @@ -16,23 +16,21 @@ use criterion::Criterion; mod ed25519_benches { use super::*; + use ed25519_dalek::verify_batch; use ed25519_dalek::ExpandedSecretKey; use ed25519_dalek::Keypair; use ed25519_dalek::PublicKey; use ed25519_dalek::Signature; use ed25519_dalek::Signer; - use ed25519_dalek::verify_batch; - use rand::thread_rng; use rand::prelude::ThreadRng; + use rand::thread_rng; fn sign(c: &mut Criterion) { let mut csprng: ThreadRng = thread_rng(); let keypair: Keypair = Keypair::generate(&mut csprng); let msg: &[u8] = b""; - c.bench_function("Ed25519 signing", move |b| { - b.iter(| | keypair.sign(msg)) - }); + c.bench_function("Ed25519 signing", move |b| b.iter(|| keypair.sign(msg))); } fn sign_expanded_key(c: &mut Criterion) { @@ -40,9 +38,9 @@ mod ed25519_benches { let keypair: Keypair = Keypair::generate(&mut csprng); let expanded: ExpandedSecretKey = (&keypair.secret).into(); let msg: &[u8] = b""; - + c.bench_function("Ed25519 signing with an expanded secret key", move |b| { - b.iter(| | expanded.sign(msg, &keypair.public)) + b.iter(|| expanded.sign(msg, &keypair.public)) }); } @@ -51,9 +49,9 @@ mod ed25519_benches { let keypair: Keypair = Keypair::generate(&mut csprng); let msg: &[u8] = b""; let sig: Signature = keypair.sign(msg); - + c.bench_function("Ed25519 signature verification", move |b| { - b.iter(| | keypair.verify(msg, &sig)) + b.iter(|| keypair.verify(msg, &sig)) }); } @@ -64,7 +62,7 @@ mod ed25519_benches { let sig: Signature = keypair.sign(msg); c.bench_function("Ed25519 strict signature verification", move |b| { - b.iter(| | keypair.verify_strict(msg, &sig)) + b.iter(|| keypair.verify_strict(msg, &sig)) }); } @@ -75,10 +73,12 @@ mod ed25519_benches { "Ed25519 batch signature verification", |b, &&size| { let mut csprng: ThreadRng = thread_rng(); - let keypairs: Vec = (0..size).map(|_| Keypair::generate(&mut csprng)).collect(); + let keypairs: Vec = + (0..size).map(|_| Keypair::generate(&mut csprng)).collect(); let msg: &[u8] = b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; let messages: Vec<&[u8]> = (0..size).map(|_| msg).collect(); - let signatures: Vec = keypairs.iter().map(|key| key.sign(&msg)).collect(); + let signatures: Vec = + keypairs.iter().map(|key| key.sign(&msg)).collect(); let public_keys: Vec = keypairs.iter().map(|key| key.public).collect(); b.iter(|| verify_batch(&messages[..], &signatures[..], &public_keys[..])); @@ -91,11 +91,11 @@ mod ed25519_benches { let mut csprng: ThreadRng = thread_rng(); c.bench_function("Ed25519 keypair generation", move |b| { - b.iter(| | Keypair::generate(&mut csprng)) + b.iter(|| Keypair::generate(&mut csprng)) }); } - criterion_group!{ + criterion_group! { name = ed25519_benches; config = Criterion::default(); targets = @@ -108,6 +108,4 @@ mod ed25519_benches { } } -criterion_main!( - ed25519_benches::ed25519_benches, -); +criterion_main!(ed25519_benches::ed25519_benches,); diff --git a/vendor/ed25519-dalek/src/batch.rs b/vendor/ed25519-dalek/src/batch.rs index 3a4b8e9dc..fe0aaf162 100644 --- a/vendor/ed25519-dalek/src/batch.rs +++ b/vendor/ed25519-dalek/src/batch.rs @@ -29,9 +29,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 +101,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); @@ -215,24 +215,30 @@ fn zero_rng() -> ZeroRng { /// assert!(result.is_ok()); /// # } /// ``` -#[cfg(all(any(feature = "batch", feature = "batch_deterministic"), - any(feature = "alloc", feature = "std")))] +#[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 +248,15 @@ pub fn verify_batch( .collect::, _>>()?; // Compute H(R || A || M) for each (signature, public_key, message) triplet - let hrams: Vec = (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 = (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 +303,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(()) diff --git a/vendor/ed25519-dalek/src/constants.rs b/vendor/ed25519-dalek/src/constants.rs index f8ccb840b..4dc48a04b 100644 --- a/vendor/ed25519-dalek/src/constants.rs +++ b/vendor/ed25519-dalek/src/constants.rs @@ -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; diff --git a/vendor/ed25519-dalek/src/errors.rs b/vendor/ed25519-dalek/src/errors.rs index b66fae0fc..539760672 100644 --- a/vendor/ed25519-dalek/src/errors.rs +++ b/vendor/ed25519-dalek/src/errors.rs @@ -38,9 +38,14 @@ pub(crate) enum InternalError { VerifyError, /// Two arrays did not match in size, making the called signature /// verification method impossible. - 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, } @@ -48,27 +53,35 @@ 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::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::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::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" + ), } } } #[cfg(feature = "std")] -impl Error for InternalError { } +impl Error for InternalError {} /// Errors which may occur while processing signatures and keypairs. /// diff --git a/vendor/ed25519-dalek/src/keypair.rs b/vendor/ed25519-dalek/src/keypair.rs index 55af2df5b..c008b16f0 100644 --- a/vendor/ed25519-dalek/src/keypair.rs +++ b/vendor/ed25519-dalek/src/keypair.rs @@ -17,7 +17,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}; pub use sha2::Sha512; @@ -82,12 +82,16 @@ impl Keypair { return Err(InternalError::BytesLengthError { name: "Keypair", length: KEYPAIR_LENGTH, - }.into()); + } + .into()); } let secret = SecretKey::from_bytes(&bytes[..SECRET_KEY_LENGTH])?; let public = PublicKey::from_bytes(&bytes[SECRET_KEY_LENGTH..])?; - Ok(Keypair{ secret: secret, public: public }) + Ok(Keypair { + secret: secret, + public: public, + }) } /// Generate an ed25519 keypair. @@ -131,7 +135,10 @@ impl Keypair { let sk: SecretKey = SecretKey::generate(csprng); let pk: PublicKey = (&sk).into(); - Keypair{ public: pk, secret: sk } + Keypair { + public: pk, + secret: sk, + } } /// Sign a `prehashed_message` with this `Keypair` using the @@ -244,16 +251,17 @@ impl Keypair { { let expanded: ExpandedSecretKey = (&self.secret).into(); // xxx thanks i hate this - expanded.sign_prehashed(prehashed_message, &self.public, context).into() + expanded + .sign_prehashed(prehashed_message, &self.public, context) + .into() } /// Verify a signature on a message with this keypair's public key. pub fn verify( &self, message: &[u8], - signature: &ed25519::Signature - ) -> Result<(), SignatureError> - { + signature: &ed25519::Signature, + ) -> Result<(), SignatureError> { self.public.verify(message, signature) } @@ -329,7 +337,8 @@ impl Keypair { where D: Digest, { - self.public.verify_prehashed(prehashed_message, context, signature) + self.public + .verify_prehashed(prehashed_message, context, signature) } /// Strictly verify a signature on a message with this keypair's public key. @@ -399,8 +408,7 @@ impl Keypair { &self, message: &[u8], signature: &ed25519::Signature, - ) -> Result<(), SignatureError> - { + ) -> Result<(), SignatureError> { self.public.verify_strict(message, signature) } } diff --git a/vendor/ed25519-dalek/src/lib.rs b/vendor/ed25519-dalek/src/lib.rs index 2c42e76a7..c0aa68468 100644 --- a/vendor/ed25519-dalek/src/lib.rs +++ b/vendor/ed25519-dalek/src/lib.rs @@ -233,11 +233,9 @@ #![allow(clippy::all)] #![allow(dead_code)] - #![no_std] #![warn(future_incompatible)] #![deny(missing_docs)] // refuse to compile if documentation is missing - #![cfg(not(test))] #![forbid(unsafe_code)] @@ -250,7 +248,10 @@ 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")))] +#[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; @@ -259,18 +260,24 @@ 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")))] +#[cfg(all( + any(feature = "batch", feature = "batch_deterministic"), + any(feature = "std", feature = "alloc") +))] 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(all( + any(feature = "batch", feature = "batch_deterministic"), + any(feature = "std", feature = "alloc") +))] pub use crate::batch::*; pub use crate::constants::*; pub use crate::errors::*; diff --git a/vendor/ed25519-dalek/src/public.rs b/vendor/ed25519-dalek/src/public.rs index 342adf6c6..5d0940c85 100644 --- a/vendor/ed25519-dalek/src/public.rs +++ b/vendor/ed25519-dalek/src/public.rs @@ -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::*; @@ -131,7 +131,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 +196,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 +288,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 +329,7 @@ impl Verifier 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(); diff --git a/vendor/ed25519-dalek/src/secret.rs b/vendor/ed25519-dalek/src/secret.rs index 2ca3a129c..03c2f9c4c 100644 --- a/vendor/ed25519-dalek/src/secret.rs +++ b/vendor/ed25519-dalek/src/secret.rs @@ -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; @@ -108,7 +108,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]); @@ -264,7 +265,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]; @@ -274,11 +275,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, + } } } @@ -371,7 +375,8 @@ impl ExpandedSecretKey { return Err(InternalError::BytesLengthError { name: "ExpandedSecretKey", length: EXPANDED_SECRET_KEY_LENGTH, - }.into()); + } + .into()); } let mut lower: [u8; 32] = [0u8; 32]; let mut upper: [u8; 32] = [0u8; 32]; @@ -451,7 +456,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; @@ -528,7 +535,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(); diff --git a/vendor/ed25519-dalek/src/signature.rs b/vendor/ed25519-dalek/src/signature.rs index 880a78b4c..774e0603c 100644 --- a/vendor/ed25519-dalek/src/signature.rs +++ b/vendor/ed25519-dalek/src/signature.rs @@ -92,7 +92,7 @@ fn check_scalar(bytes: [u8; 32]) -> Result { // // 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) { @@ -168,7 +168,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]; @@ -179,7 +180,7 @@ impl InternalSignature { let s: Scalar; match check_scalar(upper) { - Ok(x) => s = x, + Ok(x) => s = x, Err(x) => return Err(x), } diff --git a/vendor/ed25519-dalek/tests/ed25519.rs b/vendor/ed25519-dalek/tests/ed25519.rs index 696e2876b..4ccf5d4c7 100644 --- a/vendor/ed25519-dalek/tests/ed25519.rs +++ b/vendor/ed25519-dalek/tests/ed25519.rs @@ -13,10 +13,10 @@ 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; +extern crate sha2; #[cfg(all(test, feature = "serde"))] extern crate toml; @@ -30,9 +30,9 @@ use sha2::Sha512; mod vectors { use ed25519::signature::Signature as _; - use std::io::BufReader; - use std::io::BufRead; use std::fs::File; + use std::io::BufRead; + use std::io::BufReader; use super::*; @@ -40,15 +40,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 \ + 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."); + and is therefore not included within the distributed crate." + ); panic!(); } let file = BufReader::new(f.unwrap()); @@ -67,16 +70,22 @@ mod vectors { let secret: SecretKey = SecretKey::from_bytes(&sec_bytes[..SECRET_KEY_LENGTH]).unwrap(); let public: PublicKey = PublicKey::from_bytes(&pub_bytes[..PUBLIC_KEY_LENGTH]).unwrap(); - let keypair: Keypair = Keypair{ secret: secret, public: public }; + let keypair: Keypair = Keypair { + secret: secret, + public: public, + }; - // 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 + ); } } @@ -95,7 +104,10 @@ mod vectors { let secret: SecretKey = SecretKey::from_bytes(&sec_bytes[..SECRET_KEY_LENGTH]).unwrap(); let public: PublicKey = PublicKey::from_bytes(&pub_bytes[..PUBLIC_KEY_LENGTH]).unwrap(); - let keypair: Keypair = Keypair{ secret: secret, public: public }; + let keypair: Keypair = Keypair { + secret: secret, + public: public, + }; let sig1: Signature = Signature::from_bytes(&sig_bytes[..]).unwrap(); let mut prehash_for_signing: Sha512 = Sha512::default(); @@ -106,11 +118,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!" + ); } } @@ -120,38 +140,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); - - 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!"); + 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!" + ); } #[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(); @@ -168,16 +195,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(); - - 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!"); + 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!" + ); } #[cfg(feature = "batch")] @@ -191,7 +234,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 = Vec::new(); let mut signatures: Vec = Vec::new(); @@ -209,7 +252,7 @@ mod integrations { #[test] fn pubkey_from_secret_and_expanded_secret() { - let mut csprng = OsRng{}; + let mut csprng = OsRng {}; let secret: SecretKey = SecretKey::generate(&mut csprng); let expanded_secret: ExpandedSecretKey = (&secret).into(); let public_from_secret: PublicKey = (&secret).into(); // XXX eww @@ -223,7 +266,7 @@ mod integrations { #[cfg(all(test, feature = "serde"))] #[derive(Debug, serde_crate::Serialize, serde_crate::Deserialize)] struct Demo { - keypair: Keypair + keypair: Keypair, } #[cfg(all(test, feature = "serde"))] @@ -236,37 +279,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() { @@ -292,7 +327,10 @@ mod serialisation { let encoded_public_key: Vec = 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); } @@ -329,23 +367,34 @@ mod serialisation { #[test] fn serialize_deserialize_expanded_secret_key_bincode() { - let expanded_secret_key = ExpandedSecretKey::from(&SecretKey::from_bytes(&SECRET_KEY_BYTES).unwrap()); - let encoded_expanded_secret_key: Vec = bincode::serialize(&expanded_secret_key).unwrap(); - let decoded_expanded_secret_key: ExpandedSecretKey = bincode::deserialize(&encoded_expanded_secret_key).unwrap(); + let expanded_secret_key = + ExpandedSecretKey::from(&SecretKey::from_bytes(&SECRET_KEY_BYTES).unwrap()); + let encoded_expanded_secret_key: Vec = + bincode::serialize(&expanded_secret_key).unwrap(); + let decoded_expanded_secret_key: ExpandedSecretKey = + bincode::deserialize(&encoded_expanded_secret_key).unwrap(); for i in 0..EXPANDED_SECRET_KEY_LENGTH { - assert_eq!(expanded_secret_key.to_bytes()[i], decoded_expanded_secret_key.to_bytes()[i]); + assert_eq!( + expanded_secret_key.to_bytes()[i], + decoded_expanded_secret_key.to_bytes()[i] + ); } } #[test] fn serialize_deserialize_expanded_secret_key_json() { - let expanded_secret_key = ExpandedSecretKey::from(&SecretKey::from_bytes(&SECRET_KEY_BYTES).unwrap()); + let expanded_secret_key = + ExpandedSecretKey::from(&SecretKey::from_bytes(&SECRET_KEY_BYTES).unwrap()); let encoded_expanded_secret_key = serde_json::to_string(&expanded_secret_key).unwrap(); - let decoded_expanded_secret_key: ExpandedSecretKey = serde_json::from_str(&encoded_expanded_secret_key).unwrap(); + let decoded_expanded_secret_key: ExpandedSecretKey = + serde_json::from_str(&encoded_expanded_secret_key).unwrap(); for i in 0..EXPANDED_SECRET_KEY_LENGTH { - assert_eq!(expanded_secret_key.to_bytes()[i], decoded_expanded_secret_key.to_bytes()[i]); + assert_eq!( + expanded_secret_key.to_bytes()[i], + decoded_expanded_secret_key.to_bytes()[i] + ); } } @@ -373,7 +422,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(); @@ -385,30 +436,46 @@ 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] fn serialize_secret_key_size() { let secret_key: SecretKey = SecretKey::from_bytes(&SECRET_KEY_BYTES).unwrap(); - assert_eq!(bincode::serialized_size(&secret_key).unwrap() as usize, BINCODE_INT_LENGTH + SECRET_KEY_LENGTH); + assert_eq!( + bincode::serialized_size(&secret_key).unwrap() as usize, + BINCODE_INT_LENGTH + SECRET_KEY_LENGTH + ); } #[test] fn serialize_expanded_secret_key_size() { - let expanded_secret_key = ExpandedSecretKey::from(&SecretKey::from_bytes(&SECRET_KEY_BYTES).unwrap()); - assert_eq!(bincode::serialized_size(&expanded_secret_key).unwrap() as usize, BINCODE_INT_LENGTH + EXPANDED_SECRET_KEY_LENGTH); + let expanded_secret_key = + ExpandedSecretKey::from(&SecretKey::from_bytes(&SECRET_KEY_BYTES).unwrap()); + assert_eq!( + bincode::serialized_size(&expanded_secret_key).unwrap() as usize, + BINCODE_INT_LENGTH + EXPANDED_SECRET_KEY_LENGTH + ); } #[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 + ); } }