/ algorithms / benches / crypto_hash / poseidon.rs
poseidon.rs
 1  // Copyright (c) 2025-2026 ACDC Network
 2  // This file is part of the alphavm library.
 3  //
 4  // Alpha Chain | Delta Chain Protocol
 5  // International Monetary Graphite.
 6  //
 7  // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com).
 8  // They built world-class ZK infrastructure. We installed the EASY button.
 9  // Their cryptography: elegant. Our modifications: bureaucracy-compatible.
10  // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours.
11  //
12  // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
13  // All modifications and new work: CC0 1.0 Universal Public Domain Dedication.
14  // No rights reserved. No permission required. No warranty. No refunds.
15  //
16  // https://creativecommons.org/publicdomain/zero/1.0/
17  // SPDX-License-Identifier: CC0-1.0
18  
19  #[macro_use]
20  extern crate criterion;
21  
22  use alphavm_algorithms::{crypto_hash::PoseidonSponge, AlgebraicSponge};
23  use alphavm_curves::bls12_377::{Fq, FqParameters};
24  use alphavm_fields::Fp384;
25  use alphavm_utilities::{TestRng, Uniform};
26  
27  use criterion::Criterion;
28  use std::time::Duration;
29  
30  fn sponge_2_1_absorb_100_native(c: &mut Criterion) {
31      let rng = &mut TestRng::default();
32      let mut sponge = PoseidonSponge::<Fq, 2, 1>::new();
33  
34      let mut input = Vec::with_capacity(100);
35      for _ in 0..100 {
36          input.push(Fq::rand(rng));
37      }
38      c.bench_function("PoseidonSponge<2, 1> Absorb 100 native", move |b| {
39          b.iter(|| sponge.absorb_native_field_elements(&input))
40      });
41  }
42  
43  fn sponge_2_1_absorb_100_nonnative(c: &mut Criterion) {
44      let rng = &mut TestRng::default();
45      let mut sponge = PoseidonSponge::<Fq, 2, 1>::new();
46  
47      let mut input = Vec::with_capacity(100);
48      for _ in 0..100 {
49          input.push(Fp384::<FqParameters>::rand(rng));
50      }
51      c.bench_function("PoseidonSponge<2, 1> Absorb 100 nonnative", move |b| {
52          b.iter(|| sponge.absorb_nonnative_field_elements(input.clone()))
53      });
54  }
55  
56  criterion_group! {
57      name = sponge;
58      config = Criterion::default().measurement_time(Duration::from_secs(10));
59      targets = sponge_2_1_absorb_100_native, sponge_2_1_absorb_100_nonnative
60  }
61  
62  criterion_main!(sponge);