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.

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