snarkjs/test/zksnark.js
test/zksnark.jsRaw
1/*
2 Copyright 2018 0kims association.
3
4 This file is part of zksnark JavaScript library.
5
6 zksnark JavaScript library is a free software: you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by the
8 Free Software Foundation, either version 3 of the License, or (at your option)
9 any later version.
10
11 zksnark JavaScript library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU General Public License along with
17 zksnark JavaScript library. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20const chai = require("chai");
21const fs = require("fs");
22const path = require("path");
23const bigInt = require("../src/bigint.js");
24
25const Circuit = require("../src/circuit.js");
26const zkSnark = require("../index.js");
27const BN128 = require("../src/bn128.js");
28const PolField = require("../src/polfield.js");
29const ZqField = require("../src/zqfield.js");
30
31const {stringifyBigInts, unstringifyBigInts} = require("../src/stringifybigint.js");
32
33const bn128 = new BN128();
34const PolF = new PolField(new ZqField(bn128.r));
35const G1 = bn128.G1;
36const G2 = bn128.G2;
37
38const assert = chai.assert;
39
40
41
42
43
44describe("zkSnark", () => {
45 it("Load a circuit, create trusted setup, create a proof and validate it", () => {
46
47
48 const cirDef = JSON.parse(fs.readFileSync(path.join(__dirname, "circuit", "sum.json"), "utf8"));
49 const cir = new Circuit(cirDef);
50
51 const setup = zkSnark.setup(cir);
52 const strSetup = stringifyBigInts(setup);
53 fs.writeFileSync("vk_proof.json", JSON.stringify(strSetup.vk_proof), "utf-8");
54 fs.writeFileSync("vk_verifier.json", JSON.stringify(strSetup.vk_verifier), "utf-8");
55
56
57 function polT2S(p) {
58 const p_T = new Array(setup.vk_proof.domainSize).fill(bigInt(0));
59
60 for (let c in p) {
61 p_T[c] = p[c];
62 }
63
64 return PolF.ifft(p_T);
65 }
66
67/*
68 const setup = {};
69 setup.vk_proof = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_proof.json", "utf8")));
70 setup.vk_verifier = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_verifier.json", "utf8")));
71*/
72 const witness = cir.calculateWitness({"a": "33", "b": "34"});
73
74 const {proof, publicSignals} = zkSnark.genProof(setup.vk_proof, witness);
75
76/*
77 const polA = new Array(cir.nVars);
78 const polB = new Array(cir.nVars);
79 const polC = new Array(cir.nVars);
80 for (let i=0; i<cir.nVars; i++) {
81 polA[i] = polT2S(setup.vk_proof.polsA[i]);
82 polB[i] = polT2S(setup.vk_proof.polsB[i]);
83 polC[i] = polT2S(setup.vk_proof.polsC[i]);
84 }
85
86 PolF._setRoots(setup.vk_proof.domainBits);
87 for (let c=0; c<setup.vk_proof.domainSize; c++) {
88 let A = bigInt(0);
89 let B = bigInt(0);
90 let C = bigInt(0);
91 for (let s=0; s<cir.nVars; s++) {
92 A = PolF.F.add(A, PolF.F.mul(PolF.eval(polA[s], PolF.roots[setup.vk_proof.domainBits][c]), witness[s]));
93 B = PolF.F.add(B, PolF.F.mul(PolF.eval(polB[s], PolF.roots[setup.vk_proof.domainBits][c]), witness[s]));
94 C = PolF.F.add(C, PolF.F.mul(PolF.eval(polC[s], PolF.roots[setup.vk_proof.domainBits][c]), witness[s]));
95 }
96 assert(PolF.F.equals(PolF.F.mul(A,B), C));
97 }
98
99 let A = bigInt(0);
100 let B = bigInt(0);
101 let C = bigInt(0);
102 for (let s=0; s<cir.nVars; s++) {
103 A = PolF.F.add(A, PolF.F.mul(PolF.eval(polA[s], setup.toxic.t), witness[s]));
104 B = PolF.F.add(B, PolF.F.mul(PolF.eval(polB[s], setup.toxic.t), witness[s]));
105 C = PolF.F.add(C, PolF.F.mul(PolF.eval(polC[s], setup.toxic.t), witness[s]));
106 }
107
108 let A2 = bigInt(0);
109 let B2 = bigInt(0);
110 let C2 = bigInt(0);
111 const u = PolF.evaluateLagrangePolynomials(setup.vk_proof.domainBits, setup.toxic.t);
112 for (let s=0; s<cir.nVars; s++) {
113 let at = PolF.F.zero;
114 for (let c in setup.vk_proof.polsA[s]) {
115 at = PolF.F.add(at, PolF.F.mul(u[c], setup.vk_proof.polsA[s][c]));
116 }
117 A2 = PolF.F.add(A2, PolF.F.mul(at, witness[s]));
118
119 let bt = PolF.F.zero;
120 for (let c in setup.vk_proof.polsB[s]) {
121 bt = PolF.F.add(bt, PolF.F.mul(u[c], setup.vk_proof.polsB[s][c]));
122 }
123 B2 = PolF.F.add(B2, PolF.F.mul(bt, witness[s]));
124
125 let ct = PolF.F.zero;
126 for (let c in setup.vk_proof.polsC[s]) {
127 ct = PolF.F.add(ct, PolF.F.mul(u[c], setup.vk_proof.polsC[s][c]));
128 }
129 C2 = PolF.F.add(C2, PolF.F.mul(ct, witness[s]));
130 }
131
132 A=PolF.F.affine(A);
133 B=PolF.F.affine(B);
134 C=PolF.F.affine(C);
135 A2=PolF.F.affine(A2);
136 B2=PolF.F.affine(B2);
137 C2=PolF.F.affine(C2);
138
139 assert(PolF.F.equals(C,C2));
140 assert(PolF.F.equals(B,B2));
141 assert(PolF.F.equals(A,A2));
142 const ABC = PolF.F.affine(PolF.F.sub(PolF.F.mul(A,B), C));
143
144
145
146 assert.equal(witness[cir.getSignalIdx("main.out")].toString(), "67");
147
148 const H = PolF.eval(proof.h, setup.toxic.t).affine();
149
150 const Z = PolF.F.sub(PolF.F.exp(setup.toxic.t, setup.vk_proof.domainSize), bigInt(1));
151
152 const HZ = PolF.F.affine(PolF.F.mul(H,Z));
153 assert(PolF.F.equals(ABC, HZ));
154
155
156 const gH = G1.affine(G1.mulScalar( G1.g, H));
157 const gZ = G2.affine(G2.mulScalar( G2.g, Z));
158 const gA = G1.affine(G1.mulScalar( G1.g, A));
159 const gB = G2.affine(G2.mulScalar( G2.g, B));
160 const gC = G1.affine(G1.mulScalar( G1.g, C));
161
162 assert(G1.equals(gH, proof.pi_h));
163 assert(G2.equals(gZ, setup.vk_verifier.vk_z));
164 assert(G2.equals(gB, proof.pi_b));
165 assert(G1.equals(gC, proof.pi_c));
166// assert(G1.equals(gA, proof.pi_a));
167*/
168 assert( zkSnark.isValid(setup.vk_verifier, proof, publicSignals));
169 }).timeout(10000000);
170/*
171 it("validate sha256_2", () => {
172
173 const cirDef = JSON.parse(fs.readFileSync(path.join(__dirname, "circuit", "sha256_2.json"), "utf8"));
174 const cir = new Circuit(cirDef);
175
176 console.log("Start setup: "+Date().toString());
177 const setup = zkSnark.setup(cir);
178 const strSetup = stringifyBigInts(setup);
179 fs.writeFileSync("sha256_2_vk_proof.json", JSON.stringify(strSetup.vk_proof), "utf-8");
180 fs.writeFileSync("sha256_2_vk_verifier.json", JSON.stringify(strSetup.vk_verifier), "utf-8");
181
182
183// const setup = {};
184// setup.vk_proof = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_proof.json", "utf8")));
185// setup.vk_verifier = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_verifier.json", "utf8")));
186
187 const witness = cir.calculateWitness({"a": "1", "b": "2"});
188
189// assert.equal(witness[cir.getSignalIdx("main.out")].toString(), "67");
190
191 console.log("Start calculating the proof: "+Date().toString());
192 const {proof, publicSignals} = zkSnark.genProof(setup.vk_proof, witness);
193
194 console.log("Start verifiying: "+ Date().toString());
195 assert( zkSnark.isValid(setup.vk_verifier, proof, publicSignals));
196 }).timeout(10000000);
197*/
198
199});