Browse Source

Merge pull request #17 from 0xPolygonMiden/al-rpo-bench

feat: benchmark RPO
al-gkr-basic-workflow
Bobbin Threadbare 2 years ago
committed by GitHub
parent
commit
adb6083066
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 0 deletions
  1. +5
    -0
      Cargo.toml
  2. +57
    -0
      benches/hash.rs

+ 5
- 0
Cargo.toml

@ -10,6 +10,10 @@ categories = ["cryptography", "no-std"]
keywords = ["miden", "crypto", "hash", "merkle"]
edition = "2021"
[[bench]]
name = "hash"
harness = false
[features]
default = ["std", "winter_crypto/default", "winter_math/default", "winter_utils/default"]
std = ["winter_crypto/std", "winter_math/std", "winter_utils/std"]
@ -22,3 +26,4 @@ winter_utils = { version = "0.4.1", package = "winter-utils", default-features =
[dev-dependencies]
proptest = "1.0.0"
rand_utils = { version = "0.4", package = "winter-rand-utils" }
criterion = "0.4"

+ 57
- 0
benches/hash.rs

@ -0,0 +1,57 @@
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
use miden_crypto::{
hash::{Digest, Hasher},
ElementHasher, Felt, HashFn,
};
use rand_utils::rand_value;
fn rpo256_2to1(c: &mut Criterion) {
let v: [Digest; 2] = [Hasher::hash(&[1_u8]), Hasher::hash(&[2_u8])];
c.bench_function("RPO256 2-to-1 hashing (cached)", |bench| {
bench.iter(|| Hasher::merge(black_box(&v)))
});
c.bench_function("RPO256 2-to-1 hashing (random)", |bench| {
bench.iter_batched(
|| {
[
Hasher::hash(&rand_value::<u64>().to_le_bytes()),
Hasher::hash(&rand_value::<u64>().to_le_bytes()),
]
},
|state| Hasher::merge(&state),
BatchSize::SmallInput,
)
});
}
fn rpo256_sequential(c: &mut Criterion) {
let v: [Felt; 100] = (0..100)
.into_iter()
.map(Felt::new)
.collect::<Vec<Felt>>()
.try_into()
.expect("should not fail");
c.bench_function("RPO256 sequential hashing (cached)", |bench| {
bench.iter(|| Hasher::hash_elements(black_box(&v)))
});
c.bench_function("RPO256 sequential hashing (random)", |bench| {
bench.iter_batched(
|| {
let v: [Felt; 100] = (0..100)
.into_iter()
.map(|_| Felt::new(rand_value()))
.collect::<Vec<Felt>>()
.try_into()
.expect("should not fail");
v
},
|state| Hasher::hash_elements(&state),
BatchSize::SmallInput,
)
});
}
criterion_group!(hash_group, rpo256_sequential, rpo256_2to1);
criterion_main!(hash_group);

Loading…
Cancel
Save