diff --git a/filecoin-hashers/src/poseidon.rs b/filecoin-hashers/src/poseidon.rs index faf85bb3d..b0b44b3b0 100644 --- a/filecoin-hashers/src/poseidon.rs +++ b/filecoin-hashers/src/poseidon.rs @@ -362,8 +362,10 @@ impl LightAlgorithm for PoseidonFunction { 1 | 2 | 4 | 8 | 16 => shared_hash_frs( &parts .iter() - .map(|x| { - ::Fr::from_repr(x.0).expect("from_repr failure") + .enumerate() + .map(|(i, x)| { + ::Fr::from_repr(x.0) + .unwrap_or_else(|_| panic!("from_repr failure at {}", i)) }) .collect::>(), ) diff --git a/storage-proofs/core/benches/merkle.rs b/storage-proofs/core/benches/merkle.rs index d31b1d96e..451f5c6a7 100644 --- a/storage-proofs/core/benches/merkle.rs +++ b/storage-proofs/core/benches/merkle.rs @@ -1,9 +1,12 @@ +use anyhow::Result; use criterion::{black_box, criterion_group, criterion_main, Criterion, ParameterizedBenchmark}; -use filecoin_hashers::{poseidon::PoseidonHasher, sha256::Sha256Hasher}; +use filecoin_hashers::{ + poseidon::PoseidonDomain, poseidon::PoseidonHasher, sha256::Sha256Hasher, Domain, +}; use rand::{thread_rng, Rng}; use storage_proofs_core::merkle::{create_base_merkle_tree, BinaryMerkleTree}; -fn merkle_benchmark(c: &mut Criterion) { +fn merkle_benchmark_sha256(c: &mut Criterion) { #[cfg(feature = "big-sector-sizes-bench")] let params = vec![128, 1024, 1_048_576]; #[cfg(not(feature = "big-sector-sizes-bench"))] @@ -26,23 +29,44 @@ fn merkle_benchmark(c: &mut Criterion) { }) }, params, - ) - .with_function("poseidon", move |b, n_nodes| { - let mut rng = thread_rng(); - let data: Vec = (0..32 * *n_nodes).map(|_| rng.gen()).collect(); + ), + ); +} + +fn merkle_benchmark_poseidon(c: &mut Criterion) { + #[cfg(feature = "big-sector-sizes-bench")] + let params = vec![64, 128, 1024, 1_048_576]; + #[cfg(not(feature = "big-sector-sizes-bench"))] + let params = vec![64, 128, 1024]; - b.iter(|| { - black_box( - create_base_merkle_tree::>( - None, *n_nodes, &data, + c.bench( + "merkletree-binary", + ParameterizedBenchmark::new( + "poseidon", + move |b, n_nodes| { + let mut rng = thread_rng(); + let mut data: Vec = Vec::with_capacity(32 * *n_nodes); + (0..*n_nodes) + .into_iter() + .try_for_each(|_| -> Result<()> { + let node = PoseidonDomain::random(&mut rng); + Ok(data.extend(node.into_bytes())) + }) + .expect("failed to generate data"); + + b.iter(|| { + black_box( + create_base_merkle_tree::>( + None, *n_nodes, &data, + ) + .unwrap(), ) - .unwrap(), - ) - }) - }) - .sample_size(20), + }) + }, + params, + ), ); } -criterion_group!(benches, merkle_benchmark); +criterion_group!(benches, merkle_benchmark_sha256, merkle_benchmark_poseidon); criterion_main!(benches);