You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
8.0 KiB

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