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.

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

mercurial