-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement first version of profiling scripts
- Loading branch information
1 parent
11f7062
commit b10a276
Showing
6 changed files
with
232 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters