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.

141 lines
3.1 KiB

5 years ago
  1. /*
  2. Copyright 2018 0KIMS association.
  3. This file is part of circom (Zero Knowledge Circuit Compiler).
  4. circom is a free software: you can redistribute it and/or modify it
  5. 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. circom is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with circom. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. Source: https://en.wikipedia.org/wiki/Montgomery_curve
  17. 1 + y 1 + y
  18. [u, v] = [ ------- , ---------- ]
  19. 1 - y (1 - y)x
  20. */
  21. template Edwards2Montgomery() {
  22. signal input in[2];
  23. signal output out[2];
  24. out[0] <-- (1 + in[1]) / (1 - in[1]);
  25. out[1] <-- out[0] / in[0];
  26. out[0] * (1-in[1]) === (1 + in[1]);
  27. out[1] * in[0] === out[0];
  28. }
  29. /*
  30. u u - 1
  31. [x, y] = [ ---, ------- ]
  32. v u + 1
  33. */
  34. template Montgomery2Edwards() {
  35. signal input in[2];
  36. signal output out[2];
  37. out[0] <-- in[0] / in[1];
  38. out[1] <-- (in[0] - 1) / (in[0] + 1);
  39. out[0] * in[1] === in[0];
  40. out[1] * (in[0] + 1) === in[0] - 1;
  41. }
  42. /*
  43. x2 - x1
  44. lamda = ---------
  45. y2 - y1
  46. x3 + A + x1 + x2
  47. x3 = B * lamda^2 - A - x1 -x2 => lamda^2 = ------------------
  48. B
  49. y3 = (2*x1 + x2 + A)*lamda - B*lamda^3 - y1 =>
  50. => y3 = lamda * ( 2*x1 + x2 + A - x3 - A - x1 - x2) - y1 =>
  51. => y3 = lamda * ( x1 - x3 ) - y1
  52. ----------
  53. y2 - y1
  54. lamda = ---------
  55. x2 - x1
  56. x3 = B * lamda^2 - A - x1 -x2
  57. y3 = lamda * ( x1 - x3 ) - y1
  58. */
  59. template MontgomeryAdd() {
  60. signal input in1[2];
  61. signal input in2[2];
  62. signal output out[2];
  63. var a = 168700;
  64. var d = 168696;
  65. var A = (2 * (a + d)) / (a - d);
  66. var B = 4 / (a - d);
  67. signal lamda;
  68. lamda <-- (in2[1] - in1[1]) / (in2[0] - in1[0]);
  69. lamda * (in2[0] - in1[0]) === (in2[1] - in1[1]);
  70. out[0] <== B*lamda*lamda - A - in1[0] -in2[0];
  71. out[1] <== lamda * (in1[0] - out[0]) - in1[1];
  72. }
  73. /*
  74. x1_2 = x1*x1
  75. 3*x1_2 + 2*A*x1 + 1
  76. lamda = ---------------------
  77. 2*B*y1
  78. x3 = B * lamda^2 - A - x1 -x1
  79. y3 = lamda * ( x1 - x3 ) - y1
  80. */
  81. template MontgomeryDouble() {
  82. signal input in[2];
  83. signal output out[2];
  84. var a = 168700;
  85. var d = 168696;
  86. var A = (2 * (a + d)) / (a - d);
  87. var B = 4 / (a - d);
  88. signal lamda;
  89. signal x1_2;
  90. x1_2 <== in[0] * in[0];
  91. lamda <-- (3*x1_2 + 2*A*in[0] + 1 ) / (2*B*in[1]);
  92. lamda * (2*B*in[1]) === (3*x1_2 + 2*A*in[0] + 1 );
  93. out[0] <== B*lamda*lamda - A - 2*in[0];
  94. out[1] <== lamda * (in[0] - out[0]) - in[1];
  95. }