_u64.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.add5L = exports.add5H = exports.add4H = exports.add4L = exports.add3H = exports.add3L = exports.add = exports.rotlBL = exports.rotlBH = exports.rotlSL = exports.rotlSH = exports.rotr32L = exports.rotr32H = exports.rotrBL = exports.rotrBH = exports.rotrSL = exports.rotrSH = exports.shrSL = exports.shrSH = exports.toBig = exports.split = exports.fromBig = void 0;
  4. const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
  5. const _32n = /* @__PURE__ */ BigInt(32);
  6. // We are not using BigUint64Array, because they are extremely slow as per 2022
  7. function fromBig(n, le = false) {
  8. if (le)
  9. return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
  10. return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
  11. }
  12. exports.fromBig = fromBig;
  13. function split(lst, le = false) {
  14. let Ah = new Uint32Array(lst.length);
  15. let Al = new Uint32Array(lst.length);
  16. for (let i = 0; i < lst.length; i++) {
  17. const { h, l } = fromBig(lst[i], le);
  18. [Ah[i], Al[i]] = [h, l];
  19. }
  20. return [Ah, Al];
  21. }
  22. exports.split = split;
  23. const toBig = (h, l) => (BigInt(h >>> 0) << _32n) | BigInt(l >>> 0);
  24. exports.toBig = toBig;
  25. // for Shift in [0, 32)
  26. const shrSH = (h, _l, s) => h >>> s;
  27. exports.shrSH = shrSH;
  28. const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
  29. exports.shrSL = shrSL;
  30. // Right rotate for Shift in [1, 32)
  31. const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
  32. exports.rotrSH = rotrSH;
  33. const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
  34. exports.rotrSL = rotrSL;
  35. // Right rotate for Shift in (32, 64), NOTE: 32 is special case.
  36. const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
  37. exports.rotrBH = rotrBH;
  38. const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
  39. exports.rotrBL = rotrBL;
  40. // Right rotate for shift===32 (just swaps l&h)
  41. const rotr32H = (_h, l) => l;
  42. exports.rotr32H = rotr32H;
  43. const rotr32L = (h, _l) => h;
  44. exports.rotr32L = rotr32L;
  45. // Left rotate for Shift in [1, 32)
  46. const rotlSH = (h, l, s) => (h << s) | (l >>> (32 - s));
  47. exports.rotlSH = rotlSH;
  48. const rotlSL = (h, l, s) => (l << s) | (h >>> (32 - s));
  49. exports.rotlSL = rotlSL;
  50. // Left rotate for Shift in (32, 64), NOTE: 32 is special case.
  51. const rotlBH = (h, l, s) => (l << (s - 32)) | (h >>> (64 - s));
  52. exports.rotlBH = rotlBH;
  53. const rotlBL = (h, l, s) => (h << (s - 32)) | (l >>> (64 - s));
  54. exports.rotlBL = rotlBL;
  55. // JS uses 32-bit signed integers for bitwise operations which means we cannot
  56. // simple take carry out of low bit sum by shift, we need to use division.
  57. function add(Ah, Al, Bh, Bl) {
  58. const l = (Al >>> 0) + (Bl >>> 0);
  59. return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
  60. }
  61. exports.add = add;
  62. // Addition with more than 2 elements
  63. const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
  64. exports.add3L = add3L;
  65. const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
  66. exports.add3H = add3H;
  67. const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
  68. exports.add4L = add4L;
  69. const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
  70. exports.add4H = add4H;
  71. const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
  72. exports.add5L = add5L;
  73. const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
  74. exports.add5H = add5H;
  75. // prettier-ignore
  76. const u64 = {
  77. fromBig, split, toBig,
  78. shrSH, shrSL,
  79. rotrSH, rotrSL, rotrBH, rotrBL,
  80. rotr32H, rotr32L,
  81. rotlSH, rotlSL, rotlBH, rotlBL,
  82. add, add3L, add3H, add4L, add4H, add5H, add5L,
  83. };
  84. exports.default = u64;
  85. //# sourceMappingURL=_u64.js.map