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.

122 lines
3.3 KiB

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