Fix Variables and test example

This commit is contained in:
Jordi Baylina 2018-09-06 04:40:26 +02:00
parent 2834d5cfc6
commit ef1ea14f1e
No known key found for this signature in database
GPG Key ID: 7480C80C1BE43112
6 changed files with 110 additions and 9 deletions

@ -1,6 +1,6 @@
{
"name": "zksnark",
"version": "0.0.2",
"version": "0.0.3",
"description": "zkSnark implementation in javascript",
"main": "index.js",
"scripts": {

@ -60,7 +60,7 @@ function calculatePolinomials(setup, circuit) {
const aPoints = [];
const bPoints = [];
const cPoints = [];
for (let s = 0; s<circuit.nSignals; s++) {
for (let s = 0; s<circuit.nVars; s++) {
aPoints[s] = [];
bPoints[s] = [];
cPoints[s] = [];
@ -82,8 +82,8 @@ function calculatePolinomials(setup, circuit) {
setup.vk_proof.polsA = [];
setup.vk_proof.polsB = [];
setup.vk_proof.polsC = [];
for (let s=0; s<circuit.nSignals; s++) {
// console.log(`Caclcualte Pol ${s}/${circuit.nSignals}`);
for (let s=0; s<circuit.nVars; s++) {
// console.log(`Caclcualte Pol ${s}/${circuit.nVars}`);
const pA = RatPolF.lagrange( aPoints[s] );
const pB = RatPolF.lagrange( bPoints[s] );
const pC = RatPolF.lagrange( cPoints[s] );
@ -130,7 +130,7 @@ function calculateEncriptedValuesAtT(setup, circuit) {
setup.vk_verifier.vk_gb_2 = G2.affine(G2.mulScalar( G2.g, gb));
setup.vk_verifier.vk_g = G2.affine(G2.mulScalar( G2.g, setup.toxic.kgamma));
for (let s=0; s<circuit.nSignals; s++) {
for (let s=0; s<circuit.nVars; s++) {
// A[i] = G1 * polA(t)
const at = F.affine(PolF.eval(setup.vk_proof.polsA[s], setup.toxic.t));
@ -187,7 +187,7 @@ function calculateHexps(setup, circuit) {
let maxA = 0;
let maxB = 0;
let maxC = 0;
for (let s=0; s<circuit.nSignals; s++) {
for (let s=0; s<circuit.nVars; s++) {
maxA = Math.max(maxA, setup.vk_proof.polsA[s].length);
maxB = Math.max(maxB, setup.vk_proof.polsB[s].length);
maxC = Math.max(maxC, setup.vk_proof.polsC[s].length);

@ -1161,6 +1161,7 @@
"4": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"4": "1"
},
{}
@ -1170,6 +1171,7 @@
"5": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"5": "1"
},
{}
@ -1179,6 +1181,7 @@
"6": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"6": "1"
},
{}
@ -1188,6 +1191,7 @@
"7": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"7": "1"
},
{}
@ -1197,6 +1201,7 @@
"8": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"8": "1"
},
{}
@ -1206,6 +1211,7 @@
"9": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"9": "1"
},
{}
@ -1215,6 +1221,7 @@
"10": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"10": "1"
},
{}
@ -1224,6 +1231,7 @@
"11": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"11": "1"
},
{}
@ -1233,6 +1241,7 @@
"12": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"12": "1"
},
{}
@ -1242,6 +1251,7 @@
"13": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"13": "1"
},
{}
@ -1251,6 +1261,7 @@
"14": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"14": "1"
},
{}
@ -1260,6 +1271,7 @@
"15": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"15": "1"
},
{}
@ -1269,6 +1281,7 @@
"16": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"16": "1"
},
{}
@ -1278,6 +1291,7 @@
"17": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"17": "1"
},
{}
@ -1287,6 +1301,7 @@
"18": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"18": "1"
},
{}
@ -1296,6 +1311,7 @@
"19": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"19": "1"
},
{}
@ -1305,6 +1321,7 @@
"20": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"20": "1"
},
{}
@ -1314,6 +1331,7 @@
"21": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"21": "1"
},
{}
@ -1323,6 +1341,7 @@
"22": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"22": "1"
},
{}
@ -1332,6 +1351,7 @@
"23": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"23": "1"
},
{}
@ -1341,6 +1361,7 @@
"24": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"24": "1"
},
{}
@ -1350,6 +1371,7 @@
"25": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"25": "1"
},
{}
@ -1359,6 +1381,7 @@
"26": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"26": "1"
},
{}
@ -1368,6 +1391,7 @@
"27": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"27": "1"
},
{}
@ -1377,6 +1401,7 @@
"28": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"28": "1"
},
{}
@ -1386,6 +1411,7 @@
"29": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"29": "1"
},
{}
@ -1395,6 +1421,7 @@
"30": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"30": "1"
},
{}
@ -1404,6 +1431,7 @@
"31": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"31": "1"
},
{}
@ -1413,6 +1441,7 @@
"32": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"32": "1"
},
{}
@ -1422,6 +1451,7 @@
"33": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"33": "1"
},
{}
@ -1431,6 +1461,7 @@
"34": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"34": "1"
},
{}
@ -1440,6 +1471,7 @@
"35": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"35": "1"
},
{}
@ -1488,6 +1520,7 @@
"36": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"36": "1"
},
{}
@ -1497,6 +1530,7 @@
"37": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"37": "1"
},
{}
@ -1506,6 +1540,7 @@
"38": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"38": "1"
},
{}
@ -1515,6 +1550,7 @@
"39": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"39": "1"
},
{}
@ -1524,6 +1560,7 @@
"40": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"40": "1"
},
{}
@ -1533,6 +1570,7 @@
"41": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"41": "1"
},
{}
@ -1542,6 +1580,7 @@
"42": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"42": "1"
},
{}
@ -1551,6 +1590,7 @@
"43": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"43": "1"
},
{}
@ -1560,6 +1600,7 @@
"44": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"44": "1"
},
{}
@ -1569,6 +1610,7 @@
"45": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"45": "1"
},
{}
@ -1578,6 +1620,7 @@
"46": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"46": "1"
},
{}
@ -1587,6 +1630,7 @@
"47": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"47": "1"
},
{}
@ -1596,6 +1640,7 @@
"48": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"48": "1"
},
{}
@ -1605,6 +1650,7 @@
"49": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"49": "1"
},
{}
@ -1614,6 +1660,7 @@
"50": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"50": "1"
},
{}
@ -1623,6 +1670,7 @@
"51": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"51": "1"
},
{}
@ -1632,6 +1680,7 @@
"52": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"52": "1"
},
{}
@ -1641,6 +1690,7 @@
"53": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"53": "1"
},
{}
@ -1650,6 +1700,7 @@
"54": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"54": "1"
},
{}
@ -1659,6 +1710,7 @@
"55": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"55": "1"
},
{}
@ -1668,6 +1720,7 @@
"56": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"56": "1"
},
{}
@ -1677,6 +1730,7 @@
"57": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"57": "1"
},
{}
@ -1686,6 +1740,7 @@
"58": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"58": "1"
},
{}
@ -1695,6 +1750,7 @@
"59": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"59": "1"
},
{}
@ -1704,6 +1760,7 @@
"60": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"60": "1"
},
{}
@ -1713,6 +1770,7 @@
"61": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"61": "1"
},
{}
@ -1722,6 +1780,7 @@
"62": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"62": "1"
},
{}
@ -1731,6 +1790,7 @@
"63": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"63": "1"
},
{}
@ -1740,6 +1800,7 @@
"64": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"64": "1"
},
{}
@ -1749,6 +1810,7 @@
"65": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"65": "1"
},
{}
@ -1758,6 +1820,7 @@
"66": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"66": "1"
},
{}
@ -1767,6 +1830,7 @@
"67": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"67": "1"
},
{}
@ -1815,6 +1879,7 @@
"68": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"68": "1"
},
{}
@ -1824,6 +1889,7 @@
"69": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"69": "1"
},
{}
@ -1833,6 +1899,7 @@
"70": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"70": "1"
},
{}
@ -1842,6 +1909,7 @@
"71": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"71": "1"
},
{}
@ -1851,6 +1919,7 @@
"72": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"72": "1"
},
{}
@ -1860,6 +1929,7 @@
"73": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"73": "1"
},
{}
@ -1869,6 +1939,7 @@
"74": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"74": "1"
},
{}
@ -1878,6 +1949,7 @@
"75": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"75": "1"
},
{}
@ -1887,6 +1959,7 @@
"76": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"76": "1"
},
{}
@ -1896,6 +1969,7 @@
"77": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"77": "1"
},
{}
@ -1905,6 +1979,7 @@
"78": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"78": "1"
},
{}
@ -1914,6 +1989,7 @@
"79": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"79": "1"
},
{}
@ -1923,6 +1999,7 @@
"80": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"80": "1"
},
{}
@ -1932,6 +2009,7 @@
"81": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"81": "1"
},
{}
@ -1941,6 +2019,7 @@
"82": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"82": "1"
},
{}
@ -1950,6 +2029,7 @@
"83": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"83": "1"
},
{}
@ -1959,6 +2039,7 @@
"84": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"84": "1"
},
{}
@ -1968,6 +2049,7 @@
"85": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"85": "1"
},
{}
@ -1977,6 +2059,7 @@
"86": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"86": "1"
},
{}
@ -1986,6 +2069,7 @@
"87": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"87": "1"
},
{}
@ -1995,6 +2079,7 @@
"88": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"88": "1"
},
{}
@ -2004,6 +2089,7 @@
"89": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"89": "1"
},
{}
@ -2013,6 +2099,7 @@
"90": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"90": "1"
},
{}
@ -2022,6 +2109,7 @@
"91": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"91": "1"
},
{}
@ -2031,6 +2119,7 @@
"92": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"92": "1"
},
{}
@ -2040,6 +2129,7 @@
"93": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"93": "1"
},
{}
@ -2049,6 +2139,7 @@
"94": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"94": "1"
},
{}
@ -2058,6 +2149,7 @@
"95": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"95": "1"
},
{}
@ -2067,6 +2159,7 @@
"96": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"96": "1"
},
{}
@ -2076,6 +2169,7 @@
"97": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"97": "1"
},
{}
@ -2085,6 +2179,7 @@
"98": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"98": "1"
},
{}
@ -2094,6 +2189,7 @@
"99": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"99": "1"
},
{}
@ -2103,6 +2199,7 @@
"100": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616",
"100": "1"
},
{}

@ -62,9 +62,13 @@ function unstringifyBigInts(o) {
describe("zkSnark", () => {
it("Load a circuit, create trusted setup, create a proof and validate", () => {
const cirDef = JSON.parse(fs.readFileSync(path.join(__dirname, "circuit", "sum.json"), "utf8"));
const cirDef = JSON.parse(fs.readFileSync(path.join(__dirname, "circuit", "sum_old.json"), "utf8"));
const cir = new Circuit(cirDef);
console.log("Signals: " + cir.nSignals);
console.log("Vars: " + cir.nVars);
console.log("Constants: " + cir.nConstants);
const setup = zkSnark.setup(cir);
const strSetup = stringifyBigInts(setup);
fs.writeFileSync("vk_proof.json", JSON.stringify(strSetup.vk_proof), "utf-8");

File diff suppressed because one or more lines are too long

@ -1 +1 @@
{"nPublic":2,"A":[["0","1","0"],["0","1","0"],["0","1","0"]],"vk_a":[["20560152968605635047047919676756549283523817170997848427440669466397276089891","7818200049125752241522512514595555840965570134556904697441429125880983431973"],["4004622395899538748391960201261807969480869650929771125579186354842467518877","18588817277049499857912086791069903740856836703741772287766008332848801238683"],["1","0"]],"vk_b":["7675095142863148568659609174259589451633443643157677274235081172746171956542","18338594371505088398217690997908895905630211655128125704886936054880330961649","1"],"vk_c":[["970991860474890601986787809500909910117209143914824514633406060579917804831","4489472998861614386435429631561506491479661731588162275433693707556712547841"],["10480421612152685783877114152701120795938125690414386886166896214739837143779","5640799180464335612585129736766378273209064892082182915157435013613846562729"],["1","0"]],"vk_gb_1":["4728671699406973361533638453844939122296785601499319421870766751819864821810","18590397052718771409382296794461534416250066676149101531720436218867605770708","1"],"vk_gb_2":[["17844101039536210909524887542440503349088218139829556536038350017244860377748","486615893104745799154322826376187390141407828968942579410680571889220991650"],["16222046142698304277705000927939371189198509817041408962815545046461085484277","16939467019814743646851821875954149589630819655681114269447165709665490121261"],["1","0"]],"vk_g":[["18172673215462466998482333376307489788260969976249399115937860752249844438018","19760300569958518554347022518467441716192319217874332681674016606222863236037"],["17380602559151721627106163894145371007936686580900076060630372401915351905984","20709756557188364181510512059091800556109827259575108072191711716654455458177"],["1","0"]],"vk_z":[["8623243840792232633123147565914995855135594279231733924099511669061508984668","7360712253826041579457165313251304415222924008591401809078818557165549277617"],["2456098287596332925405250001167037649249238481321660217499315092313772466659","16563797406530073760270029887321638579276696184392138086412297266689741878778"],["1","0"]]}
{"nPublic":2,"A":[["0","1","0"],["0","1","0"],["0","1","0"]],"vk_a":[["7089990502144118832604367521011815819798606716933228956649110482164464380406","11230368732527352934600748828578966453798139638406648889650954564514158968703"],["2552025287468333777208188228671587362279970708925048598762654161010066177043","15817150506762213830286503668291435731391320924329741514835476096095673797541"],["1","0"]],"vk_b":["21226082957049381990294170600222709388915844017670592613996115036848934411084","6006027414836389636439941146361896036828945752850102505912600116069920998905","1"],"vk_c":[["14972475653539484594981526569218338016113092790075151393066856871644596682724","21082271776918104650812338650952413460358778951692637967538121533518900717477"],["9488935497761382226730503363657417022919431431195898152774653504290411140945","19381581614863000065474721359406816581030290577744306196107899638975834631453"],["1","0"]],"vk_gb_1":["21252927779317539356866989571443021111053362742199247507918719071083375839517","10363106511526332770968391923850585843602882105799869583139073142840826136733","1"],"vk_gb_2":[["4008279642693334257982765700260663237694948550659008283313366773842380503881","8753806373766515819666987287783146863922344545260667289971101962956210093327"],["5807885013795520098816830751231276772859952798588983742628691967101071302818","6978771515240022856338630590059538314801825386905681885426584494404793679829"],["1","0"]],"vk_g":[["4320480846845722370509620085106073102683489399848883412499707153377041321853","21539907783935770792755522803992030540887731620905279286817443960442024347124"],["1151638670443091902847376995820628224336466611917726211841718214221717869223","21656897510530916728876429664556536866823248506277537270768352818364789952939"],["1","0"]],"vk_z":[["14036484800577492864176055863563743801312918031455705713947899654164747095274","17621798242161700147652033360082706311851117228822901176511817790342197240076"],["16373804800800879502996680064240926365601097587279049944810167207060925171284","11833921311104078902370088937110388846651781339933440468666548707337992518688"],["1","0"]]}