From 42887c80e65fee37310af91dd9d464b774d0cba6 Mon Sep 17 00:00:00 2001 From: vinhtc27 Date: Wed, 23 Apr 2025 10:33:45 +0700 Subject: [PATCH] chore(rln-wasm): save this commit for later benchmark in html and wasm-bindgen-test in a seperate branch --- rln-wasm/Makefile.toml | 24 +- rln-wasm/README.md | 57 +++ rln-wasm/benches/index.html | 925 +++++++++++++++++++----------------- rln-wasm/tests/browser.rs | 11 +- 4 files changed, 569 insertions(+), 448 deletions(-) diff --git a/rln-wasm/Makefile.toml b/rln-wasm/Makefile.toml index 6044629..1454e19 100644 --- a/rln-wasm/Makefile.toml +++ b/rln-wasm/Makefile.toml @@ -8,11 +8,19 @@ dependencies = ["pack_build_arkzkey", "pack_rename"] [tasks.build_multithread] clear = true -dependencies = ["pack_build_multithread", "pack_rename"] +dependencies = [ + "pack_build_multithread", + "pack_rename", + "post_build_multithread", +] [tasks.build_multithread_arkzkey] clear = true -dependencies = ["pack_build_multithread_arkzkey", "pack_rename"] +dependencies = [ + "pack_build_multithread_arkzkey", + "pack_rename", + "post_build_multithread", +] [tasks.pack_build] command = "wasm-pack" @@ -80,12 +88,12 @@ args = [ "build-std=panic_abort,std", ] -# [tasks.post_build_multithread] -# script = ''' -# wasm-bindgen --target web --split-linked-modules --out-dir ./pkg ../target/wasm32-unknown-unknown/release/rln_wasm.wasm && \ -# find ./pkg/snippets -name "workerHelpers.worker.js" -exec sed -i.bak 's|from '\''\.\.\/\.\.\/\.\.\/'\'';|from "../../../rln_wasm.js";|g' {} \; -exec rm -f {}.bak \; && \ -# find ./pkg/snippets -name "workerHelpers.worker.js" -exec sed -i.bak 's|await initWbg(module, memory);|await initWbg({ module, memory });|g' {} \; -exec rm -f {}.bak \; -# ''' +[tasks.post_build_multithread] +script = ''' +wasm-bindgen --target web --split-linked-modules --out-dir ./pkg ../target/wasm32-unknown-unknown/release/rln_wasm.wasm && \ +find ./pkg/snippets -name "workerHelpers.worker.js" -exec sed -i.bak 's|from '\''\.\.\/\.\.\/\.\.\/'\'';|from "../../../rln_wasm.js";|g' {} \; -exec rm -f {}.bak \; && \ +find ./pkg/snippets -name "workerHelpers.worker.js" -exec sed -i.bak 's|await initWbg(module, memory);|await initWbg({ module, memory });|g' {} \; -exec rm -f {}.bak \; +''' [tasks.pack_rename] script = "sed -i.bak 's/rln-wasm/zerokit-rln-wasm/g' pkg/package.json && rm pkg/package.json.bak" diff --git a/rln-wasm/README.md b/rln-wasm/README.md index 4ba9a9f..32a0e71 100644 --- a/rln-wasm/README.md +++ b/rln-wasm/README.md @@ -53,3 +53,60 @@ Or test with the **arkzkey** feature enabled ```bash cargo make test_arkzkey ``` + +If you want to run the tests in browser headless mode, you can use the following command: + +```bash +cargo make test_browser +cargo make test_browser_arkzkey +``` + +## Parallel computation + +The library supports parallel computation using the `wasm-bindgen-rayon` crate, enabling multi-threaded execution in the browser. + +> **Note**: Parallel support is not enabled by default due to WebAssembly and browser limitations. Compiling this feature requires `nightly` Rust. + +To enable parallel computation for WebAssembly threads, you can use the following command: + +```bash +cargo make build_multithread +``` + +Or with the **arkzkey** feature enabled: + +```bash +cargo make build_multithread_arkzkey +``` + +### WebAssembly Threading Support + +Most modern browsers support WebAssembly threads, but they require the following headers to enable `SharedArrayBuffer` and multithreading: + +- Cross-Origin-Opener-Policy: same-origin +- Cross-Origin-Embedder-Policy: require-corp + +Without these, the application will fall back to single-threaded mode. + +## Feature detection + +If you're targeting [older browser versions that didn't support WebAssembly threads yet](https://webassembly.org/roadmap/), you'll likely want to make two builds - one with threads support and one without - and use feature detection to choose the right one on the JavaScript side. + +You can use [wasm-feature-detect](https://github.com/GoogleChromeLabs/wasm-feature-detect) library for this purpose. The code will look roughly like this: + +```js +import { threads } from 'wasm-feature-detect'; + +let wasmPkg; + +if (await threads()) { + wasmPkg = await import('./pkg-with-threads/index.js'); + await wasmPkg.default(); + await wasmPkg.initThreadPool(navigator.hardwareConcurrency); +} else { + wasmPkg = await import('./pkg-without-threads/index.js'); + await wasmPkg.default(); +} + +wasmPkg.nowCallAnyExportedFuncs(); +``` diff --git a/rln-wasm/benches/index.html b/rln-wasm/benches/index.html index 55cb71c..6c8ea2d 100644 --- a/rln-wasm/benches/index.html +++ b/rln-wasm/benches/index.html @@ -1,502 +1,561 @@ - - - - + + + RLN WASM Benchmark - + - +

RLN WASM Benchmark

-
- Detecting CPU cores... -
+
Detecting CPU cores...
-
- - -
+
+ + +
- + -
- - -
-
- - -
-
- - -
-
- - -
-
- - -
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
- + -
Please initialize thread pool first
+
Please initialize thread pool first
-

Results

- - - - - - - - - - -
OperationTime (ms)
+

Results

+ + + + + + + + + + +
OperationTime (ms)
- +
- - - \ No newline at end of file + + diff --git a/rln-wasm/tests/browser.rs b/rln-wasm/tests/browser.rs index 30d1cf0..48b72d1 100644 --- a/rln-wasm/tests/browser.rs +++ b/rln-wasm/tests/browser.rs @@ -50,19 +50,16 @@ mod tests { "#)] extern "C" { #[wasm_bindgen(catch)] - pub fn isThreadpoolSupported() -> Result; + fn isThreadpoolSupported() -> Result; #[wasm_bindgen(catch)] - pub fn initWitnessCalculator(js: &str) -> Result; + fn initWitnessCalculator(js: &str) -> Result; #[wasm_bindgen(catch)] - pub fn readFile(data: &[u8]) -> Result; + fn readFile(data: &[u8]) -> Result; #[wasm_bindgen(catch)] - pub async fn calculateWitness( - circom_data: &[u8], - inputs: Object, - ) -> Result; + async fn calculateWitness(circom_data: &[u8], inputs: Object) -> Result; } const WITNESS_CALCULATOR_JS: &str = include_str!("../resources/witness_calculator(browser).js");