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.

66 lines
1.7 KiB

  1. const bigInt = require("../src/bigint.js");
  2. const ZqField = require("../src/zqfield.js");
  3. const r = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  4. const s = 28;
  5. const nqr_to_t = bigInt("19103219067921713944291392827692070036145651957329286315305642004821462161904");
  6. const t_minus_1_over_2 = bigInt("40770029410420498293352137776570907027550720424234931066070132305055");
  7. const root_unity = bigInt("19103219067921713944291392827692070036145651957329286315305642004821462161904");
  8. const t = bigInt("81540058820840996586704275553141814055101440848469862132140264610111");
  9. const F = new ZqField(r);
  10. function sqrt(a) {
  11. let v = s;
  12. let z = nqr_to_t;
  13. let w = F.exp(a, t_minus_1_over_2);
  14. let x = F.mul(a, w);
  15. let b = F.mul(x, w);
  16. // compute square root with Tonelli--Shanks
  17. // (does not terminate if not a square!)
  18. while (!F.equals(b, F.one))
  19. {
  20. let m = 0;
  21. let b2m = b;
  22. while (!F.equals(b2m, F.one))
  23. {
  24. /* invariant: b2m = b^(2^m) after entering this loop */
  25. b2m = F.square(b2m);
  26. m += 1;
  27. }
  28. let j = v-m-1;
  29. w = z;
  30. while (j > 0)
  31. {
  32. w = F.square(w);
  33. --j;
  34. } // w = z^2^(v-m-1)
  35. z = F.square(w);
  36. b = F.mul(b, z);
  37. x = F.mul(x, w);
  38. v = m;
  39. }
  40. return x;
  41. }
  42. const p_minus1= F.sub(r,bigInt(1));
  43. const gen = bigInt(bigInt(5));
  44. const twoto28= F.exp(bigInt(2), bigInt(28));
  45. const rem = F.div(p_minus1, twoto28);
  46. const w28 = F.exp(gen, rem);
  47. const one = F.exp(w28, twoto28);
  48. console.log(F.toString(w28));
  49. console.log(w28.toString(10));
  50. console.log(F.toString(one));