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.

182 lines
5.5 KiB

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 free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) 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
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with zksnark javascript library. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. const fUtils = require("./futils.js");
  16. class GCurve {
  17. constructor(F, g) {
  18. this.F = F;
  19. this.g = [F.copy(g[0]), F.copy(g[1])];
  20. if (this.g.length == 2) this.g[2] = this.F.one;
  21. this.zero = [this.F.zero, this.F.one, this.F.zero];
  22. }
  23. isZero(p) {
  24. return this.F.isZero(p[2]);
  25. }
  26. add(p1, p2) {
  27. if (this.isZero(p1)) return p2;
  28. if (this.isZero(p2)) return p1;
  29. const res = new Array(3);
  30. const Z1Z1 = this.F.square( p1[2] );
  31. const Z2Z2 = this.F.square( p2[2] );
  32. const U1 = this.F.mul( p1[0] , Z2Z2 ); // U1 = X1 * Z2Z2
  33. const U2 = this.F.mul( p2[0] , Z1Z1 ); // U2 = X2 * Z1Z1
  34. const Z1_cubed = this.F.mul( p1[2] , Z1Z1);
  35. const Z2_cubed = this.F.mul( p2[2] , Z2Z2);
  36. const S1 = this.F.mul( p1[1] , Z2_cubed); // S1 = Y1 * Z2 * Z2Z2
  37. const S2 = this.F.mul( p2[1] , Z1_cubed); // S2 = Y2 * Z1 * Z1Z1
  38. if (this.F.equals(U1,U2) && this.F.equals(S1,S2)) {
  39. return this.double(p1);
  40. }
  41. const H = this.F.sub( U2 , U1 ); // H = U2-U1
  42. const S2_minus_S1 = this.F.sub( S2 , S1 );
  43. const I = this.F.square( this.F.add(H,H) ); // I = (2 * H)^2
  44. const J = this.F.mul( H , I ); // J = H * I
  45. const r = this.F.add( S2_minus_S1 , S2_minus_S1 ); // r = 2 * (S2-S1)
  46. const V = this.F.mul( U1 , I ); // V = U1 * I
  47. res[0] =
  48. this.F.sub(
  49. this.F.sub( this.F.square(r) , J ),
  50. this.F.add( V , V )); // X3 = r^2 - J - 2 * V
  51. const S1_J = this.F.mul( S1 , J );
  52. res[1] =
  53. this.F.sub(
  54. this.F.mul( r , this.F.sub(V,res[0])),
  55. this.F.add( S1_J,S1_J )); // Y3 = r * (V-X3)-2 S1 J
  56. res[2] =
  57. this.F.mul(
  58. H,
  59. this.F.sub(
  60. this.F.square( this.F.add(p1[2],p2[2]) ),
  61. this.F.add( Z1Z1 , Z2Z2 ))); // Z3 = ((Z1+Z2)^2-Z1Z1-Z2Z2) * H
  62. return res;
  63. }
  64. neg(p) {
  65. return [p[0], this.F.neg(p[1]), p[2]];
  66. }
  67. sub(a, b) {
  68. return this.add(a, this.neg(b));
  69. }
  70. double(p) {
  71. const res = new Array(3);
  72. if (this.isZero(p)) return p;
  73. const A = this.F.square( p[0] ); // A = X1^2
  74. const B = this.F.square( p[1] ); // B = Y1^2
  75. const C = this.F.square( B ); // C = B^2
  76. let D =
  77. this.F.sub(
  78. this.F.square( this.F.add(p[0] , B )),
  79. this.F.add( A , C));
  80. D = this.F.add(D,D); // D = 2 * ((X1 + B)^2 - A - C)
  81. const E = this.F.add( this.F.add(A,A), A); // E = 3 * A
  82. const F = this.F.square( E ); // F = E^2
  83. res[0] = this.F.sub( F , this.F.add(D,D) ); // X3 = F - 2 D
  84. let eightC = this.F.add( C , C );
  85. eightC = this.F.add( eightC , eightC );
  86. eightC = this.F.add( eightC , eightC );
  87. res[1] =
  88. this.F.sub(
  89. this.F.mul(
  90. E,
  91. this.F.sub( D, res[0] )),
  92. eightC); // Y3 = E * (D - X3) - 8 * C
  93. const Y1Z1 = this.F.mul( p[1] , p[2] );
  94. res[2] = this.F.add( Y1Z1 , Y1Z1 ); // Z3 = 2 * Y1 * Z1
  95. return res;
  96. }
  97. mulScalar(base, e) {
  98. return fUtils.mulScalar(this, base, e);
  99. }
  100. affine(p) {
  101. if (this.isZero(p)) {
  102. return this.zero;
  103. } else {
  104. const Z_inv = this.F.inverse(p[2]);
  105. const Z2_inv = this.F.square(Z_inv);
  106. const Z3_inv = this.F.mul(Z2_inv, Z_inv);
  107. const res = new Array(3);
  108. res[0] = this.F.affine( this.F.mul(p[0],Z2_inv));
  109. res[1] = this.F.affine( this.F.mul(p[1],Z3_inv));
  110. res[2] = this.F.one;
  111. return res;
  112. }
  113. }
  114. equals(p1, p2) {
  115. if (this.isZero(p1)) return this.isZero(p2);
  116. if (this.isZero(p2)) return this.isZero(p1);
  117. const Z1Z1 = this.F.square( p1[2] );
  118. const Z2Z2 = this.F.square( p2[2] );
  119. const U1 = this.F.mul( p1[0] , Z2Z2 );
  120. const U2 = this.F.mul( p2[0] , Z1Z1 );
  121. const Z1_cubed = this.F.mul( p1[2] , Z1Z1);
  122. const Z2_cubed = this.F.mul( p2[2] , Z2Z2);
  123. const S1 = this.F.mul( p1[1] , Z2_cubed);
  124. const S2 = this.F.mul( p2[1] , Z1_cubed);
  125. return (this.F.equals(U1,U2) && this.F.equals(S1,S2));
  126. }
  127. toString(p) {
  128. const cp = this.affine(p);
  129. return `[ ${this.F.toString(cp[0])} , ${this.F.toString(cp[1])} ]`;
  130. }
  131. }
  132. module.exports = GCurve;