2018-07-11 22:41:14 +00:00
|
|
|
// -*- mode: rust; -*-
|
|
|
|
|
//
|
|
|
|
|
// This file is part of ed25519-dalek.
|
2019-01-17 23:28:13 +00:00
|
|
|
// Copyright (c) 2018-2019 isis lovecruft
|
2018-07-11 22:41:14 +00:00
|
|
|
// See LICENSE for licensing information.
|
|
|
|
|
//
|
|
|
|
|
// Authors:
|
2019-01-17 23:28:13 +00:00
|
|
|
// - isis agora lovecruft <isis@patternsinthevoid.net>
|
2018-07-11 22:41:14 +00:00
|
|
|
|
2023-01-02 05:59:19 +00:00
|
|
|
use criterion::{criterion_group, Criterion};
|
2018-07-11 22:41:14 +00:00
|
|
|
|
|
|
|
|
mod ed25519_benches {
|
|
|
|
|
use super::*;
|
|
|
|
|
use ed25519_dalek::Signature;
|
2020-07-13 23:16:30 +00:00
|
|
|
use ed25519_dalek::Signer;
|
2022-12-18 06:24:58 +00:00
|
|
|
use ed25519_dalek::SigningKey;
|
2019-10-03 23:14:49 +00:00
|
|
|
use rand::prelude::ThreadRng;
|
2022-10-16 22:51:26 +00:00
|
|
|
use rand::thread_rng;
|
2018-07-11 22:41:14 +00:00
|
|
|
|
|
|
|
|
fn sign(c: &mut Criterion) {
|
|
|
|
|
let mut csprng: ThreadRng = thread_rng();
|
2022-12-18 06:24:58 +00:00
|
|
|
let keypair: SigningKey = SigningKey::generate(&mut csprng);
|
2018-07-11 22:41:14 +00:00
|
|
|
let msg: &[u8] = b"";
|
|
|
|
|
|
2022-10-15 19:04:03 +00:00
|
|
|
c.bench_function("Ed25519 signing", move |b| b.iter(|| keypair.sign(msg)));
|
2018-07-11 22:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn verify(c: &mut Criterion) {
|
|
|
|
|
let mut csprng: ThreadRng = thread_rng();
|
2022-12-18 06:24:58 +00:00
|
|
|
let keypair: SigningKey = SigningKey::generate(&mut csprng);
|
2018-07-11 22:41:14 +00:00
|
|
|
let msg: &[u8] = b"";
|
2018-12-30 04:26:22 +00:00
|
|
|
let sig: Signature = keypair.sign(msg);
|
2022-10-16 22:51:26 +00:00
|
|
|
|
2018-07-11 22:41:14 +00:00
|
|
|
c.bench_function("Ed25519 signature verification", move |b| {
|
2022-10-16 22:51:26 +00:00
|
|
|
b.iter(|| keypair.verify(msg, &sig))
|
2018-07-11 22:41:14 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 00:37:30 +00:00
|
|
|
fn verify_strict(c: &mut Criterion) {
|
|
|
|
|
let mut csprng: ThreadRng = thread_rng();
|
2022-12-18 06:24:58 +00:00
|
|
|
let keypair: SigningKey = SigningKey::generate(&mut csprng);
|
2019-09-27 00:37:30 +00:00
|
|
|
let msg: &[u8] = b"";
|
|
|
|
|
let sig: Signature = keypair.sign(msg);
|
|
|
|
|
|
|
|
|
|
c.bench_function("Ed25519 strict signature verification", move |b| {
|
2022-10-16 22:51:26 +00:00
|
|
|
b.iter(|| keypair.verify_strict(msg, &sig))
|
2019-09-27 00:37:30 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-15 02:26:39 +00:00
|
|
|
#[cfg(feature = "batch")]
|
Implement batch verification.
The API for this isn't the greatest and I apologise for that. Suggestions for
improvement welcome. One thing which @hdevalence and I considered was to
change the function signature to:
pub fn verify_batch<D, C, M, S, K>(messages: M,
signatures: S,
public_keys: K,
csprng: &mut C) -> Result<(), SignatureError>
where D: Digest<OutputSize = U64> + Default,
C: Rng + CryptoRng,
M: IntoIterator<Item = &[u8]>,
S: IntoIterator,
S::Item: Borrow<Signature>,
K: IntoIterator,
K::Item: Borrow<Signature>,
The other improvement which could be made is to implement 128-bit scalars for
the randomnesses.
* CLOSES #27
2018-07-16 23:41:09 +00:00
|
|
|
fn verify_batch_signatures(c: &mut Criterion) {
|
2023-01-02 05:59:19 +00:00
|
|
|
use ed25519_dalek::verify_batch;
|
|
|
|
|
|
2018-07-27 19:17:03 +00:00
|
|
|
static BATCH_SIZES: [usize; 8] = [4, 8, 16, 32, 64, 96, 128, 256];
|
Implement batch verification.
The API for this isn't the greatest and I apologise for that. Suggestions for
improvement welcome. One thing which @hdevalence and I considered was to
change the function signature to:
pub fn verify_batch<D, C, M, S, K>(messages: M,
signatures: S,
public_keys: K,
csprng: &mut C) -> Result<(), SignatureError>
where D: Digest<OutputSize = U64> + Default,
C: Rng + CryptoRng,
M: IntoIterator<Item = &[u8]>,
S: IntoIterator,
S::Item: Borrow<Signature>,
K: IntoIterator,
K::Item: Borrow<Signature>,
The other improvement which could be made is to implement 128-bit scalars for
the randomnesses.
* CLOSES #27
2018-07-16 23:41:09 +00:00
|
|
|
|
2023-01-02 05:59:19 +00:00
|
|
|
// Benchmark batch verification for all the above batch sizes
|
|
|
|
|
let mut group = c.benchmark_group("Ed25519 batch signature verification");
|
|
|
|
|
for size in BATCH_SIZES {
|
|
|
|
|
let name = format!("size={size}");
|
|
|
|
|
group.bench_function(name, |b| {
|
Implement batch verification.
The API for this isn't the greatest and I apologise for that. Suggestions for
improvement welcome. One thing which @hdevalence and I considered was to
change the function signature to:
pub fn verify_batch<D, C, M, S, K>(messages: M,
signatures: S,
public_keys: K,
csprng: &mut C) -> Result<(), SignatureError>
where D: Digest<OutputSize = U64> + Default,
C: Rng + CryptoRng,
M: IntoIterator<Item = &[u8]>,
S: IntoIterator,
S::Item: Borrow<Signature>,
K: IntoIterator,
K::Item: Borrow<Signature>,
The other improvement which could be made is to implement 128-bit scalars for
the randomnesses.
* CLOSES #27
2018-07-16 23:41:09 +00:00
|
|
|
let mut csprng: ThreadRng = thread_rng();
|
2022-12-18 06:24:58 +00:00
|
|
|
let keypairs: Vec<SigningKey> = (0..size)
|
|
|
|
|
.map(|_| SigningKey::generate(&mut csprng))
|
|
|
|
|
.collect();
|
Implement batch verification.
The API for this isn't the greatest and I apologise for that. Suggestions for
improvement welcome. One thing which @hdevalence and I considered was to
change the function signature to:
pub fn verify_batch<D, C, M, S, K>(messages: M,
signatures: S,
public_keys: K,
csprng: &mut C) -> Result<(), SignatureError>
where D: Digest<OutputSize = U64> + Default,
C: Rng + CryptoRng,
M: IntoIterator<Item = &[u8]>,
S: IntoIterator,
S::Item: Borrow<Signature>,
K: IntoIterator,
K::Item: Borrow<Signature>,
The other improvement which could be made is to implement 128-bit scalars for
the randomnesses.
* CLOSES #27
2018-07-16 23:41:09 +00:00
|
|
|
let msg: &[u8] = b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
|
|
|
|
let messages: Vec<&[u8]> = (0..size).map(|_| msg).collect();
|
2022-10-15 19:04:03 +00:00
|
|
|
let signatures: Vec<Signature> =
|
|
|
|
|
keypairs.iter().map(|key| key.sign(&msg)).collect();
|
2023-01-02 05:59:19 +00:00
|
|
|
let verifying_keys: Vec<_> =
|
2022-12-18 06:24:58 +00:00
|
|
|
keypairs.iter().map(|key| key.verifying_key()).collect();
|
Implement batch verification.
The API for this isn't the greatest and I apologise for that. Suggestions for
improvement welcome. One thing which @hdevalence and I considered was to
change the function signature to:
pub fn verify_batch<D, C, M, S, K>(messages: M,
signatures: S,
public_keys: K,
csprng: &mut C) -> Result<(), SignatureError>
where D: Digest<OutputSize = U64> + Default,
C: Rng + CryptoRng,
M: IntoIterator<Item = &[u8]>,
S: IntoIterator,
S::Item: Borrow<Signature>,
K: IntoIterator,
K::Item: Borrow<Signature>,
The other improvement which could be made is to implement 128-bit scalars for
the randomnesses.
* CLOSES #27
2018-07-16 23:41:09 +00:00
|
|
|
|
2022-12-18 06:24:58 +00:00
|
|
|
b.iter(|| verify_batch(&messages[..], &signatures[..], &verifying_keys[..]));
|
2023-01-02 05:59:19 +00:00
|
|
|
});
|
|
|
|
|
}
|
Implement batch verification.
The API for this isn't the greatest and I apologise for that. Suggestions for
improvement welcome. One thing which @hdevalence and I considered was to
change the function signature to:
pub fn verify_batch<D, C, M, S, K>(messages: M,
signatures: S,
public_keys: K,
csprng: &mut C) -> Result<(), SignatureError>
where D: Digest<OutputSize = U64> + Default,
C: Rng + CryptoRng,
M: IntoIterator<Item = &[u8]>,
S: IntoIterator,
S::Item: Borrow<Signature>,
K: IntoIterator,
K::Item: Borrow<Signature>,
The other improvement which could be made is to implement 128-bit scalars for
the randomnesses.
* CLOSES #27
2018-07-16 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-02 05:59:19 +00:00
|
|
|
// If the above function isn't defined, make a placeholder function
|
2023-01-15 02:26:39 +00:00
|
|
|
#[cfg(not(feature = "batch"))]
|
2023-01-02 05:59:19 +00:00
|
|
|
fn verify_batch_signatures(_: &mut Criterion) {}
|
|
|
|
|
|
2018-07-11 22:41:14 +00:00
|
|
|
fn key_generation(c: &mut Criterion) {
|
2018-07-12 20:19:42 +00:00
|
|
|
let mut csprng: ThreadRng = thread_rng();
|
2018-07-11 22:41:14 +00:00
|
|
|
|
|
|
|
|
c.bench_function("Ed25519 keypair generation", move |b| {
|
2022-12-18 06:24:58 +00:00
|
|
|
b.iter(|| SigningKey::generate(&mut csprng))
|
2018-07-11 22:41:14 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 22:51:26 +00:00
|
|
|
criterion_group! {
|
2018-07-11 22:41:14 +00:00
|
|
|
name = ed25519_benches;
|
|
|
|
|
config = Criterion::default();
|
|
|
|
|
targets =
|
|
|
|
|
sign,
|
|
|
|
|
verify,
|
2019-09-27 00:37:30 +00:00
|
|
|
verify_strict,
|
Implement batch verification.
The API for this isn't the greatest and I apologise for that. Suggestions for
improvement welcome. One thing which @hdevalence and I considered was to
change the function signature to:
pub fn verify_batch<D, C, M, S, K>(messages: M,
signatures: S,
public_keys: K,
csprng: &mut C) -> Result<(), SignatureError>
where D: Digest<OutputSize = U64> + Default,
C: Rng + CryptoRng,
M: IntoIterator<Item = &[u8]>,
S: IntoIterator,
S::Item: Borrow<Signature>,
K: IntoIterator,
K::Item: Borrow<Signature>,
The other improvement which could be made is to implement 128-bit scalars for
the randomnesses.
* CLOSES #27
2018-07-16 23:41:09 +00:00
|
|
|
verify_batch_signatures,
|
2018-07-11 22:41:14 +00:00
|
|
|
key_generation,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 05:59:19 +00:00
|
|
|
criterion::criterion_main!(ed25519_benches::ed25519_benches);
|