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

Use ABI to correctly pass around private/public params in recursive proofs #8

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Abi } from "@noir-lang/noirc_abi";

export function filterPublic(abi: Abi): Abi {
return {
...abi,
parameters: abi.parameters.filter((parameter) => parameter.visibility === "public"),
};
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readInputMap, readProof, readWitnessMap, initCircuit } from "./utils.js";
import { readAllInputs, readProof, readWitnessMap, initCircuit } from "./utils.js";
import assert from "assert";
import { Action, argv } from "./cli.js";

Expand All @@ -14,7 +14,7 @@ if (argv.action == Action.Verify) {
let isCorrect = await noir.verifyProof(proofData);
assert(isCorrect, "Proof verification failed");
} else if (argv.action == Action.Prove) {
let inputMap = await readInputMap(argv.package);
let inputMap = await readAllInputs(argv.package);
await noir.generateProof(inputMap);
}

Expand Down
11 changes: 7 additions & 4 deletions src/recursive.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { initCircuit, readInputMap } from "./utils.js";
import { Field, type InputMap } from "@noir-lang/noirc_abi";
import { encodePublicInputs, initCircuit, readCircuit } from "./utils.js";
import { type InputMap } from "@noir-lang/noirc_abi";
import assert from "assert";
import { prepareIntermediateProofArtifacts, prepareIntermediateProofArtifactsUsingNargo } from "./recursive_utils.js";

const packageName = "keccak_big";
const packageName = "keccak";
const keccakCircuit = await readCircuit(packageName);
const { vkAsFields, proofAsFields, vkHash } = await prepareIntermediateProofArtifactsUsingNargo(packageName);

const publicInputs = await encodePublicInputs(packageName, keccakCircuit.abi);

// // RECURSIVE PROOF
const recursive = await initCircuit("recursive");
const recursionInputs: InputMap = {
verification_key: vkAsFields,
proof: proofAsFields,
public_inputs: [(await readInputMap(packageName)).x as Field],
public_inputs: publicInputs,
key_hash: vkHash,
};
const { witness: recursiveWitness } = await recursive.noir.execute(recursionInputs);
Expand Down
6 changes: 3 additions & 3 deletions src/recursive_tree.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { initCircuit, readInputMap } from "./utils.js";
import { initCircuit, readAllInputs } from "./utils.js";
import { InputMap } from "@noir-lang/noir_js";
import { Field } from "@noir-lang/noirc_abi";
import assert from "assert";
Expand All @@ -10,8 +10,8 @@ const rhsProof = await prepareIntermediateProofArtifacts(packageName);

// RECURSIVE PROOF
const recursive2x = await initCircuit("recursive_2x");
const lhsPublicInputs = [(await readInputMap(packageName)).x as Field];
const rhsPublicInputs = [(await readInputMap(packageName)).x as Field];
const lhsPublicInputs = [(await readAllInputs(packageName)).x as Field];
const rhsPublicInputs = [(await readAllInputs(packageName)).x as Field];
const lhsRecursionInputs: InputMap = {
verification_key: lhsProof.vkAsFields,
proof: lhsProof.proofAsFields,
Expand Down
21 changes: 14 additions & 7 deletions src/recursive_utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import assert from "assert";
import { initCircuit, readInputMap, readProofHex, readWitnessMap } from "./utils.js";
import {
encodePublicInputs,
initCircuit,
readAllInputs,
readProofHex,
readPublicInputs,
readWitnessMap,
} from "./utils.js";
import { readFile, writeFile } from "fs/promises";
import { Hex, concatHex } from "viem";
import { CompiledCircuit } from "@noir-lang/noir_js";
Expand All @@ -15,7 +22,7 @@ export interface IntermediateProofArtifacts {
export async function prepareIntermediateProofArtifacts(packageName: string): Promise<IntermediateProofArtifacts> {
const { bb, noir } = await initCircuit(packageName);

const inputs = await readInputMap(packageName);
const inputs = await readAllInputs(packageName);
let { witness } = await noir.execute(inputs);

console.time(`${packageName}.bb.generateIntermediateProof`);
Expand All @@ -28,7 +35,7 @@ export async function prepareIntermediateProofArtifacts(packageName: string): Pr
console.timeEnd(`${packageName}.bb.verifyIntermediateProof`);

console.time(`${packageName}.bb.generateIntermediateProofArtifacts`);
const numPublicInputs = 1;
const numPublicInputs = 32 + 32 + 1;
const intermediateProofArtifacts = await bb.generateRecursiveProofArtifacts(proof, numPublicInputs);
console.timeEnd(`${packageName}.bb.generateIntermediateProofArtifacts`);

Expand All @@ -44,17 +51,17 @@ async function getProofAsFields(
): Promise<string[]> {
const proofHex = await readProofHex(packageName);

const inputs = await readWitnessMap(packageName, circuit.abi);
const inputsHex = concatHex(Array.from(inputs.values()) as Hex[]);
const publicInputs = await encodePublicInputs(packageName, circuit.abi);
const publicInputsHex = concatHex(publicInputs);

const proofWithInputsHex = concatHex([inputsHex, proofHex]);
const proofWithInputsHex = concatHex([publicInputsHex, proofHex]);

await writeFile(bb.proofWithInputsPath, proofWithInputsHex.substring(2), "hex");

await bb.proofAsFields();

const proofAsFieldsWithInputs = JSON.parse(await readFile(bb.proofAsFieldsPath, "utf8"));
return proofAsFieldsWithInputs.slice(inputs.size);
return proofAsFieldsWithInputs.slice(publicInputs.length);
}

export async function prepareIntermediateProofArtifactsUsingNargo(
Expand Down
24 changes: 20 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import os from "os";

import { Abi, abiEncode, type InputMap } from "@noir-lang/noirc_abi";
import { BarretenbergBackend } from "@noir-lang/backend_barretenberg";
import { filterPublic } from "./abi.js";

interface CircuitData {
noir: Noir;
Expand Down Expand Up @@ -45,16 +46,23 @@ export async function readProofHex(packageName: string): Promise<Hex> {
}

export async function readWitnessMap(packageName: string, abi: Abi): Promise<WitnessMap> {
const inputMap = await readInputMap(packageName);
const inputMap = await readAllInputs(packageName);
const witnessMap = abiEncode(abi, inputMap, inputMap["return"]);
return witnessMap;
}

export async function readInputMap(packageName: string): Promise<InputMap> {
export async function readAllInputs(packageName: string): Promise<InputMap> {
const PROVER_MAP_PATH = `./circuits/${packageName}/Prover.toml`;
const verifierData = await fs.readFile(PROVER_MAP_PATH, "utf-8");
const inputMap = toml.parse(verifierData);
return inputMap;
const allInputs = toml.parse(verifierData);
return allInputs;
}

export async function readPublicInputs(packageName: string): Promise<InputMap> {
const VERIFIER_MAP_PATH = `./circuits/${packageName}/Verifier.toml`;
const verifierData = await fs.readFile(VERIFIER_MAP_PATH, "utf-8");
const publicInputs = toml.parse(verifierData);
return publicInputs;
}

export function encodeHexString(value: string): Uint8Array {
Expand Down Expand Up @@ -82,3 +90,11 @@ export async function withProfiling<T>(name: string, fn: () => Promise<T>): Prom
console.timeEnd(name);
return result;
}

export async function encodePublicInputs(packageName: string, abi: Abi): Promise<Hex[]> {
const publicInputs = await readPublicInputs(packageName);
const publicInputsAbi = filterPublic(abi);
const publicInputsEncodedMap = abiEncode(publicInputsAbi, publicInputs, publicInputs["return"]);
const publicInputsEncoded = Array.from(publicInputsEncodedMap.values());
return publicInputsEncoded as Hex[];
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea:
new CircuitAbiCodec(abi).encodeInputs(input, onlyPublic: true)

new Circuit(abi, bytecode, vkHash, vkAsFields)

Loading