1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jit-test/tests/v8-v5/check-crypto.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1717 @@ 1.4 +// |jit-test| slow; 1.5 +// This test times out in rooting analyis builds, and so is marked slow so that 1.6 +// it's not run as part of the rooting analysis tests on tinderbox. 1.7 + 1.8 +/* 1.9 + * Copyright (c) 2003-2005 Tom Wu 1.10 + * All Rights Reserved. 1.11 + * 1.12 + * Permission is hereby granted, free of charge, to any person obtaining 1.13 + * a copy of this software and associated documentation files (the 1.14 + * "Software"), to deal in the Software without restriction, including 1.15 + * without limitation the rights to use, copy, modify, merge, publish, 1.16 + * distribute, sublicense, and/or sell copies of the Software, and to 1.17 + * permit persons to whom the Software is furnished to do so, subject to 1.18 + * the following conditions: 1.19 + * 1.20 + * The above copyright notice and this permission notice shall be 1.21 + * included in all copies or substantial portions of the Software. 1.22 + * 1.23 + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 1.24 + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 1.25 + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 1.26 + * 1.27 + * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 1.28 + * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 1.29 + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF 1.30 + * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT 1.31 + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1.32 + * 1.33 + * In addition, the following condition applies: 1.34 + * 1.35 + * All redistributions must retain an intact copy of this copyright notice 1.36 + * and disclaimer. 1.37 + */ 1.38 + 1.39 + 1.40 +// The code has been adapted for use as a benchmark by Google. 1.41 +//var Crypto = new BenchmarkSuite('Crypto', 203037, [ 1.42 +// new Benchmark("Encrypt", encrypt), 1.43 +// new Benchmark("Decrypt", decrypt) 1.44 +//]); 1.45 + 1.46 + 1.47 +// Basic JavaScript BN library - subset useful for RSA encryption. 1.48 + 1.49 +// Bits per digit 1.50 +var dbits; 1.51 +var BI_DB; 1.52 +var BI_DM; 1.53 +var BI_DV; 1.54 + 1.55 +var BI_FP; 1.56 +var BI_FV; 1.57 +var BI_F1; 1.58 +var BI_F2; 1.59 + 1.60 +// JavaScript engine analysis 1.61 +var canary = 0xdeadbeefcafe; 1.62 +var j_lm = ((canary&0xffffff)==0xefcafe); 1.63 + 1.64 +// This is the best random number generator available to mankind ;) 1.65 +var MyMath = { 1.66 + curr: 0, 1.67 + random: function() { 1.68 + this.curr = this.curr + 1; 1.69 + return this.curr; 1.70 + }, 1.71 +}; 1.72 + 1.73 + 1.74 +// (public) Constructor 1.75 +function BigInteger(a,b,c) { 1.76 + this.array = new Array(); 1.77 + if(a != null) 1.78 + if("number" == typeof a) this.fromNumber(a,b,c); 1.79 + else if(b == null && "string" != typeof a) this.fromString(a,256); 1.80 + else this.fromString(a,b); 1.81 +} 1.82 + 1.83 +// return new, unset BigInteger 1.84 +function nbi() { return new BigInteger(null); } 1.85 + 1.86 +// am: Compute w_j += (x*this_i), propagate carries, 1.87 +// c is initial carry, returns final carry. 1.88 +// c < 3*dvalue, x < 2*dvalue, this_i < dvalue 1.89 +// We need to select the fastest one that works in this environment. 1.90 + 1.91 +// am1: use a single mult and divide to get the high bits, 1.92 +// max digit bits should be 26 because 1.93 +// max internal value = 2*dvalue^2-2*dvalue (< 2^53) 1.94 +function am1(i,x,w,j,c,n) { 1.95 + var this_array = this.array; 1.96 + var w_array = w.array; 1.97 + while(--n >= 0) { 1.98 + var v = x*this_array[i++]+w_array[j]+c; 1.99 + c = Math.floor(v/0x4000000); 1.100 + w_array[j++] = v&0x3ffffff; 1.101 + } 1.102 + return c; 1.103 +} 1.104 + 1.105 +// am2 avoids a big mult-and-extract completely. 1.106 +// Max digit bits should be <= 30 because we do bitwise ops 1.107 +// on values up to 2*hdvalue^2-hdvalue-1 (< 2^31) 1.108 +function am2(i,x,w,j,c,n) { 1.109 + var this_array = this.array; 1.110 + var w_array = w.array; 1.111 + var xl = x&0x7fff, xh = x>>15; 1.112 + while(--n >= 0) { 1.113 + var l = this_array[i]&0x7fff; 1.114 + var h = this_array[i++]>>15; 1.115 + var m = xh*l+h*xl; 1.116 + l = xl*l+((m&0x7fff)<<15)+w_array[j]+(c&0x3fffffff); 1.117 + c = (l>>>30)+(m>>>15)+xh*h+(c>>>30); 1.118 + w_array[j++] = l&0x3fffffff; 1.119 + } 1.120 + return c; 1.121 +} 1.122 + 1.123 +// Alternately, set max digit bits to 28 since some 1.124 +// browsers slow down when dealing with 32-bit numbers. 1.125 +function am3(i,x,w,j,c,n) { 1.126 + var this_array = this.array; 1.127 + var w_array = w.array; 1.128 + 1.129 + var xl = x&0x3fff, xh = x>>14; 1.130 + while(--n >= 0) { 1.131 + var l = this_array[i]&0x3fff; 1.132 + var h = this_array[i++]>>14; 1.133 + var m = xh*l+h*xl; 1.134 + l = xl*l+((m&0x3fff)<<14)+w_array[j]+c; 1.135 + c = (l>>28)+(m>>14)+xh*h; 1.136 + w_array[j++] = l&0xfffffff; 1.137 + } 1.138 + return c; 1.139 +} 1.140 + 1.141 +// This is tailored to VMs with 2-bit tagging. It makes sure 1.142 +// that all the computations stay within the 29 bits available. 1.143 +function am4(i,x,w,j,c,n) { 1.144 + var this_array = this.array; 1.145 + var w_array = w.array; 1.146 + 1.147 + var xl = x&0x1fff, xh = x>>13; 1.148 + while(--n >= 0) { 1.149 + var l = this_array[i]&0x1fff; 1.150 + var h = this_array[i++]>>13; 1.151 + var m = xh*l+h*xl; 1.152 + l = xl*l+((m&0x1fff)<<13)+w_array[j]+c; 1.153 + c = (l>>26)+(m>>13)+xh*h; 1.154 + w_array[j++] = l&0x3ffffff; 1.155 + } 1.156 + return c; 1.157 +} 1.158 + 1.159 +// am3/28 is best for SM, Rhino, but am4/26 is best for v8. 1.160 +// Kestrel (Opera 9.5) gets its best result with am4/26. 1.161 +// IE7 does 9% better with am3/28 than with am4/26. 1.162 +// Firefox (SM) gets 10% faster with am3/28 than with am4/26. 1.163 + 1.164 +setupEngine = function(fn, bits) { 1.165 + BigInteger.prototype.am = fn; 1.166 + dbits = bits; 1.167 + 1.168 + BI_DB = dbits; 1.169 + BI_DM = ((1<<dbits)-1); 1.170 + BI_DV = (1<<dbits); 1.171 + 1.172 + BI_FP = 52; 1.173 + BI_FV = Math.pow(2,BI_FP); 1.174 + BI_F1 = BI_FP-dbits; 1.175 + BI_F2 = 2*dbits-BI_FP; 1.176 +} 1.177 + 1.178 + 1.179 +// Digit conversions 1.180 +var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz"; 1.181 +var BI_RC = new Array(); 1.182 +var rr,vv; 1.183 +rr = "0".charCodeAt(0); 1.184 +for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv; 1.185 +rr = "a".charCodeAt(0); 1.186 +for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv; 1.187 +rr = "A".charCodeAt(0); 1.188 +for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv; 1.189 + 1.190 +function int2char(n) { return BI_RM.charAt(n); } 1.191 +function intAt(s,i) { 1.192 + var c = BI_RC[s.charCodeAt(i)]; 1.193 + return (c==null)?-1:c; 1.194 +} 1.195 + 1.196 +// (protected) copy this to r 1.197 +function bnpCopyTo(r) { 1.198 + var this_array = this.array; 1.199 + var r_array = r.array; 1.200 + 1.201 + for(var i = this.t-1; i >= 0; --i) r_array[i] = this_array[i]; 1.202 + r.t = this.t; 1.203 + r.s = this.s; 1.204 +} 1.205 + 1.206 +// (protected) set from integer value x, -DV <= x < DV 1.207 +function bnpFromInt(x) { 1.208 + var this_array = this.array; 1.209 + this.t = 1; 1.210 + this.s = (x<0)?-1:0; 1.211 + if(x > 0) this_array[0] = x; 1.212 + else if(x < -1) this_array[0] = x+DV; 1.213 + else this.t = 0; 1.214 +} 1.215 + 1.216 +// return bigint initialized to value 1.217 +function nbv(i) { var r = nbi(); r.fromInt(i); return r; } 1.218 + 1.219 +// (protected) set from string and radix 1.220 +function bnpFromString(s,b) { 1.221 + var this_array = this.array; 1.222 + var k; 1.223 + if(b == 16) k = 4; 1.224 + else if(b == 8) k = 3; 1.225 + else if(b == 256) k = 8; // byte array 1.226 + else if(b == 2) k = 1; 1.227 + else if(b == 32) k = 5; 1.228 + else if(b == 4) k = 2; 1.229 + else { this.fromRadix(s,b); return; } 1.230 + this.t = 0; 1.231 + this.s = 0; 1.232 + var i = s.length, mi = false, sh = 0; 1.233 + while(--i >= 0) { 1.234 + var x = (k==8)?s[i]&0xff:intAt(s,i); 1.235 + if(x < 0) { 1.236 + if(s.charAt(i) == "-") mi = true; 1.237 + continue; 1.238 + } 1.239 + mi = false; 1.240 + if(sh == 0) 1.241 + this_array[this.t++] = x; 1.242 + else if(sh+k > BI_DB) { 1.243 + this_array[this.t-1] |= (x&((1<<(BI_DB-sh))-1))<<sh; 1.244 + this_array[this.t++] = (x>>(BI_DB-sh)); 1.245 + } 1.246 + else 1.247 + this_array[this.t-1] |= x<<sh; 1.248 + sh += k; 1.249 + if(sh >= BI_DB) sh -= BI_DB; 1.250 + } 1.251 + if(k == 8 && (s[0]&0x80) != 0) { 1.252 + this.s = -1; 1.253 + if(sh > 0) this_array[this.t-1] |= ((1<<(BI_DB-sh))-1)<<sh; 1.254 + } 1.255 + this.clamp(); 1.256 + if(mi) BigInteger.ZERO.subTo(this,this); 1.257 +} 1.258 + 1.259 +// (protected) clamp off excess high words 1.260 +function bnpClamp() { 1.261 + var this_array = this.array; 1.262 + var c = this.s&BI_DM; 1.263 + while(this.t > 0 && this_array[this.t-1] == c) --this.t; 1.264 +} 1.265 + 1.266 +// (public) return string representation in given radix 1.267 +function bnToString(b) { 1.268 + var this_array = this.array; 1.269 + if(this.s < 0) return "-"+this.negate().toString(b); 1.270 + var k; 1.271 + if(b == 16) k = 4; 1.272 + else if(b == 8) k = 3; 1.273 + else if(b == 2) k = 1; 1.274 + else if(b == 32) k = 5; 1.275 + else if(b == 4) k = 2; 1.276 + else return this.toRadix(b); 1.277 + var km = (1<<k)-1, d, m = false, r = "", i = this.t; 1.278 + var p = BI_DB-(i*BI_DB)%k; 1.279 + if(i-- > 0) { 1.280 + if(p < BI_DB && (d = this_array[i]>>p) > 0) { m = true; r = int2char(d); } 1.281 + while(i >= 0) { 1.282 + if(p < k) { 1.283 + d = (this_array[i]&((1<<p)-1))<<(k-p); 1.284 + d |= this_array[--i]>>(p+=BI_DB-k); 1.285 + } 1.286 + else { 1.287 + d = (this_array[i]>>(p-=k))&km; 1.288 + if(p <= 0) { p += BI_DB; --i; } 1.289 + } 1.290 + if(d > 0) m = true; 1.291 + if(m) r += int2char(d); 1.292 + } 1.293 + } 1.294 + return m?r:"0"; 1.295 +} 1.296 + 1.297 +// (public) -this 1.298 +function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; } 1.299 + 1.300 +// (public) |this| 1.301 +function bnAbs() { return (this.s<0)?this.negate():this; } 1.302 + 1.303 +// (public) return + if this > a, - if this < a, 0 if equal 1.304 +function bnCompareTo(a) { 1.305 + var this_array = this.array; 1.306 + var a_array = a.array; 1.307 + 1.308 + var r = this.s-a.s; 1.309 + if(r != 0) return r; 1.310 + var i = this.t; 1.311 + r = i-a.t; 1.312 + if(r != 0) return r; 1.313 + while(--i >= 0) if((r=this_array[i]-a_array[i]) != 0) return r; 1.314 + return 0; 1.315 +} 1.316 + 1.317 +// returns bit length of the integer x 1.318 +function nbits(x) { 1.319 + var r = 1, t; 1.320 + if((t=x>>>16) != 0) { x = t; r += 16; } 1.321 + if((t=x>>8) != 0) { x = t; r += 8; } 1.322 + if((t=x>>4) != 0) { x = t; r += 4; } 1.323 + if((t=x>>2) != 0) { x = t; r += 2; } 1.324 + if((t=x>>1) != 0) { x = t; r += 1; } 1.325 + return r; 1.326 +} 1.327 + 1.328 +// (public) return the number of bits in "this" 1.329 +function bnBitLength() { 1.330 + var this_array = this.array; 1.331 + if(this.t <= 0) return 0; 1.332 + return BI_DB*(this.t-1)+nbits(this_array[this.t-1]^(this.s&BI_DM)); 1.333 +} 1.334 + 1.335 +// (protected) r = this << n*DB 1.336 +function bnpDLShiftTo(n,r) { 1.337 + var this_array = this.array; 1.338 + var r_array = r.array; 1.339 + var i; 1.340 + for(i = this.t-1; i >= 0; --i) r_array[i+n] = this_array[i]; 1.341 + for(i = n-1; i >= 0; --i) r_array[i] = 0; 1.342 + r.t = this.t+n; 1.343 + r.s = this.s; 1.344 +} 1.345 + 1.346 +// (protected) r = this >> n*DB 1.347 +function bnpDRShiftTo(n,r) { 1.348 + var this_array = this.array; 1.349 + var r_array = r.array; 1.350 + for(var i = n; i < this.t; ++i) r_array[i-n] = this_array[i]; 1.351 + r.t = Math.max(this.t-n,0); 1.352 + r.s = this.s; 1.353 +} 1.354 + 1.355 +// (protected) r = this << n 1.356 +function bnpLShiftTo(n,r) { 1.357 + var this_array = this.array; 1.358 + var r_array = r.array; 1.359 + var bs = n%BI_DB; 1.360 + var cbs = BI_DB-bs; 1.361 + var bm = (1<<cbs)-1; 1.362 + var ds = Math.floor(n/BI_DB), c = (this.s<<bs)&BI_DM, i; 1.363 + for(i = this.t-1; i >= 0; --i) { 1.364 + r_array[i+ds+1] = (this_array[i]>>cbs)|c; 1.365 + c = (this_array[i]&bm)<<bs; 1.366 + } 1.367 + for(i = ds-1; i >= 0; --i) r_array[i] = 0; 1.368 + r_array[ds] = c; 1.369 + r.t = this.t+ds+1; 1.370 + r.s = this.s; 1.371 + r.clamp(); 1.372 +} 1.373 + 1.374 +// (protected) r = this >> n 1.375 +function bnpRShiftTo(n,r) { 1.376 + var this_array = this.array; 1.377 + var r_array = r.array; 1.378 + r.s = this.s; 1.379 + var ds = Math.floor(n/BI_DB); 1.380 + if(ds >= this.t) { r.t = 0; return; } 1.381 + var bs = n%BI_DB; 1.382 + var cbs = BI_DB-bs; 1.383 + var bm = (1<<bs)-1; 1.384 + r_array[0] = this_array[ds]>>bs; 1.385 + for(var i = ds+1; i < this.t; ++i) { 1.386 + r_array[i-ds-1] |= (this_array[i]&bm)<<cbs; 1.387 + r_array[i-ds] = this_array[i]>>bs; 1.388 + } 1.389 + if(bs > 0) r_array[this.t-ds-1] |= (this.s&bm)<<cbs; 1.390 + r.t = this.t-ds; 1.391 + r.clamp(); 1.392 +} 1.393 + 1.394 +// (protected) r = this - a 1.395 +function bnpSubTo(a,r) { 1.396 + var this_array = this.array; 1.397 + var r_array = r.array; 1.398 + var a_array = a.array; 1.399 + var i = 0, c = 0, m = Math.min(a.t,this.t); 1.400 + while(i < m) { 1.401 + c += this_array[i]-a_array[i]; 1.402 + r_array[i++] = c&BI_DM; 1.403 + c >>= BI_DB; 1.404 + } 1.405 + if(a.t < this.t) { 1.406 + c -= a.s; 1.407 + while(i < this.t) { 1.408 + c += this_array[i]; 1.409 + r_array[i++] = c&BI_DM; 1.410 + c >>= BI_DB; 1.411 + } 1.412 + c += this.s; 1.413 + } 1.414 + else { 1.415 + c += this.s; 1.416 + while(i < a.t) { 1.417 + c -= a_array[i]; 1.418 + r_array[i++] = c&BI_DM; 1.419 + c >>= BI_DB; 1.420 + } 1.421 + c -= a.s; 1.422 + } 1.423 + r.s = (c<0)?-1:0; 1.424 + if(c < -1) r_array[i++] = BI_DV+c; 1.425 + else if(c > 0) r_array[i++] = c; 1.426 + r.t = i; 1.427 + r.clamp(); 1.428 +} 1.429 + 1.430 +// (protected) r = this * a, r != this,a (HAC 14.12) 1.431 +// "this" should be the larger one if appropriate. 1.432 +function bnpMultiplyTo(a,r) { 1.433 + var this_array = this.array; 1.434 + var r_array = r.array; 1.435 + var x = this.abs(), y = a.abs(); 1.436 + var y_array = y.array; 1.437 + 1.438 + var i = x.t; 1.439 + r.t = i+y.t; 1.440 + while(--i >= 0) r_array[i] = 0; 1.441 + for(i = 0; i < y.t; ++i) r_array[i+x.t] = x.am(0,y_array[i],r,i,0,x.t); 1.442 + r.s = 0; 1.443 + r.clamp(); 1.444 + if(this.s != a.s) BigInteger.ZERO.subTo(r,r); 1.445 +} 1.446 + 1.447 +// (protected) r = this^2, r != this (HAC 14.16) 1.448 +function bnpSquareTo(r) { 1.449 + var x = this.abs(); 1.450 + var x_array = x.array; 1.451 + var r_array = r.array; 1.452 + 1.453 + var i = r.t = 2*x.t; 1.454 + while(--i >= 0) r_array[i] = 0; 1.455 + for(i = 0; i < x.t-1; ++i) { 1.456 + var c = x.am(i,x_array[i],r,2*i,0,1); 1.457 + 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) { 1.458 + r_array[i+x.t] -= BI_DV; 1.459 + r_array[i+x.t+1] = 1; 1.460 + } 1.461 + } 1.462 + if(r.t > 0) r_array[r.t-1] += x.am(i,x_array[i],r,2*i,0,1); 1.463 + r.s = 0; 1.464 + r.clamp(); 1.465 +} 1.466 + 1.467 +// (protected) divide this by m, quotient and remainder to q, r (HAC 14.20) 1.468 +// r != q, this != m. q or r may be null. 1.469 +function bnpDivRemTo(m,q,r) { 1.470 + var pm = m.abs(); 1.471 + if(pm.t <= 0) return; 1.472 + var pt = this.abs(); 1.473 + if(pt.t < pm.t) { 1.474 + if(q != null) q.fromInt(0); 1.475 + if(r != null) this.copyTo(r); 1.476 + return; 1.477 + } 1.478 + if(r == null) r = nbi(); 1.479 + var y = nbi(), ts = this.s, ms = m.s; 1.480 + var pm_array = pm.array; 1.481 + var nsh = BI_DB-nbits(pm_array[pm.t-1]); // normalize modulus 1.482 + if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); } 1.483 + else { pm.copyTo(y); pt.copyTo(r); } 1.484 + var ys = y.t; 1.485 + 1.486 + var y_array = y.array; 1.487 + var y0 = y_array[ys-1]; 1.488 + if(y0 == 0) return; 1.489 + var yt = y0*(1<<BI_F1)+((ys>1)?y_array[ys-2]>>BI_F2:0); 1.490 + var d1 = BI_FV/yt, d2 = (1<<BI_F1)/yt, e = 1<<BI_F2; 1.491 + var i = r.t, j = i-ys, t = (q==null)?nbi():q; 1.492 + y.dlShiftTo(j,t); 1.493 + 1.494 + var r_array = r.array; 1.495 + if(r.compareTo(t) >= 0) { 1.496 + r_array[r.t++] = 1; 1.497 + r.subTo(t,r); 1.498 + } 1.499 + BigInteger.ONE.dlShiftTo(ys,t); 1.500 + t.subTo(y,y); // "negative" y so we can replace sub with am later 1.501 + while(y.t < ys) y_array[y.t++] = 0; 1.502 + while(--j >= 0) { 1.503 + // Estimate quotient digit 1.504 + var qd = (r_array[--i]==y0)?BI_DM:Math.floor(r_array[i]*d1+(r_array[i-1]+e)*d2); 1.505 + if((r_array[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out 1.506 + y.dlShiftTo(j,t); 1.507 + r.subTo(t,r); 1.508 + while(r_array[i] < --qd) r.subTo(t,r); 1.509 + } 1.510 + } 1.511 + if(q != null) { 1.512 + r.drShiftTo(ys,q); 1.513 + if(ts != ms) BigInteger.ZERO.subTo(q,q); 1.514 + } 1.515 + r.t = ys; 1.516 + r.clamp(); 1.517 + if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder 1.518 + if(ts < 0) BigInteger.ZERO.subTo(r,r); 1.519 +} 1.520 + 1.521 +// (public) this mod a 1.522 +function bnMod(a) { 1.523 + var r = nbi(); 1.524 + this.abs().divRemTo(a,null,r); 1.525 + if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r); 1.526 + return r; 1.527 +} 1.528 + 1.529 +// Modular reduction using "classic" algorithm 1.530 +function Classic(m) { this.m = m; } 1.531 +function cConvert(x) { 1.532 + if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m); 1.533 + else return x; 1.534 +} 1.535 +function cRevert(x) { return x; } 1.536 +function cReduce(x) { x.divRemTo(this.m,null,x); } 1.537 +function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } 1.538 +function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); } 1.539 + 1.540 +Classic.prototype.convert = cConvert; 1.541 +Classic.prototype.revert = cRevert; 1.542 +Classic.prototype.reduce = cReduce; 1.543 +Classic.prototype.mulTo = cMulTo; 1.544 +Classic.prototype.sqrTo = cSqrTo; 1.545 + 1.546 +// (protected) return "-1/this % 2^DB"; useful for Mont. reduction 1.547 +// justification: 1.548 +// xy == 1 (mod m) 1.549 +// xy = 1+km 1.550 +// xy(2-xy) = (1+km)(1-km) 1.551 +// x[y(2-xy)] = 1-k^2m^2 1.552 +// x[y(2-xy)] == 1 (mod m^2) 1.553 +// if y is 1/x mod m, then y(2-xy) is 1/x mod m^2 1.554 +// should reduce x and y(2-xy) by m^2 at each step to keep size bounded. 1.555 +// JS multiply "overflows" differently from C/C++, so care is needed here. 1.556 +function bnpInvDigit() { 1.557 + var this_array = this.array; 1.558 + if(this.t < 1) return 0; 1.559 + var x = this_array[0]; 1.560 + if((x&1) == 0) return 0; 1.561 + var y = x&3; // y == 1/x mod 2^2 1.562 + y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4 1.563 + y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8 1.564 + y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16 1.565 + // last step - calculate inverse mod DV directly; 1.566 + // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints 1.567 + y = (y*(2-x*y%BI_DV))%BI_DV; // y == 1/x mod 2^dbits 1.568 + // we really want the negative inverse, and -DV < y < DV 1.569 + return (y>0)?BI_DV-y:-y; 1.570 +} 1.571 + 1.572 +// Montgomery reduction 1.573 +function Montgomery(m) { 1.574 + this.m = m; 1.575 + this.mp = m.invDigit(); 1.576 + this.mpl = this.mp&0x7fff; 1.577 + this.mph = this.mp>>15; 1.578 + this.um = (1<<(BI_DB-15))-1; 1.579 + this.mt2 = 2*m.t; 1.580 +} 1.581 + 1.582 +// xR mod m 1.583 +function montConvert(x) { 1.584 + var r = nbi(); 1.585 + x.abs().dlShiftTo(this.m.t,r); 1.586 + r.divRemTo(this.m,null,r); 1.587 + if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r); 1.588 + return r; 1.589 +} 1.590 + 1.591 +// x/R mod m 1.592 +function montRevert(x) { 1.593 + var r = nbi(); 1.594 + x.copyTo(r); 1.595 + this.reduce(r); 1.596 + return r; 1.597 +} 1.598 + 1.599 +// x = x/R mod m (HAC 14.32) 1.600 +function montReduce(x) { 1.601 + var x_array = x.array; 1.602 + while(x.t <= this.mt2) // pad x so am has enough room later 1.603 + x_array[x.t++] = 0; 1.604 + for(var i = 0; i < this.m.t; ++i) { 1.605 + // faster way of calculating u0 = x[i]*mp mod DV 1.606 + var j = x_array[i]&0x7fff; 1.607 + var u0 = (j*this.mpl+(((j*this.mph+(x_array[i]>>15)*this.mpl)&this.um)<<15))&BI_DM; 1.608 + // use am to combine the multiply-shift-add into one call 1.609 + j = i+this.m.t; 1.610 + x_array[j] += this.m.am(0,u0,x,i,0,this.m.t); 1.611 + // propagate carry 1.612 + while(x_array[j] >= BI_DV) { x_array[j] -= BI_DV; x_array[++j]++; } 1.613 + } 1.614 + x.clamp(); 1.615 + x.drShiftTo(this.m.t,x); 1.616 + if(x.compareTo(this.m) >= 0) x.subTo(this.m,x); 1.617 +} 1.618 + 1.619 +// r = "x^2/R mod m"; x != r 1.620 +function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); } 1.621 + 1.622 +// r = "xy/R mod m"; x,y != r 1.623 +function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } 1.624 + 1.625 +Montgomery.prototype.convert = montConvert; 1.626 +Montgomery.prototype.revert = montRevert; 1.627 +Montgomery.prototype.reduce = montReduce; 1.628 +Montgomery.prototype.mulTo = montMulTo; 1.629 +Montgomery.prototype.sqrTo = montSqrTo; 1.630 + 1.631 +// (protected) true iff this is even 1.632 +function bnpIsEven() { 1.633 + var this_array = this.array; 1.634 + return ((this.t>0)?(this_array[0]&1):this.s) == 0; 1.635 +} 1.636 + 1.637 +// (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79) 1.638 +function bnpExp(e,z) { 1.639 + if(e > 0xffffffff || e < 1) return BigInteger.ONE; 1.640 + var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1; 1.641 + g.copyTo(r); 1.642 + while(--i >= 0) { 1.643 + z.sqrTo(r,r2); 1.644 + if((e&(1<<i)) > 0) z.mulTo(r2,g,r); 1.645 + else { var t = r; r = r2; r2 = t; } 1.646 + } 1.647 + return z.revert(r); 1.648 +} 1.649 + 1.650 +// (public) this^e % m, 0 <= e < 2^32 1.651 +function bnModPowInt(e,m) { 1.652 + var z; 1.653 + if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m); 1.654 + return this.exp(e,z); 1.655 +} 1.656 + 1.657 +// protected 1.658 +BigInteger.prototype.copyTo = bnpCopyTo; 1.659 +BigInteger.prototype.fromInt = bnpFromInt; 1.660 +BigInteger.prototype.fromString = bnpFromString; 1.661 +BigInteger.prototype.clamp = bnpClamp; 1.662 +BigInteger.prototype.dlShiftTo = bnpDLShiftTo; 1.663 +BigInteger.prototype.drShiftTo = bnpDRShiftTo; 1.664 +BigInteger.prototype.lShiftTo = bnpLShiftTo; 1.665 +BigInteger.prototype.rShiftTo = bnpRShiftTo; 1.666 +BigInteger.prototype.subTo = bnpSubTo; 1.667 +BigInteger.prototype.multiplyTo = bnpMultiplyTo; 1.668 +BigInteger.prototype.squareTo = bnpSquareTo; 1.669 +BigInteger.prototype.divRemTo = bnpDivRemTo; 1.670 +BigInteger.prototype.invDigit = bnpInvDigit; 1.671 +BigInteger.prototype.isEven = bnpIsEven; 1.672 +BigInteger.prototype.exp = bnpExp; 1.673 + 1.674 +// public 1.675 +BigInteger.prototype.toString = bnToString; 1.676 +BigInteger.prototype.negate = bnNegate; 1.677 +BigInteger.prototype.abs = bnAbs; 1.678 +BigInteger.prototype.compareTo = bnCompareTo; 1.679 +BigInteger.prototype.bitLength = bnBitLength; 1.680 +BigInteger.prototype.mod = bnMod; 1.681 +BigInteger.prototype.modPowInt = bnModPowInt; 1.682 + 1.683 +// "constants" 1.684 +BigInteger.ZERO = nbv(0); 1.685 +BigInteger.ONE = nbv(1); 1.686 +// Copyright (c) 2005 Tom Wu 1.687 +// All Rights Reserved. 1.688 +// See "LICENSE" for details. 1.689 + 1.690 +// Extended JavaScript BN functions, required for RSA private ops. 1.691 + 1.692 +// (public) 1.693 +function bnClone() { var r = nbi(); this.copyTo(r); return r; } 1.694 + 1.695 +// (public) return value as integer 1.696 +function bnIntValue() { 1.697 + var this_array = this.array; 1.698 + if(this.s < 0) { 1.699 + if(this.t == 1) return this_array[0]-BI_DV; 1.700 + else if(this.t == 0) return -1; 1.701 + } 1.702 + else if(this.t == 1) return this_array[0]; 1.703 + else if(this.t == 0) return 0; 1.704 + // assumes 16 < DB < 32 1.705 + return ((this_array[1]&((1<<(32-BI_DB))-1))<<BI_DB)|this_array[0]; 1.706 +} 1.707 + 1.708 +// (public) return value as byte 1.709 +function bnByteValue() { 1.710 + var this_array = this.array; 1.711 + return (this.t==0)?this.s:(this_array[0]<<24)>>24; 1.712 +} 1.713 + 1.714 +// (public) return value as short (assumes DB>=16) 1.715 +function bnShortValue() { 1.716 + var this_array = this.array; 1.717 + return (this.t==0)?this.s:(this_array[0]<<16)>>16; 1.718 +} 1.719 + 1.720 +// (protected) return x s.t. r^x < DV 1.721 +function bnpChunkSize(r) { return Math.floor(Math.LN2*BI_DB/Math.log(r)); } 1.722 + 1.723 +// (public) 0 if this == 0, 1 if this > 0 1.724 +function bnSigNum() { 1.725 + var this_array = this.array; 1.726 + if(this.s < 0) return -1; 1.727 + else if(this.t <= 0 || (this.t == 1 && this_array[0] <= 0)) return 0; 1.728 + else return 1; 1.729 +} 1.730 + 1.731 +// (protected) convert to radix string 1.732 +function bnpToRadix(b) { 1.733 + if(b == null) b = 10; 1.734 + if(this.signum() == 0 || b < 2 || b > 36) return "0"; 1.735 + var cs = this.chunkSize(b); 1.736 + var a = Math.pow(b,cs); 1.737 + var d = nbv(a), y = nbi(), z = nbi(), r = ""; 1.738 + this.divRemTo(d,y,z); 1.739 + while(y.signum() > 0) { 1.740 + r = (a+z.intValue()).toString(b).substr(1) + r; 1.741 + y.divRemTo(d,y,z); 1.742 + } 1.743 + return z.intValue().toString(b) + r; 1.744 +} 1.745 + 1.746 +// (protected) convert from radix string 1.747 +function bnpFromRadix(s,b) { 1.748 + this.fromInt(0); 1.749 + if(b == null) b = 10; 1.750 + var cs = this.chunkSize(b); 1.751 + var d = Math.pow(b,cs), mi = false, j = 0, w = 0; 1.752 + for(var i = 0; i < s.length; ++i) { 1.753 + var x = intAt(s,i); 1.754 + if(x < 0) { 1.755 + if(s.charAt(i) == "-" && this.signum() == 0) mi = true; 1.756 + continue; 1.757 + } 1.758 + w = b*w+x; 1.759 + if(++j >= cs) { 1.760 + this.dMultiply(d); 1.761 + this.dAddOffset(w,0); 1.762 + j = 0; 1.763 + w = 0; 1.764 + } 1.765 + } 1.766 + if(j > 0) { 1.767 + this.dMultiply(Math.pow(b,j)); 1.768 + this.dAddOffset(w,0); 1.769 + } 1.770 + if(mi) BigInteger.ZERO.subTo(this,this); 1.771 +} 1.772 + 1.773 +// (protected) alternate constructor 1.774 +function bnpFromNumber(a,b,c) { 1.775 + if("number" == typeof b) { 1.776 + // new BigInteger(int,int,RNG) 1.777 + if(a < 2) this.fromInt(1); 1.778 + else { 1.779 + this.fromNumber(a,c); 1.780 + if(!this.testBit(a-1)) // force MSB set 1.781 + this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this); 1.782 + if(this.isEven()) this.dAddOffset(1,0); // force odd 1.783 + while(!this.isProbablePrime(b)) { 1.784 + this.dAddOffset(2,0); 1.785 + if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this); 1.786 + } 1.787 + } 1.788 + } 1.789 + else { 1.790 + // new BigInteger(int,RNG) 1.791 + var x = new Array(), t = a&7; 1.792 + x.length = (a>>3)+1; 1.793 + b.nextBytes(x); 1.794 + if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0; 1.795 + this.fromString(x,256); 1.796 + } 1.797 +} 1.798 + 1.799 +// (public) convert to bigendian byte array 1.800 +function bnToByteArray() { 1.801 + var this_array = this.array; 1.802 + var i = this.t, r = new Array(); 1.803 + r[0] = this.s; 1.804 + var p = BI_DB-(i*BI_DB)%8, d, k = 0; 1.805 + if(i-- > 0) { 1.806 + if(p < BI_DB && (d = this_array[i]>>p) != (this.s&BI_DM)>>p) 1.807 + r[k++] = d|(this.s<<(BI_DB-p)); 1.808 + while(i >= 0) { 1.809 + if(p < 8) { 1.810 + d = (this_array[i]&((1<<p)-1))<<(8-p); 1.811 + d |= this_array[--i]>>(p+=BI_DB-8); 1.812 + } 1.813 + else { 1.814 + d = (this_array[i]>>(p-=8))&0xff; 1.815 + if(p <= 0) { p += BI_DB; --i; } 1.816 + } 1.817 + if((d&0x80) != 0) d |= -256; 1.818 + if(k == 0 && (this.s&0x80) != (d&0x80)) ++k; 1.819 + if(k > 0 || d != this.s) r[k++] = d; 1.820 + } 1.821 + } 1.822 + return r; 1.823 +} 1.824 + 1.825 +function bnEquals(a) { return(this.compareTo(a)==0); } 1.826 +function bnMin(a) { return(this.compareTo(a)<0)?this:a; } 1.827 +function bnMax(a) { return(this.compareTo(a)>0)?this:a; } 1.828 + 1.829 +// (protected) r = this op a (bitwise) 1.830 +function bnpBitwiseTo(a,op,r) { 1.831 + var this_array = this.array; 1.832 + var a_array = a.array; 1.833 + var r_array = r.array; 1.834 + var i, f, m = Math.min(a.t,this.t); 1.835 + for(i = 0; i < m; ++i) r_array[i] = op(this_array[i],a_array[i]); 1.836 + if(a.t < this.t) { 1.837 + f = a.s&BI_DM; 1.838 + for(i = m; i < this.t; ++i) r_array[i] = op(this_array[i],f); 1.839 + r.t = this.t; 1.840 + } 1.841 + else { 1.842 + f = this.s&BI_DM; 1.843 + for(i = m; i < a.t; ++i) r_array[i] = op(f,a_array[i]); 1.844 + r.t = a.t; 1.845 + } 1.846 + r.s = op(this.s,a.s); 1.847 + r.clamp(); 1.848 +} 1.849 + 1.850 +// (public) this & a 1.851 +function op_and(x,y) { return x&y; } 1.852 +function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; } 1.853 + 1.854 +// (public) this | a 1.855 +function op_or(x,y) { return x|y; } 1.856 +function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; } 1.857 + 1.858 +// (public) this ^ a 1.859 +function op_xor(x,y) { return x^y; } 1.860 +function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; } 1.861 + 1.862 +// (public) this & ~a 1.863 +function op_andnot(x,y) { return x&~y; } 1.864 +function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; } 1.865 + 1.866 +// (public) ~this 1.867 +function bnNot() { 1.868 + var this_array = this.array; 1.869 + var r = nbi(); 1.870 + var r_array = r.array; 1.871 + 1.872 + for(var i = 0; i < this.t; ++i) r_array[i] = BI_DM&~this_array[i]; 1.873 + r.t = this.t; 1.874 + r.s = ~this.s; 1.875 + return r; 1.876 +} 1.877 + 1.878 +// (public) this << n 1.879 +function bnShiftLeft(n) { 1.880 + var r = nbi(); 1.881 + if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r); 1.882 + return r; 1.883 +} 1.884 + 1.885 +// (public) this >> n 1.886 +function bnShiftRight(n) { 1.887 + var r = nbi(); 1.888 + if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r); 1.889 + return r; 1.890 +} 1.891 + 1.892 +// return index of lowest 1-bit in x, x < 2^31 1.893 +function lbit(x) { 1.894 + if(x == 0) return -1; 1.895 + var r = 0; 1.896 + if((x&0xffff) == 0) { x >>= 16; r += 16; } 1.897 + if((x&0xff) == 0) { x >>= 8; r += 8; } 1.898 + if((x&0xf) == 0) { x >>= 4; r += 4; } 1.899 + if((x&3) == 0) { x >>= 2; r += 2; } 1.900 + if((x&1) == 0) ++r; 1.901 + return r; 1.902 +} 1.903 + 1.904 +// (public) returns index of lowest 1-bit (or -1 if none) 1.905 +function bnGetLowestSetBit() { 1.906 + var this_array = this.array; 1.907 + for(var i = 0; i < this.t; ++i) 1.908 + if(this_array[i] != 0) return i*BI_DB+lbit(this_array[i]); 1.909 + if(this.s < 0) return this.t*BI_DB; 1.910 + return -1; 1.911 +} 1.912 + 1.913 +// return number of 1 bits in x 1.914 +function cbit(x) { 1.915 + var r = 0; 1.916 + while(x != 0) { x &= x-1; ++r; } 1.917 + return r; 1.918 +} 1.919 + 1.920 +// (public) return number of set bits 1.921 +function bnBitCount() { 1.922 + var r = 0, x = this.s&BI_DM; 1.923 + for(var i = 0; i < this.t; ++i) r += cbit(this_array[i]^x); 1.924 + return r; 1.925 +} 1.926 + 1.927 +// (public) true iff nth bit is set 1.928 +function bnTestBit(n) { 1.929 + var this_array = this.array; 1.930 + var j = Math.floor(n/BI_DB); 1.931 + if(j >= this.t) return(this.s!=0); 1.932 + return((this_array[j]&(1<<(n%BI_DB)))!=0); 1.933 +} 1.934 + 1.935 +// (protected) this op (1<<n) 1.936 +function bnpChangeBit(n,op) { 1.937 + var r = BigInteger.ONE.shiftLeft(n); 1.938 + this.bitwiseTo(r,op,r); 1.939 + return r; 1.940 +} 1.941 + 1.942 +// (public) this | (1<<n) 1.943 +function bnSetBit(n) { return this.changeBit(n,op_or); } 1.944 + 1.945 +// (public) this & ~(1<<n) 1.946 +function bnClearBit(n) { return this.changeBit(n,op_andnot); } 1.947 + 1.948 +// (public) this ^ (1<<n) 1.949 +function bnFlipBit(n) { return this.changeBit(n,op_xor); } 1.950 + 1.951 +// (protected) r = this + a 1.952 +function bnpAddTo(a,r) { 1.953 + var this_array = this.array; 1.954 + var a_array = a.array; 1.955 + var r_array = r.array; 1.956 + var i = 0, c = 0, m = Math.min(a.t,this.t); 1.957 + while(i < m) { 1.958 + c += this_array[i]+a_array[i]; 1.959 + r_array[i++] = c&BI_DM; 1.960 + c >>= BI_DB; 1.961 + } 1.962 + if(a.t < this.t) { 1.963 + c += a.s; 1.964 + while(i < this.t) { 1.965 + c += this_array[i]; 1.966 + r_array[i++] = c&BI_DM; 1.967 + c >>= BI_DB; 1.968 + } 1.969 + c += this.s; 1.970 + } 1.971 + else { 1.972 + c += this.s; 1.973 + while(i < a.t) { 1.974 + c += a_array[i]; 1.975 + r_array[i++] = c&BI_DM; 1.976 + c >>= BI_DB; 1.977 + } 1.978 + c += a.s; 1.979 + } 1.980 + r.s = (c<0)?-1:0; 1.981 + if(c > 0) r_array[i++] = c; 1.982 + else if(c < -1) r_array[i++] = BI_DV+c; 1.983 + r.t = i; 1.984 + r.clamp(); 1.985 +} 1.986 + 1.987 +// (public) this + a 1.988 +function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; } 1.989 + 1.990 +// (public) this - a 1.991 +function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; } 1.992 + 1.993 +// (public) this * a 1.994 +function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; } 1.995 + 1.996 +// (public) this / a 1.997 +function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; } 1.998 + 1.999 +// (public) this % a 1.1000 +function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; } 1.1001 + 1.1002 +// (public) [this/a,this%a] 1.1003 +function bnDivideAndRemainder(a) { 1.1004 + var q = nbi(), r = nbi(); 1.1005 + this.divRemTo(a,q,r); 1.1006 + return new Array(q,r); 1.1007 +} 1.1008 + 1.1009 +// (protected) this *= n, this >= 0, 1 < n < DV 1.1010 +function bnpDMultiply(n) { 1.1011 + var this_array = this.array; 1.1012 + this_array[this.t] = this.am(0,n-1,this,0,0,this.t); 1.1013 + ++this.t; 1.1014 + this.clamp(); 1.1015 +} 1.1016 + 1.1017 +// (protected) this += n << w words, this >= 0 1.1018 +function bnpDAddOffset(n,w) { 1.1019 + var this_array = this.array; 1.1020 + while(this.t <= w) this_array[this.t++] = 0; 1.1021 + this_array[w] += n; 1.1022 + while(this_array[w] >= BI_DV) { 1.1023 + this_array[w] -= BI_DV; 1.1024 + if(++w >= this.t) this_array[this.t++] = 0; 1.1025 + ++this_array[w]; 1.1026 + } 1.1027 +} 1.1028 + 1.1029 +// A "null" reducer 1.1030 +function NullExp() {} 1.1031 +function nNop(x) { return x; } 1.1032 +function nMulTo(x,y,r) { x.multiplyTo(y,r); } 1.1033 +function nSqrTo(x,r) { x.squareTo(r); } 1.1034 + 1.1035 +NullExp.prototype.convert = nNop; 1.1036 +NullExp.prototype.revert = nNop; 1.1037 +NullExp.prototype.mulTo = nMulTo; 1.1038 +NullExp.prototype.sqrTo = nSqrTo; 1.1039 + 1.1040 +// (public) this^e 1.1041 +function bnPow(e) { return this.exp(e,new NullExp()); } 1.1042 + 1.1043 +// (protected) r = lower n words of "this * a", a.t <= n 1.1044 +// "this" should be the larger one if appropriate. 1.1045 +function bnpMultiplyLowerTo(a,n,r) { 1.1046 + var r_array = r.array; 1.1047 + var a_array = a.array; 1.1048 + var i = Math.min(this.t+a.t,n); 1.1049 + r.s = 0; // assumes a,this >= 0 1.1050 + r.t = i; 1.1051 + while(i > 0) r_array[--i] = 0; 1.1052 + var j; 1.1053 + 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); 1.1054 + for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a_array[i],r,i,0,n-i); 1.1055 + r.clamp(); 1.1056 +} 1.1057 + 1.1058 +// (protected) r = "this * a" without lower n words, n > 0 1.1059 +// "this" should be the larger one if appropriate. 1.1060 +function bnpMultiplyUpperTo(a,n,r) { 1.1061 + var r_array = r.array; 1.1062 + var a_array = a.array; 1.1063 + --n; 1.1064 + var i = r.t = this.t+a.t-n; 1.1065 + r.s = 0; // assumes a,this >= 0 1.1066 + while(--i >= 0) r_array[i] = 0; 1.1067 + for(i = Math.max(n-this.t,0); i < a.t; ++i) 1.1068 + r_array[this.t+i-n] = this.am(n-i,a_array[i],r,0,0,this.t+i-n); 1.1069 + r.clamp(); 1.1070 + r.drShiftTo(1,r); 1.1071 +} 1.1072 + 1.1073 +// Barrett modular reduction 1.1074 +function Barrett(m) { 1.1075 + // setup Barrett 1.1076 + this.r2 = nbi(); 1.1077 + this.q3 = nbi(); 1.1078 + BigInteger.ONE.dlShiftTo(2*m.t,this.r2); 1.1079 + this.mu = this.r2.divide(m); 1.1080 + this.m = m; 1.1081 +} 1.1082 + 1.1083 +function barrettConvert(x) { 1.1084 + if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m); 1.1085 + else if(x.compareTo(this.m) < 0) return x; 1.1086 + else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; } 1.1087 +} 1.1088 + 1.1089 +function barrettRevert(x) { return x; } 1.1090 + 1.1091 +// x = x mod m (HAC 14.42) 1.1092 +function barrettReduce(x) { 1.1093 + x.drShiftTo(this.m.t-1,this.r2); 1.1094 + if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); } 1.1095 + this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3); 1.1096 + this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2); 1.1097 + while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1); 1.1098 + x.subTo(this.r2,x); 1.1099 + while(x.compareTo(this.m) >= 0) x.subTo(this.m,x); 1.1100 +} 1.1101 + 1.1102 +// r = x^2 mod m; x != r 1.1103 +function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); } 1.1104 + 1.1105 +// r = x*y mod m; x,y != r 1.1106 +function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } 1.1107 + 1.1108 +Barrett.prototype.convert = barrettConvert; 1.1109 +Barrett.prototype.revert = barrettRevert; 1.1110 +Barrett.prototype.reduce = barrettReduce; 1.1111 +Barrett.prototype.mulTo = barrettMulTo; 1.1112 +Barrett.prototype.sqrTo = barrettSqrTo; 1.1113 + 1.1114 +// (public) this^e % m (HAC 14.85) 1.1115 +function bnModPow(e,m) { 1.1116 + var e_array = e.array; 1.1117 + var i = e.bitLength(), k, r = nbv(1), z; 1.1118 + if(i <= 0) return r; 1.1119 + else if(i < 18) k = 1; 1.1120 + else if(i < 48) k = 3; 1.1121 + else if(i < 144) k = 4; 1.1122 + else if(i < 768) k = 5; 1.1123 + else k = 6; 1.1124 + if(i < 8) 1.1125 + z = new Classic(m); 1.1126 + else if(m.isEven()) 1.1127 + z = new Barrett(m); 1.1128 + else 1.1129 + z = new Montgomery(m); 1.1130 + 1.1131 + // precomputation 1.1132 + var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1; 1.1133 + g[1] = z.convert(this); 1.1134 + if(k > 1) { 1.1135 + var g2 = nbi(); 1.1136 + z.sqrTo(g[1],g2); 1.1137 + while(n <= km) { 1.1138 + g[n] = nbi(); 1.1139 + z.mulTo(g2,g[n-2],g[n]); 1.1140 + n += 2; 1.1141 + } 1.1142 + } 1.1143 + 1.1144 + var j = e.t-1, w, is1 = true, r2 = nbi(), t; 1.1145 + i = nbits(e_array[j])-1; 1.1146 + while(j >= 0) { 1.1147 + if(i >= k1) w = (e_array[j]>>(i-k1))&km; 1.1148 + else { 1.1149 + w = (e_array[j]&((1<<(i+1))-1))<<(k1-i); 1.1150 + if(j > 0) w |= e_array[j-1]>>(BI_DB+i-k1); 1.1151 + } 1.1152 + 1.1153 + n = k; 1.1154 + while((w&1) == 0) { w >>= 1; --n; } 1.1155 + if((i -= n) < 0) { i += BI_DB; --j; } 1.1156 + if(is1) { // ret == 1, don't bother squaring or multiplying it 1.1157 + g[w].copyTo(r); 1.1158 + is1 = false; 1.1159 + } 1.1160 + else { 1.1161 + while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; } 1.1162 + if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; } 1.1163 + z.mulTo(r2,g[w],r); 1.1164 + } 1.1165 + 1.1166 + while(j >= 0 && (e_array[j]&(1<<i)) == 0) { 1.1167 + z.sqrTo(r,r2); t = r; r = r2; r2 = t; 1.1168 + if(--i < 0) { i = BI_DB-1; --j; } 1.1169 + } 1.1170 + } 1.1171 + return z.revert(r); 1.1172 +} 1.1173 + 1.1174 +// (public) gcd(this,a) (HAC 14.54) 1.1175 +function bnGCD(a) { 1.1176 + var x = (this.s<0)?this.negate():this.clone(); 1.1177 + var y = (a.s<0)?a.negate():a.clone(); 1.1178 + if(x.compareTo(y) < 0) { var t = x; x = y; y = t; } 1.1179 + var i = x.getLowestSetBit(), g = y.getLowestSetBit(); 1.1180 + if(g < 0) return x; 1.1181 + if(i < g) g = i; 1.1182 + if(g > 0) { 1.1183 + x.rShiftTo(g,x); 1.1184 + y.rShiftTo(g,y); 1.1185 + } 1.1186 + while(x.signum() > 0) { 1.1187 + if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x); 1.1188 + if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y); 1.1189 + if(x.compareTo(y) >= 0) { 1.1190 + x.subTo(y,x); 1.1191 + x.rShiftTo(1,x); 1.1192 + } 1.1193 + else { 1.1194 + y.subTo(x,y); 1.1195 + y.rShiftTo(1,y); 1.1196 + } 1.1197 + } 1.1198 + if(g > 0) y.lShiftTo(g,y); 1.1199 + return y; 1.1200 +} 1.1201 + 1.1202 +// (protected) this % n, n < 2^26 1.1203 +function bnpModInt(n) { 1.1204 + var this_array = this.array; 1.1205 + if(n <= 0) return 0; 1.1206 + var d = BI_DV%n, r = (this.s<0)?n-1:0; 1.1207 + if(this.t > 0) 1.1208 + if(d == 0) r = this_array[0]%n; 1.1209 + else for(var i = this.t-1; i >= 0; --i) r = (d*r+this_array[i])%n; 1.1210 + return r; 1.1211 +} 1.1212 + 1.1213 +// (public) 1/this % m (HAC 14.61) 1.1214 +function bnModInverse(m) { 1.1215 + var ac = m.isEven(); 1.1216 + if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO; 1.1217 + var u = m.clone(), v = this.clone(); 1.1218 + var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1); 1.1219 + while(u.signum() != 0) { 1.1220 + while(u.isEven()) { 1.1221 + u.rShiftTo(1,u); 1.1222 + if(ac) { 1.1223 + if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); } 1.1224 + a.rShiftTo(1,a); 1.1225 + } 1.1226 + else if(!b.isEven()) b.subTo(m,b); 1.1227 + b.rShiftTo(1,b); 1.1228 + } 1.1229 + while(v.isEven()) { 1.1230 + v.rShiftTo(1,v); 1.1231 + if(ac) { 1.1232 + if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); } 1.1233 + c.rShiftTo(1,c); 1.1234 + } 1.1235 + else if(!d.isEven()) d.subTo(m,d); 1.1236 + d.rShiftTo(1,d); 1.1237 + } 1.1238 + if(u.compareTo(v) >= 0) { 1.1239 + u.subTo(v,u); 1.1240 + if(ac) a.subTo(c,a); 1.1241 + b.subTo(d,b); 1.1242 + } 1.1243 + else { 1.1244 + v.subTo(u,v); 1.1245 + if(ac) c.subTo(a,c); 1.1246 + d.subTo(b,d); 1.1247 + } 1.1248 + } 1.1249 + if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO; 1.1250 + if(d.compareTo(m) >= 0) return d.subtract(m); 1.1251 + if(d.signum() < 0) d.addTo(m,d); else return d; 1.1252 + if(d.signum() < 0) return d.add(m); else return d; 1.1253 +} 1.1254 + 1.1255 +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]; 1.1256 +var lplim = (1<<26)/lowprimes[lowprimes.length-1]; 1.1257 + 1.1258 +// (public) test primality with certainty >= 1-.5^t 1.1259 +function bnIsProbablePrime(t) { 1.1260 + var i, x = this.abs(); 1.1261 + var x_array = x.array; 1.1262 + if(x.t == 1 && x_array[0] <= lowprimes[lowprimes.length-1]) { 1.1263 + for(i = 0; i < lowprimes.length; ++i) 1.1264 + if(x_array[0] == lowprimes[i]) return true; 1.1265 + return false; 1.1266 + } 1.1267 + if(x.isEven()) return false; 1.1268 + i = 1; 1.1269 + while(i < lowprimes.length) { 1.1270 + var m = lowprimes[i], j = i+1; 1.1271 + while(j < lowprimes.length && m < lplim) m *= lowprimes[j++]; 1.1272 + m = x.modInt(m); 1.1273 + while(i < j) if(m%lowprimes[i++] == 0) return false; 1.1274 + } 1.1275 + return x.millerRabin(t); 1.1276 +} 1.1277 + 1.1278 +// (protected) true if probably prime (HAC 4.24, Miller-Rabin) 1.1279 +function bnpMillerRabin(t) { 1.1280 + var n1 = this.subtract(BigInteger.ONE); 1.1281 + var k = n1.getLowestSetBit(); 1.1282 + if(k <= 0) return false; 1.1283 + var r = n1.shiftRight(k); 1.1284 + t = (t+1)>>1; 1.1285 + if(t > lowprimes.length) t = lowprimes.length; 1.1286 + var a = nbi(); 1.1287 + for(var i = 0; i < t; ++i) { 1.1288 + a.fromInt(lowprimes[i]); 1.1289 + var y = a.modPow(r,this); 1.1290 + if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) { 1.1291 + var j = 1; 1.1292 + while(j++ < k && y.compareTo(n1) != 0) { 1.1293 + y = y.modPowInt(2,this); 1.1294 + if(y.compareTo(BigInteger.ONE) == 0) return false; 1.1295 + } 1.1296 + if(y.compareTo(n1) != 0) return false; 1.1297 + } 1.1298 + } 1.1299 + return true; 1.1300 +} 1.1301 + 1.1302 +// protected 1.1303 +BigInteger.prototype.chunkSize = bnpChunkSize; 1.1304 +BigInteger.prototype.toRadix = bnpToRadix; 1.1305 +BigInteger.prototype.fromRadix = bnpFromRadix; 1.1306 +BigInteger.prototype.fromNumber = bnpFromNumber; 1.1307 +BigInteger.prototype.bitwiseTo = bnpBitwiseTo; 1.1308 +BigInteger.prototype.changeBit = bnpChangeBit; 1.1309 +BigInteger.prototype.addTo = bnpAddTo; 1.1310 +BigInteger.prototype.dMultiply = bnpDMultiply; 1.1311 +BigInteger.prototype.dAddOffset = bnpDAddOffset; 1.1312 +BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo; 1.1313 +BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo; 1.1314 +BigInteger.prototype.modInt = bnpModInt; 1.1315 +BigInteger.prototype.millerRabin = bnpMillerRabin; 1.1316 + 1.1317 +// public 1.1318 +BigInteger.prototype.clone = bnClone; 1.1319 +BigInteger.prototype.intValue = bnIntValue; 1.1320 +BigInteger.prototype.byteValue = bnByteValue; 1.1321 +BigInteger.prototype.shortValue = bnShortValue; 1.1322 +BigInteger.prototype.signum = bnSigNum; 1.1323 +BigInteger.prototype.toByteArray = bnToByteArray; 1.1324 +BigInteger.prototype.equals = bnEquals; 1.1325 +BigInteger.prototype.min = bnMin; 1.1326 +BigInteger.prototype.max = bnMax; 1.1327 +BigInteger.prototype.and = bnAnd; 1.1328 +BigInteger.prototype.or = bnOr; 1.1329 +BigInteger.prototype.xor = bnXor; 1.1330 +BigInteger.prototype.andNot = bnAndNot; 1.1331 +BigInteger.prototype.not = bnNot; 1.1332 +BigInteger.prototype.shiftLeft = bnShiftLeft; 1.1333 +BigInteger.prototype.shiftRight = bnShiftRight; 1.1334 +BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit; 1.1335 +BigInteger.prototype.bitCount = bnBitCount; 1.1336 +BigInteger.prototype.testBit = bnTestBit; 1.1337 +BigInteger.prototype.setBit = bnSetBit; 1.1338 +BigInteger.prototype.clearBit = bnClearBit; 1.1339 +BigInteger.prototype.flipBit = bnFlipBit; 1.1340 +BigInteger.prototype.add = bnAdd; 1.1341 +BigInteger.prototype.subtract = bnSubtract; 1.1342 +BigInteger.prototype.multiply = bnMultiply; 1.1343 +BigInteger.prototype.divide = bnDivide; 1.1344 +BigInteger.prototype.remainder = bnRemainder; 1.1345 +BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder; 1.1346 +BigInteger.prototype.modPow = bnModPow; 1.1347 +BigInteger.prototype.modInverse = bnModInverse; 1.1348 +BigInteger.prototype.pow = bnPow; 1.1349 +BigInteger.prototype.gcd = bnGCD; 1.1350 +BigInteger.prototype.isProbablePrime = bnIsProbablePrime; 1.1351 + 1.1352 +// BigInteger interfaces not implemented in jsbn: 1.1353 + 1.1354 +// BigInteger(int signum, byte[] magnitude) 1.1355 +// double doubleValue() 1.1356 +// float floatValue() 1.1357 +// int hashCode() 1.1358 +// long longValue() 1.1359 +// static BigInteger valueOf(long val) 1.1360 +// prng4.js - uses Arcfour as a PRNG 1.1361 + 1.1362 +function Arcfour() { 1.1363 + this.i = 0; 1.1364 + this.j = 0; 1.1365 + this.S = new Array(); 1.1366 +} 1.1367 + 1.1368 +// Initialize arcfour context from key, an array of ints, each from [0..255] 1.1369 +function ARC4init(key) { 1.1370 + var i, j, t; 1.1371 + for(i = 0; i < 256; ++i) 1.1372 + this.S[i] = i; 1.1373 + j = 0; 1.1374 + for(i = 0; i < 256; ++i) { 1.1375 + j = (j + this.S[i] + key[i % key.length]) & 255; 1.1376 + t = this.S[i]; 1.1377 + this.S[i] = this.S[j]; 1.1378 + this.S[j] = t; 1.1379 + } 1.1380 + this.i = 0; 1.1381 + this.j = 0; 1.1382 +} 1.1383 + 1.1384 +function ARC4next() { 1.1385 + var t; 1.1386 + this.i = (this.i + 1) & 255; 1.1387 + this.j = (this.j + this.S[this.i]) & 255; 1.1388 + t = this.S[this.i]; 1.1389 + this.S[this.i] = this.S[this.j]; 1.1390 + this.S[this.j] = t; 1.1391 + return this.S[(t + this.S[this.i]) & 255]; 1.1392 +} 1.1393 + 1.1394 +Arcfour.prototype.init = ARC4init; 1.1395 +Arcfour.prototype.next = ARC4next; 1.1396 + 1.1397 +// Plug in your RNG constructor here 1.1398 +function prng_newstate() { 1.1399 + return new Arcfour(); 1.1400 +} 1.1401 + 1.1402 +// Pool size must be a multiple of 4 and greater than 32. 1.1403 +// An array of bytes the size of the pool will be passed to init() 1.1404 +var rng_psize = 256; 1.1405 +// Random number generator - requires a PRNG backend, e.g. prng4.js 1.1406 + 1.1407 +// For best results, put code like 1.1408 +// <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'> 1.1409 +// in your main HTML document. 1.1410 + 1.1411 +var rng_state; 1.1412 +var rng_pool; 1.1413 +var rng_pptr; 1.1414 + 1.1415 +// Mix in a 32-bit integer into the pool 1.1416 +function rng_seed_int(x) { 1.1417 + rng_pool[rng_pptr++] ^= x & 255; 1.1418 + rng_pool[rng_pptr++] ^= (x >> 8) & 255; 1.1419 + rng_pool[rng_pptr++] ^= (x >> 16) & 255; 1.1420 + rng_pool[rng_pptr++] ^= (x >> 24) & 255; 1.1421 + if(rng_pptr >= rng_psize) rng_pptr -= rng_psize; 1.1422 +} 1.1423 + 1.1424 +// Mix in the current time (w/milliseconds) into the pool 1.1425 +function rng_seed_time() { 1.1426 + // Use pre-computed date to avoid making the benchmark 1.1427 + // results dependent on the current date. 1.1428 + rng_seed_int(1122926989487); 1.1429 +} 1.1430 + 1.1431 +// Initialize the pool with junk if needed. 1.1432 +if(rng_pool == null) { 1.1433 + rng_pool = new Array(); 1.1434 + rng_pptr = 0; 1.1435 + var t; 1.1436 + while(rng_pptr < rng_psize) { // extract some randomness from Math.random() 1.1437 + t = Math.floor(65536 * MyMath.random()); 1.1438 + rng_pool[rng_pptr++] = t >>> 8; 1.1439 + rng_pool[rng_pptr++] = t & 255; 1.1440 + } 1.1441 + rng_pptr = 0; 1.1442 + rng_seed_time(); 1.1443 + //rng_seed_int(window.screenX); 1.1444 + //rng_seed_int(window.screenY); 1.1445 +} 1.1446 + 1.1447 +function rng_get_byte() { 1.1448 + if(rng_state == null) { 1.1449 + rng_seed_time(); 1.1450 + rng_state = prng_newstate(); 1.1451 + rng_state.init(rng_pool); 1.1452 + for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr) 1.1453 + rng_pool[rng_pptr] = 0; 1.1454 + rng_pptr = 0; 1.1455 + //rng_pool = null; 1.1456 + } 1.1457 + // TODO: allow reseeding after first request 1.1458 + return rng_state.next(); 1.1459 +} 1.1460 + 1.1461 +function rng_get_bytes(ba) { 1.1462 + var i; 1.1463 + for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte(); 1.1464 +} 1.1465 + 1.1466 +function SecureRandom() {} 1.1467 + 1.1468 +SecureRandom.prototype.nextBytes = rng_get_bytes; 1.1469 +// Depends on jsbn.js and rng.js 1.1470 + 1.1471 +// convert a (hex) string to a bignum object 1.1472 +function parseBigInt(str,r) { 1.1473 + return new BigInteger(str,r); 1.1474 +} 1.1475 + 1.1476 +function linebrk(s,n) { 1.1477 + var ret = ""; 1.1478 + var i = 0; 1.1479 + while(i + n < s.length) { 1.1480 + ret += s.substring(i,i+n) + "\n"; 1.1481 + i += n; 1.1482 + } 1.1483 + return ret + s.substring(i,s.length); 1.1484 +} 1.1485 + 1.1486 +function byte2Hex(b) { 1.1487 + if(b < 0x10) 1.1488 + return "0" + b.toString(16); 1.1489 + else 1.1490 + return b.toString(16); 1.1491 +} 1.1492 + 1.1493 +// PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint 1.1494 +function pkcs1pad2(s,n) { 1.1495 + if(n < s.length + 11) { 1.1496 + alert("Message too long for RSA"); 1.1497 + return null; 1.1498 + } 1.1499 + var ba = new Array(); 1.1500 + var i = s.length - 1; 1.1501 + while(i >= 0 && n > 0) ba[--n] = s.charCodeAt(i--); 1.1502 + ba[--n] = 0; 1.1503 + var rng = new SecureRandom(); 1.1504 + var x = new Array(); 1.1505 + while(n > 2) { // random non-zero pad 1.1506 + x[0] = 0; 1.1507 + while(x[0] == 0) rng.nextBytes(x); 1.1508 + ba[--n] = x[0]; 1.1509 + } 1.1510 + ba[--n] = 2; 1.1511 + ba[--n] = 0; 1.1512 + return new BigInteger(ba); 1.1513 +} 1.1514 + 1.1515 +// "empty" RSA key constructor 1.1516 +function RSAKey() { 1.1517 + this.n = null; 1.1518 + this.e = 0; 1.1519 + this.d = null; 1.1520 + this.p = null; 1.1521 + this.q = null; 1.1522 + this.dmp1 = null; 1.1523 + this.dmq1 = null; 1.1524 + this.coeff = null; 1.1525 +} 1.1526 + 1.1527 +// Set the public key fields N and e from hex strings 1.1528 +function RSASetPublic(N,E) { 1.1529 + if(N != null && E != null && N.length > 0 && E.length > 0) { 1.1530 + this.n = parseBigInt(N,16); 1.1531 + this.e = parseInt(E,16); 1.1532 + } 1.1533 + else 1.1534 + alert("Invalid RSA public key"); 1.1535 +} 1.1536 + 1.1537 +// Perform raw public operation on "x": return x^e (mod n) 1.1538 +function RSADoPublic(x) { 1.1539 + return x.modPowInt(this.e, this.n); 1.1540 +} 1.1541 + 1.1542 +// Return the PKCS#1 RSA encryption of "text" as an even-length hex string 1.1543 +function RSAEncrypt(text) { 1.1544 + var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3); 1.1545 + if(m == null) return null; 1.1546 + var c = this.doPublic(m); 1.1547 + if(c == null) return null; 1.1548 + var h = c.toString(16); 1.1549 + if((h.length & 1) == 0) return h; else return "0" + h; 1.1550 +} 1.1551 + 1.1552 +// Return the PKCS#1 RSA encryption of "text" as a Base64-encoded string 1.1553 +//function RSAEncryptB64(text) { 1.1554 +// var h = this.encrypt(text); 1.1555 +// if(h) return hex2b64(h); else return null; 1.1556 +//} 1.1557 + 1.1558 +// protected 1.1559 +RSAKey.prototype.doPublic = RSADoPublic; 1.1560 + 1.1561 +// public 1.1562 +RSAKey.prototype.setPublic = RSASetPublic; 1.1563 +RSAKey.prototype.encrypt = RSAEncrypt; 1.1564 +//RSAKey.prototype.encrypt_b64 = RSAEncryptB64; 1.1565 +// Depends on rsa.js and jsbn2.js 1.1566 + 1.1567 +// Undo PKCS#1 (type 2, random) padding and, if valid, return the plaintext 1.1568 +function pkcs1unpad2(d,n) { 1.1569 + var b = d.toByteArray(); 1.1570 + var i = 0; 1.1571 + while(i < b.length && b[i] == 0) ++i; 1.1572 + if(b.length-i != n-1 || b[i] != 2) 1.1573 + return null; 1.1574 + ++i; 1.1575 + while(b[i] != 0) 1.1576 + if(++i >= b.length) return null; 1.1577 + var ret = ""; 1.1578 + while(++i < b.length) 1.1579 + ret += String.fromCharCode(b[i]); 1.1580 + return ret; 1.1581 +} 1.1582 + 1.1583 +// Set the private key fields N, e, and d from hex strings 1.1584 +function RSASetPrivate(N,E,D) { 1.1585 + if(N != null && E != null && N.length > 0 && E.length > 0) { 1.1586 + this.n = parseBigInt(N,16); 1.1587 + this.e = parseInt(E,16); 1.1588 + this.d = parseBigInt(D,16); 1.1589 + } 1.1590 + else 1.1591 + alert("Invalid RSA private key"); 1.1592 +} 1.1593 + 1.1594 +// Set the private key fields N, e, d and CRT params from hex strings 1.1595 +function RSASetPrivateEx(N,E,D,P,Q,DP,DQ,C) { 1.1596 + if(N != null && E != null && N.length > 0 && E.length > 0) { 1.1597 + this.n = parseBigInt(N,16); 1.1598 + this.e = parseInt(E,16); 1.1599 + this.d = parseBigInt(D,16); 1.1600 + this.p = parseBigInt(P,16); 1.1601 + this.q = parseBigInt(Q,16); 1.1602 + this.dmp1 = parseBigInt(DP,16); 1.1603 + this.dmq1 = parseBigInt(DQ,16); 1.1604 + this.coeff = parseBigInt(C,16); 1.1605 + } 1.1606 + else 1.1607 + alert("Invalid RSA private key"); 1.1608 +} 1.1609 + 1.1610 +// Generate a new random private key B bits long, using public expt E 1.1611 +function RSAGenerate(B,E) { 1.1612 + var rng = new SecureRandom(); 1.1613 + var qs = B>>1; 1.1614 + this.e = parseInt(E,16); 1.1615 + var ee = new BigInteger(E,16); 1.1616 + for(;;) { 1.1617 + for(;;) { 1.1618 + this.p = new BigInteger(B-qs,1,rng); 1.1619 + if(this.p.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.p.isProbablePrime(10)) break; 1.1620 + } 1.1621 + for(;;) { 1.1622 + this.q = new BigInteger(qs,1,rng); 1.1623 + if(this.q.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.q.isProbablePrime(10)) break; 1.1624 + } 1.1625 + if(this.p.compareTo(this.q) <= 0) { 1.1626 + var t = this.p; 1.1627 + this.p = this.q; 1.1628 + this.q = t; 1.1629 + } 1.1630 + var p1 = this.p.subtract(BigInteger.ONE); 1.1631 + var q1 = this.q.subtract(BigInteger.ONE); 1.1632 + var phi = p1.multiply(q1); 1.1633 + if(phi.gcd(ee).compareTo(BigInteger.ONE) == 0) { 1.1634 + this.n = this.p.multiply(this.q); 1.1635 + this.d = ee.modInverse(phi); 1.1636 + this.dmp1 = this.d.mod(p1); 1.1637 + this.dmq1 = this.d.mod(q1); 1.1638 + this.coeff = this.q.modInverse(this.p); 1.1639 + break; 1.1640 + } 1.1641 + } 1.1642 +} 1.1643 + 1.1644 +// Perform raw private operation on "x": return x^d (mod n) 1.1645 +function RSADoPrivate(x) { 1.1646 + if(this.p == null || this.q == null) 1.1647 + return x.modPow(this.d, this.n); 1.1648 + 1.1649 + // TODO: re-calculate any missing CRT params 1.1650 + var xp = x.mod(this.p).modPow(this.dmp1, this.p); 1.1651 + var xq = x.mod(this.q).modPow(this.dmq1, this.q); 1.1652 + 1.1653 + while(xp.compareTo(xq) < 0) 1.1654 + xp = xp.add(this.p); 1.1655 + return xp.subtract(xq).multiply(this.coeff).mod(this.p).multiply(this.q).add(xq); 1.1656 +} 1.1657 + 1.1658 +// Return the PKCS#1 RSA decryption of "ctext". 1.1659 +// "ctext" is an even-length hex string and the output is a plain string. 1.1660 +function RSADecrypt(ctext) { 1.1661 + var c = parseBigInt(ctext, 16); 1.1662 + var m = this.doPrivate(c); 1.1663 + if(m == null) return null; 1.1664 + return pkcs1unpad2(m, (this.n.bitLength()+7)>>3); 1.1665 +} 1.1666 + 1.1667 +// Return the PKCS#1 RSA decryption of "ctext". 1.1668 +// "ctext" is a Base64-encoded string and the output is a plain string. 1.1669 +//function RSAB64Decrypt(ctext) { 1.1670 +// var h = b64tohex(ctext); 1.1671 +// if(h) return this.decrypt(h); else return null; 1.1672 +//} 1.1673 + 1.1674 +// protected 1.1675 +RSAKey.prototype.doPrivate = RSADoPrivate; 1.1676 + 1.1677 +// public 1.1678 +RSAKey.prototype.setPrivate = RSASetPrivate; 1.1679 +RSAKey.prototype.setPrivateEx = RSASetPrivateEx; 1.1680 +RSAKey.prototype.generate = RSAGenerate; 1.1681 +RSAKey.prototype.decrypt = RSADecrypt; 1.1682 +//RSAKey.prototype.b64_decrypt = RSAB64Decrypt; 1.1683 + 1.1684 + 1.1685 +nValue="a5261939975948bb7a58dffe5ff54e65f0498f9175f5a09288810b8975871e99af3b5dd94057b0fc07535f5f97444504fa35169d461d0d30cf0192e307727c065168c788771c561a9400fb49175e9e6aa4e23fe11af69e9412dd23b0cb6684c4c2429bce139e848ab26d0829073351f4acd36074eafd036a5eb83359d2a698d3"; 1.1686 +eValue="10001"; 1.1687 +dValue="8e9912f6d3645894e8d38cb58c0db81ff516cf4c7e5a14c7f1eddb1459d2cded4d8d293fc97aee6aefb861859c8b6a3d1dfe710463e1f9ddc72048c09751971c4a580aa51eb523357a3cc48d31cfad1d4a165066ed92d4748fb6571211da5cb14bc11b6e2df7c1a559e6d5ac1cd5c94703a22891464fba23d0d965086277a161"; 1.1688 +pValue="d090ce58a92c75233a6486cb0a9209bf3583b64f540c76f5294bb97d285eed33aec220bde14b2417951178ac152ceab6da7090905b478195498b352048f15e7d"; 1.1689 +qValue="cab575dc652bb66df15a0359609d51d1db184750c00c6698b90ef3465c99655103edbf0d54c56aec0ce3c4d22592338092a126a0cc49f65a4a30d222b411e58f"; 1.1690 +dmp1Value="1a24bca8e273df2f0e47c199bbf678604e7df7215480c77c8db39f49b000ce2cf7500038acfff5433b7d582a01f1826e6f4d42e1c57f5e1fef7b12aabc59fd25"; 1.1691 +dmq1Value="3d06982efbbe47339e1f6d36b1216b8a741d410b0c662f54f7118b27b9a4ec9d914337eb39841d8666f3034408cf94f5b62f11c402fc994fe15a05493150d9fd"; 1.1692 +coeffValue="3a3e731acd8960b7ff9eb81a7ff93bd1cfa74cbd56987db58b4594fb09c09084db1734c8143f98b602b981aaa9243ca28deb69b5b280ee8dcee0fd2625e53250"; 1.1693 + 1.1694 +setupEngine(am3, 28); 1.1695 + 1.1696 +// So that v8 understands assertEq() 1.1697 +if (assertEq == undefined) 1.1698 +{ 1.1699 + function assertEq(to_check, expected) { 1.1700 + if ( to_check !== expected ) 1.1701 + { 1.1702 + print( "Error: Assertion failed: got \"" + to_check + "\", expected \"" + expected + "\"" ); 1.1703 + } 1.1704 + } 1.1705 +} 1.1706 + 1.1707 +function check_correctness(text, hash) { 1.1708 + var RSA = new RSAKey(); 1.1709 + RSA.setPublic(nValue, eValue); 1.1710 + RSA.setPrivateEx(nValue, eValue, dValue, pValue, qValue, dmp1Value, dmq1Value, coeffValue); 1.1711 + var encrypted = RSA.encrypt(text); 1.1712 + var decrypted = RSA.decrypt(encrypted); 1.1713 + assertEq( encrypted, hash ); 1.1714 + assertEq( decrypted, text ); 1.1715 +} 1.1716 + 1.1717 +// All 'correct' hashes here come from v8's javascript shell built off of tag 2.3.4 1.1718 +check_correctness("Hello! I am some text.", "142b19b40fee712ab9468be296447d38c7dfe81a7850f11ae6aa21e49396a4e90bd6ba4aa385105e15960a59f95447dfad89671da6e08ed42229939583753be84d07558abb4feee4d46a92fd31d962679a1a5f4bf0fb7af414b9a756e18df7e6d1e96971cc66769f3b27d61ad932f2211373e0de388dc040557d4c3c3fe74320"); 1.1719 +check_correctness("PLEASE ENCRYPT ME. I AM TEXT. I AM DIEING TO BE ENCRYPTED. OH WHY WONT YOU ENCRYPT ME!?", "490c1fae87d7046296e4b34b357912a72cb7c38c0da3198f1ac3aad3489662ce02663ec5ea1be58ae73a275f3096b16c491f3520ebf822df6c65cc95e28be1cc0a4454dfba3fdd402c3a9de0db2f308989bfc1a7fada0dd680db76d24b2d96bd6b7e7d7e7f962deb953038bae06092f7bb9bcb40bba4ec92e040df32f98e035e"); 1.1720 +check_correctness("x","46c1b7cf202171b1b588e9ecf250e768dcf3b300490e859d508f708e702ef799bc496b9fac7634d60a82644653c5fd25b808393b234567116b8890d5f119c7c74dae7c97c8e40ba78ca2dc3e3d78ce859a7fa3815f42c27d0607eafc3940896abb6019cc28b2ff875531ed581a6351728a8df0d607b7c2c26265bf3dddbe4f84");