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

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