Skip to content

Commit

Permalink
feat: use singleton WasmBlackBoxFunctionSolver in noir_js (#3966)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This PR stores a `WasmBlackboxFunctionSolver` in a global variable so
that we can avoid reinitialisation on subsequent exections.

This takes a trick from `aztec-packages` but I haven't validated that it
actually causes a speedup

## Additional Context


## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
TomAFrench authored Jan 15, 2024
1 parent 0e91bb2 commit 10b28de
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions tooling/noir_js/src/witness_generation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import { abiEncode, InputMap } from '@noir-lang/noirc_abi';
import { base64Decode } from './base64_decode.js';
import { executeCircuit, WitnessMap, ForeignCallHandler, ForeignCallInput } from '@noir-lang/acvm_js';
import {
WitnessMap,
ForeignCallHandler,
ForeignCallInput,
createBlackBoxSolver,
WasmBlackBoxFunctionSolver,
executeCircuitWithBlackBoxSolver,
} from '@noir-lang/acvm_js';
import { CompiledCircuit } from '@noir-lang/types';

let solver: Promise<WasmBlackBoxFunctionSolver>;

const getSolver = (): Promise<WasmBlackBoxFunctionSolver> => {
if (!solver) {
solver = createBlackBoxSolver();
}
return solver;
};

const defaultForeignCallHandler: ForeignCallHandler = async (name: string, args: ForeignCallInput[]) => {
if (name == 'print') {
// By default we do not print anything for `print` foreign calls due to a need for formatting,
Expand All @@ -26,7 +42,12 @@ export async function generateWitness(
// Execute the circuit to generate the rest of the witnesses and serialize
// them into a Uint8Array.
try {
const solvedWitness = await executeCircuit(base64Decode(compiledProgram.bytecode), witnessMap, foreignCallHandler);
const solvedWitness = await executeCircuitWithBlackBoxSolver(
await getSolver(),
base64Decode(compiledProgram.bytecode),
witnessMap,
foreignCallHandler,
);
return solvedWitness;
} catch (err) {
throw new Error(`Circuit execution failed: ${err}`);
Expand Down

0 comments on commit 10b28de

Please sign in to comment.