OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2003-2005 Tom Wu |
| 3 * All Rights Reserved. |
| 4 * |
| 5 * Permission is hereby granted, free of charge, to any person obtaining |
| 6 * a copy of this software and associated documentation files (the |
| 7 * "Software"), to deal in the Software without restriction, including |
| 8 * without limitation the rights to use, copy, modify, merge, publish, |
| 9 * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 * permit persons to whom the Software is furnished to do so, subject to |
| 11 * the following conditions: |
| 12 * |
| 13 * The above copyright notice and this permission notice shall be |
| 14 * included in all copies or substantial portions of the Software. |
| 15 * |
| 16 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 17 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 18 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 19 * |
| 20 * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL, |
| 21 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER |
| 22 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF |
| 23 * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT |
| 24 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 25 * |
| 26 * In addition, the following condition applies: |
| 27 * |
| 28 * All redistributions must retain an intact copy of this copyright notice |
| 29 * and disclaimer. |
| 30 */ |
| 31 |
| 32 // Basic JavaScript BN library - subset useful for RSA encryption. |
| 33 |
| 34 // Bits per digit |
| 35 var dbits; |
| 36 |
| 37 // JavaScript engine analysis |
| 38 var canary = 0xdeadbeefcafe; |
| 39 var j_lm = ((canary&0xffffff)==0xefcafe); |
| 40 |
| 41 // (public) Constructor |
| 42 function BigInteger(a,b,c) { |
| 43 if(a != null) |
| 44 if("number" == typeof a) this.fromNumber(a,b,c); |
| 45 else if(b == null && "string" != typeof a) this.fromString(a,256); |
| 46 else this.fromString(a,b); |
| 47 } |
| 48 exports.BigInteger = BigInteger; |
| 49 |
| 50 // return new, unset BigInteger |
| 51 function nbi() { return new BigInteger(null); } |
| 52 |
| 53 // am: Compute w_j += (x*this_i), propagate carries, |
| 54 // c is initial carry, returns final carry. |
| 55 // c < 3*dvalue, x < 2*dvalue, this_i < dvalue |
| 56 // We need to select the fastest one that works in this environment. |
| 57 |
| 58 // am1: use a single mult and divide to get the high bits, |
| 59 // max digit bits should be 26 because |
| 60 // max internal value = 2*dvalue^2-2*dvalue (< 2^53) |
| 61 function am1(i,x,w,j,c,n) { |
| 62 while(--n >= 0) { |
| 63 var v = x*this[i++]+w[j]+c; |
| 64 c = Math.floor(v/0x4000000); |
| 65 w[j++] = v&0x3ffffff; |
| 66 } |
| 67 return c; |
| 68 } |
| 69 // am2 avoids a big mult-and-extract completely. |
| 70 // Max digit bits should be <= 30 because we do bitwise ops |
| 71 // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31) |
| 72 function am2(i,x,w,j,c,n) { |
| 73 var xl = x&0x7fff, xh = x>>15; |
| 74 while(--n >= 0) { |
| 75 var l = this[i]&0x7fff; |
| 76 var h = this[i++]>>15; |
| 77 var m = xh*l+h*xl; |
| 78 l = xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff); |
| 79 c = (l>>>30)+(m>>>15)+xh*h+(c>>>30); |
| 80 w[j++] = l&0x3fffffff; |
| 81 } |
| 82 return c; |
| 83 } |
| 84 // Alternately, set max digit bits to 28 since some |
| 85 // browsers slow down when dealing with 32-bit numbers. |
| 86 function am3(i,x,w,j,c,n) { |
| 87 var xl = x&0x3fff, xh = x>>14; |
| 88 while(--n >= 0) { |
| 89 var l = this[i]&0x3fff; |
| 90 var h = this[i++]>>14; |
| 91 var m = xh*l+h*xl; |
| 92 l = xl*l+((m&0x3fff)<<14)+w[j]+c; |
| 93 c = (l>>28)+(m>>14)+xh*h; |
| 94 w[j++] = l&0xfffffff; |
| 95 } |
| 96 return c; |
| 97 } |
| 98 if(j_lm && (typeof navigator != "undefined" && navigator.appName == "Microsoft I
nternet Explorer")) { |
| 99 BigInteger.prototype.am = am2; |
| 100 dbits = 30; |
| 101 } |
| 102 else if(j_lm && (typeof navigator != "undefined" && navigator.appName != "Netsca
pe")) { |
| 103 BigInteger.prototype.am = am1; |
| 104 dbits = 26; |
| 105 } |
| 106 else { // Mozilla/Netscape seems to prefer am3 |
| 107 BigInteger.prototype.am = am3; |
| 108 dbits = 28; |
| 109 } |
| 110 |
| 111 BigInteger.prototype.DB = dbits; |
| 112 BigInteger.prototype.DM = ((1<<dbits)-1); |
| 113 BigInteger.prototype.DV = (1<<dbits); |
| 114 |
| 115 var BI_FP = 52; |
| 116 BigInteger.prototype.FV = Math.pow(2,BI_FP); |
| 117 BigInteger.prototype.F1 = BI_FP-dbits; |
| 118 BigInteger.prototype.F2 = 2*dbits-BI_FP; |
| 119 |
| 120 // Digit conversions |
| 121 var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz"; |
| 122 var BI_RC = new Array(); |
| 123 var rr,vv; |
| 124 rr = "0".charCodeAt(0); |
| 125 for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv; |
| 126 rr = "a".charCodeAt(0); |
| 127 for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv; |
| 128 rr = "A".charCodeAt(0); |
| 129 for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv; |
| 130 |
| 131 function int2char(n) { return BI_RM.charAt(n); } |
| 132 function intAt(s,i) { |
| 133 var c = BI_RC[s.charCodeAt(i)]; |
| 134 return (c==null)?-1:c; |
| 135 } |
| 136 |
| 137 // (protected) copy this to r |
| 138 function bnpCopyTo(r) { |
| 139 for(var i = this.t-1; i >= 0; --i) r[i] = this[i]; |
| 140 r.t = this.t; |
| 141 r.s = this.s; |
| 142 } |
| 143 |
| 144 // (protected) set from integer value x, -DV <= x < DV |
| 145 function bnpFromInt(x) { |
| 146 this.t = 1; |
| 147 this.s = (x<0)?-1:0; |
| 148 if(x > 0) this[0] = x; |
| 149 else if(x < -1) this[0] = x+DV; |
| 150 else this.t = 0; |
| 151 } |
| 152 |
| 153 // return bigint initialized to value |
| 154 function nbv(i) { var r = nbi(); r.fromInt(i); return r; } |
| 155 |
| 156 // (protected) set from string and radix |
| 157 function bnpFromString(s,b) { |
| 158 var k; |
| 159 if(b == 16) k = 4; |
| 160 else if(b == 8) k = 3; |
| 161 else if(b == 256) k = 8; // byte array |
| 162 else if(b == 2) k = 1; |
| 163 else if(b == 32) k = 5; |
| 164 else if(b == 4) k = 2; |
| 165 else { this.fromRadix(s,b); return; } |
| 166 this.t = 0; |
| 167 this.s = 0; |
| 168 var i = s.length, mi = false, sh = 0; |
| 169 while(--i >= 0) { |
| 170 var x = (k==8)?s.charCodeAt(i)&0xff:intAt(s,i); /** MODIFIED **/ |
| 171 if(x < 0) { |
| 172 if(s.charAt(i) == "-") mi = true; |
| 173 continue; |
| 174 } |
| 175 mi = false; |
| 176 if(sh == 0) |
| 177 this[this.t++] = x; |
| 178 else if(sh+k > this.DB) { |
| 179 this[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh; |
| 180 this[this.t++] = (x>>(this.DB-sh)); |
| 181 } |
| 182 else |
| 183 this[this.t-1] |= x<<sh; |
| 184 sh += k; |
| 185 if(sh >= this.DB) sh -= this.DB; |
| 186 } |
| 187 if(k == 8 && (s[0]&0x80) != 0) { |
| 188 this.s = -1; |
| 189 if(sh > 0) this[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh; |
| 190 } |
| 191 this.clamp(); |
| 192 if(mi) BigInteger.ZERO.subTo(this,this); |
| 193 } |
| 194 |
| 195 // (protected) clamp off excess high words |
| 196 function bnpClamp() { |
| 197 var c = this.s&this.DM; |
| 198 while(this.t > 0 && this[this.t-1] == c) --this.t; |
| 199 } |
| 200 |
| 201 // (public) return string representation in given radix |
| 202 function bnToString(b) { |
| 203 if(this.s < 0) return "-"+this.negate().toString(b); |
| 204 var k; |
| 205 if(b == 16) k = 4; |
| 206 else if(b == 8) k = 3; |
| 207 else if(b == 256) k = 8; // byte array /** MODIFIED **/ |
| 208 else if(b == 2) k = 1; |
| 209 else if(b == 32) k = 5; |
| 210 else if(b == 4) k = 2; |
| 211 else return this.toRadix(b); |
| 212 var km = (1<<k)-1, d, m = false, r = "", i = this.t; |
| 213 var p = this.DB-(i*this.DB)%k; |
| 214 if(i-- > 0) { |
| 215 if(p < this.DB && (d = this[i]>>p) > 0) { m = true; r = (k==8)?String.fromCh
arCode(d):int2char(d); } /** MODIFIED **/ |
| 216 while(i >= 0) { |
| 217 if(p < k) { |
| 218 d = (this[i]&((1<<p)-1))<<(k-p); |
| 219 d |= this[--i]>>(p+=this.DB-k); |
| 220 } |
| 221 else { |
| 222 d = (this[i]>>(p-=k))&km; |
| 223 if(p <= 0) { p += this.DB; --i; } |
| 224 } |
| 225 if(d > 0) m = true; |
| 226 if(m) r += (k==8)?String.fromCharCode(d):int2char(d); /** MODIFIED **/ |
| 227 } |
| 228 } |
| 229 return m?r:"0"; |
| 230 } |
| 231 |
| 232 // (public) -this |
| 233 function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; } |
| 234 |
| 235 // (public) |this| |
| 236 function bnAbs() { return (this.s<0)?this.negate():this; } |
| 237 |
| 238 // (public) return + if this > a, - if this < a, 0 if equal |
| 239 function bnCompareTo(a) { |
| 240 var r = this.s-a.s; |
| 241 if(r != 0) return r; |
| 242 var i = this.t; |
| 243 r = i-a.t; |
| 244 if(r != 0) return r; |
| 245 while(--i >= 0) if((r=this[i]-a[i]) != 0) return r; |
| 246 return 0; |
| 247 } |
| 248 |
| 249 // returns bit length of the integer x |
| 250 function nbits(x) { |
| 251 var r = 1, t; |
| 252 if((t=x>>>16) != 0) { x = t; r += 16; } |
| 253 if((t=x>>8) != 0) { x = t; r += 8; } |
| 254 if((t=x>>4) != 0) { x = t; r += 4; } |
| 255 if((t=x>>2) != 0) { x = t; r += 2; } |
| 256 if((t=x>>1) != 0) { x = t; r += 1; } |
| 257 return r; |
| 258 } |
| 259 |
| 260 // (public) return the number of bits in "this" |
| 261 function bnBitLength() { |
| 262 if(this.t <= 0) return 0; |
| 263 return this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM)); |
| 264 } |
| 265 |
| 266 // (protected) r = this << n*DB |
| 267 function bnpDLShiftTo(n,r) { |
| 268 var i; |
| 269 for(i = this.t-1; i >= 0; --i) r[i+n] = this[i]; |
| 270 for(i = n-1; i >= 0; --i) r[i] = 0; |
| 271 r.t = this.t+n; |
| 272 r.s = this.s; |
| 273 } |
| 274 |
| 275 // (protected) r = this >> n*DB |
| 276 function bnpDRShiftTo(n,r) { |
| 277 for(var i = n; i < this.t; ++i) r[i-n] = this[i]; |
| 278 r.t = Math.max(this.t-n,0); |
| 279 r.s = this.s; |
| 280 } |
| 281 |
| 282 // (protected) r = this << n |
| 283 function bnpLShiftTo(n,r) { |
| 284 var bs = n%this.DB; |
| 285 var cbs = this.DB-bs; |
| 286 var bm = (1<<cbs)-1; |
| 287 var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i; |
| 288 for(i = this.t-1; i >= 0; --i) { |
| 289 r[i+ds+1] = (this[i]>>cbs)|c; |
| 290 c = (this[i]&bm)<<bs; |
| 291 } |
| 292 for(i = ds-1; i >= 0; --i) r[i] = 0; |
| 293 r[ds] = c; |
| 294 r.t = this.t+ds+1; |
| 295 r.s = this.s; |
| 296 r.clamp(); |
| 297 } |
| 298 |
| 299 // (protected) r = this >> n |
| 300 function bnpRShiftTo(n,r) { |
| 301 r.s = this.s; |
| 302 var ds = Math.floor(n/this.DB); |
| 303 if(ds >= this.t) { r.t = 0; return; } |
| 304 var bs = n%this.DB; |
| 305 var cbs = this.DB-bs; |
| 306 var bm = (1<<bs)-1; |
| 307 r[0] = this[ds]>>bs; |
| 308 for(var i = ds+1; i < this.t; ++i) { |
| 309 r[i-ds-1] |= (this[i]&bm)<<cbs; |
| 310 r[i-ds] = this[i]>>bs; |
| 311 } |
| 312 if(bs > 0) r[this.t-ds-1] |= (this.s&bm)<<cbs; |
| 313 r.t = this.t-ds; |
| 314 r.clamp(); |
| 315 } |
| 316 |
| 317 // (protected) r = this - a |
| 318 function bnpSubTo(a,r) { |
| 319 var i = 0, c = 0, m = Math.min(a.t,this.t); |
| 320 while(i < m) { |
| 321 c += this[i]-a[i]; |
| 322 r[i++] = c&this.DM; |
| 323 c >>= this.DB; |
| 324 } |
| 325 if(a.t < this.t) { |
| 326 c -= a.s; |
| 327 while(i < this.t) { |
| 328 c += this[i]; |
| 329 r[i++] = c&this.DM; |
| 330 c >>= this.DB; |
| 331 } |
| 332 c += this.s; |
| 333 } |
| 334 else { |
| 335 c += this.s; |
| 336 while(i < a.t) { |
| 337 c -= a[i]; |
| 338 r[i++] = c&this.DM; |
| 339 c >>= this.DB; |
| 340 } |
| 341 c -= a.s; |
| 342 } |
| 343 r.s = (c<0)?-1:0; |
| 344 if(c < -1) r[i++] = this.DV+c; |
| 345 else if(c > 0) r[i++] = c; |
| 346 r.t = i; |
| 347 r.clamp(); |
| 348 } |
| 349 |
| 350 // (protected) r = this * a, r != this,a (HAC 14.12) |
| 351 // "this" should be the larger one if appropriate. |
| 352 function bnpMultiplyTo(a,r) { |
| 353 var x = this.abs(), y = a.abs(); |
| 354 var i = x.t; |
| 355 r.t = i+y.t; |
| 356 while(--i >= 0) r[i] = 0; |
| 357 for(i = 0; i < y.t; ++i) r[i+x.t] = x.am(0,y[i],r,i,0,x.t); |
| 358 r.s = 0; |
| 359 r.clamp(); |
| 360 if(this.s != a.s) BigInteger.ZERO.subTo(r,r); |
| 361 } |
| 362 |
| 363 // (protected) r = this^2, r != this (HAC 14.16) |
| 364 function bnpSquareTo(r) { |
| 365 var x = this.abs(); |
| 366 var i = r.t = 2*x.t; |
| 367 while(--i >= 0) r[i] = 0; |
| 368 for(i = 0; i < x.t-1; ++i) { |
| 369 var c = x.am(i,x[i],r,2*i,0,1); |
| 370 if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1)) >= x.DV) { |
| 371 r[i+x.t] -= x.DV; |
| 372 r[i+x.t+1] = 1; |
| 373 } |
| 374 } |
| 375 if(r.t > 0) r[r.t-1] += x.am(i,x[i],r,2*i,0,1); |
| 376 r.s = 0; |
| 377 r.clamp(); |
| 378 } |
| 379 |
| 380 // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20) |
| 381 // r != q, this != m. q or r may be null. |
| 382 function bnpDivRemTo(m,q,r) { |
| 383 var pm = m.abs(); |
| 384 if(pm.t <= 0) return; |
| 385 var pt = this.abs(); |
| 386 if(pt.t < pm.t) { |
| 387 if(q != null) q.fromInt(0); |
| 388 if(r != null) this.copyTo(r); |
| 389 return; |
| 390 } |
| 391 if(r == null) r = nbi(); |
| 392 var y = nbi(), ts = this.s, ms = m.s; |
| 393 var nsh = this.DB-nbits(pm[pm.t-1]); // normalize modulus |
| 394 if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); } |
| 395 else { pm.copyTo(y); pt.copyTo(r); } |
| 396 var ys = y.t; |
| 397 var y0 = y[ys-1]; |
| 398 if(y0 == 0) return; |
| 399 var yt = y0*(1<<this.F1)+((ys>1)?y[ys-2]>>this.F2:0); |
| 400 var d1 = this.FV/yt, d2 = (1<<this.F1)/yt, e = 1<<this.F2; |
| 401 var i = r.t, j = i-ys, t = (q==null)?nbi():q; |
| 402 y.dlShiftTo(j,t); |
| 403 if(r.compareTo(t) >= 0) { |
| 404 r[r.t++] = 1; |
| 405 r.subTo(t,r); |
| 406 } |
| 407 BigInteger.ONE.dlShiftTo(ys,t); |
| 408 t.subTo(y,y); // "negative" y so we can replace sub with am later |
| 409 while(y.t < ys) y[y.t++] = 0; |
| 410 while(--j >= 0) { |
| 411 // Estimate quotient digit |
| 412 var qd = (r[--i]==y0)?this.DM:Math.floor(r[i]*d1+(r[i-1]+e)*d2); |
| 413 if((r[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out |
| 414 y.dlShiftTo(j,t); |
| 415 r.subTo(t,r); |
| 416 while(r[i] < --qd) r.subTo(t,r); |
| 417 } |
| 418 } |
| 419 if(q != null) { |
| 420 r.drShiftTo(ys,q); |
| 421 if(ts != ms) BigInteger.ZERO.subTo(q,q); |
| 422 } |
| 423 r.t = ys; |
| 424 r.clamp(); |
| 425 if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder |
| 426 if(ts < 0) BigInteger.ZERO.subTo(r,r); |
| 427 } |
| 428 |
| 429 // (public) this mod a |
| 430 function bnMod(a) { |
| 431 var r = nbi(); |
| 432 this.abs().divRemTo(a,null,r); |
| 433 if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r); |
| 434 return r; |
| 435 } |
| 436 |
| 437 // Modular reduction using "classic" algorithm |
| 438 function Classic(m) { this.m = m; } |
| 439 function cConvert(x) { |
| 440 if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m); |
| 441 else return x; |
| 442 } |
| 443 function cRevert(x) { return x; } |
| 444 function cReduce(x) { x.divRemTo(this.m,null,x); } |
| 445 function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } |
| 446 function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); } |
| 447 |
| 448 Classic.prototype.convert = cConvert; |
| 449 Classic.prototype.revert = cRevert; |
| 450 Classic.prototype.reduce = cReduce; |
| 451 Classic.prototype.mulTo = cMulTo; |
| 452 Classic.prototype.sqrTo = cSqrTo; |
| 453 |
| 454 // (protected) return "-1/this % 2^DB"; useful for Mont. reduction |
| 455 // justification: |
| 456 // xy == 1 (mod m) |
| 457 // xy = 1+km |
| 458 // xy(2-xy) = (1+km)(1-km) |
| 459 // x[y(2-xy)] = 1-k^2m^2 |
| 460 // x[y(2-xy)] == 1 (mod m^2) |
| 461 // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2 |
| 462 // should reduce x and y(2-xy) by m^2 at each step to keep size bounded. |
| 463 // JS multiply "overflows" differently from C/C++, so care is needed here. |
| 464 function bnpInvDigit() { |
| 465 if(this.t < 1) return 0; |
| 466 var x = this[0]; |
| 467 if((x&1) == 0) return 0; |
| 468 var y = x&3; // y == 1/x mod 2^2 |
| 469 y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4 |
| 470 y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8 |
| 471 y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16 |
| 472 // last step - calculate inverse mod DV directly; |
| 473 // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints |
| 474 y = (y*(2-x*y%this.DV))%this.DV; // y == 1/x mod 2^dbits |
| 475 // we really want the negative inverse, and -DV < y < DV |
| 476 return (y>0)?this.DV-y:-y; |
| 477 } |
| 478 |
| 479 // Montgomery reduction |
| 480 function Montgomery(m) { |
| 481 this.m = m; |
| 482 this.mp = m.invDigit(); |
| 483 this.mpl = this.mp&0x7fff; |
| 484 this.mph = this.mp>>15; |
| 485 this.um = (1<<(m.DB-15))-1; |
| 486 this.mt2 = 2*m.t; |
| 487 } |
| 488 |
| 489 // xR mod m |
| 490 function montConvert(x) { |
| 491 var r = nbi(); |
| 492 x.abs().dlShiftTo(this.m.t,r); |
| 493 r.divRemTo(this.m,null,r); |
| 494 if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r); |
| 495 return r; |
| 496 } |
| 497 |
| 498 // x/R mod m |
| 499 function montRevert(x) { |
| 500 var r = nbi(); |
| 501 x.copyTo(r); |
| 502 this.reduce(r); |
| 503 return r; |
| 504 } |
| 505 |
| 506 // x = x/R mod m (HAC 14.32) |
| 507 function montReduce(x) { |
| 508 while(x.t <= this.mt2) // pad x so am has enough room later |
| 509 x[x.t++] = 0; |
| 510 for(var i = 0; i < this.m.t; ++i) { |
| 511 // faster way of calculating u0 = x[i]*mp mod DV |
| 512 var j = x[i]&0x7fff; |
| 513 var u0 = (j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.DM; |
| 514 // use am to combine the multiply-shift-add into one call |
| 515 j = i+this.m.t; |
| 516 x[j] += this.m.am(0,u0,x,i,0,this.m.t); |
| 517 // propagate carry |
| 518 while(x[j] >= x.DV) { x[j] -= x.DV; x[++j]++; } |
| 519 } |
| 520 x.clamp(); |
| 521 x.drShiftTo(this.m.t,x); |
| 522 if(x.compareTo(this.m) >= 0) x.subTo(this.m,x); |
| 523 } |
| 524 |
| 525 // r = "x^2/R mod m"; x != r |
| 526 function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); } |
| 527 |
| 528 // r = "xy/R mod m"; x,y != r |
| 529 function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } |
| 530 |
| 531 Montgomery.prototype.convert = montConvert; |
| 532 Montgomery.prototype.revert = montRevert; |
| 533 Montgomery.prototype.reduce = montReduce; |
| 534 Montgomery.prototype.mulTo = montMulTo; |
| 535 Montgomery.prototype.sqrTo = montSqrTo; |
| 536 |
| 537 // (protected) true iff this is even |
| 538 function bnpIsEven() { return ((this.t>0)?(this[0]&1):this.s) == 0; } |
| 539 |
| 540 // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79) |
| 541 function bnpExp(e,z) { |
| 542 if(e > 0xffffffff || e < 1) return BigInteger.ONE; |
| 543 var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1; |
| 544 g.copyTo(r); |
| 545 while(--i >= 0) { |
| 546 z.sqrTo(r,r2); |
| 547 if((e&(1<<i)) > 0) z.mulTo(r2,g,r); |
| 548 else { var t = r; r = r2; r2 = t; } |
| 549 } |
| 550 return z.revert(r); |
| 551 } |
| 552 |
| 553 // (public) this^e % m, 0 <= e < 2^32 |
| 554 function bnModPowInt(e,m) { |
| 555 var z; |
| 556 if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m); |
| 557 return this.exp(e,z); |
| 558 } |
| 559 |
| 560 // protected |
| 561 BigInteger.prototype.copyTo = bnpCopyTo; |
| 562 BigInteger.prototype.fromInt = bnpFromInt; |
| 563 BigInteger.prototype.fromString = bnpFromString; |
| 564 BigInteger.prototype.clamp = bnpClamp; |
| 565 BigInteger.prototype.dlShiftTo = bnpDLShiftTo; |
| 566 BigInteger.prototype.drShiftTo = bnpDRShiftTo; |
| 567 BigInteger.prototype.lShiftTo = bnpLShiftTo; |
| 568 BigInteger.prototype.rShiftTo = bnpRShiftTo; |
| 569 BigInteger.prototype.subTo = bnpSubTo; |
| 570 BigInteger.prototype.multiplyTo = bnpMultiplyTo; |
| 571 BigInteger.prototype.squareTo = bnpSquareTo; |
| 572 BigInteger.prototype.divRemTo = bnpDivRemTo; |
| 573 BigInteger.prototype.invDigit = bnpInvDigit; |
| 574 BigInteger.prototype.isEven = bnpIsEven; |
| 575 BigInteger.prototype.exp = bnpExp; |
| 576 |
| 577 // public |
| 578 BigInteger.prototype.toString = bnToString; |
| 579 BigInteger.prototype.negate = bnNegate; |
| 580 BigInteger.prototype.abs = bnAbs; |
| 581 BigInteger.prototype.compareTo = bnCompareTo; |
| 582 BigInteger.prototype.bitLength = bnBitLength; |
| 583 BigInteger.prototype.mod = bnMod; |
| 584 BigInteger.prototype.modPowInt = bnModPowInt; |
| 585 |
| 586 // "constants" |
| 587 BigInteger.ZERO = nbv(0); |
| 588 BigInteger.ONE = nbv(1); |
OLD | NEW |