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
3.4 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 "mimcsponge.circom";
  19. include "bitify.circom";
  20. include "escalarmulany.circom";
  21. include "escalarmulfix.circom";
  22. template EdDSAMiMCSpongeVerifier() {
  23. signal input enabled;
  24. signal input Ax;
  25. signal input Ay;
  26. signal input S;
  27. signal input R8x;
  28. signal input R8y;
  29. signal input M;
  30. var i;
  31. // Ensure S<Subgroup Order
  32. component snum2bits = Num2Bits(253);
  33. snum2bits.in <== S;
  34. component compConstant = CompConstant(2736030358979909402780800718157159386076813972158567259200215660948447373040);
  35. for (i=0; i<253; i++) {
  36. snum2bits.out[i] ==> compConstant.in[i];
  37. }
  38. compConstant.in[253] <== 0;
  39. compConstant.out === 0;
  40. // Calculate the h = H(R,A, msg)
  41. component hash = MiMCSponge(5, 220, 1);
  42. hash.ins[0] <== R8x;
  43. hash.ins[1] <== R8y;
  44. hash.ins[2] <== Ax;
  45. hash.ins[3] <== Ay;
  46. hash.ins[4] <== M;
  47. hash.k <== 0;
  48. component h2bits = Num2Bits_strict();
  49. h2bits.in <== hash.outs[0];
  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(254);
  67. for (i=0; i<254; i++) {
  68. mulAny.e[i] <== h2bits.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[2] = [
  80. 5299619240641551281634865583518297030282874472190772894086521144482721001553,
  81. 16950150798460657717958625567821834550301663161624707787222815936182638968203
  82. ];
  83. component mulFix = EscalarMulFix(253, BASE8);
  84. for (i=0; i<253; i++) {
  85. mulFix.e[i] <== snum2bits.out[i];
  86. }
  87. // Do the comparation left == right if enabled;
  88. component eqCheckX = ForceEqualIfEnabled();
  89. eqCheckX.enabled <== enabled;
  90. eqCheckX.in[0] <== mulFix.out[0];
  91. eqCheckX.in[1] <== addRight.xout;
  92. component eqCheckY = ForceEqualIfEnabled();
  93. eqCheckY.enabled <== enabled;
  94. eqCheckY.in[0] <== mulFix.out[1];
  95. eqCheckY.in[1] <== addRight.yout;
  96. }