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.

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