multichain-address-validator 0.0.1

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 (131) hide show
  1. package/.editorconfig +10 -0
  2. package/.travis.yml +11 -0
  3. package/.vscode/launch.json +23 -0
  4. package/LICENSE +21 -0
  5. package/README.md +87 -0
  6. package/config/esbuild.inject.js +1 -0
  7. package/config/esbuild.ts +14 -0
  8. package/dist/chain-validators.d.ts +2 -0
  9. package/dist/chain-validators.js +98 -0
  10. package/dist/crypto/base32.d.ts +5 -0
  11. package/dist/crypto/base32.js +64 -0
  12. package/dist/crypto/base58.d.ts +4 -0
  13. package/dist/crypto/base58.js +42 -0
  14. package/dist/crypto/bech32.d.ts +17 -0
  15. package/dist/crypto/bech32.js +124 -0
  16. package/dist/crypto/biginteger.d.ts +57 -0
  17. package/dist/crypto/biginteger.js +1311 -0
  18. package/dist/crypto/blake256.d.ts +22 -0
  19. package/dist/crypto/blake256.js +169 -0
  20. package/dist/crypto/blake2b.d.ts +13 -0
  21. package/dist/crypto/blake2b.js +242 -0
  22. package/dist/crypto/cnBase58.d.ts +7 -0
  23. package/dist/crypto/cnBase58.js +209 -0
  24. package/dist/crypto/segwit_addr.d.ts +12 -0
  25. package/dist/crypto/segwit_addr.js +102 -0
  26. package/dist/crypto/utils.d.ts +26 -0
  27. package/dist/crypto/utils.js +123 -0
  28. package/dist/helpers.d.ts +2 -0
  29. package/dist/helpers.js +5 -0
  30. package/dist/multichain-address-validator.bundle.min.js +17 -0
  31. package/dist/multichain-address-validator.d.ts +2 -0
  32. package/dist/multichain-address-validator.js +8 -0
  33. package/dist/types.d.ts +15 -0
  34. package/dist/types.js +5 -0
  35. package/dist/validators/algorand_validator.d.ts +5 -0
  36. package/dist/validators/algorand_validator.js +23 -0
  37. package/dist/validators/base58_validator.d.ts +4 -0
  38. package/dist/validators/base58_validator.js +31 -0
  39. package/dist/validators/bch_validator.d.ts +13 -0
  40. package/dist/validators/bch_validator.js +50 -0
  41. package/dist/validators/bip173_validator.d.ts +7 -0
  42. package/dist/validators/bip173_validator.js +12 -0
  43. package/dist/validators/bitcoin_validator.d.ts +12 -0
  44. package/dist/validators/bitcoin_validator.js +68 -0
  45. package/dist/validators/cardano_validator.d.ts +5 -0
  46. package/dist/validators/cardano_validator.js +41 -0
  47. package/dist/validators/eos_validator.d.ts +5 -0
  48. package/dist/validators/eos_validator.js +10 -0
  49. package/dist/validators/ethereum_validator.d.ts +6 -0
  50. package/dist/validators/ethereum_validator.js +30 -0
  51. package/dist/validators/index.d.ts +16 -0
  52. package/dist/validators/index.js +16 -0
  53. package/dist/validators/monero_validator.d.ts +5 -0
  54. package/dist/validators/monero_validator.js +58 -0
  55. package/dist/validators/nano_validator.d.ts +6 -0
  56. package/dist/validators/nano_validator.js +23 -0
  57. package/dist/validators/nem_validator.d.ts +5 -0
  58. package/dist/validators/nem_validator.js +14 -0
  59. package/dist/validators/polkadot_validator.d.ts +5 -0
  60. package/dist/validators/polkadot_validator.js +49 -0
  61. package/dist/validators/ripple_validator.d.ts +10 -0
  62. package/dist/validators/ripple_validator.js +26 -0
  63. package/dist/validators/sia_validator.d.ts +5 -0
  64. package/dist/validators/sia_validator.js +27 -0
  65. package/dist/validators/solana_validator.d.ts +5 -0
  66. package/dist/validators/solana_validator.js +10 -0
  67. package/dist/validators/tezos_validator.d.ts +5 -0
  68. package/dist/validators/tezos_validator.js +30 -0
  69. package/dist/validators/tron_validator.d.ts +8 -0
  70. package/dist/validators/tron_validator.js +45 -0
  71. package/dist/validators/xlm_validator.d.ts +6 -0
  72. package/dist/validators/xlm_validator.js +32 -0
  73. package/index.html +12 -0
  74. package/package.json +353 -0
  75. package/src/chain-validators.ts +131 -0
  76. package/src/crypto/base32.ts +66 -0
  77. package/src/crypto/base58.ts +46 -0
  78. package/src/crypto/bech32.js +132 -0
  79. package/src/crypto/biginteger.js +1426 -0
  80. package/src/crypto/blake256.js +186 -0
  81. package/src/crypto/blake2b.js +276 -0
  82. package/src/crypto/cnBase58.js +226 -0
  83. package/src/crypto/segwit_addr.js +112 -0
  84. package/src/crypto/utils.ts +133 -0
  85. package/src/helpers.ts +7 -0
  86. package/src/multichain-address-validator.ts +11 -0
  87. package/src/types.ts +18 -0
  88. package/src/validators/algorand_validator.ts +28 -0
  89. package/src/validators/base58_validator.ts +32 -0
  90. package/src/validators/bch_validator.ts +66 -0
  91. package/src/validators/bip173_validator.ts +19 -0
  92. package/src/validators/bitcoin_validator.ts +94 -0
  93. package/src/validators/cardano_validator.ts +50 -0
  94. package/src/validators/eos_validator.ts +13 -0
  95. package/src/validators/ethereum_validator.ts +37 -0
  96. package/src/validators/index.ts +16 -0
  97. package/src/validators/monero_validator.ts +72 -0
  98. package/src/validators/nano_validator.ts +32 -0
  99. package/src/validators/nem_validator.ts +18 -0
  100. package/src/validators/polkadot_validator.ts +57 -0
  101. package/src/validators/ripple_validator.ts +36 -0
  102. package/src/validators/sia_validator.ts +33 -0
  103. package/src/validators/solana_validator.ts +12 -0
  104. package/src/validators/tezos_validator.ts +36 -0
  105. package/src/validators/tron_validator.ts +59 -0
  106. package/src/validators/xlm_validator.ts +42 -0
  107. package/test/addresses/addresses.ts +45 -0
  108. package/test/addresses/algorand.json +6 -0
  109. package/test/addresses/bch-testnet.json +4 -0
  110. package/test/addresses/bch.json +12 -0
  111. package/test/addresses/btc-testnet.json +6 -0
  112. package/test/addresses/btc.json +14 -0
  113. package/test/addresses/cardano.json +8 -0
  114. package/test/addresses/doge.json +7 -0
  115. package/test/addresses/eos.json +6 -0
  116. package/test/addresses/evm.json +21 -0
  117. package/test/addresses/invalid.json +15 -0
  118. package/test/addresses/ltc-testnet.json +7 -0
  119. package/test/addresses/ltc.json +9 -0
  120. package/test/addresses/monero.json +7 -0
  121. package/test/addresses/nano.json +12 -0
  122. package/test/addresses/nem.json +4 -0
  123. package/test/addresses/polkadot.json +8 -0
  124. package/test/addresses/ripple.json +11 -0
  125. package/test/addresses/sia.json +6 -0
  126. package/test/addresses/solana.json +8 -0
  127. package/test/addresses/tezos.json +9 -0
  128. package/test/addresses/tron.json +6 -0
  129. package/test/addresses/xlm.json +12 -0
  130. package/test/multichain-address-validator.test.ts +1589 -0
  131. package/tsconfig.json +23 -0
@@ -0,0 +1,1311 @@
1
+ /*
2
+ JavaScript BigInteger library version 0.9.1
3
+ http://silentmatt.com/biginteger/
4
+ Copyright (c) 2009 Matthew Crumley <email@matthewcrumley.com>
5
+ Copyright (c) 2010,2011 by John Tobey <John.Tobey@gmail.com>
6
+ Licensed under the MIT license.
7
+ Support for arbitrary internal representation base was added by
8
+ Vitaly Magerya.
9
+ */
10
+ /*
11
+ File: biginteger.js
12
+ Exports:
13
+ <BigInteger>
14
+ */
15
+ "use strict";
16
+ /*
17
+ Class: BigInteger
18
+ An arbitrarily-large integer.
19
+ <BigInteger> objects should be considered immutable. None of the "built-in"
20
+ methods modify *this* or their arguments. All properties should be
21
+ considered private.
22
+ All the methods of <BigInteger> instances can be called "statically". The
23
+ static versions are convenient if you don't already have a <BigInteger>
24
+ object.
25
+ As an example, these calls are equivalent.
26
+ > BigInteger(4).multiply(5); // returns BigInteger(20);
27
+ > BigInteger.multiply(4, 5); // returns BigInteger(20);
28
+ > var a = 42;
29
+ > var a = BigInteger.toJSValue("0b101010"); // Not completely useless...
30
+ */
31
+ var CONSTRUCT = {}; // Unique token to call "private" version of constructor
32
+ /*
33
+ Constructor: BigInteger()
34
+ Convert a value to a <BigInteger>.
35
+ Although <BigInteger()> is the constructor for <BigInteger> objects, it is
36
+ best not to call it as a constructor. If *n* is a <BigInteger> object, it is
37
+ simply returned as-is. Otherwise, <BigInteger()> is equivalent to <parse>
38
+ without a radix argument.
39
+ > var n0 = BigInteger(); // Same as <BigInteger.ZERO>
40
+ > var n1 = BigInteger("123"); // Create a new <BigInteger> with value 123
41
+ > var n2 = BigInteger(123); // Create a new <BigInteger> with value 123
42
+ > var n3 = BigInteger(n2); // Return n2, unchanged
43
+ The constructor form only takes an array and a sign. *n* must be an
44
+ array of numbers in little-endian order, where each digit is between 0
45
+ and BigInteger.base. The second parameter sets the sign: -1 for
46
+ negative, +1 for positive, or 0 for zero. The array is *not copied and
47
+ may be modified*. If the array contains only zeros, the sign parameter
48
+ is ignored and is forced to zero.
49
+ > new BigInteger([5], -1): create a new BigInteger with value -5
50
+ Parameters:
51
+ n - Value to convert to a <BigInteger>.
52
+ Returns:
53
+ A <BigInteger> value.
54
+ See Also:
55
+ <parse>, <BigInteger>
56
+ */
57
+ export function BigInteger(n, s, token) {
58
+ if (token !== CONSTRUCT) {
59
+ if (n instanceof BigInteger) {
60
+ return n;
61
+ }
62
+ else if (typeof n === "undefined") {
63
+ return ZERO;
64
+ }
65
+ return BigInteger.parse(n);
66
+ }
67
+ n = n || []; // Provide the nullary constructor for subclasses.
68
+ while (n.length && !n[n.length - 1]) {
69
+ --n.length;
70
+ }
71
+ this._d = n;
72
+ this._s = n.length ? (s || 1) : 0;
73
+ }
74
+ BigInteger._construct = function (n, s) {
75
+ return new BigInteger(n, s, CONSTRUCT);
76
+ };
77
+ // Base-10 speedup hacks in parse, toString, exp10 and log functions
78
+ // require base to be a power of 10. 10^7 is the largest such power
79
+ // that won't cause a precision loss when digits are multiplied.
80
+ var BigInteger_base = 10000000;
81
+ var BigInteger_base_log10 = 7;
82
+ BigInteger.base = BigInteger_base;
83
+ BigInteger.base_log10 = BigInteger_base_log10;
84
+ var ZERO = new BigInteger([], 0, CONSTRUCT);
85
+ // Constant: ZERO
86
+ // <BigInteger> 0.
87
+ BigInteger.ZERO = ZERO;
88
+ var ONE = new BigInteger([1], 1, CONSTRUCT);
89
+ // Constant: ONE
90
+ // <BigInteger> 1.
91
+ BigInteger.ONE = ONE;
92
+ var M_ONE = new BigInteger(ONE._d, -1, CONSTRUCT);
93
+ // Constant: M_ONE
94
+ // <BigInteger> -1.
95
+ BigInteger.M_ONE = M_ONE;
96
+ // Constant: _0
97
+ // Shortcut for <ZERO>.
98
+ BigInteger._0 = ZERO;
99
+ // Constant: _1
100
+ // Shortcut for <ONE>.
101
+ BigInteger._1 = ONE;
102
+ /*
103
+ Constant: small
104
+ Array of <BigIntegers> from 0 to 36.
105
+ These are used internally for parsing, but useful when you need a "small"
106
+ <BigInteger>.
107
+ See Also:
108
+ <ZERO>, <ONE>, <_0>, <_1>
109
+ */
110
+ BigInteger.small = [
111
+ ZERO,
112
+ ONE,
113
+ /* Assuming BigInteger_base > 36 */
114
+ new BigInteger([2], 1, CONSTRUCT),
115
+ new BigInteger([3], 1, CONSTRUCT),
116
+ new BigInteger([4], 1, CONSTRUCT),
117
+ new BigInteger([5], 1, CONSTRUCT),
118
+ new BigInteger([6], 1, CONSTRUCT),
119
+ new BigInteger([7], 1, CONSTRUCT),
120
+ new BigInteger([8], 1, CONSTRUCT),
121
+ new BigInteger([9], 1, CONSTRUCT),
122
+ new BigInteger([10], 1, CONSTRUCT),
123
+ new BigInteger([11], 1, CONSTRUCT),
124
+ new BigInteger([12], 1, CONSTRUCT),
125
+ new BigInteger([13], 1, CONSTRUCT),
126
+ new BigInteger([14], 1, CONSTRUCT),
127
+ new BigInteger([15], 1, CONSTRUCT),
128
+ new BigInteger([16], 1, CONSTRUCT),
129
+ new BigInteger([17], 1, CONSTRUCT),
130
+ new BigInteger([18], 1, CONSTRUCT),
131
+ new BigInteger([19], 1, CONSTRUCT),
132
+ new BigInteger([20], 1, CONSTRUCT),
133
+ new BigInteger([21], 1, CONSTRUCT),
134
+ new BigInteger([22], 1, CONSTRUCT),
135
+ new BigInteger([23], 1, CONSTRUCT),
136
+ new BigInteger([24], 1, CONSTRUCT),
137
+ new BigInteger([25], 1, CONSTRUCT),
138
+ new BigInteger([26], 1, CONSTRUCT),
139
+ new BigInteger([27], 1, CONSTRUCT),
140
+ new BigInteger([28], 1, CONSTRUCT),
141
+ new BigInteger([29], 1, CONSTRUCT),
142
+ new BigInteger([30], 1, CONSTRUCT),
143
+ new BigInteger([31], 1, CONSTRUCT),
144
+ new BigInteger([32], 1, CONSTRUCT),
145
+ new BigInteger([33], 1, CONSTRUCT),
146
+ new BigInteger([34], 1, CONSTRUCT),
147
+ new BigInteger([35], 1, CONSTRUCT),
148
+ new BigInteger([36], 1, CONSTRUCT)
149
+ ];
150
+ // Used for parsing/radix conversion
151
+ BigInteger.digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
152
+ /*
153
+ Method: toString
154
+ Convert a <BigInteger> to a string.
155
+ When *base* is greater than 10, letters are upper case.
156
+ Parameters:
157
+ base - Optional base to represent the number in (default is base 10).
158
+ Must be between 2 and 36 inclusive, or an Error will be thrown.
159
+ Returns:
160
+ The string representation of the <BigInteger>.
161
+ */
162
+ BigInteger.prototype.toString = function (base) {
163
+ base = +base || 10;
164
+ if (base < 2 || base > 36) {
165
+ throw new Error("illegal radix " + base + ".");
166
+ }
167
+ if (this._s === 0) {
168
+ return "0";
169
+ }
170
+ if (base === 10) {
171
+ var str = this._s < 0 ? "-" : "";
172
+ str += this._d[this._d.length - 1].toString();
173
+ for (var i = this._d.length - 2; i >= 0; i--) {
174
+ var group = this._d[i].toString();
175
+ while (group.length < BigInteger_base_log10)
176
+ group = '0' + group;
177
+ str += group;
178
+ }
179
+ return str;
180
+ }
181
+ else {
182
+ var numerals = BigInteger.digits;
183
+ base = BigInteger.small[base];
184
+ var sign = this._s;
185
+ var n = this.abs();
186
+ var digits = [];
187
+ var digit;
188
+ while (n._s !== 0) {
189
+ var divmod = n.divRem(base);
190
+ n = divmod[0];
191
+ digit = divmod[1];
192
+ // TODO: This could be changed to unshift instead of reversing at the end.
193
+ // Benchmark both to compare speeds.
194
+ digits.push(numerals[digit.valueOf()]);
195
+ }
196
+ return (sign < 0 ? "-" : "") + digits.reverse().join("");
197
+ }
198
+ };
199
+ // Verify strings for parsing
200
+ BigInteger.radixRegex = [
201
+ /^$/,
202
+ /^$/,
203
+ /^[01]*$/,
204
+ /^[012]*$/,
205
+ /^[0-3]*$/,
206
+ /^[0-4]*$/,
207
+ /^[0-5]*$/,
208
+ /^[0-6]*$/,
209
+ /^[0-7]*$/,
210
+ /^[0-8]*$/,
211
+ /^[0-9]*$/,
212
+ /^[0-9aA]*$/,
213
+ /^[0-9abAB]*$/,
214
+ /^[0-9abcABC]*$/,
215
+ /^[0-9a-dA-D]*$/,
216
+ /^[0-9a-eA-E]*$/,
217
+ /^[0-9a-fA-F]*$/,
218
+ /^[0-9a-gA-G]*$/,
219
+ /^[0-9a-hA-H]*$/,
220
+ /^[0-9a-iA-I]*$/,
221
+ /^[0-9a-jA-J]*$/,
222
+ /^[0-9a-kA-K]*$/,
223
+ /^[0-9a-lA-L]*$/,
224
+ /^[0-9a-mA-M]*$/,
225
+ /^[0-9a-nA-N]*$/,
226
+ /^[0-9a-oA-O]*$/,
227
+ /^[0-9a-pA-P]*$/,
228
+ /^[0-9a-qA-Q]*$/,
229
+ /^[0-9a-rA-R]*$/,
230
+ /^[0-9a-sA-S]*$/,
231
+ /^[0-9a-tA-T]*$/,
232
+ /^[0-9a-uA-U]*$/,
233
+ /^[0-9a-vA-V]*$/,
234
+ /^[0-9a-wA-W]*$/,
235
+ /^[0-9a-xA-X]*$/,
236
+ /^[0-9a-yA-Y]*$/,
237
+ /^[0-9a-zA-Z]*$/
238
+ ];
239
+ /*
240
+ Function: parse
241
+ Parse a string into a <BigInteger>.
242
+ *base* is optional but, if provided, must be from 2 to 36 inclusive. If
243
+ *base* is not provided, it will be guessed based on the leading characters
244
+ of *s* as follows:
245
+ - "0x" or "0X": *base* = 16
246
+ - "0c" or "0C": *base* = 8
247
+ - "0b" or "0B": *base* = 2
248
+ - else: *base* = 10
249
+ If no base is provided, or *base* is 10, the number can be in exponential
250
+ form. For example, these are all valid:
251
+ > BigInteger.parse("1e9"); // Same as "1000000000"
252
+ > BigInteger.parse("1.234*10^3"); // Same as 1234
253
+ > BigInteger.parse("56789 * 10 ** -2"); // Same as 567
254
+ If any characters fall outside the range defined by the radix, an exception
255
+ will be thrown.
256
+ Parameters:
257
+ s - The string to parse.
258
+ base - Optional radix (default is to guess based on *s*).
259
+ Returns:
260
+ a <BigInteger> instance.
261
+ */
262
+ BigInteger.parse = function (s, base) {
263
+ // Expands a number in exponential form to decimal form.
264
+ // expandExponential("-13.441*10^5") === "1344100";
265
+ // expandExponential("1.12300e-1") === "0.112300";
266
+ // expandExponential(1000000000000000000000000000000) === "1000000000000000000000000000000";
267
+ function expandExponential(str) {
268
+ str = str.replace(/\s*[*xX]\s*10\s*(\^|\*\*)\s*/, "e");
269
+ return str.replace(/^([+\-])?(\d+)\.?(\d*)[eE]([+\-]?\d+)$/, function (x, s, n, f, c) {
270
+ c = +c;
271
+ var l = c < 0;
272
+ var i = n.length + c;
273
+ x = (l ? n : f).length;
274
+ c = ((c = Math.abs(c)) >= x ? c - x + l : 0);
275
+ var z = (new Array(c + 1)).join("0");
276
+ var r = n + f;
277
+ return (s || "") + (l ? r = z + r : r += z).substr(0, i += l ? z.length : 0) + (i < r.length ? "." + r.substr(i) : "");
278
+ });
279
+ }
280
+ s = s.toString();
281
+ if (typeof base === "undefined" || +base === 10) {
282
+ s = expandExponential(s);
283
+ }
284
+ var prefixRE;
285
+ if (typeof base === "undefined") {
286
+ prefixRE = '0[xcb]';
287
+ }
288
+ else if (base == 16) {
289
+ prefixRE = '0x';
290
+ }
291
+ else if (base == 8) {
292
+ prefixRE = '0c';
293
+ }
294
+ else if (base == 2) {
295
+ prefixRE = '0b';
296
+ }
297
+ else {
298
+ prefixRE = '';
299
+ }
300
+ var parts = new RegExp('^([+\\-]?)(' + prefixRE + ')?([0-9a-z]*)(?:\\.\\d*)?$', 'i').exec(s);
301
+ if (parts) {
302
+ var sign = parts[1] || "+";
303
+ var baseSection = parts[2] || "";
304
+ var digits = parts[3] || "";
305
+ if (typeof base === "undefined") {
306
+ // Guess base
307
+ if (baseSection === "0x" || baseSection === "0X") { // Hex
308
+ base = 16;
309
+ }
310
+ else if (baseSection === "0c" || baseSection === "0C") { // Octal
311
+ base = 8;
312
+ }
313
+ else if (baseSection === "0b" || baseSection === "0B") { // Binary
314
+ base = 2;
315
+ }
316
+ else {
317
+ base = 10;
318
+ }
319
+ }
320
+ else if (base < 2 || base > 36) {
321
+ throw new Error("Illegal radix " + base + ".");
322
+ }
323
+ base = +base;
324
+ // Check for digits outside the range
325
+ if (!(BigInteger.radixRegex[base].test(digits))) {
326
+ throw new Error("Bad digit for radix " + base);
327
+ }
328
+ // Strip leading zeros, and convert to array
329
+ digits = digits.replace(/^0+/, "").split("");
330
+ if (digits.length === 0) {
331
+ return ZERO;
332
+ }
333
+ // Get the sign (we know it's not zero)
334
+ sign = (sign === "-") ? -1 : 1;
335
+ // Optimize 10
336
+ if (base == 10) {
337
+ var d = [];
338
+ while (digits.length >= BigInteger_base_log10) {
339
+ d.push(parseInt(digits.splice(digits.length - BigInteger.base_log10, BigInteger.base_log10).join(''), 10));
340
+ }
341
+ d.push(parseInt(digits.join(''), 10));
342
+ return new BigInteger(d, sign, CONSTRUCT);
343
+ }
344
+ // Do the conversion
345
+ var d = ZERO;
346
+ base = BigInteger.small[base];
347
+ var small = BigInteger.small;
348
+ for (var i = 0; i < digits.length; i++) {
349
+ d = d.multiply(base).add(small[parseInt(digits[i], 36)]);
350
+ }
351
+ return new BigInteger(d._d, sign, CONSTRUCT);
352
+ }
353
+ else {
354
+ throw new Error("Invalid BigInteger format: " + s);
355
+ }
356
+ };
357
+ /*
358
+ Function: add
359
+ Add two <BigIntegers>.
360
+ Parameters:
361
+ n - The number to add to *this*. Will be converted to a <BigInteger>.
362
+ Returns:
363
+ The numbers added together.
364
+ See Also:
365
+ <subtract>, <multiply>, <quotient>, <next>
366
+ */
367
+ BigInteger.prototype.add = function (n) {
368
+ if (this._s === 0) {
369
+ return BigInteger(n);
370
+ }
371
+ n = BigInteger(n);
372
+ if (n._s === 0) {
373
+ return this;
374
+ }
375
+ if (this._s !== n._s) {
376
+ n = n.negate();
377
+ return this.subtract(n);
378
+ }
379
+ var a = this._d;
380
+ var b = n._d;
381
+ var al = a.length;
382
+ var bl = b.length;
383
+ var sum = new Array(Math.max(al, bl) + 1);
384
+ var size = Math.min(al, bl);
385
+ var carry = 0;
386
+ var digit;
387
+ for (var i = 0; i < size; i++) {
388
+ digit = a[i] + b[i] + carry;
389
+ sum[i] = digit % BigInteger_base;
390
+ carry = (digit / BigInteger_base) | 0;
391
+ }
392
+ if (bl > al) {
393
+ a = b;
394
+ al = bl;
395
+ }
396
+ for (i = size; carry && i < al; i++) {
397
+ digit = a[i] + carry;
398
+ sum[i] = digit % BigInteger_base;
399
+ carry = (digit / BigInteger_base) | 0;
400
+ }
401
+ if (carry) {
402
+ sum[i] = carry;
403
+ }
404
+ for (; i < al; i++) {
405
+ sum[i] = a[i];
406
+ }
407
+ return new BigInteger(sum, this._s, CONSTRUCT);
408
+ };
409
+ /*
410
+ Function: negate
411
+ Get the additive inverse of a <BigInteger>.
412
+ Returns:
413
+ A <BigInteger> with the same magnatude, but with the opposite sign.
414
+ See Also:
415
+ <abs>
416
+ */
417
+ BigInteger.prototype.negate = function () {
418
+ return new BigInteger(this._d, (-this._s) | 0, CONSTRUCT);
419
+ };
420
+ /*
421
+ Function: abs
422
+ Get the absolute value of a <BigInteger>.
423
+ Returns:
424
+ A <BigInteger> with the same magnatude, but always positive (or zero).
425
+ See Also:
426
+ <negate>
427
+ */
428
+ BigInteger.prototype.abs = function () {
429
+ return (this._s < 0) ? this.negate() : this;
430
+ };
431
+ /*
432
+ Function: subtract
433
+ Subtract two <BigIntegers>.
434
+ Parameters:
435
+ n - The number to subtract from *this*. Will be converted to a <BigInteger>.
436
+ Returns:
437
+ The *n* subtracted from *this*.
438
+ See Also:
439
+ <add>, <multiply>, <quotient>, <prev>
440
+ */
441
+ BigInteger.prototype.subtract = function (n) {
442
+ if (this._s === 0) {
443
+ return BigInteger(n).negate();
444
+ }
445
+ n = BigInteger(n);
446
+ if (n._s === 0) {
447
+ return this;
448
+ }
449
+ if (this._s !== n._s) {
450
+ n = n.negate();
451
+ return this.add(n);
452
+ }
453
+ var m = this;
454
+ // negative - negative => -|a| - -|b| => -|a| + |b| => |b| - |a|
455
+ if (this._s < 0) {
456
+ m = new BigInteger(n._d, 1, CONSTRUCT);
457
+ n = new BigInteger(this._d, 1, CONSTRUCT);
458
+ }
459
+ // Both are positive => a - b
460
+ var sign = m.compareAbs(n);
461
+ if (sign === 0) {
462
+ return ZERO;
463
+ }
464
+ else if (sign < 0) {
465
+ // swap m and n
466
+ var t = n;
467
+ n = m;
468
+ m = t;
469
+ }
470
+ // a > b
471
+ var a = m._d;
472
+ var b = n._d;
473
+ var al = a.length;
474
+ var bl = b.length;
475
+ var diff = new Array(al); // al >= bl since a > b
476
+ var borrow = 0;
477
+ var i;
478
+ var digit;
479
+ for (i = 0; i < bl; i++) {
480
+ digit = a[i] - borrow - b[i];
481
+ if (digit < 0) {
482
+ digit += BigInteger_base;
483
+ borrow = 1;
484
+ }
485
+ else {
486
+ borrow = 0;
487
+ }
488
+ diff[i] = digit;
489
+ }
490
+ for (i = bl; i < al; i++) {
491
+ digit = a[i] - borrow;
492
+ if (digit < 0) {
493
+ digit += BigInteger_base;
494
+ }
495
+ else {
496
+ diff[i++] = digit;
497
+ break;
498
+ }
499
+ diff[i] = digit;
500
+ }
501
+ for (; i < al; i++) {
502
+ diff[i] = a[i];
503
+ }
504
+ return new BigInteger(diff, sign, CONSTRUCT);
505
+ };
506
+ (function () {
507
+ function addOne(n, sign) {
508
+ var a = n._d;
509
+ var sum = a.slice();
510
+ var carry = true;
511
+ var i = 0;
512
+ while (true) {
513
+ var digit = (a[i] || 0) + 1;
514
+ sum[i] = digit % BigInteger_base;
515
+ if (digit <= BigInteger_base - 1) {
516
+ break;
517
+ }
518
+ ++i;
519
+ }
520
+ return new BigInteger(sum, sign, CONSTRUCT);
521
+ }
522
+ function subtractOne(n, sign) {
523
+ var a = n._d;
524
+ var sum = a.slice();
525
+ var borrow = true;
526
+ var i = 0;
527
+ while (true) {
528
+ var digit = (a[i] || 0) - 1;
529
+ if (digit < 0) {
530
+ sum[i] = digit + BigInteger_base;
531
+ }
532
+ else {
533
+ sum[i] = digit;
534
+ break;
535
+ }
536
+ ++i;
537
+ }
538
+ return new BigInteger(sum, sign, CONSTRUCT);
539
+ }
540
+ /*
541
+ Function: next
542
+ Get the next <BigInteger> (add one).
543
+ Returns:
544
+ *this* + 1.
545
+ See Also:
546
+ <add>, <prev>
547
+ */
548
+ BigInteger.prototype.next = function () {
549
+ switch (this._s) {
550
+ case 0:
551
+ return ONE;
552
+ case -1:
553
+ return subtractOne(this, -1);
554
+ // case 1:
555
+ default:
556
+ return addOne(this, 1);
557
+ }
558
+ };
559
+ /*
560
+ Function: prev
561
+ Get the previous <BigInteger> (subtract one).
562
+ Returns:
563
+ *this* - 1.
564
+ See Also:
565
+ <next>, <subtract>
566
+ */
567
+ BigInteger.prototype.prev = function () {
568
+ switch (this._s) {
569
+ case 0:
570
+ return M_ONE;
571
+ case -1:
572
+ return addOne(this, -1);
573
+ // case 1:
574
+ default:
575
+ return subtractOne(this, 1);
576
+ }
577
+ };
578
+ })();
579
+ /*
580
+ Function: compareAbs
581
+ Compare the absolute value of two <BigIntegers>.
582
+ Calling <compareAbs> is faster than calling <abs> twice, then <compare>.
583
+ Parameters:
584
+ n - The number to compare to *this*. Will be converted to a <BigInteger>.
585
+ Returns:
586
+ -1, 0, or +1 if *|this|* is less than, equal to, or greater than *|n|*.
587
+ See Also:
588
+ <compare>, <abs>
589
+ */
590
+ BigInteger.prototype.compareAbs = function (n) {
591
+ if (this === n) {
592
+ return 0;
593
+ }
594
+ if (!(n instanceof BigInteger)) {
595
+ if (!isFinite(n)) {
596
+ return (isNaN(n) ? n : -1);
597
+ }
598
+ n = BigInteger(n);
599
+ }
600
+ if (this._s === 0) {
601
+ return (n._s !== 0) ? -1 : 0;
602
+ }
603
+ if (n._s === 0) {
604
+ return 1;
605
+ }
606
+ var l = this._d.length;
607
+ var nl = n._d.length;
608
+ if (l < nl) {
609
+ return -1;
610
+ }
611
+ else if (l > nl) {
612
+ return 1;
613
+ }
614
+ var a = this._d;
615
+ var b = n._d;
616
+ for (var i = l - 1; i >= 0; i--) {
617
+ if (a[i] !== b[i]) {
618
+ return a[i] < b[i] ? -1 : 1;
619
+ }
620
+ }
621
+ return 0;
622
+ };
623
+ /*
624
+ Function: compare
625
+ Compare two <BigIntegers>.
626
+ Parameters:
627
+ n - The number to compare to *this*. Will be converted to a <BigInteger>.
628
+ Returns:
629
+ -1, 0, or +1 if *this* is less than, equal to, or greater than *n*.
630
+ See Also:
631
+ <compareAbs>, <isPositive>, <isNegative>, <isUnit>
632
+ */
633
+ BigInteger.prototype.compare = function (n) {
634
+ if (this === n) {
635
+ return 0;
636
+ }
637
+ n = BigInteger(n);
638
+ if (this._s === 0) {
639
+ return -n._s;
640
+ }
641
+ if (this._s === n._s) { // both positive or both negative
642
+ var cmp = this.compareAbs(n);
643
+ return cmp * this._s;
644
+ }
645
+ else {
646
+ return this._s;
647
+ }
648
+ };
649
+ /*
650
+ Function: isUnit
651
+ Return true iff *this* is either 1 or -1.
652
+ Returns:
653
+ true if *this* compares equal to <BigInteger.ONE> or <BigInteger.M_ONE>.
654
+ See Also:
655
+ <isZero>, <isNegative>, <isPositive>, <compareAbs>, <compare>,
656
+ <BigInteger.ONE>, <BigInteger.M_ONE>
657
+ */
658
+ BigInteger.prototype.isUnit = function () {
659
+ return this === ONE ||
660
+ this === M_ONE ||
661
+ (this._d.length === 1 && this._d[0] === 1);
662
+ };
663
+ /*
664
+ Function: multiply
665
+ Multiply two <BigIntegers>.
666
+ Parameters:
667
+ n - The number to multiply *this* by. Will be converted to a
668
+ <BigInteger>.
669
+ Returns:
670
+ The numbers multiplied together.
671
+ See Also:
672
+ <add>, <subtract>, <quotient>, <square>
673
+ */
674
+ BigInteger.prototype.multiply = function (n) {
675
+ // TODO: Consider adding Karatsuba multiplication for large numbers
676
+ if (this._s === 0) {
677
+ return ZERO;
678
+ }
679
+ n = BigInteger(n);
680
+ if (n._s === 0) {
681
+ return ZERO;
682
+ }
683
+ if (this.isUnit()) {
684
+ if (this._s < 0) {
685
+ return n.negate();
686
+ }
687
+ return n;
688
+ }
689
+ if (n.isUnit()) {
690
+ if (n._s < 0) {
691
+ return this.negate();
692
+ }
693
+ return this;
694
+ }
695
+ if (this === n) {
696
+ return this.square();
697
+ }
698
+ var r = (this._d.length >= n._d.length);
699
+ var a = (r ? this : n)._d; // a will be longer than b
700
+ var b = (r ? n : this)._d;
701
+ var al = a.length;
702
+ var bl = b.length;
703
+ var pl = al + bl;
704
+ var partial = new Array(pl);
705
+ var i;
706
+ for (i = 0; i < pl; i++) {
707
+ partial[i] = 0;
708
+ }
709
+ for (i = 0; i < bl; i++) {
710
+ var carry = 0;
711
+ var bi = b[i];
712
+ var jlimit = al + i;
713
+ var digit;
714
+ for (var j = i; j < jlimit; j++) {
715
+ digit = partial[j] + bi * a[j - i] + carry;
716
+ carry = (digit / BigInteger_base) | 0;
717
+ partial[j] = (digit % BigInteger_base) | 0;
718
+ }
719
+ if (carry) {
720
+ digit = partial[j] + carry;
721
+ carry = (digit / BigInteger_base) | 0;
722
+ partial[j] = digit % BigInteger_base;
723
+ }
724
+ }
725
+ return new BigInteger(partial, this._s * n._s, CONSTRUCT);
726
+ };
727
+ // Multiply a BigInteger by a single-digit native number
728
+ // Assumes that this and n are >= 0
729
+ // This is not really intended to be used outside the library itself
730
+ BigInteger.prototype.multiplySingleDigit = function (n) {
731
+ if (n === 0 || this._s === 0) {
732
+ return ZERO;
733
+ }
734
+ if (n === 1) {
735
+ return this;
736
+ }
737
+ var digit;
738
+ if (this._d.length === 1) {
739
+ digit = this._d[0] * n;
740
+ if (digit >= BigInteger_base) {
741
+ return new BigInteger([(digit % BigInteger_base) | 0,
742
+ (digit / BigInteger_base) | 0], 1, CONSTRUCT);
743
+ }
744
+ return new BigInteger([digit], 1, CONSTRUCT);
745
+ }
746
+ if (n === 2) {
747
+ return this.add(this);
748
+ }
749
+ if (this.isUnit()) {
750
+ return new BigInteger([n], 1, CONSTRUCT);
751
+ }
752
+ var a = this._d;
753
+ var al = a.length;
754
+ var pl = al + 1;
755
+ var partial = new Array(pl);
756
+ for (var i = 0; i < pl; i++) {
757
+ partial[i] = 0;
758
+ }
759
+ var carry = 0;
760
+ for (var j = 0; j < al; j++) {
761
+ digit = n * a[j] + carry;
762
+ carry = (digit / BigInteger_base) | 0;
763
+ partial[j] = (digit % BigInteger_base) | 0;
764
+ }
765
+ if (carry) {
766
+ partial[j] = carry;
767
+ }
768
+ return new BigInteger(partial, 1, CONSTRUCT);
769
+ };
770
+ /*
771
+ Function: square
772
+ Multiply a <BigInteger> by itself.
773
+ This is slightly faster than regular multiplication, since it removes the
774
+ duplicated multiplcations.
775
+ Returns:
776
+ > this.multiply(this)
777
+ See Also:
778
+ <multiply>
779
+ */
780
+ BigInteger.prototype.square = function () {
781
+ // Normally, squaring a 10-digit number would take 100 multiplications.
782
+ // Of these 10 are unique diagonals, of the remaining 90 (100-10), 45 are repeated.
783
+ // This procedure saves (N*(N-1))/2 multiplications, (e.g., 45 of 100 multiplies).
784
+ // Based on code by Gary Darby, Intellitech Systems Inc., www.DelphiForFun.org
785
+ if (this._s === 0) {
786
+ return ZERO;
787
+ }
788
+ if (this.isUnit()) {
789
+ return ONE;
790
+ }
791
+ var digits = this._d;
792
+ var length = digits.length;
793
+ var imult1 = new Array(length + length + 1);
794
+ var product, carry, k;
795
+ var i;
796
+ // Calculate diagonal
797
+ for (i = 0; i < length; i++) {
798
+ k = i * 2;
799
+ product = digits[i] * digits[i];
800
+ carry = (product / BigInteger_base) | 0;
801
+ imult1[k] = product % BigInteger_base;
802
+ imult1[k + 1] = carry;
803
+ }
804
+ // Calculate repeating part
805
+ for (i = 0; i < length; i++) {
806
+ carry = 0;
807
+ k = i * 2 + 1;
808
+ for (var j = i + 1; j < length; j++, k++) {
809
+ product = digits[j] * digits[i] * 2 + imult1[k] + carry;
810
+ carry = (product / BigInteger_base) | 0;
811
+ imult1[k] = product % BigInteger_base;
812
+ }
813
+ k = length + i;
814
+ var digit = carry + imult1[k];
815
+ carry = (digit / BigInteger_base) | 0;
816
+ imult1[k] = digit % BigInteger_base;
817
+ imult1[k + 1] += carry;
818
+ }
819
+ return new BigInteger(imult1, 1, CONSTRUCT);
820
+ };
821
+ /*
822
+ Function: quotient
823
+ Divide two <BigIntegers> and truncate towards zero.
824
+ <quotient> throws an exception if *n* is zero.
825
+ Parameters:
826
+ n - The number to divide *this* by. Will be converted to a <BigInteger>.
827
+ Returns:
828
+ The *this* / *n*, truncated to an integer.
829
+ See Also:
830
+ <add>, <subtract>, <multiply>, <divRem>, <remainder>
831
+ */
832
+ BigInteger.prototype.quotient = function (n) {
833
+ return this.divRem(n)[0];
834
+ };
835
+ /*
836
+ Function: divide
837
+ Deprecated synonym for <quotient>.
838
+ */
839
+ BigInteger.prototype.divide = BigInteger.prototype.quotient;
840
+ /*
841
+ Function: remainder
842
+ Calculate the remainder of two <BigIntegers>.
843
+ <remainder> throws an exception if *n* is zero.
844
+ Parameters:
845
+ n - The remainder after *this* is divided *this* by *n*. Will be
846
+ converted to a <BigInteger>.
847
+ Returns:
848
+ *this* % *n*.
849
+ See Also:
850
+ <divRem>, <quotient>
851
+ */
852
+ BigInteger.prototype.remainder = function (n) {
853
+ return this.divRem(n)[1];
854
+ };
855
+ /*
856
+ Function: divRem
857
+ Calculate the integer quotient and remainder of two <BigIntegers>.
858
+ <divRem> throws an exception if *n* is zero.
859
+ Parameters:
860
+ n - The number to divide *this* by. Will be converted to a <BigInteger>.
861
+ Returns:
862
+ A two-element array containing the quotient and the remainder.
863
+ > a.divRem(b)
864
+ is exactly equivalent to
865
+ > [a.quotient(b), a.remainder(b)]
866
+ except it is faster, because they are calculated at the same time.
867
+ See Also:
868
+ <quotient>, <remainder>
869
+ */
870
+ BigInteger.prototype.divRem = function (n) {
871
+ n = BigInteger(n);
872
+ if (n._s === 0) {
873
+ throw new Error("Divide by zero");
874
+ }
875
+ if (this._s === 0) {
876
+ return [ZERO, ZERO];
877
+ }
878
+ if (n._d.length === 1) {
879
+ return this.divRemSmall(n._s * n._d[0]);
880
+ }
881
+ // Test for easy cases -- |n1| <= |n2|
882
+ switch (this.compareAbs(n)) {
883
+ case 0: // n1 == n2
884
+ return [this._s === n._s ? ONE : M_ONE, ZERO];
885
+ case -1: // |n1| < |n2|
886
+ return [ZERO, this];
887
+ }
888
+ var sign = this._s * n._s;
889
+ var a = n.abs();
890
+ var b_digits = this._d;
891
+ var b_index = b_digits.length;
892
+ var digits = n._d.length;
893
+ var quot = [];
894
+ var guess;
895
+ var part = new BigInteger([], 0, CONSTRUCT);
896
+ while (b_index) {
897
+ part._d.unshift(b_digits[--b_index]);
898
+ part = new BigInteger(part._d, 1, CONSTRUCT);
899
+ if (part.compareAbs(n) < 0) {
900
+ quot.push(0);
901
+ continue;
902
+ }
903
+ if (part._s === 0) {
904
+ guess = 0;
905
+ }
906
+ else {
907
+ var xlen = part._d.length, ylen = a._d.length;
908
+ var highx = part._d[xlen - 1] * BigInteger_base + part._d[xlen - 2];
909
+ var highy = a._d[ylen - 1] * BigInteger_base + a._d[ylen - 2];
910
+ if (part._d.length > a._d.length) {
911
+ // The length of part._d can either match a._d length,
912
+ // or exceed it by one.
913
+ highx = (highx + 1) * BigInteger_base;
914
+ }
915
+ guess = Math.ceil(highx / highy);
916
+ }
917
+ do {
918
+ var check = a.multiplySingleDigit(guess);
919
+ if (check.compareAbs(part) <= 0) {
920
+ break;
921
+ }
922
+ guess--;
923
+ } while (guess);
924
+ quot.push(guess);
925
+ if (!guess) {
926
+ continue;
927
+ }
928
+ var diff = part.subtract(check);
929
+ part._d = diff._d.slice();
930
+ }
931
+ return [new BigInteger(quot.reverse(), sign, CONSTRUCT),
932
+ new BigInteger(part._d, this._s, CONSTRUCT)];
933
+ };
934
+ // Throws an exception if n is outside of (-BigInteger.base, -1] or
935
+ // [1, BigInteger.base). It's not necessary to call this, since the
936
+ // other division functions will call it if they are able to.
937
+ BigInteger.prototype.divRemSmall = function (n) {
938
+ var r;
939
+ n = +n;
940
+ if (n === 0) {
941
+ throw new Error("Divide by zero");
942
+ }
943
+ var n_s = n < 0 ? -1 : 1;
944
+ var sign = this._s * n_s;
945
+ n = Math.abs(n);
946
+ if (n < 1 || n >= BigInteger_base) {
947
+ throw new Error("Argument out of range");
948
+ }
949
+ if (this._s === 0) {
950
+ return [ZERO, ZERO];
951
+ }
952
+ if (n === 1 || n === -1) {
953
+ return [(sign === 1) ? this.abs() : new BigInteger(this._d, sign, CONSTRUCT), ZERO];
954
+ }
955
+ // 2 <= n < BigInteger_base
956
+ // divide a single digit by a single digit
957
+ if (this._d.length === 1) {
958
+ var q = new BigInteger([(this._d[0] / n) | 0], 1, CONSTRUCT);
959
+ r = new BigInteger([(this._d[0] % n) | 0], 1, CONSTRUCT);
960
+ if (sign < 0) {
961
+ q = q.negate();
962
+ }
963
+ if (this._s < 0) {
964
+ r = r.negate();
965
+ }
966
+ return [q, r];
967
+ }
968
+ var digits = this._d.slice();
969
+ var quot = new Array(digits.length);
970
+ var part = 0;
971
+ var diff = 0;
972
+ var i = 0;
973
+ var guess;
974
+ while (digits.length) {
975
+ part = part * BigInteger_base + digits[digits.length - 1];
976
+ if (part < n) {
977
+ quot[i++] = 0;
978
+ digits.pop();
979
+ diff = BigInteger_base * diff + part;
980
+ continue;
981
+ }
982
+ if (part === 0) {
983
+ guess = 0;
984
+ }
985
+ else {
986
+ guess = (part / n) | 0;
987
+ }
988
+ var check = n * guess;
989
+ diff = part - check;
990
+ quot[i++] = guess;
991
+ if (!guess) {
992
+ digits.pop();
993
+ continue;
994
+ }
995
+ digits.pop();
996
+ part = diff;
997
+ }
998
+ r = new BigInteger([diff], 1, CONSTRUCT);
999
+ if (this._s < 0) {
1000
+ r = r.negate();
1001
+ }
1002
+ return [new BigInteger(quot.reverse(), sign, CONSTRUCT), r];
1003
+ };
1004
+ /*
1005
+ Function: isEven
1006
+ Return true iff *this* is divisible by two.
1007
+ Note that <BigInteger.ZERO> is even.
1008
+ Returns:
1009
+ true if *this* is even, false otherwise.
1010
+ See Also:
1011
+ <isOdd>
1012
+ */
1013
+ BigInteger.prototype.isEven = function () {
1014
+ var digits = this._d;
1015
+ return this._s === 0 || digits.length === 0 || (digits[0] % 2) === 0;
1016
+ };
1017
+ /*
1018
+ Function: isOdd
1019
+ Return true iff *this* is not divisible by two.
1020
+ Returns:
1021
+ true if *this* is odd, false otherwise.
1022
+ See Also:
1023
+ <isEven>
1024
+ */
1025
+ BigInteger.prototype.isOdd = function () {
1026
+ return !this.isEven();
1027
+ };
1028
+ /*
1029
+ Function: sign
1030
+ Get the sign of a <BigInteger>.
1031
+ Returns:
1032
+ * -1 if *this* < 0
1033
+ * 0 if *this* == 0
1034
+ * +1 if *this* > 0
1035
+ See Also:
1036
+ <isZero>, <isPositive>, <isNegative>, <compare>, <BigInteger.ZERO>
1037
+ */
1038
+ BigInteger.prototype.sign = function () {
1039
+ return this._s;
1040
+ };
1041
+ /*
1042
+ Function: isPositive
1043
+ Return true iff *this* > 0.
1044
+ Returns:
1045
+ true if *this*.compare(<BigInteger.ZERO>) == 1.
1046
+ See Also:
1047
+ <sign>, <isZero>, <isNegative>, <isUnit>, <compare>, <BigInteger.ZERO>
1048
+ */
1049
+ BigInteger.prototype.isPositive = function () {
1050
+ return this._s > 0;
1051
+ };
1052
+ /*
1053
+ Function: isNegative
1054
+ Return true iff *this* < 0.
1055
+ Returns:
1056
+ true if *this*.compare(<BigInteger.ZERO>) == -1.
1057
+ See Also:
1058
+ <sign>, <isPositive>, <isZero>, <isUnit>, <compare>, <BigInteger.ZERO>
1059
+ */
1060
+ BigInteger.prototype.isNegative = function () {
1061
+ return this._s < 0;
1062
+ };
1063
+ /*
1064
+ Function: isZero
1065
+ Return true iff *this* == 0.
1066
+ Returns:
1067
+ true if *this*.compare(<BigInteger.ZERO>) == 0.
1068
+ See Also:
1069
+ <sign>, <isPositive>, <isNegative>, <isUnit>, <BigInteger.ZERO>
1070
+ */
1071
+ BigInteger.prototype.isZero = function () {
1072
+ return this._s === 0;
1073
+ };
1074
+ /*
1075
+ Function: exp10
1076
+ Multiply a <BigInteger> by a power of 10.
1077
+ This is equivalent to, but faster than
1078
+ > if (n >= 0) {
1079
+ > return this.multiply(BigInteger("1e" + n));
1080
+ > }
1081
+ > else { // n <= 0
1082
+ > return this.quotient(BigInteger("1e" + -n));
1083
+ > }
1084
+ Parameters:
1085
+ n - The power of 10 to multiply *this* by. *n* is converted to a
1086
+ javascipt number and must be no greater than <BigInteger.MAX_EXP>
1087
+ (0x7FFFFFFF), or an exception will be thrown.
1088
+ Returns:
1089
+ *this* * (10 ** *n*), truncated to an integer if necessary.
1090
+ See Also:
1091
+ <pow>, <multiply>
1092
+ */
1093
+ BigInteger.prototype.exp10 = function (n) {
1094
+ n = +n;
1095
+ if (n === 0) {
1096
+ return this;
1097
+ }
1098
+ if (Math.abs(n) > Number(MAX_EXP)) {
1099
+ throw new Error("exponent too large in BigInteger.exp10");
1100
+ }
1101
+ // Optimization for this == 0. This also keeps us from having to trim zeros in the positive n case
1102
+ if (this._s === 0) {
1103
+ return ZERO;
1104
+ }
1105
+ if (n > 0) {
1106
+ var k = new BigInteger(this._d.slice(), this._s, CONSTRUCT);
1107
+ for (; n >= BigInteger_base_log10; n -= BigInteger_base_log10) {
1108
+ k._d.unshift(0);
1109
+ }
1110
+ if (n == 0)
1111
+ return k;
1112
+ k._s = 1;
1113
+ k = k.multiplySingleDigit(Math.pow(10, n));
1114
+ return (this._s < 0 ? k.negate() : k);
1115
+ }
1116
+ else if (-n >= this._d.length * BigInteger_base_log10) {
1117
+ return ZERO;
1118
+ }
1119
+ else {
1120
+ var k = new BigInteger(this._d.slice(), this._s, CONSTRUCT);
1121
+ for (n = -n; n >= BigInteger_base_log10; n -= BigInteger_base_log10) {
1122
+ k._d.shift();
1123
+ }
1124
+ return (n == 0) ? k : k.divRemSmall(Math.pow(10, n))[0];
1125
+ }
1126
+ };
1127
+ /*
1128
+ Function: pow
1129
+ Raise a <BigInteger> to a power.
1130
+ In this implementation, 0**0 is 1.
1131
+ Parameters:
1132
+ n - The exponent to raise *this* by. *n* must be no greater than
1133
+ <BigInteger.MAX_EXP> (0x7FFFFFFF), or an exception will be thrown.
1134
+ Returns:
1135
+ *this* raised to the *nth* power.
1136
+ See Also:
1137
+ <modPow>
1138
+ */
1139
+ BigInteger.prototype.pow = function (n) {
1140
+ if (this.isUnit()) {
1141
+ if (this._s > 0) {
1142
+ return this;
1143
+ }
1144
+ else {
1145
+ return BigInteger(n).isOdd() ? this : this.negate();
1146
+ }
1147
+ }
1148
+ n = BigInteger(n);
1149
+ if (n._s === 0) {
1150
+ return ONE;
1151
+ }
1152
+ else if (n._s < 0) {
1153
+ if (this._s === 0) {
1154
+ throw new Error("Divide by zero");
1155
+ }
1156
+ else {
1157
+ return ZERO;
1158
+ }
1159
+ }
1160
+ if (this._s === 0) {
1161
+ return ZERO;
1162
+ }
1163
+ if (n.isUnit()) {
1164
+ return this;
1165
+ }
1166
+ if (n.compareAbs(MAX_EXP) > 0) {
1167
+ throw new Error("exponent too large in BigInteger.pow");
1168
+ }
1169
+ var x = this;
1170
+ var aux = ONE;
1171
+ var two = BigInteger.small[2];
1172
+ while (n.isPositive()) {
1173
+ if (n.isOdd()) {
1174
+ aux = aux.multiply(x);
1175
+ if (n.isUnit()) {
1176
+ return aux;
1177
+ }
1178
+ }
1179
+ x = x.square();
1180
+ n = n.quotient(two);
1181
+ }
1182
+ return aux;
1183
+ };
1184
+ /*
1185
+ Function: modPow
1186
+ Raise a <BigInteger> to a power (mod m).
1187
+ Because it is reduced by a modulus, <modPow> is not limited by
1188
+ <BigInteger.MAX_EXP> like <pow>.
1189
+ Parameters:
1190
+ exponent - The exponent to raise *this* by. Must be positive.
1191
+ modulus - The modulus.
1192
+ Returns:
1193
+ *this* ^ *exponent* (mod *modulus*).
1194
+ See Also:
1195
+ <pow>, <mod>
1196
+ */
1197
+ BigInteger.prototype.modPow = function (exponent, modulus) {
1198
+ var result = ONE;
1199
+ var base = this;
1200
+ while (exponent.isPositive()) {
1201
+ if (exponent.isOdd()) {
1202
+ result = result.multiply(base).remainder(modulus);
1203
+ }
1204
+ exponent = exponent.quotient(BigInteger.small[2]);
1205
+ if (exponent.isPositive()) {
1206
+ base = base.square().remainder(modulus);
1207
+ }
1208
+ }
1209
+ return result;
1210
+ };
1211
+ /*
1212
+ Function: log
1213
+ Get the natural logarithm of a <BigInteger> as a native JavaScript number.
1214
+ This is equivalent to
1215
+ > Math.log(this.toJSValue())
1216
+ but handles values outside of the native number range.
1217
+ Returns:
1218
+ log( *this* )
1219
+ See Also:
1220
+ <toJSValue>
1221
+ */
1222
+ BigInteger.prototype.log = function () {
1223
+ switch (this._s) {
1224
+ case 0:
1225
+ return -Infinity;
1226
+ case -1:
1227
+ return NaN;
1228
+ default: // Fall through.
1229
+ }
1230
+ var l = this._d.length;
1231
+ if (l * BigInteger_base_log10 < 30) {
1232
+ return Math.log(this.valueOf());
1233
+ }
1234
+ var N = Math.ceil(30 / BigInteger_base_log10);
1235
+ var firstNdigits = this._d.slice(l - N);
1236
+ return Math.log((new BigInteger(firstNdigits, 1, CONSTRUCT)).valueOf()) + (l - N) * Math.log(BigInteger_base);
1237
+ };
1238
+ /*
1239
+ Function: valueOf
1240
+ Convert a <BigInteger> to a native JavaScript integer.
1241
+ This is called automatically by JavaScipt to convert a <BigInteger> to a
1242
+ native value.
1243
+ Returns:
1244
+ > parseInt(this.toString(), 10)
1245
+ See Also:
1246
+ <toString>, <toJSValue>
1247
+ */
1248
+ BigInteger.prototype.valueOf = function () {
1249
+ return parseInt(this.toString(), 10);
1250
+ };
1251
+ /*
1252
+ Function: toJSValue
1253
+ Convert a <BigInteger> to a native JavaScript integer.
1254
+ This is the same as valueOf, but more explicitly named.
1255
+ Returns:
1256
+ > parseInt(this.toString(), 10)
1257
+ See Also:
1258
+ <toString>, <valueOf>
1259
+ */
1260
+ BigInteger.prototype.toJSValue = function () {
1261
+ return parseInt(this.toString(), 10);
1262
+ };
1263
+ /*
1264
+ Function: lowVal
1265
+ Author: Lucas Jones
1266
+ */
1267
+ BigInteger.prototype.lowVal = function () {
1268
+ return this._d[0] || 0;
1269
+ };
1270
+ var MAX_EXP = BigInteger(0x7FFFFFFF);
1271
+ // Constant: MAX_EXP
1272
+ // The largest exponent allowed in <pow> and <exp10> (0x7FFFFFFF or 2147483647).
1273
+ BigInteger.MAX_EXP = MAX_EXP;
1274
+ (function () {
1275
+ function makeUnary(fn) {
1276
+ return function (a) {
1277
+ return fn.call(BigInteger(a));
1278
+ };
1279
+ }
1280
+ function makeBinary(fn) {
1281
+ return function (a, b) {
1282
+ return fn.call(BigInteger(a), BigInteger(b));
1283
+ };
1284
+ }
1285
+ function makeTrinary(fn) {
1286
+ return function (a, b, c) {
1287
+ return fn.call(BigInteger(a), BigInteger(b), BigInteger(c));
1288
+ };
1289
+ }
1290
+ (function () {
1291
+ var i, fn;
1292
+ var unary = "toJSValue,isEven,isOdd,sign,isZero,isNegative,abs,isUnit,square,negate,isPositive,toString,next,prev,log".split(",");
1293
+ var binary = "compare,remainder,divRem,subtract,add,quotient,divide,multiply,pow,compareAbs".split(",");
1294
+ var trinary = ["modPow"];
1295
+ for (i = 0; i < unary.length; i++) {
1296
+ fn = unary[i];
1297
+ BigInteger[fn] = makeUnary(BigInteger.prototype[fn]);
1298
+ }
1299
+ for (i = 0; i < binary.length; i++) {
1300
+ fn = binary[i];
1301
+ BigInteger[fn] = makeBinary(BigInteger.prototype[fn]);
1302
+ }
1303
+ for (i = 0; i < trinary.length; i++) {
1304
+ fn = trinary[i];
1305
+ BigInteger[fn] = makeTrinary(BigInteger.prototype[fn]);
1306
+ }
1307
+ BigInteger.exp10 = function (x, n) {
1308
+ return BigInteger(x).exp10(n);
1309
+ };
1310
+ })();
1311
+ })();