Skip to content

Commit f4de3d7

Browse files
committed
docs(wasm): document cross origin mode for zk proofs
1 parent 782a9e1 commit f4de3d7

File tree

2 files changed

+119
-4
lines changed

2 files changed

+119
-4
lines changed

tfhe/docs/fhe-computation/advanced-features/zk-pok.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
145145
}
146146
```
147147

148+
## Usage in web browsers
149+
150+
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+
148157
## Benchmark
149158

150159
Please refer to the [Zero-knowledge proof benchmarks](../../getting-started/benchmarks/zk-proof-benchmarks.md) for detailed performance benchmark results.

tfhe/docs/integration/js-on-wasm-api.md

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ This document outlines how to use the **TFHE-rs** WebAssembly (WASM) client API
77
**TFHE-rs** supports 3 WASM `targets`:
88

99
* 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
1212

1313
The core of the API remains the same, requiring only minor changes in the initialization functions.
1414

@@ -95,13 +95,119 @@ async function example() {
9595
}
9696
```
9797

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:
107+
108+
```js
109+
import { setupCoordinator } from "./pkg/coordinator.js";
110+
setupCoordinator();
111+
```
112+
113+
### Initialization from the main thread
114+
115+
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+
async function setup() {
124+
await init();
125+
await init_cross_origin_worker_pool(
126+
"/sw.js", // URL to the coordinator Service Worker
127+
null, // number of workers (null = auto-detect)
128+
);
129+
await init_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:
147+
148+
**Main thread (`index.js`):**
149+
150+
```js
151+
import init, { register_cross_origin_coordinator } from "./pkg/tfhe.js";
152+
153+
async function setup() {
154+
await init();
155+
// Register the coordinator Service Worker from the main thread
156+
await register_cross_origin_coordinator("/sw.js");
157+
158+
// Then create a Web Worker that will call init_cross_origin_worker_pool_from_worker
159+
const worker = new Worker(new URL("./worker.js", import.meta.url), {
160+
type: "module",
161+
});
162+
// ...
163+
}
164+
```
165+
166+
**Web Worker (`worker.js`):**
167+
168+
```js
169+
import init, {
170+
init_panic_hook,
171+
init_cross_origin_worker_pool_from_worker,
172+
} from "./pkg/tfhe.js";
173+
174+
async function main() {
175+
await init();
176+
await init_cross_origin_worker_pool_from_worker();
177+
await init_panic_hook();
178+
// TFHE-rs is ready, ZK proof operations will run in parallel
179+
}
180+
```
181+
182+
### Runtime detection
183+
184+
You can detect at runtime whether cross-origin isolation is available and fall back to cross-origin workers automatically:
185+
186+
```js
187+
import { threads } from "wasm-feature-detect";
188+
import init, {
189+
initThreadPool,
190+
init_cross_origin_worker_pool,
191+
} from "./pkg/tfhe.js";
192+
193+
await init();
194+
let supportsThreads = await threads();
195+
if (supportsThreads) {
196+
// Standard SharedArrayBuffer parallelism
197+
await initThreadPool(navigator.hardwareConcurrency);
198+
} else {
199+
// Fallback: cross-origin worker pool
200+
await init_cross_origin_worker_pool("/sw.js");
201+
}
202+
```
203+
98204
## Compiling the WASM API
99205
100206
Use the provided Makefile in the **TFHE-rs** repository to compile for the desired target:
101207
102208
* `make build_node_js_api` for the Node.js API
103-
* `make build_web_js_api` for the browser API
104-
* `make build_web_js_api_parallel` for the browser API with parallelism
209+
* `make build_web_js_api` for the browser API (also used for cross-origin parallelism)
210+
* `make build_web_js_api_parallel` for the browser API with parallelism (requires cross-origin isolation)
105211
106212
The compiled WASM packages are located in `tfhe/pkg`.
107213

0 commit comments

Comments
 (0)