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.

160 lines
4.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. const bigInt = require("big-integer");
  2. const ZnField = require("./znfield.js");
  3. const PolField = require("./polfield.js");
  4. const G1Curve = require("./g1curve");
  5. const G2Curve = require("./g2curve");
  6. const F = new ZnField(bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617"));
  7. const PolF = new PolField(F);
  8. const G1 = new G1Curve();
  9. const G2 = new G2Curve();
  10. module.exports = function setup(circuit) {
  11. const setup = {
  12. vk_proof : {
  13. nSignals: circuit.nSignals,
  14. nPublic: circuit.nPublic
  15. },
  16. vk_verifier: {
  17. nPublic: circuit.nPublic
  18. },
  19. toxic: {}
  20. };
  21. calculatePolinomials(setup, circuit);
  22. setup.toxic.t = F.random();
  23. calculateEncriptedValuesAtT(setup, circuit);
  24. calculateHexps(setup, circuit);
  25. };
  26. function calculatePolinomials(setup, circuit) {
  27. // Calculate the points that must cross each polinomial
  28. const aPoints = [];
  29. const bPoints = [];
  30. const cPoints = [];
  31. for (let s = 0; circuit.nSignals; s++) {
  32. aPoints[s] = [];
  33. bPoints[s] = [];
  34. cPoints[s] = [];
  35. for (let c=0; c<circuit.nConstrains; c++) {
  36. aPoints[s].push([bigInt(c), circuit.a(c, s)]);
  37. bPoints[s].push([bigInt(c), circuit.b(c, s)]);
  38. cPoints[s].push([bigInt(c), circuit.c(c, s)]);
  39. }
  40. }
  41. // Calculate the polinomials using Lagrange
  42. setup.vk_proof.polsA = [];
  43. setup.vk_proof.polsB = [];
  44. setup.vk_proof.polsC = [];
  45. for (let s=0; s<circuit.nSignals; s++) {
  46. setup.vk_proof.polsA.push(PolF.lagrange( aPoints[s] ));
  47. setup.vk_proof.polsB.push(PolF.lagrange( bPoints[s] ));
  48. setup.vk_proof.polsC.push(PolF.lagrange( cPoints[s] ));
  49. }
  50. // Calculate Z polinomial
  51. // Z = 1
  52. setup.vk_proof.polZ = [bigInt(1)];
  53. for (let c=0; c<circuit.nConstrains; c++) {
  54. // Z = Z * (x - p_c)
  55. setup.vk_proof.polZ = PolF.mul(
  56. setup.vk_proof.polZ,
  57. [F.neg(bigInt(c)), bigInt(1)] );
  58. }
  59. }
  60. function calculateEncriptedValuesAtT(setup, circuit) {
  61. setup.vk_proof.A = [];
  62. setup.vk_proof.B = [];
  63. setup.vk_proof.C = [];
  64. setup.vk_proof.Ap = [];
  65. setup.vk_proof.Bp = [];
  66. setup.vk_proof.Cp = [];
  67. setup.vk_proof.Kp = [];
  68. setup.vk_verifier.A = [];
  69. setup.toxic.ka = F.random();
  70. setup.toxic.kb = F.random();
  71. setup.toxic.kc = F.random();
  72. setup.toxic.kbeta = F.random();
  73. setup.toxic.kgamma = F.random();
  74. const gb = F.mul(setup.toxic.kbeta, setup.toxic.kgamma);
  75. setup.vk_verifier.vk_a = G2.mulEscalar( G2.g, setup.toxic.ka);
  76. setup.vk_verifier.vk_b = G1.mulEscalar( G1.g, setup.toxic.kb);
  77. setup.vk_verifier.vk_c = G2.mulEscalar( G2.g, setup.toxic.kc);
  78. setup.vk_verifier.vk_gb_1 = G1.mulEscalar( G1.g, gb);
  79. setup.vk_verifier.vk_gb_2 = G2.mulEscalar( G2.g, gb);
  80. setup.vk_verifier.vk_g = G2.mulEscalar( G2.g, setup.toxic.kgamma);
  81. for (let s=0; s<circuit.nSignals; s++) {
  82. // A[i] = G1 * polA(t)
  83. const A = G1.mulEscalar(
  84. G1.g,
  85. PolF.eval(setup.vk_proof.polsA[s], setup.vk_proof.t));
  86. setup.vk_proof.A.push(A);
  87. if (s < circuit.nPublicSignals) {
  88. setup.vk_verifier.A.pusj(A);
  89. }
  90. // B1[i] = G1 * polB(t)
  91. const B1 = G1.mulEscalar(
  92. G1.g,
  93. PolF.eval(setup.vk_proof.polsB[s], setup.vk_proof.t));
  94. // B2[i] = G2 * polB(t)
  95. const B2 = G2.mulEscalar(
  96. G2.g,
  97. PolF.eval(setup.vk_proof.polsB[s], setup.vk_proof.t));
  98. setup.vk_proof.B.push(B2);
  99. // C[i] = G1 * polC(t)
  100. const C = G1.mulEscalar(
  101. G1.g,
  102. PolF.eval(setup.vk_proof.polsC[s], setup.vk_proof.t));
  103. setup.vk_proof.C.push (C);
  104. // K = G1 * (A+B+C)
  105. const K = G1.mulEscalar(
  106. G1.g,
  107. G1.add(G1.add(A, B1), C));
  108. setup.vk_proof.Ap.push(G1.mulEscalar(A, setup.toxic.ka));
  109. setup.vk_proof.Bp.push(G1.mulEscalar(B1, setup.toxic.kb));
  110. setup.vk_proof.Cp.push(G1.mulEscalar(C, setup.toxic.kc));
  111. setup.vk_proof.Kp.push(G1.mulEscalar(K, setup.toxic.beta));
  112. }
  113. setup.vk_verifier.vk_z = G2.mulEscalar(
  114. G2.g,
  115. PolF.eval(setup.vk_proof.polZ, setup.vk_proof.t));
  116. }
  117. function calculateHexps(setup, circuit) {
  118. let maxA = 0;
  119. let maxB = 0;
  120. let maxC = 0;
  121. for (let s=0; s<circuit.nSignals; s++) {
  122. maxA = Math.max(maxA, setup.vk_proof.polsA[s]);
  123. maxB = Math.max(maxB, setup.vk_proof.polsB[s]);
  124. maxC = Math.max(maxC, setup.vk_proof.polsC[s]);
  125. }
  126. let maxFull = Math.max(maxA * maxB - 1, maxC);
  127. const maxH = maxFull - setup.vk_proof.polZ.length + 1;
  128. setup.vk_proof.hExps = new Array(maxH);
  129. setup.vk_proof.hExps[0] = G1.g;
  130. let eT = setup.toxic.t;
  131. for (let i=1; i<maxH; i++) {
  132. setup.vk_proof.hExps[i] = G1.mulEscalar(G1.g, eT);
  133. eT = F.mul(eT, setup.toxic.t);
  134. }
  135. }