/ console / algorithms / benches / poseidon.rs
poseidon.rs
 1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
 2  // This file is part of the alphavm library.
 3  
 4  // Licensed under the Apache License, Version 2.0 (the "License");
 5  // you may not use this file except in compliance with the License.
 6  // You may obtain a copy of the License at:
 7  
 8  // http://www.apache.org/licenses/LICENSE-2.0
 9  
10  // Unless required by applicable law or agreed to in writing, software
11  // distributed under the License is distributed on an "AS IS" BASIS,
12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  // See the License for the specific language governing permissions and
14  // limitations under the License.
15  
16  #[macro_use]
17  extern crate criterion;
18  
19  use alphavm_console_algorithms::{Poseidon2, Poseidon4, Poseidon8};
20  use alphavm_console_types::prelude::*;
21  use alphavm_utilities::{TestRng, Uniform};
22  
23  use criterion::Criterion;
24  type F = Field<Console>;
25  
26  fn poseidon2(c: &mut Criterion) {
27      let rng = &mut TestRng::default();
28      let hash = Poseidon2::<Console>::setup("Poseidon2").unwrap();
29  
30      let input = [F::rand(rng), F::rand(rng), F::rand(rng), F::rand(rng)];
31      c.bench_function("Poseidon2 Hash 4 -> 1", |b| b.iter(|| hash.hash(&input)));
32      c.bench_function("Poseidon2 Hash 4 -> 2", |b| b.iter(|| hash.hash_many(&input, 2)));
33  
34      let input: Vec<_> = (0..10).map(|_| F::rand(rng)).collect();
35      c.bench_function("Poseidon2 Hash 10 -> 1", |b| b.iter(|| hash.hash(&input)));
36      c.bench_function("Poseidon2 Hash 10 -> 4", |b| b.iter(|| hash.hash_many(&input, 4)));
37      c.bench_function("Poseidon2 Hash 10 -> 8", |b| b.iter(|| hash.hash_many(&input, 8)));
38  }
39  
40  fn poseidon4(c: &mut Criterion) {
41      let rng = &mut TestRng::default();
42      let hash = Poseidon4::<Console>::setup("Poseidon4").unwrap();
43  
44      let input = [F::rand(rng), F::rand(rng), F::rand(rng), F::rand(rng)];
45      c.bench_function("Poseidon4 Hash 4 -> 1", |b| b.iter(|| hash.hash(&input)));
46      c.bench_function("Poseidon4 Hash 4 -> 2", |b| b.iter(|| hash.hash_many(&input, 2)));
47  
48      let input: Vec<_> = (0..10).map(|_| F::rand(rng)).collect();
49      c.bench_function("Poseidon4 Hash 10 -> 1", |b| b.iter(|| hash.hash(&input)));
50      c.bench_function("Poseidon4 Hash 10 -> 4", |b| b.iter(|| hash.hash_many(&input, 4)));
51      c.bench_function("Poseidon4 Hash 10 -> 8", |b| b.iter(|| hash.hash_many(&input, 8)));
52  }
53  
54  fn poseidon8(c: &mut Criterion) {
55      let rng = &mut TestRng::default();
56      let hash = Poseidon8::<Console>::setup("Poseidon8").unwrap();
57  
58      let input = [F::rand(rng), F::rand(rng), F::rand(rng), F::rand(rng)];
59      c.bench_function("Poseidon8 Hash 4 -> 1", |b| b.iter(|| hash.hash(&input)));
60      c.bench_function("Poseidon8 Hash 4 -> 2", |b| b.iter(|| hash.hash_many(&input, 2)));
61  
62      let input: Vec<_> = (0..10).map(|_| F::rand(rng)).collect();
63      c.bench_function("Poseidon8 Hash 10 -> 1", |b| b.iter(|| hash.hash(&input)));
64      c.bench_function("Poseidon8 Hash 10 -> 4", |b| b.iter(|| hash.hash_many(&input, 4)));
65      c.bench_function("Poseidon8 Hash 10 -> 8", |b| b.iter(|| hash.hash_many(&input, 8)));
66  }
67  
68  criterion_group! {
69      name = sponge;
70      config = Criterion::default().sample_size(50);
71      targets = poseidon2, poseidon4, poseidon8,
72  }
73  
74  criterion_main!(sponge);