You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Zero-knowledge proof computation can be accelerated with parallelism in the browser using the WASM API. Two modes are available:
151
+
152
+
***Cross-origin isolated mode**: Uses `SharedArrayBuffer` with `initThreadPool`. Requires COOP/COEP headers.
153
+
***Cross-origin worker mode**: Uses a Service Worker–based worker pool. Works on any website without special headers.
154
+
155
+
See the [JS on WASM API documentation](../../integration/js-on-wasm-api.md) for setup instructions.
156
+
148
157
## Benchmark
149
158
150
159
Please refer to the [Zero-knowledge proof benchmarks](../../getting-started/benchmarks/zk-proof-benchmarks.md) for detailed performance benchmark results.
Copy file name to clipboardExpand all lines: tfhe/docs/integration/js-on-wasm-api.md
+110-4Lines changed: 110 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,8 +7,8 @@ This document outlines how to use the **TFHE-rs** WebAssembly (WASM) client API
7
7
**TFHE-rs** supports 3 WASM `targets`:
8
8
9
9
* Node.js: For use in Node.js applications or packages
10
-
* Web: For use in web browsers
11
-
* Web-parallel: For use in web browsers with multi-threading support
10
+
* Web: For use in web browsers without multi-threading support. Support parallelism via web workers.
11
+
* Web-parallel: For use in web browsers with full multi-threading support
12
12
13
13
The core of the API remains the same, requiring only minor changes in the initialization functions.
14
14
@@ -95,13 +95,119 @@ async function example() {
95
95
}
96
96
```
97
97
98
+
## Web (cross-origin parallelism)
99
+
100
+
Standard multi-threading in WASM requires [cross-origin isolation](https://developer.mozilla.org/en-US/docs/Web/API/Window/crossOriginIsolated) (COOP/COEP headers) to enable `SharedArrayBuffer`. Some deployment environments cannot set these headers (e.g., when embedding in third-party pages or certain hosting platforms).
101
+
102
+
**TFHE-rs** provides an alternative parallelism mode that works **without cross-origin isolation** by using a Service Worker coordinator and a message-passing–based worker pool. This mode is used to accelerate **zero-knowledge proof** computation in the browser.
103
+
104
+
### Setup
105
+
106
+
Cross-origin parallelism requires a **coordinator Service Worker** (`sw.js`) to be served at the root of your application. The `pkg/` directory will contain a `coordinator.js` module; your `sw.js` simply re-exports it:
If your application runs entirely from the main page (no dedicated Web Worker), you can initialize everything in one call:
116
+
117
+
```js
118
+
import init, {
119
+
init_panic_hook,
120
+
init_cross_origin_worker_pool,
121
+
} from"./pkg/tfhe.js";
122
+
123
+
asyncfunctionsetup() {
124
+
awaitinit();
125
+
awaitinit_cross_origin_worker_pool(
126
+
"/sw.js", // URL to the coordinator Service Worker
127
+
null, // number of workers (null = auto-detect)
128
+
);
129
+
awaitinit_panic_hook();
130
+
// TFHE-rs is ready, ZK proof operations will run in parallel
131
+
}
132
+
```
133
+
134
+
Since the main thread JS cannot block, you need to build the list using the dedicated async method:
135
+
```
136
+
let list = await builder.build_with_proof_packed_async(
137
+
crs,
138
+
metadata,
139
+
ZkComputeLoad.Proof,
140
+
);
141
+
```
142
+
143
+
144
+
### Initialization with a dedicated Web Worker (Comlink pattern)
145
+
146
+
To reduce the overhead of synchronization between workers, it is advised to offload the encryption process to a Web Worker using a library like [Comlink](https://github.com/GoogleChromeLabs/comlink). In this case, the coordinator must be registered on the **main thread** before the worker initializes the pool:
0 commit comments