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