Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,44 @@ test_cases.forEach((testInfo) => {

const contract = await ethers.deployContract(testInfo.compiled, [], {});

const result = await contract.verify(proofData.proof, proofData.publicInputs);
const publicInputIndices = [...proofData.publicInputs.keys()].sort();
const flattenedPublicInputs = publicInputIndices.map((index) =>
hexToUint8Array(proofData.publicInputs.get(index) as string),
);
const publicInputsConcatenated = flattenUint8Arrays(flattenedPublicInputs);

const result = await contract.verify(proofData.proof, publicInputsConcatenated);

expect(result).to.be.true;
});
});

function flattenUint8Arrays(arrays: Uint8Array[]): Uint8Array {
const totalLength = arrays.reduce((acc, val) => acc + val.length, 0);
const result = new Uint8Array(totalLength);

let offset = 0;
for (const arr of arrays) {
result.set(arr, offset);
offset += arr.length;
}

return result;
}

function hexToUint8Array(hex: string): Uint8Array {
const sanitised_hex = BigInt(hex).toString(16).padStart(64, '0');

const len = sanitised_hex.length / 2;
const u8 = new Uint8Array(len);

let i = 0;
let j = 0;
while (i < len) {
u8[i] = parseInt(sanitised_hex.slice(j, j + 2), 16);
i += 1;
j += 2;
}

return u8;
}
63 changes: 59 additions & 4 deletions tooling/noir_js_backend_barretenberg/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { decompressSync as gunzip } from 'fflate';
import { acirToUint8Array } from './serialize.js';
import { Backend, CompiledCircuit, ProofData } from '@noir-lang/types';
import { BackendOptions } from './types.js';
import { WitnessMap } from '@noir-lang/noirc_abi';

// This is the number of bytes in a UltraPlonk proof
// minus the public inputs.
Expand All @@ -18,7 +19,7 @@ export class BarretenbergBackend implements Backend {
private acirUncompressedBytecode: Uint8Array;

constructor(
acirCircuit: CompiledCircuit,
private acirCircuit: CompiledCircuit,
private options: BackendOptions = { threads: 1 },
) {
const acirBytecodeBase64 = acirCircuit.bytecode;
Expand Down Expand Up @@ -93,13 +94,32 @@ export class BarretenbergBackend implements Backend {
const publicInputsConcatenated = proofWithPublicInputs.slice(0, splitIndex);

const publicInputSize = 32;
const publicInputs: Uint8Array[] = [];
const flattenedPublicInputs: Uint8Array[] = [];

for (let i = 0; i < publicInputsConcatenated.length; i += publicInputSize) {
const publicInput = publicInputsConcatenated.slice(i, i + publicInputSize);
publicInputs.push(publicInput);
flattenedPublicInputs.push(publicInput);
}

const abi = this.acirCircuit.abi;
const return_value_witnesses = abi.return_witnesses;
const public_parameters = abi.parameters.filter((param) => param.visibility === 'public');
const public_parameter_witnesses: number[] = public_parameters.flatMap((param) =>
abi.param_witnesses[param.name].flatMap((witness_range) =>
Array.from({ length: witness_range.end - witness_range.start }, (_, i) => witness_range.start + i),
),
);

// We now have an array of witness indices which have been deduplicated and sorted in ascending order.
// The elements of this array should correspond to the elements of `flattenedPublicInputs` so that we can build up a `WitnessMap`.
const public_input_witnesses = [...new Set(public_parameter_witnesses.concat(return_value_witnesses))].sort();

const publicInputs: WitnessMap = new Map();
public_input_witnesses.forEach((witness_index, index) => {
const witness_value = uint8ArrayToHex(flattenedPublicInputs[index]);
publicInputs.set(witness_index, witness_value);
});

const proof = proofWithPublicInputs.slice(splitIndex);

return { proof, publicInputs };
Expand Down Expand Up @@ -185,7 +205,11 @@ export class BarretenbergBackend implements Backend {

function reconstructProofWithPublicInputs(proofData: ProofData): Uint8Array {
// Flatten publicInputs
const publicInputsConcatenated = flattenUint8Arrays(proofData.publicInputs);
const publicInputIndices = [...proofData.publicInputs.keys()].sort();
const flattenedPublicInputs = publicInputIndices.map((index) =>
hexToUint8Array(proofData.publicInputs.get(index) as string),
);
const publicInputsConcatenated = flattenUint8Arrays(flattenedPublicInputs);

// Concatenate publicInputs and proof
const proofWithPublicInputs = Uint8Array.from([...publicInputsConcatenated, ...proofData.proof]);
Expand All @@ -206,5 +230,36 @@ function flattenUint8Arrays(arrays: Uint8Array[]): Uint8Array {
return result;
}

function uint8ArrayToHex(buffer: Uint8Array): string {
const hex: string[] = [];

buffer.forEach(function (i) {
let h = i.toString(16);
if (h.length % 2) {
h = '0' + h;
}
hex.push(h);
});

return '0x' + hex.join('');
}

function hexToUint8Array(hex: string): Uint8Array {
const sanitised_hex = BigInt(hex).toString(16).padStart(64, '0');

const len = sanitised_hex.length / 2;
const u8 = new Uint8Array(len);

let i = 0;
let j = 0;
while (i < len) {
u8[i] = parseInt(sanitised_hex.slice(j, j + 2), 16);
i += 1;
j += 2;
}

return u8;
}

// typedoc exports
export { Backend, BackendOptions, CompiledCircuit, ProofData };
4 changes: 2 additions & 2 deletions tooling/noir_js_types/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Abi } from '@noir-lang/noirc_abi';
import { Abi, WitnessMap } from '@noir-lang/noirc_abi';

export interface Backend {
/**
Expand Down Expand Up @@ -43,7 +43,7 @@ export interface Backend {
* */
export type ProofData = {
/** @description Public inputs of a proof */
publicInputs: Uint8Array[];
publicInputs: WitnessMap;
/** @description An byte array representing the proof */
proof: Uint8Array;
};
Expand Down