Skip to content

Commit

Permalink
Run rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rrybarczyk committed Jun 8, 2021
1 parent b910cad commit f143482
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 186 deletions.
3 changes: 2 additions & 1 deletion utils/buffer/src/buffer_pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
32 changes: 15 additions & 17 deletions vendor/ed25519-dalek/benches/ed25519_benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,31 @@ 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) {
let mut csprng: ThreadRng = thread_rng();
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))
});
}

Expand All @@ -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))
});
}

Expand All @@ -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))
});
}

Expand All @@ -75,10 +73,12 @@ mod ed25519_benches {
"Ed25519 batch signature verification",
|b, &&size| {
let mut csprng: ThreadRng = thread_rng();
let keypairs: Vec<Keypair> = (0..size).map(|_| Keypair::generate(&mut csprng)).collect();
let keypairs: Vec<Keypair> =
(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<Signature> = keypairs.iter().map(|key| key.sign(&msg)).collect();
let signatures: Vec<Signature> =
keypairs.iter().map(|key| key.sign(&msg)).collect();
let public_keys: Vec<PublicKey> = keypairs.iter().map(|key| key.public).collect();

b.iter(|| verify_batch(&messages[..], &signatures[..], &public_keys[..]));
Expand All @@ -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 =
Expand All @@ -108,6 +108,4 @@ mod ed25519_benches {
}
}

criterion_main!(
ed25519_benches::ed25519_benches,
);
criterion_main!(ed25519_benches::ed25519_benches,);
53 changes: 31 additions & 22 deletions vendor/ed25519-dalek/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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`
Expand All @@ -242,13 +248,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.
Expand Down Expand Up @@ -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(())
Expand Down
3 changes: 2 additions & 1 deletion vendor/ed25519-dalek/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
51 changes: 32 additions & 19 deletions vendor/ed25519-dalek/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,50 @@ 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,
}

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.
///
Expand Down
30 changes: 19 additions & 11 deletions vendor/ed25519-dalek/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -329,7 +337,8 @@ impl Keypair {
where
D: Digest<OutputSize = U64>,
{
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.
Expand Down Expand Up @@ -399,8 +408,7 @@ impl Keypair {
&self,
message: &[u8],
signature: &ed25519::Signature,
) -> Result<(), SignatureError>
{
) -> Result<(), SignatureError> {
self.public.verify_strict(message, signature)
}
}
Expand Down
Loading

0 comments on commit f143482

Please sign in to comment.