diff --git a/Cargo.lock b/Cargo.lock index 17426b0c..b0981f4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2021,10 +2021,12 @@ dependencies = [ "ark-std", "bincode", "console_error_panic_hook", + "criterion 0.4.0", "getrandom 0.2.7", "group-threshold-cryptography", "js-sys", "rand 0.8.5", + "rand_core 0.6.3", "serde", "serde_with", "wasm-bindgen", diff --git a/tpke-wasm/.gitignore b/tpke-wasm/.gitignore index 4e301317..bb9a2539 100644 --- a/tpke-wasm/.gitignore +++ b/tpke-wasm/.gitignore @@ -2,5 +2,5 @@ **/*.rs.bk Cargo.lock bin/ -pkg/ +pkg wasm-pack.log diff --git a/tpke-wasm/BENCHMARK.md b/tpke-wasm/BENCHMARK.md new file mode 100644 index 00000000..572462ab --- /dev/null +++ b/tpke-wasm/BENCHMARK.md @@ -0,0 +1,56 @@ +# Benchmarks + +## Benchmarking WASM + +This time we may not use [`centurion.rs`](https://github.com/bheisler/criterion.rs/blob/version-0.4/book/src/user_guide/wasi.md#webasseblywasi-benchmarking) because `wasm32-wasi` is incompatible with `wasm_bindgen` ([1](https://github.com/rustwasm/wasm-bindgen/issues/2554), [2](https://github.com/bevyengine/bevy/discussions/5908?sort=new)). Instead, we're going to measure performance directly in the browser. + +### Setup + +```bash +wasm-pack build --release --target web + +cd js-benches +ln -s ../pkg . +``` + +### Running + +```bash +npx http-server +# Visit localhost:8080/index.html +``` + +## Benchmarking Rust + +```bash +cargo bench +``` + +## Results + +### WASM Results + +``` + +``` + +### Rust Results + +``` +TPKE-WASM/tpke-wasm::encrypt - num_shares=8, num_entities=8, threshold=8 + time: [4.8427 ms 4.9178 ms 5.0113 ms] +Found 2 outliers among 10 measurements (20.00%) + 2 (20.00%) high mild +TPKE-WASM/tpke-wasm::encrypt - num_shares=16, num_entities=16, threshold=16 + time: [4.8967 ms 4.9732 ms 5.1114 ms] +Found 1 outliers among 10 measurements (10.00%) + 1 (10.00%) high mild +TPKE-WASM/tpke-wasm::encrypt - num_shares=32, num_entities=32, threshold=32 + time: [4.8219 ms 5.0377 ms 5.3367 ms] +Found 1 outliers among 10 measurements (10.00%) + 1 (10.00%) high severe +TPKE-WASM/tpke-wasm::encrypt - num_shares=64, num_entities=64, threshold=64 + time: [4.8865 ms 4.9192 ms 4.9529 ms] +TPKE-WASM/tpke-wasm::encrypt - num_shares=128, num_entities=128, threshold=128 + time: [4.8900 ms 4.9389 ms 4.9834 ms] +``` diff --git a/tpke-wasm/Cargo.toml b/tpke-wasm/Cargo.toml index aff4b5b8..9eab8584 100644 --- a/tpke-wasm/Cargo.toml +++ b/tpke-wasm/Cargo.toml @@ -33,3 +33,9 @@ ark-std = "0.3.0" [dev-dependencies] wasm-bindgen-test = "0.3.13" console_error_panic_hook = "0.1.7" +criterion = { version = "0.4", default-features = false } +rand_core = "0.6" + +[[bench]] +name = "benchmarks" +harness = false \ No newline at end of file diff --git a/tpke-wasm/benches/benchmarks.rs b/tpke-wasm/benches/benchmarks.rs new file mode 100644 index 00000000..a882a01c --- /dev/null +++ b/tpke-wasm/benches/benchmarks.rs @@ -0,0 +1,30 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +pub fn bench_encrypt_combine(c: &mut Criterion) { + fn encrypt_bench( + num_shares: usize, + num_entities: usize, + threshold: usize, + ) -> impl Fn() { + let message = "my-secret-message".as_bytes().to_vec(); + let setup = tpke_wasm::Setup::new(threshold, num_shares, num_entities); + move || { + let message = message.clone(); + black_box(tpke_wasm::encrypt(message, setup.public_key)); + } + } + + let mut group = c.benchmark_group("TPKE-WASM"); + group.sample_size(10); + + for num_shares in [8, 16, 32, 64, 128].iter() { + let a = encrypt_bench(*num_shares, *num_shares, *num_shares); + group.measurement_time(core::time::Duration::new(30, 0)); + group.bench_function(format!("tpke-wasm::encrypt - num_shares={}, num_entities={}, threshold={}", num_shares, num_shares, num_shares), |b| { + b.iter(|| a()) + }); + } +} + +criterion_group!(benches, bench_encrypt_combine); +criterion_main!(benches); diff --git a/tpke-wasm/js-benches/index.html b/tpke-wasm/js-benches/index.html new file mode 100644 index 00000000..70068db4 --- /dev/null +++ b/tpke-wasm/js-benches/index.html @@ -0,0 +1,9 @@ + + + + + TPKE benchmark + + + + diff --git a/tpke-wasm/js-benches/index.js b/tpke-wasm/js-benches/index.js new file mode 100644 index 00000000..9f9dd9c7 --- /dev/null +++ b/tpke-wasm/js-benches/index.js @@ -0,0 +1,16 @@ +import init from "./pkg/tpke_wasm.js"; +import { Setup } from "./pkg/tpke_wasm.js"; + +const runBenchmarks = async () => { + console.log("Initializing ..."); + + const setup = new Setup(); + + console.log("Setup: ", setup); + + console.log("Done!"); +}; + +init() + .then(runBenchmarks) + .catch((err) => console.error(err)); \ No newline at end of file diff --git a/tpke-wasm/src/lib.rs b/tpke-wasm/src/lib.rs index 31f429b8..06444176 100644 --- a/tpke-wasm/src/lib.rs +++ b/tpke-wasm/src/lib.rs @@ -10,8 +10,6 @@ use serde_with::serde_as; use utils::set_panic_hook; use wasm_bindgen::prelude::*; -extern crate alloc; -// Use `wee_alloc` as the global allocator. extern crate wee_alloc; pub type E = ark_bls12_381::Bls12_381; diff --git a/ferveo/BENCHMARK.md b/tpke/BENCHMARK.md similarity index 90% rename from ferveo/BENCHMARK.md rename to tpke/BENCHMARK.md index b17fc2c6..7eb6c1e7 100644 --- a/ferveo/BENCHMARK.md +++ b/tpke/BENCHMARK.md @@ -1,4 +1,34 @@ -# WASM results +# Benchmarks + +## Benchmarking WASM + +Based on `centurion.rs` (docs)[https://github.com/bheisler/criterion.rs/blob/version-0.4/book/src/user_guide/wasi.md#webasseblywasi-benchmarking] + +### Setup + +```bash +cargo install cargo-wasi +npm install -g @wasmer/cli + +cargo wasi build --bench=benchmarks --release +cp `ls -t ../target/wasm32-wasi/release/deps/*.wasm | head -n 1` benchmarks.wasm +``` + +### Running + +```bash +wasmer-js run --dir=. benchmarks.wasm -- --bench +``` + +## Benchmarking Rust + +```bash +cargo bench +``` + +## Results + +### WASM Results ``` TPKE/share_combine: 100 validators threshold 1024*2/3 - #msg 1 - msg-size = 100 bytes @@ -27,7 +57,8 @@ TPKE/share_combine: 200 validators threshold 8192*2/3 - #msg 1 - msg-size = 100 time: [413.68 ms 426.59 ms 434.22 ms] ``` -# Rust results +### Rust Results + ``` TPKE/share_combine: 100 validators threshold 1024*2/3 - #msg 1 - msg-size = 100 bytes time: [39.402 ms 39.933 ms 40.442 ms] @@ -76,4 +107,4 @@ TPKE/share_combine: 200 validators threshold 8192*2/3 - #msg 1 - msg-size = 100 time: [76.676 ms 77.459 ms 78.123 ms] change: [-80.315% -79.559% -78.876%] (p = 0.00 < 0.05) Performance has improved. -``` \ No newline at end of file +``` diff --git a/tpke/README.md b/tpke/README.md index ae7ffb48..cf7ddad7 100644 --- a/tpke/README.md +++ b/tpke/README.md @@ -18,6 +18,4 @@ cp `ls -t ../target/wasm32-wasi/release/deps/*.wasm | head -n 1` benchmarks.wasm ```bash wasmer-js run --dir=. benchmarks.wasm -- --bench -``` - -### Results +``` \ No newline at end of file