@ -0,0 +1,352 @@ |
|||||
|
#!/usr/bin/env node
|
||||
|
|
||||
|
/* |
||||
|
Copyright 2018 0KIMS association. |
||||
|
|
||||
|
This file is part of jaz (Zero Knowledge Circuit Compiler). |
||||
|
|
||||
|
jaz 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. |
||||
|
|
||||
|
jaz 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 jaz. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
*/ |
||||
|
|
||||
|
/* eslint-disable no-console */ |
||||
|
|
||||
|
const fs = require("fs"); |
||||
|
const path = require("path"); |
||||
|
|
||||
|
const zkSnark = require("./index.js"); |
||||
|
const {stringifyBigInts, unstringifyBigInts} = require("./src/stringifybigint.js"); |
||||
|
|
||||
|
|
||||
|
const version = require("./package").version; |
||||
|
|
||||
|
const argv = require("yargs") |
||||
|
.version(version) |
||||
|
.usage(`snarkjs <command> <options>
|
||||
|
|
||||
|
setup command |
||||
|
============= |
||||
|
|
||||
|
snarkjs setup <option> |
||||
|
|
||||
|
Runs a setup for a circuit generating the proving and the verification key. |
||||
|
|
||||
|
-c or --circuit <circuitFile> |
||||
|
|
||||
|
Filename of the compiled circuit file generated by circom. |
||||
|
|
||||
|
Default: circuit.json |
||||
|
|
||||
|
--pk or --provingkey <provingKeyFile> |
||||
|
|
||||
|
Output filename where the proving key will be stored. |
||||
|
|
||||
|
Default: proving_key.json |
||||
|
|
||||
|
--vk or --verificationkey <verificationKeyFile> |
||||
|
|
||||
|
Output Filename where the verification key will be stored. |
||||
|
|
||||
|
Default: verification_key.json |
||||
|
|
||||
|
calculate witness command |
||||
|
========================= |
||||
|
|
||||
|
snarkjs calculatewitness <options> |
||||
|
|
||||
|
Calculate the witness of a circuit given an input. |
||||
|
|
||||
|
-c or --circuit <circuitFile> |
||||
|
|
||||
|
Filename of the compiled circuit file generated by circom. |
||||
|
|
||||
|
Default: circuit.json |
||||
|
|
||||
|
-i or --input <inputFile> |
||||
|
|
||||
|
JSON file with the inputs of the circuit. |
||||
|
|
||||
|
Default: input.json |
||||
|
|
||||
|
Example of for a circuit with tow inputs a and b: |
||||
|
|
||||
|
{"a": "22", "b": "33"} |
||||
|
|
||||
|
-w or --witness |
||||
|
|
||||
|
Output filename with the generated witness. |
||||
|
|
||||
|
Default: witness.json |
||||
|
|
||||
|
generate a proof command |
||||
|
======================== |
||||
|
|
||||
|
snarkjs proof <options> |
||||
|
|
||||
|
-w or --witness |
||||
|
|
||||
|
Input filename used to calculate the proof. |
||||
|
|
||||
|
Default: witness.json |
||||
|
|
||||
|
--pk or --provingkey <provingKeyFile> |
||||
|
|
||||
|
Input filename with the proving key (generated during the setup). |
||||
|
|
||||
|
Default: proving_key.json |
||||
|
|
||||
|
-p or --proof |
||||
|
|
||||
|
Output filenam with the zero knowlage proof. |
||||
|
|
||||
|
Default: proof.json |
||||
|
|
||||
|
--pub or --public <publicFilename> |
||||
|
|
||||
|
Output filename with the value of the public wires/signals. |
||||
|
This info will be needed to verify the proof. |
||||
|
|
||||
|
Default: public.json |
||||
|
|
||||
|
verify command |
||||
|
============== |
||||
|
|
||||
|
snarkjs verify <options> |
||||
|
|
||||
|
The command returns "OK" if the proof is valid |
||||
|
and "INVALID" in case it is not a valid proof. |
||||
|
|
||||
|
--vk or --verificationkey <verificationKeyFile> |
||||
|
|
||||
|
Input Filename with the verification key (generated during the setup). |
||||
|
|
||||
|
Default: verification_key.json |
||||
|
|
||||
|
-p or --proof |
||||
|
|
||||
|
Input filenam with the zero knowlage proof you want to verify |
||||
|
|
||||
|
Default: proof.json |
||||
|
|
||||
|
--pub or --public <publicFilename> |
||||
|
|
||||
|
Input filename with the public wires/signals. |
||||
|
|
||||
|
Default: public.json |
||||
|
|
||||
|
generate solidity verifier command |
||||
|
================================== |
||||
|
|
||||
|
snarkjs generateverifier <options> |
||||
|
|
||||
|
Generates a solidity smart contract that verifies the zero knowlage proof. |
||||
|
|
||||
|
--vk or --verificationkey <verificationKeyFile> |
||||
|
|
||||
|
Input Filename with the verification key (generated during the setup). |
||||
|
|
||||
|
Default: verification_key.json |
||||
|
|
||||
|
-v or --verifier |
||||
|
|
||||
|
Output file with a solidity smart contract that verifies a zero knowlage proof. |
||||
|
|
||||
|
Default: verifier.sol |
||||
|
|
||||
|
generate call parameters |
||||
|
======================== |
||||
|
|
||||
|
snarkjs generatecall <options> |
||||
|
|
||||
|
Outputs into the console the raw parameters to be used in 'verifyProof' |
||||
|
method of the solidity verifier. |
||||
|
|
||||
|
-p or --proof |
||||
|
|
||||
|
Input filenam with the zero knowlage proof you want to use |
||||
|
|
||||
|
Default: proof.json |
||||
|
|
||||
|
--pub or --public <publicFilename> |
||||
|
|
||||
|
Input filename with the public wires/signals. |
||||
|
|
||||
|
Default: public.json |
||||
|
|
||||
|
|
||||
|
`)
|
||||
|
.alias("c", "circuit") |
||||
|
.alias("pk", "provingkey") |
||||
|
.alias("vk", "verificationkey") |
||||
|
.alias("w", "witness") |
||||
|
.alias("p", "proof") |
||||
|
.alias("i", "input") |
||||
|
.alias("pub", "public") |
||||
|
.alias("v", "verifier") |
||||
|
.help("h") |
||||
|
.alias("h", "help") |
||||
|
.epilogue(`Copyright (C) 2018 0kims association
|
||||
|
This program comes with ABSOLUTELY NO WARRANTY; |
||||
|
This is free software, and you are welcome to redistribute it |
||||
|
under certain conditions; see the COPYING file in the official |
||||
|
repo directory at https://github.com/iden3/circom `)
|
||||
|
.argv; |
||||
|
|
||||
|
const circuitName = (argv.circuit) ? argv.circuit : "circuit.json"; |
||||
|
const provingKeyName = (argv.provingkey) ? argv.provingkey : "proving_key.json"; |
||||
|
const verificationKeyName = (argv.verificationkey) ? argv.verificationkey : "verification_key.json"; |
||||
|
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"; |
||||
|
|
||||
|
function p256(n) { |
||||
|
let nstr = n.toString(16); |
||||
|
while (nstr.length < 64) nstr = "0"+nstr; |
||||
|
nstr = `"0x${nstr}"`; |
||||
|
return nstr; |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
if (argv._[0].toUpperCase() == "SETUP") { |
||||
|
const cirDef = JSON.parse(fs.readFileSync(circuitName, "utf8")); |
||||
|
const cir = new zkSnark.Circuit(cirDef); |
||||
|
const setup = zkSnark.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"); |
||||
|
process.exit(0); |
||||
|
} else if (argv._[0].toUpperCase() == "CALCULATEWITNESS") { |
||||
|
const cirDef = JSON.parse(fs.readFileSync(circuitName, "utf8")); |
||||
|
const cir = new zkSnark.Circuit(cirDef); |
||||
|
const input = unstringifyBigInts(JSON.parse(fs.readFileSync(inputName, "utf8"))); |
||||
|
|
||||
|
const witness = cir.calculateWitness(input); |
||||
|
|
||||
|
fs.writeFileSync(witnessName, JSON.stringify(stringifyBigInts(witness), null, 1), "utf-8"); |
||||
|
process.exit(0); |
||||
|
} else if (argv._[0].toUpperCase() == "PROOF") { |
||||
|
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); |
||||
|
|
||||
|
fs.writeFileSync(proofName, JSON.stringify(stringifyBigInts(proof), null, 1), "utf-8"); |
||||
|
fs.writeFileSync(publicName, JSON.stringify(stringifyBigInts(publicSignals), null, 1), "utf-8"); |
||||
|
process.exit(0); |
||||
|
} else if (argv._[0].toUpperCase() == "VERIFY") { |
||||
|
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); |
||||
|
|
||||
|
if (isValid) { |
||||
|
console.log("OK"); |
||||
|
process.exit(0); |
||||
|
} else { |
||||
|
console.log("INVALID"); |
||||
|
process.exit(1); |
||||
|
} |
||||
|
} 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`; |
||||
|
} |
||||
|
template = template.replace("<%vk_ic_pts%>", vi); |
||||
|
|
||||
|
|
||||
|
fs.writeFileSync(verifierName, template, "utf-8"); |
||||
|
process.exit(0); |
||||
|
|
||||
|
} else if (argv._[0].toUpperCase() == "GENERATECALL") { |
||||
|
|
||||
|
const public = unstringifyBigInts(JSON.parse(fs.readFileSync(publicName, "utf8"))); |
||||
|
const proof = unstringifyBigInts(JSON.parse(fs.readFileSync(proofName, "utf8"))); |
||||
|
|
||||
|
inputs = ""; |
||||
|
for (let i=0; i<public.length; i++) { |
||||
|
if (inputs != "") inputs = inputs + ","; |
||||
|
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}]` ; |
||||
|
|
||||
|
console.log(S); |
||||
|
process.exit(0); |
||||
|
} |
||||
|
} catch(err) { |
||||
|
console.log("ERROR: " + err); |
||||
|
process.exit(1); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
@ -0,0 +1,36 @@ |
|||||
|
const bigInt = require("./bigint.js"); |
||||
|
|
||||
|
module.exports.stringifyBigInts = stringifyBigInts; |
||||
|
module.exports.unstringifyBigInts = unstringifyBigInts; |
||||
|
|
||||
|
function stringifyBigInts(o) { |
||||
|
if ((typeof(o) == "bigint") || (o instanceof bigInt)) { |
||||
|
return o.toString(10); |
||||
|
} else if (Array.isArray(o)) { |
||||
|
return o.map(stringifyBigInts); |
||||
|
} else if (typeof o == "object") { |
||||
|
const res = {}; |
||||
|
for (let k in o) { |
||||
|
res[k] = stringifyBigInts(o[k]); |
||||
|
} |
||||
|
return res; |
||||
|
} else { |
||||
|
return o; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function unstringifyBigInts(o) { |
||||
|
if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) )) { |
||||
|
return bigInt(o); |
||||
|
} else if (Array.isArray(o)) { |
||||
|
return o.map(unstringifyBigInts); |
||||
|
} else if (typeof o == "object") { |
||||
|
const res = {}; |
||||
|
for (let k in o) { |
||||
|
res[k] = unstringifyBigInts(o[k]); |
||||
|
} |
||||
|
return res; |
||||
|
} else { |
||||
|
return o; |
||||
|
} |
||||
|
} |
@ -0,0 +1,243 @@ |
|||||
|
// |
||||
|
// 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.G2Point A; |
||||
|
Pairing.G1Point B; |
||||
|
Pairing.G2Point C; |
||||
|
Pairing.G2Point gamma; |
||||
|
Pairing.G1Point gammaBeta1; |
||||
|
Pairing.G2Point gammaBeta2; |
||||
|
Pairing.G2Point Z; |
||||
|
Pairing.G1Point[] IC; |
||||
|
} |
||||
|
struct Proof { |
||||
|
Pairing.G1Point A; |
||||
|
Pairing.G1Point A_p; |
||||
|
Pairing.G2Point B; |
||||
|
Pairing.G1Point B_p; |
||||
|
Pairing.G1Point C; |
||||
|
Pairing.G1Point C_p; |
||||
|
Pairing.G1Point K; |
||||
|
Pairing.G1Point H; |
||||
|
} |
||||
|
function verifyingKey() pure internal returns (VerifyingKey vk) { |
||||
|
vk.A = Pairing.G2Point(<%vk_a%>); |
||||
|
vk.B = Pairing.G1Point(<%vk_b%>); |
||||
|
vk.C = Pairing.G2Point(<%vk_c%>); |
||||
|
vk.gamma = Pairing.G2Point(<%vk_g%>); |
||||
|
vk.gammaBeta1 = Pairing.G1Point(<%vk_gb1%>); |
||||
|
vk.gammaBeta2 = Pairing.G2Point(<%vk_gb2%>); |
||||
|
vk.Z = Pairing.G2Point(<%vk_z%>); |
||||
|
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.pairingProd2(proof.A, vk.A, Pairing.negate(proof.A_p), Pairing.P2())) return 1; |
||||
|
if (!Pairing.pairingProd2(vk.B, proof.B, Pairing.negate(proof.B_p), Pairing.P2())) return 2; |
||||
|
if (!Pairing.pairingProd2(proof.C, vk.C, Pairing.negate(proof.C_p), Pairing.P2())) return 3; |
||||
|
if (!Pairing.pairingProd3( |
||||
|
proof.K, vk.gamma, |
||||
|
Pairing.negate(Pairing.addition(vk_x, Pairing.addition(proof.A, proof.C))), vk.gammaBeta2, |
||||
|
Pairing.negate(vk.gammaBeta1), proof.B |
||||
|
)) return 4; |
||||
|
if (!Pairing.pairingProd3( |
||||
|
Pairing.addition(vk_x, proof.A), proof.B, |
||||
|
Pairing.negate(proof.H), vk.Z, |
||||
|
Pairing.negate(proof.C), Pairing.P2() |
||||
|
)) return 5; |
||||
|
return 0; |
||||
|
} |
||||
|
function verifyProof( |
||||
|
uint[2] a, |
||||
|
uint[2] a_p, |
||||
|
uint[2][2] b, |
||||
|
uint[2] b_p, |
||||
|
uint[2] c, |
||||
|
uint[2] c_p, |
||||
|
uint[2] h, |
||||
|
uint[2] k, |
||||
|
uint[<%vk_input_length%>] input |
||||
|
) view public returns (bool r) { |
||||
|
Proof memory proof; |
||||
|
proof.A = Pairing.G1Point(a[0], a[1]); |
||||
|
proof.A_p = Pairing.G1Point(a_p[0], a_p[1]); |
||||
|
proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); |
||||
|
proof.B_p = Pairing.G1Point(b_p[0], b_p[1]); |
||||
|
proof.C = Pairing.G1Point(c[0], c[1]); |
||||
|
proof.C_p = Pairing.G1Point(c_p[0], c_p[1]); |
||||
|
proof.H = Pairing.G1Point(h[0], h[1]); |
||||
|
proof.K = Pairing.G1Point(k[0], k[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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
@ -0,0 +1,65 @@ |
|||||
|
[ |
||||
|
{ |
||||
|
"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" |
||||
|
} |
||||
|
] |
@ -1 +1 @@ |
|||||
{"nPublic":2,"A":[["19866551811478279859371505738760842460298832691829573450466306875259147816351","18484210313804053852691998481397858585039949227200722719662305248245758849458","1"],["3321197521086530222974755730452803687377653332413676316053594427542097303026","21405036250373007328155097101376947599833245333713640707489754435587971845022","1"],["21833147244326570889506780953761640127646772724320477084900501403705556180608","13228000966455921757863898151554541961470202242230065946010077829374885469919","1"],null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"vk_a":[["17284880101934421716867000857655867908135830044531782413024303548989886895730","1735917810785977377641441402870114971383566928423272074303188394633486948966"],["18143813711447758200728255064833995555385678921614879156953742259682697013582","9778931845031431023119429754246894069349980576715652054851141383204525095587"],["1","0"]],"vk_b":["14976617904846839439005826980055141931353928644267029029764697310692939095021","5867142307356813613225767321301226628803608539631022334080355947827485621235","1"],"vk_c":[["14825307856429073007768725150519096517177743964102549507390431522825885473512","2993723324749955168046184125337407556318901678522829604900052191883372228164"],["10596857378882997562444306396128548970225716331660657686316389569267793694877","8924471540644031663480734201722112310725171102165508663043207184149950292723"],["1","0"]],"vk_gb_1":["6391389769949607088371850260826019006443584539084910402039856482240477201196","11459237189519841212595651868736624042781096594211055657773003974759899266504","1"],"vk_gb_2":[["8903022736104336682024560755602518274051619743344817898484205184695903011669","17253174988259244044272263486767974170507168609917129675725408354806456107130"],["7034345039375637030366458266633445787806831685389312798374746515789881396853","21229655513890199467150912049419581060418312327352989132791006661085219742639"],["1","0"]],"vk_g":[["21409225604727282108960769517090184919782401135734772621926559957826424142099","4886558222812229990753251273025235769688607418216792970736499124200668838569"],["10597146209887467940103412590383790281995865109533815104011762238891151306672","6277981791909106949721324666906110645859081424664916287488692958783040780257"],["1","0"]],"vk_z":[["14875504132786978560894206711379200637747665615205380091651641102667915352355","534711614457492228196937099267844466151486371571151310554367455631281265482"],["17462155184890063878837340287437334687618721645486008827392227127539533750814","10005242267604321112420693035002138527489909284960643107354149199565804371109"],["1","0"]]} |
|
||||
|
{"nPublic":2,"A":[["21783206803516044994174750002568690252068256560383904524664570548274544012277","1679775398117103986758101064951226346356097053425072202459493001797187917029","1"],["3563931453303131448175617496629182396918497049045278511792868802015132125617","17108253597332388611410216938743242005120426021393164810072439917710370685334","1"],["18179154975167298459464967912756929539327940172006222494491237104150902332714","11897109050886577708781458409326193138191930520293738495773116438104193774744","1"],null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"vk_a":[["4173169569084855090451015931771305703791038614957422228114247239725673275484","15737814612943777266478815572455893036780131604136610205845198132131788177577"],["1644920580273342432541014783358872312652899730596419348798911195398240709441","17160612290645377130311216483095075521122368171209018500717585377548945449124"],["1","0"]],"vk_b":["3129477430316857273072652156669680608349177144500726024771966478535189917291","2204597623351392423204933166035596624315558069159716487001087795509032407966","1"],"vk_c":[["6922742103746135240859914028276952915767498632628485531113151802414101730779","2493359791500660583968747154877749072901979233596700327715061079445897608264"],["15479419835813324972058251699388948445591919493838401826032382765310218114299","11216226198303890903116858400517129966589639754755110980352510264090367304324"],["1","0"]],"vk_gb_1":["20636273490948975989301814480411448137428962548037114221547396388112418751537","2153047681821913584045881307154242044203392985058304522044183326616941760513","1"],"vk_gb_2":[["563621456460024257753708881112012393912388465269005796975012484248913174902","11703781974560846553921002567783147967101964919208467709816995544499787064518"],["400695514409109256820817717804748868087353053091778307255603601175602390130","804564173322442171955490892303197064166628245840972758087205328751897748494"],["1","0"]],"vk_g":[["15945306278353779575372259791829745890525116619705142648112339784668070264339","2395213805761838023496888843888695283485611819932093758555565635859446667666"],["14128126802440372327888268365395629550202124513234768223004952085604370737789","4732453395557645117865101298914691363869880242145323027177232228553106025538"],["1","0"]],"vk_z":[["8032936434160817668602170465209401652117194389636501044820380642856094643737","5470877429980811677348989917749436245542761942229263490094842318618756128755"],["524782744940140160762259687120679618686923326750254515638261595371928594284","20422436679908706145328503275240616703082912175224896119015320150398834742278"],["1","0"]]} |