Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add crsPath to BackendOptions #8775

Merged
merged 8 commits into from
Oct 2, 2024
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
Loading