arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
miden-crypto/benches/hash.rs
benches/hash.rsRaw
 1use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
 2use miden_crypto::{
 3    hash::rpo::{Rpo256, RpoDigest},
 4    Felt,
 5};
 6use rand_utils::rand_value;
 7
 8fn rpo256_2to1(c: &mut Criterion) {
 9    let v: [RpoDigest; 2] = [Rpo256::hash(&[1_u8]), Rpo256::hash(&[2_u8])];
10    c.bench_function("RPO256 2-to-1 hashing (cached)", |bench| {
11        bench.iter(|| Rpo256::merge(black_box(&v)))
12    });
13
14    c.bench_function("RPO256 2-to-1 hashing (random)", |bench| {
15        bench.iter_batched(
16            || {
17                [
18                    Rpo256::hash(&rand_value::<u64>().to_le_bytes()),
19                    Rpo256::hash(&rand_value::<u64>().to_le_bytes()),
20                ]
21            },
22            |state| Rpo256::merge(&state),
23            BatchSize::SmallInput,
24        )
25    });
26}
27
28fn rpo256_sequential(c: &mut Criterion) {
29    let v: [Felt; 100] = (0..100)
30        .into_iter()
31        .map(Felt::new)
32        .collect::<Vec<Felt>>()
33        .try_into()
34        .expect("should not fail");
35    c.bench_function("RPO256 sequential hashing (cached)", |bench| {
36        bench.iter(|| Rpo256::hash_elements(black_box(&v)))
37    });
38
39    c.bench_function("RPO256 sequential hashing (random)", |bench| {
40        bench.iter_batched(
41            || {
42                let v: [Felt; 100] = (0..100)
43                    .into_iter()
44                    .map(|_| Felt::new(rand_value()))
45                    .collect::<Vec<Felt>>()
46                    .try_into()
47                    .expect("should not fail");
48                v
49            },
50            |state| Rpo256::hash_elements(&state),
51            BatchSize::SmallInput,
52        )
53    });
54}
55
56criterion_group!(hash_group, rpo256_sequential, rpo256_2to1);
57criterion_main!(hash_group);