multichain-address-validator 0.7.4 → 0.7.5

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