Expose and benchmark Sinsemilla primitive

This commit is contained in:
Jack Grigg 2021-08-10 12:45:23 +01:00
parent c4fb1efb1c
commit 08b279b900
4 changed files with 67 additions and 8 deletions

View File

@ -64,6 +64,10 @@ test-dependencies = ["proptest"]
name = "note_decryption"
harness = false
[[bench]]
name = "primitives"
harness = false
[[bench]]
name = "small"
harness = false

55
benches/primitives.rs Normal file
View File

@ -0,0 +1,55 @@
use std::array;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use ff::Field;
use orchard::primitives::sinsemilla;
use pasta_curves::pallas;
#[cfg(unix)]
use pprof::criterion::{Output, PProfProfiler};
use rand::{rngs::OsRng, Rng};
fn bench_primitives(c: &mut Criterion) {
let mut rng = OsRng;
{
let mut group = c.benchmark_group("Sinsemilla");
let hasher = sinsemilla::HashDomain::new("hasher");
let committer = sinsemilla::CommitDomain::new("committer");
let bits: Vec<bool> = (0..1086).map(|_| rng.gen()).collect();
let r = pallas::Scalar::random(rng);
// Benchmark the input sizes we use in Orchard:
// - 510 bits for Commit^ivk
// - 520 bits for MerkleCRH
// - 1086 bits for NoteCommit
for size in array::IntoIter::new([510, 520, 1086]) {
group.bench_function(BenchmarkId::new("hash-to-point", size), |b| {
b.iter(|| hasher.hash_to_point(bits[..size].iter().cloned()))
});
group.bench_function(BenchmarkId::new("hash", size), |b| {
b.iter(|| hasher.hash(bits[..size].iter().cloned()))
});
group.bench_function(BenchmarkId::new("commit", size), |b| {
b.iter(|| committer.commit(bits[..size].iter().cloned(), &r))
});
group.bench_function(BenchmarkId::new("short-commit", size), |b| {
b.iter(|| committer.commit(bits[..size].iter().cloned(), &r))
});
}
}
}
#[cfg(unix)]
criterion_group! {
name = benches;
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
targets = bench_primitives
}
#[cfg(not(unix))]
criterion_group!(benches, bench_primitives);
criterion_main!(benches);

View File

@ -6,4 +6,4 @@
pub(crate) mod poseidon;
pub mod redpallas;
pub(crate) mod sinsemilla;
pub mod sinsemilla;

View File

@ -22,7 +22,7 @@ pub(crate) fn lebs2ip_k(bits: &[bool]) -> u32 {
/// The sequence of K bits in little-endian order representing an integer
/// up to `2^K` - 1.
pub fn i2lebsp_k(int: usize) -> [bool; K] {
pub(crate) fn i2lebsp_k(int: usize) -> [bool; K] {
assert!(int < (1 << K));
i2lebsp(int as u64)
}
@ -97,7 +97,7 @@ pub struct HashDomain {
impl HashDomain {
/// Constructs a new `HashDomain` with a specific prefix string.
pub(crate) fn new(domain: &str) -> Self {
pub fn new(domain: &str) -> Self {
HashDomain {
Q: pallas::Point::hash_to_curve(Q_PERSONALIZATION)(domain.as_bytes()),
}
@ -106,7 +106,7 @@ impl HashDomain {
/// $\mathsf{SinsemillaHashToPoint}$ from [§ 5.4.1.9][concretesinsemillahash].
///
/// [concretesinsemillahash]: https://zips.z.cash/protocol/nu5.pdf#concretesinsemillahash
pub(crate) fn hash_to_point(&self, msg: impl Iterator<Item = bool>) -> CtOption<pallas::Point> {
pub fn hash_to_point(&self, msg: impl Iterator<Item = bool>) -> CtOption<pallas::Point> {
self.hash_to_point_inner(msg).into()
}
@ -131,7 +131,7 @@ impl HashDomain {
/// # Panics
///
/// This panics if the message length is greater than [`K`] * [`C`]
pub(crate) fn hash(&self, msg: impl Iterator<Item = bool>) -> CtOption<pallas::Base> {
pub fn hash(&self, msg: impl Iterator<Item = bool>) -> CtOption<pallas::Base> {
extract_p_bottom(self.hash_to_point(msg))
}
@ -154,7 +154,7 @@ pub struct CommitDomain {
impl CommitDomain {
/// Constructs a new `CommitDomain` with a specific prefix string.
pub(crate) fn new(domain: &str) -> Self {
pub fn new(domain: &str) -> Self {
let m_prefix = format!("{}-M", domain);
let r_prefix = format!("{}-r", domain);
let hasher_r = pallas::Point::hash_to_curve(&r_prefix);
@ -168,7 +168,7 @@ impl CommitDomain {
///
/// [concretesinsemillacommit]: https://zips.z.cash/protocol/nu5.pdf#concretesinsemillacommit
#[allow(non_snake_case)]
pub(crate) fn commit(
pub fn commit(
&self,
msg: impl Iterator<Item = bool>,
r: &pallas::Scalar,
@ -179,7 +179,7 @@ impl CommitDomain {
/// $\mathsf{SinsemillaShortCommit}$ from [§ 5.4.8.4][concretesinsemillacommit].
///
/// [concretesinsemillacommit]: https://zips.z.cash/protocol/nu5.pdf#concretesinsemillacommit
pub(crate) fn short_commit(
pub fn short_commit(
&self,
msg: impl Iterator<Item = bool>,
r: &pallas::Scalar,