Fix benches

This commit is contained in:
Deirdre Connolly 2020-07-04 20:18:06 -04:00 committed by Deirdre Connolly
parent ba256655dd
commit b521ffb17f
1 changed files with 55 additions and 15 deletions

View File

@ -1,34 +1,66 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use rand::thread_rng;
use rand::{thread_rng, Rng};
use redjubjub::*;
use std::convert::TryFrom;
fn sigs_with_distinct_keys<T: SigType>(
) -> impl Iterator<Item = (VerificationKeyBytes<T>, Signature<T>)> {
enum Item {
SpendAuth {
vk_bytes: VerificationKeyBytes<SpendAuth>,
sig: Signature<SpendAuth>,
},
Binding {
vk_bytes: VerificationKeyBytes<Binding>,
sig: Signature<Binding>,
},
}
fn sigs_with_distinct_keys() -> impl Iterator<Item = Item> {
std::iter::repeat_with(|| {
let sk = SigningKey::<T>::new(thread_rng());
let vk_bytes = VerificationKey::from(&sk).into();
let sig = sk.sign(thread_rng(), b"");
(vk_bytes, sig)
let mut rng = thread_rng();
let msg = b"Bench";
match rng.gen::<u8>() % 2 {
0 => {
let sk = SigningKey::<SpendAuth>::new(thread_rng());
let vk_bytes = VerificationKey::from(&sk).into();
let sig = sk.sign(thread_rng(), &msg[..]);
Item::SpendAuth { vk_bytes, sig }
}
1 => {
let sk = SigningKey::<Binding>::new(thread_rng());
let vk_bytes = VerificationKey::from(&sk).into();
let sig = sk.sign(thread_rng(), &msg[..]);
Item::Binding { vk_bytes, sig }
}
_ => panic!(),
}
})
}
fn bench_batch_verify(c: &mut Criterion) {
let mut group = c.benchmark_group("Batch Verification");
for &n in [8usize, 16, 24, 32, 40, 48, 56, 64].iter() {
group.throughput(Throughput::Elements(*n as u64));
group.throughput(Throughput::Elements(n as u64));
let sigs = sigs_with_distinct_keys().take(*n).collect::<Vec<_>>();
let sigs = sigs_with_distinct_keys().take(n).collect::<Vec<_>>();
group.bench_with_input(
BenchmarkId::new("Unbatched verification", n),
&sigs,
|b, sigs| {
b.iter(|| {
for (vk_bytes, sig) in sigs.iter() {
let _ =
VerificationKey::try_from(*vk_bytes).and_then(|vk| vk.verify(b"", sig));
for item in sigs.iter() {
let msg = b"Bench";
match item {
Item::SpendAuth { vk_bytes, sig } => {
let _ = VerificationKey::try_from(*vk_bytes)
.and_then(|vk| vk.verify(msg, sig));
}
Item::Binding { vk_bytes, sig } => {
let _ = VerificationKey::try_from(*vk_bytes)
.and_then(|vk| vk.verify(msg, sig));
}
}
}
})
},
@ -39,9 +71,17 @@ fn bench_batch_verify(c: &mut Criterion) {
&sigs,
|b, sigs| {
b.iter(|| {
let mut batch = batch::Verifier::<SpendAuth>::new();
for (vk_bytes, sig) in sigs.iter().cloned() {
batch.queue((vk_bytes, sig, b""));
let mut batch = batch::Verifier::new();
for item in sigs.iter() {
let msg = b"Bench";
match item {
Item::SpendAuth { vk_bytes, sig } => {
batch.queue((*vk_bytes, *sig, msg));
}
Item::Binding { vk_bytes, sig } => {
batch.queue((*vk_bytes, *sig, msg));
}
}
}
batch.verify(thread_rng())
})