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.

142 lines
3.1 KiB

  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. pragma circom 2.0.0;
  22. template Edwards2Montgomery() {
  23. signal input in[2];
  24. signal output out[2];
  25. out[0] <-- (1 + in[1]) / (1 - in[1]);
  26. out[1] <-- out[0] / in[0];
  27. out[0] * (1-in[1]) === (1 + in[1]);
  28. out[1] * in[0] === out[0];
  29. }
  30. /*
  31. u u - 1
  32. [x, y] = [ ---, ------- ]
  33. v u + 1
  34. */
  35. template Montgomery2Edwards() {
  36. signal input in[2];
  37. signal output out[2];
  38. out[0] <-- in[0] / in[1];
  39. out[1] <-- (in[0] - 1) / (in[0] + 1);
  40. out[0] * in[1] === in[0];
  41. out[1] * (in[0] + 1) === in[0] - 1;
  42. }
  43. /*
  44. x2 - x1
  45. lamda = ---------
  46. y2 - y1
  47. x3 + A + x1 + x2
  48. x3 = B * lamda^2 - A - x1 -x2 => lamda^2 = ------------------
  49. B
  50. y3 = (2*x1 + x2 + A)*lamda - B*lamda^3 - y1 =>
  51. => y3 = lamda * ( 2*x1 + x2 + A - x3 - A - x1 - x2) - y1 =>
  52. => y3 = lamda * ( x1 - x3 ) - y1
  53. ----------
  54. y2 - y1
  55. lamda = ---------
  56. x2 - x1
  57. x3 = B * lamda^2 - A - x1 -x2
  58. y3 = lamda * ( x1 - x3 ) - y1
  59. */
  60. template MontgomeryAdd() {
  61. signal input in1[2];
  62. signal input in2[2];
  63. signal output out[2];
  64. var a = 168700;
  65. var d = 168696;
  66. var A = (2 * (a + d)) / (a - d);
  67. var B = 4 / (a - d);
  68. signal lamda;
  69. lamda <-- (in2[1] - in1[1]) / (in2[0] - in1[0]);
  70. lamda * (in2[0] - in1[0]) === (in2[1] - in1[1]);
  71. out[0] <== B*lamda*lamda - A - in1[0] -in2[0];
  72. out[1] <== lamda * (in1[0] - out[0]) - in1[1];
  73. }
  74. /*
  75. x1_2 = x1*x1
  76. 3*x1_2 + 2*A*x1 + 1
  77. lamda = ---------------------
  78. 2*B*y1
  79. x3 = B * lamda^2 - A - x1 -x1
  80. y3 = lamda * ( x1 - x3 ) - y1
  81. */
  82. template MontgomeryDouble() {
  83. signal input in[2];
  84. signal output out[2];
  85. var a = 168700;
  86. var d = 168696;
  87. var A = (2 * (a + d)) / (a - d);
  88. var B = 4 / (a - d);
  89. signal lamda;
  90. signal x1_2;
  91. x1_2 <== in[0] * in[0];
  92. lamda <-- (3*x1_2 + 2*A*in[0] + 1 ) / (2*B*in[1]);
  93. lamda * (2*B*in[1]) === (3*x1_2 + 2*A*in[0] + 1 );
  94. out[0] <== B*lamda*lamda - A - 2*in[0];
  95. out[1] <== lamda * (in[0] - out[0]) - in[1];
  96. }