From 1e177f0ba3c277be64143bade1073f416b717844 Mon Sep 17 00:00:00 2001 From: Al-Kindi-0 <82364884+Al-Kindi-0@users.noreply.github.com> Date: Fri, 25 Nov 2022 16:16:20 +0100 Subject: [PATCH 1/2] feat: benchmark RPO --- Cargo.toml | 5 +++++ benches/hash.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 benches/hash.rs diff --git a/Cargo.toml b/Cargo.toml index 531e0ac..e861b56 100644 --- a/Cargo.toml +++ b/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.3" diff --git a/benches/hash.rs b/benches/hash.rs new file mode 100644 index 0000000..5276e33 --- /dev/null +++ b/benches/hash.rs @@ -0,0 +1,56 @@ + + +use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion}; +use miden_crypto::{hash::{Hasher, Digest}, HashFn, Felt, ElementHasher}; +use rand_utils::rand_value; + +fn rpo256_2to1(c: &mut Criterion) { + let v: [Digest; 2] = [Hasher::hash(&[1u8]), Hasher::hash(&[2u8])]; + 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::().to_le_bytes()), + Hasher::hash(&rand_value::().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::>() + .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::>() + .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); \ No newline at end of file From 0abadf327ea8c95bde4b90244784e4f394025abb Mon Sep 17 00:00:00 2001 From: Al-Kindi-0 <82364884+Al-Kindi-0@users.noreply.github.com> Date: Fri, 25 Nov 2022 17:24:11 +0100 Subject: [PATCH 2/2] chore: rustfmt fix + minor nits --- Cargo.toml | 2 +- benches/hash.rs | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e861b56..d8d4d5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,4 +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.3" +criterion = "0.4" diff --git a/benches/hash.rs b/benches/hash.rs index 5276e33..c0c29d7 100644 --- a/benches/hash.rs +++ b/benches/hash.rs @@ -1,11 +1,12 @@ - - use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion}; -use miden_crypto::{hash::{Hasher, Digest}, HashFn, Felt, ElementHasher}; +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(&[1u8]), Hasher::hash(&[2u8])]; + 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))) }); @@ -53,4 +54,4 @@ fn rpo256_sequential(c: &mut Criterion) { } criterion_group!(hash_group, rpo256_sequential, rpo256_2to1); -criterion_main!(hash_group); \ No newline at end of file +criterion_main!(hash_group);