-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(verification): helper function to verify that a zkey is valid
given a r1cs, a pot and a zkey, this helper function will verify that the zkey is valid
- Loading branch information
Showing
11 changed files
with
115 additions
and
0 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
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 | ||
} |
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
Binary file not shown.
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,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 not shown.
Binary file not shown.
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,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 not shown.
Binary file not shown.
Binary file not shown.
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,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" | ||
) | ||
}) | ||
}) | ||
}) |