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