UNPKG

19.5 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = void 0;
5// A small implementation of BigInteger based on http://www-cs-students.stanford.edu/~tjw/jsbn/
6//
7// All public methods have been removed except the following:
8// new BigInteger(a, b) (only radix 2, 4, 8, 16 and 32 supported)
9// toString (only radix 2, 4, 8, 16 and 32 supported)
10// negate
11// abs
12// compareTo
13// bitLength
14// mod
15// equals
16// add
17// subtract
18// multiply
19// divide
20// modPow
21var _default = BigInteger;
22/*
23 * Copyright (c) 2003-2005 Tom Wu
24 * All Rights Reserved.
25 *
26 * Permission is hereby granted, free of charge, to any person obtaining
27 * a copy of this software and associated documentation files (the
28 * "Software"), to deal in the Software without restriction, including
29 * without limitation the rights to use, copy, modify, merge, publish,
30 * distribute, sublicense, and/or sell copies of the Software, and to
31 * permit persons to whom the Software is furnished to do so, subject to
32 * the following conditions:
33 *
34 * The above copyright notice and this permission notice shall be
35 * included in all copies or substantial portions of the Software.
36 *
37 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
38 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
39 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
40 *
41 * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
42 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
43 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
44 * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
45 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46 *
47 * In addition, the following condition applies:
48 *
49 * All redistributions must retain an intact copy of this copyright notice
50 * and disclaimer.
51 */
52// (public) Constructor
53
54exports["default"] = _default;
55
56function BigInteger(a, b) {
57 if (a != null) this.fromString(a, b);
58} // return new, unset BigInteger
59
60
61function nbi() {
62 return new BigInteger(null);
63} // Bits per digit
64
65
66var dbits; // JavaScript engine analysis
67
68var canary = 0xdeadbeefcafe;
69var j_lm = (canary & 0xffffff) == 0xefcafe; // am: Compute w_j += (x*this_i), propagate carries,
70// c is initial carry, returns final carry.
71// c < 3*dvalue, x < 2*dvalue, this_i < dvalue
72// We need to select the fastest one that works in this environment.
73// am1: use a single mult and divide to get the high bits,
74// max digit bits should be 26 because
75// max internal value = 2*dvalue^2-2*dvalue (< 2^53)
76
77function am1(i, x, w, j, c, n) {
78 while (--n >= 0) {
79 var v = x * this[i++] + w[j] + c;
80 c = Math.floor(v / 0x4000000);
81 w[j++] = v & 0x3ffffff;
82 }
83
84 return c;
85} // am2 avoids a big mult-and-extract completely.
86// Max digit bits should be <= 30 because we do bitwise ops
87// on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
88
89
90function am2(i, x, w, j, c, n) {
91 var xl = x & 0x7fff,
92 xh = x >> 15;
93
94 while (--n >= 0) {
95 var l = this[i] & 0x7fff;
96 var h = this[i++] >> 15;
97 var m = xh * l + h * xl;
98 l = xl * l + ((m & 0x7fff) << 15) + w[j] + (c & 0x3fffffff);
99 c = (l >>> 30) + (m >>> 15) + xh * h + (c >>> 30);
100 w[j++] = l & 0x3fffffff;
101 }
102
103 return c;
104} // Alternately, set max digit bits to 28 since some
105// browsers slow down when dealing with 32-bit numbers.
106
107
108function am3(i, x, w, j, c, n) {
109 var xl = x & 0x3fff,
110 xh = x >> 14;
111
112 while (--n >= 0) {
113 var l = this[i] & 0x3fff;
114 var h = this[i++] >> 14;
115 var m = xh * l + h * xl;
116 l = xl * l + ((m & 0x3fff) << 14) + w[j] + c;
117 c = (l >> 28) + (m >> 14) + xh * h;
118 w[j++] = l & 0xfffffff;
119 }
120
121 return c;
122}
123
124var inBrowser = typeof navigator !== 'undefined';
125
126if (inBrowser && j_lm && navigator.appName == 'Microsoft Internet Explorer') {
127 BigInteger.prototype.am = am2;
128 dbits = 30;
129} else if (inBrowser && j_lm && navigator.appName != 'Netscape') {
130 BigInteger.prototype.am = am1;
131 dbits = 26;
132} else {
133 // Mozilla/Netscape seems to prefer am3
134 BigInteger.prototype.am = am3;
135 dbits = 28;
136}
137
138BigInteger.prototype.DB = dbits;
139BigInteger.prototype.DM = (1 << dbits) - 1;
140BigInteger.prototype.DV = 1 << dbits;
141var BI_FP = 52;
142BigInteger.prototype.FV = Math.pow(2, BI_FP);
143BigInteger.prototype.F1 = BI_FP - dbits;
144BigInteger.prototype.F2 = 2 * dbits - BI_FP; // Digit conversions
145
146var BI_RM = '0123456789abcdefghijklmnopqrstuvwxyz';
147var BI_RC = new Array();
148var rr, vv;
149rr = '0'.charCodeAt(0);
150
151for (vv = 0; vv <= 9; ++vv) {
152 BI_RC[rr++] = vv;
153}
154
155rr = 'a'.charCodeAt(0);
156
157for (vv = 10; vv < 36; ++vv) {
158 BI_RC[rr++] = vv;
159}
160
161rr = 'A'.charCodeAt(0);
162
163for (vv = 10; vv < 36; ++vv) {
164 BI_RC[rr++] = vv;
165}
166
167function int2char(n) {
168 return BI_RM.charAt(n);
169}
170
171function intAt(s, i) {
172 var c = BI_RC[s.charCodeAt(i)];
173 return c == null ? -1 : c;
174} // (protected) copy this to r
175
176
177function bnpCopyTo(r) {
178 for (var i = this.t - 1; i >= 0; --i) {
179 r[i] = this[i];
180 }
181
182 r.t = this.t;
183 r.s = this.s;
184} // (protected) set from integer value x, -DV <= x < DV
185
186
187function bnpFromInt(x) {
188 this.t = 1;
189 this.s = x < 0 ? -1 : 0;
190 if (x > 0) this[0] = x;else if (x < -1) this[0] = x + this.DV;else this.t = 0;
191} // return bigint initialized to value
192
193
194function nbv(i) {
195 var r = nbi();
196 r.fromInt(i);
197 return r;
198} // (protected) set from string and radix
199
200
201function bnpFromString(s, b) {
202 var k;
203 if (b == 16) k = 4;else if (b == 8) k = 3;else if (b == 2) k = 1;else if (b == 32) k = 5;else if (b == 4) k = 2;else throw new Error('Only radix 2, 4, 8, 16, 32 are supported');
204 this.t = 0;
205 this.s = 0;
206 var i = s.length,
207 mi = false,
208 sh = 0;
209
210 while (--i >= 0) {
211 var x = intAt(s, i);
212
213 if (x < 0) {
214 if (s.charAt(i) == '-') mi = true;
215 continue;
216 }
217
218 mi = false;
219 if (sh == 0) this[this.t++] = x;else if (sh + k > this.DB) {
220 this[this.t - 1] |= (x & (1 << this.DB - sh) - 1) << sh;
221 this[this.t++] = x >> this.DB - sh;
222 } else this[this.t - 1] |= x << sh;
223 sh += k;
224 if (sh >= this.DB) sh -= this.DB;
225 }
226
227 this.clamp();
228 if (mi) BigInteger.ZERO.subTo(this, this);
229} // (protected) clamp off excess high words
230
231
232function bnpClamp() {
233 var c = this.s & this.DM;
234
235 while (this.t > 0 && this[this.t - 1] == c) {
236 --this.t;
237 }
238} // (public) return string representation in given radix
239
240
241function bnToString(b) {
242 if (this.s < 0) return '-' + this.negate().toString(b);
243 var k;
244 if (b == 16) k = 4;else if (b == 8) k = 3;else if (b == 2) k = 1;else if (b == 32) k = 5;else if (b == 4) k = 2;else throw new Error('Only radix 2, 4, 8, 16, 32 are supported');
245 var km = (1 << k) - 1,
246 d,
247 m = false,
248 r = '',
249 i = this.t;
250 var p = this.DB - i * this.DB % k;
251
252 if (i-- > 0) {
253 if (p < this.DB && (d = this[i] >> p) > 0) {
254 m = true;
255 r = int2char(d);
256 }
257
258 while (i >= 0) {
259 if (p < k) {
260 d = (this[i] & (1 << p) - 1) << k - p;
261 d |= this[--i] >> (p += this.DB - k);
262 } else {
263 d = this[i] >> (p -= k) & km;
264
265 if (p <= 0) {
266 p += this.DB;
267 --i;
268 }
269 }
270
271 if (d > 0) m = true;
272 if (m) r += int2char(d);
273 }
274 }
275
276 return m ? r : '0';
277} // (public) -this
278
279
280function bnNegate() {
281 var r = nbi();
282 BigInteger.ZERO.subTo(this, r);
283 return r;
284} // (public) |this|
285
286
287function bnAbs() {
288 return this.s < 0 ? this.negate() : this;
289} // (public) return + if this > a, - if this < a, 0 if equal
290
291
292function bnCompareTo(a) {
293 var r = this.s - a.s;
294 if (r != 0) return r;
295 var i = this.t;
296 r = i - a.t;
297 if (r != 0) return this.s < 0 ? -r : r;
298
299 while (--i >= 0) {
300 if ((r = this[i] - a[i]) != 0) return r;
301 }
302
303 return 0;
304} // returns bit length of the integer x
305
306
307function nbits(x) {
308 var r = 1,
309 t;
310
311 if ((t = x >>> 16) != 0) {
312 x = t;
313 r += 16;
314 }
315
316 if ((t = x >> 8) != 0) {
317 x = t;
318 r += 8;
319 }
320
321 if ((t = x >> 4) != 0) {
322 x = t;
323 r += 4;
324 }
325
326 if ((t = x >> 2) != 0) {
327 x = t;
328 r += 2;
329 }
330
331 if ((t = x >> 1) != 0) {
332 x = t;
333 r += 1;
334 }
335
336 return r;
337} // (public) return the number of bits in "this"
338
339
340function bnBitLength() {
341 if (this.t <= 0) return 0;
342 return this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ this.s & this.DM);
343} // (protected) r = this << n*DB
344
345
346function bnpDLShiftTo(n, r) {
347 var i;
348
349 for (i = this.t - 1; i >= 0; --i) {
350 r[i + n] = this[i];
351 }
352
353 for (i = n - 1; i >= 0; --i) {
354 r[i] = 0;
355 }
356
357 r.t = this.t + n;
358 r.s = this.s;
359} // (protected) r = this >> n*DB
360
361
362function bnpDRShiftTo(n, r) {
363 for (var i = n; i < this.t; ++i) {
364 r[i - n] = this[i];
365 }
366
367 r.t = Math.max(this.t - n, 0);
368 r.s = this.s;
369} // (protected) r = this << n
370
371
372function bnpLShiftTo(n, r) {
373 var bs = n % this.DB;
374 var cbs = this.DB - bs;
375 var bm = (1 << cbs) - 1;
376 var ds = Math.floor(n / this.DB),
377 c = this.s << bs & this.DM,
378 i;
379
380 for (i = this.t - 1; i >= 0; --i) {
381 r[i + ds + 1] = this[i] >> cbs | c;
382 c = (this[i] & bm) << bs;
383 }
384
385 for (i = ds - 1; i >= 0; --i) {
386 r[i] = 0;
387 }
388
389 r[ds] = c;
390 r.t = this.t + ds + 1;
391 r.s = this.s;
392 r.clamp();
393} // (protected) r = this >> n
394
395
396function bnpRShiftTo(n, r) {
397 r.s = this.s;
398 var ds = Math.floor(n / this.DB);
399
400 if (ds >= this.t) {
401 r.t = 0;
402 return;
403 }
404
405 var bs = n % this.DB;
406 var cbs = this.DB - bs;
407 var bm = (1 << bs) - 1;
408 r[0] = this[ds] >> bs;
409
410 for (var i = ds + 1; i < this.t; ++i) {
411 r[i - ds - 1] |= (this[i] & bm) << cbs;
412 r[i - ds] = this[i] >> bs;
413 }
414
415 if (bs > 0) r[this.t - ds - 1] |= (this.s & bm) << cbs;
416 r.t = this.t - ds;
417 r.clamp();
418} // (protected) r = this - a
419
420
421function bnpSubTo(a, r) {
422 var i = 0,
423 c = 0,
424 m = Math.min(a.t, this.t);
425
426 while (i < m) {
427 c += this[i] - a[i];
428 r[i++] = c & this.DM;
429 c >>= this.DB;
430 }
431
432 if (a.t < this.t) {
433 c -= a.s;
434
435 while (i < this.t) {
436 c += this[i];
437 r[i++] = c & this.DM;
438 c >>= this.DB;
439 }
440
441 c += this.s;
442 } else {
443 c += this.s;
444
445 while (i < a.t) {
446 c -= a[i];
447 r[i++] = c & this.DM;
448 c >>= this.DB;
449 }
450
451 c -= a.s;
452 }
453
454 r.s = c < 0 ? -1 : 0;
455 if (c < -1) r[i++] = this.DV + c;else if (c > 0) r[i++] = c;
456 r.t = i;
457 r.clamp();
458} // (protected) r = this * a, r != this,a (HAC 14.12)
459// "this" should be the larger one if appropriate.
460
461
462function bnpMultiplyTo(a, r) {
463 var x = this.abs(),
464 y = a.abs();
465 var i = x.t;
466 r.t = i + y.t;
467
468 while (--i >= 0) {
469 r[i] = 0;
470 }
471
472 for (i = 0; i < y.t; ++i) {
473 r[i + x.t] = x.am(0, y[i], r, i, 0, x.t);
474 }
475
476 r.s = 0;
477 r.clamp();
478 if (this.s != a.s) BigInteger.ZERO.subTo(r, r);
479} // (protected) r = this^2, r != this (HAC 14.16)
480
481
482function bnpSquareTo(r) {
483 var x = this.abs();
484 var i = r.t = 2 * x.t;
485
486 while (--i >= 0) {
487 r[i] = 0;
488 }
489
490 for (i = 0; i < x.t - 1; ++i) {
491 var c = x.am(i, x[i], r, 2 * i, 0, 1);
492
493 if ((r[i + x.t] += x.am(i + 1, 2 * x[i], r, 2 * i + 1, c, x.t - i - 1)) >= x.DV) {
494 r[i + x.t] -= x.DV;
495 r[i + x.t + 1] = 1;
496 }
497 }
498
499 if (r.t > 0) r[r.t - 1] += x.am(i, x[i], r, 2 * i, 0, 1);
500 r.s = 0;
501 r.clamp();
502} // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
503// r != q, this != m. q or r may be null.
504
505
506function bnpDivRemTo(m, q, r) {
507 var pm = m.abs();
508 if (pm.t <= 0) return;
509 var pt = this.abs();
510
511 if (pt.t < pm.t) {
512 if (q != null) q.fromInt(0);
513 if (r != null) this.copyTo(r);
514 return;
515 }
516
517 if (r == null) r = nbi();
518 var y = nbi(),
519 ts = this.s,
520 ms = m.s;
521 var nsh = this.DB - nbits(pm[pm.t - 1]); // normalize modulus
522
523 if (nsh > 0) {
524 pm.lShiftTo(nsh, y);
525 pt.lShiftTo(nsh, r);
526 } else {
527 pm.copyTo(y);
528 pt.copyTo(r);
529 }
530
531 var ys = y.t;
532 var y0 = y[ys - 1];
533 if (y0 == 0) return;
534 var yt = y0 * (1 << this.F1) + (ys > 1 ? y[ys - 2] >> this.F2 : 0);
535 var d1 = this.FV / yt,
536 d2 = (1 << this.F1) / yt,
537 e = 1 << this.F2;
538 var i = r.t,
539 j = i - ys,
540 t = q == null ? nbi() : q;
541 y.dlShiftTo(j, t);
542
543 if (r.compareTo(t) >= 0) {
544 r[r.t++] = 1;
545 r.subTo(t, r);
546 }
547
548 BigInteger.ONE.dlShiftTo(ys, t);
549 t.subTo(y, y); // "negative" y so we can replace sub with am later
550
551 while (y.t < ys) {
552 y[y.t++] = 0;
553 }
554
555 while (--j >= 0) {
556 // Estimate quotient digit
557 var qd = r[--i] == y0 ? this.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2);
558
559 if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) {
560 // Try it out
561 y.dlShiftTo(j, t);
562 r.subTo(t, r);
563
564 while (r[i] < --qd) {
565 r.subTo(t, r);
566 }
567 }
568 }
569
570 if (q != null) {
571 r.drShiftTo(ys, q);
572 if (ts != ms) BigInteger.ZERO.subTo(q, q);
573 }
574
575 r.t = ys;
576 r.clamp();
577 if (nsh > 0) r.rShiftTo(nsh, r); // Denormalize remainder
578
579 if (ts < 0) BigInteger.ZERO.subTo(r, r);
580} // (public) this mod a
581
582
583function bnMod(a) {
584 var r = nbi();
585 this.abs().divRemTo(a, null, r);
586 if (this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r, r);
587 return r;
588} // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
589// justification:
590// xy == 1 (mod m)
591// xy = 1+km
592// xy(2-xy) = (1+km)(1-km)
593// x[y(2-xy)] = 1-k^2m^2
594// x[y(2-xy)] == 1 (mod m^2)
595// if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
596// should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
597// JS multiply "overflows" differently from C/C++, so care is needed here.
598
599
600function bnpInvDigit() {
601 if (this.t < 1) return 0;
602 var x = this[0];
603 if ((x & 1) == 0) return 0;
604 var y = x & 3; // y == 1/x mod 2^2
605
606 y = y * (2 - (x & 0xf) * y) & 0xf; // y == 1/x mod 2^4
607
608 y = y * (2 - (x & 0xff) * y) & 0xff; // y == 1/x mod 2^8
609
610 y = y * (2 - ((x & 0xffff) * y & 0xffff)) & 0xffff; // y == 1/x mod 2^16
611 // last step - calculate inverse mod DV directly;
612 // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
613
614 y = y * (2 - x * y % this.DV) % this.DV; // y == 1/x mod 2^dbits
615 // we really want the negative inverse, and -DV < y < DV
616
617 return y > 0 ? this.DV - y : -y;
618}
619
620function bnEquals(a) {
621 return this.compareTo(a) == 0;
622} // (protected) r = this + a
623
624
625function bnpAddTo(a, r) {
626 var i = 0,
627 c = 0,
628 m = Math.min(a.t, this.t);
629
630 while (i < m) {
631 c += this[i] + a[i];
632 r[i++] = c & this.DM;
633 c >>= this.DB;
634 }
635
636 if (a.t < this.t) {
637 c += a.s;
638
639 while (i < this.t) {
640 c += this[i];
641 r[i++] = c & this.DM;
642 c >>= this.DB;
643 }
644
645 c += this.s;
646 } else {
647 c += this.s;
648
649 while (i < a.t) {
650 c += a[i];
651 r[i++] = c & this.DM;
652 c >>= this.DB;
653 }
654
655 c += a.s;
656 }
657
658 r.s = c < 0 ? -1 : 0;
659 if (c > 0) r[i++] = c;else if (c < -1) r[i++] = this.DV + c;
660 r.t = i;
661 r.clamp();
662} // (public) this + a
663
664
665function bnAdd(a) {
666 var r = nbi();
667 this.addTo(a, r);
668 return r;
669} // (public) this - a
670
671
672function bnSubtract(a) {
673 var r = nbi();
674 this.subTo(a, r);
675 return r;
676} // (public) this * a
677
678
679function bnMultiply(a) {
680 var r = nbi();
681 this.multiplyTo(a, r);
682 return r;
683} // (public) this / a
684
685
686function bnDivide(a) {
687 var r = nbi();
688 this.divRemTo(a, r, null);
689 return r;
690} // Montgomery reduction
691
692
693function Montgomery(m) {
694 this.m = m;
695 this.mp = m.invDigit();
696 this.mpl = this.mp & 0x7fff;
697 this.mph = this.mp >> 15;
698 this.um = (1 << m.DB - 15) - 1;
699 this.mt2 = 2 * m.t;
700} // xR mod m
701
702
703function montConvert(x) {
704 var r = nbi();
705 x.abs().dlShiftTo(this.m.t, r);
706 r.divRemTo(this.m, null, r);
707 if (x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r, r);
708 return r;
709} // x/R mod m
710
711
712function montRevert(x) {
713 var r = nbi();
714 x.copyTo(r);
715 this.reduce(r);
716 return r;
717} // x = x/R mod m (HAC 14.32)
718
719
720function montReduce(x) {
721 while (x.t <= this.mt2) {
722 // pad x so am has enough room later
723 x[x.t++] = 0;
724 }
725
726 for (var i = 0; i < this.m.t; ++i) {
727 // faster way of calculating u0 = x[i]*mp mod DV
728 var j = x[i] & 0x7fff;
729 var u0 = j * this.mpl + ((j * this.mph + (x[i] >> 15) * this.mpl & this.um) << 15) & x.DM; // use am to combine the multiply-shift-add into one call
730
731 j = i + this.m.t;
732 x[j] += this.m.am(0, u0, x, i, 0, this.m.t); // propagate carry
733
734 while (x[j] >= x.DV) {
735 x[j] -= x.DV;
736 x[++j]++;
737 }
738 }
739
740 x.clamp();
741 x.drShiftTo(this.m.t, x);
742 if (x.compareTo(this.m) >= 0) x.subTo(this.m, x);
743} // r = "x^2/R mod m"; x != r
744
745
746function montSqrTo(x, r) {
747 x.squareTo(r);
748 this.reduce(r);
749} // r = "xy/R mod m"; x,y != r
750
751
752function montMulTo(x, y, r) {
753 x.multiplyTo(y, r);
754 this.reduce(r);
755}
756
757Montgomery.prototype.convert = montConvert;
758Montgomery.prototype.revert = montRevert;
759Montgomery.prototype.reduce = montReduce;
760Montgomery.prototype.mulTo = montMulTo;
761Montgomery.prototype.sqrTo = montSqrTo; // (public) this^e % m (HAC 14.85)
762
763function bnModPow(e, m, callback) {
764 var i = e.bitLength(),
765 k,
766 r = nbv(1),
767 z = new Montgomery(m);
768 if (i <= 0) return r;else if (i < 18) k = 1;else if (i < 48) k = 3;else if (i < 144) k = 4;else if (i < 768) k = 5;else k = 6; // precomputation
769
770 var g = new Array(),
771 n = 3,
772 k1 = k - 1,
773 km = (1 << k) - 1;
774 g[1] = z.convert(this);
775
776 if (k > 1) {
777 var g2 = nbi();
778 z.sqrTo(g[1], g2);
779
780 while (n <= km) {
781 g[n] = nbi();
782 z.mulTo(g2, g[n - 2], g[n]);
783 n += 2;
784 }
785 }
786
787 var j = e.t - 1,
788 w,
789 is1 = true,
790 r2 = nbi(),
791 t;
792 i = nbits(e[j]) - 1;
793
794 while (j >= 0) {
795 if (i >= k1) w = e[j] >> i - k1 & km;else {
796 w = (e[j] & (1 << i + 1) - 1) << k1 - i;
797 if (j > 0) w |= e[j - 1] >> this.DB + i - k1;
798 }
799 n = k;
800
801 while ((w & 1) == 0) {
802 w >>= 1;
803 --n;
804 }
805
806 if ((i -= n) < 0) {
807 i += this.DB;
808 --j;
809 }
810
811 if (is1) {
812 // ret == 1, don't bother squaring or multiplying it
813 g[w].copyTo(r);
814 is1 = false;
815 } else {
816 while (n > 1) {
817 z.sqrTo(r, r2);
818 z.sqrTo(r2, r);
819 n -= 2;
820 }
821
822 if (n > 0) z.sqrTo(r, r2);else {
823 t = r;
824 r = r2;
825 r2 = t;
826 }
827 z.mulTo(r2, g[w], r);
828 }
829
830 while (j >= 0 && (e[j] & 1 << i) == 0) {
831 z.sqrTo(r, r2);
832 t = r;
833 r = r2;
834 r2 = t;
835
836 if (--i < 0) {
837 i = this.DB - 1;
838 --j;
839 }
840 }
841 }
842
843 var result = z.revert(r);
844 callback(null, result);
845 return result;
846} // protected
847
848
849BigInteger.prototype.copyTo = bnpCopyTo;
850BigInteger.prototype.fromInt = bnpFromInt;
851BigInteger.prototype.fromString = bnpFromString;
852BigInteger.prototype.clamp = bnpClamp;
853BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
854BigInteger.prototype.drShiftTo = bnpDRShiftTo;
855BigInteger.prototype.lShiftTo = bnpLShiftTo;
856BigInteger.prototype.rShiftTo = bnpRShiftTo;
857BigInteger.prototype.subTo = bnpSubTo;
858BigInteger.prototype.multiplyTo = bnpMultiplyTo;
859BigInteger.prototype.squareTo = bnpSquareTo;
860BigInteger.prototype.divRemTo = bnpDivRemTo;
861BigInteger.prototype.invDigit = bnpInvDigit;
862BigInteger.prototype.addTo = bnpAddTo; // public
863
864BigInteger.prototype.toString = bnToString;
865BigInteger.prototype.negate = bnNegate;
866BigInteger.prototype.abs = bnAbs;
867BigInteger.prototype.compareTo = bnCompareTo;
868BigInteger.prototype.bitLength = bnBitLength;
869BigInteger.prototype.mod = bnMod;
870BigInteger.prototype.equals = bnEquals;
871BigInteger.prototype.add = bnAdd;
872BigInteger.prototype.subtract = bnSubtract;
873BigInteger.prototype.multiply = bnMultiply;
874BigInteger.prototype.divide = bnDivide;
875BigInteger.prototype.modPow = bnModPow; // "constants"
876
877BigInteger.ZERO = nbv(0);
878BigInteger.ONE = nbv(1);
\No newline at end of file