ipaddress 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/dist/src/crunchy.d.ts +130 -0
  2. package/dist/src/crunchy.d.ts.map +1 -0
  3. package/dist/src/crunchy.js +616 -0
  4. package/dist/src/index.d.ts +13 -0
  5. package/dist/src/index.d.ts.map +1 -0
  6. package/dist/src/index.js +21 -0
  7. package/{ip_bits.d.ts → dist/src/ip_bits.d.ts} +6 -7
  8. package/dist/src/ip_bits.d.ts.map +1 -0
  9. package/dist/src/ip_bits.js +133 -0
  10. package/{ip_version.d.ts → dist/src/ip_version.d.ts} +2 -1
  11. package/dist/src/ip_version.d.ts.map +1 -0
  12. package/dist/src/ip_version.js +6 -0
  13. package/{ipaddress.d.ts → dist/src/ipaddress.d.ts} +38 -35
  14. package/dist/src/ipaddress.d.ts.map +1 -0
  15. package/dist/src/ipaddress.js +1243 -0
  16. package/dist/src/ipv4.d.ts +15 -0
  17. package/dist/src/ipv4.d.ts.map +1 -0
  18. package/dist/src/ipv4.js +187 -0
  19. package/dist/src/ipv6.d.ts +13 -0
  20. package/dist/src/ipv6.d.ts.map +1 -0
  21. package/dist/src/ipv6.js +190 -0
  22. package/dist/src/ipv6_loopback.d.ts +6 -0
  23. package/dist/src/ipv6_loopback.d.ts.map +1 -0
  24. package/dist/src/ipv6_loopback.js +51 -0
  25. package/dist/src/ipv6_mapped.d.ts +6 -0
  26. package/dist/src/ipv6_mapped.d.ts.map +1 -0
  27. package/dist/src/ipv6_mapped.js +159 -0
  28. package/dist/src/ipv6_unspec.d.ts +6 -0
  29. package/dist/src/ipv6_unspec.d.ts.map +1 -0
  30. package/dist/src/ipv6_unspec.js +57 -0
  31. package/{prefix.d.ts → dist/src/prefix.d.ts} +10 -7
  32. package/dist/src/prefix.d.ts.map +1 -0
  33. package/dist/src/prefix.js +142 -0
  34. package/dist/src/prefix128.d.ts +7 -0
  35. package/dist/src/prefix128.d.ts.map +1 -0
  36. package/dist/src/prefix128.js +39 -0
  37. package/dist/src/prefix32.d.ts +7 -0
  38. package/dist/src/prefix32.d.ts.map +1 -0
  39. package/dist/src/prefix32.js +29 -0
  40. package/dist/src/rle.d.ts +19 -0
  41. package/dist/src/rle.d.ts.map +1 -0
  42. package/dist/src/rle.js +73 -0
  43. package/dist/test/test_crunchy_impls.d.ts +2 -0
  44. package/dist/test/test_crunchy_impls.d.ts.map +1 -0
  45. package/dist/test/test_crunchy_impls.js +150 -0
  46. package/dist/test/test_crunchy_with32bit.d.ts +2 -0
  47. package/dist/test/test_crunchy_with32bit.d.ts.map +1 -0
  48. package/dist/test/test_crunchy_with32bit.js +382 -0
  49. package/dist/test/test_ipaddress.d.ts +2 -0
  50. package/dist/test/test_ipaddress.d.ts.map +1 -0
  51. package/dist/test/test_ipaddress.js +182 -0
  52. package/dist/test/test_ipv4.d.ts +2 -0
  53. package/dist/test/test_ipv4.d.ts.map +1 -0
  54. package/dist/test/test_ipv4.js +561 -0
  55. package/dist/test/test_ipv6.d.ts +2 -0
  56. package/dist/test/test_ipv6.d.ts.map +1 -0
  57. package/dist/test/test_ipv6.js +313 -0
  58. package/dist/test/test_ipv6_loopback.d.ts +2 -0
  59. package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
  60. package/dist/test/test_ipv6_loopback.js +40 -0
  61. package/dist/test/test_ipv6_mapped.d.ts +2 -0
  62. package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
  63. package/dist/test/test_ipv6_mapped.js +89 -0
  64. package/dist/test/test_ipv6_unspec.d.ts +2 -0
  65. package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
  66. package/dist/test/test_ipv6_unspec.js +39 -0
  67. package/dist/test/test_prefix128.d.ts +2 -0
  68. package/dist/test/test_prefix128.d.ts.map +1 -0
  69. package/dist/test/test_prefix128.js +35 -0
  70. package/dist/test/test_prefix32.d.ts +2 -0
  71. package/dist/test/test_prefix32.d.ts.map +1 -0
  72. package/dist/test/test_prefix32.js +126 -0
  73. package/dist/test/test_rle.d.ts +2 -0
  74. package/dist/test/test_rle.d.ts.map +1 -0
  75. package/dist/test/test_rle.js +141 -0
  76. package/dist/vitest.config.d.ts +3 -0
  77. package/dist/vitest.config.d.ts.map +1 -0
  78. package/dist/vitest.config.js +11 -0
  79. package/package.json +41 -37
  80. package/src/crunchy.ts +731 -0
  81. package/src/index.ts +23 -0
  82. package/src/ip_bits.ts +148 -0
  83. package/src/ip_version.ts +6 -0
  84. package/src/ipaddress.ts +1347 -0
  85. package/src/ipv4.ts +203 -0
  86. package/src/ipv6.ts +202 -0
  87. package/src/ipv6_loopback.ts +55 -0
  88. package/src/ipv6_mapped.ts +163 -0
  89. package/src/ipv6_unspec.ts +59 -0
  90. package/src/prefix.ts +170 -0
  91. package/src/prefix128.ts +42 -0
  92. package/src/prefix32.ts +32 -0
  93. package/src/rle.ts +85 -0
  94. package/test/test_crunchy_impls.ts +179 -0
  95. package/test/test_crunchy_with32bit.ts +510 -0
  96. package/test/test_ipaddress.ts +208 -0
  97. package/test/test_ipv4.ts +596 -0
  98. package/test/test_ipv6.ts +325 -0
  99. package/test/test_ipv6_loopback.ts +52 -0
  100. package/test/test_ipv6_mapped.ts +101 -0
  101. package/test/test_ipv6_unspec.ts +51 -0
  102. package/test/test_prefix128.ts +39 -0
  103. package/test/test_prefix32.ts +130 -0
  104. package/test/test_rle.ts +144 -0
  105. package/LICENSE +0 -20
  106. package/README.md +0 -960
  107. package/crunchy.d.ts +0 -55
  108. package/index.d.ts +0 -12
  109. package/ipaddress.js +0 -2869
  110. package/ipaddress.js.map +0 -1
  111. package/ipv4.d.ts +0 -14
  112. package/ipv6.d.ts +0 -12
  113. package/ipv6_loopback.d.ts +0 -5
  114. package/ipv6_mapped.d.ts +0 -5
  115. package/ipv6_unspec.d.ts +0 -5
  116. package/prefix128.d.ts +0 -6
  117. package/prefix32.d.ts +0 -6
  118. package/rle.d.ts +0 -14
package/src/crunchy.ts ADDED
@@ -0,0 +1,731 @@
1
+ /**
2
+ * Arbitrary-precision integer used throughout ip address math.
3
+ *
4
+ * Historically this was always the hand-rolled 28-bit-limb
5
+ * implementation below (CrunchyWith32Bit), ported from
6
+ * Crunch - Arbitrary-precision integer arithmetic library
7
+ * Copyright (C) 2014 Nenad Vukicevic crunch.secureroom.net/license
8
+ *
9
+ * Native BigInt is now available in every runtime this library
10
+ * targets, so there are two implementations of CrunchyBase -
11
+ * CrunchyWithBigInt (native BigInt, used whenever available) and
12
+ * CrunchyWith32Bit (the original limb-array implementation, kept for
13
+ * environments without BigInt support). Which one backs `Crunchy` is
14
+ * decided once, at module load, by feature-detecting BigInt.
15
+ *
16
+ * `Crunchy` is a value (not a class), so `new Crunchy()` is not
17
+ * possible - use `Crunchy.create()` (or any of the other factories:
18
+ * `Crunchy.zero()`, `Crunchy.parse()`, ...).
19
+ */
20
+
21
+ export interface CrunchyBase {
22
+ clone(): CrunchyBase;
23
+ compare(y: CrunchyBase): number;
24
+ eq(oth: CrunchyBase): boolean;
25
+ lt(oth: CrunchyBase): boolean;
26
+ lte(oth: CrunchyBase): boolean;
27
+ gt(oth: CrunchyBase): boolean;
28
+ gte(oth: CrunchyBase): boolean;
29
+ add(y: CrunchyBase): CrunchyBase;
30
+ sub(y: CrunchyBase): CrunchyBase;
31
+ shl(s: number): CrunchyBase;
32
+ shr(s: number): CrunchyBase;
33
+ mod(y: CrunchyBase): CrunchyBase;
34
+ toString(radix?: number): string;
35
+
36
+ // Factories. Every value carries these too (not just the `Crunchy`
37
+ // singleton), since they're plain instance methods - but in
38
+ // practice they're only ever called on the singleton.
39
+ // create() with no argument returns zero; given a number it acts
40
+ // like from_number(), given a string it acts like parse().
41
+ create(value?: number | string): CrunchyBase;
42
+ zero(): CrunchyBase;
43
+ one(): CrunchyBase;
44
+ two(): CrunchyBase;
45
+ parse(val: string): CrunchyBase;
46
+ from_number(val: number): CrunchyBase;
47
+ from_string(val: string, radix?: number): CrunchyBase | null;
48
+ from_string_or_throw(val: string, radix?: number): CrunchyBase;
49
+ }
50
+
51
+ /**
52
+ * Native BigInt-backed implementation. Used whenever the runtime
53
+ * supports BigInt (i.e. essentially always: Node 10.4+, every
54
+ * evergreen browser, Deno).
55
+ */
56
+ export class CrunchyWithBigInt implements CrunchyBase {
57
+ public readonly value: bigint;
58
+
59
+ // Only ever constructed from within this class (its own methods, or
60
+ // the bootstrap() below used once to seed the module-level
61
+ // singleton) - real values always arrive as number|string, through
62
+ // from_number()/from_string()/parse(), never as a raw bigint.
63
+ private constructor(value: bigint) {
64
+ this.value = value;
65
+ }
66
+
67
+ public static bootstrap(): CrunchyWithBigInt {
68
+ return new CrunchyWithBigInt(0n);
69
+ }
70
+
71
+ private static valueOf(y: CrunchyBase): bigint {
72
+ if (!(y instanceof CrunchyWithBigInt)) {
73
+ throw new Error("CrunchyWithBigInt operation received a non-CrunchyWithBigInt operand");
74
+ }
75
+ return y.value;
76
+ }
77
+
78
+ public clone(): CrunchyBase {
79
+ return new CrunchyWithBigInt(this.value);
80
+ }
81
+
82
+ public compare(y: CrunchyBase): number {
83
+ const yv = CrunchyWithBigInt.valueOf(y);
84
+ if (this.value < yv) return -1;
85
+ if (this.value > yv) return 1;
86
+ return 0;
87
+ }
88
+
89
+ public eq(oth: CrunchyBase): boolean {
90
+ return this.compare(oth) == 0;
91
+ }
92
+
93
+ public lt(oth: CrunchyBase): boolean {
94
+ return this.compare(oth) < 0;
95
+ }
96
+
97
+ public lte(oth: CrunchyBase): boolean {
98
+ return this.compare(oth) <= 0;
99
+ }
100
+
101
+ public gt(oth: CrunchyBase): boolean {
102
+ return this.compare(oth) > 0;
103
+ }
104
+
105
+ public gte(oth: CrunchyBase): boolean {
106
+ return this.compare(oth) >= 0;
107
+ }
108
+
109
+ public add(y: CrunchyBase): CrunchyBase {
110
+ return new CrunchyWithBigInt(this.value + CrunchyWithBigInt.valueOf(y));
111
+ }
112
+
113
+ public sub(y: CrunchyBase): CrunchyBase {
114
+ return new CrunchyWithBigInt(this.value - CrunchyWithBigInt.valueOf(y));
115
+ }
116
+
117
+ // shl/shr shift the magnitude only and leave the sign untouched,
118
+ // matching CrunchyWith32Bit (native BigInt `>>` on a negative value
119
+ // performs an arithmetic/floor shift instead).
120
+ public shl(s: number): CrunchyBase {
121
+ const neg = this.value < 0n;
122
+ const abs = neg ? -this.value : this.value;
123
+ const shifted = abs << BigInt(s);
124
+ return new CrunchyWithBigInt(neg ? -shifted : shifted);
125
+ }
126
+
127
+ public shr(s: number): CrunchyBase {
128
+ const neg = this.value < 0n;
129
+ const abs = neg ? -this.value : this.value;
130
+ const shifted = abs >> BigInt(s);
131
+ return new CrunchyWithBigInt(neg ? -shifted : shifted);
132
+ }
133
+
134
+ // Floored modulo: result always has the same sign as `y` (or is
135
+ // zero), which matches the only path CrunchyWith32Bit.mod() is
136
+ // actually exercised with (non-negative operands).
137
+ public mod(y: CrunchyBase): CrunchyBase {
138
+ const yv = CrunchyWithBigInt.valueOf(y);
139
+ let r = this.value % yv;
140
+ if (r !== 0n && r < 0n !== yv < 0n) {
141
+ r += yv;
142
+ }
143
+ return new CrunchyWithBigInt(r);
144
+ }
145
+
146
+ // Never emits a sign, matching CrunchyWith32Bit.toString(), which
147
+ // only ever produces magnitude digits.
148
+ public toString(radix = 10): string {
149
+ const abs = this.value < 0n ? -this.value : this.value;
150
+ return abs.toString(radix);
151
+ }
152
+
153
+ public create(value?: number | string): CrunchyBase {
154
+ if (value === undefined) {
155
+ return this.zero();
156
+ }
157
+ return typeof value === "number" ? this.from_number(value) : this.parse(value);
158
+ }
159
+
160
+ public zero(): CrunchyBase {
161
+ return new CrunchyWithBigInt(0n);
162
+ }
163
+
164
+ public one(): CrunchyBase {
165
+ return new CrunchyWithBigInt(1n);
166
+ }
167
+
168
+ public two(): CrunchyBase {
169
+ return new CrunchyWithBigInt(2n);
170
+ }
171
+
172
+ public parse(val: string): CrunchyBase {
173
+ const ret = this.from_string(val, 10);
174
+ if (ret === null) {
175
+ throw new Error(`invalid decimal number: ${val}`);
176
+ }
177
+ return ret;
178
+ }
179
+
180
+ public from_number(val: number): CrunchyBase {
181
+ return new CrunchyWithBigInt(BigInt(Math.trunc(val)));
182
+ }
183
+
184
+ public from_string(val: string, radix = 10): CrunchyBase | null {
185
+ let s = val;
186
+ let neg = false;
187
+ if (s.startsWith("-")) {
188
+ neg = true;
189
+ s = s.slice(1);
190
+ }
191
+ let result = 0n;
192
+ const bigRadix = BigInt(radix);
193
+ for (const ch of s) {
194
+ const digit = parseInt(ch, radix);
195
+ if (isNaN(digit)) {
196
+ return null;
197
+ }
198
+ result = result * bigRadix + BigInt(digit);
199
+ }
200
+ return new CrunchyWithBigInt(neg ? -result : result);
201
+ }
202
+
203
+ public from_string_or_throw(val: string, radix = 10): CrunchyBase {
204
+ const ret = this.from_string(val, radix);
205
+ if (ret === null) {
206
+ throw new Error(`invalid number: ${val} (radix ${radix})`);
207
+ }
208
+ return ret;
209
+ }
210
+ }
211
+
212
+ /**
213
+ * @module Crunch
214
+ * Radix: 28 bits
215
+ * Endianness: Big
216
+ *
217
+ * @param {boolean} rawIn - expect 28-bit arrays
218
+ * @param {boolean} rawOut - return 28-bit arrays
219
+ */
220
+ export class CrunchyWith32Bit implements CrunchyBase {
221
+ num: number[] = [];
222
+ negative = false;
223
+
224
+ static zeroes: number[] = ((n: number): number[] => {
225
+ return new Array(n).fill(0);
226
+ })(60);
227
+
228
+ public clone(): CrunchyWith32Bit {
229
+ const ret = new CrunchyWith32Bit();
230
+ ret.num = this.num.slice();
231
+ ret.negative = this.negative;
232
+ return ret;
233
+ }
234
+
235
+ public static removeLeadingZeros(inn: number[]): number[] {
236
+ const out = inn.slice();
237
+ while (out[0] === 0 && out.length > 1) {
238
+ out.shift();
239
+ }
240
+ return out; // .transformOut();
241
+ }
242
+
243
+ public static from_14bit(a: number[]): CrunchyWith32Bit {
244
+ const ret = new CrunchyWith32Bit();
245
+ ret.num = a;
246
+ return ret;
247
+ }
248
+
249
+ public static from_8bit(a: number[]): CrunchyWith32Bit {
250
+ let x = [0, 0, 0, 0, 0, 0].slice((a.length - 1) % 7);
251
+ const z = new CrunchyWith32Bit();
252
+
253
+ if (a[0] < 0) {
254
+ a[0] *= -1;
255
+ z.negative = true;
256
+ } else {
257
+ z.negative = false;
258
+ }
259
+ x = x.concat(a);
260
+ for (let i = 0; i < x.length; i += 7) {
261
+ z.num.push(
262
+ x[i] * 1048576 + x[i + 1] * 4096 + x[i + 2] * 16 + (x[i + 3] >> 4),
263
+ (x[i + 3] & 15) * 16777216 + x[i + 4] * 65536 + x[i + 5] * 256 + x[i + 6],
264
+ );
265
+ }
266
+ z.num = CrunchyWith32Bit.removeLeadingZeros(z.num);
267
+ return z;
268
+ }
269
+
270
+ private static _zero = CrunchyWith32Bit.from_8bit([0]);
271
+ private static _one = CrunchyWith32Bit.from_8bit([1]);
272
+ private static _two = CrunchyWith32Bit.from_8bit([2]);
273
+
274
+ public create(value?: number | string): CrunchyBase {
275
+ if (value === undefined) {
276
+ return this.zero();
277
+ }
278
+ return typeof value === "number" ? this.from_number(value) : this.parse(value);
279
+ }
280
+
281
+ public zero(): CrunchyBase {
282
+ return CrunchyWith32Bit._zero;
283
+ }
284
+
285
+ public one(): CrunchyBase {
286
+ return CrunchyWith32Bit._one;
287
+ }
288
+
289
+ public two(): CrunchyBase {
290
+ return CrunchyWith32Bit._two;
291
+ }
292
+
293
+ public parse(val: string): CrunchyWith32Bit {
294
+ const ret = this.from_string(val, 10);
295
+ if (ret === null) {
296
+ throw new Error(`invalid decimal number: ${val}`);
297
+ }
298
+ return ret;
299
+ }
300
+
301
+ public from_number(val: number): CrunchyWith32Bit {
302
+ return this.parse("" + val);
303
+ }
304
+
305
+ // Same as from_string(), but for literals that are known to be valid
306
+ // numbers (e.g. hardcoded hex constants), so the caller doesn't need
307
+ // to deal with a nullable result it can never actually observe.
308
+ public from_string_or_throw(val: string, radix = 10): CrunchyWith32Bit {
309
+ const ret = this.from_string(val, radix);
310
+ if (ret === null) {
311
+ throw new Error(`invalid number: ${val} (radix ${radix})`);
312
+ }
313
+ return ret;
314
+ }
315
+
316
+ public from_string(val: string, radix = 10): CrunchyWith32Bit | null {
317
+ const x = val.split("");
318
+ let p = CrunchyWith32Bit._one;
319
+ let a = CrunchyWith32Bit._zero;
320
+ const b = CrunchyWith32Bit.from_8bit([radix]);
321
+ let n = false;
322
+
323
+ if (x[0] === "-") {
324
+ n = true;
325
+ x.shift();
326
+ }
327
+ while (x.length > 0) {
328
+ const popped = x.pop();
329
+ if (popped === undefined) {
330
+ break;
331
+ }
332
+ const c = parseInt(popped, radix);
333
+ if (isNaN(c)) {
334
+ console.error("from_string:", val);
335
+ return null;
336
+ }
337
+ a = a.add(p.mul(CrunchyWith32Bit.from_8bit([c])));
338
+ p = p.mul(b);
339
+ }
340
+ a.negative = n;
341
+ return a;
342
+ }
343
+
344
+ public to_8bit(): number[] {
345
+ const x = [0].slice((this.num.length - 1) % 2).concat(this.num);
346
+ let z: number[] = [];
347
+
348
+ for (let i = 0; i < x.length;) {
349
+ const u = x[i++];
350
+ const v = x[i++];
351
+
352
+ z.push(u >> 20, (u >> 12) & 255, (u >> 4) & 255, ((u << 4) | (v >> 24)) & 255, (v >> 16) & 255, (v >> 8) & 255, v & 255);
353
+ }
354
+ z = CrunchyWith32Bit.removeLeadingZeros(z);
355
+
356
+ if (this.negative) {
357
+ z[0] *= -1;
358
+ }
359
+ return z;
360
+ }
361
+
362
+ public compare(y: CrunchyBase): number {
363
+ const yy = y as CrunchyWith32Bit;
364
+ const xl = this.num.length;
365
+ const yl = yy.num.length; // zero front pad problem
366
+
367
+ if (xl < yl) {
368
+ return -1;
369
+ } else if (xl > yl) {
370
+ return 1;
371
+ }
372
+
373
+ for (let i = 0; i < xl; i++) {
374
+ if (this.num[i] < yy.num[i]) return -1;
375
+ if (this.num[i] > yy.num[i]) return 1;
376
+ }
377
+
378
+ return 0;
379
+ }
380
+
381
+ public eq(oth: CrunchyBase): boolean {
382
+ return this.compare(oth) == 0;
383
+ }
384
+
385
+ public lt(oth: CrunchyBase): boolean {
386
+ return this.compare(oth) < 0;
387
+ }
388
+
389
+ public lte(oth: CrunchyBase): boolean {
390
+ return this.compare(oth) <= 0;
391
+ }
392
+
393
+ public gt(oth: CrunchyBase): boolean {
394
+ return this.compare(oth) > 0;
395
+ }
396
+
397
+ public gte(oth: CrunchyBase): boolean {
398
+ return this.compare(oth) >= 0;
399
+ }
400
+
401
+ public add(y: CrunchyBase): CrunchyWith32Bit {
402
+ const yy = y as CrunchyWith32Bit;
403
+ let z: CrunchyWith32Bit;
404
+ if (this.negative) {
405
+ if (yy.negative) {
406
+ z = this.unsigned_add(yy);
407
+ z.negative = true;
408
+ } else {
409
+ z = yy.unsigned_sub(this, false).cut();
410
+ }
411
+ } else {
412
+ z = yy.negative ? this.unsigned_sub(yy, false).cut() : this.unsigned_add(yy);
413
+ }
414
+ return z;
415
+ }
416
+
417
+ public unsigned_add(_y: CrunchyWith32Bit): CrunchyWith32Bit {
418
+ const n = this.num.length;
419
+ const t = _y.num.length;
420
+ let i = Math.max(n, t);
421
+ let c = 0;
422
+ const z = CrunchyWith32Bit.zeroes.slice(0, i);
423
+ let x = this.num;
424
+ let y = _y.num;
425
+ if (n < t) {
426
+ x = CrunchyWith32Bit.zeroes.slice(0, t - n).concat(this.num);
427
+ } else if (n > t) {
428
+ y = CrunchyWith32Bit.zeroes.slice(0, n - t).concat(y);
429
+ }
430
+ for (i -= 1; i >= 0; i--) {
431
+ z[i] = x[i] + y[i] + c;
432
+
433
+ if (z[i] > 268435455) {
434
+ c = 1;
435
+ z[i] -= 268435456;
436
+ } else {
437
+ c = 0;
438
+ }
439
+ }
440
+
441
+ if (c === 1) {
442
+ z.unshift(c);
443
+ }
444
+ const ret = new CrunchyWith32Bit();
445
+ ret.num = z;
446
+ return ret;
447
+ }
448
+
449
+ public sub(y: CrunchyBase): CrunchyWith32Bit {
450
+ const yy = y as CrunchyWith32Bit;
451
+ let z: CrunchyWith32Bit;
452
+ if (this.negative) {
453
+ if (yy.negative) {
454
+ z = yy.unsigned_sub(this, false).cut();
455
+ } else {
456
+ z = this.unsigned_add(yy);
457
+ z.negative = true;
458
+ }
459
+ } else {
460
+ z = yy.negative ? this.unsigned_add(yy) : this.unsigned_sub(yy, false).cut();
461
+ }
462
+ return z;
463
+ }
464
+
465
+ public unsigned_sub(_y: CrunchyWith32Bit, internal = false): CrunchyWith32Bit {
466
+ const n = this.num.length;
467
+ const t = _y.num.length;
468
+ let i = Math.max(n, t);
469
+ let c = 0;
470
+ const z = CrunchyWith32Bit.zeroes.slice(0, i);
471
+ let x = this.num;
472
+ let y = _y.num;
473
+ if (n < t) {
474
+ x = CrunchyWith32Bit.zeroes.slice(0, t - n).concat(x);
475
+ } else if (n > t) {
476
+ y = CrunchyWith32Bit.zeroes.slice(0, n - t).concat(y);
477
+ }
478
+ for (i -= 1; i >= 0; i--) {
479
+ z[i] = x[i] - y[i] - c;
480
+
481
+ if (z[i] < 0) {
482
+ c = 1;
483
+ z[i] += 268435456;
484
+ } else {
485
+ c = 0;
486
+ }
487
+ }
488
+
489
+ let cry = new CrunchyWith32Bit();
490
+ cry.num = z;
491
+ if (c === 1 && !internal) {
492
+ const zero = new CrunchyWith32Bit();
493
+ zero.num = CrunchyWith32Bit.zeroes.slice(0, z.length);
494
+ cry = zero.unsigned_sub(cry, true);
495
+ cry.negative = true;
496
+ }
497
+ return cry;
498
+ }
499
+
500
+ public lsh(s: number): CrunchyWith32Bit {
501
+ const ss = s % 28;
502
+ const ls = Math.floor(s / 28);
503
+ let l = this.num.length;
504
+ const z: CrunchyWith32Bit = this.clone();
505
+ let t = 0;
506
+
507
+ if (ss) {
508
+ z.num = [];
509
+ while (l--) {
510
+ z.num[l] = ((this.num[l] << ss) + t) & 268435455;
511
+ t = this.num[l] >>> (28 - ss);
512
+ }
513
+
514
+ if (t !== 0) {
515
+ z.num.unshift(t);
516
+ }
517
+
518
+ z.negative = this.negative;
519
+ }
520
+ if (ls) {
521
+ z.num = z.num.concat(CrunchyWith32Bit.zeroes.slice(0, ls));
522
+ }
523
+ return z;
524
+ }
525
+
526
+ public rsh(s: number): CrunchyWith32Bit {
527
+ const ss = s % 28;
528
+ const ls = Math.floor(s / 28);
529
+ let l = this.num.length - ls;
530
+ const z = this.clone();
531
+ z.num = this.num.slice(0, l);
532
+ if (ss) {
533
+ while (--l >= 0) {
534
+ z.num[l] = ((z.num[l] >> ss) | (z.num[l - 1] << (28 - ss))) & 268435455;
535
+ }
536
+
537
+ z.num[l] = z.num[l] >> ss;
538
+
539
+ if (z.num[0] === 0) {
540
+ z.num.shift();
541
+ }
542
+ }
543
+ return z;
544
+ }
545
+
546
+ public mul(y: CrunchyBase): CrunchyWith32Bit {
547
+ const yy = y as CrunchyWith32Bit;
548
+ let yl: number;
549
+ let yh: number;
550
+ let c: number;
551
+ const n = this.num.length;
552
+ let i = yy.num.length;
553
+ const z = CrunchyWith32Bit.zeroes.slice(0, n + i);
554
+
555
+ while (i--) {
556
+ c = 0;
557
+
558
+ yl = yy.num[i] & 16383;
559
+ yh = yy.num[i] >> 14;
560
+
561
+ for (let j = n - 1, xl: number, xh: number, t1: number, t2: number; j >= 0; j--) {
562
+ xl = this.num[j] & 16383;
563
+ xh = this.num[j] >> 14;
564
+
565
+ t1 = yh * xl + xh * yl;
566
+ t2 = yl * xl + ((t1 & 16383) << 14) + z[j + i + 1] + c;
567
+
568
+ z[j + i + 1] = t2 & 268435455;
569
+ c = yh * xh + (t1 >> 14) + (t2 >> 28);
570
+ }
571
+
572
+ z[i] = c;
573
+ }
574
+
575
+ if (z[0] === 0) {
576
+ z.shift();
577
+ }
578
+ const ret = new CrunchyWith32Bit();
579
+ ret.negative = this.negative !== yy.negative;
580
+ ret.num = z;
581
+ return ret;
582
+ }
583
+
584
+ public static msb(x: number): number | undefined {
585
+ if (x !== 0) {
586
+ let z = 0;
587
+ for (let i = 134217728; i > x; z++) {
588
+ i /= 2;
589
+ }
590
+ return z;
591
+ }
592
+ }
593
+
594
+ public shr(s: number): CrunchyWith32Bit {
595
+ const my = this.rsh(s);
596
+ return my.cut();
597
+ }
598
+
599
+ public shl(s: number): CrunchyWith32Bit {
600
+ return this.lsh(s).cut();
601
+ }
602
+
603
+ public cut(): CrunchyWith32Bit {
604
+ const out = this.clone();
605
+ // beasty hack
606
+ if (out.num.length == 0) {
607
+ out.num = [0];
608
+ return out;
609
+ }
610
+ while (out.num[0] === 0 && out.num.length > 1) {
611
+ out.num.shift();
612
+ }
613
+ return out;
614
+ }
615
+
616
+ public div(y: CrunchyBase, internal = false): CrunchyWith32Bit | null {
617
+ const yy = y as CrunchyWith32Bit;
618
+ if (yy.num.length === 1 && yy.num[0] === 0) {
619
+ return null;
620
+ }
621
+ let u: CrunchyWith32Bit;
622
+ let v: CrunchyWith32Bit;
623
+ const s = (CrunchyWith32Bit.msb(yy.num[0]) ?? 0) - 1;
624
+ if (s > 0) {
625
+ u = this.lsh(s);
626
+ v = yy.lsh(s);
627
+ } else {
628
+ u = this.clone();
629
+ v = yy.clone();
630
+ }
631
+ const d = u.num.length - v.num.length;
632
+ const q = [0];
633
+ let k = CrunchyWith32Bit.from_14bit(v.num.concat(CrunchyWith32Bit.zeroes.slice(0, d)));
634
+ const yt = v.num[0] * 268435456 + v.num[1];
635
+
636
+ // only cmp as last resort
637
+ while (u.num[0] > k.num[0] || (u.num[0] === k.num[0] && u.compare(k) > -1)) {
638
+ q[0]++;
639
+ u = u.unsigned_sub(k, false);
640
+ }
641
+
642
+ for (let i = 1; i <= d; i++) {
643
+ q[i] = u.num[i - 1] === v.num[0] ? 268435455 : ~~((u.num[i - 1] * 268435456 + u.num[i]) / v.num[0]);
644
+
645
+ const xt = u.num[i - 1] * 72057594037927936 + u.num[i] * 268435456 + u.num[i + 1];
646
+ while (q[i] * yt > xt) {
647
+ // condition check can fail due to precision problem at 28-bit
648
+ q[i]--;
649
+ }
650
+
651
+ k = v.mul(CrunchyWith32Bit.from_14bit([q[i]]));
652
+ k.num = k.num.concat(CrunchyWith32Bit.zeroes.slice(0, d - i)); // concat after multiply, save cycles
653
+ u = u.unsigned_sub(k, false);
654
+
655
+ if (u.negative) {
656
+ u = CrunchyWith32Bit.from_14bit(v.num.concat(CrunchyWith32Bit.zeroes.slice(0, d - i))).unsigned_sub(u, false);
657
+ q[i]--;
658
+ }
659
+ }
660
+ let z: CrunchyWith32Bit;
661
+ if (internal) {
662
+ z = s > 0 ? u.cut().rsh(s) : u.cut();
663
+ } else {
664
+ z = CrunchyWith32Bit.from_14bit(CrunchyWith32Bit.removeLeadingZeros(q));
665
+ z.negative = this.negative !== yy.negative;
666
+ }
667
+
668
+ return z;
669
+ }
670
+
671
+ // Same as div(), but for divisors that are known to be non-zero, so the
672
+ // caller doesn't need to deal with a nullable result it can never
673
+ // actually observe.
674
+ public div_or_throw(y: CrunchyBase, internal = false): CrunchyWith32Bit {
675
+ const ret = this.div(y, internal);
676
+ if (ret === null) {
677
+ throw new Error("division by zero");
678
+ }
679
+ return ret;
680
+ }
681
+
682
+ public mod(y: CrunchyBase): CrunchyWith32Bit {
683
+ // For negative x, cmp doesn't work and result of div is negative
684
+ // so take result away from the modulus to get the correct result
685
+ const yy = y as CrunchyWith32Bit;
686
+ if (this.negative) {
687
+ return yy.sub(this.div_or_throw(yy, true));
688
+ }
689
+ switch (this.compare(yy)) {
690
+ case -1:
691
+ return this;
692
+ case 0:
693
+ return CrunchyWith32Bit.from_8bit([0]);
694
+ default:
695
+ return this.div_or_throw(yy, true);
696
+ }
697
+ }
698
+
699
+ public mds(n: number): number {
700
+ let z = 0;
701
+ for (let i = 0, l = this.num.length; i < l; i++) {
702
+ z = ((this.num[i] >> 14) + (z << 14)) % n;
703
+ z = ((this.num[i] & 16383) + (z << 14)) % n;
704
+ }
705
+ return z;
706
+ }
707
+
708
+ public toString(radix = 10): string {
709
+ const a: string[] = [];
710
+ let i = 0;
711
+ let x = this.clone();
712
+ const cradix = CrunchyWith32Bit.from_8bit([radix]);
713
+ const zero = CrunchyWith32Bit._zero;
714
+ do {
715
+ const digit = x.mds(radix);
716
+ x = x.div_or_throw(cradix);
717
+ a[i++] = "0123456789abcdef"[digit];
718
+ } while (!x.eq(zero));
719
+ return a.reverse().join("");
720
+ }
721
+ }
722
+
723
+ // Feature detection, computed once at module load.
724
+ export const hasBigInt: boolean = typeof BigInt !== "undefined";
725
+
726
+ const crunchySingleton: CrunchyBase = hasBigInt ? CrunchyWithBigInt.bootstrap() : new CrunchyWith32Bit();
727
+
728
+ export type Crunchy = CrunchyBase;
729
+ export const Crunchy: CrunchyBase = crunchySingleton;
730
+
731
+ export default Crunchy;