-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Poseidon benchmark that also runs in Wasm #2633
Changes from 11 commits
ee0fc5f
04cfdc9
71abeef
d9c9ec3
ece3335
cd535b3
1c116b1
1778f63
846b4b7
693ff54
2060f9b
23de603
67c7d9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ idea | |
*.sage.py | ||
params | ||
_build | ||
build | ||
.merlin | ||
*.swp | ||
*.swo | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a better place for this script than the repo root? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest you create a |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/bin/bash | ||
|
||
# Default benchmark name | ||
BENCH_NAME="" | ||
|
||
# Parse the optional --bench=<NAME> argument | ||
for i in "$@"; do | ||
case $i in | ||
--bench=*) | ||
BENCH_NAME="${i#*=}" | ||
shift # Remove --bench=<NAME> from processing | ||
;; | ||
*) | ||
# unknown option | ||
;; | ||
esac | ||
done | ||
|
||
# Throw an error if --bench was not provided | ||
if [ -z "$BENCH_NAME" ]; then | ||
echo "Error: You must specify a benchmark using --bench=<NAME>" | ||
exit 1 | ||
fi | ||
|
||
# Function to delete any old .wasm files related to the current benchmark | ||
cleanup_old_wasm_files() { | ||
WASM_FILES=$(find target/wasm32-wasi/release/deps/ -type f -name "*$BENCH_NAME*.wasm") | ||
|
||
if [ -n "$WASM_FILES" ]; then | ||
echo "Cleaning up old WASM files for benchmark '$BENCH_NAME'..." | ||
rm -f $WASM_FILES | ||
else | ||
echo "No old WASM files found for benchmark '$BENCH_NAME'." | ||
fi | ||
} | ||
|
||
# Call the cleanup function | ||
cleanup_old_wasm_files | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add |
||
|
||
# Build the WASM benchmark with cargo-wasi | ||
echo "Building benchmark '$BENCH_NAME' with cargo-wasi..." | ||
cargo wasi build --bench="$BENCH_NAME" --release | ||
|
||
# Function to find the correct WASM file | ||
find_wasm_file() { | ||
# Search for the WASM file corresponding to the benchmark name | ||
WASM_FILE=$(find target/wasm32-wasi/release/deps/ -type f -name "*$BENCH_NAME*.wasm" | head -n 1) | ||
|
||
if [ -z "$WASM_FILE" ]; then | ||
echo "Error: No WASM file found for benchmark '$BENCH_NAME'." | ||
exit 1 | ||
fi | ||
} | ||
|
||
# Call the function to find the correct WASM file | ||
find_wasm_file | ||
|
||
echo "Running benchmark at $WASM_FILE with wasmer-js..." | ||
# Run the WASM file with wasmer-js | ||
wasmer-js run --dir=. "$WASM_FILE" -- --bench |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,15 @@ ocaml-gen = { workspace = true, optional = true } | |
[dev-dependencies] | ||
serde_json.workspace = true | ||
hex.workspace = true | ||
criterion = { version = "0.3", default-features = false, features = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it work if we put this |
||
"cargo_bench_support", | ||
"html_reports", | ||
] } | ||
|
||
[features] | ||
default = [] | ||
ocaml_types = [ "ocaml", "ocaml-gen", ] | ||
ocaml_types = ["ocaml", "ocaml-gen"] | ||
|
||
[[bench]] | ||
name = "poseidon_bench" | ||
harness = false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Oracle | ||
# Poseidon | ||
|
||
## Test vectors | ||
|
||
|
@@ -17,3 +17,33 @@ cargo run -p export_test_vectors -- B10 legacy - | |
cargo run -p export_test_vectors -- b10 legacy legacy.json | ||
cargo run -p export_test_vectors -- hex kimchi kimchi.json | ||
``` | ||
|
||
## Benchmark | ||
|
||
This folder contains a Poseidon benchmark `poseidon_bench`. | ||
|
||
To run the benchmark natively, do: | ||
|
||
```sh | ||
cargo bench --bench=poseidon_bench | ||
``` | ||
|
||
It can also be run in WebAssembly (executed by Node.js), with the following prerequisites: | ||
|
||
- Add the `wasm32-wasi` target | ||
- Install `cargo-wasi` | ||
- Install the wasmer JS CLI | ||
|
||
```sh | ||
rustup target add wasm32-wasi | ||
cargo install cargo-wasi | ||
npm install -g @wasmer/cli | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooh, fun, we now have the |
||
``` | ||
|
||
Now, you can run this and other benchmarks in Wasm, using the following from the repository root: | ||
|
||
```sh | ||
./bench-wasm.sh --bench=poseidon_bench | ||
``` | ||
|
||
For this to work, the filename of your benchmark has to be the same as the benchmark name! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use mina_curves::pasta::Fp; | ||
use mina_poseidon::{ | ||
constants::PlonkSpongeConstantsKimchi, | ||
pasta::fp_kimchi as SpongeParametersKimchi, | ||
poseidon::{ArithmeticSponge as Poseidon, Sponge}, | ||
}; | ||
|
||
pub fn bench_poseidon_kimchi(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("Poseidon"); | ||
group.sample_size(100); | ||
|
||
// Chain of hashes, starting from a random value | ||
group.bench_function("poseidon_hash_kimchi", |b| { | ||
let mut hash: Fp = rand::random(); | ||
let mut poseidon = Poseidon::<Fp, PlonkSpongeConstantsKimchi>::new( | ||
SpongeParametersKimchi::static_params(), | ||
); | ||
|
||
b.iter(|| { | ||
poseidon.absorb(&[hash]); | ||
hash = poseidon.squeeze(); | ||
}) | ||
}); | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, bench_poseidon_kimchi); | ||
criterion_main!(benches); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my IDE automatically ran cmake (I think?) and added this folder, so let's gitignore it