Skip to content

Commit

Permalink
chore!: generateWitness now returns a serialized witness file (#2842)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray authored Sep 26, 2023
1 parent 76ab555 commit 57d3f37
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
6 changes: 4 additions & 2 deletions tooling/noir_js/src/witness_generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { abiEncode } from '@noir-lang/noirc_abi';
import { validateInputs } from './input_validation.js';
import { base64Decode } from './base64_decode.js';
import { WitnessMap, executeCircuit } from '@noir-lang/acvm_js';
import { witnessMapToUint8Array } from './serialize.js';

// Generates the witnesses needed to feed into the chosen proving system
export async function generateWitness(compiledProgram, inputs): Promise<WitnessMap> {
Expand All @@ -12,12 +13,13 @@ export async function generateWitness(compiledProgram, inputs): Promise<WitnessM
}
const witnessMap = abiEncode(compiledProgram.abi, inputs, null);

// Execute the circuit to generate the rest of the witnesses
// 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');
});
return solvedWitness;
return witnessMapToUint8Array(solvedWitness);
} catch (err) {
throw new Error(`Circuit execution failed: ${err}`);
}
Expand Down
14 changes: 5 additions & 9 deletions tooling/noir_js/test/node/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import assert_lt_json from '../noir_compiled_examples/assert_lt/target/assert_lt.json' assert { type: 'json' };
import { generateWitness, witnessMapToUint8Array } from '../../src/index.js';
import { generateWitness } from '../../src/index.js';
import { Backend } from '../backend/barretenberg.js';

it('end-to-end proof creation and verification (outer)', async () => {
Expand All @@ -9,14 +9,13 @@ it('end-to-end proof creation and verification (outer)', async () => {
x: '2',
y: '3',
};
const solvedWitness = await generateWitness(assert_lt_json, inputs);
const serializedWitness = await generateWitness(assert_lt_json, inputs);

// bb.js part
//
// Proof creation
const prover = new Backend(assert_lt_json.bytecode);
await prover.init();
const serializedWitness = witnessMapToUint8Array(solvedWitness);
const proof = await prover.generateOuterProof(serializedWitness);

// Proof verification
Expand All @@ -30,14 +29,13 @@ it('end-to-end proof creation and verification (inner)', async () => {
x: '2',
y: '3',
};
const solvedWitness = await generateWitness(assert_lt_json, inputs);
const serializedWitness = await generateWitness(assert_lt_json, inputs);

// bb.js part
//
// Proof creation
const prover = new Backend(assert_lt_json.bytecode);
await prover.init();
const serializedWitness = witnessMapToUint8Array(solvedWitness);
const proof = await prover.generateInnerProof(serializedWitness);

// Proof verification
Expand All @@ -63,13 +61,12 @@ it('[BUG] -- bb.js null function or function signature mismatch (different insta
x: '2',
y: '3',
};
const solvedWitness = await generateWitness(assert_lt_json, inputs);
const serializedWitness = await generateWitness(assert_lt_json, inputs);

// bb.js part
const prover = new Backend(assert_lt_json.bytecode);
await prover.init();

const serializedWitness = witnessMapToUint8Array(solvedWitness);
const proof = await prover.generateOuterProof(serializedWitness);

try {
Expand Down Expand Up @@ -98,15 +95,14 @@ it('[BUG] -- bb.js null function or function signature mismatch (outer-inner) ',
x: '2',
y: '3',
};
const solvedWitness = await generateWitness(assert_lt_json, inputs);
const serializedWitness = await generateWitness(assert_lt_json, inputs);

// bb.js part
//
// Proof creation
//
const prover = new Backend(assert_lt_json.bytecode);
await prover.init();
const serializedWitness = witnessMapToUint8Array(solvedWitness);
// Create a proof using both proving systems, the majority of the time
// one would only use outer proofs.
const proofOuter = await prover.generateOuterProof(serializedWitness);
Expand Down

0 comments on commit 57d3f37

Please sign in to comment.