| OLD | NEW | 
| (Empty) |  | 
 |    1 /* | 
 |    2  * Copyright (c) 2003-2005  Tom Wu | 
 |    3  * All Rights Reserved. | 
 |    4  * | 
 |    5  * Permission is hereby granted, free of charge, to any person obtaining | 
 |    6  * a copy of this software and associated documentation files (the | 
 |    7  * "Software"), to deal in the Software without restriction, including | 
 |    8  * without limitation the rights to use, copy, modify, merge, publish, | 
 |    9  * distribute, sublicense, and/or sell copies of the Software, and to | 
 |   10  * permit persons to whom the Software is furnished to do so, subject to | 
 |   11  * the following conditions: | 
 |   12  * | 
 |   13  * The above copyright notice and this permission notice shall be | 
 |   14  * included in all copies or substantial portions of the Software. | 
 |   15  * | 
 |   16  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, | 
 |   17  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY | 
 |   18  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. | 
 |   19  * | 
 |   20  * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL, | 
 |   21  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER | 
 |   22  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF | 
 |   23  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT | 
 |   24  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 
 |   25  * | 
 |   26  * In addition, the following condition applies: | 
 |   27  * | 
 |   28  * All redistributions must retain an intact copy of this copyright notice | 
 |   29  * and disclaimer. | 
 |   30  */ | 
 |   31  | 
 |   32 // Basic JavaScript BN library - subset useful for RSA encryption. | 
 |   33  | 
 |   34 // Bits per digit | 
 |   35 var dbits; | 
 |   36  | 
 |   37 // JavaScript engine analysis | 
 |   38 var canary = 0xdeadbeefcafe; | 
 |   39 var j_lm = ((canary&0xffffff)==0xefcafe); | 
 |   40  | 
 |   41 // (public) Constructor | 
 |   42 function BigInteger(a,b,c) { | 
 |   43   if(a != null) | 
 |   44     if("number" == typeof a) this.fromNumber(a,b,c); | 
 |   45     else if(b == null && "string" != typeof a) this.fromString(a,256); | 
 |   46     else this.fromString(a,b); | 
 |   47 } | 
 |   48  | 
 |   49 // return new, unset BigInteger | 
 |   50 function nbi() { return new BigInteger(null); } | 
 |   51  | 
 |   52 // am: Compute w_j += (x*this_i), propagate carries, | 
 |   53 // c is initial carry, returns final carry. | 
 |   54 // c < 3*dvalue, x < 2*dvalue, this_i < dvalue | 
 |   55 // We need to select the fastest one that works in this environment. | 
 |   56  | 
 |   57 // am1: use a single mult and divide to get the high bits, | 
 |   58 // max digit bits should be 26 because | 
 |   59 // max internal value = 2*dvalue^2-2*dvalue (< 2^53) | 
 |   60 function am1(i,x,w,j,c,n) { | 
 |   61   while(--n >= 0) { | 
 |   62     var v = x*this[i++]+w[j]+c; | 
 |   63     c = Math.floor(v/0x4000000); | 
 |   64     w[j++] = v&0x3ffffff; | 
 |   65   } | 
 |   66   return c; | 
 |   67 } | 
 |   68 // am2 avoids a big mult-and-extract completely. | 
 |   69 // Max digit bits should be <= 30 because we do bitwise ops | 
 |   70 // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31) | 
 |   71 function am2(i,x,w,j,c,n) { | 
 |   72   var xl = x&0x7fff, xh = x>>15; | 
 |   73   while(--n >= 0) { | 
 |   74     var l = this[i]&0x7fff; | 
 |   75     var h = this[i++]>>15; | 
 |   76     var m = xh*l+h*xl; | 
 |   77     l = xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff); | 
 |   78     c = (l>>>30)+(m>>>15)+xh*h+(c>>>30); | 
 |   79     w[j++] = l&0x3fffffff; | 
 |   80   } | 
 |   81   return c; | 
 |   82 } | 
 |   83 // Alternately, set max digit bits to 28 since some | 
 |   84 // browsers slow down when dealing with 32-bit numbers. | 
 |   85 function am3(i,x,w,j,c,n) { | 
 |   86   var xl = x&0x3fff, xh = x>>14; | 
 |   87   while(--n >= 0) { | 
 |   88     var l = this[i]&0x3fff; | 
 |   89     var h = this[i++]>>14; | 
 |   90     var m = xh*l+h*xl; | 
 |   91     l = xl*l+((m&0x3fff)<<14)+w[j]+c; | 
 |   92     c = (l>>28)+(m>>14)+xh*h; | 
 |   93     w[j++] = l&0xfffffff; | 
 |   94   } | 
 |   95   return c; | 
 |   96 } | 
 |   97 if(j_lm && (navigator.appName == "Microsoft Internet Explorer")) { | 
 |   98   BigInteger.prototype.am = am2; | 
 |   99   dbits = 30; | 
 |  100 } | 
 |  101 else if(j_lm && (navigator.appName != "Netscape")) { | 
 |  102   BigInteger.prototype.am = am1; | 
 |  103   dbits = 26; | 
 |  104 } | 
 |  105 else { // Mozilla/Netscape seems to prefer am3 | 
 |  106   BigInteger.prototype.am = am3; | 
 |  107   dbits = 28; | 
 |  108 } | 
 |  109  | 
 |  110 BigInteger.prototype.DB = dbits; | 
 |  111 BigInteger.prototype.DM = ((1<<dbits)-1); | 
 |  112 BigInteger.prototype.DV = (1<<dbits); | 
 |  113  | 
 |  114 var BI_FP = 52; | 
 |  115 BigInteger.prototype.FV = Math.pow(2,BI_FP); | 
 |  116 BigInteger.prototype.F1 = BI_FP-dbits; | 
 |  117 BigInteger.prototype.F2 = 2*dbits-BI_FP; | 
 |  118  | 
 |  119 // Digit conversions | 
 |  120 var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz"; | 
 |  121 var BI_RC = new Array(); | 
 |  122 var rr,vv; | 
 |  123 rr = "0".charCodeAt(0); | 
 |  124 for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv; | 
 |  125 rr = "a".charCodeAt(0); | 
 |  126 for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv; | 
 |  127 rr = "A".charCodeAt(0); | 
 |  128 for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv; | 
 |  129  | 
 |  130 function int2char(n) { return BI_RM.charAt(n); } | 
 |  131 function intAt(s,i) { | 
 |  132   var c = BI_RC[s.charCodeAt(i)]; | 
 |  133   return (c==null)?-1:c; | 
 |  134 } | 
 |  135  | 
 |  136 // (protected) copy this to r | 
 |  137 function bnpCopyTo(r) { | 
 |  138   for(var i = this.t-1; i >= 0; --i) r[i] = this[i]; | 
 |  139   r.t = this.t; | 
 |  140   r.s = this.s; | 
 |  141 } | 
 |  142  | 
 |  143 // (protected) set from integer value x, -DV <= x < DV | 
 |  144 function bnpFromInt(x) { | 
 |  145   this.t = 1; | 
 |  146   this.s = (x<0)?-1:0; | 
 |  147   if(x > 0) this[0] = x; | 
 |  148   else if(x < -1) this[0] = x+DV; | 
 |  149   else this.t = 0; | 
 |  150 } | 
 |  151  | 
 |  152 // return bigint initialized to value | 
 |  153 function nbv(i) { var r = nbi(); r.fromInt(i); return r; } | 
 |  154  | 
 |  155 // (protected) set from string and radix | 
 |  156 function bnpFromString(s,b) { | 
 |  157   var k; | 
 |  158   if(b == 16) k = 4; | 
 |  159   else if(b == 8) k = 3; | 
 |  160   else if(b == 256) k = 8; // byte array | 
 |  161   else if(b == 2) k = 1; | 
 |  162   else if(b == 32) k = 5; | 
 |  163   else if(b == 4) k = 2; | 
 |  164   else { this.fromRadix(s,b); return; } | 
 |  165   this.t = 0; | 
 |  166   this.s = 0; | 
 |  167   var i = s.length, mi = false, sh = 0; | 
 |  168   while(--i >= 0) { | 
 |  169     var x = (k==8)?s.charCodeAt(i)&0xff:intAt(s,i);   /** MODIFIED **/ | 
 |  170     if(x < 0) { | 
 |  171       if(s.charAt(i) == "-") mi = true; | 
 |  172       continue; | 
 |  173     } | 
 |  174     mi = false; | 
 |  175     if(sh == 0) | 
 |  176       this[this.t++] = x; | 
 |  177     else if(sh+k > this.DB) { | 
 |  178       this[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh; | 
 |  179       this[this.t++] = (x>>(this.DB-sh)); | 
 |  180     } | 
 |  181     else | 
 |  182       this[this.t-1] |= x<<sh; | 
 |  183     sh += k; | 
 |  184     if(sh >= this.DB) sh -= this.DB; | 
 |  185   } | 
 |  186   if(k == 8 && (s[0]&0x80) != 0) { | 
 |  187     this.s = -1; | 
 |  188     if(sh > 0) this[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh; | 
 |  189   } | 
 |  190   this.clamp(); | 
 |  191   if(mi) BigInteger.ZERO.subTo(this,this); | 
 |  192 } | 
 |  193  | 
 |  194 // (protected) clamp off excess high words | 
 |  195 function bnpClamp() { | 
 |  196   var c = this.s&this.DM; | 
 |  197   while(this.t > 0 && this[this.t-1] == c) --this.t; | 
 |  198 } | 
 |  199  | 
 |  200 // (public) return string representation in given radix | 
 |  201 function bnToString(b) { | 
 |  202   if(this.s < 0) return "-"+this.negate().toString(b); | 
 |  203   var k; | 
 |  204   if(b == 16) k = 4; | 
 |  205   else if(b == 8) k = 3; | 
 |  206   else if(b == 256) k = 8; // byte array      /** MODIFIED **/ | 
 |  207   else if(b == 2) k = 1; | 
 |  208   else if(b == 32) k = 5; | 
 |  209   else if(b == 4) k = 2; | 
 |  210   else return this.toRadix(b); | 
 |  211   var km = (1<<k)-1, d, m = false, r = "", i = this.t; | 
 |  212   var p = this.DB-(i*this.DB)%k; | 
 |  213   if(i-- > 0) { | 
 |  214     if(p < this.DB && (d = this[i]>>p) > 0) { m = true; r = (k==8)?String.fromCh
     arCode(d):int2char(d); }   /** MODIFIED **/ | 
 |  215     while(i >= 0) { | 
 |  216       if(p < k) { | 
 |  217         d = (this[i]&((1<<p)-1))<<(k-p); | 
 |  218         d |= this[--i]>>(p+=this.DB-k); | 
 |  219       } | 
 |  220       else { | 
 |  221         d = (this[i]>>(p-=k))&km; | 
 |  222         if(p <= 0) { p += this.DB; --i; } | 
 |  223       } | 
 |  224       if(d > 0) m = true; | 
 |  225       if(m) r += (k==8)?String.fromCharCode(d):int2char(d);    /** MODIFIED **/ | 
 |  226     } | 
 |  227   } | 
 |  228   return m?r:"0"; | 
 |  229 } | 
 |  230  | 
 |  231 // (public) -this | 
 |  232 function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; } | 
 |  233  | 
 |  234 // (public) |this| | 
 |  235 function bnAbs() { return (this.s<0)?this.negate():this; } | 
 |  236  | 
 |  237 // (public) return + if this > a, - if this < a, 0 if equal | 
 |  238 function bnCompareTo(a) { | 
 |  239   var r = this.s-a.s; | 
 |  240   if(r != 0) return r; | 
 |  241   var i = this.t; | 
 |  242   r = i-a.t; | 
 |  243   if(r != 0) return r; | 
 |  244   while(--i >= 0) if((r=this[i]-a[i]) != 0) return r; | 
 |  245   return 0; | 
 |  246 } | 
 |  247  | 
 |  248 // returns bit length of the integer x | 
 |  249 function nbits(x) { | 
 |  250   var r = 1, t; | 
 |  251   if((t=x>>>16) != 0) { x = t; r += 16; } | 
 |  252   if((t=x>>8) != 0) { x = t; r += 8; } | 
 |  253   if((t=x>>4) != 0) { x = t; r += 4; } | 
 |  254   if((t=x>>2) != 0) { x = t; r += 2; } | 
 |  255   if((t=x>>1) != 0) { x = t; r += 1; } | 
 |  256   return r; | 
 |  257 } | 
 |  258  | 
 |  259 // (public) return the number of bits in "this" | 
 |  260 function bnBitLength() { | 
 |  261   if(this.t <= 0) return 0; | 
 |  262   return this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM)); | 
 |  263 } | 
 |  264  | 
 |  265 // (protected) r = this << n*DB | 
 |  266 function bnpDLShiftTo(n,r) { | 
 |  267   var i; | 
 |  268   for(i = this.t-1; i >= 0; --i) r[i+n] = this[i]; | 
 |  269   for(i = n-1; i >= 0; --i) r[i] = 0; | 
 |  270   r.t = this.t+n; | 
 |  271   r.s = this.s; | 
 |  272 } | 
 |  273  | 
 |  274 // (protected) r = this >> n*DB | 
 |  275 function bnpDRShiftTo(n,r) { | 
 |  276   for(var i = n; i < this.t; ++i) r[i-n] = this[i]; | 
 |  277   r.t = Math.max(this.t-n,0); | 
 |  278   r.s = this.s; | 
 |  279 } | 
 |  280  | 
 |  281 // (protected) r = this << n | 
 |  282 function bnpLShiftTo(n,r) { | 
 |  283   var bs = n%this.DB; | 
 |  284   var cbs = this.DB-bs; | 
 |  285   var bm = (1<<cbs)-1; | 
 |  286   var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i; | 
 |  287   for(i = this.t-1; i >= 0; --i) { | 
 |  288     r[i+ds+1] = (this[i]>>cbs)|c; | 
 |  289     c = (this[i]&bm)<<bs; | 
 |  290   } | 
 |  291   for(i = ds-1; i >= 0; --i) r[i] = 0; | 
 |  292   r[ds] = c; | 
 |  293   r.t = this.t+ds+1; | 
 |  294   r.s = this.s; | 
 |  295   r.clamp(); | 
 |  296 } | 
 |  297  | 
 |  298 // (protected) r = this >> n | 
 |  299 function bnpRShiftTo(n,r) { | 
 |  300   r.s = this.s; | 
 |  301   var ds = Math.floor(n/this.DB); | 
 |  302   if(ds >= this.t) { r.t = 0; return; } | 
 |  303   var bs = n%this.DB; | 
 |  304   var cbs = this.DB-bs; | 
 |  305   var bm = (1<<bs)-1; | 
 |  306   r[0] = this[ds]>>bs; | 
 |  307   for(var i = ds+1; i < this.t; ++i) { | 
 |  308     r[i-ds-1] |= (this[i]&bm)<<cbs; | 
 |  309     r[i-ds] = this[i]>>bs; | 
 |  310   } | 
 |  311   if(bs > 0) r[this.t-ds-1] |= (this.s&bm)<<cbs; | 
 |  312   r.t = this.t-ds; | 
 |  313   r.clamp(); | 
 |  314 } | 
 |  315  | 
 |  316 // (protected) r = this - a | 
 |  317 function bnpSubTo(a,r) { | 
 |  318   var i = 0, c = 0, m = Math.min(a.t,this.t); | 
 |  319   while(i < m) { | 
 |  320     c += this[i]-a[i]; | 
 |  321     r[i++] = c&this.DM; | 
 |  322     c >>= this.DB; | 
 |  323   } | 
 |  324   if(a.t < this.t) { | 
 |  325     c -= a.s; | 
 |  326     while(i < this.t) { | 
 |  327       c += this[i]; | 
 |  328       r[i++] = c&this.DM; | 
 |  329       c >>= this.DB; | 
 |  330     } | 
 |  331     c += this.s; | 
 |  332   } | 
 |  333   else { | 
 |  334     c += this.s; | 
 |  335     while(i < a.t) { | 
 |  336       c -= a[i]; | 
 |  337       r[i++] = c&this.DM; | 
 |  338       c >>= this.DB; | 
 |  339     } | 
 |  340     c -= a.s; | 
 |  341   } | 
 |  342   r.s = (c<0)?-1:0; | 
 |  343   if(c < -1) r[i++] = this.DV+c; | 
 |  344   else if(c > 0) r[i++] = c; | 
 |  345   r.t = i; | 
 |  346   r.clamp(); | 
 |  347 } | 
 |  348  | 
 |  349 // (protected) r = this * a, r != this,a (HAC 14.12) | 
 |  350 // "this" should be the larger one if appropriate. | 
 |  351 function bnpMultiplyTo(a,r) { | 
 |  352   var x = this.abs(), y = a.abs(); | 
 |  353   var i = x.t; | 
 |  354   r.t = i+y.t; | 
 |  355   while(--i >= 0) r[i] = 0; | 
 |  356   for(i = 0; i < y.t; ++i) r[i+x.t] = x.am(0,y[i],r,i,0,x.t); | 
 |  357   r.s = 0; | 
 |  358   r.clamp(); | 
 |  359   if(this.s != a.s) BigInteger.ZERO.subTo(r,r); | 
 |  360 } | 
 |  361  | 
 |  362 // (protected) r = this^2, r != this (HAC 14.16) | 
 |  363 function bnpSquareTo(r) { | 
 |  364   var x = this.abs(); | 
 |  365   var i = r.t = 2*x.t; | 
 |  366   while(--i >= 0) r[i] = 0; | 
 |  367   for(i = 0; i < x.t-1; ++i) { | 
 |  368     var c = x.am(i,x[i],r,2*i,0,1); | 
 |  369     if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1)) >= x.DV) { | 
 |  370       r[i+x.t] -= x.DV; | 
 |  371       r[i+x.t+1] = 1; | 
 |  372     } | 
 |  373   } | 
 |  374   if(r.t > 0) r[r.t-1] += x.am(i,x[i],r,2*i,0,1); | 
 |  375   r.s = 0; | 
 |  376   r.clamp(); | 
 |  377 } | 
 |  378  | 
 |  379 // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20) | 
 |  380 // r != q, this != m.  q or r may be null. | 
 |  381 function bnpDivRemTo(m,q,r) { | 
 |  382   var pm = m.abs(); | 
 |  383   if(pm.t <= 0) return; | 
 |  384   var pt = this.abs(); | 
 |  385   if(pt.t < pm.t) { | 
 |  386     if(q != null) q.fromInt(0); | 
 |  387     if(r != null) this.copyTo(r); | 
 |  388     return; | 
 |  389   } | 
 |  390   if(r == null) r = nbi(); | 
 |  391   var y = nbi(), ts = this.s, ms = m.s; | 
 |  392   var nsh = this.DB-nbits(pm[pm.t-1]);  // normalize modulus | 
 |  393   if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); } | 
 |  394   else { pm.copyTo(y); pt.copyTo(r); } | 
 |  395   var ys = y.t; | 
 |  396   var y0 = y[ys-1]; | 
 |  397   if(y0 == 0) return; | 
 |  398   var yt = y0*(1<<this.F1)+((ys>1)?y[ys-2]>>this.F2:0); | 
 |  399   var d1 = this.FV/yt, d2 = (1<<this.F1)/yt, e = 1<<this.F2; | 
 |  400   var i = r.t, j = i-ys, t = (q==null)?nbi():q; | 
 |  401   y.dlShiftTo(j,t); | 
 |  402   if(r.compareTo(t) >= 0) { | 
 |  403     r[r.t++] = 1; | 
 |  404     r.subTo(t,r); | 
 |  405   } | 
 |  406   BigInteger.ONE.dlShiftTo(ys,t); | 
 |  407   t.subTo(y,y); // "negative" y so we can replace sub with am later | 
 |  408   while(y.t < ys) y[y.t++] = 0; | 
 |  409   while(--j >= 0) { | 
 |  410     // Estimate quotient digit | 
 |  411     var qd = (r[--i]==y0)?this.DM:Math.floor(r[i]*d1+(r[i-1]+e)*d2); | 
 |  412     if((r[i]+=y.am(0,qd,r,j,0,ys)) < qd) {      // Try it out | 
 |  413       y.dlShiftTo(j,t); | 
 |  414       r.subTo(t,r); | 
 |  415       while(r[i] < --qd) r.subTo(t,r); | 
 |  416     } | 
 |  417   } | 
 |  418   if(q != null) { | 
 |  419     r.drShiftTo(ys,q); | 
 |  420     if(ts != ms) BigInteger.ZERO.subTo(q,q); | 
 |  421   } | 
 |  422   r.t = ys; | 
 |  423   r.clamp(); | 
 |  424   if(nsh > 0) r.rShiftTo(nsh,r);        // Denormalize remainder | 
 |  425   if(ts < 0) BigInteger.ZERO.subTo(r,r); | 
 |  426 } | 
 |  427  | 
 |  428 // (public) this mod a | 
 |  429 function bnMod(a) { | 
 |  430   var r = nbi(); | 
 |  431   this.abs().divRemTo(a,null,r); | 
 |  432   if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r); | 
 |  433   return r; | 
 |  434 } | 
 |  435  | 
 |  436 // Modular reduction using "classic" algorithm | 
 |  437 function Classic(m) { this.m = m; } | 
 |  438 function cConvert(x) { | 
 |  439   if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m); | 
 |  440   else return x; | 
 |  441 } | 
 |  442 function cRevert(x) { return x; } | 
 |  443 function cReduce(x) { x.divRemTo(this.m,null,x); } | 
 |  444 function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } | 
 |  445 function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); } | 
 |  446  | 
 |  447 Classic.prototype.convert = cConvert; | 
 |  448 Classic.prototype.revert = cRevert; | 
 |  449 Classic.prototype.reduce = cReduce; | 
 |  450 Classic.prototype.mulTo = cMulTo; | 
 |  451 Classic.prototype.sqrTo = cSqrTo; | 
 |  452  | 
 |  453 // (protected) return "-1/this % 2^DB"; useful for Mont. reduction | 
 |  454 // justification: | 
 |  455 //         xy == 1 (mod m) | 
 |  456 //         xy =  1+km | 
 |  457 //   xy(2-xy) = (1+km)(1-km) | 
 |  458 // x[y(2-xy)] = 1-k^2m^2 | 
 |  459 // x[y(2-xy)] == 1 (mod m^2) | 
 |  460 // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2 | 
 |  461 // should reduce x and y(2-xy) by m^2 at each step to keep size bounded. | 
 |  462 // JS multiply "overflows" differently from C/C++, so care is needed here. | 
 |  463 function bnpInvDigit() { | 
 |  464   if(this.t < 1) return 0; | 
 |  465   var x = this[0]; | 
 |  466   if((x&1) == 0) return 0; | 
 |  467   var y = x&3;          // y == 1/x mod 2^2 | 
 |  468   y = (y*(2-(x&0xf)*y))&0xf;    // y == 1/x mod 2^4 | 
 |  469   y = (y*(2-(x&0xff)*y))&0xff;  // y == 1/x mod 2^8 | 
 |  470   y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff;   // y == 1/x mod 2^16 | 
 |  471   // last step - calculate inverse mod DV directly; | 
 |  472   // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints | 
 |  473   y = (y*(2-x*y%this.DV))%this.DV;              // y == 1/x mod 2^dbits | 
 |  474   // we really want the negative inverse, and -DV < y < DV | 
 |  475   return (y>0)?this.DV-y:-y; | 
 |  476 } | 
 |  477  | 
 |  478 // Montgomery reduction | 
 |  479 function Montgomery(m) { | 
 |  480   this.m = m; | 
 |  481   this.mp = m.invDigit(); | 
 |  482   this.mpl = this.mp&0x7fff; | 
 |  483   this.mph = this.mp>>15; | 
 |  484   this.um = (1<<(m.DB-15))-1; | 
 |  485   this.mt2 = 2*m.t; | 
 |  486 } | 
 |  487  | 
 |  488 // xR mod m | 
 |  489 function montConvert(x) { | 
 |  490   var r = nbi(); | 
 |  491   x.abs().dlShiftTo(this.m.t,r); | 
 |  492   r.divRemTo(this.m,null,r); | 
 |  493   if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r); | 
 |  494   return r; | 
 |  495 } | 
 |  496  | 
 |  497 // x/R mod m | 
 |  498 function montRevert(x) { | 
 |  499   var r = nbi(); | 
 |  500   x.copyTo(r); | 
 |  501   this.reduce(r); | 
 |  502   return r; | 
 |  503 } | 
 |  504  | 
 |  505 // x = x/R mod m (HAC 14.32) | 
 |  506 function montReduce(x) { | 
 |  507   while(x.t <= this.mt2)        // pad x so am has enough room later | 
 |  508     x[x.t++] = 0; | 
 |  509   for(var i = 0; i < this.m.t; ++i) { | 
 |  510     // faster way of calculating u0 = x[i]*mp mod DV | 
 |  511     var j = x[i]&0x7fff; | 
 |  512     var u0 = (j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.DM; | 
 |  513     // use am to combine the multiply-shift-add into one call | 
 |  514     j = i+this.m.t; | 
 |  515     x[j] += this.m.am(0,u0,x,i,0,this.m.t); | 
 |  516     // propagate carry | 
 |  517     while(x[j] >= x.DV) { x[j] -= x.DV; x[++j]++; } | 
 |  518   } | 
 |  519   x.clamp(); | 
 |  520   x.drShiftTo(this.m.t,x); | 
 |  521   if(x.compareTo(this.m) >= 0) x.subTo(this.m,x); | 
 |  522 } | 
 |  523  | 
 |  524 // r = "x^2/R mod m"; x != r | 
 |  525 function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); } | 
 |  526  | 
 |  527 // r = "xy/R mod m"; x,y != r | 
 |  528 function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } | 
 |  529  | 
 |  530 Montgomery.prototype.convert = montConvert; | 
 |  531 Montgomery.prototype.revert = montRevert; | 
 |  532 Montgomery.prototype.reduce = montReduce; | 
 |  533 Montgomery.prototype.mulTo = montMulTo; | 
 |  534 Montgomery.prototype.sqrTo = montSqrTo; | 
 |  535  | 
 |  536 // (protected) true iff this is even | 
 |  537 function bnpIsEven() { return ((this.t>0)?(this[0]&1):this.s) == 0; } | 
 |  538  | 
 |  539 // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79) | 
 |  540 function bnpExp(e,z) { | 
 |  541   if(e > 0xffffffff || e < 1) return BigInteger.ONE; | 
 |  542   var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1; | 
 |  543   g.copyTo(r); | 
 |  544   while(--i >= 0) { | 
 |  545     z.sqrTo(r,r2); | 
 |  546     if((e&(1<<i)) > 0) z.mulTo(r2,g,r); | 
 |  547     else { var t = r; r = r2; r2 = t; } | 
 |  548   } | 
 |  549   return z.revert(r); | 
 |  550 } | 
 |  551  | 
 |  552 // (public) this^e % m, 0 <= e < 2^32 | 
 |  553 function bnModPowInt(e,m) { | 
 |  554   var z; | 
 |  555   if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m); | 
 |  556   return this.exp(e,z); | 
 |  557 } | 
 |  558  | 
 |  559 // protected | 
 |  560 BigInteger.prototype.copyTo = bnpCopyTo; | 
 |  561 BigInteger.prototype.fromInt = bnpFromInt; | 
 |  562 BigInteger.prototype.fromString = bnpFromString; | 
 |  563 BigInteger.prototype.clamp = bnpClamp; | 
 |  564 BigInteger.prototype.dlShiftTo = bnpDLShiftTo; | 
 |  565 BigInteger.prototype.drShiftTo = bnpDRShiftTo; | 
 |  566 BigInteger.prototype.lShiftTo = bnpLShiftTo; | 
 |  567 BigInteger.prototype.rShiftTo = bnpRShiftTo; | 
 |  568 BigInteger.prototype.subTo = bnpSubTo; | 
 |  569 BigInteger.prototype.multiplyTo = bnpMultiplyTo; | 
 |  570 BigInteger.prototype.squareTo = bnpSquareTo; | 
 |  571 BigInteger.prototype.divRemTo = bnpDivRemTo; | 
 |  572 BigInteger.prototype.invDigit = bnpInvDigit; | 
 |  573 BigInteger.prototype.isEven = bnpIsEven; | 
 |  574 BigInteger.prototype.exp = bnpExp; | 
 |  575  | 
 |  576 // public | 
 |  577 BigInteger.prototype.toString = bnToString; | 
 |  578 BigInteger.prototype.negate = bnNegate; | 
 |  579 BigInteger.prototype.abs = bnAbs; | 
 |  580 BigInteger.prototype.compareTo = bnCompareTo; | 
 |  581 BigInteger.prototype.bitLength = bnBitLength; | 
 |  582 BigInteger.prototype.mod = bnMod; | 
 |  583 BigInteger.prototype.modPowInt = bnModPowInt; | 
 |  584  | 
 |  585 // "constants" | 
 |  586 BigInteger.ZERO = nbv(0); | 
 |  587 BigInteger.ONE = nbv(1); | 
| OLD | NEW |