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.

199 lines
7.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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 {stringifyBigInts, unstringifyBigInts} = require("../src/stringifybigint.js");
  25. const bn128 = new BN128();
  26. const PolF = new PolField(new ZqField(bn128.r));
  27. const G1 = bn128.G1;
  28. const G2 = bn128.G2;
  29. const assert = chai.assert;
  30. describe("zkSnark", () => {
  31. it("Load a circuit, create trusted setup, create a proof and validate it", () => {
  32. const cirDef = JSON.parse(fs.readFileSync(path.join(__dirname, "circuit", "sum.json"), "utf8"));
  33. const cir = new Circuit(cirDef);
  34. const setup = zkSnark.setup(cir);
  35. const strSetup = stringifyBigInts(setup);
  36. fs.writeFileSync("vk_proof.json", JSON.stringify(strSetup.vk_proof), "utf-8");
  37. fs.writeFileSync("vk_verifier.json", JSON.stringify(strSetup.vk_verifier), "utf-8");
  38. function polT2S(p) {
  39. const p_T = new Array(setup.vk_proof.domainSize).fill(bigInt(0));
  40. for (let c in p) {
  41. p_T[c] = p[c];
  42. }
  43. return PolF.ifft(p_T);
  44. }
  45. /*
  46. const setup = {};
  47. setup.vk_proof = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_proof.json", "utf8")));
  48. setup.vk_verifier = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_verifier.json", "utf8")));
  49. */
  50. const witness = cir.calculateWitness({"a": "33", "b": "34"});
  51. const {proof, publicSignals} = zkSnark.genProof(setup.vk_proof, witness);
  52. /*
  53. const polA = new Array(cir.nVars);
  54. const polB = new Array(cir.nVars);
  55. const polC = new Array(cir.nVars);
  56. for (let i=0; i<cir.nVars; i++) {
  57. polA[i] = polT2S(setup.vk_proof.polsA[i]);
  58. polB[i] = polT2S(setup.vk_proof.polsB[i]);
  59. polC[i] = polT2S(setup.vk_proof.polsC[i]);
  60. }
  61. PolF._setRoots(setup.vk_proof.domainBits);
  62. for (let c=0; c<setup.vk_proof.domainSize; c++) {
  63. let A = bigInt(0);
  64. let B = bigInt(0);
  65. let C = bigInt(0);
  66. for (let s=0; s<cir.nVars; s++) {
  67. A = PolF.F.add(A, PolF.F.mul(PolF.eval(polA[s], PolF.roots[setup.vk_proof.domainBits][c]), witness[s]));
  68. B = PolF.F.add(B, PolF.F.mul(PolF.eval(polB[s], PolF.roots[setup.vk_proof.domainBits][c]), witness[s]));
  69. C = PolF.F.add(C, PolF.F.mul(PolF.eval(polC[s], PolF.roots[setup.vk_proof.domainBits][c]), witness[s]));
  70. }
  71. assert(PolF.F.equals(PolF.F.mul(A,B), C));
  72. }
  73. let A = bigInt(0);
  74. let B = bigInt(0);
  75. let C = bigInt(0);
  76. for (let s=0; s<cir.nVars; s++) {
  77. A = PolF.F.add(A, PolF.F.mul(PolF.eval(polA[s], setup.toxic.t), witness[s]));
  78. B = PolF.F.add(B, PolF.F.mul(PolF.eval(polB[s], setup.toxic.t), witness[s]));
  79. C = PolF.F.add(C, PolF.F.mul(PolF.eval(polC[s], setup.toxic.t), witness[s]));
  80. }
  81. let A2 = bigInt(0);
  82. let B2 = bigInt(0);
  83. let C2 = bigInt(0);
  84. const u = PolF.evaluateLagrangePolynomials(setup.vk_proof.domainBits, setup.toxic.t);
  85. for (let s=0; s<cir.nVars; s++) {
  86. let at = PolF.F.zero;
  87. for (let c in setup.vk_proof.polsA[s]) {
  88. at = PolF.F.add(at, PolF.F.mul(u[c], setup.vk_proof.polsA[s][c]));
  89. }
  90. A2 = PolF.F.add(A2, PolF.F.mul(at, witness[s]));
  91. let bt = PolF.F.zero;
  92. for (let c in setup.vk_proof.polsB[s]) {
  93. bt = PolF.F.add(bt, PolF.F.mul(u[c], setup.vk_proof.polsB[s][c]));
  94. }
  95. B2 = PolF.F.add(B2, PolF.F.mul(bt, witness[s]));
  96. let ct = PolF.F.zero;
  97. for (let c in setup.vk_proof.polsC[s]) {
  98. ct = PolF.F.add(ct, PolF.F.mul(u[c], setup.vk_proof.polsC[s][c]));
  99. }
  100. C2 = PolF.F.add(C2, PolF.F.mul(ct, witness[s]));
  101. }
  102. A=PolF.F.affine(A);
  103. B=PolF.F.affine(B);
  104. C=PolF.F.affine(C);
  105. A2=PolF.F.affine(A2);
  106. B2=PolF.F.affine(B2);
  107. C2=PolF.F.affine(C2);
  108. assert(PolF.F.equals(C,C2));
  109. assert(PolF.F.equals(B,B2));
  110. assert(PolF.F.equals(A,A2));
  111. const ABC = PolF.F.affine(PolF.F.sub(PolF.F.mul(A,B), C));
  112. assert.equal(witness[cir.getSignalIdx("main.out")].toString(), "67");
  113. const H = PolF.eval(proof.h, setup.toxic.t).affine();
  114. const Z = PolF.F.sub(PolF.F.exp(setup.toxic.t, setup.vk_proof.domainSize), bigInt(1));
  115. const HZ = PolF.F.affine(PolF.F.mul(H,Z));
  116. assert(PolF.F.equals(ABC, HZ));
  117. const gH = G1.affine(G1.mulScalar( G1.g, H));
  118. const gZ = G2.affine(G2.mulScalar( G2.g, Z));
  119. const gA = G1.affine(G1.mulScalar( G1.g, A));
  120. const gB = G2.affine(G2.mulScalar( G2.g, B));
  121. const gC = G1.affine(G1.mulScalar( G1.g, C));
  122. assert(G1.equals(gH, proof.pi_h));
  123. assert(G2.equals(gZ, setup.vk_verifier.vk_z));
  124. assert(G2.equals(gB, proof.pi_b));
  125. assert(G1.equals(gC, proof.pi_c));
  126. // assert(G1.equals(gA, proof.pi_a));
  127. */
  128. assert( zkSnark.isValid(setup.vk_verifier, proof, publicSignals));
  129. }).timeout(10000000);
  130. /*
  131. it("validate sha256_2", () => {
  132. const cirDef = JSON.parse(fs.readFileSync(path.join(__dirname, "circuit", "sha256_2.json"), "utf8"));
  133. const cir = new Circuit(cirDef);
  134. console.log("Start setup: "+Date().toString());
  135. const setup = zkSnark.setup(cir);
  136. const strSetup = stringifyBigInts(setup);
  137. fs.writeFileSync("sha256_2_vk_proof.json", JSON.stringify(strSetup.vk_proof), "utf-8");
  138. fs.writeFileSync("sha256_2_vk_verifier.json", JSON.stringify(strSetup.vk_verifier), "utf-8");
  139. // const setup = {};
  140. // setup.vk_proof = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_proof.json", "utf8")));
  141. // setup.vk_verifier = unstringifyBigInts(JSON.parse(fs.readFileSync("vk_verifier.json", "utf8")));
  142. const witness = cir.calculateWitness({"a": "1", "b": "2"});
  143. // assert.equal(witness[cir.getSignalIdx("main.out")].toString(), "67");
  144. console.log("Start calculating the proof: "+Date().toString());
  145. const {proof, publicSignals} = zkSnark.genProof(setup.vk_proof, witness);
  146. console.log("Start verifiying: "+ Date().toString());
  147. assert( zkSnark.isValid(setup.vk_verifier, proof, publicSignals));
  148. }).timeout(10000000);
  149. */
  150. });