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
@@ -1,9 +1,160 @@
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()`, ...).
6
19
  */
20
+ /**
21
+ * Native BigInt-backed implementation. Used whenever the runtime
22
+ * supports BigInt (i.e. essentially always: Node 10.4+, every
23
+ * evergreen browser, Deno).
24
+ */
25
+ export class CrunchyWithBigInt {
26
+ value;
27
+ // Only ever constructed from within this class (its own methods, or
28
+ // the bootstrap() below used once to seed the module-level
29
+ // singleton) - real values always arrive as number|string, through
30
+ // from_number()/from_string()/parse(), never as a raw bigint.
31
+ constructor(value) {
32
+ this.value = value;
33
+ }
34
+ static bootstrap() {
35
+ return new CrunchyWithBigInt(0n);
36
+ }
37
+ static valueOf(y) {
38
+ if (!(y instanceof CrunchyWithBigInt)) {
39
+ throw new Error("CrunchyWithBigInt operation received a non-CrunchyWithBigInt operand");
40
+ }
41
+ return y.value;
42
+ }
43
+ clone() {
44
+ return new CrunchyWithBigInt(this.value);
45
+ }
46
+ compare(y) {
47
+ const yv = CrunchyWithBigInt.valueOf(y);
48
+ if (this.value < yv)
49
+ return -1;
50
+ if (this.value > yv)
51
+ return 1;
52
+ return 0;
53
+ }
54
+ eq(oth) {
55
+ return this.compare(oth) == 0;
56
+ }
57
+ lt(oth) {
58
+ return this.compare(oth) < 0;
59
+ }
60
+ lte(oth) {
61
+ return this.compare(oth) <= 0;
62
+ }
63
+ gt(oth) {
64
+ return this.compare(oth) > 0;
65
+ }
66
+ gte(oth) {
67
+ return this.compare(oth) >= 0;
68
+ }
69
+ add(y) {
70
+ return new CrunchyWithBigInt(this.value + CrunchyWithBigInt.valueOf(y));
71
+ }
72
+ sub(y) {
73
+ return new CrunchyWithBigInt(this.value - CrunchyWithBigInt.valueOf(y));
74
+ }
75
+ // shl/shr shift the magnitude only and leave the sign untouched,
76
+ // matching CrunchyWith32Bit (native BigInt `>>` on a negative value
77
+ // performs an arithmetic/floor shift instead).
78
+ shl(s) {
79
+ const neg = this.value < 0n;
80
+ const abs = neg ? -this.value : this.value;
81
+ const shifted = abs << BigInt(s);
82
+ return new CrunchyWithBigInt(neg ? -shifted : shifted);
83
+ }
84
+ shr(s) {
85
+ const neg = this.value < 0n;
86
+ const abs = neg ? -this.value : this.value;
87
+ const shifted = abs >> BigInt(s);
88
+ return new CrunchyWithBigInt(neg ? -shifted : shifted);
89
+ }
90
+ // Floored modulo: result always has the same sign as `y` (or is
91
+ // zero), which matches the only path CrunchyWith32Bit.mod() is
92
+ // actually exercised with (non-negative operands).
93
+ mod(y) {
94
+ const yv = CrunchyWithBigInt.valueOf(y);
95
+ let r = this.value % yv;
96
+ if (r !== 0n && r < 0n !== yv < 0n) {
97
+ r += yv;
98
+ }
99
+ return new CrunchyWithBigInt(r);
100
+ }
101
+ // Never emits a sign, matching CrunchyWith32Bit.toString(), which
102
+ // only ever produces magnitude digits.
103
+ toString(radix = 10) {
104
+ const abs = this.value < 0n ? -this.value : this.value;
105
+ return abs.toString(radix);
106
+ }
107
+ create(value) {
108
+ if (value === undefined) {
109
+ return this.zero();
110
+ }
111
+ return typeof value === "number" ? this.from_number(value) : this.parse(value);
112
+ }
113
+ zero() {
114
+ return new CrunchyWithBigInt(0n);
115
+ }
116
+ one() {
117
+ return new CrunchyWithBigInt(1n);
118
+ }
119
+ two() {
120
+ return new CrunchyWithBigInt(2n);
121
+ }
122
+ parse(val) {
123
+ const ret = this.from_string(val, 10);
124
+ if (ret === null) {
125
+ throw new Error(`invalid decimal number: ${val}`);
126
+ }
127
+ return ret;
128
+ }
129
+ from_number(val) {
130
+ return new CrunchyWithBigInt(BigInt(Math.trunc(val)));
131
+ }
132
+ from_string(val, radix = 10) {
133
+ let s = val;
134
+ let neg = false;
135
+ if (s.startsWith("-")) {
136
+ neg = true;
137
+ s = s.slice(1);
138
+ }
139
+ let result = 0n;
140
+ const bigRadix = BigInt(radix);
141
+ for (const ch of s) {
142
+ const digit = parseInt(ch, radix);
143
+ if (isNaN(digit)) {
144
+ return null;
145
+ }
146
+ result = result * bigRadix + BigInt(digit);
147
+ }
148
+ return new CrunchyWithBigInt(neg ? -result : result);
149
+ }
150
+ from_string_or_throw(val, radix = 10) {
151
+ const ret = this.from_string(val, radix);
152
+ if (ret === null) {
153
+ throw new Error(`invalid number: ${val} (radix ${radix})`);
154
+ }
155
+ return ret;
156
+ }
157
+ }
7
158
  /**
8
159
  * @module Crunch
9
160
  * Radix: 28 bits
@@ -12,15 +163,14 @@
12
163
  * @param {boolean} rawIn - expect 28-bit arrays
13
164
  * @param {boolean} rawOut - return 28-bit arrays
14
165
  */
15
- // import Crunch from './crunch';
16
- export class Crunchy {
166
+ export class CrunchyWith32Bit {
17
167
  num = [];
18
168
  negative = false;
19
169
  static zeroes = ((n) => {
20
170
  return new Array(n).fill(0);
21
171
  })(60);
22
172
  clone() {
23
- const ret = new Crunchy();
173
+ const ret = new CrunchyWith32Bit();
24
174
  ret.num = this.num.slice();
25
175
  ret.negative = this.negative;
26
176
  return ret;
@@ -33,13 +183,13 @@ export class Crunchy {
33
183
  return out; // .transformOut();
34
184
  }
35
185
  static from_14bit(a) {
36
- const ret = new Crunchy();
186
+ const ret = new CrunchyWith32Bit();
37
187
  ret.num = a;
38
188
  return ret;
39
189
  }
40
190
  static from_8bit(a) {
41
191
  let x = [0, 0, 0, 0, 0, 0].slice((a.length - 1) % 7);
42
- const z = new Crunchy();
192
+ const z = new CrunchyWith32Bit();
43
193
  if (a[0] < 0) {
44
194
  a[0] *= -1;
45
195
  z.negative = true;
@@ -51,36 +201,68 @@ export class Crunchy {
51
201
  for (let i = 0; i < x.length; i += 7) {
52
202
  z.num.push(x[i] * 1048576 + x[i + 1] * 4096 + x[i + 2] * 16 + (x[i + 3] >> 4), (x[i + 3] & 15) * 16777216 + x[i + 4] * 65536 + x[i + 5] * 256 + x[i + 6]);
53
203
  }
54
- z.num = Crunchy.removeLeadingZeros(z.num);
204
+ z.num = CrunchyWith32Bit.removeLeadingZeros(z.num);
55
205
  return z;
56
206
  }
57
- static parse(val) {
58
- return Crunchy.from_string(val, 10);
207
+ static _zero = CrunchyWith32Bit.from_8bit([0]);
208
+ static _one = CrunchyWith32Bit.from_8bit([1]);
209
+ static _two = CrunchyWith32Bit.from_8bit([2]);
210
+ create(value) {
211
+ if (value === undefined) {
212
+ return this.zero();
213
+ }
214
+ return typeof value === "number" ? this.from_number(value) : this.parse(value);
215
+ }
216
+ zero() {
217
+ return CrunchyWith32Bit._zero;
218
+ }
219
+ one() {
220
+ return CrunchyWith32Bit._one;
59
221
  }
60
- static from_number(val) {
61
- return Crunchy.parse("" + val);
222
+ two() {
223
+ return CrunchyWith32Bit._two;
62
224
  }
63
- static from_string(val, radix = 10) {
225
+ parse(val) {
226
+ const ret = this.from_string(val, 10);
227
+ if (ret === null) {
228
+ throw new Error(`invalid decimal number: ${val}`);
229
+ }
230
+ return ret;
231
+ }
232
+ from_number(val) {
233
+ return this.parse("" + val);
234
+ }
235
+ // Same as from_string(), but for literals that are known to be valid
236
+ // numbers (e.g. hardcoded hex constants), so the caller doesn't need
237
+ // to deal with a nullable result it can never actually observe.
238
+ from_string_or_throw(val, radix = 10) {
239
+ const ret = this.from_string(val, radix);
240
+ if (ret === null) {
241
+ throw new Error(`invalid number: ${val} (radix ${radix})`);
242
+ }
243
+ return ret;
244
+ }
245
+ from_string(val, radix = 10) {
64
246
  const x = val.split("");
65
- let p = Crunchy.one();
66
- let a = Crunchy.zero();
67
- const b = Crunchy.from_8bit([radix]);
68
- // console.log("fromstring:one:", p);
69
- // console.log("fromstring:zero:", a);
70
- // console.log("fromstring:radix:", b);
247
+ let p = CrunchyWith32Bit._one;
248
+ let a = CrunchyWith32Bit._zero;
249
+ const b = CrunchyWith32Bit.from_8bit([radix]);
71
250
  let n = false;
72
251
  if (x[0] === "-") {
73
252
  n = true;
74
253
  x.shift();
75
254
  }
76
255
  while (x.length > 0) {
77
- const c = parseInt(x.pop(), radix);
256
+ const popped = x.pop();
257
+ if (popped === undefined) {
258
+ break;
259
+ }
260
+ const c = parseInt(popped, radix);
78
261
  if (isNaN(c)) {
79
262
  console.error("from_string:", val);
80
263
  return null;
81
264
  }
82
- // console.log(a,p,b);
83
- a = a.add(p.mul(Crunchy.from_8bit([c])));
265
+ a = a.add(p.mul(CrunchyWith32Bit.from_8bit([c])));
84
266
  p = p.mul(b);
85
267
  }
86
268
  a.negative = n;
@@ -89,22 +271,21 @@ export class Crunchy {
89
271
  to_8bit() {
90
272
  const x = [0].slice((this.num.length - 1) % 2).concat(this.num);
91
273
  let z = [];
92
- // z.num = [];
93
274
  for (let i = 0; i < x.length;) {
94
275
  const u = x[i++];
95
276
  const v = x[i++];
96
277
  z.push(u >> 20, (u >> 12) & 255, (u >> 4) & 255, ((u << 4) | (v >> 24)) & 255, (v >> 16) & 255, (v >> 8) & 255, v & 255);
97
278
  }
98
- // console.log("co:", a, z);
99
- z = Crunchy.removeLeadingZeros(z);
279
+ z = CrunchyWith32Bit.removeLeadingZeros(z);
100
280
  if (this.negative) {
101
281
  z[0] *= -1;
102
282
  }
103
283
  return z;
104
284
  }
105
285
  compare(y) {
286
+ const yy = y;
106
287
  const xl = this.num.length;
107
- const yl = y.num.length; // zero front pad problem
288
+ const yl = yy.num.length; // zero front pad problem
108
289
  if (xl < yl) {
109
290
  return -1;
110
291
  }
@@ -112,10 +293,9 @@ export class Crunchy {
112
293
  return 1;
113
294
  }
114
295
  for (let i = 0; i < xl; i++) {
115
- // console.log("x=y:", i, x, y);
116
- if (this.num[i] < y.num[i])
296
+ if (this.num[i] < yy.num[i])
117
297
  return -1;
118
- if (this.num[i] > y.num[i])
298
+ if (this.num[i] > yy.num[i])
119
299
  return 1;
120
300
  }
121
301
  return 0;
@@ -123,12 +303,12 @@ export class Crunchy {
123
303
  eq(oth) {
124
304
  return this.compare(oth) == 0;
125
305
  }
126
- lte(oth) {
127
- return this.compare(oth) <= 0;
128
- }
129
306
  lt(oth) {
130
307
  return this.compare(oth) < 0;
131
308
  }
309
+ lte(oth) {
310
+ return this.compare(oth) <= 0;
311
+ }
132
312
  gt(oth) {
133
313
  return this.compare(oth) > 0;
134
314
  }
@@ -136,18 +316,19 @@ export class Crunchy {
136
316
  return this.compare(oth) >= 0;
137
317
  }
138
318
  add(y) {
319
+ const yy = y;
139
320
  let z;
140
321
  if (this.negative) {
141
- if (y.negative) {
142
- z = this.unsigned_add(y);
322
+ if (yy.negative) {
323
+ z = this.unsigned_add(yy);
143
324
  z.negative = true;
144
325
  }
145
326
  else {
146
- z = y.unsigned_sub(this, false).cut();
327
+ z = yy.unsigned_sub(this, false).cut();
147
328
  }
148
329
  }
149
330
  else {
150
- z = y.negative ? this.unsigned_sub(y, false).cut() : this.unsigned_add(y);
331
+ z = yy.negative ? this.unsigned_sub(yy, false).cut() : this.unsigned_add(yy);
151
332
  }
152
333
  return z;
153
334
  }
@@ -156,17 +337,15 @@ export class Crunchy {
156
337
  const t = _y.num.length;
157
338
  let i = Math.max(n, t);
158
339
  let c = 0;
159
- const z = Crunchy.zeroes.slice(0, i);
160
- // console.log("add:1:", new Date());
340
+ const z = CrunchyWith32Bit.zeroes.slice(0, i);
161
341
  let x = this.num;
162
342
  let y = _y.num;
163
343
  if (n < t) {
164
- x = Crunchy.zeroes.slice(0, t - n).concat(this.num);
344
+ x = CrunchyWith32Bit.zeroes.slice(0, t - n).concat(this.num);
165
345
  }
166
346
  else if (n > t) {
167
- y = Crunchy.zeroes.slice(0, n - t).concat(y);
347
+ y = CrunchyWith32Bit.zeroes.slice(0, n - t).concat(y);
168
348
  }
169
- // console.log("add:2:", new Date());
170
349
  for (i -= 1; i >= 0; i--) {
171
350
  z[i] = x[i] + y[i] + c;
172
351
  if (z[i] > 268435455) {
@@ -177,31 +356,27 @@ export class Crunchy {
177
356
  c = 0;
178
357
  }
179
358
  }
180
- // console.log("add:3:", new Date());
181
359
  if (c === 1) {
182
360
  z.unshift(c);
183
361
  }
184
- const ret = new Crunchy();
362
+ const ret = new CrunchyWith32Bit();
185
363
  ret.num = z;
186
- // console.log("add:4:", new Date());
187
364
  return ret;
188
365
  }
189
366
  sub(y) {
367
+ const yy = y;
190
368
  let z;
191
369
  if (this.negative) {
192
- if (y.negative) {
193
- // console.log("sub-c1");
194
- z = y.unsigned_sub(this, false).cut();
370
+ if (yy.negative) {
371
+ z = yy.unsigned_sub(this, false).cut();
195
372
  }
196
373
  else {
197
- // console.log("sub-c2");
198
- z = this.unsigned_add(y);
374
+ z = this.unsigned_add(yy);
199
375
  z.negative = true;
200
376
  }
201
377
  }
202
378
  else {
203
- // console.log("sub-c3", this, y);
204
- z = y.negative ? this.unsigned_add(y) : this.unsigned_sub(y, false).cut();
379
+ z = yy.negative ? this.unsigned_add(yy) : this.unsigned_sub(yy, false).cut();
205
380
  }
206
381
  return z;
207
382
  }
@@ -210,33 +385,30 @@ export class Crunchy {
210
385
  const t = _y.num.length;
211
386
  let i = Math.max(n, t);
212
387
  let c = 0;
213
- const z = Crunchy.zeroes.slice(0, i);
388
+ const z = CrunchyWith32Bit.zeroes.slice(0, i);
214
389
  let x = this.num;
215
390
  let y = _y.num;
216
391
  if (n < t) {
217
- x = Crunchy.zeroes.slice(0, t - n).concat(x);
392
+ x = CrunchyWith32Bit.zeroes.slice(0, t - n).concat(x);
218
393
  }
219
394
  else if (n > t) {
220
- y = Crunchy.zeroes.slice(0, n - t).concat(y);
395
+ y = CrunchyWith32Bit.zeroes.slice(0, n - t).concat(y);
221
396
  }
222
397
  for (i -= 1; i >= 0; i--) {
223
398
  z[i] = x[i] - y[i] - c;
224
- // console.log(z, x, y);
225
399
  if (z[i] < 0) {
226
400
  c = 1;
227
- // console.log("pre:+", z[i], z);
228
401
  z[i] += 268435456;
229
- // console.log("pre:-", z[i], z);
230
402
  }
231
403
  else {
232
404
  c = 0;
233
405
  }
234
406
  }
235
- let cry = new Crunchy();
407
+ let cry = new CrunchyWith32Bit();
236
408
  cry.num = z;
237
409
  if (c === 1 && !internal) {
238
- const zero = new Crunchy();
239
- zero.num = Crunchy.zeroes.slice(0, z.length);
410
+ const zero = new CrunchyWith32Bit();
411
+ zero.num = CrunchyWith32Bit.zeroes.slice(0, z.length);
240
412
  cry = zero.unsigned_sub(cry, true);
241
413
  cry.negative = true;
242
414
  }
@@ -260,19 +432,10 @@ export class Crunchy {
260
432
  z.negative = this.negative;
261
433
  }
262
434
  if (ls) {
263
- z.num = z.num.concat(Crunchy.zeroes.slice(0, ls));
264
- // return (ls) ? z.concat(zeroes.slice(0, ls)) : z;
435
+ z.num = z.num.concat(CrunchyWith32Bit.zeroes.slice(0, ls));
265
436
  }
266
- // console.log(this, s, z);
267
437
  return z;
268
438
  }
269
- // public leftShift(s: number): Crunchy {
270
- // return this.transformIn()
271
- // Crunchy.transformOut(Crunch.lsh(.pop(), s));
272
- // }
273
- // public shl(num: number): Crunchy {
274
- // return Crunchy.from_8bit(Crunch.leftShift(this.num, num));
275
- // }
276
439
  rsh(s) {
277
440
  const ss = s % 28;
278
441
  const ls = Math.floor(s / 28);
@@ -290,23 +453,18 @@ export class Crunchy {
290
453
  }
291
454
  return z;
292
455
  }
293
- // public shr(num: number): Crunchy {
294
- // //console.log("shr<<", num, this.num);
295
- // let res = this.rightShift(this.num, num);
296
- // // console.log("shr>>", num, this.num, res);
297
- // return Crunchy.from_8bit(res);
298
- // }
299
456
  mul(y) {
457
+ const yy = y;
300
458
  let yl;
301
459
  let yh;
302
460
  let c;
303
461
  const n = this.num.length;
304
- let i = y.num.length;
305
- const z = Crunchy.zeroes.slice(0, n + i);
462
+ let i = yy.num.length;
463
+ const z = CrunchyWith32Bit.zeroes.slice(0, n + i);
306
464
  while (i--) {
307
465
  c = 0;
308
- yl = y.num[i] & 16383;
309
- yh = y.num[i] >> 14;
466
+ yl = yy.num[i] & 16383;
467
+ yh = yy.num[i] >> 14;
310
468
  for (let j = n - 1, xl, xh, t1, t2; j >= 0; j--) {
311
469
  xl = this.num[j] & 16383;
312
470
  xh = this.num[j] >> 14;
@@ -320,8 +478,8 @@ export class Crunchy {
320
478
  if (z[0] === 0) {
321
479
  z.shift();
322
480
  }
323
- const ret = new Crunchy();
324
- ret.negative = this.negative !== y.negative;
481
+ const ret = new CrunchyWith32Bit();
482
+ ret.negative = this.negative !== yy.negative;
325
483
  ret.num = z;
326
484
  return ret;
327
485
  }
@@ -334,25 +492,12 @@ export class Crunchy {
334
492
  return z;
335
493
  }
336
494
  }
337
- // public transformIn(): Crunchy {
338
- // // return Array.prototype.slice.call(a).map((v: Crunchy) => {
339
- // // return Crunchy.from_8bit(v.num.slice())
340
- // // });
341
- // return null;
342
- // }
343
- // public transformOut(): Crunchy {
344
- // console.log("in-transformOut:", this);
345
- // let ret = Crunchy.co(this);
346
- // console.log("out-transformOut:", this, ret);
347
- // return ret;
348
- // }
349
495
  shr(s) {
350
496
  const my = this.rsh(s);
351
497
  return my.cut();
352
498
  }
353
499
  shl(s) {
354
500
  return this.lsh(s).cut();
355
- // Crunch.transformOut(Crunch.lsh(Crunch.transformIn([x]).pop(), s));
356
501
  }
357
502
  cut() {
358
503
  const out = this.clone();
@@ -364,31 +509,27 @@ export class Crunchy {
364
509
  while (out.num[0] === 0 && out.num.length > 1) {
365
510
  out.num.shift();
366
511
  }
367
- return out; // .transformOut();
512
+ return out;
368
513
  }
369
- // return Crunchy.transformOut(this.num
370
- // cut.apply(null, transformIn(arguments))
371
- // );
372
- // }
373
514
  div(y, internal = false) {
374
- if (y.num.length === 1 && y.num[0] === 0) {
515
+ const yy = y;
516
+ if (yy.num.length === 1 && yy.num[0] === 0) {
375
517
  return null;
376
518
  }
377
- // var u, v, xt, yt, d, q, k, i, z;
378
519
  let u;
379
520
  let v;
380
- const s = Crunchy.msb(y.num[0]) - 1;
521
+ const s = (CrunchyWith32Bit.msb(yy.num[0]) ?? 0) - 1;
381
522
  if (s > 0) {
382
523
  u = this.lsh(s);
383
- v = y.lsh(s);
524
+ v = yy.lsh(s);
384
525
  }
385
526
  else {
386
527
  u = this.clone();
387
- v = y.clone();
528
+ v = yy.clone();
388
529
  }
389
530
  const d = u.num.length - v.num.length;
390
531
  const q = [0];
391
- let k = Crunchy.from_14bit(v.num.concat(Crunchy.zeroes.slice(0, d)));
532
+ let k = CrunchyWith32Bit.from_14bit(v.num.concat(CrunchyWith32Bit.zeroes.slice(0, d)));
392
533
  const yt = v.num[0] * 268435456 + v.num[1];
393
534
  // only cmp as last resort
394
535
  while (u.num[0] > k.num[0] || (u.num[0] === k.num[0] && u.compare(k) > -1)) {
@@ -402,11 +543,11 @@ export class Crunchy {
402
543
  // condition check can fail due to precision problem at 28-bit
403
544
  q[i]--;
404
545
  }
405
- k = v.mul(Crunchy.from_14bit([q[i]]));
406
- k.num = k.num.concat(Crunchy.zeroes.slice(0, d - i)); // concat after multiply, save cycles
546
+ k = v.mul(CrunchyWith32Bit.from_14bit([q[i]]));
547
+ k.num = k.num.concat(CrunchyWith32Bit.zeroes.slice(0, d - i)); // concat after multiply, save cycles
407
548
  u = u.unsigned_sub(k, false);
408
549
  if (u.negative) {
409
- u = Crunchy.from_14bit(v.num.concat(Crunchy.zeroes.slice(0, d - i))).unsigned_sub(u, false);
550
+ u = CrunchyWith32Bit.from_14bit(v.num.concat(CrunchyWith32Bit.zeroes.slice(0, d - i))).unsigned_sub(u, false);
410
551
  q[i]--;
411
552
  }
412
553
  }
@@ -415,27 +556,35 @@ export class Crunchy {
415
556
  z = s > 0 ? u.cut().rsh(s) : u.cut();
416
557
  }
417
558
  else {
418
- z = Crunchy.from_14bit(Crunchy.removeLeadingZeros(q));
419
- z.negative = this.negative !== y.negative;
559
+ z = CrunchyWith32Bit.from_14bit(CrunchyWith32Bit.removeLeadingZeros(q));
560
+ z.negative = this.negative !== yy.negative;
420
561
  }
421
562
  return z;
422
563
  }
423
- // public sub(cry: Crunchy): Crunchy {
424
- // return Crunchy.from_8bit(Crunch.sub(this.num, cry.num));
425
- // }
564
+ // Same as div(), but for divisors that are known to be non-zero, so the
565
+ // caller doesn't need to deal with a nullable result it can never
566
+ // actually observe.
567
+ div_or_throw(y, internal = false) {
568
+ const ret = this.div(y, internal);
569
+ if (ret === null) {
570
+ throw new Error("division by zero");
571
+ }
572
+ return ret;
573
+ }
426
574
  mod(y) {
427
575
  // For negative x, cmp doesn't work and result of div is negative
428
576
  // so take result away from the modulus to get the correct result
577
+ const yy = y;
429
578
  if (this.negative) {
430
- return y.sub(this.div(y, true));
579
+ return yy.sub(this.div_or_throw(yy, true));
431
580
  }
432
- switch (this.compare(y)) {
581
+ switch (this.compare(yy)) {
433
582
  case -1:
434
583
  return this;
435
584
  case 0:
436
- return Crunchy.from_8bit([0]);
585
+ return CrunchyWith32Bit.from_8bit([0]);
437
586
  default:
438
- return this.div(y, true);
587
+ return this.div_or_throw(yy, true);
439
588
  }
440
589
  }
441
590
  mds(n) {
@@ -450,35 +599,18 @@ export class Crunchy {
450
599
  const a = [];
451
600
  let i = 0;
452
601
  let x = this.clone();
453
- // console.log("toString:", new Date());
454
- const cradix = Crunchy.from_8bit([radix]);
455
- const zero = Crunchy.zero();
602
+ const cradix = CrunchyWith32Bit.from_8bit([radix]);
603
+ const zero = CrunchyWith32Bit._zero;
456
604
  do {
457
605
  const digit = x.mds(radix);
458
- x = x.div(cradix);
606
+ x = x.div_or_throw(cradix);
459
607
  a[i++] = "0123456789abcdef"[digit];
460
- // console.log("1-toString:", x, radix, digit, a.join(""));
461
- // console.log("2-toString:", x, radix, digit, a, Crunch.compare(x, Crunchy._zero.num));
462
608
  } while (!x.eq(zero));
463
- // console.log("Crunchy.tostring-1:", a);
464
- const ret = a.reverse().join("");
465
- // console.log("Crunchy.tostring-2:", ret, new Date());
466
- return ret;
467
- }
468
- // public toNumber() : number {
469
- // return null;
470
- // }
471
- static _zero = Crunchy.from_8bit([0]);
472
- static zero() {
473
- return Crunchy._zero;
474
- }
475
- static _one = Crunchy.from_8bit([1]);
476
- static one() {
477
- return Crunchy._one;
478
- }
479
- static _two = Crunchy.from_8bit([2]);
480
- static two() {
481
- return Crunchy._two;
609
+ return a.reverse().join("");
482
610
  }
483
611
  }
612
+ // Feature detection, computed once at module load.
613
+ export const hasBigInt = typeof BigInt !== "undefined";
614
+ const crunchySingleton = hasBigInt ? CrunchyWithBigInt.bootstrap() : new CrunchyWith32Bit();
615
+ export const Crunchy = crunchySingleton;
484
616
  export default Crunchy;
@@ -16,9 +16,9 @@ export declare class IpBits {
16
16
  as_compressed_string(bu: Crunchy): string;
17
17
  as_uncompressed_string(bu: Crunchy): string;
18
18
  dns_part_format(i: number): string;
19
- static _v4: IpBits;
19
+ static _v4: IpBits | null;
20
20
  static v4(): IpBits;
21
- static _v6: IpBits;
21
+ static _v6: IpBits | null;
22
22
  static v6(): IpBits;
23
23
  static ipv4_as_compressed(ip_bits: IpBits, host_address: Crunchy): string;
24
24
  static ipv6_as_compressed(ip_bits: IpBits, host_address: Crunchy): string;