ipaddress 0.2.6 → 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 (57) hide show
  1. package/dist/src/crunchy.d.ts +107 -33
  2. package/dist/src/crunchy.d.ts.map +1 -1
  3. package/dist/src/crunchy.js +274 -142
  4. package/dist/src/ip_bits.d.ts +2 -2
  5. package/dist/src/ip_bits.d.ts.map +1 -1
  6. package/dist/src/ipaddress.d.ts +24 -23
  7. package/dist/src/ipaddress.d.ts.map +1 -1
  8. package/dist/src/ipaddress.js +22 -7
  9. package/dist/src/ipv4.d.ts +3 -3
  10. package/dist/src/ipv4.d.ts.map +1 -1
  11. package/dist/src/ipv4.js +26 -15
  12. package/dist/src/ipv6.d.ts +4 -4
  13. package/dist/src/ipv6.d.ts.map +1 -1
  14. package/dist/src/ipv6.js +1 -1
  15. package/dist/src/ipv6_loopback.d.ts +1 -1
  16. package/dist/src/ipv6_loopback.d.ts.map +1 -1
  17. package/dist/src/ipv6_mapped.d.ts +1 -1
  18. package/dist/src/ipv6_mapped.d.ts.map +1 -1
  19. package/dist/src/ipv6_unspec.d.ts +1 -1
  20. package/dist/src/ipv6_unspec.d.ts.map +1 -1
  21. package/dist/src/prefix128.d.ts +1 -1
  22. package/dist/src/prefix128.d.ts.map +1 -1
  23. package/dist/src/prefix128.js +5 -1
  24. package/dist/src/prefix32.d.ts +1 -1
  25. package/dist/src/prefix32.d.ts.map +1 -1
  26. package/dist/src/prefix32.js +5 -1
  27. package/dist/test/test_crunchy_impls.d.ts +2 -0
  28. package/dist/test/test_crunchy_impls.d.ts.map +1 -0
  29. package/dist/test/test_crunchy_impls.js +150 -0
  30. package/dist/test/test_crunchy_with32bit.d.ts +2 -0
  31. package/dist/test/test_crunchy_with32bit.d.ts.map +1 -0
  32. package/dist/test/{test_crunchy.js → test_crunchy_with32bit.js} +113 -182
  33. package/dist/test/test_ipv4.js +1 -1
  34. package/dist/test/test_ipv6.js +1 -1
  35. package/package.json +10 -9
  36. package/src/crunchy.ts +365 -180
  37. package/src/ip_bits.ts +11 -11
  38. package/src/ipaddress.ts +49 -33
  39. package/src/ipv4.ts +32 -19
  40. package/src/ipv6.ts +6 -6
  41. package/src/ipv6_loopback.ts +1 -1
  42. package/src/ipv6_mapped.ts +1 -1
  43. package/src/ipv6_unspec.ts +1 -1
  44. package/src/prefix128.ts +6 -2
  45. package/src/prefix32.ts +6 -2
  46. package/test/test_crunchy_impls.ts +179 -0
  47. package/test/{test_crunchy.ts → test_crunchy_with32bit.ts} +131 -235
  48. package/test/test_ipaddress.ts +9 -9
  49. package/test/test_ipv4.ts +115 -115
  50. package/test/test_ipv6.ts +62 -62
  51. package/test/test_ipv6_loopback.ts +1 -1
  52. package/test/test_ipv6_mapped.ts +4 -4
  53. package/test/test_ipv6_unspec.ts +1 -1
  54. package/test/test_prefix128.ts +2 -2
  55. package/test/test_prefix32.ts +13 -13
  56. package/dist/test/test_crunchy.d.ts +0 -2
  57. package/dist/test/test_crunchy.d.ts.map +0 -1
package/src/crunchy.ts CHANGED
@@ -1,9 +1,214 @@
1
1
  /**
2
- * based
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
3
6
  * Crunch - Arbitrary-precision integer arithmetic library
4
7
  * Copyright (C) 2014 Nenad Vukicevic crunch.secureroom.net/license
5
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).
6
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
+
7
212
  /**
8
213
  * @module Crunch
9
214
  * Radix: 28 bits
@@ -12,9 +217,7 @@
12
217
  * @param {boolean} rawIn - expect 28-bit arrays
13
218
  * @param {boolean} rawOut - return 28-bit arrays
14
219
  */
15
- // import Crunch from './crunch';
16
-
17
- export class Crunchy {
220
+ export class CrunchyWith32Bit implements CrunchyBase {
18
221
  num: number[] = [];
19
222
  negative = false;
20
223
 
@@ -22,8 +225,8 @@ export class Crunchy {
22
225
  return new Array(n).fill(0);
23
226
  })(60);
24
227
 
25
- public clone(): Crunchy {
26
- const ret = new Crunchy();
228
+ public clone(): CrunchyWith32Bit {
229
+ const ret = new CrunchyWith32Bit();
27
230
  ret.num = this.num.slice();
28
231
  ret.negative = this.negative;
29
232
  return ret;
@@ -37,15 +240,15 @@ export class Crunchy {
37
240
  return out; // .transformOut();
38
241
  }
39
242
 
40
- public static from_14bit(a: number[]): Crunchy {
41
- const ret = new Crunchy();
243
+ public static from_14bit(a: number[]): CrunchyWith32Bit {
244
+ const ret = new CrunchyWith32Bit();
42
245
  ret.num = a;
43
246
  return ret;
44
247
  }
45
248
 
46
- public static from_8bit(a: number[]): Crunchy {
249
+ public static from_8bit(a: number[]): CrunchyWith32Bit {
47
250
  let x = [0, 0, 0, 0, 0, 0].slice((a.length - 1) % 7);
48
- const z = new Crunchy();
251
+ const z = new CrunchyWith32Bit();
49
252
 
50
253
  if (a[0] < 0) {
51
254
  a[0] *= -1;
@@ -60,26 +263,61 @@ export class Crunchy {
60
263
  (x[i + 3] & 15) * 16777216 + x[i + 4] * 65536 + x[i + 5] * 256 + x[i + 6],
61
264
  );
62
265
  }
63
- z.num = Crunchy.removeLeadingZeros(z.num);
266
+ z.num = CrunchyWith32Bit.removeLeadingZeros(z.num);
64
267
  return z;
65
268
  }
66
269
 
67
- public static parse(val: string): Crunchy {
68
- return Crunchy.from_string(val, 10);
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);
69
303
  }
70
304
 
71
- public static from_number(val: number): Crunchy {
72
- return Crunchy.parse("" + val);
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;
73
314
  }
74
315
 
75
- public static from_string(val: string, radix = 10): Crunchy {
316
+ public from_string(val: string, radix = 10): CrunchyWith32Bit | null {
76
317
  const x = val.split("");
77
- let p = Crunchy.one();
78
- let a = Crunchy.zero();
79
- const b = Crunchy.from_8bit([radix]);
80
- // console.log("fromstring:one:", p);
81
- // console.log("fromstring:zero:", a);
82
- // console.log("fromstring:radix:", b);
318
+ let p = CrunchyWith32Bit._one;
319
+ let a = CrunchyWith32Bit._zero;
320
+ const b = CrunchyWith32Bit.from_8bit([radix]);
83
321
  let n = false;
84
322
 
85
323
  if (x[0] === "-") {
@@ -87,13 +325,16 @@ export class Crunchy {
87
325
  x.shift();
88
326
  }
89
327
  while (x.length > 0) {
90
- const c = parseInt(x.pop(), radix);
328
+ const popped = x.pop();
329
+ if (popped === undefined) {
330
+ break;
331
+ }
332
+ const c = parseInt(popped, radix);
91
333
  if (isNaN(c)) {
92
334
  console.error("from_string:", val);
93
335
  return null;
94
336
  }
95
- // console.log(a,p,b);
96
- a = a.add(p.mul(Crunchy.from_8bit([c])));
337
+ a = a.add(p.mul(CrunchyWith32Bit.from_8bit([c])));
97
338
  p = p.mul(b);
98
339
  }
99
340
  a.negative = n;
@@ -103,16 +344,14 @@ export class Crunchy {
103
344
  public to_8bit(): number[] {
104
345
  const x = [0].slice((this.num.length - 1) % 2).concat(this.num);
105
346
  let z: number[] = [];
106
- // z.num = [];
107
347
 
108
- for (let i = 0; i < x.length; ) {
348
+ for (let i = 0; i < x.length;) {
109
349
  const u = x[i++];
110
350
  const v = x[i++];
111
351
 
112
352
  z.push(u >> 20, (u >> 12) & 255, (u >> 4) & 255, ((u << 4) | (v >> 24)) & 255, (v >> 16) & 255, (v >> 8) & 255, v & 255);
113
353
  }
114
- // console.log("co:", a, z);
115
- z = Crunchy.removeLeadingZeros(z);
354
+ z = CrunchyWith32Bit.removeLeadingZeros(z);
116
355
 
117
356
  if (this.negative) {
118
357
  z[0] *= -1;
@@ -120,9 +359,10 @@ export class Crunchy {
120
359
  return z;
121
360
  }
122
361
 
123
- public compare(y: Crunchy): number {
362
+ public compare(y: CrunchyBase): number {
363
+ const yy = y as CrunchyWith32Bit;
124
364
  const xl = this.num.length;
125
- const yl = y.num.length; // zero front pad problem
365
+ const yl = yy.num.length; // zero front pad problem
126
366
 
127
367
  if (xl < yl) {
128
368
  return -1;
@@ -131,64 +371,62 @@ export class Crunchy {
131
371
  }
132
372
 
133
373
  for (let i = 0; i < xl; i++) {
134
- // console.log("x=y:", i, x, y);
135
- if (this.num[i] < y.num[i]) return -1;
136
- if (this.num[i] > y.num[i]) return 1;
374
+ if (this.num[i] < yy.num[i]) return -1;
375
+ if (this.num[i] > yy.num[i]) return 1;
137
376
  }
138
377
 
139
378
  return 0;
140
379
  }
141
380
 
142
- public eq(oth: Crunchy): boolean {
381
+ public eq(oth: CrunchyBase): boolean {
143
382
  return this.compare(oth) == 0;
144
383
  }
145
384
 
146
- public lte(oth: Crunchy): boolean {
147
- return this.compare(oth) <= 0;
385
+ public lt(oth: CrunchyBase): boolean {
386
+ return this.compare(oth) < 0;
148
387
  }
149
388
 
150
- public lt(oth: Crunchy): boolean {
151
- return this.compare(oth) < 0;
389
+ public lte(oth: CrunchyBase): boolean {
390
+ return this.compare(oth) <= 0;
152
391
  }
153
392
 
154
- public gt(oth: Crunchy): boolean {
393
+ public gt(oth: CrunchyBase): boolean {
155
394
  return this.compare(oth) > 0;
156
395
  }
157
396
 
158
- public gte(oth: Crunchy): boolean {
397
+ public gte(oth: CrunchyBase): boolean {
159
398
  return this.compare(oth) >= 0;
160
399
  }
161
400
 
162
- public add(y: Crunchy): Crunchy {
163
- let z: Crunchy;
401
+ public add(y: CrunchyBase): CrunchyWith32Bit {
402
+ const yy = y as CrunchyWith32Bit;
403
+ let z: CrunchyWith32Bit;
164
404
  if (this.negative) {
165
- if (y.negative) {
166
- z = this.unsigned_add(y);
405
+ if (yy.negative) {
406
+ z = this.unsigned_add(yy);
167
407
  z.negative = true;
168
408
  } else {
169
- z = y.unsigned_sub(this, false).cut();
409
+ z = yy.unsigned_sub(this, false).cut();
170
410
  }
171
411
  } else {
172
- z = y.negative ? this.unsigned_sub(y, false).cut() : this.unsigned_add(y);
412
+ z = yy.negative ? this.unsigned_sub(yy, false).cut() : this.unsigned_add(yy);
173
413
  }
174
414
  return z;
175
415
  }
176
416
 
177
- public unsigned_add(_y: Crunchy): Crunchy {
417
+ public unsigned_add(_y: CrunchyWith32Bit): CrunchyWith32Bit {
178
418
  const n = this.num.length;
179
419
  const t = _y.num.length;
180
420
  let i = Math.max(n, t);
181
421
  let c = 0;
182
- const z = Crunchy.zeroes.slice(0, i);
183
- // console.log("add:1:", new Date());
422
+ const z = CrunchyWith32Bit.zeroes.slice(0, i);
184
423
  let x = this.num;
185
424
  let y = _y.num;
186
425
  if (n < t) {
187
- x = Crunchy.zeroes.slice(0, t - n).concat(this.num);
426
+ x = CrunchyWith32Bit.zeroes.slice(0, t - n).concat(this.num);
188
427
  } else if (n > t) {
189
- y = Crunchy.zeroes.slice(0, n - t).concat(y);
428
+ y = CrunchyWith32Bit.zeroes.slice(0, n - t).concat(y);
190
429
  }
191
- // console.log("add:2:", new Date());
192
430
  for (i -= 1; i >= 0; i--) {
193
431
  z[i] = x[i] + y[i] + c;
194
432
 
@@ -200,77 +438,70 @@ export class Crunchy {
200
438
  }
201
439
  }
202
440
 
203
- // console.log("add:3:", new Date());
204
441
  if (c === 1) {
205
442
  z.unshift(c);
206
443
  }
207
- const ret = new Crunchy();
444
+ const ret = new CrunchyWith32Bit();
208
445
  ret.num = z;
209
- // console.log("add:4:", new Date());
210
446
  return ret;
211
447
  }
212
448
 
213
- public sub(y: Crunchy): Crunchy {
214
- let z: Crunchy;
449
+ public sub(y: CrunchyBase): CrunchyWith32Bit {
450
+ const yy = y as CrunchyWith32Bit;
451
+ let z: CrunchyWith32Bit;
215
452
  if (this.negative) {
216
- if (y.negative) {
217
- // console.log("sub-c1");
218
- z = y.unsigned_sub(this, false).cut();
453
+ if (yy.negative) {
454
+ z = yy.unsigned_sub(this, false).cut();
219
455
  } else {
220
- // console.log("sub-c2");
221
- z = this.unsigned_add(y);
456
+ z = this.unsigned_add(yy);
222
457
  z.negative = true;
223
458
  }
224
459
  } else {
225
- // console.log("sub-c3", this, y);
226
- z = y.negative ? this.unsigned_add(y) : this.unsigned_sub(y, false).cut();
460
+ z = yy.negative ? this.unsigned_add(yy) : this.unsigned_sub(yy, false).cut();
227
461
  }
228
462
  return z;
229
463
  }
230
464
 
231
- public unsigned_sub(_y: Crunchy, internal = false): Crunchy {
465
+ public unsigned_sub(_y: CrunchyWith32Bit, internal = false): CrunchyWith32Bit {
232
466
  const n = this.num.length;
233
467
  const t = _y.num.length;
234
468
  let i = Math.max(n, t);
235
469
  let c = 0;
236
- const z = Crunchy.zeroes.slice(0, i);
470
+ const z = CrunchyWith32Bit.zeroes.slice(0, i);
237
471
  let x = this.num;
238
472
  let y = _y.num;
239
473
  if (n < t) {
240
- x = Crunchy.zeroes.slice(0, t - n).concat(x);
474
+ x = CrunchyWith32Bit.zeroes.slice(0, t - n).concat(x);
241
475
  } else if (n > t) {
242
- y = Crunchy.zeroes.slice(0, n - t).concat(y);
476
+ y = CrunchyWith32Bit.zeroes.slice(0, n - t).concat(y);
243
477
  }
244
478
  for (i -= 1; i >= 0; i--) {
245
479
  z[i] = x[i] - y[i] - c;
246
- // console.log(z, x, y);
247
480
 
248
481
  if (z[i] < 0) {
249
482
  c = 1;
250
- // console.log("pre:+", z[i], z);
251
483
  z[i] += 268435456;
252
- // console.log("pre:-", z[i], z);
253
484
  } else {
254
485
  c = 0;
255
486
  }
256
487
  }
257
488
 
258
- let cry = new Crunchy();
489
+ let cry = new CrunchyWith32Bit();
259
490
  cry.num = z;
260
491
  if (c === 1 && !internal) {
261
- const zero = new Crunchy();
262
- zero.num = Crunchy.zeroes.slice(0, z.length);
492
+ const zero = new CrunchyWith32Bit();
493
+ zero.num = CrunchyWith32Bit.zeroes.slice(0, z.length);
263
494
  cry = zero.unsigned_sub(cry, true);
264
495
  cry.negative = true;
265
496
  }
266
497
  return cry;
267
498
  }
268
499
 
269
- public lsh(s: number): Crunchy {
500
+ public lsh(s: number): CrunchyWith32Bit {
270
501
  const ss = s % 28;
271
502
  const ls = Math.floor(s / 28);
272
503
  let l = this.num.length;
273
- const z: Crunchy = this.clone();
504
+ const z: CrunchyWith32Bit = this.clone();
274
505
  let t = 0;
275
506
 
276
507
  if (ss) {
@@ -287,24 +518,12 @@ export class Crunchy {
287
518
  z.negative = this.negative;
288
519
  }
289
520
  if (ls) {
290
- z.num = z.num.concat(Crunchy.zeroes.slice(0, ls));
291
- // return (ls) ? z.concat(zeroes.slice(0, ls)) : z;
521
+ z.num = z.num.concat(CrunchyWith32Bit.zeroes.slice(0, ls));
292
522
  }
293
- // console.log(this, s, z);
294
523
  return z;
295
524
  }
296
525
 
297
- // public leftShift(s: number): Crunchy {
298
- // return this.transformIn()
299
-
300
- // Crunchy.transformOut(Crunch.lsh(.pop(), s));
301
- // }
302
-
303
- // public shl(num: number): Crunchy {
304
- // return Crunchy.from_8bit(Crunch.leftShift(this.num, num));
305
- // }
306
-
307
- public rsh(s: number): Crunchy {
526
+ public rsh(s: number): CrunchyWith32Bit {
308
527
  const ss = s % 28;
309
528
  const ls = Math.floor(s / 28);
310
529
  let l = this.num.length - ls;
@@ -324,26 +543,20 @@ export class Crunchy {
324
543
  return z;
325
544
  }
326
545
 
327
- // public shr(num: number): Crunchy {
328
- // //console.log("shr<<", num, this.num);
329
- // let res = this.rightShift(this.num, num);
330
- // // console.log("shr>>", num, this.num, res);
331
- // return Crunchy.from_8bit(res);
332
- // }
333
-
334
- public mul(y: Crunchy): Crunchy {
546
+ public mul(y: CrunchyBase): CrunchyWith32Bit {
547
+ const yy = y as CrunchyWith32Bit;
335
548
  let yl: number;
336
549
  let yh: number;
337
550
  let c: number;
338
551
  const n = this.num.length;
339
- let i = y.num.length;
340
- const z = Crunchy.zeroes.slice(0, n + i);
552
+ let i = yy.num.length;
553
+ const z = CrunchyWith32Bit.zeroes.slice(0, n + i);
341
554
 
342
555
  while (i--) {
343
556
  c = 0;
344
557
 
345
- yl = y.num[i] & 16383;
346
- yh = y.num[i] >> 14;
558
+ yl = yy.num[i] & 16383;
559
+ yh = yy.num[i] >> 14;
347
560
 
348
561
  for (let j = n - 1, xl: number, xh: number, t1: number, t2: number; j >= 0; j--) {
349
562
  xl = this.num[j] & 16383;
@@ -362,13 +575,13 @@ export class Crunchy {
362
575
  if (z[0] === 0) {
363
576
  z.shift();
364
577
  }
365
- const ret = new Crunchy();
366
- ret.negative = this.negative !== y.negative;
578
+ const ret = new CrunchyWith32Bit();
579
+ ret.negative = this.negative !== yy.negative;
367
580
  ret.num = z;
368
581
  return ret;
369
582
  }
370
583
 
371
- public static msb(x: number): number {
584
+ public static msb(x: number): number | undefined {
372
585
  if (x !== 0) {
373
586
  let z = 0;
374
587
  for (let i = 134217728; i > x; z++) {
@@ -378,32 +591,16 @@ export class Crunchy {
378
591
  }
379
592
  }
380
593
 
381
- // public transformIn(): Crunchy {
382
- // // return Array.prototype.slice.call(a).map((v: Crunchy) => {
383
- // // return Crunchy.from_8bit(v.num.slice())
384
- // // });
385
- // return null;
386
- // }
387
-
388
- // public transformOut(): Crunchy {
389
- // console.log("in-transformOut:", this);
390
- // let ret = Crunchy.co(this);
391
- // console.log("out-transformOut:", this, ret);
392
- // return ret;
393
- // }
394
-
395
- public shr(s: number): Crunchy {
594
+ public shr(s: number): CrunchyWith32Bit {
396
595
  const my = this.rsh(s);
397
596
  return my.cut();
398
597
  }
399
598
 
400
- public shl(s: number): Crunchy {
599
+ public shl(s: number): CrunchyWith32Bit {
401
600
  return this.lsh(s).cut();
402
-
403
- // Crunch.transformOut(Crunch.lsh(Crunch.transformIn([x]).pop(), s));
404
601
  }
405
602
 
406
- public cut(): Crunchy {
603
+ public cut(): CrunchyWith32Bit {
407
604
  const out = this.clone();
408
605
  // beasty hack
409
606
  if (out.num.length == 0) {
@@ -413,31 +610,27 @@ export class Crunchy {
413
610
  while (out.num[0] === 0 && out.num.length > 1) {
414
611
  out.num.shift();
415
612
  }
416
- return out; // .transformOut();
613
+ return out;
417
614
  }
418
- // return Crunchy.transformOut(this.num
419
- // cut.apply(null, transformIn(arguments))
420
- // );
421
- // }
422
615
 
423
- public div(y: Crunchy, internal = false): Crunchy {
424
- if (y.num.length === 1 && y.num[0] === 0) {
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) {
425
619
  return null;
426
620
  }
427
- // var u, v, xt, yt, d, q, k, i, z;
428
- let u: Crunchy;
429
- let v: Crunchy;
430
- const s = Crunchy.msb(y.num[0]) - 1;
621
+ let u: CrunchyWith32Bit;
622
+ let v: CrunchyWith32Bit;
623
+ const s = (CrunchyWith32Bit.msb(yy.num[0]) ?? 0) - 1;
431
624
  if (s > 0) {
432
625
  u = this.lsh(s);
433
- v = y.lsh(s);
626
+ v = yy.lsh(s);
434
627
  } else {
435
628
  u = this.clone();
436
- v = y.clone();
629
+ v = yy.clone();
437
630
  }
438
631
  const d = u.num.length - v.num.length;
439
632
  const q = [0];
440
- let k = Crunchy.from_14bit(v.num.concat(Crunchy.zeroes.slice(0, d)));
633
+ let k = CrunchyWith32Bit.from_14bit(v.num.concat(CrunchyWith32Bit.zeroes.slice(0, d)));
441
634
  const yt = v.num[0] * 268435456 + v.num[1];
442
635
 
443
636
  // only cmp as last resort
@@ -455,42 +648,51 @@ export class Crunchy {
455
648
  q[i]--;
456
649
  }
457
650
 
458
- k = v.mul(Crunchy.from_14bit([q[i]]));
459
- k.num = k.num.concat(Crunchy.zeroes.slice(0, d - i)); // concat after multiply, save cycles
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
460
653
  u = u.unsigned_sub(k, false);
461
654
 
462
655
  if (u.negative) {
463
- u = Crunchy.from_14bit(v.num.concat(Crunchy.zeroes.slice(0, d - i))).unsigned_sub(u, false);
656
+ u = CrunchyWith32Bit.from_14bit(v.num.concat(CrunchyWith32Bit.zeroes.slice(0, d - i))).unsigned_sub(u, false);
464
657
  q[i]--;
465
658
  }
466
659
  }
467
- let z: Crunchy;
660
+ let z: CrunchyWith32Bit;
468
661
  if (internal) {
469
662
  z = s > 0 ? u.cut().rsh(s) : u.cut();
470
663
  } else {
471
- z = Crunchy.from_14bit(Crunchy.removeLeadingZeros(q));
472
- z.negative = this.negative !== y.negative;
664
+ z = CrunchyWith32Bit.from_14bit(CrunchyWith32Bit.removeLeadingZeros(q));
665
+ z.negative = this.negative !== yy.negative;
473
666
  }
474
667
 
475
668
  return z;
476
669
  }
477
- // public sub(cry: Crunchy): Crunchy {
478
- // return Crunchy.from_8bit(Crunch.sub(this.num, cry.num));
479
- // }
480
670
 
481
- public mod(y: Crunchy): Crunchy {
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 {
482
683
  // For negative x, cmp doesn't work and result of div is negative
483
684
  // so take result away from the modulus to get the correct result
685
+ const yy = y as CrunchyWith32Bit;
484
686
  if (this.negative) {
485
- return y.sub(this.div(y, true));
687
+ return yy.sub(this.div_or_throw(yy, true));
486
688
  }
487
- switch (this.compare(y)) {
689
+ switch (this.compare(yy)) {
488
690
  case -1:
489
691
  return this;
490
692
  case 0:
491
- return Crunchy.from_8bit([0]);
693
+ return CrunchyWith32Bit.from_8bit([0]);
492
694
  default:
493
- return this.div(y, true);
695
+ return this.div_or_throw(yy, true);
494
696
  }
495
697
  }
496
698
 
@@ -507,40 +709,23 @@ export class Crunchy {
507
709
  const a: string[] = [];
508
710
  let i = 0;
509
711
  let x = this.clone();
510
- // console.log("toString:", new Date());
511
- const cradix = Crunchy.from_8bit([radix]);
512
- const zero = Crunchy.zero();
712
+ const cradix = CrunchyWith32Bit.from_8bit([radix]);
713
+ const zero = CrunchyWith32Bit._zero;
513
714
  do {
514
715
  const digit = x.mds(radix);
515
- x = x.div(cradix);
716
+ x = x.div_or_throw(cradix);
516
717
  a[i++] = "0123456789abcdef"[digit];
517
- // console.log("1-toString:", x, radix, digit, a.join(""));
518
- // console.log("2-toString:", x, radix, digit, a, Crunch.compare(x, Crunchy._zero.num));
519
718
  } while (!x.eq(zero));
520
- // console.log("Crunchy.tostring-1:", a);
521
- const ret = a.reverse().join("");
522
- // console.log("Crunchy.tostring-2:", ret, new Date());
523
- return ret;
719
+ return a.reverse().join("");
524
720
  }
721
+ }
525
722
 
526
- // public toNumber() : number {
527
- // return null;
528
- // }
723
+ // Feature detection, computed once at module load.
724
+ export const hasBigInt: boolean = typeof BigInt !== "undefined";
529
725
 
530
- static _zero = Crunchy.from_8bit([0]);
531
- public static zero(): Crunchy {
532
- return Crunchy._zero;
533
- }
726
+ const crunchySingleton: CrunchyBase = hasBigInt ? CrunchyWithBigInt.bootstrap() : new CrunchyWith32Bit();
534
727
 
535
- static _one = Crunchy.from_8bit([1]);
536
- public static one(): Crunchy {
537
- return Crunchy._one;
538
- }
539
-
540
- static _two = Crunchy.from_8bit([2]);
541
- public static two(): Crunchy {
542
- return Crunchy._two;
543
- }
544
- }
728
+ export type Crunchy = CrunchyBase;
729
+ export const Crunchy: CrunchyBase = crunchySingleton;
545
730
 
546
731
  export default Crunchy;