js/src/jit-test/tests/v8-v5/check-crypto.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // |jit-test| slow;
     2 // This test times out in rooting analyis builds, and so is marked slow so that
     3 // it's not run as part of the rooting analysis tests on tinderbox.
     5 /*
     6  * Copyright (c) 2003-2005  Tom Wu
     7  * All Rights Reserved.
     8  *
     9  * Permission is hereby granted, free of charge, to any person obtaining
    10  * a copy of this software and associated documentation files (the
    11  * "Software"), to deal in the Software without restriction, including
    12  * without limitation the rights to use, copy, modify, merge, publish,
    13  * distribute, sublicense, and/or sell copies of the Software, and to
    14  * permit persons to whom the Software is furnished to do so, subject to
    15  * the following conditions:
    16  *
    17  * The above copyright notice and this permission notice shall be
    18  * included in all copies or substantial portions of the Software.
    19  *
    20  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
    21  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
    22  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
    23  *
    24  * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
    25  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
    26  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
    27  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
    28  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    29  *
    30  * In addition, the following condition applies:
    31  *
    32  * All redistributions must retain an intact copy of this copyright notice
    33  * and disclaimer.
    34  */
    37 // The code has been adapted for use as a benchmark by Google.
    38 //var Crypto = new BenchmarkSuite('Crypto', 203037, [
    39 //  new Benchmark("Encrypt", encrypt),
    40 //  new Benchmark("Decrypt", decrypt)
    41 //]);
    44 // Basic JavaScript BN library - subset useful for RSA encryption.
    46 // Bits per digit
    47 var dbits;
    48 var BI_DB;
    49 var BI_DM;
    50 var BI_DV;
    52 var BI_FP;
    53 var BI_FV;
    54 var BI_F1;
    55 var BI_F2;
    57 // JavaScript engine analysis
    58 var canary = 0xdeadbeefcafe;
    59 var j_lm = ((canary&0xffffff)==0xefcafe);
    61 // This is the best random number generator available to mankind ;)
    62 var MyMath = {
    63     curr: 0,
    64     random: function() {
    65         this.curr = this.curr + 1;
    66         return this.curr;
    67     },
    68 };
    71 // (public) Constructor
    72 function BigInteger(a,b,c) {
    73   this.array = new Array();
    74   if(a != null)
    75     if("number" == typeof a) this.fromNumber(a,b,c);
    76     else if(b == null && "string" != typeof a) this.fromString(a,256);
    77     else this.fromString(a,b);
    78 }
    80 // return new, unset BigInteger
    81 function nbi() { return new BigInteger(null); }
    83 // am: Compute w_j += (x*this_i), propagate carries,
    84 // c is initial carry, returns final carry.
    85 // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
    86 // We need to select the fastest one that works in this environment.
    88 // am1: use a single mult and divide to get the high bits,
    89 // max digit bits should be 26 because
    90 // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
    91 function am1(i,x,w,j,c,n) {
    92   var this_array = this.array;
    93   var w_array    = w.array;
    94   while(--n >= 0) {
    95     var v = x*this_array[i++]+w_array[j]+c;
    96     c = Math.floor(v/0x4000000);
    97     w_array[j++] = v&0x3ffffff;
    98   }
    99   return c;
   100 }
   102 // am2 avoids a big mult-and-extract completely.
   103 // Max digit bits should be <= 30 because we do bitwise ops
   104 // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
   105 function am2(i,x,w,j,c,n) {
   106   var this_array = this.array;
   107   var w_array    = w.array;
   108   var xl = x&0x7fff, xh = x>>15;
   109   while(--n >= 0) {
   110     var l = this_array[i]&0x7fff;
   111     var h = this_array[i++]>>15;
   112     var m = xh*l+h*xl;
   113     l = xl*l+((m&0x7fff)<<15)+w_array[j]+(c&0x3fffffff);
   114     c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
   115     w_array[j++] = l&0x3fffffff;
   116   }
   117   return c;
   118 }
   120 // Alternately, set max digit bits to 28 since some
   121 // browsers slow down when dealing with 32-bit numbers.
   122 function am3(i,x,w,j,c,n) {
   123   var this_array = this.array;
   124   var w_array    = w.array;
   126   var xl = x&0x3fff, xh = x>>14;
   127   while(--n >= 0) {
   128     var l = this_array[i]&0x3fff;
   129     var h = this_array[i++]>>14;
   130     var m = xh*l+h*xl;
   131     l = xl*l+((m&0x3fff)<<14)+w_array[j]+c;
   132     c = (l>>28)+(m>>14)+xh*h;
   133     w_array[j++] = l&0xfffffff;
   134   }
   135   return c;
   136 }
   138 // This is tailored to VMs with 2-bit tagging. It makes sure
   139 // that all the computations stay within the 29 bits available.
   140 function am4(i,x,w,j,c,n) {
   141   var this_array = this.array;
   142   var w_array    = w.array;
   144   var xl = x&0x1fff, xh = x>>13;
   145   while(--n >= 0) {
   146     var l = this_array[i]&0x1fff;
   147     var h = this_array[i++]>>13;
   148     var m = xh*l+h*xl;
   149     l = xl*l+((m&0x1fff)<<13)+w_array[j]+c;
   150     c = (l>>26)+(m>>13)+xh*h;
   151     w_array[j++] = l&0x3ffffff;
   152   }
   153   return c;
   154 }
   156 // am3/28 is best for SM, Rhino, but am4/26 is best for v8.
   157 // Kestrel (Opera 9.5) gets its best result with am4/26.
   158 // IE7 does 9% better with am3/28 than with am4/26.
   159 // Firefox (SM) gets 10% faster with am3/28 than with am4/26.
   161 setupEngine = function(fn, bits) {
   162   BigInteger.prototype.am = fn;
   163   dbits = bits;
   165   BI_DB = dbits;
   166   BI_DM = ((1<<dbits)-1);
   167   BI_DV = (1<<dbits);
   169   BI_FP = 52;
   170   BI_FV = Math.pow(2,BI_FP);
   171   BI_F1 = BI_FP-dbits;
   172   BI_F2 = 2*dbits-BI_FP;
   173 }
   176 // Digit conversions
   177 var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
   178 var BI_RC = new Array();
   179 var rr,vv;
   180 rr = "0".charCodeAt(0);
   181 for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
   182 rr = "a".charCodeAt(0);
   183 for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
   184 rr = "A".charCodeAt(0);
   185 for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
   187 function int2char(n) { return BI_RM.charAt(n); }
   188 function intAt(s,i) {
   189   var c = BI_RC[s.charCodeAt(i)];
   190   return (c==null)?-1:c;
   191 }
   193 // (protected) copy this to r
   194 function bnpCopyTo(r) {
   195   var this_array = this.array;
   196   var r_array    = r.array;
   198   for(var i = this.t-1; i >= 0; --i) r_array[i] = this_array[i];
   199   r.t = this.t;
   200   r.s = this.s;
   201 }
   203 // (protected) set from integer value x, -DV <= x < DV
   204 function bnpFromInt(x) {
   205   var this_array = this.array;
   206   this.t = 1;
   207   this.s = (x<0)?-1:0;
   208   if(x > 0) this_array[0] = x;
   209   else if(x < -1) this_array[0] = x+DV;
   210   else this.t = 0;
   211 }
   213 // return bigint initialized to value
   214 function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
   216 // (protected) set from string and radix
   217 function bnpFromString(s,b) {
   218   var this_array = this.array;
   219   var k;
   220   if(b == 16) k = 4;
   221   else if(b == 8) k = 3;
   222   else if(b == 256) k = 8; // byte array
   223   else if(b == 2) k = 1;
   224   else if(b == 32) k = 5;
   225   else if(b == 4) k = 2;
   226   else { this.fromRadix(s,b); return; }
   227   this.t = 0;
   228   this.s = 0;
   229   var i = s.length, mi = false, sh = 0;
   230   while(--i >= 0) {
   231     var x = (k==8)?s[i]&0xff:intAt(s,i);
   232     if(x < 0) {
   233       if(s.charAt(i) == "-") mi = true;
   234       continue;
   235     }
   236     mi = false;
   237     if(sh == 0)
   238       this_array[this.t++] = x;
   239     else if(sh+k > BI_DB) {
   240       this_array[this.t-1] |= (x&((1<<(BI_DB-sh))-1))<<sh;
   241       this_array[this.t++] = (x>>(BI_DB-sh));
   242     }
   243     else
   244       this_array[this.t-1] |= x<<sh;
   245     sh += k;
   246     if(sh >= BI_DB) sh -= BI_DB;
   247   }
   248   if(k == 8 && (s[0]&0x80) != 0) {
   249     this.s = -1;
   250     if(sh > 0) this_array[this.t-1] |= ((1<<(BI_DB-sh))-1)<<sh;
   251   }
   252   this.clamp();
   253   if(mi) BigInteger.ZERO.subTo(this,this);
   254 }
   256 // (protected) clamp off excess high words
   257 function bnpClamp() {
   258   var this_array = this.array;
   259   var c = this.s&BI_DM;
   260   while(this.t > 0 && this_array[this.t-1] == c) --this.t;
   261 }
   263 // (public) return string representation in given radix
   264 function bnToString(b) {
   265   var this_array = this.array;
   266   if(this.s < 0) return "-"+this.negate().toString(b);
   267   var k;
   268   if(b == 16) k = 4;
   269   else if(b == 8) k = 3;
   270   else if(b == 2) k = 1;
   271   else if(b == 32) k = 5;
   272   else if(b == 4) k = 2;
   273   else return this.toRadix(b);
   274   var km = (1<<k)-1, d, m = false, r = "", i = this.t;
   275   var p = BI_DB-(i*BI_DB)%k;
   276   if(i-- > 0) {
   277     if(p < BI_DB && (d = this_array[i]>>p) > 0) { m = true; r = int2char(d); }
   278     while(i >= 0) {
   279       if(p < k) {
   280         d = (this_array[i]&((1<<p)-1))<<(k-p);
   281         d |= this_array[--i]>>(p+=BI_DB-k);
   282       }
   283       else {
   284         d = (this_array[i]>>(p-=k))&km;
   285         if(p <= 0) { p += BI_DB; --i; }
   286       }
   287       if(d > 0) m = true;
   288       if(m) r += int2char(d);
   289     }
   290   }
   291   return m?r:"0";
   292 }
   294 // (public) -this
   295 function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
   297 // (public) |this|
   298 function bnAbs() { return (this.s<0)?this.negate():this; }
   300 // (public) return + if this > a, - if this < a, 0 if equal
   301 function bnCompareTo(a) {
   302   var this_array = this.array;
   303   var a_array = a.array;
   305   var r = this.s-a.s;
   306   if(r != 0) return r;
   307   var i = this.t;
   308   r = i-a.t;
   309   if(r != 0) return r;
   310   while(--i >= 0) if((r=this_array[i]-a_array[i]) != 0) return r;
   311   return 0;
   312 }
   314 // returns bit length of the integer x
   315 function nbits(x) {
   316   var r = 1, t;
   317   if((t=x>>>16) != 0) { x = t; r += 16; }
   318   if((t=x>>8) != 0) { x = t; r += 8; }
   319   if((t=x>>4) != 0) { x = t; r += 4; }
   320   if((t=x>>2) != 0) { x = t; r += 2; }
   321   if((t=x>>1) != 0) { x = t; r += 1; }
   322   return r;
   323 }
   325 // (public) return the number of bits in "this"
   326 function bnBitLength() {
   327   var this_array = this.array;
   328   if(this.t <= 0) return 0;
   329   return BI_DB*(this.t-1)+nbits(this_array[this.t-1]^(this.s&BI_DM));
   330 }
   332 // (protected) r = this << n*DB
   333 function bnpDLShiftTo(n,r) {
   334   var this_array = this.array;
   335   var r_array = r.array;
   336   var i;
   337   for(i = this.t-1; i >= 0; --i) r_array[i+n] = this_array[i];
   338   for(i = n-1; i >= 0; --i) r_array[i] = 0;
   339   r.t = this.t+n;
   340   r.s = this.s;
   341 }
   343 // (protected) r = this >> n*DB
   344 function bnpDRShiftTo(n,r) {
   345   var this_array = this.array;
   346   var r_array = r.array;
   347   for(var i = n; i < this.t; ++i) r_array[i-n] = this_array[i];
   348   r.t = Math.max(this.t-n,0);
   349   r.s = this.s;
   350 }
   352 // (protected) r = this << n
   353 function bnpLShiftTo(n,r) {
   354   var this_array = this.array;
   355   var r_array = r.array;
   356   var bs = n%BI_DB;
   357   var cbs = BI_DB-bs;
   358   var bm = (1<<cbs)-1;
   359   var ds = Math.floor(n/BI_DB), c = (this.s<<bs)&BI_DM, i;
   360   for(i = this.t-1; i >= 0; --i) {
   361     r_array[i+ds+1] = (this_array[i]>>cbs)|c;
   362     c = (this_array[i]&bm)<<bs;
   363   }
   364   for(i = ds-1; i >= 0; --i) r_array[i] = 0;
   365   r_array[ds] = c;
   366   r.t = this.t+ds+1;
   367   r.s = this.s;
   368   r.clamp();
   369 }
   371 // (protected) r = this >> n
   372 function bnpRShiftTo(n,r) {
   373   var this_array = this.array;
   374   var r_array = r.array;
   375   r.s = this.s;
   376   var ds = Math.floor(n/BI_DB);
   377   if(ds >= this.t) { r.t = 0; return; }
   378   var bs = n%BI_DB;
   379   var cbs = BI_DB-bs;
   380   var bm = (1<<bs)-1;
   381   r_array[0] = this_array[ds]>>bs;
   382   for(var i = ds+1; i < this.t; ++i) {
   383     r_array[i-ds-1] |= (this_array[i]&bm)<<cbs;
   384     r_array[i-ds] = this_array[i]>>bs;
   385   }
   386   if(bs > 0) r_array[this.t-ds-1] |= (this.s&bm)<<cbs;
   387   r.t = this.t-ds;
   388   r.clamp();
   389 }
   391 // (protected) r = this - a
   392 function bnpSubTo(a,r) {
   393   var this_array = this.array;
   394   var r_array = r.array;
   395   var a_array = a.array;
   396   var i = 0, c = 0, m = Math.min(a.t,this.t);
   397   while(i < m) {
   398     c += this_array[i]-a_array[i];
   399     r_array[i++] = c&BI_DM;
   400     c >>= BI_DB;
   401   }
   402   if(a.t < this.t) {
   403     c -= a.s;
   404     while(i < this.t) {
   405       c += this_array[i];
   406       r_array[i++] = c&BI_DM;
   407       c >>= BI_DB;
   408     }
   409     c += this.s;
   410   }
   411   else {
   412     c += this.s;
   413     while(i < a.t) {
   414       c -= a_array[i];
   415       r_array[i++] = c&BI_DM;
   416       c >>= BI_DB;
   417     }
   418     c -= a.s;
   419   }
   420   r.s = (c<0)?-1:0;
   421   if(c < -1) r_array[i++] = BI_DV+c;
   422   else if(c > 0) r_array[i++] = c;
   423   r.t = i;
   424   r.clamp();
   425 }
   427 // (protected) r = this * a, r != this,a (HAC 14.12)
   428 // "this" should be the larger one if appropriate.
   429 function bnpMultiplyTo(a,r) {
   430   var this_array = this.array;
   431   var r_array = r.array;
   432   var x = this.abs(), y = a.abs();
   433   var y_array = y.array;
   435   var i = x.t;
   436   r.t = i+y.t;
   437   while(--i >= 0) r_array[i] = 0;
   438   for(i = 0; i < y.t; ++i) r_array[i+x.t] = x.am(0,y_array[i],r,i,0,x.t);
   439   r.s = 0;
   440   r.clamp();
   441   if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
   442 }
   444 // (protected) r = this^2, r != this (HAC 14.16)
   445 function bnpSquareTo(r) {
   446   var x = this.abs();
   447   var x_array = x.array;
   448   var r_array = r.array;
   450   var i = r.t = 2*x.t;
   451   while(--i >= 0) r_array[i] = 0;
   452   for(i = 0; i < x.t-1; ++i) {
   453     var c = x.am(i,x_array[i],r,2*i,0,1);
   454     if((r_array[i+x.t]+=x.am(i+1,2*x_array[i],r,2*i+1,c,x.t-i-1)) >= BI_DV) {
   455       r_array[i+x.t] -= BI_DV;
   456       r_array[i+x.t+1] = 1;
   457     }
   458   }
   459   if(r.t > 0) r_array[r.t-1] += x.am(i,x_array[i],r,2*i,0,1);
   460   r.s = 0;
   461   r.clamp();
   462 }
   464 // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
   465 // r != q, this != m.  q or r may be null.
   466 function bnpDivRemTo(m,q,r) {
   467   var pm = m.abs();
   468   if(pm.t <= 0) return;
   469   var pt = this.abs();
   470   if(pt.t < pm.t) {
   471     if(q != null) q.fromInt(0);
   472     if(r != null) this.copyTo(r);
   473     return;
   474   }
   475   if(r == null) r = nbi();
   476   var y = nbi(), ts = this.s, ms = m.s;
   477   var pm_array = pm.array;
   478   var nsh = BI_DB-nbits(pm_array[pm.t-1]);	// normalize modulus
   479   if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
   480   else { pm.copyTo(y); pt.copyTo(r); }
   481   var ys = y.t;
   483   var y_array = y.array;
   484   var y0 = y_array[ys-1];
   485   if(y0 == 0) return;
   486   var yt = y0*(1<<BI_F1)+((ys>1)?y_array[ys-2]>>BI_F2:0);
   487   var d1 = BI_FV/yt, d2 = (1<<BI_F1)/yt, e = 1<<BI_F2;
   488   var i = r.t, j = i-ys, t = (q==null)?nbi():q;
   489   y.dlShiftTo(j,t);
   491   var r_array = r.array;
   492   if(r.compareTo(t) >= 0) {
   493     r_array[r.t++] = 1;
   494     r.subTo(t,r);
   495   }
   496   BigInteger.ONE.dlShiftTo(ys,t);
   497   t.subTo(y,y);	// "negative" y so we can replace sub with am later
   498   while(y.t < ys) y_array[y.t++] = 0;
   499   while(--j >= 0) {
   500     // Estimate quotient digit
   501     var qd = (r_array[--i]==y0)?BI_DM:Math.floor(r_array[i]*d1+(r_array[i-1]+e)*d2);
   502     if((r_array[i]+=y.am(0,qd,r,j,0,ys)) < qd) {	// Try it out
   503       y.dlShiftTo(j,t);
   504       r.subTo(t,r);
   505       while(r_array[i] < --qd) r.subTo(t,r);
   506     }
   507   }
   508   if(q != null) {
   509     r.drShiftTo(ys,q);
   510     if(ts != ms) BigInteger.ZERO.subTo(q,q);
   511   }
   512   r.t = ys;
   513   r.clamp();
   514   if(nsh > 0) r.rShiftTo(nsh,r);	// Denormalize remainder
   515   if(ts < 0) BigInteger.ZERO.subTo(r,r);
   516 }
   518 // (public) this mod a
   519 function bnMod(a) {
   520   var r = nbi();
   521   this.abs().divRemTo(a,null,r);
   522   if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r);
   523   return r;
   524 }
   526 // Modular reduction using "classic" algorithm
   527 function Classic(m) { this.m = m; }
   528 function cConvert(x) {
   529   if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
   530   else return x;
   531 }
   532 function cRevert(x) { return x; }
   533 function cReduce(x) { x.divRemTo(this.m,null,x); }
   534 function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
   535 function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
   537 Classic.prototype.convert = cConvert;
   538 Classic.prototype.revert = cRevert;
   539 Classic.prototype.reduce = cReduce;
   540 Classic.prototype.mulTo = cMulTo;
   541 Classic.prototype.sqrTo = cSqrTo;
   543 // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
   544 // justification:
   545 //         xy == 1 (mod m)
   546 //         xy =  1+km
   547 //   xy(2-xy) = (1+km)(1-km)
   548 // x[y(2-xy)] = 1-k^2m^2
   549 // x[y(2-xy)] == 1 (mod m^2)
   550 // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
   551 // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
   552 // JS multiply "overflows" differently from C/C++, so care is needed here.
   553 function bnpInvDigit() {
   554   var this_array = this.array;
   555   if(this.t < 1) return 0;
   556   var x = this_array[0];
   557   if((x&1) == 0) return 0;
   558   var y = x&3;		// y == 1/x mod 2^2
   559   y = (y*(2-(x&0xf)*y))&0xf;	// y == 1/x mod 2^4
   560   y = (y*(2-(x&0xff)*y))&0xff;	// y == 1/x mod 2^8
   561   y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff;	// y == 1/x mod 2^16
   562   // last step - calculate inverse mod DV directly;
   563   // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
   564   y = (y*(2-x*y%BI_DV))%BI_DV;		// y == 1/x mod 2^dbits
   565   // we really want the negative inverse, and -DV < y < DV
   566   return (y>0)?BI_DV-y:-y;
   567 }
   569 // Montgomery reduction
   570 function Montgomery(m) {
   571   this.m = m;
   572   this.mp = m.invDigit();
   573   this.mpl = this.mp&0x7fff;
   574   this.mph = this.mp>>15;
   575   this.um = (1<<(BI_DB-15))-1;
   576   this.mt2 = 2*m.t;
   577 }
   579 // xR mod m
   580 function montConvert(x) {
   581   var r = nbi();
   582   x.abs().dlShiftTo(this.m.t,r);
   583   r.divRemTo(this.m,null,r);
   584   if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r);
   585   return r;
   586 }
   588 // x/R mod m
   589 function montRevert(x) {
   590   var r = nbi();
   591   x.copyTo(r);
   592   this.reduce(r);
   593   return r;
   594 }
   596 // x = x/R mod m (HAC 14.32)
   597 function montReduce(x) {
   598   var x_array = x.array;
   599   while(x.t <= this.mt2)	// pad x so am has enough room later
   600     x_array[x.t++] = 0;
   601   for(var i = 0; i < this.m.t; ++i) {
   602     // faster way of calculating u0 = x[i]*mp mod DV
   603     var j = x_array[i]&0x7fff;
   604     var u0 = (j*this.mpl+(((j*this.mph+(x_array[i]>>15)*this.mpl)&this.um)<<15))&BI_DM;
   605     // use am to combine the multiply-shift-add into one call
   606     j = i+this.m.t;
   607     x_array[j] += this.m.am(0,u0,x,i,0,this.m.t);
   608     // propagate carry
   609     while(x_array[j] >= BI_DV) { x_array[j] -= BI_DV; x_array[++j]++; }
   610   }
   611   x.clamp();
   612   x.drShiftTo(this.m.t,x);
   613   if(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
   614 }
   616 // r = "x^2/R mod m"; x != r
   617 function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
   619 // r = "xy/R mod m"; x,y != r
   620 function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
   622 Montgomery.prototype.convert = montConvert;
   623 Montgomery.prototype.revert = montRevert;
   624 Montgomery.prototype.reduce = montReduce;
   625 Montgomery.prototype.mulTo = montMulTo;
   626 Montgomery.prototype.sqrTo = montSqrTo;
   628 // (protected) true iff this is even
   629 function bnpIsEven() {
   630   var this_array = this.array;
   631   return ((this.t>0)?(this_array[0]&1):this.s) == 0;
   632 }
   634 // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
   635 function bnpExp(e,z) {
   636   if(e > 0xffffffff || e < 1) return BigInteger.ONE;
   637   var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
   638   g.copyTo(r);
   639   while(--i >= 0) {
   640     z.sqrTo(r,r2);
   641     if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
   642     else { var t = r; r = r2; r2 = t; }
   643   }
   644   return z.revert(r);
   645 }
   647 // (public) this^e % m, 0 <= e < 2^32
   648 function bnModPowInt(e,m) {
   649   var z;
   650   if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
   651   return this.exp(e,z);
   652 }
   654 // protected
   655 BigInteger.prototype.copyTo = bnpCopyTo;
   656 BigInteger.prototype.fromInt = bnpFromInt;
   657 BigInteger.prototype.fromString = bnpFromString;
   658 BigInteger.prototype.clamp = bnpClamp;
   659 BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
   660 BigInteger.prototype.drShiftTo = bnpDRShiftTo;
   661 BigInteger.prototype.lShiftTo = bnpLShiftTo;
   662 BigInteger.prototype.rShiftTo = bnpRShiftTo;
   663 BigInteger.prototype.subTo = bnpSubTo;
   664 BigInteger.prototype.multiplyTo = bnpMultiplyTo;
   665 BigInteger.prototype.squareTo = bnpSquareTo;
   666 BigInteger.prototype.divRemTo = bnpDivRemTo;
   667 BigInteger.prototype.invDigit = bnpInvDigit;
   668 BigInteger.prototype.isEven = bnpIsEven;
   669 BigInteger.prototype.exp = bnpExp;
   671 // public
   672 BigInteger.prototype.toString = bnToString;
   673 BigInteger.prototype.negate = bnNegate;
   674 BigInteger.prototype.abs = bnAbs;
   675 BigInteger.prototype.compareTo = bnCompareTo;
   676 BigInteger.prototype.bitLength = bnBitLength;
   677 BigInteger.prototype.mod = bnMod;
   678 BigInteger.prototype.modPowInt = bnModPowInt;
   680 // "constants"
   681 BigInteger.ZERO = nbv(0);
   682 BigInteger.ONE = nbv(1);
   683 // Copyright (c) 2005  Tom Wu
   684 // All Rights Reserved.
   685 // See "LICENSE" for details.
   687 // Extended JavaScript BN functions, required for RSA private ops.
   689 // (public)
   690 function bnClone() { var r = nbi(); this.copyTo(r); return r; }
   692 // (public) return value as integer
   693 function bnIntValue() {
   694   var this_array = this.array;
   695   if(this.s < 0) {
   696     if(this.t == 1) return this_array[0]-BI_DV;
   697     else if(this.t == 0) return -1;
   698   }
   699   else if(this.t == 1) return this_array[0];
   700   else if(this.t == 0) return 0;
   701   // assumes 16 < DB < 32
   702   return ((this_array[1]&((1<<(32-BI_DB))-1))<<BI_DB)|this_array[0];
   703 }
   705 // (public) return value as byte
   706 function bnByteValue() {
   707   var this_array = this.array;
   708   return (this.t==0)?this.s:(this_array[0]<<24)>>24;
   709 }
   711 // (public) return value as short (assumes DB>=16)
   712 function bnShortValue() {
   713   var this_array = this.array;
   714   return (this.t==0)?this.s:(this_array[0]<<16)>>16;
   715 }
   717 // (protected) return x s.t. r^x < DV
   718 function bnpChunkSize(r) { return Math.floor(Math.LN2*BI_DB/Math.log(r)); }
   720 // (public) 0 if this == 0, 1 if this > 0
   721 function bnSigNum() {
   722   var this_array = this.array;
   723   if(this.s < 0) return -1;
   724   else if(this.t <= 0 || (this.t == 1 && this_array[0] <= 0)) return 0;
   725   else return 1;
   726 }
   728 // (protected) convert to radix string
   729 function bnpToRadix(b) {
   730   if(b == null) b = 10;
   731   if(this.signum() == 0 || b < 2 || b > 36) return "0";
   732   var cs = this.chunkSize(b);
   733   var a = Math.pow(b,cs);
   734   var d = nbv(a), y = nbi(), z = nbi(), r = "";
   735   this.divRemTo(d,y,z);
   736   while(y.signum() > 0) {
   737     r = (a+z.intValue()).toString(b).substr(1) + r;
   738     y.divRemTo(d,y,z);
   739   }
   740   return z.intValue().toString(b) + r;
   741 }
   743 // (protected) convert from radix string
   744 function bnpFromRadix(s,b) {
   745   this.fromInt(0);
   746   if(b == null) b = 10;
   747   var cs = this.chunkSize(b);
   748   var d = Math.pow(b,cs), mi = false, j = 0, w = 0;
   749   for(var i = 0; i < s.length; ++i) {
   750     var x = intAt(s,i);
   751     if(x < 0) {
   752       if(s.charAt(i) == "-" && this.signum() == 0) mi = true;
   753       continue;
   754     }
   755     w = b*w+x;
   756     if(++j >= cs) {
   757       this.dMultiply(d);
   758       this.dAddOffset(w,0);
   759       j = 0;
   760       w = 0;
   761     }
   762   }
   763   if(j > 0) {
   764     this.dMultiply(Math.pow(b,j));
   765     this.dAddOffset(w,0);
   766   }
   767   if(mi) BigInteger.ZERO.subTo(this,this);
   768 }
   770 // (protected) alternate constructor
   771 function bnpFromNumber(a,b,c) {
   772   if("number" == typeof b) {
   773     // new BigInteger(int,int,RNG)
   774     if(a < 2) this.fromInt(1);
   775     else {
   776       this.fromNumber(a,c);
   777       if(!this.testBit(a-1))	// force MSB set
   778         this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this);
   779       if(this.isEven()) this.dAddOffset(1,0); // force odd
   780       while(!this.isProbablePrime(b)) {
   781         this.dAddOffset(2,0);
   782         if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this);
   783       }
   784     }
   785   }
   786   else {
   787     // new BigInteger(int,RNG)
   788     var x = new Array(), t = a&7;
   789     x.length = (a>>3)+1;
   790     b.nextBytes(x);
   791     if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0;
   792     this.fromString(x,256);
   793   }
   794 }
   796 // (public) convert to bigendian byte array
   797 function bnToByteArray() {
   798   var this_array = this.array;
   799   var i = this.t, r = new Array();
   800   r[0] = this.s;
   801   var p = BI_DB-(i*BI_DB)%8, d, k = 0;
   802   if(i-- > 0) {
   803     if(p < BI_DB && (d = this_array[i]>>p) != (this.s&BI_DM)>>p)
   804       r[k++] = d|(this.s<<(BI_DB-p));
   805     while(i >= 0) {
   806       if(p < 8) {
   807         d = (this_array[i]&((1<<p)-1))<<(8-p);
   808         d |= this_array[--i]>>(p+=BI_DB-8);
   809       }
   810       else {
   811         d = (this_array[i]>>(p-=8))&0xff;
   812         if(p <= 0) { p += BI_DB; --i; }
   813       }
   814       if((d&0x80) != 0) d |= -256;
   815       if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
   816       if(k > 0 || d != this.s) r[k++] = d;
   817     }
   818   }
   819   return r;
   820 }
   822 function bnEquals(a) { return(this.compareTo(a)==0); }
   823 function bnMin(a) { return(this.compareTo(a)<0)?this:a; }
   824 function bnMax(a) { return(this.compareTo(a)>0)?this:a; }
   826 // (protected) r = this op a (bitwise)
   827 function bnpBitwiseTo(a,op,r) {
   828   var this_array = this.array;
   829   var a_array    = a.array;
   830   var r_array    = r.array;
   831   var i, f, m = Math.min(a.t,this.t);
   832   for(i = 0; i < m; ++i) r_array[i] = op(this_array[i],a_array[i]);
   833   if(a.t < this.t) {
   834     f = a.s&BI_DM;
   835     for(i = m; i < this.t; ++i) r_array[i] = op(this_array[i],f);
   836     r.t = this.t;
   837   }
   838   else {
   839     f = this.s&BI_DM;
   840     for(i = m; i < a.t; ++i) r_array[i] = op(f,a_array[i]);
   841     r.t = a.t;
   842   }
   843   r.s = op(this.s,a.s);
   844   r.clamp();
   845 }
   847 // (public) this & a
   848 function op_and(x,y) { return x&y; }
   849 function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; }
   851 // (public) this | a
   852 function op_or(x,y) { return x|y; }
   853 function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; }
   855 // (public) this ^ a
   856 function op_xor(x,y) { return x^y; }
   857 function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; }
   859 // (public) this & ~a
   860 function op_andnot(x,y) { return x&~y; }
   861 function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; }
   863 // (public) ~this
   864 function bnNot() {
   865   var this_array = this.array;
   866   var r = nbi();
   867   var r_array = r.array;
   869   for(var i = 0; i < this.t; ++i) r_array[i] = BI_DM&~this_array[i];
   870   r.t = this.t;
   871   r.s = ~this.s;
   872   return r;
   873 }
   875 // (public) this << n
   876 function bnShiftLeft(n) {
   877   var r = nbi();
   878   if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r);
   879   return r;
   880 }
   882 // (public) this >> n
   883 function bnShiftRight(n) {
   884   var r = nbi();
   885   if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r);
   886   return r;
   887 }
   889 // return index of lowest 1-bit in x, x < 2^31
   890 function lbit(x) {
   891   if(x == 0) return -1;
   892   var r = 0;
   893   if((x&0xffff) == 0) { x >>= 16; r += 16; }
   894   if((x&0xff) == 0) { x >>= 8; r += 8; }
   895   if((x&0xf) == 0) { x >>= 4; r += 4; }
   896   if((x&3) == 0) { x >>= 2; r += 2; }
   897   if((x&1) == 0) ++r;
   898   return r;
   899 }
   901 // (public) returns index of lowest 1-bit (or -1 if none)
   902 function bnGetLowestSetBit() {
   903   var this_array = this.array;
   904   for(var i = 0; i < this.t; ++i)
   905     if(this_array[i] != 0) return i*BI_DB+lbit(this_array[i]);
   906   if(this.s < 0) return this.t*BI_DB;
   907   return -1;
   908 }
   910 // return number of 1 bits in x
   911 function cbit(x) {
   912   var r = 0;
   913   while(x != 0) { x &= x-1; ++r; }
   914   return r;
   915 }
   917 // (public) return number of set bits
   918 function bnBitCount() {
   919   var r = 0, x = this.s&BI_DM;
   920   for(var i = 0; i < this.t; ++i) r += cbit(this_array[i]^x);
   921   return r;
   922 }
   924 // (public) true iff nth bit is set
   925 function bnTestBit(n) {
   926   var this_array = this.array;
   927   var j = Math.floor(n/BI_DB);
   928   if(j >= this.t) return(this.s!=0);
   929   return((this_array[j]&(1<<(n%BI_DB)))!=0);
   930 }
   932 // (protected) this op (1<<n)
   933 function bnpChangeBit(n,op) {
   934   var r = BigInteger.ONE.shiftLeft(n);
   935   this.bitwiseTo(r,op,r);
   936   return r;
   937 }
   939 // (public) this | (1<<n)
   940 function bnSetBit(n) { return this.changeBit(n,op_or); }
   942 // (public) this & ~(1<<n)
   943 function bnClearBit(n) { return this.changeBit(n,op_andnot); }
   945 // (public) this ^ (1<<n)
   946 function bnFlipBit(n) { return this.changeBit(n,op_xor); }
   948 // (protected) r = this + a
   949 function bnpAddTo(a,r) {
   950   var this_array = this.array;
   951   var a_array = a.array;
   952   var r_array = r.array;
   953   var i = 0, c = 0, m = Math.min(a.t,this.t);
   954   while(i < m) {
   955     c += this_array[i]+a_array[i];
   956     r_array[i++] = c&BI_DM;
   957     c >>= BI_DB;
   958   }
   959   if(a.t < this.t) {
   960     c += a.s;
   961     while(i < this.t) {
   962       c += this_array[i];
   963       r_array[i++] = c&BI_DM;
   964       c >>= BI_DB;
   965     }
   966     c += this.s;
   967   }
   968   else {
   969     c += this.s;
   970     while(i < a.t) {
   971       c += a_array[i];
   972       r_array[i++] = c&BI_DM;
   973       c >>= BI_DB;
   974     }
   975     c += a.s;
   976   }
   977   r.s = (c<0)?-1:0;
   978   if(c > 0) r_array[i++] = c;
   979   else if(c < -1) r_array[i++] = BI_DV+c;
   980   r.t = i;
   981   r.clamp();
   982 }
   984 // (public) this + a
   985 function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; }
   987 // (public) this - a
   988 function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; }
   990 // (public) this * a
   991 function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; }
   993 // (public) this / a
   994 function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; }
   996 // (public) this % a
   997 function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; }
   999 // (public) [this/a,this%a]
  1000 function bnDivideAndRemainder(a) {
  1001   var q = nbi(), r = nbi();
  1002   this.divRemTo(a,q,r);
  1003   return new Array(q,r);
  1006 // (protected) this *= n, this >= 0, 1 < n < DV
  1007 function bnpDMultiply(n) {
  1008   var this_array = this.array;
  1009   this_array[this.t] = this.am(0,n-1,this,0,0,this.t);
  1010   ++this.t;
  1011   this.clamp();
  1014 // (protected) this += n << w words, this >= 0
  1015 function bnpDAddOffset(n,w) {
  1016   var this_array = this.array;
  1017   while(this.t <= w) this_array[this.t++] = 0;
  1018   this_array[w] += n;
  1019   while(this_array[w] >= BI_DV) {
  1020     this_array[w] -= BI_DV;
  1021     if(++w >= this.t) this_array[this.t++] = 0;
  1022     ++this_array[w];
  1026 // A "null" reducer
  1027 function NullExp() {}
  1028 function nNop(x) { return x; }
  1029 function nMulTo(x,y,r) { x.multiplyTo(y,r); }
  1030 function nSqrTo(x,r) { x.squareTo(r); }
  1032 NullExp.prototype.convert = nNop;
  1033 NullExp.prototype.revert = nNop;
  1034 NullExp.prototype.mulTo = nMulTo;
  1035 NullExp.prototype.sqrTo = nSqrTo;
  1037 // (public) this^e
  1038 function bnPow(e) { return this.exp(e,new NullExp()); }
  1040 // (protected) r = lower n words of "this * a", a.t <= n
  1041 // "this" should be the larger one if appropriate.
  1042 function bnpMultiplyLowerTo(a,n,r) {
  1043   var r_array = r.array;
  1044   var a_array = a.array;
  1045   var i = Math.min(this.t+a.t,n);
  1046   r.s = 0; // assumes a,this >= 0
  1047   r.t = i;
  1048   while(i > 0) r_array[--i] = 0;
  1049   var j;
  1050   for(j = r.t-this.t; i < j; ++i) r_array[i+this.t] = this.am(0,a_array[i],r,i,0,this.t);
  1051   for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a_array[i],r,i,0,n-i);
  1052   r.clamp();
  1055 // (protected) r = "this * a" without lower n words, n > 0
  1056 // "this" should be the larger one if appropriate.
  1057 function bnpMultiplyUpperTo(a,n,r) {
  1058   var r_array = r.array;
  1059   var a_array = a.array;
  1060   --n;
  1061   var i = r.t = this.t+a.t-n;
  1062   r.s = 0; // assumes a,this >= 0
  1063   while(--i >= 0) r_array[i] = 0;
  1064   for(i = Math.max(n-this.t,0); i < a.t; ++i)
  1065     r_array[this.t+i-n] = this.am(n-i,a_array[i],r,0,0,this.t+i-n);
  1066   r.clamp();
  1067   r.drShiftTo(1,r);
  1070 // Barrett modular reduction
  1071 function Barrett(m) {
  1072   // setup Barrett
  1073   this.r2 = nbi();
  1074   this.q3 = nbi();
  1075   BigInteger.ONE.dlShiftTo(2*m.t,this.r2);
  1076   this.mu = this.r2.divide(m);
  1077   this.m = m;
  1080 function barrettConvert(x) {
  1081   if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m);
  1082   else if(x.compareTo(this.m) < 0) return x;
  1083   else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
  1086 function barrettRevert(x) { return x; }
  1088 // x = x mod m (HAC 14.42)
  1089 function barrettReduce(x) {
  1090   x.drShiftTo(this.m.t-1,this.r2);
  1091   if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
  1092   this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);
  1093   this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);
  1094   while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1);
  1095   x.subTo(this.r2,x);
  1096   while(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
  1099 // r = x^2 mod m; x != r
  1100 function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
  1102 // r = x*y mod m; x,y != r
  1103 function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
  1105 Barrett.prototype.convert = barrettConvert;
  1106 Barrett.prototype.revert = barrettRevert;
  1107 Barrett.prototype.reduce = barrettReduce;
  1108 Barrett.prototype.mulTo = barrettMulTo;
  1109 Barrett.prototype.sqrTo = barrettSqrTo;
  1111 // (public) this^e % m (HAC 14.85)
  1112 function bnModPow(e,m) {
  1113   var e_array = e.array;
  1114   var i = e.bitLength(), k, r = nbv(1), z;
  1115   if(i <= 0) return r;
  1116   else if(i < 18) k = 1;
  1117   else if(i < 48) k = 3;
  1118   else if(i < 144) k = 4;
  1119   else if(i < 768) k = 5;
  1120   else k = 6;
  1121   if(i < 8)
  1122     z = new Classic(m);
  1123   else if(m.isEven())
  1124     z = new Barrett(m);
  1125   else
  1126     z = new Montgomery(m);
  1128   // precomputation
  1129   var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1;
  1130   g[1] = z.convert(this);
  1131   if(k > 1) {
  1132     var g2 = nbi();
  1133     z.sqrTo(g[1],g2);
  1134     while(n <= km) {
  1135       g[n] = nbi();
  1136       z.mulTo(g2,g[n-2],g[n]);
  1137       n += 2;
  1141   var j = e.t-1, w, is1 = true, r2 = nbi(), t;
  1142   i = nbits(e_array[j])-1;
  1143   while(j >= 0) {
  1144     if(i >= k1) w = (e_array[j]>>(i-k1))&km;
  1145     else {
  1146       w = (e_array[j]&((1<<(i+1))-1))<<(k1-i);
  1147       if(j > 0) w |= e_array[j-1]>>(BI_DB+i-k1);
  1150     n = k;
  1151     while((w&1) == 0) { w >>= 1; --n; }
  1152     if((i -= n) < 0) { i += BI_DB; --j; }
  1153     if(is1) {	// ret == 1, don't bother squaring or multiplying it
  1154       g[w].copyTo(r);
  1155       is1 = false;
  1157     else {
  1158       while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; }
  1159       if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; }
  1160       z.mulTo(r2,g[w],r);
  1163     while(j >= 0 && (e_array[j]&(1<<i)) == 0) {
  1164       z.sqrTo(r,r2); t = r; r = r2; r2 = t;
  1165       if(--i < 0) { i = BI_DB-1; --j; }
  1168   return z.revert(r);
  1171 // (public) gcd(this,a) (HAC 14.54)
  1172 function bnGCD(a) {
  1173   var x = (this.s<0)?this.negate():this.clone();
  1174   var y = (a.s<0)?a.negate():a.clone();
  1175   if(x.compareTo(y) < 0) { var t = x; x = y; y = t; }
  1176   var i = x.getLowestSetBit(), g = y.getLowestSetBit();
  1177   if(g < 0) return x;
  1178   if(i < g) g = i;
  1179   if(g > 0) {
  1180     x.rShiftTo(g,x);
  1181     y.rShiftTo(g,y);
  1183   while(x.signum() > 0) {
  1184     if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x);
  1185     if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y);
  1186     if(x.compareTo(y) >= 0) {
  1187       x.subTo(y,x);
  1188       x.rShiftTo(1,x);
  1190     else {
  1191       y.subTo(x,y);
  1192       y.rShiftTo(1,y);
  1195   if(g > 0) y.lShiftTo(g,y);
  1196   return y;
  1199 // (protected) this % n, n < 2^26
  1200 function bnpModInt(n) {
  1201   var this_array = this.array;
  1202   if(n <= 0) return 0;
  1203   var d = BI_DV%n, r = (this.s<0)?n-1:0;
  1204   if(this.t > 0)
  1205     if(d == 0) r = this_array[0]%n;
  1206     else for(var i = this.t-1; i >= 0; --i) r = (d*r+this_array[i])%n;
  1207   return r;
  1210 // (public) 1/this % m (HAC 14.61)
  1211 function bnModInverse(m) {
  1212   var ac = m.isEven();
  1213   if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO;
  1214   var u = m.clone(), v = this.clone();
  1215   var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
  1216   while(u.signum() != 0) {
  1217     while(u.isEven()) {
  1218       u.rShiftTo(1,u);
  1219       if(ac) {
  1220         if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); }
  1221         a.rShiftTo(1,a);
  1223       else if(!b.isEven()) b.subTo(m,b);
  1224       b.rShiftTo(1,b);
  1226     while(v.isEven()) {
  1227       v.rShiftTo(1,v);
  1228       if(ac) {
  1229         if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); }
  1230         c.rShiftTo(1,c);
  1232       else if(!d.isEven()) d.subTo(m,d);
  1233       d.rShiftTo(1,d);
  1235     if(u.compareTo(v) >= 0) {
  1236       u.subTo(v,u);
  1237       if(ac) a.subTo(c,a);
  1238       b.subTo(d,b);
  1240     else {
  1241       v.subTo(u,v);
  1242       if(ac) c.subTo(a,c);
  1243       d.subTo(b,d);
  1246   if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
  1247   if(d.compareTo(m) >= 0) return d.subtract(m);
  1248   if(d.signum() < 0) d.addTo(m,d); else return d;
  1249   if(d.signum() < 0) return d.add(m); else return d;
  1252 var 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];
  1253 var lplim = (1<<26)/lowprimes[lowprimes.length-1];
  1255 // (public) test primality with certainty >= 1-.5^t
  1256 function bnIsProbablePrime(t) {
  1257   var i, x = this.abs();
  1258   var x_array = x.array;
  1259   if(x.t == 1 && x_array[0] <= lowprimes[lowprimes.length-1]) {
  1260     for(i = 0; i < lowprimes.length; ++i)
  1261       if(x_array[0] == lowprimes[i]) return true;
  1262     return false;
  1264   if(x.isEven()) return false;
  1265   i = 1;
  1266   while(i < lowprimes.length) {
  1267     var m = lowprimes[i], j = i+1;
  1268     while(j < lowprimes.length && m < lplim) m *= lowprimes[j++];
  1269     m = x.modInt(m);
  1270     while(i < j) if(m%lowprimes[i++] == 0) return false;
  1272   return x.millerRabin(t);
  1275 // (protected) true if probably prime (HAC 4.24, Miller-Rabin)
  1276 function bnpMillerRabin(t) {
  1277   var n1 = this.subtract(BigInteger.ONE);
  1278   var k = n1.getLowestSetBit();
  1279   if(k <= 0) return false;
  1280   var r = n1.shiftRight(k);
  1281   t = (t+1)>>1;
  1282   if(t > lowprimes.length) t = lowprimes.length;
  1283   var a = nbi();
  1284   for(var i = 0; i < t; ++i) {
  1285     a.fromInt(lowprimes[i]);
  1286     var y = a.modPow(r,this);
  1287     if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
  1288       var j = 1;
  1289       while(j++ < k && y.compareTo(n1) != 0) {
  1290         y = y.modPowInt(2,this);
  1291         if(y.compareTo(BigInteger.ONE) == 0) return false;
  1293       if(y.compareTo(n1) != 0) return false;
  1296   return true;
  1299 // protected
  1300 BigInteger.prototype.chunkSize = bnpChunkSize;
  1301 BigInteger.prototype.toRadix = bnpToRadix;
  1302 BigInteger.prototype.fromRadix = bnpFromRadix;
  1303 BigInteger.prototype.fromNumber = bnpFromNumber;
  1304 BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
  1305 BigInteger.prototype.changeBit = bnpChangeBit;
  1306 BigInteger.prototype.addTo = bnpAddTo;
  1307 BigInteger.prototype.dMultiply = bnpDMultiply;
  1308 BigInteger.prototype.dAddOffset = bnpDAddOffset;
  1309 BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
  1310 BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
  1311 BigInteger.prototype.modInt = bnpModInt;
  1312 BigInteger.prototype.millerRabin = bnpMillerRabin;
  1314 // public
  1315 BigInteger.prototype.clone = bnClone;
  1316 BigInteger.prototype.intValue = bnIntValue;
  1317 BigInteger.prototype.byteValue = bnByteValue;
  1318 BigInteger.prototype.shortValue = bnShortValue;
  1319 BigInteger.prototype.signum = bnSigNum;
  1320 BigInteger.prototype.toByteArray = bnToByteArray;
  1321 BigInteger.prototype.equals = bnEquals;
  1322 BigInteger.prototype.min = bnMin;
  1323 BigInteger.prototype.max = bnMax;
  1324 BigInteger.prototype.and = bnAnd;
  1325 BigInteger.prototype.or = bnOr;
  1326 BigInteger.prototype.xor = bnXor;
  1327 BigInteger.prototype.andNot = bnAndNot;
  1328 BigInteger.prototype.not = bnNot;
  1329 BigInteger.prototype.shiftLeft = bnShiftLeft;
  1330 BigInteger.prototype.shiftRight = bnShiftRight;
  1331 BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
  1332 BigInteger.prototype.bitCount = bnBitCount;
  1333 BigInteger.prototype.testBit = bnTestBit;
  1334 BigInteger.prototype.setBit = bnSetBit;
  1335 BigInteger.prototype.clearBit = bnClearBit;
  1336 BigInteger.prototype.flipBit = bnFlipBit;
  1337 BigInteger.prototype.add = bnAdd;
  1338 BigInteger.prototype.subtract = bnSubtract;
  1339 BigInteger.prototype.multiply = bnMultiply;
  1340 BigInteger.prototype.divide = bnDivide;
  1341 BigInteger.prototype.remainder = bnRemainder;
  1342 BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
  1343 BigInteger.prototype.modPow = bnModPow;
  1344 BigInteger.prototype.modInverse = bnModInverse;
  1345 BigInteger.prototype.pow = bnPow;
  1346 BigInteger.prototype.gcd = bnGCD;
  1347 BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
  1349 // BigInteger interfaces not implemented in jsbn:
  1351 // BigInteger(int signum, byte[] magnitude)
  1352 // double doubleValue()
  1353 // float floatValue()
  1354 // int hashCode()
  1355 // long longValue()
  1356 // static BigInteger valueOf(long val)
  1357 // prng4.js - uses Arcfour as a PRNG
  1359 function Arcfour() {
  1360   this.i = 0;
  1361   this.j = 0;
  1362   this.S = new Array();
  1365 // Initialize arcfour context from key, an array of ints, each from [0..255]
  1366 function ARC4init(key) {
  1367   var i, j, t;
  1368   for(i = 0; i < 256; ++i)
  1369     this.S[i] = i;
  1370   j = 0;
  1371   for(i = 0; i < 256; ++i) {
  1372     j = (j + this.S[i] + key[i % key.length]) & 255;
  1373     t = this.S[i];
  1374     this.S[i] = this.S[j];
  1375     this.S[j] = t;
  1377   this.i = 0;
  1378   this.j = 0;
  1381 function ARC4next() {
  1382   var t;
  1383   this.i = (this.i + 1) & 255;
  1384   this.j = (this.j + this.S[this.i]) & 255;
  1385   t = this.S[this.i];
  1386   this.S[this.i] = this.S[this.j];
  1387   this.S[this.j] = t;
  1388   return this.S[(t + this.S[this.i]) & 255];
  1391 Arcfour.prototype.init = ARC4init;
  1392 Arcfour.prototype.next = ARC4next;
  1394 // Plug in your RNG constructor here
  1395 function prng_newstate() {
  1396   return new Arcfour();
  1399 // Pool size must be a multiple of 4 and greater than 32.
  1400 // An array of bytes the size of the pool will be passed to init()
  1401 var rng_psize = 256;
  1402 // Random number generator - requires a PRNG backend, e.g. prng4.js
  1404 // For best results, put code like
  1405 // <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'>
  1406 // in your main HTML document.
  1408 var rng_state;
  1409 var rng_pool;
  1410 var rng_pptr;
  1412 // Mix in a 32-bit integer into the pool
  1413 function rng_seed_int(x) {
  1414   rng_pool[rng_pptr++] ^= x & 255;
  1415   rng_pool[rng_pptr++] ^= (x >> 8) & 255;
  1416   rng_pool[rng_pptr++] ^= (x >> 16) & 255;
  1417   rng_pool[rng_pptr++] ^= (x >> 24) & 255;
  1418   if(rng_pptr >= rng_psize) rng_pptr -= rng_psize;
  1421 // Mix in the current time (w/milliseconds) into the pool
  1422 function rng_seed_time() {
  1423   // Use pre-computed date to avoid making the benchmark 
  1424   // results dependent on the current date.
  1425   rng_seed_int(1122926989487);
  1428 // Initialize the pool with junk if needed.
  1429 if(rng_pool == null) {
  1430   rng_pool = new Array();
  1431   rng_pptr = 0;
  1432   var t;
  1433   while(rng_pptr < rng_psize) {  // extract some randomness from Math.random()
  1434     t = Math.floor(65536 * MyMath.random());
  1435     rng_pool[rng_pptr++] = t >>> 8;
  1436     rng_pool[rng_pptr++] = t & 255;
  1438   rng_pptr = 0;
  1439   rng_seed_time();
  1440   //rng_seed_int(window.screenX);
  1441   //rng_seed_int(window.screenY);
  1444 function rng_get_byte() {
  1445   if(rng_state == null) {
  1446     rng_seed_time();
  1447     rng_state = prng_newstate();
  1448     rng_state.init(rng_pool);
  1449     for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)
  1450       rng_pool[rng_pptr] = 0;
  1451     rng_pptr = 0;
  1452     //rng_pool = null;
  1454   // TODO: allow reseeding after first request
  1455   return rng_state.next();
  1458 function rng_get_bytes(ba) {
  1459   var i;
  1460   for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();
  1463 function SecureRandom() {}
  1465 SecureRandom.prototype.nextBytes = rng_get_bytes;
  1466 // Depends on jsbn.js and rng.js
  1468 // convert a (hex) string to a bignum object
  1469 function parseBigInt(str,r) {
  1470   return new BigInteger(str,r);
  1473 function linebrk(s,n) {
  1474   var ret = "";
  1475   var i = 0;
  1476   while(i + n < s.length) {
  1477     ret += s.substring(i,i+n) + "\n";
  1478     i += n;
  1480   return ret + s.substring(i,s.length);
  1483 function byte2Hex(b) {
  1484   if(b < 0x10)
  1485     return "0" + b.toString(16);
  1486   else
  1487     return b.toString(16);
  1490 // PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint
  1491 function pkcs1pad2(s,n) {
  1492   if(n < s.length + 11) {
  1493     alert("Message too long for RSA");
  1494     return null;
  1496   var ba = new Array();
  1497   var i = s.length - 1;
  1498   while(i >= 0 && n > 0) ba[--n] = s.charCodeAt(i--);
  1499   ba[--n] = 0;
  1500   var rng = new SecureRandom();
  1501   var x = new Array();
  1502   while(n > 2) { // random non-zero pad
  1503     x[0] = 0;
  1504     while(x[0] == 0) rng.nextBytes(x);
  1505     ba[--n] = x[0];
  1507   ba[--n] = 2;
  1508   ba[--n] = 0;
  1509   return new BigInteger(ba);
  1512 // "empty" RSA key constructor
  1513 function RSAKey() {
  1514   this.n = null;
  1515   this.e = 0;
  1516   this.d = null;
  1517   this.p = null;
  1518   this.q = null;
  1519   this.dmp1 = null;
  1520   this.dmq1 = null;
  1521   this.coeff = null;
  1524 // Set the public key fields N and e from hex strings
  1525 function RSASetPublic(N,E) {
  1526   if(N != null && E != null && N.length > 0 && E.length > 0) {
  1527     this.n = parseBigInt(N,16);
  1528     this.e = parseInt(E,16);
  1530   else
  1531     alert("Invalid RSA public key");
  1534 // Perform raw public operation on "x": return x^e (mod n)
  1535 function RSADoPublic(x) {
  1536   return x.modPowInt(this.e, this.n);
  1539 // Return the PKCS#1 RSA encryption of "text" as an even-length hex string
  1540 function RSAEncrypt(text) {
  1541   var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3);
  1542   if(m == null) return null;
  1543   var c = this.doPublic(m);
  1544   if(c == null) return null;
  1545   var h = c.toString(16);
  1546   if((h.length & 1) == 0) return h; else return "0" + h;
  1549 // Return the PKCS#1 RSA encryption of "text" as a Base64-encoded string
  1550 //function RSAEncryptB64(text) {
  1551 //  var h = this.encrypt(text);
  1552 //  if(h) return hex2b64(h); else return null;
  1553 //}
  1555 // protected
  1556 RSAKey.prototype.doPublic = RSADoPublic;
  1558 // public
  1559 RSAKey.prototype.setPublic = RSASetPublic;
  1560 RSAKey.prototype.encrypt = RSAEncrypt;
  1561 //RSAKey.prototype.encrypt_b64 = RSAEncryptB64;
  1562 // Depends on rsa.js and jsbn2.js
  1564 // Undo PKCS#1 (type 2, random) padding and, if valid, return the plaintext
  1565 function pkcs1unpad2(d,n) {
  1566   var b = d.toByteArray();
  1567   var i = 0;
  1568   while(i < b.length && b[i] == 0) ++i;
  1569   if(b.length-i != n-1 || b[i] != 2)
  1570     return null;
  1571   ++i;
  1572   while(b[i] != 0)
  1573     if(++i >= b.length) return null;
  1574   var ret = "";
  1575   while(++i < b.length)
  1576     ret += String.fromCharCode(b[i]);
  1577   return ret;
  1580 // Set the private key fields N, e, and d from hex strings
  1581 function RSASetPrivate(N,E,D) {
  1582   if(N != null && E != null && N.length > 0 && E.length > 0) {
  1583     this.n = parseBigInt(N,16);
  1584     this.e = parseInt(E,16);
  1585     this.d = parseBigInt(D,16);
  1587   else
  1588     alert("Invalid RSA private key");
  1591 // Set the private key fields N, e, d and CRT params from hex strings
  1592 function RSASetPrivateEx(N,E,D,P,Q,DP,DQ,C) {
  1593   if(N != null && E != null && N.length > 0 && E.length > 0) {
  1594     this.n = parseBigInt(N,16);
  1595     this.e = parseInt(E,16);
  1596     this.d = parseBigInt(D,16);
  1597     this.p = parseBigInt(P,16);
  1598     this.q = parseBigInt(Q,16);
  1599     this.dmp1 = parseBigInt(DP,16);
  1600     this.dmq1 = parseBigInt(DQ,16);
  1601     this.coeff = parseBigInt(C,16);
  1603   else
  1604     alert("Invalid RSA private key");
  1607 // Generate a new random private key B bits long, using public expt E
  1608 function RSAGenerate(B,E) {
  1609   var rng = new SecureRandom();
  1610   var qs = B>>1;
  1611   this.e = parseInt(E,16);
  1612   var ee = new BigInteger(E,16);
  1613   for(;;) {
  1614     for(;;) {
  1615       this.p = new BigInteger(B-qs,1,rng);
  1616       if(this.p.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.p.isProbablePrime(10)) break;
  1618     for(;;) {
  1619       this.q = new BigInteger(qs,1,rng);
  1620       if(this.q.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.q.isProbablePrime(10)) break;
  1622     if(this.p.compareTo(this.q) <= 0) {
  1623       var t = this.p;
  1624       this.p = this.q;
  1625       this.q = t;
  1627     var p1 = this.p.subtract(BigInteger.ONE);
  1628     var q1 = this.q.subtract(BigInteger.ONE);
  1629     var phi = p1.multiply(q1);
  1630     if(phi.gcd(ee).compareTo(BigInteger.ONE) == 0) {
  1631       this.n = this.p.multiply(this.q);
  1632       this.d = ee.modInverse(phi);
  1633       this.dmp1 = this.d.mod(p1);
  1634       this.dmq1 = this.d.mod(q1);
  1635       this.coeff = this.q.modInverse(this.p);
  1636       break;
  1641 // Perform raw private operation on "x": return x^d (mod n)
  1642 function RSADoPrivate(x) {
  1643   if(this.p == null || this.q == null)
  1644     return x.modPow(this.d, this.n);
  1646   // TODO: re-calculate any missing CRT params
  1647   var xp = x.mod(this.p).modPow(this.dmp1, this.p);
  1648   var xq = x.mod(this.q).modPow(this.dmq1, this.q);
  1650   while(xp.compareTo(xq) < 0)
  1651     xp = xp.add(this.p);
  1652   return xp.subtract(xq).multiply(this.coeff).mod(this.p).multiply(this.q).add(xq);
  1655 // Return the PKCS#1 RSA decryption of "ctext".
  1656 // "ctext" is an even-length hex string and the output is a plain string.
  1657 function RSADecrypt(ctext) {
  1658   var c = parseBigInt(ctext, 16);
  1659   var m = this.doPrivate(c);
  1660   if(m == null) return null;
  1661   return pkcs1unpad2(m, (this.n.bitLength()+7)>>3);
  1664 // Return the PKCS#1 RSA decryption of "ctext".
  1665 // "ctext" is a Base64-encoded string and the output is a plain string.
  1666 //function RSAB64Decrypt(ctext) {
  1667 //  var h = b64tohex(ctext);
  1668 //  if(h) return this.decrypt(h); else return null;
  1669 //}
  1671 // protected
  1672 RSAKey.prototype.doPrivate = RSADoPrivate;
  1674 // public
  1675 RSAKey.prototype.setPrivate = RSASetPrivate;
  1676 RSAKey.prototype.setPrivateEx = RSASetPrivateEx;
  1677 RSAKey.prototype.generate = RSAGenerate;
  1678 RSAKey.prototype.decrypt = RSADecrypt;
  1679 //RSAKey.prototype.b64_decrypt = RSAB64Decrypt;
  1682 nValue="a5261939975948bb7a58dffe5ff54e65f0498f9175f5a09288810b8975871e99af3b5dd94057b0fc07535f5f97444504fa35169d461d0d30cf0192e307727c065168c788771c561a9400fb49175e9e6aa4e23fe11af69e9412dd23b0cb6684c4c2429bce139e848ab26d0829073351f4acd36074eafd036a5eb83359d2a698d3";
  1683 eValue="10001";
  1684 dValue="8e9912f6d3645894e8d38cb58c0db81ff516cf4c7e5a14c7f1eddb1459d2cded4d8d293fc97aee6aefb861859c8b6a3d1dfe710463e1f9ddc72048c09751971c4a580aa51eb523357a3cc48d31cfad1d4a165066ed92d4748fb6571211da5cb14bc11b6e2df7c1a559e6d5ac1cd5c94703a22891464fba23d0d965086277a161";
  1685 pValue="d090ce58a92c75233a6486cb0a9209bf3583b64f540c76f5294bb97d285eed33aec220bde14b2417951178ac152ceab6da7090905b478195498b352048f15e7d";
  1686 qValue="cab575dc652bb66df15a0359609d51d1db184750c00c6698b90ef3465c99655103edbf0d54c56aec0ce3c4d22592338092a126a0cc49f65a4a30d222b411e58f";
  1687 dmp1Value="1a24bca8e273df2f0e47c199bbf678604e7df7215480c77c8db39f49b000ce2cf7500038acfff5433b7d582a01f1826e6f4d42e1c57f5e1fef7b12aabc59fd25";
  1688 dmq1Value="3d06982efbbe47339e1f6d36b1216b8a741d410b0c662f54f7118b27b9a4ec9d914337eb39841d8666f3034408cf94f5b62f11c402fc994fe15a05493150d9fd";
  1689 coeffValue="3a3e731acd8960b7ff9eb81a7ff93bd1cfa74cbd56987db58b4594fb09c09084db1734c8143f98b602b981aaa9243ca28deb69b5b280ee8dcee0fd2625e53250";
  1691 setupEngine(am3, 28);
  1693 // So that v8 understands assertEq()
  1694 if (assertEq == undefined)
  1696     function assertEq(to_check, expected) {
  1697         if ( to_check !== expected )
  1699             print( "Error: Assertion failed: got \"" + to_check + "\", expected \"" + expected + "\"" );
  1704 function check_correctness(text, hash) {
  1705   var RSA = new RSAKey();
  1706   RSA.setPublic(nValue, eValue);
  1707   RSA.setPrivateEx(nValue, eValue, dValue, pValue, qValue, dmp1Value, dmq1Value, coeffValue);
  1708   var encrypted = RSA.encrypt(text);
  1709   var decrypted = RSA.decrypt(encrypted);
  1710   assertEq( encrypted, hash );
  1711   assertEq( decrypted, text );
  1714 // All 'correct' hashes here come from v8's javascript shell built off of tag 2.3.4
  1715 check_correctness("Hello! I am some text.", "142b19b40fee712ab9468be296447d38c7dfe81a7850f11ae6aa21e49396a4e90bd6ba4aa385105e15960a59f95447dfad89671da6e08ed42229939583753be84d07558abb4feee4d46a92fd31d962679a1a5f4bf0fb7af414b9a756e18df7e6d1e96971cc66769f3b27d61ad932f2211373e0de388dc040557d4c3c3fe74320");
  1716 check_correctness("PLEASE ENCRYPT ME. I AM TEXT. I AM DIEING TO BE ENCRYPTED. OH WHY WONT YOU ENCRYPT ME!?", "490c1fae87d7046296e4b34b357912a72cb7c38c0da3198f1ac3aad3489662ce02663ec5ea1be58ae73a275f3096b16c491f3520ebf822df6c65cc95e28be1cc0a4454dfba3fdd402c3a9de0db2f308989bfc1a7fada0dd680db76d24b2d96bd6b7e7d7e7f962deb953038bae06092f7bb9bcb40bba4ec92e040df32f98e035e");
  1717 check_correctness("x","46c1b7cf202171b1b588e9ecf250e768dcf3b300490e859d508f708e702ef799bc496b9fac7634d60a82644653c5fd25b808393b234567116b8890d5f119c7c74dae7c97c8e40ba78ca2dc3e3d78ce859a7fa3815f42c27d0607eafc3940896abb6019cc28b2ff875531ed581a6351728a8df0d607b7c2c26265bf3dddbe4f84");

mercurial