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.

107 lines
2.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 "bitify.circom";
  17. include "escalarmulfix.circom";
  18. template BabyAdd() {
  19. signal input x1;
  20. signal input y1;
  21. signal input x2;
  22. signal input y2;
  23. signal output xout;
  24. signal output yout;
  25. signal beta;
  26. signal gamma;
  27. signal delta;
  28. signal tau;
  29. var a = 168700;
  30. var d = 168696;
  31. beta <== x1*y2;
  32. gamma <== y1*x2;
  33. delta <== (-a*x1+y1)*(x2 + y2);
  34. tau <== beta * gamma;
  35. xout <-- (beta + gamma) / (1+ d*tau);
  36. (1+ d*tau) * xout === (beta + gamma);
  37. yout <-- (delta + a*beta - gamma) / (1-d*tau);
  38. (1-d*tau)*yout === (delta + a*beta - gamma);
  39. }
  40. template BabyDbl() {
  41. signal input x;
  42. signal input y;
  43. signal output xout;
  44. signal output yout;
  45. component adder = BabyAdd();
  46. adder.x1 <== x;
  47. adder.y1 <== y;
  48. adder.x2 <== x;
  49. adder.y2 <== y;
  50. adder.xout ==> xout;
  51. adder.yout ==> yout;
  52. }
  53. template BabyCheck() {
  54. signal input x;
  55. signal input y;
  56. signal x2;
  57. signal y2;
  58. var a = 168700;
  59. var d = 168696;
  60. x2 <== x*x;
  61. y2 <== y*y;
  62. a*x2 + y2 === 1 + d*x2*y2;
  63. }
  64. // Extracts the public key from private key
  65. template BabyPbk() {
  66. signal input in;
  67. signal output Ax;
  68. signal output Ay;
  69. var BASE8[2] = [
  70. 5299619240641551281634865583518297030282874472190772894086521144482721001553,
  71. 16950150798460657717958625567821834550301663161624707787222815936182638968203
  72. ];
  73. component pvkBits = Num2Bits(253);
  74. pvkBits.in <== in;
  75. component mulFix = EscalarMulFix(253, BASE8);
  76. var i;
  77. for (i=0; i<253; i++) {
  78. mulFix.e[i] <== pvkBits.out[i];
  79. }
  80. Ax <== mulFix.out[0];
  81. Ay <== mulFix.out[1];
  82. }