Skip to content

Commit

Permalink
feat(verification): helper function to verify that a zkey is valid
Browse files Browse the repository at this point in the history
given a r1cs, a pot and a zkey, this helper function will verify that the zkey is valid
  • Loading branch information
ctrlc03 committed Mar 2, 2023
1 parent aa3d437 commit ca5712c
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/actions/src/helpers/verification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from "fs"
import { zKey } from "snarkjs"

/**
* Verify that a zKey is valid
* @param r1csLocalFilePath <string> path to the r1cs file
* @param zkeyLocalPath <string> path to the zKey file
* @param potLocalFilePath <string> path to the PoT file
* @param logger <any> logger instance
* @returns <boolean> true if the zKey is valid, false otherwise
*/
export const verifyZKey = async (
r1csLocalFilePath: string,
zkeyLocalPath: string,
potLocalFilePath: string,
logger?: any
): Promise<boolean> => {
if (!fs.existsSync(r1csLocalFilePath)) throw new Error(`R1CS file not found at ${r1csLocalFilePath}`)

if (!fs.existsSync(zkeyLocalPath)) throw new Error(`zKey file not found at ${zkeyLocalPath}`)

if (!fs.existsSync(potLocalFilePath)) throw new Error(`PoT file not found at ${potLocalFilePath}`)

const res = await zKey.verifyFromR1cs(r1csLocalFilePath, potLocalFilePath, zkeyLocalPath, logger)
return res
}
1 change: 1 addition & 0 deletions packages/actions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ export {
finalizeCeremony
} from "./helpers/functions"
export { toHex, blake512FromPath, computeSHA256ToHex } from "./helpers/crypto"
export { verifyZKey } from "./helpers/verification"
Binary file not shown.
23 changes: 23 additions & 0 deletions packages/actions/test/data/artifacts/circuit.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma circom 2.0.3;

template Main() {
signal input x1;
signal input x2;
signal input x3;
signal input x4;

signal y1;
signal y2;

signal output out;

y1 <-- x1 + x2;
y2 <-- y1 / x3;
out <-- y2 - x4;

y1 === x1 + x2;
y1 === y2 * x3;
out === y2 - x4;
}

component main { public [ x2 ] } = Main();
Binary file added packages/actions/test/data/artifacts/circuit.r1cs
Binary file not shown.
Binary file not shown.
18 changes: 18 additions & 0 deletions packages/actions/test/data/artifacts/notcircuit.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pragma circom 2.0.3;

template Main() {
signal input x;
signal x_squared;
signal x_cubed;
signal output out;

x_squared <-- x * x;
x_cubed <-- x_squared * x;
out <-- x_cubed - x + 7;

x_squared === x * x;
x_cubed === x_squared * x;
out === x_cubed - x + 7;
}

component main = Main();
Binary file added packages/actions/test/data/artifacts/notcircuit.r1cs
Binary file not shown.
Binary file not shown.
Binary file not shown.
47 changes: 47 additions & 0 deletions packages/actions/test/unit/verification.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import chai, { expect } from "chai"
import chaiAsPromised from "chai-as-promised"
import { cwd } from "process"
import { verifyZKey } from "../../src"

chai.use(chaiAsPromised)

/**
* Verification tests
*/
describe("Verification", () => {
/// @note verify that a zKey is valid
describe("verifyzKey", () => {
const zkeyPath = `${cwd()}/packages/actions/test/data/artifacts/circuit_0000.zkey`
const badzkeyPath = `${cwd()}/packages/actions/test/data/artifacts/bad_circuit_0000.zkey`
const wrongZkeyPath = `${cwd()}/packages/actions/test/data/artifacts/notcircuit_0000.zkey`
const potPath = `${cwd()}/packages/actions/test/data/artifacts/powersOfTau28_hez_final_02.ptau`
const r1csPath = `${cwd()}/packages/actions/test/data/artifacts/circuit.r1cs`
it("should return true for a valid zkey", async () => {
expect(await verifyZKey(r1csPath, zkeyPath, potPath)).to.be.true
})
it("should throw when given an invalid zkey", async () => {
await expect(verifyZKey(r1csPath, badzkeyPath, potPath)).to.be.rejected
})
it("should return false when given a zkey for another circuit", async () => {
expect(await verifyZKey(r1csPath, wrongZkeyPath, potPath)).to.be.false
})
it("should throw an error if the r1cs file is not found", async () => {
await expect(verifyZKey("invalid", zkeyPath, potPath)).to.be.rejectedWith(
Error,
"R1CS file not found at invalid"
)
})
it("should throw an error if the zkey file is not found", async () => {
await expect(verifyZKey(r1csPath, "invalid", potPath)).to.be.rejectedWith(
Error,
"zKey file not found at invalid"
)
})
it("should throw an error if the pot file is not found", async () => {
await expect(verifyZKey(r1csPath, zkeyPath, "invalid")).to.be.rejectedWith(
Error,
"PoT file not found at invalid"
)
})
})
})

0 comments on commit ca5712c

Please sign in to comment.