2021-08-06 08:21:37 -07:00
|
|
|
use std::iter;
|
|
|
|
|
|
|
|
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
|
2020-09-09 14:25:56 -07:00
|
|
|
use ff::Field;
|
|
|
|
use rand_core::OsRng;
|
2021-08-06 08:21:37 -07:00
|
|
|
use zcash_note_encryption::batch;
|
2020-09-09 14:25:56 -07:00
|
|
|
use zcash_primitives::{
|
2022-12-10 14:27:09 -08:00
|
|
|
consensus::{NetworkUpgrade::Canopy, Parameters, TEST_NETWORK},
|
2020-10-29 09:48:26 -07:00
|
|
|
memo::MemoBytes,
|
2021-03-04 13:26:39 -08:00
|
|
|
sapling::{
|
2021-08-05 14:13:23 -07:00
|
|
|
note_encryption::{
|
2022-12-10 14:27:09 -08:00
|
|
|
try_sapling_compact_note_decryption, try_sapling_note_decryption,
|
|
|
|
PreparedIncomingViewingKey, SaplingDomain,
|
2021-08-05 14:13:23 -07:00
|
|
|
},
|
2022-12-10 14:27:09 -08:00
|
|
|
prover::mock::MockTxProver,
|
2022-12-09 17:09:06 -08:00
|
|
|
value::NoteValue,
|
2023-01-10 03:30:21 -08:00
|
|
|
Diversifier, SaplingIvk,
|
2021-03-04 13:26:39 -08:00
|
|
|
},
|
2022-12-09 17:09:06 -08:00
|
|
|
transaction::components::sapling::{
|
|
|
|
builder::SaplingBuilder, CompactOutputDescription, GrothProofBytes, OutputDescription,
|
2021-04-07 14:58:30 -07:00
|
|
|
},
|
2020-09-09 14:25:56 -07:00
|
|
|
};
|
|
|
|
|
2021-08-05 14:21:56 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
use pprof::criterion::{Output, PProfProfiler};
|
|
|
|
|
2020-09-09 14:25:56 -07:00
|
|
|
fn bench_note_decryption(c: &mut Criterion) {
|
|
|
|
let mut rng = OsRng;
|
2020-09-21 15:41:40 -07:00
|
|
|
let height = TEST_NETWORK.activation_height(Canopy).unwrap();
|
2020-09-09 14:25:56 -07:00
|
|
|
|
2020-11-20 11:31:29 -08:00
|
|
|
let valid_ivk = SaplingIvk(jubjub::Fr::random(&mut rng));
|
|
|
|
let invalid_ivk = SaplingIvk(jubjub::Fr::random(&mut rng));
|
2020-09-09 14:25:56 -07:00
|
|
|
|
2022-12-10 14:27:09 -08:00
|
|
|
// Construct a Sapling output.
|
2021-06-03 19:21:10 -07:00
|
|
|
let output: OutputDescription<GrothProofBytes> = {
|
2020-09-09 14:25:56 -07:00
|
|
|
let diversifier = Diversifier([0; 11]);
|
2023-01-10 03:30:21 -08:00
|
|
|
let pa = valid_ivk.to_payment_address(diversifier).unwrap();
|
2020-09-09 14:25:56 -07:00
|
|
|
|
2022-12-10 14:27:09 -08:00
|
|
|
let mut builder = SaplingBuilder::new(TEST_NETWORK, height);
|
|
|
|
builder
|
|
|
|
.add_output(
|
|
|
|
&mut rng,
|
|
|
|
None,
|
|
|
|
pa,
|
2022-12-09 17:09:06 -08:00
|
|
|
NoteValue::from_raw(100),
|
2022-12-10 14:27:09 -08:00
|
|
|
MemoBytes::empty(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let bundle = builder
|
|
|
|
.build(&MockTxProver, &mut (), &mut rng, height, None)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
bundle.shielded_outputs()[0].clone()
|
2020-09-09 14:25:56 -07:00
|
|
|
};
|
|
|
|
|
2022-09-12 14:54:09 -07:00
|
|
|
let valid_ivk = PreparedIncomingViewingKey::new(&valid_ivk);
|
|
|
|
let invalid_ivk = PreparedIncomingViewingKey::new(&invalid_ivk);
|
|
|
|
|
2021-08-06 08:21:37 -07:00
|
|
|
{
|
|
|
|
let mut group = c.benchmark_group("sapling-note-decryption");
|
|
|
|
group.throughput(Throughput::Elements(1));
|
|
|
|
|
|
|
|
group.bench_function("valid", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
try_sapling_note_decryption(&TEST_NETWORK, height, &valid_ivk, &output).unwrap()
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
group.bench_function("invalid", |b| {
|
|
|
|
b.iter(|| try_sapling_note_decryption(&TEST_NETWORK, height, &invalid_ivk, &output))
|
|
|
|
});
|
|
|
|
|
|
|
|
let compact = CompactOutputDescription::from(output.clone());
|
|
|
|
|
|
|
|
group.bench_function("compact-valid", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
try_sapling_compact_note_decryption(&TEST_NETWORK, height, &valid_ivk, &compact)
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
group.bench_function("compact-invalid", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
try_sapling_compact_note_decryption(&TEST_NETWORK, height, &invalid_ivk, &compact)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut group = c.benchmark_group("sapling-batch-note-decryption");
|
|
|
|
|
2022-09-14 18:18:03 -07:00
|
|
|
for (nivks, noutputs) in [(1, 10), (10, 1), (10, 10), (50, 50)] {
|
|
|
|
let invalid_ivks: Vec<_> = iter::repeat(invalid_ivk.clone()).take(nivks).collect();
|
|
|
|
let valid_ivks: Vec<_> = iter::repeat(valid_ivk.clone()).take(nivks).collect();
|
|
|
|
|
|
|
|
let outputs: Vec<_> = iter::repeat(output.clone())
|
|
|
|
.take(noutputs)
|
|
|
|
.map(|output| (SaplingDomain::for_height(TEST_NETWORK, height), output))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
group.bench_function(
|
|
|
|
BenchmarkId::new(format!("valid-{}", nivks), noutputs),
|
|
|
|
|b| b.iter(|| batch::try_note_decryption(&valid_ivks, &outputs)),
|
|
|
|
);
|
|
|
|
|
|
|
|
group.bench_function(
|
|
|
|
BenchmarkId::new(format!("invalid-{}", nivks), noutputs),
|
|
|
|
|b| b.iter(|| batch::try_note_decryption(&invalid_ivks, &outputs)),
|
|
|
|
);
|
|
|
|
|
|
|
|
let compact: Vec<_> = outputs
|
|
|
|
.into_iter()
|
|
|
|
.map(|(domain, output)| (domain, CompactOutputDescription::from(output)))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
group.bench_function(
|
|
|
|
BenchmarkId::new(format!("compact-valid-{}", nivks), noutputs),
|
|
|
|
|b| b.iter(|| batch::try_compact_note_decryption(&valid_ivks, &compact)),
|
|
|
|
);
|
|
|
|
|
|
|
|
group.bench_function(
|
|
|
|
BenchmarkId::new(format!("compact-invalid-{}", nivks), noutputs),
|
|
|
|
|b| b.iter(|| batch::try_compact_note_decryption(&invalid_ivks, &compact)),
|
|
|
|
);
|
|
|
|
}
|
2021-08-06 08:21:37 -07:00
|
|
|
}
|
2020-09-09 14:25:56 -07:00
|
|
|
}
|
|
|
|
|
2021-08-05 14:21:56 -07:00
|
|
|
#[cfg(unix)]
|
|
|
|
criterion_group! {
|
|
|
|
name = benches;
|
|
|
|
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
|
|
|
|
targets = bench_note_decryption
|
|
|
|
}
|
2021-08-05 15:22:12 -07:00
|
|
|
#[cfg(not(unix))]
|
2020-09-09 14:25:56 -07:00
|
|
|
criterion_group!(benches, bench_note_decryption);
|
|
|
|
criterion_main!(benches);
|