@@ -2,8 +2,10 @@ use ark_bn254::Fr;
22use 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
810const 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+
1929pub 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 }
0 commit comments