[483] | 1 | // AMD-ID "dojox/math/BigInteger-ext" |
---|
| 2 | define(["dojo", "dojox", "dojox/math/BigInteger"], function(dojo, dojox) { |
---|
| 3 | dojo.experimental("dojox.math.BigInteger-ext"); |
---|
| 4 | |
---|
| 5 | // Contributed under CLA by Tom Wu |
---|
| 6 | |
---|
| 7 | // Extended JavaScript BN functions, required for RSA private ops. |
---|
| 8 | var BigInteger = dojox.math.BigInteger, |
---|
| 9 | nbi = BigInteger._nbi, nbv = BigInteger._nbv, |
---|
| 10 | nbits = BigInteger._nbits, |
---|
| 11 | Montgomery = BigInteger._Montgomery; |
---|
| 12 | |
---|
| 13 | // (public) |
---|
| 14 | function bnClone() { var r = nbi(); this._copyTo(r); return r; } |
---|
| 15 | |
---|
| 16 | // (public) return value as integer |
---|
| 17 | function bnIntValue() { |
---|
| 18 | if(this.s < 0) { |
---|
| 19 | if(this.t == 1) return this[0]-this._DV; |
---|
| 20 | else if(this.t == 0) return -1; |
---|
| 21 | } |
---|
| 22 | else if(this.t == 1) return this[0]; |
---|
| 23 | else if(this.t == 0) return 0; |
---|
| 24 | // assumes 16 < DB < 32 |
---|
| 25 | return ((this[1]&((1<<(32-this._DB))-1))<<this._DB)|this[0]; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | // (public) return value as byte |
---|
| 29 | function bnByteValue() { return (this.t==0)?this.s:(this[0]<<24)>>24; } |
---|
| 30 | |
---|
| 31 | // (public) return value as short (assumes DB>=16) |
---|
| 32 | function bnShortValue() { return (this.t==0)?this.s:(this[0]<<16)>>16; } |
---|
| 33 | |
---|
| 34 | // (protected) return x s.t. r^x < DV |
---|
| 35 | function bnpChunkSize(r) { return Math.floor(Math.LN2*this._DB/Math.log(r)); } |
---|
| 36 | |
---|
| 37 | // (public) 0 if this == 0, 1 if this > 0 |
---|
| 38 | function bnSigNum() { |
---|
| 39 | if(this.s < 0) return -1; |
---|
| 40 | else if(this.t <= 0 || (this.t == 1 && this[0] <= 0)) return 0; |
---|
| 41 | else return 1; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | // (protected) convert to radix string |
---|
| 45 | function bnpToRadix(b) { |
---|
| 46 | if(b == null) b = 10; |
---|
| 47 | if(this.signum() == 0 || b < 2 || b > 36) return "0"; |
---|
| 48 | var cs = this._chunkSize(b); |
---|
| 49 | var a = Math.pow(b,cs); |
---|
| 50 | var d = nbv(a), y = nbi(), z = nbi(), r = ""; |
---|
| 51 | this._divRemTo(d,y,z); |
---|
| 52 | while(y.signum() > 0) { |
---|
| 53 | r = (a+z.intValue()).toString(b).substr(1) + r; |
---|
| 54 | y._divRemTo(d,y,z); |
---|
| 55 | } |
---|
| 56 | return z.intValue().toString(b) + r; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | // (protected) convert from radix string |
---|
| 60 | function bnpFromRadix(s,b) { |
---|
| 61 | this._fromInt(0); |
---|
| 62 | if(b == null) b = 10; |
---|
| 63 | var cs = this._chunkSize(b); |
---|
| 64 | var d = Math.pow(b,cs), mi = false, j = 0, w = 0; |
---|
| 65 | for(var i = 0; i < s.length; ++i) { |
---|
| 66 | var x = intAt(s,i); |
---|
| 67 | if(x < 0) { |
---|
| 68 | if(s.charAt(i) == "-" && this.signum() == 0) mi = true; |
---|
| 69 | continue; |
---|
| 70 | } |
---|
| 71 | w = b*w+x; |
---|
| 72 | if(++j >= cs) { |
---|
| 73 | this._dMultiply(d); |
---|
| 74 | this._dAddOffset(w,0); |
---|
| 75 | j = 0; |
---|
| 76 | w = 0; |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | if(j > 0) { |
---|
| 80 | this._dMultiply(Math.pow(b,j)); |
---|
| 81 | this._dAddOffset(w,0); |
---|
| 82 | } |
---|
| 83 | if(mi) BigInteger.ZERO._subTo(this,this); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | // (protected) alternate constructor |
---|
| 87 | function bnpFromNumber(a,b,c) { |
---|
| 88 | if("number" == typeof b) { |
---|
| 89 | // new BigInteger(int,int,RNG) |
---|
| 90 | if(a < 2) this._fromInt(1); |
---|
| 91 | else { |
---|
| 92 | this._fromNumber(a,c); |
---|
| 93 | if(!this.testBit(a-1)) // force MSB set |
---|
| 94 | this._bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this); |
---|
| 95 | if(this._isEven()) this._dAddOffset(1,0); // force odd |
---|
| 96 | while(!this.isProbablePrime(b)) { |
---|
| 97 | this._dAddOffset(2,0); |
---|
| 98 | if(this.bitLength() > a) this._subTo(BigInteger.ONE.shiftLeft(a-1),this); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | else { |
---|
| 103 | // new BigInteger(int,RNG) |
---|
| 104 | var x = [], t = a&7; |
---|
| 105 | x.length = (a>>3)+1; |
---|
| 106 | b.nextBytes(x); |
---|
| 107 | if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0; |
---|
| 108 | this._fromString(x,256); |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | // (public) convert to bigendian byte array |
---|
| 113 | function bnToByteArray() { |
---|
| 114 | var i = this.t, r = []; |
---|
| 115 | r[0] = this.s; |
---|
| 116 | var p = this._DB-(i*this._DB)%8, d, k = 0; |
---|
| 117 | if(i-- > 0) { |
---|
| 118 | if(p < this._DB && (d = this[i]>>p) != (this.s&this._DM)>>p) |
---|
| 119 | r[k++] = d|(this.s<<(this._DB-p)); |
---|
| 120 | while(i >= 0) { |
---|
| 121 | if(p < 8) { |
---|
| 122 | d = (this[i]&((1<<p)-1))<<(8-p); |
---|
| 123 | d |= this[--i]>>(p+=this._DB-8); |
---|
| 124 | } |
---|
| 125 | else { |
---|
| 126 | d = (this[i]>>(p-=8))&0xff; |
---|
| 127 | if(p <= 0) { p += this._DB; --i; } |
---|
| 128 | } |
---|
| 129 | if((d&0x80) != 0) d |= -256; |
---|
| 130 | if(k == 0 && (this.s&0x80) != (d&0x80)) ++k; |
---|
| 131 | if(k > 0 || d != this.s) r[k++] = d; |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | return r; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | function bnEquals(a) { return(this.compareTo(a)==0); } |
---|
| 138 | function bnMin(a) { return(this.compareTo(a)<0)?this:a; } |
---|
| 139 | function bnMax(a) { return(this.compareTo(a)>0)?this:a; } |
---|
| 140 | |
---|
| 141 | // (protected) r = this op a (bitwise) |
---|
| 142 | function bnpBitwiseTo(a,op,r) { |
---|
| 143 | var i, f, m = Math.min(a.t,this.t); |
---|
| 144 | for(i = 0; i < m; ++i) r[i] = op(this[i],a[i]); |
---|
| 145 | if(a.t < this.t) { |
---|
| 146 | f = a.s&this._DM; |
---|
| 147 | for(i = m; i < this.t; ++i) r[i] = op(this[i],f); |
---|
| 148 | r.t = this.t; |
---|
| 149 | } |
---|
| 150 | else { |
---|
| 151 | f = this.s&this._DM; |
---|
| 152 | for(i = m; i < a.t; ++i) r[i] = op(f,a[i]); |
---|
| 153 | r.t = a.t; |
---|
| 154 | } |
---|
| 155 | r.s = op(this.s,a.s); |
---|
| 156 | r._clamp(); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | // (public) this & a |
---|
| 160 | function op_and(x,y) { return x&y; } |
---|
| 161 | function bnAnd(a) { var r = nbi(); this._bitwiseTo(a,op_and,r); return r; } |
---|
| 162 | |
---|
| 163 | // (public) this | a |
---|
| 164 | function op_or(x,y) { return x|y; } |
---|
| 165 | function bnOr(a) { var r = nbi(); this._bitwiseTo(a,op_or,r); return r; } |
---|
| 166 | |
---|
| 167 | // (public) this ^ a |
---|
| 168 | function op_xor(x,y) { return x^y; } |
---|
| 169 | function bnXor(a) { var r = nbi(); this._bitwiseTo(a,op_xor,r); return r; } |
---|
| 170 | |
---|
| 171 | // (public) this & ~a |
---|
| 172 | function op_andnot(x,y) { return x&~y; } |
---|
| 173 | function bnAndNot(a) { var r = nbi(); this._bitwiseTo(a,op_andnot,r); return r; } |
---|
| 174 | |
---|
| 175 | // (public) ~this |
---|
| 176 | function bnNot() { |
---|
| 177 | var r = nbi(); |
---|
| 178 | for(var i = 0; i < this.t; ++i) r[i] = this._DM&~this[i]; |
---|
| 179 | r.t = this.t; |
---|
| 180 | r.s = ~this.s; |
---|
| 181 | return r; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | // (public) this << n |
---|
| 185 | function bnShiftLeft(n) { |
---|
| 186 | var r = nbi(); |
---|
| 187 | if(n < 0) this._rShiftTo(-n,r); else this._lShiftTo(n,r); |
---|
| 188 | return r; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | // (public) this >> n |
---|
| 192 | function bnShiftRight(n) { |
---|
| 193 | var r = nbi(); |
---|
| 194 | if(n < 0) this._lShiftTo(-n,r); else this._rShiftTo(n,r); |
---|
| 195 | return r; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | // return index of lowest 1-bit in x, x < 2^31 |
---|
| 199 | function lbit(x) { |
---|
| 200 | if(x == 0) return -1; |
---|
| 201 | var r = 0; |
---|
| 202 | if((x&0xffff) == 0) { x >>= 16; r += 16; } |
---|
| 203 | if((x&0xff) == 0) { x >>= 8; r += 8; } |
---|
| 204 | if((x&0xf) == 0) { x >>= 4; r += 4; } |
---|
| 205 | if((x&3) == 0) { x >>= 2; r += 2; } |
---|
| 206 | if((x&1) == 0) ++r; |
---|
| 207 | return r; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | // (public) returns index of lowest 1-bit (or -1 if none) |
---|
| 211 | function bnGetLowestSetBit() { |
---|
| 212 | for(var i = 0; i < this.t; ++i) |
---|
| 213 | if(this[i] != 0) return i*this._DB+lbit(this[i]); |
---|
| 214 | if(this.s < 0) return this.t*this._DB; |
---|
| 215 | return -1; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | // return number of 1 bits in x |
---|
| 219 | function cbit(x) { |
---|
| 220 | var r = 0; |
---|
| 221 | while(x != 0) { x &= x-1; ++r; } |
---|
| 222 | return r; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | // (public) return number of set bits |
---|
| 226 | function bnBitCount() { |
---|
| 227 | var r = 0, x = this.s&this._DM; |
---|
| 228 | for(var i = 0; i < this.t; ++i) r += cbit(this[i]^x); |
---|
| 229 | return r; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | // (public) true iff nth bit is set |
---|
| 233 | function bnTestBit(n) { |
---|
| 234 | var j = Math.floor(n/this._DB); |
---|
| 235 | if(j >= this.t) return(this.s!=0); |
---|
| 236 | return((this[j]&(1<<(n%this._DB)))!=0); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | // (protected) this op (1<<n) |
---|
| 240 | function bnpChangeBit(n,op) { |
---|
| 241 | var r = BigInteger.ONE.shiftLeft(n); |
---|
| 242 | this._bitwiseTo(r,op,r); |
---|
| 243 | return r; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | // (public) this | (1<<n) |
---|
| 247 | function bnSetBit(n) { return this._changeBit(n,op_or); } |
---|
| 248 | |
---|
| 249 | // (public) this & ~(1<<n) |
---|
| 250 | function bnClearBit(n) { return this._changeBit(n,op_andnot); } |
---|
| 251 | |
---|
| 252 | // (public) this ^ (1<<n) |
---|
| 253 | function bnFlipBit(n) { return this._changeBit(n,op_xor); } |
---|
| 254 | |
---|
| 255 | // (protected) r = this + a |
---|
| 256 | function bnpAddTo(a,r) { |
---|
| 257 | var i = 0, c = 0, m = Math.min(a.t,this.t); |
---|
| 258 | while(i < m) { |
---|
| 259 | c += this[i]+a[i]; |
---|
| 260 | r[i++] = c&this._DM; |
---|
| 261 | c >>= this._DB; |
---|
| 262 | } |
---|
| 263 | if(a.t < this.t) { |
---|
| 264 | c += a.s; |
---|
| 265 | while(i < this.t) { |
---|
| 266 | c += this[i]; |
---|
| 267 | r[i++] = c&this._DM; |
---|
| 268 | c >>= this._DB; |
---|
| 269 | } |
---|
| 270 | c += this.s; |
---|
| 271 | } |
---|
| 272 | else { |
---|
| 273 | c += this.s; |
---|
| 274 | while(i < a.t) { |
---|
| 275 | c += a[i]; |
---|
| 276 | r[i++] = c&this._DM; |
---|
| 277 | c >>= this._DB; |
---|
| 278 | } |
---|
| 279 | c += a.s; |
---|
| 280 | } |
---|
| 281 | r.s = (c<0)?-1:0; |
---|
| 282 | if(c > 0) r[i++] = c; |
---|
| 283 | else if(c < -1) r[i++] = this._DV+c; |
---|
| 284 | r.t = i; |
---|
| 285 | r._clamp(); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | // (public) this + a |
---|
| 289 | function bnAdd(a) { var r = nbi(); this._addTo(a,r); return r; } |
---|
| 290 | |
---|
| 291 | // (public) this - a |
---|
| 292 | function bnSubtract(a) { var r = nbi(); this._subTo(a,r); return r; } |
---|
| 293 | |
---|
| 294 | // (public) this * a |
---|
| 295 | function bnMultiply(a) { var r = nbi(); this._multiplyTo(a,r); return r; } |
---|
| 296 | |
---|
| 297 | // (public) this / a |
---|
| 298 | function bnDivide(a) { var r = nbi(); this._divRemTo(a,r,null); return r; } |
---|
| 299 | |
---|
| 300 | // (public) this % a |
---|
| 301 | function bnRemainder(a) { var r = nbi(); this._divRemTo(a,null,r); return r; } |
---|
| 302 | |
---|
| 303 | // (public) [this/a,this%a] |
---|
| 304 | function bnDivideAndRemainder(a) { |
---|
| 305 | var q = nbi(), r = nbi(); |
---|
| 306 | this._divRemTo(a,q,r); |
---|
| 307 | return [q, r]; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | // (protected) this *= n, this >= 0, 1 < n < DV |
---|
| 311 | function bnpDMultiply(n) { |
---|
| 312 | this[this.t] = this.am(0,n-1,this,0,0,this.t); |
---|
| 313 | ++this.t; |
---|
| 314 | this._clamp(); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | // (protected) this += n << w words, this >= 0 |
---|
| 318 | function bnpDAddOffset(n,w) { |
---|
| 319 | while(this.t <= w) this[this.t++] = 0; |
---|
| 320 | this[w] += n; |
---|
| 321 | while(this[w] >= this._DV) { |
---|
| 322 | this[w] -= this._DV; |
---|
| 323 | if(++w >= this.t) this[this.t++] = 0; |
---|
| 324 | ++this[w]; |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | // A "null" reducer |
---|
| 329 | function NullExp() {} |
---|
| 330 | function nNop(x) { return x; } |
---|
| 331 | function nMulTo(x,y,r) { x._multiplyTo(y,r); } |
---|
| 332 | function nSqrTo(x,r) { x._squareTo(r); } |
---|
| 333 | |
---|
| 334 | NullExp.prototype.convert = nNop; |
---|
| 335 | NullExp.prototype.revert = nNop; |
---|
| 336 | NullExp.prototype.mulTo = nMulTo; |
---|
| 337 | NullExp.prototype.sqrTo = nSqrTo; |
---|
| 338 | |
---|
| 339 | // (public) this^e |
---|
| 340 | function bnPow(e) { return this._exp(e,new NullExp()); } |
---|
| 341 | |
---|
| 342 | // (protected) r = lower n words of "this * a", a.t <= n |
---|
| 343 | // "this" should be the larger one if appropriate. |
---|
| 344 | function bnpMultiplyLowerTo(a,n,r) { |
---|
| 345 | var i = Math.min(this.t+a.t,n); |
---|
| 346 | r.s = 0; // assumes a,this >= 0 |
---|
| 347 | r.t = i; |
---|
| 348 | while(i > 0) r[--i] = 0; |
---|
| 349 | var j; |
---|
| 350 | for(j = r.t-this.t; i < j; ++i) r[i+this.t] = this.am(0,a[i],r,i,0,this.t); |
---|
| 351 | for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a[i],r,i,0,n-i); |
---|
| 352 | r._clamp(); |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | // (protected) r = "this * a" without lower n words, n > 0 |
---|
| 356 | // "this" should be the larger one if appropriate. |
---|
| 357 | function bnpMultiplyUpperTo(a,n,r) { |
---|
| 358 | --n; |
---|
| 359 | var i = r.t = this.t+a.t-n; |
---|
| 360 | r.s = 0; // assumes a,this >= 0 |
---|
| 361 | while(--i >= 0) r[i] = 0; |
---|
| 362 | for(i = Math.max(n-this.t,0); i < a.t; ++i) |
---|
| 363 | r[this.t+i-n] = this.am(n-i,a[i],r,0,0,this.t+i-n); |
---|
| 364 | r._clamp(); |
---|
| 365 | r._drShiftTo(1,r); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | // Barrett modular reduction |
---|
| 369 | function Barrett(m) { |
---|
| 370 | // setup Barrett |
---|
| 371 | this.r2 = nbi(); |
---|
| 372 | this.q3 = nbi(); |
---|
| 373 | BigInteger.ONE._dlShiftTo(2*m.t,this.r2); |
---|
| 374 | this.mu = this.r2.divide(m); |
---|
| 375 | this.m = m; |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | function barrettConvert(x) { |
---|
| 379 | if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m); |
---|
| 380 | else if(x.compareTo(this.m) < 0) return x; |
---|
| 381 | else { var r = nbi(); x._copyTo(r); this.reduce(r); return r; } |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | function barrettRevert(x) { return x; } |
---|
| 385 | |
---|
| 386 | // x = x mod m (HAC 14.42) |
---|
| 387 | function barrettReduce(x) { |
---|
| 388 | x._drShiftTo(this.m.t-1,this.r2); |
---|
| 389 | if(x.t > this.m.t+1) { x.t = this.m.t+1; x._clamp(); } |
---|
| 390 | this.mu._multiplyUpperTo(this.r2,this.m.t+1,this.q3); |
---|
| 391 | this.m._multiplyLowerTo(this.q3,this.m.t+1,this.r2); |
---|
| 392 | while(x.compareTo(this.r2) < 0) x._dAddOffset(1,this.m.t+1); |
---|
| 393 | x._subTo(this.r2,x); |
---|
| 394 | while(x.compareTo(this.m) >= 0) x._subTo(this.m,x); |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | // r = x^2 mod m; x != r |
---|
| 398 | function barrettSqrTo(x,r) { x._squareTo(r); this.reduce(r); } |
---|
| 399 | |
---|
| 400 | // r = x*y mod m; x,y != r |
---|
| 401 | function barrettMulTo(x,y,r) { x._multiplyTo(y,r); this.reduce(r); } |
---|
| 402 | |
---|
| 403 | Barrett.prototype.convert = barrettConvert; |
---|
| 404 | Barrett.prototype.revert = barrettRevert; |
---|
| 405 | Barrett.prototype.reduce = barrettReduce; |
---|
| 406 | Barrett.prototype.mulTo = barrettMulTo; |
---|
| 407 | Barrett.prototype.sqrTo = barrettSqrTo; |
---|
| 408 | |
---|
| 409 | // (public) this^e % m (HAC 14.85) |
---|
| 410 | function bnModPow(e,m) { |
---|
| 411 | var i = e.bitLength(), k, r = nbv(1), z; |
---|
| 412 | if(i <= 0) return r; |
---|
| 413 | else if(i < 18) k = 1; |
---|
| 414 | else if(i < 48) k = 3; |
---|
| 415 | else if(i < 144) k = 4; |
---|
| 416 | else if(i < 768) k = 5; |
---|
| 417 | else k = 6; |
---|
| 418 | if(i < 8) |
---|
| 419 | z = new Classic(m); |
---|
| 420 | else if(m._isEven()) |
---|
| 421 | z = new Barrett(m); |
---|
| 422 | else |
---|
| 423 | z = new Montgomery(m); |
---|
| 424 | |
---|
| 425 | // precomputation |
---|
| 426 | var g = [], n = 3, k1 = k-1, km = (1<<k)-1; |
---|
| 427 | g[1] = z.convert(this); |
---|
| 428 | if(k > 1) { |
---|
| 429 | var g2 = nbi(); |
---|
| 430 | z.sqrTo(g[1],g2); |
---|
| 431 | while(n <= km) { |
---|
| 432 | g[n] = nbi(); |
---|
| 433 | z.mulTo(g2,g[n-2],g[n]); |
---|
| 434 | n += 2; |
---|
| 435 | } |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | var j = e.t-1, w, is1 = true, r2 = nbi(), t; |
---|
| 439 | i = nbits(e[j])-1; |
---|
| 440 | while(j >= 0) { |
---|
| 441 | if(i >= k1) w = (e[j]>>(i-k1))&km; |
---|
| 442 | else { |
---|
| 443 | w = (e[j]&((1<<(i+1))-1))<<(k1-i); |
---|
| 444 | if(j > 0) w |= e[j-1]>>(this._DB+i-k1); |
---|
| 445 | } |
---|
| 446 | |
---|
| 447 | n = k; |
---|
| 448 | while((w&1) == 0) { w >>= 1; --n; } |
---|
| 449 | if((i -= n) < 0) { i += this._DB; --j; } |
---|
| 450 | if(is1) { // ret == 1, don't bother squaring or multiplying it |
---|
| 451 | g[w]._copyTo(r); |
---|
| 452 | is1 = false; |
---|
| 453 | } |
---|
| 454 | else { |
---|
| 455 | while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; } |
---|
| 456 | if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; } |
---|
| 457 | z.mulTo(r2,g[w],r); |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | while(j >= 0 && (e[j]&(1<<i)) == 0) { |
---|
| 461 | z.sqrTo(r,r2); t = r; r = r2; r2 = t; |
---|
| 462 | if(--i < 0) { i = this._DB-1; --j; } |
---|
| 463 | } |
---|
| 464 | } |
---|
| 465 | return z.revert(r); |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | // (public) gcd(this,a) (HAC 14.54) |
---|
| 469 | function bnGCD(a) { |
---|
| 470 | var x = (this.s<0)?this.negate():this.clone(); |
---|
| 471 | var y = (a.s<0)?a.negate():a.clone(); |
---|
| 472 | if(x.compareTo(y) < 0) { var t = x; x = y; y = t; } |
---|
| 473 | var i = x.getLowestSetBit(), g = y.getLowestSetBit(); |
---|
| 474 | if(g < 0) return x; |
---|
| 475 | if(i < g) g = i; |
---|
| 476 | if(g > 0) { |
---|
| 477 | x._rShiftTo(g,x); |
---|
| 478 | y._rShiftTo(g,y); |
---|
| 479 | } |
---|
| 480 | while(x.signum() > 0) { |
---|
| 481 | if((i = x.getLowestSetBit()) > 0) x._rShiftTo(i,x); |
---|
| 482 | if((i = y.getLowestSetBit()) > 0) y._rShiftTo(i,y); |
---|
| 483 | if(x.compareTo(y) >= 0) { |
---|
| 484 | x._subTo(y,x); |
---|
| 485 | x._rShiftTo(1,x); |
---|
| 486 | } |
---|
| 487 | else { |
---|
| 488 | y._subTo(x,y); |
---|
| 489 | y._rShiftTo(1,y); |
---|
| 490 | } |
---|
| 491 | } |
---|
| 492 | if(g > 0) y._lShiftTo(g,y); |
---|
| 493 | return y; |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | // (protected) this % n, n < 2^26 |
---|
| 497 | function bnpModInt(n) { |
---|
| 498 | if(n <= 0) return 0; |
---|
| 499 | var d = this._DV%n, r = (this.s<0)?n-1:0; |
---|
| 500 | if(this.t > 0) |
---|
| 501 | if(d == 0) r = this[0]%n; |
---|
| 502 | else for(var i = this.t-1; i >= 0; --i) r = (d*r+this[i])%n; |
---|
| 503 | return r; |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | // (public) 1/this % m (HAC 14.61) |
---|
| 507 | function bnModInverse(m) { |
---|
| 508 | var ac = m._isEven(); |
---|
| 509 | if((this._isEven() && ac) || m.signum() == 0) return BigInteger.ZERO; |
---|
| 510 | var u = m.clone(), v = this.clone(); |
---|
| 511 | var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1); |
---|
| 512 | while(u.signum() != 0) { |
---|
| 513 | while(u._isEven()) { |
---|
| 514 | u._rShiftTo(1,u); |
---|
| 515 | if(ac) { |
---|
| 516 | if(!a._isEven() || !b._isEven()) { a._addTo(this,a); b._subTo(m,b); } |
---|
| 517 | a._rShiftTo(1,a); |
---|
| 518 | } |
---|
| 519 | else if(!b._isEven()) b._subTo(m,b); |
---|
| 520 | b._rShiftTo(1,b); |
---|
| 521 | } |
---|
| 522 | while(v._isEven()) { |
---|
| 523 | v._rShiftTo(1,v); |
---|
| 524 | if(ac) { |
---|
| 525 | if(!c._isEven() || !d._isEven()) { c._addTo(this,c); d._subTo(m,d); } |
---|
| 526 | c._rShiftTo(1,c); |
---|
| 527 | } |
---|
| 528 | else if(!d._isEven()) d._subTo(m,d); |
---|
| 529 | d._rShiftTo(1,d); |
---|
| 530 | } |
---|
| 531 | if(u.compareTo(v) >= 0) { |
---|
| 532 | u._subTo(v,u); |
---|
| 533 | if(ac) a._subTo(c,a); |
---|
| 534 | b._subTo(d,b); |
---|
| 535 | } |
---|
| 536 | else { |
---|
| 537 | v._subTo(u,v); |
---|
| 538 | if(ac) c._subTo(a,c); |
---|
| 539 | d._subTo(b,d); |
---|
| 540 | } |
---|
| 541 | } |
---|
| 542 | if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO; |
---|
| 543 | if(d.compareTo(m) >= 0) return d.subtract(m); |
---|
| 544 | if(d.signum() < 0) d._addTo(m,d); else return d; |
---|
| 545 | if(d.signum() < 0) return d.add(m); else return d; |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | 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]; |
---|
| 549 | var lplim = (1<<26)/lowprimes[lowprimes.length-1]; |
---|
| 550 | |
---|
| 551 | // (public) test primality with certainty >= 1-.5^t |
---|
| 552 | function bnIsProbablePrime(t) { |
---|
| 553 | var i, x = this.abs(); |
---|
| 554 | if(x.t == 1 && x[0] <= lowprimes[lowprimes.length-1]) { |
---|
| 555 | for(i = 0; i < lowprimes.length; ++i) |
---|
| 556 | if(x[0] == lowprimes[i]) return true; |
---|
| 557 | return false; |
---|
| 558 | } |
---|
| 559 | if(x._isEven()) return false; |
---|
| 560 | i = 1; |
---|
| 561 | while(i < lowprimes.length) { |
---|
| 562 | var m = lowprimes[i], j = i+1; |
---|
| 563 | while(j < lowprimes.length && m < lplim) m *= lowprimes[j++]; |
---|
| 564 | m = x._modInt(m); |
---|
| 565 | while(i < j) if(m%lowprimes[i++] == 0) return false; |
---|
| 566 | } |
---|
| 567 | return x._millerRabin(t); |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | // (protected) true if probably prime (HAC 4.24, Miller-Rabin) |
---|
| 571 | function bnpMillerRabin(t) { |
---|
| 572 | var n1 = this.subtract(BigInteger.ONE); |
---|
| 573 | var k = n1.getLowestSetBit(); |
---|
| 574 | if(k <= 0) return false; |
---|
| 575 | var r = n1.shiftRight(k); |
---|
| 576 | t = (t+1)>>1; |
---|
| 577 | if(t > lowprimes.length) t = lowprimes.length; |
---|
| 578 | var a = nbi(); |
---|
| 579 | for(var i = 0; i < t; ++i) { |
---|
| 580 | a._fromInt(lowprimes[i]); |
---|
| 581 | var y = a.modPow(r,this); |
---|
| 582 | if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) { |
---|
| 583 | var j = 1; |
---|
| 584 | while(j++ < k && y.compareTo(n1) != 0) { |
---|
| 585 | y = y.modPowInt(2,this); |
---|
| 586 | if(y.compareTo(BigInteger.ONE) == 0) return false; |
---|
| 587 | } |
---|
| 588 | if(y.compareTo(n1) != 0) return false; |
---|
| 589 | } |
---|
| 590 | } |
---|
| 591 | return true; |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | dojo.extend(BigInteger, { |
---|
| 595 | // protected |
---|
| 596 | _chunkSize: bnpChunkSize, |
---|
| 597 | _toRadix: bnpToRadix, |
---|
| 598 | _fromRadix: bnpFromRadix, |
---|
| 599 | _fromNumber: bnpFromNumber, |
---|
| 600 | _bitwiseTo: bnpBitwiseTo, |
---|
| 601 | _changeBit: bnpChangeBit, |
---|
| 602 | _addTo: bnpAddTo, |
---|
| 603 | _dMultiply: bnpDMultiply, |
---|
| 604 | _dAddOffset: bnpDAddOffset, |
---|
| 605 | _multiplyLowerTo: bnpMultiplyLowerTo, |
---|
| 606 | _multiplyUpperTo: bnpMultiplyUpperTo, |
---|
| 607 | _modInt: bnpModInt, |
---|
| 608 | _millerRabin: bnpMillerRabin, |
---|
| 609 | |
---|
| 610 | // public |
---|
| 611 | clone: bnClone, |
---|
| 612 | intValue: bnIntValue, |
---|
| 613 | byteValue: bnByteValue, |
---|
| 614 | shortValue: bnShortValue, |
---|
| 615 | signum: bnSigNum, |
---|
| 616 | toByteArray: bnToByteArray, |
---|
| 617 | equals: bnEquals, |
---|
| 618 | min: bnMin, |
---|
| 619 | max: bnMax, |
---|
| 620 | and: bnAnd, |
---|
| 621 | or: bnOr, |
---|
| 622 | xor: bnXor, |
---|
| 623 | andNot: bnAndNot, |
---|
| 624 | not: bnNot, |
---|
| 625 | shiftLeft: bnShiftLeft, |
---|
| 626 | shiftRight: bnShiftRight, |
---|
| 627 | getLowestSetBit: bnGetLowestSetBit, |
---|
| 628 | bitCount: bnBitCount, |
---|
| 629 | testBit: bnTestBit, |
---|
| 630 | setBit: bnSetBit, |
---|
| 631 | clearBit: bnClearBit, |
---|
| 632 | flipBit: bnFlipBit, |
---|
| 633 | add: bnAdd, |
---|
| 634 | subtract: bnSubtract, |
---|
| 635 | multiply: bnMultiply, |
---|
| 636 | divide: bnDivide, |
---|
| 637 | remainder: bnRemainder, |
---|
| 638 | divideAndRemainder: bnDivideAndRemainder, |
---|
| 639 | modPow: bnModPow, |
---|
| 640 | modInverse: bnModInverse, |
---|
| 641 | pow: bnPow, |
---|
| 642 | gcd: bnGCD, |
---|
| 643 | isProbablePrime: bnIsProbablePrime |
---|
| 644 | }); |
---|
| 645 | |
---|
| 646 | // BigInteger interfaces not implemented in jsbn: |
---|
| 647 | |
---|
| 648 | // BigInteger(int signum, byte[] magnitude) |
---|
| 649 | // double doubleValue() |
---|
| 650 | // float floatValue() |
---|
| 651 | // int hashCode() |
---|
| 652 | // long longValue() |
---|
| 653 | // static BigInteger valueOf(long val) |
---|
| 654 | |
---|
| 655 | return dojox.math.BigInteger; |
---|
| 656 | }); |
---|