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.

138 lines
3.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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. include "compconstant.circom";
  16. include "pointbits.circom";
  17. include "pedersen.circom";
  18. include "escalarmulany.circom";
  19. include "escalarmulfix.circom";
  20. template EdDSAVerifier(n) {
  21. signal input msg[n];
  22. signal input A[256];
  23. signal input R8[256];
  24. signal input S[256];
  25. signal Ax;
  26. signal Ay;
  27. signal R8x;
  28. signal R8y;
  29. var i;
  30. // Ensure S<Subgroup Order
  31. component compConstant = CompConstant(2736030358979909402780800718157159386076813972158567259200215660948447373040);
  32. for (i=0; i<254; i++) {
  33. S[i] ==> compConstant.in[i];
  34. }
  35. compConstant.out === 0;
  36. S[254] === 0;
  37. S[255] === 0;
  38. // Convert A to Field elements (And verify A)
  39. component bits2pointA = Bits2Point_Strict();
  40. for (i=0; i<256; i++) {
  41. bits2pointA.in[i] <== A[i];
  42. }
  43. Ax <== bits2pointA.out[0];
  44. Ay <== bits2pointA.out[1];
  45. // Convert R8 to Field elements (And verify R8)
  46. component bits2pointR8 = Bits2Point_Strict();
  47. for (i=0; i<256; i++) {
  48. bits2pointR8.in[i] <== R8[i];
  49. }
  50. R8x <== bits2pointR8.out[0];
  51. R8y <== bits2pointR8.out[1];
  52. // Calculate the h = H(R,A, msg)
  53. component hash = Pedersen(512+n);
  54. for (i=0; i<256; i++) {
  55. hash.in[i] <== R8[i];
  56. hash.in[256+i] <== A[i];
  57. }
  58. for (i=0; i<n; i++) {
  59. hash.in[512+i] <== msg[i];
  60. }
  61. component point2bitsH = Point2Bits_Strict();
  62. point2bitsH.in[0] <== hash.out[0];
  63. point2bitsH.in[1] <== hash.out[1];
  64. // Calculate second part of the right side: right2 = h*8*A
  65. // Multiply by 8 by adding it 3 times. This also ensure that the result is in
  66. // the subgroup.
  67. component dbl1 = BabyDbl();
  68. dbl1.x <== Ax;
  69. dbl1.y <== Ay;
  70. component dbl2 = BabyDbl();
  71. dbl2.x <== dbl1.xout;
  72. dbl2.y <== dbl1.yout;
  73. component dbl3 = BabyDbl();
  74. dbl3.x <== dbl2.xout;
  75. dbl3.y <== dbl2.yout;
  76. // We check that A is not zero.
  77. component isZero = IsZero();
  78. isZero.in <== dbl3.x;
  79. isZero.out === 0;
  80. component mulAny = EscalarMulAny(256);
  81. for (i=0; i<256; i++) {
  82. mulAny.e[i] <== point2bitsH.out[i];
  83. }
  84. mulAny.p[0] <== dbl3.xout;
  85. mulAny.p[1] <== dbl3.yout;
  86. // Compute the right side: right = R8 + right2
  87. component addRight = BabyAdd();
  88. addRight.x1 <== R8x;
  89. addRight.y1 <== R8y;
  90. addRight.x2 <== mulAny.out[0];
  91. addRight.y2 <== mulAny.out[1];
  92. // Calculate left side of equation left = S*B8
  93. var BASE8 = [
  94. 5299619240641551281634865583518297030282874472190772894086521144482721001553,
  95. 16950150798460657717958625567821834550301663161624707787222815936182638968203
  96. ];
  97. component mulFix = EscalarMulFix(256, BASE8);
  98. for (i=0; i<256; i++) {
  99. mulFix.e[i] <== S[i];
  100. }
  101. // Do the comparation left == right
  102. mulFix.out[0] === addRight.xout;
  103. mulFix.out[1] === addRight.yout;
  104. }