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