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: use singleton WasmBlackBoxFunctionSolver in noir_js #3966

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading