From 5c74d367124c67ef7aff8ee9eb6210cffd51d867 Mon Sep 17 00:00:00 2001 From: Saleel Date: Wed, 20 Nov 2024 17:52:54 +0530 Subject: [PATCH 1/2] docs: add docs to enable multi-threading in bb.js --- barretenberg/ts/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/barretenberg/ts/README.md b/barretenberg/ts/README.md index 07101e7b726..de65658ca7c 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. Here is an example for NextJS: + +```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 From 34ea7c0b4c5d407411faa0ec38db95af80f70452 Mon Sep 17 00:00:00 2001 From: Saleel Date: Thu, 21 Nov 2024 01:07:05 +0530 Subject: [PATCH 2/2] docs: add example for multi-threaded proving --- barretenberg/ts/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barretenberg/ts/README.md b/barretenberg/ts/README.md index de65658ca7c..53df298aa07 100644 --- a/barretenberg/ts/README.md +++ b/barretenberg/ts/README.md @@ -113,7 +113,7 @@ const { Barretenberg, RawBuffer, Crs } = await import('@aztec/bb.js'); 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. Here is an example for NextJS: +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 {