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.

123 lines
3.3 KiB

5 years ago
5 years ago
5 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 "mimc.circom";
  18. include "bitify.circom";
  19. include "escalarmulany.circom";
  20. include "escalarmulfix.circom";
  21. template EdDSAMiMCVerifier() {
  22. signal input enabled;
  23. signal input Ax;
  24. signal input Ay;
  25. signal input S;
  26. signal input R8x;
  27. signal input R8y;
  28. signal input M;
  29. var i;
  30. // Ensure S<Subgroup Order
  31. component snum2bits = Num2Bits(253);
  32. snum2bits.in <== S;
  33. component compConstant = CompConstant(2736030358979909402780800718157159386076813972158567259200215660948447373040);
  34. for (i=0; i<253; i++) {
  35. snum2bits.out[i] ==> compConstant.in[i];
  36. }
  37. compConstant.in[253] <== 0;
  38. compConstant.out === 0;
  39. // Calculate the h = H(R,A, msg)
  40. component hash = MultiMiMC7(5, 91);
  41. hash.in[0] <== R8x;
  42. hash.in[1] <== R8y;
  43. hash.in[2] <== Ax;
  44. hash.in[3] <== Ay;
  45. hash.in[4] <== M;
  46. hash.k <== 0;
  47. component h2bits = Num2Bits_strict();
  48. h2bits.in <== hash.out;
  49. // Calculate second part of the right side: right2 = h*8*A
  50. // Multiply by 8 by adding it 3 times. This also ensure that the result is in
  51. // the subgroup.
  52. component dbl1 = BabyDbl();
  53. dbl1.x <== Ax;
  54. dbl1.y <== Ay;
  55. component dbl2 = BabyDbl();
  56. dbl2.x <== dbl1.xout;
  57. dbl2.y <== dbl1.yout;
  58. component dbl3 = BabyDbl();
  59. dbl3.x <== dbl2.xout;
  60. dbl3.y <== dbl2.yout;
  61. // We check that A is not zero.
  62. component isZero = IsZero();
  63. isZero.in <== dbl3.x;
  64. isZero.out === 0;
  65. component mulAny = EscalarMulAny(254);
  66. for (i=0; i<254; i++) {
  67. mulAny.e[i] <== h2bits.out[i];
  68. }
  69. mulAny.p[0] <== dbl3.xout;
  70. mulAny.p[1] <== dbl3.yout;
  71. // Compute the right side: right = R8 + right2
  72. component addRight = BabyAdd();
  73. addRight.x1 <== R8x;
  74. addRight.y1 <== R8y;
  75. addRight.x2 <== mulAny.out[0];
  76. addRight.y2 <== mulAny.out[1];
  77. // Calculate left side of equation left = S*B8
  78. var BASE8 = [
  79. 5299619240641551281634865583518297030282874472190772894086521144482721001553,
  80. 16950150798460657717958625567821834550301663161624707787222815936182638968203
  81. ];
  82. component mulFix = EscalarMulFix(253, BASE8);
  83. for (i=0; i<253; i++) {
  84. mulFix.e[i] <== snum2bits.out[i];
  85. }
  86. // Do the comparation left == right if enabled;
  87. component eqCheckX = ForceEqualIfEnabled();
  88. eqCheckX.enabled <== enabled;
  89. eqCheckX.in[0] <== mulFix.out[0];
  90. eqCheckX.in[1] <== addRight.xout;
  91. component eqCheckY = ForceEqualIfEnabled();
  92. eqCheckY.enabled <== enabled;
  93. eqCheckY.in[0] <== mulFix.out[1];
  94. eqCheckY.in[1] <== addRight.yout;
  95. }