Skip to content

Commit b65433b

Browse files
committed
(ugly as sin)test: generate objects needed to bench_cmp poseidons
1 parent 80b3935 commit b65433b

2 files changed

Lines changed: 70 additions & 27 deletions

File tree

utils/benches/poseidon_benchmark.rs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use ark_bn254::Fr;
22
use criterion::{
33
black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, Throughput,
44
};
5-
use light_poseidon::PoseidonParameters as LPoseidonParameters;
6-
use zerokit_utils::Poseidon;
5+
use light_poseidon::{
6+
PoseidonHasher as LPoseidonHasher, PoseidonParameters as LPoseidonParameters,
7+
};
8+
use zerokit_utils::{Poseidon, RoundParameters};
79

810
const ROUND_PARAMS: [(usize, usize, usize, usize); 8] = [
911
(2, 8, 56, 0),
@@ -16,6 +18,14 @@ const ROUND_PARAMS: [(usize, usize, usize, usize); 8] = [
1618
(9, 8, 63, 0),
1719
];
1820

21+
fn make_values(size: u32) -> Vec<[Fr; 1]> {
22+
let mut values = Vec::with_capacity(size as usize);
23+
for i in 0..size {
24+
values.push([Fr::from(i)]);
25+
}
26+
values
27+
}
28+
1929
pub fn poseidon_benchmark(c: &mut Criterion) {
2030
let hasher = Poseidon::<Fr>::from(&ROUND_PARAMS);
2131
let mut group = c.benchmark_group("poseidon Fr");
@@ -26,20 +36,49 @@ pub fn poseidon_benchmark(c: &mut Criterion) {
2636
group.bench_with_input(BenchmarkId::new("Array hash", size), size, |b, &size| {
2737
b.iter_batched(
2838
// Setup: create values for each benchmark iteration
29-
|| {
30-
let mut values = Vec::with_capacity(size as usize);
31-
for i in 0..size {
32-
values.push([Fr::from(i)]);
33-
}
34-
values
35-
},
39+
|| make_values(size),
3640
// Actual benchmark
3741
|values| {
3842
for v in values.iter() {
3943
let _ = hasher.hash(black_box(&v[..]));
4044
}
4145
},
4246
BatchSize::SmallInput,
47+
);
48+
b.iter_batched(
49+
// Setup: create values for each benchmark iteration
50+
|| {
51+
// first, we need to pull out the parameters that the internal hasher is
52+
// using...
53+
let RoundParameters {
54+
t,
55+
n_rounds_full,
56+
n_rounds_partial,
57+
skip_matrices: _,
58+
ark_consts,
59+
mds,
60+
} = hasher.select_params(&[Fr::from(1)]).unwrap();
61+
// then we need to translate it to the light-poseidon paramater
62+
let l_params = LPoseidonParameters {
63+
ark: ark_consts.clone(),
64+
mds: mds.clone(),
65+
full_rounds: *n_rounds_full,
66+
partial_rounds: *n_rounds_partial,
67+
width: *t,
68+
alpha: 1,
69+
};
70+
71+
let vals = make_values(size);
72+
let light_hasher = light_poseidon::Poseidon::<Fr>::new(l_params);
73+
(vals, light_hasher)
74+
},
75+
// Actual benchmark
76+
|(values, mut light_hasher)| {
77+
for v in values.iter() {
78+
let _ = light_hasher.hash(black_box(&v[..]));
79+
}
80+
},
81+
BatchSize::SmallInput,
4382
)
4483
});
4584
}

utils/src/poseidon/poseidon_hash.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,24 @@ impl<F: PrimeField> Poseidon<F> {
123123
}
124124
}
125125

126-
pub fn hash(&self, inp: &[F]) -> Result<F, String> {
126+
pub fn select_params(&self, inp: &[F]) -> Result<&RoundParameters<F>, String> {
127127
if inp.is_empty() {
128128
return Err("Attempt to hash empty data input".to_string());
129129
}
130130
// Note that the rate t becomes input length + 1; hence for length N we pick parameters with T = N + 1
131131
let t = inp.len() + 1;
132+
self.round_params
133+
.iter()
134+
.find(|el| el.t == t)
135+
.ok_or("No parameters found for inputs length".to_string())
136+
}
132137

133-
let Some(params) = self.round_params.iter().find(|el| el.t == t) else {
134-
return Err("No parameters found for inputs length".to_string());
135-
};
136-
137-
let mut state = vec![F::ZERO; t];
138-
let mut state_2 = state.clone();
139-
state[1..].clone_from_slice(inp);
138+
pub fn hash(&self, inp: &[F]) -> Result<F, String> {
139+
let params = self.select_params(inp)?;
140+
let mut state = Vec::with_capacity(inp.len() + 1);
141+
state.push(F::ZERO);
142+
state.extend_from_slice(inp);
143+
let mut state_2 = vec![F::ZERO; inp.len() + 1];
140144

141145
for i in 0..(params.n_rounds_full + params.n_rounds_partial) {
142146
self.ark(&mut state, &params.ark_consts, i * params.t);
@@ -185,14 +189,14 @@ mod test {
185189
// }
186190
// panic!();
187191
// }
188-
#[test]
189-
fn see_data() {
190-
let size = 10;
191-
let mut param_vec = RoundParameVec::<Fr>::make_param_vec(&ROUND_PARAMS);
192-
let mut values = Vec::with_capacity(size as usize);
193-
for i in 0..size {
194-
values.push([Fr::from(u128::MAX - i)]);
195-
}
196-
panic!("{:?}", values);
197-
}
192+
// #[test]
193+
// fn see_data() {
194+
// let size = 10;
195+
// let mut param_vec = RoundParameVec::<Fr>::make_param_vec(&ROUND_PARAMS);
196+
// let mut values = Vec::with_capacity(size as usize);
197+
// for i in 0..size {
198+
// values.push([Fr::from(u128::MAX - i)]);
199+
// }
200+
// panic!("{:?}", values);
201+
// }
198202
}

0 commit comments

Comments
 (0)