Skip to content
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

WASM Benchmark #66

Merged
merged 9 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Benchmark
on:
workflow_dispatch:
env:
CARGO_TERM_COLOR: always
RUSTFLAGS: -D warnings
RUST_BACKTRACE: full
jobs:
cargo-bench:
name: Bench With Cargo (${{ matrix.os }} + ${{ matrix.channel }})
strategy:
fail-fast: false
matrix:
os:
- macos-latest
- ubuntu-latest
- windows-latest
channel:
- stable
- nightly
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- run: rustup update ${{ matrix.channel }} && rustup default ${{ matrix.channel }}
- run: cargo bench --workspace --all-features
wasm-pack-bench:
name: Bench With Wasm-Pack (${{ matrix.os }} + ${{ matrix.channel }})
strategy:
fail-fast: false
matrix:
os:
- macos-latest
- ubuntu-latest
- windows-latest
channel:
- stable
- nightly
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- run: rustup update ${{ matrix.channel }} && rustup default ${{ matrix.channel }}
- run: cargo install wasm-pack
- run: wasm-pack test --headless --chrome --release --package manta-benchmark
16 changes: 11 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ env:
RUST_BACKTRACE: full
jobs:
test:
name: Test (${{ matrix.os }})
name: Test (${{ matrix.os }} + ${{ matrix.channel }})
strategy:
fail-fast: false
matrix:
Expand All @@ -26,7 +26,7 @@ jobs:
- run: rustup update ${{ matrix.channel }} --no-self-update && rustup default ${{ matrix.channel }}
- run: cargo test --all-features --workspace --release
lint:
name: Lint (${{ matrix.os }})
name: Lint (${{ matrix.os }} + ${{ matrix.channel }})
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -58,14 +58,20 @@ jobs:
- uses: actions/checkout@v3
- run: rustup update nightly && rustup default nightly
- run: RUSTDOCFLAGS="-D warnings --cfg doc_cfg" cargo doc --workspace --all-features --no-deps --document-private-items
bench:
name: Bench (${{ matrix.os }})
compile-bench:
name: Compile Bench (${{ matrix.os }} + ${{ matrix.channel }})
strategy:
fail-fast: false
matrix:
os:
- macos-latest
- ubuntu-latest
- windows-latest
channel:
- stable
- nightly
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- run: rustup update nightly && rustup default nightly
- run: rustup update ${{ matrix.channel }} && rustup default ${{ matrix.channel }}
- run: cargo bench --no-run --workspace --all-features
10 changes: 9 additions & 1 deletion manta-benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ publish = false
all-features = true
rustdoc-args = ["--cfg", "doc_cfg"]

[lib]
crate-type = ["cdylib", "lib"]

[[bench]]
name = "mint"
harness = false
Expand All @@ -32,9 +35,14 @@ name = "reclaim"
harness = false

[dependencies]
getrandom = { version = "0.2", features = ["js"]}
instant = { version = "0.1", features = [ "wasm-bindgen" ] }
manta-accounting = { path = "../manta-accounting", default-features = false, features = ["test"] }
manta-crypto = { path = "../manta-crypto", default-features = false, features = ["test"] }
manta-crypto = { path = "../manta-crypto", default-features = false, features = ["getrandom", "test"] }
manta-pay = { path = "../manta-pay", default-features = false, features = ["groth16", "test"] }
wasm-bindgen = "0.2"
wasm-bindgen-test = { version = "0.3"}
web-sys = { version = "0.3", features = ["console"]}

[dev-dependencies]
criterion = { version = "0.3.4", default-features = false }
27 changes: 26 additions & 1 deletion manta-benchmark/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
# manta-benchmark
# manta-benchmark

## Dependencies:

* [Rust toolchain](https://www.rust-lang.org/tools/install)
* [npm](https://www.npmjs.com/get-npm)
* `wasm-pack` package:
```bash
cargo install wasm-pack
```

## Run the benchmark

* WASM time:
```bash
./wasm-bench.sh
```
You can view the result at `localhost:8080`.
* Headless WASM time for prover time:
```bash
wasm-pack test --headless --chrome --release
```
* Native time:
```bash
cargo bench
```
92 changes: 92 additions & 0 deletions manta-benchmark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,96 @@
// You should have received a copy of the GNU General Public License
// along with manta-rs. If not, see <http://www.gnu.org/licenses/>.

use crate::payment::assert_valid_proof;
use manta_crypto::rand::{OsRng, Rand};
use manta_pay::{
config::{
MultiProvingContext, MultiVerifyingContext, Parameters, TransferPost, UtxoAccumulatorModel,
},
parameters::{generate_parameters, SEED},
};
use wasm_bindgen::prelude::wasm_bindgen;

pub mod payment;

#[wasm_bindgen]
#[derive(Clone, Debug)]
pub struct Context {
proving_context: MultiProvingContext,
verifying_context: MultiVerifyingContext,
parameters: Parameters,
utxo_accumulator_model: UtxoAccumulatorModel,
}

#[wasm_bindgen]
impl Context {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
let (proving_context, verifying_context, parameters, utxo_accumulator_model) =
generate_parameters(SEED).unwrap();
Self {
proving_context,
verifying_context,
parameters,
utxo_accumulator_model,
}
}
}

impl Default for Context {
fn default() -> Self {
Self::new()
}
}
BoyuanFeng marked this conversation as resolved.
Show resolved Hide resolved

#[wasm_bindgen]
pub struct Proof(TransferPost);

#[wasm_bindgen]
pub fn prove_mint(context: &Context) -> Proof {
let mut rng = OsRng;
Proof(payment::prove_mint(
&context.proving_context.mint,
&context.parameters,
&context.utxo_accumulator_model,
rng.gen(),
&mut rng,
))
}

#[wasm_bindgen]
pub fn prove_private_transfer(context: &Context) -> Proof {
let mut rng = OsRng;
Proof(payment::prove_private_transfer(
&context.proving_context,
&context.parameters,
&context.utxo_accumulator_model,
&mut rng,
))
}

#[wasm_bindgen]
pub fn prove_reclaim(context: &Context) -> Proof {
let mut rng = OsRng;
Proof(payment::prove_reclaim(
&context.proving_context,
&context.parameters,
&context.utxo_accumulator_model,
&mut rng,
))
}

#[wasm_bindgen]
pub fn verify_mint(context: &Context, proof: &Proof) {
assert_valid_proof(&context.verifying_context.mint, &proof.0);
}

#[wasm_bindgen]
pub fn verify_private_transfer(context: &Context, proof: &Proof) {
assert_valid_proof(&context.verifying_context.private_transfer, &proof.0);
}

#[wasm_bindgen]
pub fn verify_reclaim(context: &Context, proof: &Proof) {
assert_valid_proof(&context.verifying_context.reclaim, &proof.0);
}
58 changes: 58 additions & 0 deletions manta-benchmark/tests/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2019-2022 Manta Network.
// This file is part of manta-rs.
//
// manta-rs is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// manta-rs is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with manta-rs. If not, see <http://www.gnu.org/licenses/>.

use manta_benchmark::{prove_mint, prove_private_transfer, prove_reclaim, Context, Proof};
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
use web_sys::console;

wasm_bindgen_test_configure!(run_in_browser);
BoyuanFeng marked this conversation as resolved.
Show resolved Hide resolved

static REPEAT: usize = 3;

fn bench<F>(mut f: F, operation: &str)
where
F: FnMut(&Context) -> Proof,
{
let context = Context::new();
let start_time = instant::Instant::now();
for _ in 0..REPEAT {
f(&context);
}
let end_time = instant::Instant::now();
console::log_1(
&format!(
"{:?} Performance: {:?}",
operation,
((end_time - start_time) / REPEAT as u32)
)
.into(),
);
}

#[wasm_bindgen_test]
fn bench_prove_mint() {
bench(prove_mint, "Prove Mint");
}

#[wasm_bindgen_test]
fn bench_prove_private_transfer() {
bench(prove_private_transfer, "Prove Private Transfer");
}

#[wasm_bindgen_test]
fn bench_prove_reclaim() {
bench(prove_reclaim, "Prove Reclaim");
}
4 changes: 4 additions & 0 deletions manta-benchmark/wasm-bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
wasm-pack build --release
cd ./www
npm install
npm run start
3 changes: 3 additions & 0 deletions manta-benchmark/www/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
package-lock.json
5 changes: 5 additions & 0 deletions manta-benchmark/www/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// A dependency graph that contains any wasm must all be imported
// asynchronously. This `bootstrap.js` file does the single async import, so
// that no one else needs to worry about it again.
import("./index.js")
.catch(e => console.error("Error importing `index.js`:", e));
12 changes: 12 additions & 0 deletions manta-benchmark/www/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Manta Benchmark</title>
</head>
<body>
<noscript>This page contains webassembly and javascript content, please enable javascript in your browser.</noscript>
<pre id="wasm-prover"></pre>
BoyuanFeng marked this conversation as resolved.
Show resolved Hide resolved
<script src="./bootstrap.js"></script>
</body>
</html>
63 changes: 63 additions & 0 deletions manta-benchmark/www/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Context, prove_mint, prove_private_transfer, prove_reclaim} from "wasm-prover";

const pre = document.getElementById("wasm-prover");

const REPEAT = 5;
BoyuanFeng marked this conversation as resolved.
Show resolved Hide resolved

// Computes the median of an array
const median = arr => {
const mid = Math.floor(arr.length / 2),
nums = [...arr].sort((a, b) => a - b);
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
};

function bench_prove_mint() {
const context = new Context();
const perf = Array.from(
{length: REPEAT},
(_, i) => {
const t0 = performance.now();
prove_mint(context);
const t1 = performance.now();
return t1 - t0;
}
);
return `prove mint performance: ${median(perf)} ms \n`;
}

function bench_prove_private_transfer() {
const context = new Context();
const perf = Array.from(
{length: REPEAT},
(_, i) => {
const t0 = performance.now();
prove_private_transfer(context);
const t1 = performance.now();
return t1 - t0;
}
);
return `prove private transfer performance: ${median(perf)} ms \n`;
}

function bench_prove_reclaim() {
const context = new Context();
const perf = Array.from(
{length: REPEAT},
(_, i) => {
const t0 = performance.now();
prove_reclaim(context);
const t1 = performance.now();
return t1 - t0;
}
);
return `prove reclaim performance: ${median(perf)} ms \n`;
}

// benchmarks proof time for mint
pre.textContent = bench_prove_mint();

// benchmarks proof time for private transfer
pre.textContent += bench_prove_private_transfer();

// benchmarks proof time for reclaim
pre.textContent += bench_prove_reclaim();
Loading