Skip to content

Commit

Permalink
Implement first version of profiling scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
LogvinovLeon committed Jan 25, 2024
1 parent 11f7062 commit b10a276
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 3 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
"@noir-lang/backend_barretenberg": "0.22.0-179c90d.nightly",
"@noir-lang/noir_js": "0.22.0-179c90d.nightly",
"@noir-lang/noirc_abi": "^0.22.0-179c90d.nightly",
"toml": "^3.0.0",
"tsx": "^4.7.0",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"yargs": "^17.7.2"
},
"type": "module",
"license": "MIT",
"devDependencies": {
"@types/node": "^20.11.6",
"@types/yargs": "^17.0.32",
"prettier": "^3.2.4"
}
}
27 changes: 27 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

export enum Action {
Verify = "verify",
Prove = "prove",
}

interface Arguments {
package: string;
action: Action;
}

export const argv = yargs(hideBin(process.argv))
.usage("Usage: $0 --package <string> --action <action>")
.option("package", {
describe: "Noir package name",
type: "string",
demandOption: true,
})
.option("action", {
describe: "Action to perform",
type: "string",
demandOption: true,
choices: Object.values(Action),
})
.help().argv as unknown as Arguments;
37 changes: 37 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
readInputMap,
readProof,
readWitnessMap,
readCircuit,
} from "./utils.js";
import assert from "assert";
import os from "os";
import { BarretenbergBackend } from "@noir-lang/backend_barretenberg";
import { Noir } from "@noir-lang/noir_js";
import { Action, argv } from "./cli.js";

const PROOF_PATH = `./proofs/${argv.package}.proof`;
const VERIFIER_MAP_PATH = `./circuits/${argv.package}/Verifier.toml`;
const PROVER_MAP_PATH = `./circuits/${argv.package}/Prover.toml`;

const circuit = await readCircuit(argv.package);
const backend = new BarretenbergBackend(circuit, {
threads: os.cpus().length,
});
const noir = new Noir(circuit, backend);

if (argv.action == Action.Verify) {
let proof = await readProof(PROOF_PATH);
let witnessMap = await readWitnessMap(VERIFIER_MAP_PATH, circuit.abi);
const proofData = {
proof,
publicInputs: Array.from(witnessMap.values()),
};
let isCorrect = await noir.verifyFinalProof(proofData);
assert(isCorrect, "Proof vefification failed");
} else if (argv.action == Action.Prove) {
let inputMap = await readInputMap(PROVER_MAP_PATH);
await noir.generateFinalProof(inputMap);
}

await noir.destroy();
55 changes: 55 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { CompiledCircuit, WitnessMap } from "@noir-lang/noir_js";
import { promises as fs } from "fs";
import toml from "toml";

import { Abi, abiEncode, type InputMap } from "@noir-lang/noirc_abi";

export async function readCircuit(
packageName: string,
): Promise<CompiledCircuit> {
const CIRCUIT_PATH = `./target/${packageName}.json`;
const data = await fs.readFile(CIRCUIT_PATH, "utf8");
return JSON.parse(data);
}

export async function readProof(path: string): Promise<Uint8Array> {
const proofHex = await fs.readFile(path, "utf-8");
return encodeHexString("0x" + proofHex);
}

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

export async function readInputMap(path: string): Promise<InputMap> {
const verifierData = await fs.readFile(path, "utf-8");
const inputMap = toml.parse(verifierData);
return inputMap;
}

export function encodeHexString(value: string): Uint8Array {
if (!isHex(value)) {
throw new Error(`Invalid hexstring: ${value}`);
}
const arr = [];
for (let i = 2; i < value.length; i += 2) {
arr.push(parseInt(value.substr(i, 2), 16));
}
return new Uint8Array(arr);
}

export type Hex = `0x${string}`;

export function isHex(
value: unknown,
{ strict = true }: { strict?: boolean } = {},
): value is Hex {
if (!value) return false;
if (typeof value !== "string") return false;
return strict ? /^0x[0-9a-fA-F]*$/.test(value) : value.startsWith("0x");
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"target": "es2020",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"resolveJsonModule": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
Expand Down
108 changes: 106 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,31 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:^20.11.6":
version: 20.11.6
resolution: "@types/node@npm:20.11.6"
dependencies:
undici-types: "npm:~5.26.4"
checksum: 922bc1e78076bb79fd168902c80321b54487181e3be3386e183fc535d102d91c8902da25148eaec841410fab7fb872fd4a93ec9ac9299cedf0ebcbf16b442aa1
languageName: node
linkType: hard

"@types/yargs-parser@npm:*":
version: 21.0.3
resolution: "@types/yargs-parser@npm:21.0.3"
checksum: e71c3bd9d0b73ca82e10bee2064c384ab70f61034bbfb78e74f5206283fc16a6d85267b606b5c22cb2a3338373586786fed595b2009825d6a9115afba36560a0
languageName: node
linkType: hard

"@types/yargs@npm:^17.0.32":
version: 17.0.32
resolution: "@types/yargs@npm:17.0.32"
dependencies:
"@types/yargs-parser": "npm:*"
checksum: 2095e8aad8a4e66b86147415364266b8d607a3b95b4239623423efd7e29df93ba81bb862784a6e08664f645cc1981b25fd598f532019174cd3e5e1e689e1cccf
languageName: node
linkType: hard

"abbrev@npm:^2.0.0":
version: 2.0.0
resolution: "abbrev@npm:2.0.0"
Expand Down Expand Up @@ -381,6 +406,17 @@ __metadata:
languageName: node
linkType: hard

"cliui@npm:^8.0.1":
version: 8.0.1
resolution: "cliui@npm:8.0.1"
dependencies:
string-width: "npm:^4.2.0"
strip-ansi: "npm:^6.0.1"
wrap-ansi: "npm:^7.0.0"
checksum: 4bda0f09c340cbb6dfdc1ed508b3ca080f12992c18d68c6be4d9cf51756033d5266e61ec57529e610dacbf4da1c634423b0c1b11037709cc6b09045cbd815df5
languageName: node
linkType: hard

"color-convert@npm:^2.0.1":
version: 2.0.1
resolution: "color-convert@npm:2.0.1"
Expand Down Expand Up @@ -558,6 +594,13 @@ __metadata:
languageName: node
linkType: hard

"escalade@npm:^3.1.1":
version: 3.1.1
resolution: "escalade@npm:3.1.1"
checksum: afd02e6ca91ffa813e1108b5e7756566173d6bc0d1eb951cb44d6b21702ec17c1cf116cfe75d4a2b02e05acb0b808a7a9387d0d1ca5cf9c04ad03a8445c3e46d
languageName: node
linkType: hard

"exponential-backoff@npm:^3.1.1":
version: 3.1.1
resolution: "exponential-backoff@npm:3.1.1"
Expand Down Expand Up @@ -619,6 +662,13 @@ __metadata:
languageName: node
linkType: hard

"get-caller-file@npm:^2.0.5":
version: 2.0.5
resolution: "get-caller-file@npm:2.0.5"
checksum: c6c7b60271931fa752aeb92f2b47e355eac1af3a2673f47c9589e8f8a41adc74d45551c1bc57b5e66a80609f10ffb72b6f575e4370d61cc3f7f3aaff01757cde
languageName: node
linkType: hard

"get-tsconfig@npm:^4.7.2":
version: 4.7.2
resolution: "get-tsconfig@npm:4.7.2"
Expand Down Expand Up @@ -926,9 +976,13 @@ __metadata:
"@noir-lang/backend_barretenberg": "npm:0.22.0-179c90d.nightly"
"@noir-lang/noir_js": "npm:0.22.0-179c90d.nightly"
"@noir-lang/noirc_abi": "npm:^0.22.0-179c90d.nightly"
"@types/node": "npm:^20.11.6"
"@types/yargs": "npm:^17.0.32"
prettier: "npm:^3.2.4"
toml: "npm:^3.0.0"
tsx: "npm:^4.7.0"
typescript: "npm:^5.3.3"
yargs: "npm:^17.7.2"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -995,6 +1049,13 @@ __metadata:
languageName: node
linkType: hard

"require-directory@npm:^2.1.1":
version: 2.1.1
resolution: "require-directory@npm:2.1.1"
checksum: 83aa76a7bc1531f68d92c75a2ca2f54f1b01463cb566cf3fbc787d0de8be30c9dbc211d1d46be3497dac5785fe296f2dd11d531945ac29730643357978966e99
languageName: node
linkType: hard

"resolve-pkg-maps@npm:^1.0.0":
version: 1.0.0
resolution: "resolve-pkg-maps@npm:1.0.0"
Expand Down Expand Up @@ -1087,7 +1148,7 @@ __metadata:
languageName: node
linkType: hard

"string-width-cjs@npm:string-width@^4.2.0, string-width@npm:^4.1.0":
"string-width-cjs@npm:string-width@^4.2.0, string-width@npm:^4.1.0, string-width@npm:^4.2.0, string-width@npm:^4.2.3":
version: 4.2.3
resolution: "string-width@npm:4.2.3"
dependencies:
Expand Down Expand Up @@ -1141,6 +1202,13 @@ __metadata:
languageName: node
linkType: hard

"toml@npm:^3.0.0":
version: 3.0.0
resolution: "toml@npm:3.0.0"
checksum: 8d7ed3e700ca602e5419fca343e1c595eb7aa177745141f0761a5b20874b58ee5c878cd045c408da9d130cb2b611c639912210ba96ce2f78e443569aa8060c18
languageName: node
linkType: hard

"tslib@npm:^2.4.0":
version: 2.6.2
resolution: "tslib@npm:2.6.2"
Expand Down Expand Up @@ -1184,6 +1252,13 @@ __metadata:
languageName: node
linkType: hard

"undici-types@npm:~5.26.4":
version: 5.26.5
resolution: "undici-types@npm:5.26.5"
checksum: bb673d7876c2d411b6eb6c560e0c571eef4a01c1c19925175d16e3a30c4c428181fb8d7ae802a261f283e4166a0ac435e2f505743aa9e45d893f9a3df017b501
languageName: node
linkType: hard

"unique-filename@npm:^3.0.0":
version: 3.0.0
resolution: "unique-filename@npm:3.0.0"
Expand Down Expand Up @@ -1224,7 +1299,7 @@ __metadata:
languageName: node
linkType: hard

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0, wrap-ansi@npm:^7.0.0":
version: 7.0.0
resolution: "wrap-ansi@npm:7.0.0"
dependencies:
Expand All @@ -1246,9 +1321,38 @@ __metadata:
languageName: node
linkType: hard

"y18n@npm:^5.0.5":
version: 5.0.8
resolution: "y18n@npm:5.0.8"
checksum: 4df2842c36e468590c3691c894bc9cdbac41f520566e76e24f59401ba7d8b4811eb1e34524d57e54bc6d864bcb66baab7ffd9ca42bf1eda596618f9162b91249
languageName: node
linkType: hard

"yallist@npm:^4.0.0":
version: 4.0.0
resolution: "yallist@npm:4.0.0"
checksum: 2286b5e8dbfe22204ab66e2ef5cc9bbb1e55dfc873bbe0d568aa943eb255d131890dfd5bf243637273d31119b870f49c18fcde2c6ffbb7a7a092b870dc90625a
languageName: node
linkType: hard

"yargs-parser@npm:^21.1.1":
version: 21.1.1
resolution: "yargs-parser@npm:21.1.1"
checksum: f84b5e48169479d2f402239c59f084cfd1c3acc197a05c59b98bab067452e6b3ea46d4dd8ba2985ba7b3d32a343d77df0debd6b343e5dae3da2aab2cdf5886b2
languageName: node
linkType: hard

"yargs@npm:^17.7.2":
version: 17.7.2
resolution: "yargs@npm:17.7.2"
dependencies:
cliui: "npm:^8.0.1"
escalade: "npm:^3.1.1"
get-caller-file: "npm:^2.0.5"
require-directory: "npm:^2.1.1"
string-width: "npm:^4.2.3"
y18n: "npm:^5.0.5"
yargs-parser: "npm:^21.1.1"
checksum: ccd7e723e61ad5965fffbb791366db689572b80cca80e0f96aad968dfff4156cd7cd1ad18607afe1046d8241e6fb2d6c08bf7fa7bfb5eaec818735d8feac8f05
languageName: node
linkType: hard

0 comments on commit b10a276

Please sign in to comment.