From 8b4ebd1ddf3e8b3bac341c612444f28ea819f6c3 Mon Sep 17 00:00:00 2001 From: saleel Date: Thu, 21 Nov 2024 09:51:29 +0530 Subject: [PATCH] docs: add docs to enable multi-threading in bb.js (#10064) --- barretenberg/ts/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/barretenberg/ts/README.md b/barretenberg/ts/README.md index 07101e7b726..53df298aa07 100644 --- a/barretenberg/ts/README.md +++ b/barretenberg/ts/README.md @@ -109,6 +109,33 @@ in size) is loaded and keeps page load times responsive. const { Barretenberg, RawBuffer, Crs } = await import('@aztec/bb.js'); ``` +### Multithreading in browser + +Multithreading in bb.js requires [`SharedArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer) to be enabled. It is only enabled in browsers if COOP and COEP headers are set by the server. Read more [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements). + +You can configure your server to set these headers for pages that perform proof generation. See [this example project](https://github.com/saleel/gitclaim/blob/main/app/next.config.mjs#L48-L67) that implements multi-threaded browser proving, which contains the below Next.js config: + +```typescript +{ + ... + async headers() { + return [ + { + source: '/:path*', + headers: [ + { key: 'Cross-Origin-Embedder-Policy', value: 'require-corp' }, + { key: 'Cross-Origin-Opener-Policy', value: 'same-origin' }, + ], + }, + ]; + }, +} +``` + +Note that adding COOP and COEP headers will disable loading of external scripts, which might be required by your application. + +You can enable these headers for specific pages that perform proof generation, but this may be challenging, especially in single-page applications. One workaround is to move the proof generation to a separate page, load it in an invisible iframe within your main application, and then use `postMessage` to communicate between the pages for generating proofs. + ## Development Create a symlink to the root script `bb.js-dev` in your path. You can now run the current state of the code from