## Changes
- Enabled parallelism in the browser for `rln-wasm` with the
`multithread` feature flag.
- Added browser tests for both single-threaded and multi-threaded modes.
- Enabled browser tests in the CI workflow.
- Pending: resolving hanging issue with `wasm-bindgen-rayon`
([comment](https://github.com/RReverser/wasm-bindgen-rayon/issues/6#issuecomment-2814372940)).
- Forked [this
commit](42887c80e6)
into a separate
[branch](https://github.com/vacp2p/zerokit/tree/benchmark-v0.8.0), which
includes an HTML benchmark file and a test case for the multithreaded
feature in `rln-wasm`.
- The test case still has the known issue above, so it's temporarily
disabled in this PR and will be addressed in the future.
- Improve the `make installdeps` which resolves the issue of NVM not
enabling Node.js in the current terminal session.
- Reduce the build size of the `.wasm` blob using the `wasm-opt` tool
from [Binaryen](https://github.com/WebAssembly/binaryen).
- Maybe we can close this draft
[PR](https://github.com/vacp2p/zerokit/pull/226), which is already very
outdated?
RLN for WASM
This library is used in waku-org/js-rln
Install Dependencies
Note
This project requires the following tools:
wasm-pack(for compiling Rust to WebAssembly)cargo-make(for running build commands)nvm(to install and manage Node.js)Ensure all dependencies are installed before proceeding.
Manually
Install wasm-pack
cargo install wasm-pack --version=0.13.1
Install cargo-make
cargo install cargo-make
Install Node.js
If you don't have nvm (Node Version Manager), install it by following the installation instructions.
After installing nvm, install and use Node.js v22.14.0:
nvm install 22.14.0
nvm use 22.14.0
nvm alias default 22.14.0
If you already have Node.js installed, check your version with node -v command — the version must be strictly greater than 22.
Or install everything
You can run the following command from the root of the repository to install all required dependencies for zerokit
make installdeps
Building the library
First, navigate to the rln-wasm directory:
cd rln-wasm
Compile zerokit for wasm32-unknown-unknown:
cargo make build
Or compile with the arkzkey feature enabled
cargo make build_arkzkey
Running tests and benchmarks
cargo make test
Or test with the arkzkey feature enabled
cargo make test_arkzkey
If you want to run the tests in browser headless mode, you can use the following command:
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 requiresnightlyRust and thewasm-bindgen-clitool.
Build Setup
Install nightly Rust
rustup install nightly
Install wasm-bindgen-cli
cargo install wasm-bindgen-cli --version=0.2.100
Build Commands
To enable parallel computation for WebAssembly threads, you can use the following command:
cargo make build_multithread
Or with the arkzkey feature enabled:
cargo make build_multithread_arkzkey
WebAssembly Threading Support
Most modern browsers support WebAssembly threads, but they require the following headers to enable SharedArrayBuffer, which is necessary for 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, you'll likely want to create two builds - one with thread support and one without - and use feature detection to choose the right one on the JavaScript side.
You can use wasm-feature-detect library for this purpose. For example, your code might look like this:
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();