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.

144 lines
2.9 KiB

  1. include "../node_modules/circom/circuits/bitify.circom";
  2. include "aliascheck.circom";
  3. include "compconstant.circom";
  4. include "babyjub.circom";
  5. function sqrt(n) {
  6. if (n == 0) {
  7. return 0;
  8. }
  9. // Test that have solution
  10. var res = n ** ((-1) >> 1);
  11. // if (res!=1) assert(false, "SQRT does not exists");
  12. if (res!=1) return 0;
  13. var m = 28;
  14. var c = 19103219067921713944291392827692070036145651957329286315305642004821462161904;
  15. var t = n ** 81540058820840996586704275553141814055101440848469862132140264610111;
  16. var r = n ** ((81540058820840996586704275553141814055101440848469862132140264610111+1)>>1);
  17. var sq;
  18. var i;
  19. var b;
  20. var j;
  21. while ((r != 0)&&(t != 1)) {
  22. sq = t*t;
  23. i = 1;
  24. while (sq!=1) {
  25. i++;
  26. sq = sq*sq;
  27. }
  28. // b = c ^ m-i-1
  29. b = c;
  30. for (j=0; j< m-i-1; j ++) b = b*b;
  31. m = i;
  32. c = b*b;
  33. t = t*c;
  34. r = r*b;
  35. }
  36. if (r > ((-1) >> 1)) {
  37. r = -r;
  38. }
  39. return r;
  40. }
  41. template Bits2Point() {
  42. signal input in[256];
  43. signal output out[2];
  44. }
  45. template Bits2Point_Strict() {
  46. signal input in[256];
  47. signal output out[2];
  48. var i;
  49. // Check aliasing
  50. component aliasCheckY = AliasCheck();
  51. for (i=0; i<254; i++) {
  52. aliasCheckY.in[i] <== in[i];
  53. }
  54. in[254] === 0;
  55. component b2nY = Bits2Num(254);
  56. for (i=0; i<254; i++) {
  57. b2nY.in[i] <== in[i];
  58. }
  59. out[1] <== b2nY.out;
  60. var a = 168700;
  61. var d = 168696;
  62. var y2 = out[1] * out[1];
  63. var x = sqrt( (1-y2)/(a - d*y2) );
  64. if (in[255] == 1) x = -x;
  65. out[0] <-- x;
  66. component babyCheck = BabyCheck();
  67. babyCheck.x <== out[0];
  68. babyCheck.y <== out[1];
  69. component n2bX = Num2Bits(254);
  70. n2bX.in <== out[0];
  71. component aliasCheckX = AliasCheck();
  72. for (i=0; i<254; i++) {
  73. aliasCheckX.in[i] <== n2bX.out[i];
  74. }
  75. component signCalc = CompConstant(10944121435919637611123202872628637544274182200208017171849102093287904247808);
  76. for (i=0; i<254; i++) {
  77. signCalc.in[i] <== n2bX.out[i];
  78. }
  79. signCalc.out === in[255];
  80. }
  81. template Point2Bits() {
  82. signal input in[2];
  83. signal output out[256];
  84. }
  85. template Point2Bits_Strict() {
  86. signal input in[2];
  87. signal output out[256];
  88. var i;
  89. component n2bX = Num2Bits(254);
  90. n2bX.in <== in[0];
  91. component n2bY = Num2Bits(254);
  92. n2bY.in <== in[1];
  93. component aliasCheckX = AliasCheck();
  94. component aliasCheckY = AliasCheck();
  95. for (i=0; i<254; i++) {
  96. aliasCheckX.in[i] <== n2bX.out[i];
  97. aliasCheckY.in[i] <== n2bY.out[i];
  98. }
  99. component signCalc = CompConstant(10944121435919637611123202872628637544274182200208017171849102093287904247808);
  100. for (i=0; i<254; i++) {
  101. signCalc.in[i] <== n2bX.out[i];
  102. }
  103. for (i=0; i<254; i++) {
  104. out[i] <== n2bY.out[i];
  105. }
  106. out[254] <== 0;
  107. out[255] <== signCalc.out;
  108. }