mirror of
https://github.com/logos-storage/rust-poseidon-bn254-pure.git
synced 2026-05-12 23:49:58 +00:00
add a very simple bench
This commit is contained in:
parent
2d8f9163cd
commit
fb74a20cd2
@ -20,6 +20,6 @@ name = "testmain"
|
||||
test = false
|
||||
bench = false
|
||||
|
||||
#[[bench]]
|
||||
#name = "iterated_perm"
|
||||
#harness = false
|
||||
[[bench]]
|
||||
name = "iterated_perm"
|
||||
harness = false
|
||||
|
||||
@ -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
54
benches/iterated_perm.rs
Normal 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);
|
||||
|
||||
@ -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);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user