Added zKey verify from init file

This commit is contained in:
Jordi Baylina 2020-12-18 15:38:31 +01:00
parent f07951c379
commit 9f7f26c49c
No known key found for this signature in database
GPG Key ID: 7480C80C1BE43112
9 changed files with 192 additions and 93 deletions

@ -5171,7 +5171,7 @@ const sameRatio$2 = sameRatio;
async function phase2verify(r1csFileName, pTauFileName, zkeyFileName, logger) {
async function phase2verifyFromInit(initFileName, pTauFileName, zkeyFileName, logger) {
let sr;
await Blake2b.ready();
@ -5240,11 +5240,6 @@ async function phase2verify(r1csFileName, pTauFileName, zkeyFileName, logger) {
}
// const initFileName = "~" + zkeyFileName + ".init";
const initFileName = {type: "bigMem"};
await newZKey(r1csFileName, pTauFileName, initFileName, logger);
const {fd: fdInit, sections: sectionsInit} = await readBinFile$1(initFileName, "zkey", 2);
const zkeyInit = await readHeader(fdInit, sectionsInit, "groth16");
@ -5561,6 +5556,15 @@ async function phase2verify(r1csFileName, pTauFileName, zkeyFileName, logger) {
}
async function phase2verifyFromR1cs(r1csFileName, pTauFileName, zkeyFileName, logger) {
// const initFileName = "~" + zkeyFileName + ".init";
const initFileName = {type: "bigMem"};
await newZKey(r1csFileName, pTauFileName, initFileName, logger);
return await phase2verifyFromInit(initFileName, pTauFileName, zkeyFileName);
}
async function phase2contribute(zkeyNameOld, zkeyNameNew, name, entropy, logger) {
await Blake2b.ready();
@ -6581,7 +6585,7 @@ const commands = [
action: wtnsExportJson$1
},
{
cmd: "zkey new [circuit.r1cs] [powersoftau.ptau] [circuit.zkey]",
cmd: "zkey new [circuit.r1cs] [powersoftau.ptau] [circuit_0000.zkey]",
description: "Creates an initial pkey file with zero contributions ",
alias: ["zkn"],
options: "-verbose|v",
@ -6595,7 +6599,7 @@ const commands = [
action: zkeyContribute
},
{
cmd: "zkey export bellman [circuit.zkey] [circuit.mpcparams]",
cmd: "zkey export bellman <circuit_xxxx.zkey> [circuit.mpcparams]",
description: "Export a zKey to a MPCParameters file compatible with kobi/phase2 (Bellman)",
alias: ["zkeb"],
options: "-verbose|v",
@ -6623,27 +6627,34 @@ const commands = [
action: zkeyBeacon
},
{
cmd: "zkey verify [circuit.r1cs] [powersoftau.ptau] [circuit.zkey]",
cmd: "zkey verify r1cs [circuit.r1cs] [powersoftau.ptau] [circuit_final.zkey]",
description: "Verify zkey file contributions and verify that matches with the original circuit.r1cs and ptau",
alias: ["zkv"],
alias: ["zkv", "zkvr", "zkey verify"],
options: "-verbose|v",
action: zkeyVerify
action: zkeyVerifyFromR1cs
},
{
cmd: "zkey export verificationkey [circuit.zkey] [verification_key.json]",
cmd: "zkey verify init [circuit_0000.zkey] [powersoftau.ptau] [circuit_final.zkey]",
description: "Verify zkey file contributions and verify that matches with the original circuit.r1cs and ptau",
alias: ["zkvi"],
options: "-verbose|v",
action: zkeyVerifyFromInit
},
{
cmd: "zkey export verificationkey [circuit_final.zkey] [verification_key.json]",
description: "Exports a verification key",
alias: ["zkev"],
action: zkeyExportVKey
},
{
cmd: "zkey export json [circuit.zkey] [circuit.zkey.json]",
cmd: "zkey export json [circuit_final.zkey] [circuit_final.zkey.json]",
description: "Exports a circuit key to a JSON file",
alias: ["zkej"],
options: "-verbose|v",
action: zkeyExportJson$1
},
{
cmd: "zkey export solidityverifier [circuit.zkey] [verifier.sol]",
cmd: "zkey export solidityverifier [circuit_final.zkey] [verifier.sol]",
description: "Creates a verifier in solidity",
alias: ["zkesv", "generateverifier -vk|verificationkey -v|verifier"],
action: zkeyExportSolidityVerifier
@ -6655,14 +6666,14 @@ const commands = [
action: zkeyExportSolidityCalldata
},
{
cmd: "groth16 prove [circuit.zkey] [witness.wtns] [proof.json] [public.json]",
cmd: "groth16 prove [circuit_final.zkey] [witness.wtns] [proof.json] [public.json]",
description: "Generates a zk Proof from witness",
alias: ["g16p", "zpw", "zksnark proof", "proof -pk|provingkey -wt|witness -p|proof -pub|public"],
options: "-verbose|v -protocol",
action: groth16Prove$1
},
{
cmd: "groth16 fullprove [input.json] [circuit.wasm] [circuit.zkey] [proof.json] [public.json]",
cmd: "groth16 fullprove [input.json] [circuit_final.wasm] [circuit_final.zkey] [proof.json] [public.json]",
description: "Generates a zk Proof from input",
alias: ["g16f", "g16i"],
options: "-verbose|v -protocol",
@ -6848,7 +6859,7 @@ async function zksnarkSetup(params, options) {
// groth16 prove [circuit.zkey] [witness.wtns] [proof.json] [public.json]
async function groth16Prove$1(params, options) {
const zkeyName = params[0] || "circuit.zkey";
const zkeyName = params[0] || "circuit_final.zkey";
const witnessName = params[1] || "witness.wtns";
const proofName = params[2] || "proof.json";
const publicName = params[3] || "public.json";
@ -6868,7 +6879,7 @@ async function groth16FullProve$1(params, options) {
const inputName = params[0] || "input.json";
const wasmName = params[1] || "circuit.wasm";
const zkeyName = params[2] || "circuit.zkey";
const zkeyName = params[2] || "circuit_final.zkey";
const proofName = params[3] || "proof.json";
const publicName = params[4] || "public.json";
@ -6906,9 +6917,9 @@ async function groth16Verify$1(params, options) {
}
}
// zkey export vkey [circuit.zkey] [verification_key.json]",
// zkey export vkey [circuit_final.zkey] [verification_key.json]",
async function zkeyExportVKey(params, options) {
const zkeyName = params[0] || "circuit.zkey";
const zkeyName = params[0] || "circuit_final.zkey";
const verificationKeyName = params[1] || "verification_key.json";
if (options.verbose) Logger.setLogLevel("DEBUG");
@ -6919,10 +6930,10 @@ async function zkeyExportVKey(params, options) {
await fs.promises.writeFile(verificationKeyName, S);
}
// zkey export json [circuit.zkey] [circuit.zkey.json]",
// zkey export json [circuit_final.zkey] [circuit.zkey.json]",
async function zkeyExportJson$1(params, options) {
const zkeyName = params[0] || "circuit.zkey";
const zkeyJsonName = params[1] || "circuit.zkey.json";
const zkeyName = params[0] || "circuit_final.zkey";
const zkeyJsonName = params[1] || "circuit_final.zkey.json";
if (options.verbose) Logger.setLogLevel("DEBUG");
@ -6932,13 +6943,13 @@ async function zkeyExportJson$1(params, options) {
await fs.promises.writeFile(zkeyJsonName, S);
}
// solidity genverifier [circuit.zkey] [verifier.sol]
// solidity genverifier [circuit_final.zkey] [verifier.sol]
async function zkeyExportSolidityVerifier(params, options) {
let zkeyName;
let verifierName;
if (params.length < 1) {
zkeyName = "circuit.zkey";
zkeyName = "circuit_final.zkey";
} else {
zkeyName = params[0];
}
@ -7209,7 +7220,7 @@ async function powersOfTauExportJson(params, options) {
}
// phase2 new <circuit.r1cs> <powersoftau.ptau> <circuit.zkey>
// phase2 new <circuit.r1cs> <powersoftau.ptau> <circuit_0000.zkey>
async function zkeyNew(params, options) {
let r1csName;
let ptauName;
@ -7228,7 +7239,7 @@ async function zkeyNew(params, options) {
}
if (params.length < 3) {
zkeyName = "circuit.zkey";
zkeyName = "circuit_0000.zkey";
} else {
zkeyName = params[2];
}
@ -7238,16 +7249,12 @@ async function zkeyNew(params, options) {
return newZKey(r1csName, ptauName, zkeyName, logger);
}
// zkey export bellman [circuit.zkey] [circuit.mpcparams]
// zkey export bellman [circuit_0000.zkey] [circuit.mpcparams]
async function zkeyExportBellman(params, options) {
let zkeyName;
let mpcparamsName;
if (params.length < 1) {
zkeyName = "circuit.zkey";
} else {
zkeyName = params[0];
}
zkeyName = params[0];
if (params.length < 2) {
mpcparamsName = "circuit.mpcparams";
@ -7277,8 +7284,8 @@ async function zkeyImportBellman(params, options) {
return phase2importMPCParams(zkeyNameOld, mpcParamsName, zkeyNameNew, options.name, logger);
}
// phase2 verify [circuit.r1cs] [powersoftau.ptau] [circuit.zkey]
async function zkeyVerify(params, options) {
// phase2 verify r1cs [circuit.r1cs] [powersoftau.ptau] [circuit_final.zkey]
async function zkeyVerifyFromR1cs(params, options) {
let r1csName;
let ptauName;
let zkeyName;
@ -7296,14 +7303,14 @@ async function zkeyVerify(params, options) {
}
if (params.length < 3) {
zkeyName = "circuit.zkey";
zkeyName = "circuit_final.zkey";
} else {
zkeyName = params[2];
}
if (options.verbose) Logger.setLogLevel("DEBUG");
const res = await phase2verify(r1csName, ptauName, zkeyName, logger);
const res = await phase2verifyFromR1cs(r1csName, ptauName, zkeyName, logger);
if (res === true) {
return 0;
} else {
@ -7312,6 +7319,39 @@ async function zkeyVerify(params, options) {
}
// phase2 verify [circuit_0000] [powersoftau.ptau] [circuit_final.zkey]
async function zkeyVerifyFromInit(params, options) {
let initZKeyName;
let ptauName;
let zkeyName;
if (params.length < 1) {
initZKeyName = "circuit_0000.zkey";
} else {
initZKeyName = params[0];
}
if (params.length < 2) {
ptauName = "powersoftau.ptau";
} else {
ptauName = params[1];
}
if (params.length < 3) {
zkeyName = "circuit_final.zkey";
} else {
zkeyName = params[2];
}
if (options.verbose) Logger.setLogLevel("DEBUG");
const res = await phase2verifyFromInit(initZKeyName, zkeyName, logger);
if (res === true) {
return 0;
} else {
return 1;
}
}
// zkey contribute <circuit_old.zkey> <circuit_new.zkey>
async function zkeyContribute(params, options) {

@ -5383,7 +5383,7 @@ const sameRatio$2 = sameRatio;
async function phase2verify(r1csFileName, pTauFileName, zkeyFileName, logger) {
async function phase2verifyFromInit(initFileName, pTauFileName, zkeyFileName, logger) {
let sr;
await Blake2b.ready();
@ -5452,11 +5452,6 @@ async function phase2verify(r1csFileName, pTauFileName, zkeyFileName, logger) {
}
// const initFileName = "~" + zkeyFileName + ".init";
const initFileName = {type: "bigMem"};
await newZKey(r1csFileName, pTauFileName, initFileName, logger);
const {fd: fdInit, sections: sectionsInit} = await readBinFile(initFileName, "zkey", 2);
const zkeyInit = await readHeader(fdInit, sectionsInit, "groth16");
@ -5773,6 +5768,15 @@ async function phase2verify(r1csFileName, pTauFileName, zkeyFileName, logger) {
}
async function phase2verifyFromR1cs(r1csFileName, pTauFileName, zkeyFileName, logger) {
// const initFileName = "~" + zkeyFileName + ".init";
const initFileName = {type: "bigMem"};
await newZKey(r1csFileName, pTauFileName, initFileName, logger);
return await phase2verifyFromInit(initFileName, pTauFileName, zkeyFileName);
}
async function phase2contribute(zkeyNameOld, zkeyNameNew, name, entropy, logger) {
await Blake2b.ready();
@ -6217,7 +6221,8 @@ var zkey = /*#__PURE__*/Object.freeze({
newZKey: newZKey,
exportBellman: phase2exportMPCParams,
importBellman: phase2importMPCParams,
verify: phase2verify,
verifyFromR1cs: phase2verifyFromR1cs,
verifyFromInit: phase2verifyFromInit,
contribute: phase2contribute,
beacon: beacon$1,
exportJson: zkeyExportJson,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

98
cli.js

@ -167,7 +167,7 @@ const commands = [
action: wtnsExportJson
},
{
cmd: "zkey new [circuit.r1cs] [powersoftau.ptau] [circuit.zkey]",
cmd: "zkey new [circuit.r1cs] [powersoftau.ptau] [circuit_0000.zkey]",
description: "Creates an initial pkey file with zero contributions ",
alias: ["zkn"],
options: "-verbose|v",
@ -181,7 +181,7 @@ const commands = [
action: zkeyContribute
},
{
cmd: "zkey export bellman [circuit.zkey] [circuit.mpcparams]",
cmd: "zkey export bellman <circuit_xxxx.zkey> [circuit.mpcparams]",
description: "Export a zKey to a MPCParameters file compatible with kobi/phase2 (Bellman)",
alias: ["zkeb"],
options: "-verbose|v",
@ -209,27 +209,34 @@ const commands = [
action: zkeyBeacon
},
{
cmd: "zkey verify [circuit.r1cs] [powersoftau.ptau] [circuit.zkey]",
cmd: "zkey verify r1cs [circuit.r1cs] [powersoftau.ptau] [circuit_final.zkey]",
description: "Verify zkey file contributions and verify that matches with the original circuit.r1cs and ptau",
alias: ["zkv"],
alias: ["zkv", "zkvr", "zkey verify"],
options: "-verbose|v",
action: zkeyVerify
action: zkeyVerifyFromR1cs
},
{
cmd: "zkey export verificationkey [circuit.zkey] [verification_key.json]",
cmd: "zkey verify init [circuit_0000.zkey] [powersoftau.ptau] [circuit_final.zkey]",
description: "Verify zkey file contributions and verify that matches with the original circuit.r1cs and ptau",
alias: ["zkvi"],
options: "-verbose|v",
action: zkeyVerifyFromInit
},
{
cmd: "zkey export verificationkey [circuit_final.zkey] [verification_key.json]",
description: "Exports a verification key",
alias: ["zkev"],
action: zkeyExportVKey
},
{
cmd: "zkey export json [circuit.zkey] [circuit.zkey.json]",
cmd: "zkey export json [circuit_final.zkey] [circuit_final.zkey.json]",
description: "Exports a circuit key to a JSON file",
alias: ["zkej"],
options: "-verbose|v",
action: zkeyExportJson
},
{
cmd: "zkey export solidityverifier [circuit.zkey] [verifier.sol]",
cmd: "zkey export solidityverifier [circuit_final.zkey] [verifier.sol]",
description: "Creates a verifier in solidity",
alias: ["zkesv", "generateverifier -vk|verificationkey -v|verifier"],
action: zkeyExportSolidityVerifier
@ -241,14 +248,14 @@ const commands = [
action: zkeyExportSolidityCalldata
},
{
cmd: "groth16 prove [circuit.zkey] [witness.wtns] [proof.json] [public.json]",
cmd: "groth16 prove [circuit_final.zkey] [witness.wtns] [proof.json] [public.json]",
description: "Generates a zk Proof from witness",
alias: ["g16p", "zpw", "zksnark proof", "proof -pk|provingkey -wt|witness -p|proof -pub|public"],
options: "-verbose|v -protocol",
action: groth16Prove
},
{
cmd: "groth16 fullprove [input.json] [circuit.wasm] [circuit.zkey] [proof.json] [public.json]",
cmd: "groth16 fullprove [input.json] [circuit_final.wasm] [circuit_final.zkey] [proof.json] [public.json]",
description: "Generates a zk Proof from input",
alias: ["g16f", "g16i"],
options: "-verbose|v -protocol",
@ -434,7 +441,7 @@ async function zksnarkSetup(params, options) {
// groth16 prove [circuit.zkey] [witness.wtns] [proof.json] [public.json]
async function groth16Prove(params, options) {
const zkeyName = params[0] || "circuit.zkey";
const zkeyName = params[0] || "circuit_final.zkey";
const witnessName = params[1] || "witness.wtns";
const proofName = params[2] || "proof.json";
const publicName = params[3] || "public.json";
@ -454,7 +461,7 @@ async function groth16FullProve(params, options) {
const inputName = params[0] || "input.json";
const wasmName = params[1] || "circuit.wasm";
const zkeyName = params[2] || "circuit.zkey";
const zkeyName = params[2] || "circuit_final.zkey";
const proofName = params[3] || "proof.json";
const publicName = params[4] || "public.json";
@ -492,9 +499,9 @@ async function groth16Verify(params, options) {
}
}
// zkey export vkey [circuit.zkey] [verification_key.json]",
// zkey export vkey [circuit_final.zkey] [verification_key.json]",
async function zkeyExportVKey(params, options) {
const zkeyName = params[0] || "circuit.zkey";
const zkeyName = params[0] || "circuit_final.zkey";
const verificationKeyName = params[1] || "verification_key.json";
if (options.verbose) Logger.setLogLevel("DEBUG");
@ -505,10 +512,10 @@ async function zkeyExportVKey(params, options) {
await fs.promises.writeFile(verificationKeyName, S);
}
// zkey export json [circuit.zkey] [circuit.zkey.json]",
// zkey export json [circuit_final.zkey] [circuit.zkey.json]",
async function zkeyExportJson(params, options) {
const zkeyName = params[0] || "circuit.zkey";
const zkeyJsonName = params[1] || "circuit.zkey.json";
const zkeyName = params[0] || "circuit_final.zkey";
const zkeyJsonName = params[1] || "circuit_final.zkey.json";
if (options.verbose) Logger.setLogLevel("DEBUG");
@ -518,13 +525,13 @@ async function zkeyExportJson(params, options) {
await fs.promises.writeFile(zkeyJsonName, S);
}
// solidity genverifier [circuit.zkey] [verifier.sol]
// solidity genverifier [circuit_final.zkey] [verifier.sol]
async function zkeyExportSolidityVerifier(params, options) {
let zkeyName;
let verifierName;
if (params.length < 1) {
zkeyName = "circuit.zkey";
zkeyName = "circuit_final.zkey";
} else {
zkeyName = params[0];
}
@ -795,7 +802,7 @@ async function powersOfTauExportJson(params, options) {
}
// phase2 new <circuit.r1cs> <powersoftau.ptau> <circuit.zkey>
// phase2 new <circuit.r1cs> <powersoftau.ptau> <circuit_0000.zkey>
async function zkeyNew(params, options) {
let r1csName;
let ptauName;
@ -814,7 +821,7 @@ async function zkeyNew(params, options) {
}
if (params.length < 3) {
zkeyName = "circuit.zkey";
zkeyName = "circuit_0000.zkey";
} else {
zkeyName = params[2];
}
@ -824,16 +831,12 @@ async function zkeyNew(params, options) {
return zkey.newZKey(r1csName, ptauName, zkeyName, logger);
}
// zkey export bellman [circuit.zkey] [circuit.mpcparams]
// zkey export bellman [circuit_0000.zkey] [circuit.mpcparams]
async function zkeyExportBellman(params, options) {
let zkeyName;
let mpcparamsName;
if (params.length < 1) {
zkeyName = "circuit.zkey";
} else {
zkeyName = params[0];
}
zkeyName = params[0];
if (params.length < 2) {
mpcparamsName = "circuit.mpcparams";
@ -863,8 +866,8 @@ async function zkeyImportBellman(params, options) {
return zkey.importBellman(zkeyNameOld, mpcParamsName, zkeyNameNew, options.name, logger);
}
// phase2 verify [circuit.r1cs] [powersoftau.ptau] [circuit.zkey]
async function zkeyVerify(params, options) {
// phase2 verify r1cs [circuit.r1cs] [powersoftau.ptau] [circuit_final.zkey]
async function zkeyVerifyFromR1cs(params, options) {
let r1csName;
let ptauName;
let zkeyName;
@ -882,14 +885,14 @@ async function zkeyVerify(params, options) {
}
if (params.length < 3) {
zkeyName = "circuit.zkey";
zkeyName = "circuit_final.zkey";
} else {
zkeyName = params[2];
}
if (options.verbose) Logger.setLogLevel("DEBUG");
const res = await zkey.verify(r1csName, ptauName, zkeyName, logger);
const res = await zkey.verifyFromR1cs(r1csName, ptauName, zkeyName, logger);
if (res === true) {
return 0;
} else {
@ -898,6 +901,39 @@ async function zkeyVerify(params, options) {
}
// phase2 verify [circuit_0000] [powersoftau.ptau] [circuit_final.zkey]
async function zkeyVerifyFromInit(params, options) {
let initZKeyName;
let ptauName;
let zkeyName;
if (params.length < 1) {
initZKeyName = "circuit_0000.zkey";
} else {
initZKeyName = params[0];
}
if (params.length < 2) {
ptauName = "powersoftau.ptau";
} else {
ptauName = params[1];
}
if (params.length < 3) {
zkeyName = "circuit_final.zkey";
} else {
zkeyName = params[2];
}
if (options.verbose) Logger.setLogLevel("DEBUG");
const res = await zkey.verifyFromInit(initZKeyName, zkeyName, logger);
if (res === true) {
return 0;
} else {
return 1;
}
}
// zkey contribute <circuit_old.zkey> <circuit_new.zkey>
async function zkeyContribute(params, options) {

@ -1,7 +1,8 @@
export {default as newZKey} from "./zkey_new.js";
export {default as exportBellman} from "./zkey_export_bellman.js";
export {default as importBellman} from "./zkey_import_bellman.js";
export {default as verify} from "./zkey_verify.js";
export {default as verifyFromR1cs} from "./zkey_verify_fromr1cs.js";
export {default as verifyFromInit} from "./zkey_verify_frominit.js";
export {default as contribute} from "./zkey_contribute.js";
export {default as beacon} from "./zkey_beacon.js";
export {default as exportJson} from "./zkey_export_json.js";

@ -12,7 +12,7 @@ import { Scalar, ChaCha, BigBuffer } from "ffjavascript";
export default async function phase2verify(r1csFileName, pTauFileName, zkeyFileName, logger) {
export default async function phase2verifyFromInit(initFileName, pTauFileName, zkeyFileName, logger) {
let sr;
await Blake2b.ready();
@ -81,11 +81,6 @@ export default async function phase2verify(r1csFileName, pTauFileName, zkeyFileN
}
// const initFileName = "~" + zkeyFileName + ".init";
const initFileName = {type: "bigMem"};
await newZKey(r1csFileName, pTauFileName, initFileName, logger);
const {fd: fdInit, sections: sectionsInit} = await binFileUtils.readBinFile(initFileName, "zkey", 2);
const zkeyInit = await zkeyUtils.readHeader(fdInit, sectionsInit, "groth16");

@ -0,0 +1,12 @@
import newZKey from "./zkey_new.js";
import phase2verifyFromInit from "./zkey_verify_frominit.js";
export default async function phase2verifyFromR1cs(r1csFileName, pTauFileName, zkeyFileName, logger) {
// const initFileName = "~" + zkeyFileName + ".init";
const initFileName = {type: "bigMem"};
await newZKey(r1csFileName, pTauFileName, initFileName, logger);
return await phase2verifyFromInit(initFileName, pTauFileName, zkeyFileName);
}

@ -92,8 +92,13 @@ describe("Full process", function () {
await snarkjs.zKey.beacon(zkey_2, zkey_final, "B3", "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20", 10);
});
it ("zkey verify", async () => {
const res = await snarkjs.zKey.verify(path.join("test", "circuit", "circuit.r1cs"), ptau_final, zkey_final);
it ("zkey verify r1cs", async () => {
const res = await snarkjs.zKey.verifyFromR1cs(path.join("test", "circuit", "circuit.r1cs"), ptau_final, zkey_final);
assert(res);
});
it ("zkey verify init", async () => {
const res = await snarkjs.zKey.verifyFromInit(zkey_0, ptau_final, zkey_final);
assert(res);
});