Skip to content

Commit

Permalink
chore: Switch Noir JS to use execute program instead of circuit (#4965)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #4706 

## Summary\*

This PR actually does not do too much as we still use
`executeCircuitWithBlackBoxSolver` throughout the ACIR simulator on
aztec. That should be removed separately before we look to remove it
from ACVM JS. Noir JS backend also needs to wait for the barretenberg
interface updates before we can pass a collection of circuits to the
backend.

## Additional Context


## Documentation\*

Check one:
- [ ] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
vezenovm authored May 3, 2024
1 parent f7fa696 commit e04deec
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 8 deletions.
2 changes: 2 additions & 0 deletions tooling/noir_js/scripts/compile_test_programs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
rm -rf ./test/noir_compiled_examples/**/target
nargo --program-dir ./test/noir_compiled_examples/assert_lt compile --force
nargo --program-dir ./test/noir_compiled_examples/assert_msg_runtime compile --force
nargo --program-dir ./test/noir_compiled_examples/fold_fibonacci compile --force

9 changes: 5 additions & 4 deletions tooling/noir_js/src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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, ForeignCallHandler } from '@noir-lang/acvm_js';
import initACVM, { compressWitnessStack, ForeignCallHandler } from '@noir-lang/acvm_js';

export class Noir {
constructor(
Expand Down Expand Up @@ -55,9 +55,10 @@ export class Noir {
foreignCallHandler?: ForeignCallHandler,
): Promise<{ witness: Uint8Array; returnValue: InputValue }> {
await this.init();
const witness = await generateWitness(this.circuit, inputs, foreignCallHandler);
const { return_value: returnValue } = abiDecode(this.circuit.abi, witness);
return { witness: compressWitness(witness), returnValue };
const witness_stack = await generateWitness(this.circuit, inputs, foreignCallHandler);
const main_witness = witness_stack[0].witness;
const { return_value: returnValue } = abiDecode(this.circuit.abi, main_witness);
return { witness: compressWitnessStack(witness_stack), returnValue };
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tooling/noir_js/src/witness_generation.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { abiEncode, InputMap } from '@noir-lang/noirc_abi';
import { base64Decode } from './base64_decode.js';
import {
WitnessMap,
WitnessStack,
ForeignCallHandler,
ForeignCallInput,
createBlackBoxSolver,
WasmBlackBoxFunctionSolver,
executeCircuitWithBlackBoxSolver,
executeProgramWithBlackBoxSolver,
} from '@noir-lang/acvm_js';
import { CompiledCircuit } from '@noir-lang/types';

Expand Down Expand Up @@ -41,14 +41,14 @@ export async function generateWitness(
compiledProgram: CompiledCircuit,
inputs: InputMap,
foreignCallHandler: ForeignCallHandler = defaultForeignCallHandler,
): Promise<WitnessMap> {
): Promise<WitnessStack> {
// 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 executeCircuitWithBlackBoxSolver(
const solvedWitness = await executeProgramWithBlackBoxSolver(
await getSolver(),
base64Decode(compiledProgram.bytecode),
witnessMap,
Expand Down
23 changes: 23 additions & 0 deletions tooling/noir_js/test/node/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { expect } from 'chai';
import assert_lt_json from '../noir_compiled_examples/assert_lt/target/assert_lt.json' assert { type: 'json' };
import fold_fibonacci_json from '../noir_compiled_examples/fold_fibonacci/target/fold_fibonacci.json' assert { type: 'json' };
import { Noir } from '@noir-lang/noir_js';
import { BarretenbergBackend as Backend, BarretenbergVerifier as Verifier } from '@noir-lang/backend_barretenberg';
import { CompiledCircuit } from '@noir-lang/types';

const assert_lt_program = assert_lt_json as CompiledCircuit;
const fold_fibonacci_program = fold_fibonacci_json as CompiledCircuit;

it('end-to-end proof creation and verification (outer)', async () => {
// Noir.Js part
Expand Down Expand Up @@ -149,3 +151,24 @@ it('[BUG] -- bb.js null function or function signature mismatch (outer-inner) ',
const isValidInner = await prover.verifyProof(_proofInner);
expect(isValidInner).to.be.true;
});

it('end-to-end proof creation and verification for multiple ACIR circuits (inner)', async () => {
// Noir.Js part
const inputs = {
x: '10',
};

const program = new Noir(fold_fibonacci_program);

const { witness } = await program.execute(inputs);

// bb.js part
//
// Proof creation
const backend = new Backend(fold_fibonacci_program);
const proof = await backend.generateProof(witness);

// Proof verification
const isValid = await backend.verifyProof(proof);
expect(isValid).to.be.true;
});
14 changes: 14 additions & 0 deletions tooling/noir_js/test/node/execute.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import assert_lt_json from '../noir_compiled_examples/assert_lt/target/assert_lt.json' assert { type: 'json' };
import assert_msg_json from '../noir_compiled_examples/assert_msg_runtime/target/assert_msg_runtime.json' assert { type: 'json' };
import fold_fibonacci_json from '../noir_compiled_examples/fold_fibonacci/target/fold_fibonacci.json' assert { type: 'json' };
import { Noir } from '@noir-lang/noir_js';
import { CompiledCircuit } from '@noir-lang/types';
import { expect } from 'chai';

const assert_lt_program = assert_lt_json as CompiledCircuit;
const assert_msg_runtime = assert_msg_json as CompiledCircuit;
const fold_fibonacci_program = fold_fibonacci_json as CompiledCircuit;

it('returns the return value of the circuit', async () => {
const inputs = {
Expand All @@ -29,3 +31,15 @@ it('circuit with a dynamic assert message should fail on an assert failure not t
expect(knownError.message).to.equal('Circuit execution failed: Error: Cannot satisfy constraint');
}
});

it('successfully executes a program with multiple acir circuits', async () => {
const inputs = {
x: '10',
};
try {
await new Noir(fold_fibonacci_program).execute(inputs);
} catch (error) {
const knownError = error as Error;
expect(knownError.message).to.equal('Circuit execution failed: Error: Cannot satisfy constraint');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "fold_fibonacci"
type = "bin"
authors = [""]
compiler_version = ">=0.28.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fn main(x: u32) {
assert(fibonacci(x) == 55);
}

#[fold]
fn fibonacci(x: u32) -> u32 {
if x <= 1 {
x
} else {
fibonacci(x - 1) + fibonacci(x - 2)
}
}

0 comments on commit e04deec

Please sign in to comment.