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(noir_js): allow providing foreign call handlers in noirJS #3294

Merged
merged 6 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions tooling/noir_js/src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import { Backend, CompiledCircuit, ProofData } from '@noir-lang/types';
import { generateWitness } from './witness_generation.js';
import initAbi, { abiDecode, InputMap, InputValue } from '@noir-lang/noirc_abi';
import initACVM, { compressWitness } from '@noir-lang/acvm_js';
import initACVM, { compressWitness, ForeignCallHandler } from '@noir-lang/acvm_js';

export class Noir {
constructor(
private circuit: CompiledCircuit,
private backend?: Backend,
) {}
) { }

async init(): Promise<void> {
// If these are available, then we are in the
Expand All @@ -29,9 +29,9 @@ export class Noir {
}

// Initial inputs to your program
async execute(inputs: InputMap): Promise<{ witness: Uint8Array; returnValue: InputValue }> {
async execute(inputs: InputMap, foreignCallHandler?: ForeignCallHandler): Promise<{ witness: Uint8Array; returnValue: InputValue }> {
await this.init();
const witness = await generateWitness(this.circuit, inputs);
const witness = await generateWitness(this.circuit, inputs, foreignCallHandler);
const { return_value: returnValue } = abiDecode(this.circuit.abi, witness);
return { witness: compressWitness(witness), returnValue };
}
Expand Down
12 changes: 7 additions & 5 deletions tooling/noir_js/src/witness_generation.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { abiEncode, InputMap } from '@noir-lang/noirc_abi';
import { base64Decode } from './base64_decode.js';
import { executeCircuit, WitnessMap } from '@noir-lang/acvm_js';
import { executeCircuit, WitnessMap, ForeignCallHandler, ForeignCallInput } from '@noir-lang/acvm_js';
import { CompiledCircuit } from '@noir-lang/types';

const defaultForeignCallHandler: ForeignCallHandler = (name: string, args: ForeignCallInput[]) => {
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
throw Error(`Unexpected oracle during execution: ${name}(${args.join(', ')})`)
}

// Generates the witnesses needed to feed into the chosen proving system
export async function generateWitness(compiledProgram: CompiledCircuit, inputs: InputMap): Promise<WitnessMap> {
export async function generateWitness(compiledProgram: CompiledCircuit, inputs: InputMap, foreignCallHandler: ForeignCallHandler = defaultForeignCallHandler): Promise<WitnessMap> {
// Throws on ABI encoding error
const witnessMap = abiEncode(compiledProgram.abi, inputs);

// 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, () => {
throw Error('unexpected oracle during execution');
});
const solvedWitness = await executeCircuit(base64Decode(compiledProgram.bytecode), witnessMap, foreignCallHandler);
return solvedWitness;
} catch (err) {
throw new Error(`Circuit execution failed: ${err}`);
Expand Down
Loading