Skip to content

Commit

Permalink
chore: optimize poseidon2 implementation (#4807)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This PR removes the usage of a `BigUint` from the poseidon2 solver. I've
also wrapped all of the config in a `lazy_static` to avoid having to
re-instantiate it each time.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
TomAFrench authored Apr 15, 2024
1 parent 645dba1 commit 0adeb08
Show file tree
Hide file tree
Showing 5 changed files with 494 additions and 938 deletions.
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion acvm-repo/bn254_blackbox_solver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ repository.workspace = true
acir.workspace = true
acvm_blackbox_solver.workspace = true
thiserror.workspace = true
num-traits.workspace = true
cfg-if = "1.0.0"
hex.workspace = true
lazy_static = "1.4"

# BN254 fixed base scalar multiplication solver
grumpkin = { version = "0.1.0", package = "noir_grumpkin", features = ["std"] }
Expand All @@ -38,6 +39,18 @@ js-sys.workspace = true
getrandom.workspace = true
wasmer = "4.2.6"

[dev-dependencies]
criterion = "0.5.0"
pprof = { version = "0.12", features = [
"flamegraph",
"frame-pointer",
"criterion",
] }

[[bench]]
name = "criterion"
harness = false

[features]
default = ["bn254"]
bn254 = ["acir/bn254"]
21 changes: 21 additions & 0 deletions acvm-repo/bn254_blackbox_solver/benches/criterion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use criterion::{criterion_group, criterion_main, Criterion};
use std::{hint::black_box, time::Duration};

use acir::FieldElement;
use bn254_blackbox_solver::poseidon2_permutation;

use pprof::criterion::{Output, PProfProfiler};

fn bench_poseidon2(c: &mut Criterion) {
let inputs = [FieldElement::zero(); 4];

c.bench_function("poseidon2", |b| b.iter(|| poseidon2_permutation(black_box(&inputs), 4)));
}

criterion_group!(
name = benches;
config = Criterion::default().sample_size(40).measurement_time(Duration::from_secs(20)).with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
targets = bench_poseidon2
);

criterion_main!(benches);
5 changes: 2 additions & 3 deletions acvm-repo/bn254_blackbox_solver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod poseidon2;
mod wasm;

pub use fixed_base_scalar_mul::{embedded_curve_add, fixed_base_scalar_mul};
use poseidon2::Poseidon2;
pub use poseidon2::poseidon2_permutation;
use wasm::Barretenberg;

use self::wasm::{Pedersen, SchnorrSig};
Expand Down Expand Up @@ -112,7 +112,6 @@ impl BlackBoxFunctionSolver for Bn254BlackBoxSolver {
inputs: &[FieldElement],
len: u32,
) -> Result<Vec<FieldElement>, BlackBoxResolutionError> {
let poseidon = Poseidon2::new();
poseidon.permutation(inputs, len)
poseidon2_permutation(inputs, len)
}
}
Loading

0 comments on commit 0adeb08

Please sign in to comment.