add a very simple bench

This commit is contained in:
Balazs Komuves 2026-01-22 22:36:25 +01:00
parent 2d8f9163cd
commit fb74a20cd2
No known key found for this signature in database
GPG Key ID: F63B7AEF18435562
4 changed files with 73 additions and 4 deletions

View File

@ -20,6 +20,6 @@ name = "testmain"
test = false
bench = false
#[[bench]]
#name = "iterated_perm"
#harness = false
[[bench]]
name = "iterated_perm"
harness = false

View File

@ -13,7 +13,7 @@ and [`staging-agda`](https://github.com/faulhornlabs/staging-agda/).
### TODO
- [ ] optimize squaring to use less multiplication
- [ ] optimize squaring to use less multiplications
- [ ] benchmark RISC-V cycles
- [ ] add more Poseidon2 state widths than `t=3`
- [ ] implement `circomlib`-compatible Poseidon

54
benches/iterated_perm.rs Normal file
View File

@ -0,0 +1,54 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rust_poseidon_bn254_pure::bn254::field::*;
use rust_poseidon_bn254_pure::poseidon2::permutation::*;
//------------------------------------------------------------------------------
type State = (Felt,Felt,Felt);
fn initial_state() -> State {
( Felt::from_u32(0)
, Felt::from_u32(1)
, Felt::from_u32(2)
)
}
fn iterate_perm(n: usize) -> State {
let mut state: State = initial_state();
for _i in 0..n {
state = permute_felt(&state);
}
state
}
// for a Merkle tree update with depth 20, we need 20 permutation calls
fn twenty_permutations() -> State {
let mut state: State = initial_state();
iterate_perm(20);
state
}
fn bench_iterated_perm(c: &mut Criterion , n: usize) {
let msg = format!("Poseidon2 permutation iterated {} times", n);
c.bench_function(&msg, |b| b.iter(|| iterate_perm(black_box(n)) ));
}
fn bench_twenty(c: &mut Criterion) {
let msg = format!("Poseidon2 permutation iterated 20 times");
c.bench_function(&msg, |b| b.iter(|| twenty_permutations() ));
}
//------------------------------------------------------------------------------
fn bench_permutations(c: &mut Criterion) {
bench_iterated_perm(c, 1000);
bench_twenty(c);
}
//------------------------------------------------------------------------------
criterion_group!(benches, bench_permutations);
criterion_main!(benches);

View File

@ -1,4 +1,6 @@
use std::time::Instant;
use rust_poseidon_bn254_pure::bn254::bigint::*;
use rust_poseidon_bn254_pure::bn254::constant::*;
use rust_poseidon_bn254_pure::bn254::montgomery::*;
@ -124,6 +126,19 @@ fn main() {
println!("z' = {}", output.2 );
println!("");
println!("poseidon2 iterated 10,000 times:");
println!("");
let now = Instant::now();
let mut state: (Felt,Felt,Felt) = input.clone();
for _i in 0..10000 {
state = permute_felt(&state);
}
println!("x'' = {}", state.0 );
println!("y'' = {}", state.1 );
println!("z'' = {}", state.2 );
let elapsed = now.elapsed();
println!("Elapsed: {:.3?}", elapsed);
//----------------------------------------------------------------------------