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.

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