Skip to content

Commit

Permalink
feat: add crsPath to BackendOptions (#8775)
Browse files Browse the repository at this point in the history
- The default CRS download path of `$HOME/.bb-crs` might not be always
available in all deployments - for example in a serverless function
(NextJS API, AWS Lambda) only `/tmp` is available for writing files.

- In this PR, `crsPath` param is added to `BackendOptions` and used when
calling `Crs.new()` - so they can be used in backend and verifiers
([ref1](https://github.com/AztecProtocol/aztec-packages/blob/master/noir/noir-repo/tooling/noir_js_backend_barretenberg/src/verifier.ts#L37-L43),
[ref2](https://github.com/AztecProtocol/aztec-packages/blob/master/barretenberg/ts/src/barretenberg/verifier.ts#L32-L33)).

Co-authored-by: ludamad <[email protected]>
  • Loading branch information
saleel and ludamad authored Oct 2, 2024
1 parent 784d483 commit 78fa676
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions barretenberg/ts/src/barretenberg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@ export { UltraPlonkBackend, UltraHonkBackend } from './backend.js';
const debug = createDebug('bb.js:wasm');

export type BackendOptions = {
/** @description Number of threads to run the backend worker on */
threads?: number;

/** @description Initial and Maximum memory to be alloted to the backend worker */
memory?: { initial?: number; maximum?: number };

/** @description Path to download CRS files */
crsPath?: string;
};

/**
* The main class library consumers interact with.
* It extends the generated api, and provides a static constructor "new" to compose components.
*/
export class Barretenberg extends BarretenbergApi {
private constructor(private worker: any, wasm: BarretenbergWasmWorker) {
private options: BackendOptions;

private constructor(private worker: any, wasm: BarretenbergWasmWorker, options: BackendOptions) {
super(wasm);
this.options = options;
}

/**
Expand All @@ -33,20 +42,20 @@ export class Barretenberg extends BarretenbergApi {
* and blocking the main thread in the browser is not allowed.
* It threads > 1 (defaults to hardware availability), child threads will be created on their own workers.
*/
static async new({ threads: desiredThreads, memory }: BackendOptions = {}) {
static async new(options: BackendOptions = {}) {
const worker = createMainWorker();
const wasm = getRemoteBarretenbergWasm<BarretenbergWasmMainWorker>(worker);
const { module, threads } = await fetchModuleAndThreads(desiredThreads);
await wasm.init(module, threads, proxy(debug), memory?.initial, memory?.maximum);
return new Barretenberg(worker, wasm);
const { module, threads } = await fetchModuleAndThreads(options.threads);
await wasm.init(module, threads, proxy(debug), options.memory?.initial, options.memory?.maximum);
return new Barretenberg(worker, wasm, options);
}

async getNumThreads() {
return await this.wasm.getNumThreads();
}

async initSRSForCircuitSize(circuitSize: number): Promise<void> {
const crs = await Crs.new(circuitSize + Math.floor((circuitSize * 6) / 10) + 1);
const crs = await Crs.new(circuitSize + Math.floor((circuitSize * 6) / 10) + 1, this.options.crsPath);
await this.commonInitSlabAllocator(circuitSize);
await this.srsInitSrs(new RawBuffer(crs.getG1Data()), crs.numPoints, new RawBuffer(crs.getG2Data()));
}
Expand Down

0 comments on commit 78fa676

Please sign in to comment.