Groth protocol imlemented
This commit is contained in:
parent
bd48938152
commit
448dc345ad
@ -1,6 +1,7 @@
|
||||
# snarkjs: JavaScript implementation of zkSNARKs.
|
||||
|
||||
This is a JavaScript implementation of zkSNARK schemes.
|
||||
This is a JavaScript implementation of zkSNARK schemes. It allows the original 8points protocol
|
||||
and the Groth Protocol (3 point only and 3 pairings)
|
||||
|
||||
This library allows to do the trusted setup, generate proofs and verify the proofs.
|
||||
|
||||
|
210
cli.js
210
cli.js
@ -59,6 +59,12 @@ setup command
|
||||
|
||||
Default: verification_key.json
|
||||
|
||||
--protocol [original|groth]
|
||||
|
||||
Defines withc variant of snark you want to use
|
||||
|
||||
Default: original
|
||||
|
||||
calculate witness command
|
||||
=========================
|
||||
|
||||
@ -144,6 +150,7 @@ verify command
|
||||
|
||||
Default: public.json
|
||||
|
||||
|
||||
generate solidity verifier command
|
||||
==================================
|
||||
|
||||
@ -163,6 +170,7 @@ generate solidity verifier command
|
||||
|
||||
Default: verifier.sol
|
||||
|
||||
|
||||
generate call parameters
|
||||
========================
|
||||
|
||||
@ -233,7 +241,8 @@ const inputName = (argv.input) ? argv.input : "input.json";
|
||||
const witnessName = (argv.witness) ? argv.witness : "witness.json";
|
||||
const proofName = (argv.proof) ? argv.proof : "proof.json";
|
||||
const publicName = (argv.public) ? argv.public : "public.json";
|
||||
const verifierName = (argv.public) ? argv.public : "verifier.sol";
|
||||
const verifierName = (argv.verifier) ? argv.verifier : "verifier.sol";
|
||||
const protocol = (argv.protocol) ? argv.protocol : "original";
|
||||
|
||||
function p256(n) {
|
||||
let nstr = n.toString(16);
|
||||
@ -262,7 +271,9 @@ try {
|
||||
} else if (argv._[0].toUpperCase() == "SETUP") {
|
||||
const cirDef = JSON.parse(fs.readFileSync(circuitName, "utf8"));
|
||||
const cir = new zkSnark.Circuit(cirDef);
|
||||
const setup = zkSnark.setup(cir);
|
||||
|
||||
if (!zkSnark[protocol]) throw new Error("Invalid protocol");
|
||||
const setup = zkSnark[protocol].setup(cir);
|
||||
|
||||
fs.writeFileSync(provingKeyName, JSON.stringify(stringifyBigInts(setup.vk_proof), null, 1), "utf-8");
|
||||
fs.writeFileSync(verificationKeyName, JSON.stringify(stringifyBigInts(setup.vk_verifier), null, 1), "utf-8");
|
||||
@ -280,7 +291,9 @@ try {
|
||||
const witness = unstringifyBigInts(JSON.parse(fs.readFileSync(witnessName, "utf8")));
|
||||
const provingKey = unstringifyBigInts(JSON.parse(fs.readFileSync(provingKeyName, "utf8")));
|
||||
|
||||
const {proof, publicSignals} = zkSnark.genProof(provingKey, witness);
|
||||
const protocol = provingKey.protocol;
|
||||
if (!zkSnark[protocol]) throw new Error("Invalid protocol");
|
||||
const {proof, publicSignals} = zkSnark[protocol].genProof(provingKey, witness);
|
||||
|
||||
fs.writeFileSync(proofName, JSON.stringify(stringifyBigInts(proof), null, 1), "utf-8");
|
||||
fs.writeFileSync(publicName, JSON.stringify(stringifyBigInts(publicSignals), null, 1), "utf-8");
|
||||
@ -289,7 +302,11 @@ try {
|
||||
const public = unstringifyBigInts(JSON.parse(fs.readFileSync(publicName, "utf8")));
|
||||
const verificationKey = unstringifyBigInts(JSON.parse(fs.readFileSync(verificationKeyName, "utf8")));
|
||||
const proof = unstringifyBigInts(JSON.parse(fs.readFileSync(proofName, "utf8")));
|
||||
const isValid = zkSnark.isValid(verificationKey, proof, public);
|
||||
|
||||
const protocol = verificationKey.protocol;
|
||||
if (!zkSnark[protocol]) throw new Error("Invalid protocol");
|
||||
|
||||
const isValid = zkSnark[protocol].isValid(verificationKey, proof, public);
|
||||
|
||||
if (isValid) {
|
||||
console.log("OK");
|
||||
@ -301,60 +318,17 @@ try {
|
||||
} else if (argv._[0].toUpperCase() == "GENERATEVERIFIER") {
|
||||
|
||||
const verificationKey = unstringifyBigInts(JSON.parse(fs.readFileSync(verificationKeyName, "utf8")));
|
||||
let template = fs.readFileSync(path.join( __dirname, "templates", "verifier.sol"), "utf-8");
|
||||
|
||||
const vka_str = `[${verificationKey.vk_a[0][1].toString()},`+
|
||||
`${verificationKey.vk_a[0][0].toString()}], `+
|
||||
`[${verificationKey.vk_a[1][1].toString()},` +
|
||||
`${verificationKey.vk_a[1][0].toString()}]`;
|
||||
template = template.replace("<%vk_a%>", vka_str);
|
||||
|
||||
const vkb_str = `${verificationKey.vk_b[0].toString()},`+
|
||||
`${verificationKey.vk_b[1].toString()}`;
|
||||
template = template.replace("<%vk_b%>", vkb_str);
|
||||
|
||||
const vkc_str = `[${verificationKey.vk_c[0][1].toString()},`+
|
||||
`${verificationKey.vk_c[0][0].toString()}], `+
|
||||
`[${verificationKey.vk_c[1][1].toString()},` +
|
||||
`${verificationKey.vk_c[1][0].toString()}]`;
|
||||
template = template.replace("<%vk_c%>", vkc_str);
|
||||
|
||||
const vkg_str = `[${verificationKey.vk_g[0][1].toString()},`+
|
||||
`${verificationKey.vk_g[0][0].toString()}], `+
|
||||
`[${verificationKey.vk_g[1][1].toString()},` +
|
||||
`${verificationKey.vk_g[1][0].toString()}]`;
|
||||
template = template.replace("<%vk_g%>", vkg_str);
|
||||
|
||||
const vkgb1_str = `${verificationKey.vk_gb_1[0].toString()},`+
|
||||
`${verificationKey.vk_gb_1[1].toString()}`;
|
||||
template = template.replace("<%vk_gb1%>", vkgb1_str);
|
||||
|
||||
const vkgb2_str = `[${verificationKey.vk_gb_2[0][1].toString()},`+
|
||||
`${verificationKey.vk_gb_2[0][0].toString()}], `+
|
||||
`[${verificationKey.vk_gb_2[1][1].toString()},` +
|
||||
`${verificationKey.vk_gb_2[1][0].toString()}]`;
|
||||
template = template.replace("<%vk_gb2%>", vkgb2_str);
|
||||
|
||||
const vkz_str = `[${verificationKey.vk_z[0][1].toString()},`+
|
||||
`${verificationKey.vk_z[0][0].toString()}], `+
|
||||
`[${verificationKey.vk_z[1][1].toString()},` +
|
||||
`${verificationKey.vk_z[1][0].toString()}]`;
|
||||
template = template.replace("<%vk_z%>", vkz_str);
|
||||
|
||||
// The points
|
||||
|
||||
template = template.replace("<%vk_input_length%>", (verificationKey.A.length-1).toString());
|
||||
template = template.replace("<%vk_ic_length%>", verificationKey.A.length.toString());
|
||||
let vi = "";
|
||||
for (let i=0; i<verificationKey.A.length; i++) {
|
||||
if (vi != "") vi = vi + " ";
|
||||
vi = vi + `vk.IC[${i}] = Pairing.G1Point(${verificationKey.A[i][0].toString()},`+
|
||||
`${verificationKey.A[i][1].toString()});\n`;
|
||||
let verifierCode;
|
||||
if (verificationKey.protocol == "original") {
|
||||
verifierCode = generateVerifier_original(verificationKey);
|
||||
} else if (verificationKey.protocol == "groth") {
|
||||
verifierCode = generateVerifier_groth(verificationKey);
|
||||
} else {
|
||||
throw new Error("InvalidProof");
|
||||
}
|
||||
template = template.replace("<%vk_ic_pts%>", vi);
|
||||
|
||||
|
||||
fs.writeFileSync(verifierName, template, "utf-8");
|
||||
fs.writeFileSync(verifierName, verifierCode, "utf-8");
|
||||
process.exit(0);
|
||||
|
||||
} else if (argv._[0].toUpperCase() == "GENERATECALL") {
|
||||
@ -368,15 +342,25 @@ try {
|
||||
inputs = inputs + p256(public[i]);
|
||||
}
|
||||
|
||||
const S=`[${p256(proof.pi_a[0])}, ${p256(proof.pi_a[1])}],` +
|
||||
`[${p256(proof.pi_ap[0])}, ${p256(proof.pi_ap[1])}],` +
|
||||
`[[${p256(proof.pi_b[0][1])}, ${p256(proof.pi_b[0][0])}],[${p256(proof.pi_b[1][1])}, ${p256(proof.pi_b[1][0])}]],` +
|
||||
`[${p256(proof.pi_bp[0])}, ${p256(proof.pi_bp[1])}],` +
|
||||
`[${p256(proof.pi_c[0])}, ${p256(proof.pi_c[1])}],` +
|
||||
`[${p256(proof.pi_cp[0])}, ${p256(proof.pi_cp[1])}],` +
|
||||
`[${p256(proof.pi_h[0])}, ${p256(proof.pi_h[1])}],` +
|
||||
`[${p256(proof.pi_kp[0])}, ${p256(proof.pi_kp[1])}],` +
|
||||
`[${inputs}]` ;
|
||||
let S;
|
||||
if ((typeof proof.protocol === "undefined") || (proof.protocol == "original")) {
|
||||
S=`[${p256(proof.pi_a[0])}, ${p256(proof.pi_a[1])}],` +
|
||||
`[${p256(proof.pi_ap[0])}, ${p256(proof.pi_ap[1])}],` +
|
||||
`[[${p256(proof.pi_b[0][1])}, ${p256(proof.pi_b[0][0])}],[${p256(proof.pi_b[1][1])}, ${p256(proof.pi_b[1][0])}]],` +
|
||||
`[${p256(proof.pi_bp[0])}, ${p256(proof.pi_bp[1])}],` +
|
||||
`[${p256(proof.pi_c[0])}, ${p256(proof.pi_c[1])}],` +
|
||||
`[${p256(proof.pi_cp[0])}, ${p256(proof.pi_cp[1])}],` +
|
||||
`[${p256(proof.pi_h[0])}, ${p256(proof.pi_h[1])}],` +
|
||||
`[${p256(proof.pi_kp[0])}, ${p256(proof.pi_kp[1])}],` +
|
||||
`[${inputs}]`;
|
||||
} else if (proof.protocol == "groth") {
|
||||
S=`[${p256(proof.pi_a[0])}, ${p256(proof.pi_a[1])}],` +
|
||||
`[[${p256(proof.pi_b[0][1])}, ${p256(proof.pi_b[0][0])}],[${p256(proof.pi_b[1][1])}, ${p256(proof.pi_b[1][0])}]],` +
|
||||
`[${p256(proof.pi_c[0])}, ${p256(proof.pi_c[1])}],` +
|
||||
`[${inputs}]`;
|
||||
} else {
|
||||
throw new Error("InvalidProof");
|
||||
}
|
||||
|
||||
console.log(S);
|
||||
process.exit(0);
|
||||
@ -384,11 +368,109 @@ try {
|
||||
throw new Error("Invalid Command");
|
||||
}
|
||||
} catch(err) {
|
||||
console.log(err.stack);
|
||||
console.log("ERROR: " + err);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
|
||||
function generateVerifier_original(verificationKey) {
|
||||
let template = fs.readFileSync(path.join( __dirname, "templates", "verifier_original.sol"), "utf-8");
|
||||
|
||||
const vka_str = `[${verificationKey.vk_a[0][1].toString()},`+
|
||||
`${verificationKey.vk_a[0][0].toString()}], `+
|
||||
`[${verificationKey.vk_a[1][1].toString()},` +
|
||||
`${verificationKey.vk_a[1][0].toString()}]`;
|
||||
template = template.replace("<%vk_a%>", vka_str);
|
||||
|
||||
const vkb_str = `${verificationKey.vk_b[0].toString()},`+
|
||||
`${verificationKey.vk_b[1].toString()}`;
|
||||
template = template.replace("<%vk_b%>", vkb_str);
|
||||
|
||||
const vkc_str = `[${verificationKey.vk_c[0][1].toString()},`+
|
||||
`${verificationKey.vk_c[0][0].toString()}], `+
|
||||
`[${verificationKey.vk_c[1][1].toString()},` +
|
||||
`${verificationKey.vk_c[1][0].toString()}]`;
|
||||
template = template.replace("<%vk_c%>", vkc_str);
|
||||
|
||||
const vkg_str = `[${verificationKey.vk_g[0][1].toString()},`+
|
||||
`${verificationKey.vk_g[0][0].toString()}], `+
|
||||
`[${verificationKey.vk_g[1][1].toString()},` +
|
||||
`${verificationKey.vk_g[1][0].toString()}]`;
|
||||
template = template.replace("<%vk_g%>", vkg_str);
|
||||
|
||||
const vkgb1_str = `${verificationKey.vk_gb_1[0].toString()},`+
|
||||
`${verificationKey.vk_gb_1[1].toString()}`;
|
||||
template = template.replace("<%vk_gb1%>", vkgb1_str);
|
||||
|
||||
const vkgb2_str = `[${verificationKey.vk_gb_2[0][1].toString()},`+
|
||||
`${verificationKey.vk_gb_2[0][0].toString()}], `+
|
||||
`[${verificationKey.vk_gb_2[1][1].toString()},` +
|
||||
`${verificationKey.vk_gb_2[1][0].toString()}]`;
|
||||
template = template.replace("<%vk_gb2%>", vkgb2_str);
|
||||
|
||||
const vkz_str = `[${verificationKey.vk_z[0][1].toString()},`+
|
||||
`${verificationKey.vk_z[0][0].toString()}], `+
|
||||
`[${verificationKey.vk_z[1][1].toString()},` +
|
||||
`${verificationKey.vk_z[1][0].toString()}]`;
|
||||
template = template.replace("<%vk_z%>", vkz_str);
|
||||
|
||||
// The points
|
||||
|
||||
template = template.replace("<%vk_input_length%>", (verificationKey.IC.length-1).toString());
|
||||
template = template.replace("<%vk_ic_length%>", verificationKey.IC.length.toString());
|
||||
let vi = "";
|
||||
for (let i=0; i<verificationKey.IC.length; i++) {
|
||||
if (vi != "") vi = vi + " ";
|
||||
vi = vi + `vk.IC[${i}] = Pairing.G1Point(${verificationKey.IC[i][0].toString()},`+
|
||||
`${verificationKey.IC[i][1].toString()});\n`;
|
||||
}
|
||||
template = template.replace("<%vk_ic_pts%>", vi);
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
|
||||
function generateVerifier_groth(verificationKey) {
|
||||
let template = fs.readFileSync(path.join( __dirname, "templates", "verifier_groth.sol"), "utf-8");
|
||||
|
||||
|
||||
const vkalfa1_str = `${verificationKey.vk_alfa_1[0].toString()},`+
|
||||
`${verificationKey.vk_alfa_1[1].toString()}`;
|
||||
template = template.replace("<%vk_alfa1%>", vkalfa1_str);
|
||||
|
||||
const vkbeta2_str = `[${verificationKey.vk_beta_2[0][1].toString()},`+
|
||||
`${verificationKey.vk_beta_2[0][0].toString()}], `+
|
||||
`[${verificationKey.vk_beta_2[1][1].toString()},` +
|
||||
`${verificationKey.vk_beta_2[1][0].toString()}]`;
|
||||
template = template.replace("<%vk_beta2%>", vkbeta2_str);
|
||||
|
||||
const vkgamma2_str = `[${verificationKey.vk_gamma_2[0][1].toString()},`+
|
||||
`${verificationKey.vk_gamma_2[0][0].toString()}], `+
|
||||
`[${verificationKey.vk_gamma_2[1][1].toString()},` +
|
||||
`${verificationKey.vk_gamma_2[1][0].toString()}]`;
|
||||
template = template.replace("<%vk_gamma2%>", vkgamma2_str);
|
||||
|
||||
const vkdelta2_str = `[${verificationKey.vk_delta_2[0][1].toString()},`+
|
||||
`${verificationKey.vk_delta_2[0][0].toString()}], `+
|
||||
`[${verificationKey.vk_delta_2[1][1].toString()},` +
|
||||
`${verificationKey.vk_delta_2[1][0].toString()}]`;
|
||||
template = template.replace("<%vk_delta2%>", vkdelta2_str);
|
||||
|
||||
// The points
|
||||
|
||||
template = template.replace("<%vk_input_length%>", (verificationKey.IC.length-1).toString());
|
||||
template = template.replace("<%vk_ic_length%>", verificationKey.IC.length.toString());
|
||||
let vi = "";
|
||||
for (let i=0; i<verificationKey.IC.length; i++) {
|
||||
if (vi != "") vi = vi + " ";
|
||||
vi = vi + `vk.IC[${i}] = Pairing.G1Point(${verificationKey.IC[i][0].toString()},`+
|
||||
`${verificationKey.IC[i][1].toString()});\n`;
|
||||
}
|
||||
template = template.replace("<%vk_ic_pts%>", vi);
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
13
index.js
13
index.js
@ -18,7 +18,14 @@
|
||||
*/
|
||||
|
||||
exports.Circuit = require("./src/circuit.js");
|
||||
exports.setup = require("./src/setup.js");
|
||||
exports.genProof = require("./src/prover.js");
|
||||
exports.isValid = require("./src/verifier.js");
|
||||
exports.original = {
|
||||
setup: require("./src/setup_original.js"),
|
||||
genProof: require("./src/prover_original.js"),
|
||||
isValid: require("./src/verifier_original.js")
|
||||
};
|
||||
exports.groth = {
|
||||
setup: require("./src/setup_groth.js"),
|
||||
genProof: require("./src/prover_groth.js"),
|
||||
isValid: require("./src/verifier_groth.js")
|
||||
};
|
||||
exports.bigInt = require("./src/bigint.js");
|
||||
|
162
src/prover_groth.js
Normal file
162
src/prover_groth.js
Normal file
@ -0,0 +1,162 @@
|
||||
/*
|
||||
Copyright 2018 0kims association.
|
||||
|
||||
This file is part of snarkjs.
|
||||
|
||||
snarkjs is a free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as published by the
|
||||
Free Software Foundation, either version 3 of the License, or (at your option)
|
||||
any later version.
|
||||
|
||||
snarkjs is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
snarkjs. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/* Implementation of this paper: https://eprint.iacr.org/2016/260.pdf */
|
||||
|
||||
const BN128 = require("./bn128.js");
|
||||
const PolField = require("./polfield.js");
|
||||
const ZqField = require("./zqfield.js");
|
||||
|
||||
const bn128 = new BN128();
|
||||
const PolF = new PolField(new ZqField(bn128.r));
|
||||
const G1 = bn128.G1;
|
||||
const G2 = bn128.G2;
|
||||
|
||||
module.exports = function genProof(vk_proof, witness) {
|
||||
|
||||
const proof = {};
|
||||
|
||||
const r = PolF.F.random();
|
||||
const s = PolF.F.random();
|
||||
|
||||
proof.pi_a = G1.zero;
|
||||
proof.pi_b = G2.zero;
|
||||
proof.pi_c = G1.zero;
|
||||
|
||||
let pib1 = G1.zero;
|
||||
|
||||
|
||||
// Skip public entries and the "1" signal that are forced by the verifier
|
||||
|
||||
for (let s= 0; s< vk_proof.nVars; s++) {
|
||||
// pi_a = pi_a + A[s] * witness[s];
|
||||
proof.pi_a = G1.add( proof.pi_a, G1.mulScalar( vk_proof.A[s], witness[s]));
|
||||
|
||||
// pi_b = pi_b + B[s] * witness[s];
|
||||
proof.pi_b = G2.add( proof.pi_b, G2.mulScalar( vk_proof.B2[s], witness[s]));
|
||||
|
||||
pib1 = G1.add( pib1, G1.mulScalar( vk_proof.B1[s], witness[s]));
|
||||
}
|
||||
|
||||
for (let s= vk_proof.nPublic+1; s< vk_proof.nVars; s++) {
|
||||
|
||||
// pi_a = pi_a + A[s] * witness[s];
|
||||
proof.pi_c = G1.add( proof.pi_c, G1.mulScalar( vk_proof.C[s], witness[s]));
|
||||
}
|
||||
|
||||
proof.pi_a = G1.add( proof.pi_a, vk_proof.vk_alfa_1 );
|
||||
proof.pi_a = G1.add( proof.pi_a, G1.mulScalar( vk_proof.vk_delta_1, r ));
|
||||
|
||||
proof.pi_b = G2.add( proof.pi_b, vk_proof.vk_beta_2 );
|
||||
proof.pi_b = G2.add( proof.pi_b, G2.mulScalar( vk_proof.vk_delta_2, s ));
|
||||
|
||||
pib1 = G1.add( pib1, vk_proof.vk_beta_1 );
|
||||
pib1 = G1.add( pib1, G1.mulScalar( vk_proof.vk_delta_1, s ));
|
||||
|
||||
const h = calculateH(vk_proof, witness, PolF.F.zero, PolF.F.zero, PolF.F.zero);
|
||||
|
||||
// console.log(h.length + "/" + vk_proof.hExps.length);
|
||||
|
||||
for (let i = 0; i < h.length; i++) {
|
||||
proof.pi_c = G1.add( proof.pi_c, G1.mulScalar( vk_proof.hExps[i], h[i]));
|
||||
}
|
||||
|
||||
|
||||
proof.pi_c = G1.add( proof.pi_c, G1.mulScalar( proof.pi_a, s ));
|
||||
proof.pi_c = G1.add( proof.pi_c, G1.mulScalar( pib1, r ));
|
||||
proof.pi_c = G1.add( proof.pi_c, G1.mulScalar( vk_proof.vk_delta_1, PolF.F.affine(PolF.F.neg(PolF.F.mul(r,s) ))));
|
||||
|
||||
|
||||
const publicSignals = witness.slice(1, vk_proof.nPublic+1);
|
||||
|
||||
proof.pi_a = G1.affine(proof.pi_a);
|
||||
proof.pi_b = G2.affine(proof.pi_b);
|
||||
proof.pi_c = G1.affine(proof.pi_c);
|
||||
|
||||
proof.protocol = "groth";
|
||||
|
||||
return {proof, publicSignals};
|
||||
};
|
||||
|
||||
|
||||
function calculateH(vk_proof, witness, d1, d2, d3) {
|
||||
|
||||
const F = PolF.F;
|
||||
const m = vk_proof.domainSize;
|
||||
const polA_T = new Array(m).fill(PolF.F.zero);
|
||||
const polB_T = new Array(m).fill(PolF.F.zero);
|
||||
const polC_T = new Array(m).fill(PolF.F.zero);
|
||||
|
||||
for (let s=0; s<vk_proof.nVars; s++) {
|
||||
for (let c in vk_proof.polsA[s]) {
|
||||
polA_T[c] = F.add(polA_T[c], F.mul(witness[s], vk_proof.polsA[s][c]));
|
||||
}
|
||||
for (let c in vk_proof.polsB[s]) {
|
||||
polB_T[c] = F.add(polB_T[c], F.mul(witness[s], vk_proof.polsB[s][c]));
|
||||
}
|
||||
for (let c in vk_proof.polsC[s]) {
|
||||
polC_T[c] = F.add(polC_T[c], F.mul(witness[s], vk_proof.polsC[s][c]));
|
||||
}
|
||||
}
|
||||
|
||||
const polA_S = PolF.ifft(polA_T);
|
||||
const polB_S = PolF.ifft(polB_T);
|
||||
|
||||
const polAB_S = PolF.mul(polA_S, polB_S);
|
||||
|
||||
const polC_S = PolF.ifft(polC_T);
|
||||
|
||||
const polABC_S = PolF.sub(polAB_S, polC_S);
|
||||
|
||||
const polZ_S = new Array(m+1).fill(F.zero);
|
||||
polZ_S[m] = F.one;
|
||||
polZ_S[0] = F.neg(F.one);
|
||||
|
||||
let H_S = PolF.div(polABC_S, polZ_S);
|
||||
/*
|
||||
const H2S = PolF.mul(H_S, polZ_S);
|
||||
|
||||
if (PolF.equals(H2S, polABC_S)) {
|
||||
console.log("Is Divisible!");
|
||||
} else {
|
||||
console.log("ERROR: Not divisible!");
|
||||
}
|
||||
*/
|
||||
|
||||
/* add coefficients of the polynomial (d2*A + d1*B - d3) + d1*d2*Z */
|
||||
|
||||
H_S = PolF.extend(H_S, m+1);
|
||||
|
||||
for (let i=0; i<m; i++) {
|
||||
const d2A = PolF.F.mul(d2, polA_S[i]);
|
||||
const d1B = PolF.F.mul(d1, polB_S[i]);
|
||||
H_S[i] = PolF.F.add(H_S[i], PolF.F.add(d2A, d1B));
|
||||
}
|
||||
|
||||
H_S[0] = PolF.F.sub(H_S[0], d3);
|
||||
|
||||
// Z = x^m -1
|
||||
const d1d2 = PolF.F.mul(d1, d2);
|
||||
H_S[m] = PolF.F.add(H_S[m], d1d2);
|
||||
H_S[0] = PolF.F.sub(H_S[0], d1d2);
|
||||
|
||||
H_S = PolF.reduce(PolF.affine(H_S));
|
||||
|
||||
return H_S;
|
||||
}
|
@ -135,6 +135,8 @@ module.exports = function genProof(vk_proof, witness) {
|
||||
|
||||
// proof.h=h;
|
||||
|
||||
proof.protocol = "original";
|
||||
|
||||
const publicSignals = witness.slice(1, vk_proof.nPublic+1);
|
||||
|
||||
return {proof, publicSignals};
|
212
src/setup_groth.js
Normal file
212
src/setup_groth.js
Normal file
@ -0,0 +1,212 @@
|
||||
/*
|
||||
Copyright 2018 0kims association.
|
||||
|
||||
This file is part of snarkjs.
|
||||
|
||||
snarkjs is a free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as published by the
|
||||
Free Software Foundation, either version 3 of the License, or (at your option)
|
||||
any later version.
|
||||
|
||||
snarkjs is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
snarkjs. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/* Implementation of this paper: https://eprint.iacr.org/2016/260.pdf */
|
||||
|
||||
const bigInt = require("./bigint.js");
|
||||
|
||||
const BN128 = require("./bn128.js");
|
||||
const PolField = require("./polfield.js");
|
||||
const ZqField = require("./zqfield.js");
|
||||
|
||||
const bn128 = new BN128();
|
||||
const G1 = bn128.G1;
|
||||
const G2 = bn128.G2;
|
||||
const PolF = new PolField(new ZqField(bn128.r));
|
||||
const F = new ZqField(bn128.r);
|
||||
|
||||
module.exports = function setup(circuit) {
|
||||
const setup = {
|
||||
vk_proof : {
|
||||
protocol: "groth",
|
||||
nVars: circuit.nVars,
|
||||
nPublic: circuit.nPubInputs + circuit.nOutputs
|
||||
},
|
||||
vk_verifier: {
|
||||
protocol: "groth",
|
||||
nPublic: circuit.nPubInputs + circuit.nOutputs
|
||||
},
|
||||
toxic: {}
|
||||
};
|
||||
|
||||
|
||||
setup.vk_proof.domainBits = PolF.log2(circuit.nConstraints + circuit.nPubInputs + circuit.nOutputs +1 -1) +1;
|
||||
setup.vk_proof.domainSize = 1 << setup.vk_proof.domainBits;
|
||||
|
||||
calculatePolinomials(setup, circuit);
|
||||
setup.toxic.t = F.random();
|
||||
calculateEncriptedValuesAtT(setup, circuit);
|
||||
|
||||
return setup;
|
||||
};
|
||||
|
||||
|
||||
function calculatePolinomials(setup, circuit) {
|
||||
|
||||
setup.vk_proof.polsA = new Array(circuit.nVars);
|
||||
setup.vk_proof.polsB = new Array(circuit.nVars);
|
||||
setup.vk_proof.polsC = new Array(circuit.nVars);
|
||||
for (let i=0; i<circuit.nVars; i++) {
|
||||
setup.vk_proof.polsA[i] = {};
|
||||
setup.vk_proof.polsB[i] = {};
|
||||
setup.vk_proof.polsC[i] = {};
|
||||
}
|
||||
for (let c=0; c<circuit.nConstraints; c++) {
|
||||
|
||||
for (let s in circuit.constraints[c][0]) {
|
||||
setup.vk_proof.polsA[s][c] = bigInt(circuit.constraints[c][0][s]);
|
||||
}
|
||||
for (let s in circuit.constraints[c][1]) {
|
||||
setup.vk_proof.polsB[s][c] = bigInt(circuit.constraints[c][1][s]);
|
||||
}
|
||||
for (let s in circuit.constraints[c][2]) {
|
||||
setup.vk_proof.polsC[s][c] = bigInt(circuit.constraints[c][2][s]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* add and process the constraints
|
||||
* input_i * 0 = 0
|
||||
* to ensure soundness of input consistency
|
||||
*/
|
||||
for (let i = 0; i < circuit.nPubInputs + circuit.nOutputs + 1; ++i)
|
||||
{
|
||||
setup.vk_proof.polsA[i][circuit.nConstraints + i] = F.one;
|
||||
}
|
||||
}
|
||||
|
||||
function calculateValuesAtT(setup, circuit) {
|
||||
const z_t = PolF.computeVanishingPolinomial(setup.vk_proof.domainBits, setup.toxic.t);
|
||||
const u = PolF.evaluateLagrangePolynomials(setup.vk_proof.domainBits, setup.toxic.t);
|
||||
|
||||
const a_t = new Array(circuit.nVars).fill(F.zero);
|
||||
const b_t = new Array(circuit.nVars).fill(F.zero);
|
||||
const c_t = new Array(circuit.nVars).fill(F.zero);
|
||||
|
||||
// TODO: substitute setup.polsA for coeficients
|
||||
for (let s=0; s<circuit.nVars; s++) {
|
||||
for (let c in setup.vk_proof.polsA[s]) {
|
||||
a_t[s] = F.add(a_t[s], F.mul(u[c], setup.vk_proof.polsA[s][c]));
|
||||
}
|
||||
for (let c in setup.vk_proof.polsB[s]) {
|
||||
b_t[s] = F.add(b_t[s], F.mul(u[c], setup.vk_proof.polsB[s][c]));
|
||||
}
|
||||
for (let c in setup.vk_proof.polsC[s]) {
|
||||
c_t[s] = F.add(c_t[s], F.mul(u[c], setup.vk_proof.polsC[s][c]));
|
||||
}
|
||||
}
|
||||
|
||||
return {a_t, b_t, c_t, z_t};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
function calculateEncriptedValuesAtT(setup, circuit) {
|
||||
|
||||
const v = calculateValuesAtT(setup, circuit);
|
||||
setup.vk_proof.A = new Array(circuit.nVars);
|
||||
setup.vk_proof.B1 = new Array(circuit.nVars);
|
||||
setup.vk_proof.B2 = new Array(circuit.nVars);
|
||||
setup.vk_proof.C = new Array(circuit.nVars);
|
||||
setup.vk_verifier.IC = new Array(circuit.nPublic);
|
||||
|
||||
setup.toxic.kalfa = F.random();
|
||||
setup.toxic.kbeta = F.random();
|
||||
setup.toxic.kgamma = F.random();
|
||||
setup.toxic.kdelta = F.random();
|
||||
|
||||
let invDelta = F.inverse(setup.toxic.kdelta);
|
||||
let invGamma = F.inverse(setup.toxic.kgamma);
|
||||
|
||||
setup.vk_proof.vk_alfa_1 = G1.affine(G1.mulScalar( G1.g, setup.toxic.kalfa));
|
||||
setup.vk_proof.vk_beta_1 = G1.affine(G1.mulScalar( G1.g, setup.toxic.kbeta));
|
||||
setup.vk_proof.vk_delta_1 = G1.affine(G1.mulScalar( G1.g, setup.toxic.kdelta));
|
||||
|
||||
setup.vk_proof.vk_beta_2 = G2.affine(G2.mulScalar( G2.g, setup.toxic.kbeta));
|
||||
setup.vk_proof.vk_delta_2 = G2.affine(G2.mulScalar( G2.g, setup.toxic.kdelta));
|
||||
|
||||
|
||||
setup.vk_verifier.vk_alfa_1 = G1.affine(G1.mulScalar( G1.g, setup.toxic.kalfa));
|
||||
|
||||
setup.vk_verifier.vk_beta_2 = G2.affine(G2.mulScalar( G2.g, setup.toxic.kbeta));
|
||||
setup.vk_verifier.vk_gamma_2 = G2.affine(G2.mulScalar( G2.g, setup.toxic.kgamma));
|
||||
setup.vk_verifier.vk_delta_2 = G2.affine(G2.mulScalar( G2.g, setup.toxic.kdelta));
|
||||
|
||||
setup.vk_verifier.vk_alfabeta_12 = bn128.F12.affine(bn128.pairing( setup.vk_verifier.vk_alfa_1 , setup.vk_verifier.vk_beta_2 ));
|
||||
|
||||
for (let s=0; s<circuit.nVars; s++) {
|
||||
|
||||
const A = G1.affine(G1.mulScalar(G1.g, v.a_t[s]));
|
||||
|
||||
setup.vk_proof.A[s] = A;
|
||||
|
||||
const B1 = G1.affine(G1.mulScalar(G1.g, v.b_t[s]));
|
||||
|
||||
setup.vk_proof.B1[s] = B1;
|
||||
|
||||
const B2 = G2.affine(G2.mulScalar(G2.g, v.b_t[s]));
|
||||
|
||||
setup.vk_proof.B2[s] = B2;
|
||||
}
|
||||
|
||||
for (let s=0; s<=setup.vk_proof.nPublic; s++) {
|
||||
let ps =
|
||||
F.mul(
|
||||
invGamma,
|
||||
F.add(
|
||||
F.add(
|
||||
F.mul(v.a_t[s], setup.toxic.kbeta),
|
||||
F.mul(v.b_t[s], setup.toxic.kalfa)),
|
||||
v.c_t[s]));
|
||||
|
||||
const IC = G1.affine(G1.mulScalar(G1.g, ps));
|
||||
setup.vk_verifier.IC[s]=IC;
|
||||
}
|
||||
|
||||
for (let s=setup.vk_proof.nPublic+1; s<circuit.nVars; s++) {
|
||||
let ps =
|
||||
F.mul(
|
||||
invDelta,
|
||||
F.add(
|
||||
F.add(
|
||||
F.mul(v.a_t[s], setup.toxic.kbeta),
|
||||
F.mul(v.b_t[s], setup.toxic.kalfa)),
|
||||
v.c_t[s]));
|
||||
const C = G1.affine(G1.mulScalar(G1.g, ps));
|
||||
setup.vk_proof.C[s]=C;
|
||||
}
|
||||
|
||||
// Calculate HExps
|
||||
|
||||
const maxH = setup.vk_proof.domainSize+1;
|
||||
|
||||
setup.vk_proof.hExps = new Array(maxH);
|
||||
|
||||
const zod = F.mul(invDelta, v.z_t);
|
||||
|
||||
setup.vk_proof.hExps[0] = G1.affine(G1.mulScalar(G1.g, zod));
|
||||
let eT = setup.toxic.t;
|
||||
for (let i=1; i<maxH; i++) {
|
||||
setup.vk_proof.hExps[i] = G1.affine(G1.mulScalar(G1.g, F.mul(eT, zod)));
|
||||
eT = F.mul(eT, setup.toxic.t);
|
||||
}
|
||||
}
|
||||
|
@ -32,10 +32,12 @@ const F = new ZqField(bn128.r);
|
||||
module.exports = function setup(circuit) {
|
||||
const setup = {
|
||||
vk_proof : {
|
||||
protocol: "original",
|
||||
nVars: circuit.nVars,
|
||||
nPublic: circuit.nPubInputs + circuit.nOutputs
|
||||
},
|
||||
vk_verifier: {
|
||||
protocol: "original",
|
||||
nPublic: circuit.nPubInputs + circuit.nOutputs
|
||||
},
|
||||
toxic: {}
|
||||
@ -126,7 +128,7 @@ function calculateEncriptedValuesAtT(setup, circuit) {
|
||||
setup.vk_proof.Bp = new Array(circuit.nVars+1);
|
||||
setup.vk_proof.Cp = new Array(circuit.nVars+1);
|
||||
setup.vk_proof.Kp = new Array(circuit.nVars+3);
|
||||
setup.vk_verifier.A = new Array(circuit.nPublic);
|
||||
setup.vk_verifier.IC = new Array(circuit.nPublic);
|
||||
|
||||
setup.toxic.ka = F.random();
|
||||
setup.toxic.kb = F.random();
|
||||
@ -155,7 +157,7 @@ function calculateEncriptedValuesAtT(setup, circuit) {
|
||||
setup.vk_proof.A[s] = A;
|
||||
|
||||
if (s <= setup.vk_proof.nPublic) {
|
||||
setup.vk_verifier.A[s]=A;
|
||||
setup.vk_verifier.IC[s]=A;
|
||||
}
|
||||
|
||||
|
@ -23,8 +23,6 @@ const bn128 = new BN128();
|
||||
const G1 = bn128.G1;
|
||||
const G2 = bn128.G2;
|
||||
|
||||
const pairing = bn128.pairing;
|
||||
|
||||
module.exports = function isValid(vk_verifier, proof, publicSignals) {
|
||||
|
||||
let full_pi_a = vk_verifier.A[0];
|
||||
|
46
src/verifier_groth.js
Normal file
46
src/verifier_groth.js
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
Copyright 2018 0kims association.
|
||||
|
||||
This file is part of snarkjs.
|
||||
|
||||
snarkjs is a free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as published by the
|
||||
Free Software Foundation, either version 3 of the License, or (at your option)
|
||||
any later version.
|
||||
|
||||
snarkjs is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
snarkjs. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/* Implementation of this paper: https://eprint.iacr.org/2016/260.pdf */
|
||||
|
||||
|
||||
const BN128 = require("./bn128.js");
|
||||
|
||||
const bn128 = new BN128();
|
||||
const G1 = bn128.G1;
|
||||
|
||||
module.exports = function isValid(vk_verifier, proof, publicSignals) {
|
||||
|
||||
let cpub = vk_verifier.IC[0];
|
||||
for (let s= 0; s< vk_verifier.nPublic; s++) {
|
||||
cpub = G1.add( cpub, G1.mulScalar( vk_verifier.IC[s+1], publicSignals[s]));
|
||||
}
|
||||
|
||||
if (! bn128.F12.equals(
|
||||
bn128.pairing( proof.pi_a , proof.pi_b ),
|
||||
bn128.F12.mul(
|
||||
vk_verifier.vk_alfabeta_12,
|
||||
bn128.F12.mul(
|
||||
bn128.pairing( cpub , vk_verifier.vk_gamma_2 ),
|
||||
bn128.pairing( proof.pi_c , vk_verifier.vk_delta_2 )
|
||||
))))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
};
|
67
src/verifier_original.js
Normal file
67
src/verifier_original.js
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
Copyright 2018 0kims association.
|
||||
|
||||
This file is part of snarkjs.
|
||||
|
||||
snarkjs is a free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as published by the
|
||||
Free Software Foundation, either version 3 of the License, or (at your option)
|
||||
any later version.
|
||||
|
||||
snarkjs is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
snarkjs. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
const BN128 = require("./bn128.js");
|
||||
|
||||
const bn128 = new BN128();
|
||||
const G1 = bn128.G1;
|
||||
const G2 = bn128.G2;
|
||||
|
||||
module.exports = function isValid(vk_verifier, proof, publicSignals) {
|
||||
|
||||
let full_pi_a = vk_verifier.IC[0];
|
||||
for (let s= 0; s< vk_verifier.nPublic; s++) {
|
||||
full_pi_a = G1.add( full_pi_a, G1.mulScalar( vk_verifier.IC[s+1], publicSignals[s]));
|
||||
}
|
||||
|
||||
full_pi_a = G1.add( full_pi_a, proof.pi_a);
|
||||
|
||||
if (! bn128.F12.equals(
|
||||
bn128.pairing( proof.pi_a , vk_verifier.vk_a ),
|
||||
bn128.pairing( proof.pi_ap , G2.g )))
|
||||
return false;
|
||||
|
||||
if (! bn128.F12.equals(
|
||||
bn128.pairing( vk_verifier.vk_b, proof.pi_b ),
|
||||
bn128.pairing( proof.pi_bp , G2.g )))
|
||||
return false;
|
||||
|
||||
if (! bn128.F12.equals(
|
||||
bn128.pairing( proof.pi_c , vk_verifier.vk_c ),
|
||||
bn128.pairing( proof.pi_cp , G2.g )))
|
||||
return false;
|
||||
|
||||
if (! bn128.F12.equals(
|
||||
bn128.F12.mul(
|
||||
bn128.pairing( G1.add(full_pi_a, proof.pi_c) , vk_verifier.vk_gb_2 ),
|
||||
bn128.pairing( vk_verifier.vk_gb_1 , proof.pi_b ),
|
||||
),
|
||||
bn128.pairing( proof.pi_kp , vk_verifier.vk_g )))
|
||||
return false;
|
||||
|
||||
if (! bn128.F12.equals(
|
||||
bn128.pairing( full_pi_a , proof.pi_b ),
|
||||
bn128.F12.mul(
|
||||
bn128.pairing( proof.pi_h , vk_verifier.vk_z ),
|
||||
bn128.pairing( proof.pi_c , G2.g ),
|
||||
)))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
};
|
@ -1,65 +0,0 @@
|
||||
[
|
||||
{
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": false,
|
||||
"name": "s",
|
||||
"type": "string"
|
||||
}
|
||||
],
|
||||
"name": "Verified",
|
||||
"type": "event"
|
||||
},
|
||||
{
|
||||
"constant": false,
|
||||
"inputs": [
|
||||
{
|
||||
"name": "a",
|
||||
"type": "uint256[2]"
|
||||
},
|
||||
{
|
||||
"name": "a_p",
|
||||
"type": "uint256[2]"
|
||||
},
|
||||
{
|
||||
"name": "b",
|
||||
"type": "uint256[2][2]"
|
||||
},
|
||||
{
|
||||
"name": "b_p",
|
||||
"type": "uint256[2]"
|
||||
},
|
||||
{
|
||||
"name": "c",
|
||||
"type": "uint256[2]"
|
||||
},
|
||||
{
|
||||
"name": "c_p",
|
||||
"type": "uint256[2]"
|
||||
},
|
||||
{
|
||||
"name": "h",
|
||||
"type": "uint256[2]"
|
||||
},
|
||||
{
|
||||
"name": "k",
|
||||
"type": "uint256[2]"
|
||||
},
|
||||
{
|
||||
"name": "input",
|
||||
"type": "uint256[2]"
|
||||
}
|
||||
],
|
||||
"name": "verifyTx",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "r",
|
||||
"type": "bool"
|
||||
}
|
||||
],
|
||||
"payable": false,
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
}
|
||||
]
|
211
templates/verifier_groth.sol
Normal file
211
templates/verifier_groth.sol
Normal file
@ -0,0 +1,211 @@
|
||||
//
|
||||
// Copyright 2017 Christian Reitwiessner
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
pragma solidity ^0.4.14;
|
||||
library Pairing {
|
||||
struct G1Point {
|
||||
uint X;
|
||||
uint Y;
|
||||
}
|
||||
// Encoding of field elements is: X[0] * z + X[1]
|
||||
struct G2Point {
|
||||
uint[2] X;
|
||||
uint[2] Y;
|
||||
}
|
||||
/// @return the generator of G1
|
||||
function P1() pure internal returns (G1Point) {
|
||||
return G1Point(1, 2);
|
||||
}
|
||||
/// @return the generator of G2
|
||||
function P2() pure internal returns (G2Point) {
|
||||
// Original code point
|
||||
return G2Point(
|
||||
[11559732032986387107991004021392285783925812861821192530917403151452391805634,
|
||||
10857046999023057135944570762232829481370756359578518086990519993285655852781],
|
||||
[4082367875863433681332203403145435568316851327593401208105741076214120093531,
|
||||
8495653923123431417604973247489272438418190587263600148770280649306958101930]
|
||||
);
|
||||
|
||||
/*
|
||||
// Changed by Jordi point
|
||||
return G2Point(
|
||||
[10857046999023057135944570762232829481370756359578518086990519993285655852781,
|
||||
11559732032986387107991004021392285783925812861821192530917403151452391805634],
|
||||
[8495653923123431417604973247489272438418190587263600148770280649306958101930,
|
||||
4082367875863433681332203403145435568316851327593401208105741076214120093531]
|
||||
);
|
||||
*/
|
||||
}
|
||||
/// @return the negation of p, i.e. p.addition(p.negate()) should be zero.
|
||||
function negate(G1Point p) pure internal returns (G1Point) {
|
||||
// The prime q in the base field F_q for G1
|
||||
uint q = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
|
||||
if (p.X == 0 && p.Y == 0)
|
||||
return G1Point(0, 0);
|
||||
return G1Point(p.X, q - (p.Y % q));
|
||||
}
|
||||
/// @return the sum of two points of G1
|
||||
function addition(G1Point p1, G1Point p2) view internal returns (G1Point r) {
|
||||
uint[4] memory input;
|
||||
input[0] = p1.X;
|
||||
input[1] = p1.Y;
|
||||
input[2] = p2.X;
|
||||
input[3] = p2.Y;
|
||||
bool success;
|
||||
assembly {
|
||||
success := staticcall(sub(gas, 2000), 6, input, 0xc0, r, 0x60)
|
||||
// Use "invalid" to make gas estimation work
|
||||
switch success case 0 { invalid() }
|
||||
}
|
||||
require(success);
|
||||
}
|
||||
/// @return the product of a point on G1 and a scalar, i.e.
|
||||
/// p == p.scalar_mul(1) and p.addition(p) == p.scalar_mul(2) for all points p.
|
||||
function scalar_mul(G1Point p, uint s) view internal returns (G1Point r) {
|
||||
uint[3] memory input;
|
||||
input[0] = p.X;
|
||||
input[1] = p.Y;
|
||||
input[2] = s;
|
||||
bool success;
|
||||
assembly {
|
||||
success := staticcall(sub(gas, 2000), 7, input, 0x80, r, 0x60)
|
||||
// Use "invalid" to make gas estimation work
|
||||
switch success case 0 { invalid() }
|
||||
}
|
||||
require (success);
|
||||
}
|
||||
/// @return the result of computing the pairing check
|
||||
/// e(p1[0], p2[0]) * .... * e(p1[n], p2[n]) == 1
|
||||
/// For example pairing([P1(), P1().negate()], [P2(), P2()]) should
|
||||
/// return true.
|
||||
function pairing(G1Point[] p1, G2Point[] p2) view internal returns (bool) {
|
||||
require(p1.length == p2.length);
|
||||
uint elements = p1.length;
|
||||
uint inputSize = elements * 6;
|
||||
uint[] memory input = new uint[](inputSize);
|
||||
for (uint i = 0; i < elements; i++)
|
||||
{
|
||||
input[i * 6 + 0] = p1[i].X;
|
||||
input[i * 6 + 1] = p1[i].Y;
|
||||
input[i * 6 + 2] = p2[i].X[0];
|
||||
input[i * 6 + 3] = p2[i].X[1];
|
||||
input[i * 6 + 4] = p2[i].Y[0];
|
||||
input[i * 6 + 5] = p2[i].Y[1];
|
||||
}
|
||||
uint[1] memory out;
|
||||
bool success;
|
||||
assembly {
|
||||
success := staticcall(sub(gas, 2000), 8, add(input, 0x20), mul(inputSize, 0x20), out, 0x20)
|
||||
// Use "invalid" to make gas estimation work
|
||||
switch success case 0 { invalid() }
|
||||
}
|
||||
require(success);
|
||||
return out[0] != 0;
|
||||
}
|
||||
/// Convenience method for a pairing check for two pairs.
|
||||
function pairingProd2(G1Point a1, G2Point a2, G1Point b1, G2Point b2) view internal returns (bool) {
|
||||
G1Point[] memory p1 = new G1Point[](2);
|
||||
G2Point[] memory p2 = new G2Point[](2);
|
||||
p1[0] = a1;
|
||||
p1[1] = b1;
|
||||
p2[0] = a2;
|
||||
p2[1] = b2;
|
||||
return pairing(p1, p2);
|
||||
}
|
||||
/// Convenience method for a pairing check for three pairs.
|
||||
function pairingProd3(
|
||||
G1Point a1, G2Point a2,
|
||||
G1Point b1, G2Point b2,
|
||||
G1Point c1, G2Point c2
|
||||
) view internal returns (bool) {
|
||||
G1Point[] memory p1 = new G1Point[](3);
|
||||
G2Point[] memory p2 = new G2Point[](3);
|
||||
p1[0] = a1;
|
||||
p1[1] = b1;
|
||||
p1[2] = c1;
|
||||
p2[0] = a2;
|
||||
p2[1] = b2;
|
||||
p2[2] = c2;
|
||||
return pairing(p1, p2);
|
||||
}
|
||||
/// Convenience method for a pairing check for four pairs.
|
||||
function pairingProd4(
|
||||
G1Point a1, G2Point a2,
|
||||
G1Point b1, G2Point b2,
|
||||
G1Point c1, G2Point c2,
|
||||
G1Point d1, G2Point d2
|
||||
) view internal returns (bool) {
|
||||
G1Point[] memory p1 = new G1Point[](4);
|
||||
G2Point[] memory p2 = new G2Point[](4);
|
||||
p1[0] = a1;
|
||||
p1[1] = b1;
|
||||
p1[2] = c1;
|
||||
p1[3] = d1;
|
||||
p2[0] = a2;
|
||||
p2[1] = b2;
|
||||
p2[2] = c2;
|
||||
p2[3] = d2;
|
||||
return pairing(p1, p2);
|
||||
}
|
||||
}
|
||||
contract Verifier {
|
||||
using Pairing for *;
|
||||
struct VerifyingKey {
|
||||
Pairing.G1Point alfa1;
|
||||
Pairing.G2Point beta2;
|
||||
Pairing.G2Point gamma2;
|
||||
Pairing.G2Point delta2;
|
||||
Pairing.G1Point[] IC;
|
||||
}
|
||||
struct Proof {
|
||||
Pairing.G1Point A;
|
||||
Pairing.G2Point B;
|
||||
Pairing.G1Point C;
|
||||
}
|
||||
function verifyingKey() pure internal returns (VerifyingKey vk) {
|
||||
vk.alfa1 = Pairing.G1Point(<%vk_alfa1%>);
|
||||
vk.beta2 = Pairing.G2Point(<%vk_beta2%>);
|
||||
vk.gamma2 = Pairing.G2Point(<%vk_gamma2%>);
|
||||
vk.delta2 = Pairing.G2Point(<%vk_delta2%>);
|
||||
vk.IC = new Pairing.G1Point[](<%vk_ic_length%>);
|
||||
<%vk_ic_pts%>
|
||||
}
|
||||
function verify(uint[] input, Proof proof) view internal returns (uint) {
|
||||
VerifyingKey memory vk = verifyingKey();
|
||||
require(input.length + 1 == vk.IC.length);
|
||||
// Compute the linear combination vk_x
|
||||
Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0);
|
||||
for (uint i = 0; i < input.length; i++)
|
||||
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.IC[i + 1], input[i]));
|
||||
vk_x = Pairing.addition(vk_x, vk.IC[0]);
|
||||
if (!Pairing.pairingProd4(
|
||||
Pairing.negate(proof.A), proof.B,
|
||||
vk.alfa1, vk.beta2,
|
||||
vk_x, vk.gamma2,
|
||||
proof.C, vk.delta2
|
||||
)) return 1;
|
||||
return 0;
|
||||
}
|
||||
function verifyProof(
|
||||
uint[2] a,
|
||||
uint[2][2] b,
|
||||
uint[2] c,
|
||||
uint[<%vk_input_length%>] input
|
||||
) view public returns (bool r) {
|
||||
Proof memory proof;
|
||||
proof.A = Pairing.G1Point(a[0], a[1]);
|
||||
proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]);
|
||||
proof.C = Pairing.G1Point(c[0], c[1]);
|
||||
uint[] memory inputValues = new uint[](input.length);
|
||||
for(uint i = 0; i < input.length; i++){
|
||||
inputValues[i] = input[i];
|
||||
}
|
||||
if (verify(inputValues, proof) == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -23,7 +23,7 @@ const path = require("path");
|
||||
const bigInt = require("../src/bigint.js");
|
||||
|
||||
const Circuit = require("../src/circuit.js");
|
||||
const zkSnark = require("../index.js");
|
||||
const zkSnark = require("../index.js").original;
|
||||
const BN128 = require("../src/bn128.js");
|
||||
const PolField = require("../src/polfield.js");
|
||||
const ZqField = require("../src/zqfield.js");
|
||||
@ -38,10 +38,7 @@ const G2 = bn128.G2;
|
||||
const assert = chai.assert;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
describe("zkSnark", () => {
|
||||
describe("zkSnark original", () => {
|
||||
it("Load a circuit, create trusted setup, create a proof and validate it", () => {
|
||||
|
||||
|
||||
|
44
test/zksnark_groth.js
Normal file
44
test/zksnark_groth.js
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
Copyright 2018 0kims association.
|
||||
|
||||
This file is part of zksnark JavaScript library.
|
||||
|
||||
zksnark JavaScript library is a free software: you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as published by the
|
||||
Free Software Foundation, either version 3 of the License, or (at your option)
|
||||
any later version.
|
||||
|
||||
zksnark JavaScript library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
zksnark JavaScript library. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
const chai = require("chai");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const Circuit = require("../src/circuit.js");
|
||||
const zkSnark = require("../index.js").groth;
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("zkSnark Groth", () => {
|
||||
it("Load a circuit, create trusted setup, create a proof and validate it", () => {
|
||||
|
||||
|
||||
const cirDef = JSON.parse(fs.readFileSync(path.join(__dirname, "circuit", "sum.json"), "utf8"));
|
||||
const cir = new Circuit(cirDef);
|
||||
|
||||
const setup = zkSnark.setup(cir);
|
||||
|
||||
const witness = cir.calculateWitness({"a": "33", "b": "34"});
|
||||
|
||||
const {proof, publicSignals} = zkSnark.genProof(setup.vk_proof, witness);
|
||||
|
||||
assert( zkSnark.isValid(setup.vk_verifier, proof, publicSignals));
|
||||
}).timeout(10000000);
|
||||
});
|
File diff suppressed because one or more lines are too long
@ -1 +1 @@
|
||||
{"nPublic":2,"A":[["13748780897548213951275281002596580813562717675655399375007549036110641136021","191470769863817069897387201389601788324536690033187060846183983079098357394","1"],["18950485977844447902223302204376167458696138040252686570435320938365864348502","7271813516566066459381958879561228755848812321761332703504031940443193860951","1"],["10550502146506181421156845871338656937604707762177820631954232892783886482938","21242116051056802188468939122405035309672553177789742050267311430394049717054","1"]],"vk_a":[["3648994822673106647455305244073563060200556471519763966341475644443204011541","19190015192797538035156514697167432375350886603409288416207360861952319261026"],["19912375136853529904499116381486024931886647702841458927760031833692938494923","7571101790086228463314286168379655212520805710327619853340541433526915960099"],["1","0"]],"vk_b":["10310887787081964577086957678647751381175396134873750255854004838446037014291","16002673809262616992952363436700888148785514822471217759714188814309317465080","1"],"vk_c":[["6160108760190506073804555639033841543328310342176679447310878073768168329940","12370096448271566220904474347814530659706679423163203247294098158176124902591"],["4706988174442652261148543353687214573443297804403418205251952930962669528199","3546107194049375164253573714498474040316692798649722107846476327644607583072"],["1","0"]],"vk_gb_1":["7146696309986681281110057575452850985532610669098681308295432109836239411953","16686123413410528449772051830348980913895858679850292529110189221785284368789","1"],"vk_gb_2":[["18953991732646709983341064534549536299218349659392861886236367876404227375356","1071540992240725849073673180902567457862664009887584586695766822861438847863"],["20870421036314350569737683283619505426680530028797392571771354832217410362185","9848505931878091491817866253146722443798097790010800493474936101706576832542"],["1","0"]],"vk_g":[["918130074516815543852789857283473367724249001127655869824094873426529782559","12374520330065818718765523336075006974954164429863062496190757782266518553924"],["15136410217911142493142327756483366574433556210485010398268543896801775084341","91578221478298064914132335114027313829587197089740823987464475984348362086"],["1","0"]],"vk_z":[["8444006341203690846248826436908513653949166405412766828583891245391665907179","19744368555025039818988622435429063512843759021805799665260531402900174284945"],["1525609944586286127212978014095291350902084283422884034256233641387793310306","20954328038043383968653227341547489558748024941604970666391828647333046834222"],["1","0"]]}
|
||||
{"protocol":"original","nPublic":2,"IC":[["8576214033540765746842371044606415301996418658271131870010054843723125163599","6341586104423260990679666539888111771038590989830860832895739252684050857707","1"],["15965749752898192947949203569369046733334441167283696622057886266799413427301","2923294366489694707913989169803315514830365425557160936060478290028119985650","1"],["7665320522958326350241374389379617045227126617343912001068756442073187713151","16937313025308113974092602028496797364239833856912463403728694496750412757538","1"]],"vk_a":[["3077982983963123319793719127530918395964362371665929870764367633238119486171","10624095704253675484950110288768135448751925925397705744445034274470877571540"],["11223362065478924443549715710933863673280794406631767082774645916652066279620","8337303782475651751238621200032482701595413914475156108205195808465067782279"],["1","0"]],"vk_b":["21564332231353345084812731105505859926291540020010209070828241581510575981361","12811195542841792594989162567316414585491137627361662320857051029637227214375","1"],"vk_c":[["19363023180056795628459533284881872720326658174081788262267940216199137656570","5359479215742140254894625739314368559215232189770028818973166302624793559662"],["5187488231426445386134004945862512998731236294362858853152004154989502945485","17318067103531560956632275402153347927836013608148619363574266960244666419321"],["1","0"]],"vk_gb_1":["13737850238192771532086098218362386191676025312053065674387897049554183714895","258437624375537354893318850011642077381094913426153928052848023008663588967","1"],"vk_gb_2":[["20913086464866824409816176346029303914897145075292440377103410617064950551322","11968128624576981916297332896923117639178526628339597211407604428593282315380"],["21502773962397802833840010558276426068561764138489279214768725069584289910777","5965528328875401985902369254259185228639060740935479481669241063329338432524"],["1","0"]],"vk_g":[["1421964931962045670203169891741553307915101481355771768590757853405478618657","18344258226310817730803896348763784823599742300007390070956950541714752264594"],["84572320859600481810744445647071373623359906260999813141892498206365049722","17232762898384488387547345327643231467979450189903835933795160671651120045038"],["1","0"]],"vk_z":[["10267283293805156712473859167140009309675016274438754125335111954220207367714","16337057180792535646473469361191381596052094521156035083114407475052456206736"],["8437712429457291876807475578260323834614411158149818941874257329388894255364","15389184070055161803675113258684887402795693485388956653591015728117764501181"],["1","0"]]}
|
Loading…
Reference in New Issue
Block a user