2 * Basic JavaScript BN library - subset useful for RSA encryption.
4 * Copyright (c) 2003-2005 Tom Wu
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
20 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
22 * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
23 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
24 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
25 * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
26 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 * In addition, the following condition applies:
30 * All redistributions must retain an intact copy of this copyright notice
35 * Added Node.js Buffers support
39var crypt = require('crypto');
41//var isNumber = require('lodash.isnumber'); // Remove this dependency to limit supply chain risks
42function isObjectLike(value) { return !!value && typeof value == 'object'; }
43const isNumber = function isNumber(value) { return typeof value == 'number' || (isObjectLike(value) && Object.prototype.toString.call(value) == '[object Number]'); }
48// JavaScript engine analysis
49var canary = 0xdeadbeefcafe;
50var j_lm = ((canary & 0xffffff) == 0xefcafe);
52// (public) Constructor
53function BigInteger(a, b) {
55 if ("number" == typeof a) {
56 this.fromNumber(a, b);
57 } else if (Buffer.isBuffer(a)) {
59 } else if (b == null && "string" != typeof a) {
60 this.fromByteArray(a);
62 this.fromString(a, b);
67// return new, unset BigInteger
69 return new BigInteger(null);
72// am: Compute w_j += (x*this_i), propagate carries,
73// c is initial carry, returns final carry.
74// c < 3*dvalue, x < 2*dvalue, this_i < dvalue
75// We need to select the fastest one that works in this environment.
77// am1: use a single mult and divide to get the high bits,
78// max digit bits should be 26 because
79// max internal value = 2*dvalue^2-2*dvalue (< 2^53)
80function am1(i, x, w, j, c, n) {
82 var v = x * this[i++] + w[j] + c;
83 c = Math.floor(v / 0x4000000);
84 w[j++] = v & 0x3ffffff;
88// am2 avoids a big mult-and-extract completely.
89// Max digit bits should be <= 30 because we do bitwise ops
90// on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
91function am2(i, x, w, j, c, n) {
92 var xl = x & 0x7fff, xh = x >> 15;
94 var l = this[i] & 0x7fff;
95 var h = this[i++] >> 15;
96 var m = xh * l + h * xl;
97 l = xl * l + ((m & 0x7fff) << 15) + w[j] + (c & 0x3fffffff);
98 c = (l >>> 30) + (m >>> 15) + xh * h + (c >>> 30);
99 w[j++] = l & 0x3fffffff;
103// Alternately, set max digit bits to 28 since some
104// browsers slow down when dealing with 32-bit numbers.
105function am3(i, x, w, j, c, n) {
106 var xl = x & 0x3fff, xh = x >> 14;
108 var l = this[i] & 0x3fff;
109 var h = this[i++] >> 14;
110 var m = xh * l + h * xl;
111 l = xl * l + ((m & 0x3fff) << 14) + w[j] + c;
112 c = (l >> 28) + (m >> 14) + xh * h;
113 w[j++] = l & 0xfffffff;
118// We need to select the fastest one that works in this environment.
119//if (j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
120// BigInteger.prototype.am = am2;
122//} else if (j_lm && (navigator.appName != "Netscape")) {
123// BigInteger.prototype.am = am1;
125//} else { // Mozilla/Netscape seems to prefer am3
126// BigInteger.prototype.am = am3;
130// For node.js, we pick am3 with max dbits to 28.
131BigInteger.prototype.am = am3;
134BigInteger.prototype.DB = dbits;
135BigInteger.prototype.DM = ((1 << dbits) - 1);
136BigInteger.prototype.DV = (1 << dbits);
139BigInteger.prototype.FV = Math.pow(2, BI_FP);
140BigInteger.prototype.F1 = BI_FP - dbits;
141BigInteger.prototype.F2 = 2 * dbits - BI_FP;
144var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
145var BI_RC = new Array();
147rr = "0".charCodeAt(0);
148for (vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
149rr = "a".charCodeAt(0);
150for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
151rr = "A".charCodeAt(0);
152for (vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
154function int2char(n) {
155 return BI_RM.charAt(n);
157function intAt(s, i) {
158 var c = BI_RC[s.charCodeAt(i)];
159 return (c == null) ? -1 : c;
162// (protected) copy this to r
163function bnpCopyTo(r) {
164 for (var i = this.t - 1; i >= 0; --i) r[i] = this[i];
169// (protected) set from integer value x, -DV <= x < DV
170function bnpFromInt(x) {
172 this.s = (x < 0) ? -1 : 0;
173 if (x > 0) this[0] = x;
174 else if (x < -1) this[0] = x + DV;
178// return bigint initialized to value
185// (protected) set from string and radix
186function bnpFromString(data, radix, unsigned) {
208 this.fromRadix(data, radix);
220 var x = (k == 8) ? data[i] & 0xff : intAt(data, i);
222 if (data.charAt(i) == "-") mi = true;
228 else if (sh + k > this.DB) {
229 this[this.t - 1] |= (x & ((1 << (this.DB - sh)) - 1)) << sh;
230 this[this.t++] = (x >> (this.DB - sh));
233 this[this.t - 1] |= x << sh;
235 if (sh >= this.DB) sh -= this.DB;
237 if ((!unsigned) && k == 8 && (data[0] & 0x80) != 0) {
239 if (sh > 0) this[this.t - 1] |= ((1 << (this.DB - sh)) - 1) << sh;
242 if (mi) BigInteger.ZERO.subTo(this, this);
245function bnpFromByteArray(a, unsigned) {
246 this.fromString(a, 256, unsigned)
249function bnpFromBuffer(a) {
250 this.fromString(a, 256, true)
253// (protected) clamp off excess high words
255 var c = this.s & this.DM;
256 while (this.t > 0 && this[this.t - 1] == c) --this.t;
259// (public) return string representation in given radix
260function bnToString(b) {
261 if (this.s < 0) return "-" + this.negate().toString(b);
264 else if (b == 8) k = 3;
265 else if (b == 2) k = 1;
266 else if (b == 32) k = 5;
267 else if (b == 4) k = 2;
268 else return this.toRadix(b);
269 var km = (1 << k) - 1, d, m = false, r = "", i = this.t;
270 var p = this.DB - (i * this.DB) % k;
272 if (p < this.DB && (d = this[i] >> p) > 0) {
278 d = (this[i] & ((1 << p) - 1)) << (k - p);
279 d |= this[--i] >> (p += this.DB - k);
282 d = (this[i] >> (p -= k)) & km;
289 if (m) r += int2char(d);
298 BigInteger.ZERO.subTo(this, r);
304 return (this.s < 0) ? this.negate() : this;
307// (public) return + if this > a, - if this < a, 0 if equal
308function bnCompareTo(a) {
309 var r = this.s - a.s;
310 if (r != 0) return r;
313 if (r != 0) return (this.s < 0) ? -r : r;
314 while (--i >= 0) if ((r = this[i] - a[i]) != 0) return r;
318// returns bit length of the integer x
321 if ((t = x >>> 16) != 0) {
325 if ((t = x >> 8) != 0) {
329 if ((t = x >> 4) != 0) {
333 if ((t = x >> 2) != 0) {
337 if ((t = x >> 1) != 0) {
344// (public) return the number of bits in "this"
345function bnBitLength() {
346 if (this.t <= 0) return 0;
347 return this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ (this.s & this.DM));
350// (protected) r = this << n*DB
351function bnpDLShiftTo(n, r) {
353 for (i = this.t - 1; i >= 0; --i) r[i + n] = this[i];
354 for (i = n - 1; i >= 0; --i) r[i] = 0;
359// (protected) r = this >> n*DB
360function bnpDRShiftTo(n, r) {
361 for (var i = n; i < this.t; ++i) r[i - n] = this[i];
362 r.t = Math.max(this.t - n, 0);
366// (protected) r = this << n
367function bnpLShiftTo(n, r) {
368 var bs = n % this.DB;
369 var cbs = this.DB - bs;
370 var bm = (1 << cbs) - 1;
371 var ds = Math.floor(n / this.DB), c = (this.s << bs) & this.DM, i;
372 for (i = this.t - 1; i >= 0; --i) {
373 r[i + ds + 1] = (this[i] >> cbs) | c;
374 c = (this[i] & bm) << bs;
376 for (i = ds - 1; i >= 0; --i) r[i] = 0;
378 r.t = this.t + ds + 1;
383// (protected) r = this >> n
384function bnpRShiftTo(n, r) {
386 var ds = Math.floor(n / this.DB);
391 var bs = n % this.DB;
392 var cbs = this.DB - bs;
393 var bm = (1 << bs) - 1;
394 r[0] = this[ds] >> bs;
395 for (var i = ds + 1; i < this.t; ++i) {
396 r[i - ds - 1] |= (this[i] & bm) << cbs;
397 r[i - ds] = this[i] >> bs;
399 if (bs > 0) r[this.t - ds - 1] |= (this.s & bm) << cbs;
404// (protected) r = this - a
405function bnpSubTo(a, r) {
406 var i = 0, c = 0, m = Math.min(a.t, this.t);
409 r[i++] = c & this.DM;
416 r[i++] = c & this.DM;
425 r[i++] = c & this.DM;
430 r.s = (c < 0) ? -1 : 0;
431 if (c < -1) r[i++] = this.DV + c;
432 else if (c > 0) r[i++] = c;
437// (protected) r = this * a, r != this,a (HAC 14.12)
438// "this" should be the larger one if appropriate.
439function bnpMultiplyTo(a, r) {
440 var x = this.abs(), y = a.abs();
443 while (--i >= 0) r[i] = 0;
444 for (i = 0; i < y.t; ++i) r[i + x.t] = x.am(0, y[i], r, i, 0, x.t);
447 if (this.s != a.s) BigInteger.ZERO.subTo(r, r);
450// (protected) r = this^2, r != this (HAC 14.16)
451function bnpSquareTo(r) {
453 var i = r.t = 2 * x.t;
454 while (--i >= 0) r[i] = 0;
455 for (i = 0; i < x.t - 1; ++i) {
456 var c = x.am(i, x[i], r, 2 * i, 0, 1);
457 if ((r[i + x.t] += x.am(i + 1, 2 * x[i], r, 2 * i + 1, c, x.t - i - 1)) >= x.DV) {
462 if (r.t > 0) r[r.t - 1] += x.am(i, x[i], r, 2 * i, 0, 1);
467// (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
468// r != q, this != m. q or r may be null.
469function bnpDivRemTo(m, q, r) {
471 if (pm.t <= 0) return;
474 if (q != null) q.fromInt(0);
475 if (r != null) this.copyTo(r);
478 if (r == null) r = nbi();
479 var y = nbi(), ts = this.s, ms = m.s;
480 var nsh = this.DB - nbits(pm[pm.t - 1]); // normalize modulus
491 if (y0 === 0) return;
492 var yt = y0 * (1 << this.F1) + ((ys > 1) ? y[ys - 2] >> this.F2 : 0);
493 var d1 = this.FV / yt, d2 = (1 << this.F1) / yt, e = 1 << this.F2;
494 var i = r.t, j = i - ys, t = (q == null) ? nbi() : q;
496 if (r.compareTo(t) >= 0) {
500 BigInteger.ONE.dlShiftTo(ys, t);
501 t.subTo(y, y); // "negative" y so we can replace sub with am later
502 while (y.t < ys) y[y.t++] = 0;
504 // Estimate quotient digit
505 var qd = (r[--i] == y0) ? this.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2);
506 if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) { // Try it out
509 while (r[i] < --qd) r.subTo(t, r);
514 if (ts != ms) BigInteger.ZERO.subTo(q, q);
518 if (nsh > 0) r.rShiftTo(nsh, r); // Denormalize remainder
519 if (ts < 0) BigInteger.ZERO.subTo(r, r);
522// (public) this mod a
525 this.abs().divRemTo(a, null, r);
526 if (this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r, r);
530// Modular reduction using "classic" algorithm
534function cConvert(x) {
535 if (x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
542 x.divRemTo(this.m, null, x);
544function cMulTo(x, y, r) {
548function cSqrTo(x, r) {
553Classic.prototype.convert = cConvert;
554Classic.prototype.revert = cRevert;
555Classic.prototype.reduce = cReduce;
556Classic.prototype.mulTo = cMulTo;
557Classic.prototype.sqrTo = cSqrTo;
559// (protected) return "-1/this % 2^DB"; useful for Mont. reduction
563// xy(2-xy) = (1+km)(1-km)
564// x[y(2-xy)] = 1-k^2m^2
565// x[y(2-xy)] == 1 (mod m^2)
566// if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
567// should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
568// JS multiply "overflows" differently from C/C++, so care is needed here.
569function bnpInvDigit() {
570 if (this.t < 1) return 0;
572 if ((x & 1) === 0) return 0;
573 var y = x & 3; // y == 1/x mod 2^2
574 y = (y * (2 - (x & 0xf) * y)) & 0xf; // y == 1/x mod 2^4
575 y = (y * (2 - (x & 0xff) * y)) & 0xff; // y == 1/x mod 2^8
576 y = (y * (2 - (((x & 0xffff) * y) & 0xffff))) & 0xffff; // y == 1/x mod 2^16
577 // last step - calculate inverse mod DV directly;
578 // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
579 y = (y * (2 - x * y % this.DV)) % this.DV; // y == 1/x mod 2^dbits
580 // we really want the negative inverse, and -DV < y < DV
581 return (y > 0) ? this.DV - y : -y;
584// Montgomery reduction
585function Montgomery(m) {
587 this.mp = m.invDigit();
588 this.mpl = this.mp & 0x7fff;
589 this.mph = this.mp >> 15;
590 this.um = (1 << (m.DB - 15)) - 1;
595function montConvert(x) {
597 x.abs().dlShiftTo(this.m.t, r);
598 r.divRemTo(this.m, null, r);
599 if (x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r, r);
604function montRevert(x) {
611// x = x/R mod m (HAC 14.32)
612function montReduce(x) {
613 while (x.t <= this.mt2) // pad x so am has enough room later
615 for (var i = 0; i < this.m.t; ++i) {
616 // faster way of calculating u0 = x[i]*mp mod DV
617 var j = x[i] & 0x7fff;
618 var u0 = (j * this.mpl + (((j * this.mph + (x[i] >> 15) * this.mpl) & this.um) << 15)) & x.DM;
619 // use am to combine the multiply-shift-add into one call
621 x[j] += this.m.am(0, u0, x, i, 0, this.m.t);
623 while (x[j] >= x.DV) {
629 x.drShiftTo(this.m.t, x);
630 if (x.compareTo(this.m) >= 0) x.subTo(this.m, x);
633// r = "x^2/R mod m"; x != r
634function montSqrTo(x, r) {
639// r = "xy/R mod m"; x,y != r
640function montMulTo(x, y, r) {
645Montgomery.prototype.convert = montConvert;
646Montgomery.prototype.revert = montRevert;
647Montgomery.prototype.reduce = montReduce;
648Montgomery.prototype.mulTo = montMulTo;
649Montgomery.prototype.sqrTo = montSqrTo;
651// (protected) true iff this is even
652function bnpIsEven() {
653 return ((this.t > 0) ? (this[0] & 1) : this.s) === 0;
656// (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
657function bnpExp(e, z) {
658 if (e > 0xffffffff || e < 1) return BigInteger.ONE;
659 var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e) - 1;
663 if ((e & (1 << i)) > 0) z.mulTo(r2, g, r);
673// (public) this^e % m, 0 <= e < 2^32
674function bnModPowInt(e, m) {
676 if (e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
677 return this.exp(e, z);
680// Copyright (c) 2005-2009 Tom Wu
681// All Rights Reserved.
682// See "LICENSE" for details.
684// Extended JavaScript BN functions, required for RSA private ops.
686// Version 1.1: new BigInteger("0", 10) returns "proper" zero
687// Version 1.2: square() API, isProbablePrime fix
696//(public) return value as integer
697function bnIntValue() {
699 if (this.t == 1) return this[0] - this.DV;
700 else if (this.t === 0) return -1;
702 else if (this.t == 1) return this[0];
703 else if (this.t === 0) return 0;
704// assumes 16 < DB < 32
705 return ((this[1] & ((1 << (32 - this.DB)) - 1)) << this.DB) | this[0];
708//(public) return value as byte
709function bnByteValue() {
710 return (this.t == 0) ? this.s : (this[0] << 24) >> 24;
713//(public) return value as short (assumes DB>=16)
714function bnShortValue() {
715 return (this.t == 0) ? this.s : (this[0] << 16) >> 16;
718//(protected) return x s.t. r^x < DV
719function bnpChunkSize(r) {
720 return Math.floor(Math.LN2 * this.DB / Math.log(r));
723//(public) 0 if this === 0, 1 if this > 0
725 if (this.s < 0) return -1;
726 else if (this.t <= 0 || (this.t == 1 && this[0] <= 0)) return 0;
730//(protected) convert to radix string
731function bnpToRadix(b) {
732 if (b == null) b = 10;
733 if (this.signum() === 0 || b < 2 || b > 36) return "0";
734 var cs = this.chunkSize(b);
735 var a = Math.pow(b, cs);
736 var d = nbv(a), y = nbi(), z = nbi(), r = "";
737 this.divRemTo(d, y, z);
738 while (y.signum() > 0) {
739 r = (a + z.intValue()).toString(b).substr(1) + r;
742 return z.intValue().toString(b) + r;
745//(protected) convert from radix string
746function bnpFromRadix(s, b) {
748 if (b == null) b = 10;
749 var cs = this.chunkSize(b);
750 var d = Math.pow(b, cs), mi = false, j = 0, w = 0;
751 for (var i = 0; i < s.length; ++i) {
754 if (s.charAt(i) == "-" && this.signum() === 0) mi = true;
760 this.dAddOffset(w, 0);
766 this.dMultiply(Math.pow(b, j));
767 this.dAddOffset(w, 0);
769 if (mi) BigInteger.ZERO.subTo(this, this);
772//(protected) alternate constructor
773function bnpFromNumber(a, b) {
774 if ("number" == typeof b) {
775 // new BigInteger(int,int,RNG)
776 if (a < 2) this.fromInt(1);
779 if (!this.testBit(a - 1)) // force MSB set
780 this.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, this);
781 if (this.isEven()) this.dAddOffset(1, 0); // force odd
782 while (!this.isProbablePrime(b)) {
783 this.dAddOffset(2, 0);
784 if (this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a - 1), this);
788 // new BigInteger(int,RNG)
789 var x = crypt.randomBytes((a >> 3) + 1)
793 x[0] &= ((1 << t) - 1);
797 this.fromByteArray(x);
801//(public) convert to bigendian byte array
802function bnToByteArray() {
803 var i = this.t, r = new Array();
805 var p = this.DB - (i * this.DB) % 8, d, k = 0;
807 if (p < this.DB && (d = this[i] >> p) != (this.s & this.DM) >> p)
808 r[k++] = d | (this.s << (this.DB - p));
811 d = (this[i] & ((1 << p) - 1)) << (8 - p);
812 d |= this[--i] >> (p += this.DB - 8);
815 d = (this[i] >> (p -= 8)) & 0xff;
821 if ((d & 0x80) != 0) d |= -256;
822 if (k === 0 && (this.s & 0x80) != (d & 0x80)) ++k;
823 if (k > 0 || d != this.s) r[k++] = d;
830 * return Buffer object
831 * @param trim {boolean} slice buffer if first element == 0
834function bnToBuffer(trimOrSize) {
835 var res = Buffer.from(this.toByteArray());
836 if (trimOrSize === true && res[0] === 0) {
838 } else if (isNumber(trimOrSize)) {
839 if (res.length > trimOrSize) {
840 for (var i = 0; i < res.length - trimOrSize; i++) {
845 return res.slice(res.length - trimOrSize);
846 } else if (res.length < trimOrSize) {
847 var padded = Buffer.alloc(trimOrSize);
848 padded.fill(0, 0, trimOrSize - res.length);
849 res.copy(padded, trimOrSize - res.length);
856function bnEquals(a) {
857 return (this.compareTo(a) == 0);
860 return (this.compareTo(a) < 0) ? this : a;
863 return (this.compareTo(a) > 0) ? this : a;
866//(protected) r = this op a (bitwise)
867function bnpBitwiseTo(a, op, r) {
868 var i, f, m = Math.min(a.t, this.t);
869 for (i = 0; i < m; ++i) r[i] = op(this[i], a[i]);
872 for (i = m; i < this.t; ++i) r[i] = op(this[i], f);
876 f = this.s & this.DM;
877 for (i = m; i < a.t; ++i) r[i] = op(f, a[i]);
880 r.s = op(this.s, a.s);
885function op_and(x, y) {
890 this.bitwiseTo(a, op_and, r);
895function op_or(x, y) {
900 this.bitwiseTo(a, op_or, r);
905function op_xor(x, y) {
910 this.bitwiseTo(a, op_xor, r);
915function op_andnot(x, y) {
918function bnAndNot(a) {
920 this.bitwiseTo(a, op_andnot, r);
927 for (var i = 0; i < this.t; ++i) r[i] = this.DM & ~this[i];
934function bnShiftLeft(n) {
936 if (n < 0) this.rShiftTo(-n, r); else this.lShiftTo(n, r);
941function bnShiftRight(n) {
943 if (n < 0) this.lShiftTo(-n, r); else this.rShiftTo(n, r);
947//return index of lowest 1-bit in x, x < 2^31
949 if (x === 0) return -1;
951 if ((x & 0xffff) === 0) {
955 if ((x & 0xff) === 0) {
959 if ((x & 0xf) === 0) {
967 if ((x & 1) === 0) ++r;
971//(public) returns index of lowest 1-bit (or -1 if none)
972function bnGetLowestSetBit() {
973 for (var i = 0; i < this.t; ++i)
974 if (this[i] != 0) return i * this.DB + lbit(this[i]);
975 if (this.s < 0) return this.t * this.DB;
979//return number of 1 bits in x
989//(public) return number of set bits
990function bnBitCount() {
991 var r = 0, x = this.s & this.DM;
992 for (var i = 0; i < this.t; ++i) r += cbit(this[i] ^ x);
996//(public) true iff nth bit is set
997function bnTestBit(n) {
998 var j = Math.floor(n / this.DB);
999 if (j >= this.t) return (this.s != 0);
1000 return ((this[j] & (1 << (n % this.DB))) != 0);
1003//(protected) this op (1<<n)
1004function bnpChangeBit(n, op) {
1005 var r = BigInteger.ONE.shiftLeft(n);
1006 this.bitwiseTo(r, op, r);
1010//(public) this | (1<<n)
1011function bnSetBit(n) {
1012 return this.changeBit(n, op_or);
1015//(public) this & ~(1<<n)
1016function bnClearBit(n) {
1017 return this.changeBit(n, op_andnot);
1020//(public) this ^ (1<<n)
1021function bnFlipBit(n) {
1022 return this.changeBit(n, op_xor);
1025//(protected) r = this + a
1026function bnpAddTo(a, r) {
1027 var i = 0, c = 0, m = Math.min(a.t, this.t);
1029 c += this[i] + a[i];
1030 r[i++] = c & this.DM;
1035 while (i < this.t) {
1037 r[i++] = c & this.DM;
1046 r[i++] = c & this.DM;
1051 r.s = (c < 0) ? -1 : 0;
1052 if (c > 0) r[i++] = c;
1053 else if (c < -1) r[i++] = this.DV + c;
1066function bnSubtract(a) {
1073function bnMultiply(a) {
1075 this.multiplyTo(a, r);
1080function bnSquare() {
1087function bnDivide(a) {
1089 this.divRemTo(a, r, null);
1094function bnRemainder(a) {
1096 this.divRemTo(a, null, r);
1100//(public) [this/a,this%a]
1101function bnDivideAndRemainder(a) {
1102 var q = nbi(), r = nbi();
1103 this.divRemTo(a, q, r);
1104 return new Array(q, r);
1107//(protected) this *= n, this >= 0, 1 < n < DV
1108function bnpDMultiply(n) {
1109 this[this.t] = this.am(0, n - 1, this, 0, 0, this.t);
1114//(protected) this += n << w words, this >= 0
1115function bnpDAddOffset(n, w) {
1116 if (n === 0) return;
1117 while (this.t <= w) this[this.t++] = 0;
1119 while (this[w] >= this.DV) {
1121 if (++w >= this.t) this[this.t++] = 0;
1132function nMulTo(x, y, r) {
1135function nSqrTo(x, r) {
1139NullExp.prototype.convert = nNop;
1140NullExp.prototype.revert = nNop;
1141NullExp.prototype.mulTo = nMulTo;
1142NullExp.prototype.sqrTo = nSqrTo;
1146 return this.exp(e, new NullExp());
1149//(protected) r = lower n words of "this * a", a.t <= n
1150//"this" should be the larger one if appropriate.
1151function bnpMultiplyLowerTo(a, n, r) {
1152 var i = Math.min(this.t + a.t, n);
1153 r.s = 0; // assumes a,this >= 0
1155 while (i > 0) r[--i] = 0;
1157 for (j = r.t - this.t; i < j; ++i) r[i + this.t] = this.am(0, a[i], r, i, 0, this.t);
1158 for (j = Math.min(a.t, n); i < j; ++i) this.am(0, a[i], r, i, 0, n - i);
1162//(protected) r = "this * a" without lower n words, n > 0
1163//"this" should be the larger one if appropriate.
1164function bnpMultiplyUpperTo(a, n, r) {
1166 var i = r.t = this.t + a.t - n;
1167 r.s = 0; // assumes a,this >= 0
1168 while (--i >= 0) r[i] = 0;
1169 for (i = Math.max(n - this.t, 0); i < a.t; ++i)
1170 r[this.t + i - n] = this.am(n - i, a[i], r, 0, 0, this.t + i - n);
1175//Barrett modular reduction
1176function Barrett(m) {
1180 BigInteger.ONE.dlShiftTo(2 * m.t, this.r2);
1181 this.mu = this.r2.divide(m);
1185function barrettConvert(x) {
1186 if (x.s < 0 || x.t > 2 * this.m.t) return x.mod(this.m);
1187 else if (x.compareTo(this.m) < 0) return x;
1196function barrettRevert(x) {
1200//x = x mod m (HAC 14.42)
1201function barrettReduce(x) {
1202 x.drShiftTo(this.m.t - 1, this.r2);
1203 if (x.t > this.m.t + 1) {
1207 this.mu.multiplyUpperTo(this.r2, this.m.t + 1, this.q3);
1208 this.m.multiplyLowerTo(this.q3, this.m.t + 1, this.r2);
1209 while (x.compareTo(this.r2) < 0) x.dAddOffset(1, this.m.t + 1);
1210 x.subTo(this.r2, x);
1211 while (x.compareTo(this.m) >= 0) x.subTo(this.m, x);
1214//r = x^2 mod m; x != r
1215function barrettSqrTo(x, r) {
1220//r = x*y mod m; x,y != r
1221function barrettMulTo(x, y, r) {
1226Barrett.prototype.convert = barrettConvert;
1227Barrett.prototype.revert = barrettRevert;
1228Barrett.prototype.reduce = barrettReduce;
1229Barrett.prototype.mulTo = barrettMulTo;
1230Barrett.prototype.sqrTo = barrettSqrTo;
1232//(public) this^e % m (HAC 14.85)
1233function bnModPow(e, m) {
1234 var i = e.bitLength(), k, r = nbv(1), z;
1235 if (i <= 0) return r;
1236 else if (i < 18) k = 1;
1237 else if (i < 48) k = 3;
1238 else if (i < 144) k = 4;
1239 else if (i < 768) k = 5;
1243 else if (m.isEven())
1246 z = new Montgomery(m);
1249 var g = new Array(), n = 3, k1 = k - 1, km = (1 << k) - 1;
1250 g[1] = z.convert(this);
1256 z.mulTo(g2, g[n - 2], g[n]);
1261 var j = e.t - 1, w, is1 = true, r2 = nbi(), t;
1262 i = nbits(e[j]) - 1;
1264 if (i >= k1) w = (e[j] >> (i - k1)) & km;
1266 w = (e[j] & ((1 << (i + 1)) - 1)) << (k1 - i);
1267 if (j > 0) w |= e[j - 1] >> (this.DB + i - k1);
1271 while ((w & 1) === 0) {
1279 if (is1) { // ret == 1, don't bother squaring or multiplying it
1289 if (n > 0) z.sqrTo(r, r2); else {
1294 z.mulTo(r2, g[w], r);
1297 while (j >= 0 && (e[j] & (1 << i)) === 0) {
1311//(public) gcd(this,a) (HAC 14.54)
1313 var x = (this.s < 0) ? this.negate() : this.clone();
1314 var y = (a.s < 0) ? a.negate() : a.clone();
1315 if (x.compareTo(y) < 0) {
1320 var i = x.getLowestSetBit(), g = y.getLowestSetBit();
1321 if (g < 0) return x;
1327 while (x.signum() > 0) {
1328 if ((i = x.getLowestSetBit()) > 0) x.rShiftTo(i, x);
1329 if ((i = y.getLowestSetBit()) > 0) y.rShiftTo(i, y);
1330 if (x.compareTo(y) >= 0) {
1339 if (g > 0) y.lShiftTo(g, y);
1343//(protected) this % n, n < 2^26
1344function bnpModInt(n) {
1345 if (n <= 0) return 0;
1346 var d = this.DV % n, r = (this.s < 0) ? n - 1 : 0;
1348 if (d === 0) r = this[0] % n;
1349 else for (var i = this.t - 1; i >= 0; --i) r = (d * r + this[i]) % n;
1353//(public) 1/this % m (HAC 14.61)
1354function bnModInverse(m) {
1355 var ac = m.isEven();
1356 if ((this.isEven() && ac) || m.signum() === 0) return BigInteger.ZERO;
1357 var u = m.clone(), v = this.clone();
1358 var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
1359 while (u.signum() != 0) {
1360 while (u.isEven()) {
1363 if (!a.isEven() || !b.isEven()) {
1369 else if (!b.isEven()) b.subTo(m, b);
1372 while (v.isEven()) {
1375 if (!c.isEven() || !d.isEven()) {
1381 else if (!d.isEven()) d.subTo(m, d);
1384 if (u.compareTo(v) >= 0) {
1386 if (ac) a.subTo(c, a);
1391 if (ac) c.subTo(a, c);
1395 if (v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
1396 if (d.compareTo(m) >= 0) return d.subtract(m);
1397 if (d.signum() < 0) d.addTo(m, d); else return d;
1398 if (d.signum() < 0) return d.add(m); else return d;
1401var lowprimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997];
1402var lplim = (1 << 26) / lowprimes[lowprimes.length - 1];
1404//(public) test primality with certainty >= 1-.5^t
1405function bnIsProbablePrime(t) {
1406 var i, x = this.abs();
1407 if (x.t == 1 && x[0] <= lowprimes[lowprimes.length - 1]) {
1408 for (i = 0; i < lowprimes.length; ++i)
1409 if (x[0] == lowprimes[i]) return true;
1412 if (x.isEven()) return false;
1414 while (i < lowprimes.length) {
1415 var m = lowprimes[i], j = i + 1;
1416 while (j < lowprimes.length && m < lplim) m *= lowprimes[j++];
1418 while (i < j) if (m % lowprimes[i++] === 0) return false;
1420 return x.millerRabin(t);
1423//(protected) true if probably prime (HAC 4.24, Miller-Rabin)
1424function bnpMillerRabin(t) {
1425 var n1 = this.subtract(BigInteger.ONE);
1426 var k = n1.getLowestSetBit();
1427 if (k <= 0) return false;
1428 var r = n1.shiftRight(k);
1430 if (t > lowprimes.length) t = lowprimes.length;
1432 for (var i = 0; i < t; ++i) {
1433 //Pick bases at random, instead of starting at 2
1434 a.fromInt(lowprimes[Math.floor(Math.random() * lowprimes.length)]);
1435 var y = a.modPow(r, this);
1436 if (y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
1438 while (j++ < k && y.compareTo(n1) != 0) {
1439 y = y.modPowInt(2, this);
1440 if (y.compareTo(BigInteger.ONE) === 0) return false;
1442 if (y.compareTo(n1) != 0) return false;
1449BigInteger.prototype.copyTo = bnpCopyTo;
1450BigInteger.prototype.fromInt = bnpFromInt;
1451BigInteger.prototype.fromString = bnpFromString;
1452BigInteger.prototype.fromByteArray = bnpFromByteArray;
1453BigInteger.prototype.fromBuffer = bnpFromBuffer;
1454BigInteger.prototype.clamp = bnpClamp;
1455BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
1456BigInteger.prototype.drShiftTo = bnpDRShiftTo;
1457BigInteger.prototype.lShiftTo = bnpLShiftTo;
1458BigInteger.prototype.rShiftTo = bnpRShiftTo;
1459BigInteger.prototype.subTo = bnpSubTo;
1460BigInteger.prototype.multiplyTo = bnpMultiplyTo;
1461BigInteger.prototype.squareTo = bnpSquareTo;
1462BigInteger.prototype.divRemTo = bnpDivRemTo;
1463BigInteger.prototype.invDigit = bnpInvDigit;
1464BigInteger.prototype.isEven = bnpIsEven;
1465BigInteger.prototype.exp = bnpExp;
1467BigInteger.prototype.chunkSize = bnpChunkSize;
1468BigInteger.prototype.toRadix = bnpToRadix;
1469BigInteger.prototype.fromRadix = bnpFromRadix;
1470BigInteger.prototype.fromNumber = bnpFromNumber;
1471BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
1472BigInteger.prototype.changeBit = bnpChangeBit;
1473BigInteger.prototype.addTo = bnpAddTo;
1474BigInteger.prototype.dMultiply = bnpDMultiply;
1475BigInteger.prototype.dAddOffset = bnpDAddOffset;
1476BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
1477BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
1478BigInteger.prototype.modInt = bnpModInt;
1479BigInteger.prototype.millerRabin = bnpMillerRabin;
1483BigInteger.prototype.toString = bnToString;
1484BigInteger.prototype.negate = bnNegate;
1485BigInteger.prototype.abs = bnAbs;
1486BigInteger.prototype.compareTo = bnCompareTo;
1487BigInteger.prototype.bitLength = bnBitLength;
1488BigInteger.prototype.mod = bnMod;
1489BigInteger.prototype.modPowInt = bnModPowInt;
1491BigInteger.prototype.clone = bnClone;
1492BigInteger.prototype.intValue = bnIntValue;
1493BigInteger.prototype.byteValue = bnByteValue;
1494BigInteger.prototype.shortValue = bnShortValue;
1495BigInteger.prototype.signum = bnSigNum;
1496BigInteger.prototype.toByteArray = bnToByteArray;
1497BigInteger.prototype.toBuffer = bnToBuffer;
1498BigInteger.prototype.equals = bnEquals;
1499BigInteger.prototype.min = bnMin;
1500BigInteger.prototype.max = bnMax;
1501BigInteger.prototype.and = bnAnd;
1502BigInteger.prototype.or = bnOr;
1503BigInteger.prototype.xor = bnXor;
1504BigInteger.prototype.andNot = bnAndNot;
1505BigInteger.prototype.not = bnNot;
1506BigInteger.prototype.shiftLeft = bnShiftLeft;
1507BigInteger.prototype.shiftRight = bnShiftRight;
1508BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
1509BigInteger.prototype.bitCount = bnBitCount;
1510BigInteger.prototype.testBit = bnTestBit;
1511BigInteger.prototype.setBit = bnSetBit;
1512BigInteger.prototype.clearBit = bnClearBit;
1513BigInteger.prototype.flipBit = bnFlipBit;
1514BigInteger.prototype.add = bnAdd;
1515BigInteger.prototype.subtract = bnSubtract;
1516BigInteger.prototype.multiply = bnMultiply;
1517BigInteger.prototype.divide = bnDivide;
1518BigInteger.prototype.remainder = bnRemainder;
1519BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
1520BigInteger.prototype.modPow = bnModPow;
1521BigInteger.prototype.modInverse = bnModInverse;
1522BigInteger.prototype.pow = bnPow;
1523BigInteger.prototype.gcd = bnGCD;
1524BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
1525BigInteger.int2char = int2char;
1528BigInteger.ZERO = nbv(0);
1529BigInteger.ONE = nbv(1);
1531// JSBN-specific extension
1532BigInteger.prototype.square = bnSquare;
1534//BigInteger interfaces not implemented in jsbn:
1536//BigInteger(int signum, byte[] magnitude)
1537//double doubleValue()
1541//static BigInteger valueOf(long val)
1543module.exports = BigInteger;