| [9af201e] | 1 | // Copyright (c) 2005 Tom Wu
|
|---|
| 2 | // All Rights Reserved.
|
|---|
| 3 | // See "LICENSE" for details.
|
|---|
| 4 |
|
|---|
| 5 | // Basic JavaScript BN library - subset useful for RSA encryption.
|
|---|
| 6 |
|
|---|
| 7 | /*
|
|---|
| 8 | Licensing (LICENSE)
|
|---|
| 9 | -------------------
|
|---|
| 10 |
|
|---|
| 11 | This software is covered under the following copyright:
|
|---|
| 12 | */
|
|---|
| 13 | /*
|
|---|
| 14 | * Copyright (c) 2003-2005 Tom Wu
|
|---|
| 15 | * All Rights Reserved.
|
|---|
| 16 | *
|
|---|
| 17 | * Permission is hereby granted, free of charge, to any person obtaining
|
|---|
| 18 | * a copy of this software and associated documentation files (the
|
|---|
| 19 | * "Software"), to deal in the Software without restriction, including
|
|---|
| 20 | * without limitation the rights to use, copy, modify, merge, publish,
|
|---|
| 21 | * distribute, sublicense, and/or sell copies of the Software, and to
|
|---|
| 22 | * permit persons to whom the Software is furnished to do so, subject to
|
|---|
| 23 | * the following conditions:
|
|---|
| 24 | *
|
|---|
| 25 | * The above copyright notice and this permission notice shall be
|
|---|
| 26 | * included in all copies or substantial portions of the Software.
|
|---|
| 27 | *
|
|---|
| 28 | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
|---|
| 29 | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
|---|
| 30 | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 31 | *
|
|---|
| 32 | * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
|
|---|
| 33 | * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
|
|---|
| 34 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
|
|---|
| 35 | * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
|
|---|
| 36 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|---|
| 37 | *
|
|---|
| 38 | * In addition, the following condition applies:
|
|---|
| 39 | *
|
|---|
| 40 | * All redistributions must retain an intact copy of this copyright notice
|
|---|
| 41 | * and disclaimer.
|
|---|
| 42 | */
|
|---|
| 43 | /*
|
|---|
| 44 | Address all questions regarding this license to:
|
|---|
| 45 |
|
|---|
| 46 | Tom Wu
|
|---|
| 47 | tjw@cs.Stanford.EDU
|
|---|
| 48 | */
|
|---|
| 49 | var forge = require('./forge');
|
|---|
| 50 |
|
|---|
| 51 | module.exports = forge.jsbn = forge.jsbn || {};
|
|---|
| 52 |
|
|---|
| 53 | // Bits per digit
|
|---|
| 54 | var dbits;
|
|---|
| 55 |
|
|---|
| 56 | // JavaScript engine analysis
|
|---|
| 57 | var canary = 0xdeadbeefcafe;
|
|---|
| 58 | var j_lm = ((canary&0xffffff)==0xefcafe);
|
|---|
| 59 |
|
|---|
| 60 | // (public) Constructor
|
|---|
| 61 | function BigInteger(a,b,c) {
|
|---|
| 62 | this.data = [];
|
|---|
| 63 | if(a != null)
|
|---|
| 64 | if("number" == typeof a) this.fromNumber(a,b,c);
|
|---|
| 65 | else if(b == null && "string" != typeof a) this.fromString(a,256);
|
|---|
| 66 | else this.fromString(a,b);
|
|---|
| 67 | }
|
|---|
| 68 | forge.jsbn.BigInteger = BigInteger;
|
|---|
| 69 |
|
|---|
| 70 | // return new, unset BigInteger
|
|---|
| 71 | function nbi() { return new BigInteger(null); }
|
|---|
| 72 |
|
|---|
| 73 | // am: Compute w_j += (x*this_i), propagate carries,
|
|---|
| 74 | // c is initial carry, returns final carry.
|
|---|
| 75 | // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
|
|---|
| 76 | // We need to select the fastest one that works in this environment.
|
|---|
| 77 |
|
|---|
| 78 | // am1: use a single mult and divide to get the high bits,
|
|---|
| 79 | // max digit bits should be 26 because
|
|---|
| 80 | // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
|
|---|
| 81 | function am1(i,x,w,j,c,n) {
|
|---|
| 82 | while(--n >= 0) {
|
|---|
| 83 | var v = x*this.data[i++]+w.data[j]+c;
|
|---|
| 84 | c = Math.floor(v/0x4000000);
|
|---|
| 85 | w.data[j++] = v&0x3ffffff;
|
|---|
| 86 | }
|
|---|
| 87 | return c;
|
|---|
| 88 | }
|
|---|
| 89 | // am2 avoids a big mult-and-extract completely.
|
|---|
| 90 | // Max digit bits should be <= 30 because we do bitwise ops
|
|---|
| 91 | // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
|
|---|
| 92 | function am2(i,x,w,j,c,n) {
|
|---|
| 93 | var xl = x&0x7fff, xh = x>>15;
|
|---|
| 94 | while(--n >= 0) {
|
|---|
| 95 | var l = this.data[i]&0x7fff;
|
|---|
| 96 | var h = this.data[i++]>>15;
|
|---|
| 97 | var m = xh*l+h*xl;
|
|---|
| 98 | l = xl*l+((m&0x7fff)<<15)+w.data[j]+(c&0x3fffffff);
|
|---|
| 99 | c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
|
|---|
| 100 | w.data[j++] = l&0x3fffffff;
|
|---|
| 101 | }
|
|---|
| 102 | return c;
|
|---|
| 103 | }
|
|---|
| 104 | // Alternately, set max digit bits to 28 since some
|
|---|
| 105 | // browsers slow down when dealing with 32-bit numbers.
|
|---|
| 106 | function am3(i,x,w,j,c,n) {
|
|---|
| 107 | var xl = x&0x3fff, xh = x>>14;
|
|---|
| 108 | while(--n >= 0) {
|
|---|
| 109 | var l = this.data[i]&0x3fff;
|
|---|
| 110 | var h = this.data[i++]>>14;
|
|---|
| 111 | var m = xh*l+h*xl;
|
|---|
| 112 | l = xl*l+((m&0x3fff)<<14)+w.data[j]+c;
|
|---|
| 113 | c = (l>>28)+(m>>14)+xh*h;
|
|---|
| 114 | w.data[j++] = l&0xfffffff;
|
|---|
| 115 | }
|
|---|
| 116 | return c;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | // node.js (no browser)
|
|---|
| 120 | if(typeof(navigator) === 'undefined')
|
|---|
| 121 | {
|
|---|
| 122 | BigInteger.prototype.am = am3;
|
|---|
| 123 | dbits = 28;
|
|---|
| 124 | } else if(j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
|
|---|
| 125 | BigInteger.prototype.am = am2;
|
|---|
| 126 | dbits = 30;
|
|---|
| 127 | }
|
|---|
| 128 | else if(j_lm && (navigator.appName != "Netscape")) {
|
|---|
| 129 | BigInteger.prototype.am = am1;
|
|---|
| 130 | dbits = 26;
|
|---|
| 131 | }
|
|---|
| 132 | else { // Mozilla/Netscape seems to prefer am3
|
|---|
| 133 | BigInteger.prototype.am = am3;
|
|---|
| 134 | dbits = 28;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | BigInteger.prototype.DB = dbits;
|
|---|
| 138 | BigInteger.prototype.DM = ((1<<dbits)-1);
|
|---|
| 139 | BigInteger.prototype.DV = (1<<dbits);
|
|---|
| 140 |
|
|---|
| 141 | var BI_FP = 52;
|
|---|
| 142 | BigInteger.prototype.FV = Math.pow(2,BI_FP);
|
|---|
| 143 | BigInteger.prototype.F1 = BI_FP-dbits;
|
|---|
| 144 | BigInteger.prototype.F2 = 2*dbits-BI_FP;
|
|---|
| 145 |
|
|---|
| 146 | // Digit conversions
|
|---|
| 147 | var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|---|
| 148 | var BI_RC = new Array();
|
|---|
| 149 | var rr,vv;
|
|---|
| 150 | rr = "0".charCodeAt(0);
|
|---|
| 151 | for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
|
|---|
| 152 | rr = "a".charCodeAt(0);
|
|---|
| 153 | for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
|
|---|
| 154 | rr = "A".charCodeAt(0);
|
|---|
| 155 | for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
|
|---|
| 156 |
|
|---|
| 157 | function int2char(n) { return BI_RM.charAt(n); }
|
|---|
| 158 | function intAt(s,i) {
|
|---|
| 159 | var c = BI_RC[s.charCodeAt(i)];
|
|---|
| 160 | return (c==null)?-1:c;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | // (protected) copy this to r
|
|---|
| 164 | function bnpCopyTo(r) {
|
|---|
| 165 | for(var i = this.t-1; i >= 0; --i) r.data[i] = this.data[i];
|
|---|
| 166 | r.t = this.t;
|
|---|
| 167 | r.s = this.s;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | // (protected) set from integer value x, -DV <= x < DV
|
|---|
| 171 | function bnpFromInt(x) {
|
|---|
| 172 | this.t = 1;
|
|---|
| 173 | this.s = (x<0)?-1:0;
|
|---|
| 174 | if(x > 0) this.data[0] = x;
|
|---|
| 175 | else if(x < -1) this.data[0] = x+this.DV;
|
|---|
| 176 | else this.t = 0;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | // return bigint initialized to value
|
|---|
| 180 | function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
|
|---|
| 181 |
|
|---|
| 182 | // (protected) set from string and radix
|
|---|
| 183 | function bnpFromString(s,b) {
|
|---|
| 184 | var k;
|
|---|
| 185 | if(b == 16) k = 4;
|
|---|
| 186 | else if(b == 8) k = 3;
|
|---|
| 187 | else if(b == 256) k = 8; // byte array
|
|---|
| 188 | else if(b == 2) k = 1;
|
|---|
| 189 | else if(b == 32) k = 5;
|
|---|
| 190 | else if(b == 4) k = 2;
|
|---|
| 191 | else { this.fromRadix(s,b); return; }
|
|---|
| 192 | this.t = 0;
|
|---|
| 193 | this.s = 0;
|
|---|
| 194 | var i = s.length, mi = false, sh = 0;
|
|---|
| 195 | while(--i >= 0) {
|
|---|
| 196 | var x = (k==8)?s[i]&0xff:intAt(s,i);
|
|---|
| 197 | if(x < 0) {
|
|---|
| 198 | if(s.charAt(i) == "-") mi = true;
|
|---|
| 199 | continue;
|
|---|
| 200 | }
|
|---|
| 201 | mi = false;
|
|---|
| 202 | if(sh == 0)
|
|---|
| 203 | this.data[this.t++] = x;
|
|---|
| 204 | else if(sh+k > this.DB) {
|
|---|
| 205 | this.data[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh;
|
|---|
| 206 | this.data[this.t++] = (x>>(this.DB-sh));
|
|---|
| 207 | }
|
|---|
| 208 | else
|
|---|
| 209 | this.data[this.t-1] |= x<<sh;
|
|---|
| 210 | sh += k;
|
|---|
| 211 | if(sh >= this.DB) sh -= this.DB;
|
|---|
| 212 | }
|
|---|
| 213 | if(k == 8 && (s[0]&0x80) != 0) {
|
|---|
| 214 | this.s = -1;
|
|---|
| 215 | if(sh > 0) this.data[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh;
|
|---|
| 216 | }
|
|---|
| 217 | this.clamp();
|
|---|
| 218 | if(mi) BigInteger.ZERO.subTo(this,this);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | // (protected) clamp off excess high words
|
|---|
| 222 | function bnpClamp() {
|
|---|
| 223 | var c = this.s&this.DM;
|
|---|
| 224 | while(this.t > 0 && this.data[this.t-1] == c) --this.t;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | // (public) return string representation in given radix
|
|---|
| 228 | function bnToString(b) {
|
|---|
| 229 | if(this.s < 0) return "-"+this.negate().toString(b);
|
|---|
| 230 | var k;
|
|---|
| 231 | if(b == 16) k = 4;
|
|---|
| 232 | else if(b == 8) k = 3;
|
|---|
| 233 | else if(b == 2) k = 1;
|
|---|
| 234 | else if(b == 32) k = 5;
|
|---|
| 235 | else if(b == 4) k = 2;
|
|---|
| 236 | else return this.toRadix(b);
|
|---|
| 237 | var km = (1<<k)-1, d, m = false, r = "", i = this.t;
|
|---|
| 238 | var p = this.DB-(i*this.DB)%k;
|
|---|
| 239 | if(i-- > 0) {
|
|---|
| 240 | if(p < this.DB && (d = this.data[i]>>p) > 0) { m = true; r = int2char(d); }
|
|---|
| 241 | while(i >= 0) {
|
|---|
| 242 | if(p < k) {
|
|---|
| 243 | d = (this.data[i]&((1<<p)-1))<<(k-p);
|
|---|
| 244 | d |= this.data[--i]>>(p+=this.DB-k);
|
|---|
| 245 | }
|
|---|
| 246 | else {
|
|---|
| 247 | d = (this.data[i]>>(p-=k))&km;
|
|---|
| 248 | if(p <= 0) { p += this.DB; --i; }
|
|---|
| 249 | }
|
|---|
| 250 | if(d > 0) m = true;
|
|---|
| 251 | if(m) r += int2char(d);
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 | return m?r:"0";
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | // (public) -this
|
|---|
| 258 | function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
|
|---|
| 259 |
|
|---|
| 260 | // (public) |this|
|
|---|
| 261 | function bnAbs() { return (this.s<0)?this.negate():this; }
|
|---|
| 262 |
|
|---|
| 263 | // (public) return + if this > a, - if this < a, 0 if equal
|
|---|
| 264 | function bnCompareTo(a) {
|
|---|
| 265 | var r = this.s-a.s;
|
|---|
| 266 | if(r != 0) return r;
|
|---|
| 267 | var i = this.t;
|
|---|
| 268 | r = i-a.t;
|
|---|
| 269 | if(r != 0) return (this.s<0)?-r:r;
|
|---|
| 270 | while(--i >= 0) if((r=this.data[i]-a.data[i]) != 0) return r;
|
|---|
| 271 | return 0;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | // returns bit length of the integer x
|
|---|
| 275 | function nbits(x) {
|
|---|
| 276 | var r = 1, t;
|
|---|
| 277 | if((t=x>>>16) != 0) { x = t; r += 16; }
|
|---|
| 278 | if((t=x>>8) != 0) { x = t; r += 8; }
|
|---|
| 279 | if((t=x>>4) != 0) { x = t; r += 4; }
|
|---|
| 280 | if((t=x>>2) != 0) { x = t; r += 2; }
|
|---|
| 281 | if((t=x>>1) != 0) { x = t; r += 1; }
|
|---|
| 282 | return r;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | // (public) return the number of bits in "this"
|
|---|
| 286 | function bnBitLength() {
|
|---|
| 287 | if(this.t <= 0) return 0;
|
|---|
| 288 | return this.DB*(this.t-1)+nbits(this.data[this.t-1]^(this.s&this.DM));
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | // (protected) r = this << n*DB
|
|---|
| 292 | function bnpDLShiftTo(n,r) {
|
|---|
| 293 | var i;
|
|---|
| 294 | for(i = this.t-1; i >= 0; --i) r.data[i+n] = this.data[i];
|
|---|
| 295 | for(i = n-1; i >= 0; --i) r.data[i] = 0;
|
|---|
| 296 | r.t = this.t+n;
|
|---|
| 297 | r.s = this.s;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | // (protected) r = this >> n*DB
|
|---|
| 301 | function bnpDRShiftTo(n,r) {
|
|---|
| 302 | for(var i = n; i < this.t; ++i) r.data[i-n] = this.data[i];
|
|---|
| 303 | r.t = Math.max(this.t-n,0);
|
|---|
| 304 | r.s = this.s;
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | // (protected) r = this << n
|
|---|
| 308 | function bnpLShiftTo(n,r) {
|
|---|
| 309 | var bs = n%this.DB;
|
|---|
| 310 | var cbs = this.DB-bs;
|
|---|
| 311 | var bm = (1<<cbs)-1;
|
|---|
| 312 | var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i;
|
|---|
| 313 | for(i = this.t-1; i >= 0; --i) {
|
|---|
| 314 | r.data[i+ds+1] = (this.data[i]>>cbs)|c;
|
|---|
| 315 | c = (this.data[i]&bm)<<bs;
|
|---|
| 316 | }
|
|---|
| 317 | for(i = ds-1; i >= 0; --i) r.data[i] = 0;
|
|---|
| 318 | r.data[ds] = c;
|
|---|
| 319 | r.t = this.t+ds+1;
|
|---|
| 320 | r.s = this.s;
|
|---|
| 321 | r.clamp();
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | // (protected) r = this >> n
|
|---|
| 325 | function bnpRShiftTo(n,r) {
|
|---|
| 326 | r.s = this.s;
|
|---|
| 327 | var ds = Math.floor(n/this.DB);
|
|---|
| 328 | if(ds >= this.t) { r.t = 0; return; }
|
|---|
| 329 | var bs = n%this.DB;
|
|---|
| 330 | var cbs = this.DB-bs;
|
|---|
| 331 | var bm = (1<<bs)-1;
|
|---|
| 332 | r.data[0] = this.data[ds]>>bs;
|
|---|
| 333 | for(var i = ds+1; i < this.t; ++i) {
|
|---|
| 334 | r.data[i-ds-1] |= (this.data[i]&bm)<<cbs;
|
|---|
| 335 | r.data[i-ds] = this.data[i]>>bs;
|
|---|
| 336 | }
|
|---|
| 337 | if(bs > 0) r.data[this.t-ds-1] |= (this.s&bm)<<cbs;
|
|---|
| 338 | r.t = this.t-ds;
|
|---|
| 339 | r.clamp();
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | // (protected) r = this - a
|
|---|
| 343 | function bnpSubTo(a,r) {
|
|---|
| 344 | var i = 0, c = 0, m = Math.min(a.t,this.t);
|
|---|
| 345 | while(i < m) {
|
|---|
| 346 | c += this.data[i]-a.data[i];
|
|---|
| 347 | r.data[i++] = c&this.DM;
|
|---|
| 348 | c >>= this.DB;
|
|---|
| 349 | }
|
|---|
| 350 | if(a.t < this.t) {
|
|---|
| 351 | c -= a.s;
|
|---|
| 352 | while(i < this.t) {
|
|---|
| 353 | c += this.data[i];
|
|---|
| 354 | r.data[i++] = c&this.DM;
|
|---|
| 355 | c >>= this.DB;
|
|---|
| 356 | }
|
|---|
| 357 | c += this.s;
|
|---|
| 358 | }
|
|---|
| 359 | else {
|
|---|
| 360 | c += this.s;
|
|---|
| 361 | while(i < a.t) {
|
|---|
| 362 | c -= a.data[i];
|
|---|
| 363 | r.data[i++] = c&this.DM;
|
|---|
| 364 | c >>= this.DB;
|
|---|
| 365 | }
|
|---|
| 366 | c -= a.s;
|
|---|
| 367 | }
|
|---|
| 368 | r.s = (c<0)?-1:0;
|
|---|
| 369 | if(c < -1) r.data[i++] = this.DV+c;
|
|---|
| 370 | else if(c > 0) r.data[i++] = c;
|
|---|
| 371 | r.t = i;
|
|---|
| 372 | r.clamp();
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | // (protected) r = this * a, r != this,a (HAC 14.12)
|
|---|
| 376 | // "this" should be the larger one if appropriate.
|
|---|
| 377 | function bnpMultiplyTo(a,r) {
|
|---|
| 378 | var x = this.abs(), y = a.abs();
|
|---|
| 379 | var i = x.t;
|
|---|
| 380 | r.t = i+y.t;
|
|---|
| 381 | while(--i >= 0) r.data[i] = 0;
|
|---|
| 382 | for(i = 0; i < y.t; ++i) r.data[i+x.t] = x.am(0,y.data[i],r,i,0,x.t);
|
|---|
| 383 | r.s = 0;
|
|---|
| 384 | r.clamp();
|
|---|
| 385 | if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | // (protected) r = this^2, r != this (HAC 14.16)
|
|---|
| 389 | function bnpSquareTo(r) {
|
|---|
| 390 | var x = this.abs();
|
|---|
| 391 | var i = r.t = 2*x.t;
|
|---|
| 392 | while(--i >= 0) r.data[i] = 0;
|
|---|
| 393 | for(i = 0; i < x.t-1; ++i) {
|
|---|
| 394 | var c = x.am(i,x.data[i],r,2*i,0,1);
|
|---|
| 395 | if((r.data[i+x.t]+=x.am(i+1,2*x.data[i],r,2*i+1,c,x.t-i-1)) >= x.DV) {
|
|---|
| 396 | r.data[i+x.t] -= x.DV;
|
|---|
| 397 | r.data[i+x.t+1] = 1;
|
|---|
| 398 | }
|
|---|
| 399 | }
|
|---|
| 400 | if(r.t > 0) r.data[r.t-1] += x.am(i,x.data[i],r,2*i,0,1);
|
|---|
| 401 | r.s = 0;
|
|---|
| 402 | r.clamp();
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
|
|---|
| 406 | // r != q, this != m. q or r may be null.
|
|---|
| 407 | function bnpDivRemTo(m,q,r) {
|
|---|
| 408 | var pm = m.abs();
|
|---|
| 409 | if(pm.t <= 0) return;
|
|---|
| 410 | var pt = this.abs();
|
|---|
| 411 | if(pt.t < pm.t) {
|
|---|
| 412 | if(q != null) q.fromInt(0);
|
|---|
| 413 | if(r != null) this.copyTo(r);
|
|---|
| 414 | return;
|
|---|
| 415 | }
|
|---|
| 416 | if(r == null) r = nbi();
|
|---|
| 417 | var y = nbi(), ts = this.s, ms = m.s;
|
|---|
| 418 | var nsh = this.DB-nbits(pm.data[pm.t-1]); // normalize modulus
|
|---|
| 419 | if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
|
|---|
| 420 | else { pm.copyTo(y); pt.copyTo(r); }
|
|---|
| 421 | var ys = y.t;
|
|---|
| 422 | var y0 = y.data[ys-1];
|
|---|
| 423 | if(y0 == 0) return;
|
|---|
| 424 | var yt = y0*(1<<this.F1)+((ys>1)?y.data[ys-2]>>this.F2:0);
|
|---|
| 425 | var d1 = this.FV/yt, d2 = (1<<this.F1)/yt, e = 1<<this.F2;
|
|---|
| 426 | var i = r.t, j = i-ys, t = (q==null)?nbi():q;
|
|---|
| 427 | y.dlShiftTo(j,t);
|
|---|
| 428 | if(r.compareTo(t) >= 0) {
|
|---|
| 429 | r.data[r.t++] = 1;
|
|---|
| 430 | r.subTo(t,r);
|
|---|
| 431 | }
|
|---|
| 432 | BigInteger.ONE.dlShiftTo(ys,t);
|
|---|
| 433 | t.subTo(y,y); // "negative" y so we can replace sub with am later
|
|---|
| 434 | while(y.t < ys) y.data[y.t++] = 0;
|
|---|
| 435 | while(--j >= 0) {
|
|---|
| 436 | // Estimate quotient digit
|
|---|
| 437 | var qd = (r.data[--i]==y0)?this.DM:Math.floor(r.data[i]*d1+(r.data[i-1]+e)*d2);
|
|---|
| 438 | if((r.data[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out
|
|---|
| 439 | y.dlShiftTo(j,t);
|
|---|
| 440 | r.subTo(t,r);
|
|---|
| 441 | while(r.data[i] < --qd) r.subTo(t,r);
|
|---|
| 442 | }
|
|---|
| 443 | }
|
|---|
| 444 | if(q != null) {
|
|---|
| 445 | r.drShiftTo(ys,q);
|
|---|
| 446 | if(ts != ms) BigInteger.ZERO.subTo(q,q);
|
|---|
| 447 | }
|
|---|
| 448 | r.t = ys;
|
|---|
| 449 | r.clamp();
|
|---|
| 450 | if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder
|
|---|
| 451 | if(ts < 0) BigInteger.ZERO.subTo(r,r);
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | // (public) this mod a
|
|---|
| 455 | function bnMod(a) {
|
|---|
| 456 | var r = nbi();
|
|---|
| 457 | this.abs().divRemTo(a,null,r);
|
|---|
| 458 | if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r);
|
|---|
| 459 | return r;
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | // Modular reduction using "classic" algorithm
|
|---|
| 463 | function Classic(m) { this.m = m; }
|
|---|
| 464 | function cConvert(x) {
|
|---|
| 465 | if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
|
|---|
| 466 | else return x;
|
|---|
| 467 | }
|
|---|
| 468 | function cRevert(x) { return x; }
|
|---|
| 469 | function cReduce(x) { x.divRemTo(this.m,null,x); }
|
|---|
| 470 | function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
|
|---|
| 471 | function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
|
|---|
| 472 |
|
|---|
| 473 | Classic.prototype.convert = cConvert;
|
|---|
| 474 | Classic.prototype.revert = cRevert;
|
|---|
| 475 | Classic.prototype.reduce = cReduce;
|
|---|
| 476 | Classic.prototype.mulTo = cMulTo;
|
|---|
| 477 | Classic.prototype.sqrTo = cSqrTo;
|
|---|
| 478 |
|
|---|
| 479 | // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
|
|---|
| 480 | // justification:
|
|---|
| 481 | // xy == 1 (mod m)
|
|---|
| 482 | // xy = 1+km
|
|---|
| 483 | // xy(2-xy) = (1+km)(1-km)
|
|---|
| 484 | // x[y(2-xy)] = 1-k^2m^2
|
|---|
| 485 | // x[y(2-xy)] == 1 (mod m^2)
|
|---|
| 486 | // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
|
|---|
| 487 | // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
|
|---|
| 488 | // JS multiply "overflows" differently from C/C++, so care is needed here.
|
|---|
| 489 | function bnpInvDigit() {
|
|---|
| 490 | if(this.t < 1) return 0;
|
|---|
| 491 | var x = this.data[0];
|
|---|
| 492 | if((x&1) == 0) return 0;
|
|---|
| 493 | var y = x&3; // y == 1/x mod 2^2
|
|---|
| 494 | y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4
|
|---|
| 495 | y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8
|
|---|
| 496 | y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16
|
|---|
| 497 | // last step - calculate inverse mod DV directly;
|
|---|
| 498 | // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
|
|---|
| 499 | y = (y*(2-x*y%this.DV))%this.DV; // y == 1/x mod 2^dbits
|
|---|
| 500 | // we really want the negative inverse, and -DV < y < DV
|
|---|
| 501 | return (y>0)?this.DV-y:-y;
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | // Montgomery reduction
|
|---|
| 505 | function Montgomery(m) {
|
|---|
| 506 | this.m = m;
|
|---|
| 507 | this.mp = m.invDigit();
|
|---|
| 508 | this.mpl = this.mp&0x7fff;
|
|---|
| 509 | this.mph = this.mp>>15;
|
|---|
| 510 | this.um = (1<<(m.DB-15))-1;
|
|---|
| 511 | this.mt2 = 2*m.t;
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 | // xR mod m
|
|---|
| 515 | function montConvert(x) {
|
|---|
| 516 | var r = nbi();
|
|---|
| 517 | x.abs().dlShiftTo(this.m.t,r);
|
|---|
| 518 | r.divRemTo(this.m,null,r);
|
|---|
| 519 | if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r);
|
|---|
| 520 | return r;
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 | // x/R mod m
|
|---|
| 524 | function montRevert(x) {
|
|---|
| 525 | var r = nbi();
|
|---|
| 526 | x.copyTo(r);
|
|---|
| 527 | this.reduce(r);
|
|---|
| 528 | return r;
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | // x = x/R mod m (HAC 14.32)
|
|---|
| 532 | function montReduce(x) {
|
|---|
| 533 | while(x.t <= this.mt2) // pad x so am has enough room later
|
|---|
| 534 | x.data[x.t++] = 0;
|
|---|
| 535 | for(var i = 0; i < this.m.t; ++i) {
|
|---|
| 536 | // faster way of calculating u0 = x.data[i]*mp mod DV
|
|---|
| 537 | var j = x.data[i]&0x7fff;
|
|---|
| 538 | var u0 = (j*this.mpl+(((j*this.mph+(x.data[i]>>15)*this.mpl)&this.um)<<15))&x.DM;
|
|---|
| 539 | // use am to combine the multiply-shift-add into one call
|
|---|
| 540 | j = i+this.m.t;
|
|---|
| 541 | x.data[j] += this.m.am(0,u0,x,i,0,this.m.t);
|
|---|
| 542 | // propagate carry
|
|---|
| 543 | while(x.data[j] >= x.DV) { x.data[j] -= x.DV; x.data[++j]++; }
|
|---|
| 544 | }
|
|---|
| 545 | x.clamp();
|
|---|
| 546 | x.drShiftTo(this.m.t,x);
|
|---|
| 547 | if(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | // r = "x^2/R mod m"; x != r
|
|---|
| 551 | function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
|
|---|
| 552 |
|
|---|
| 553 | // r = "xy/R mod m"; x,y != r
|
|---|
| 554 | function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
|
|---|
| 555 |
|
|---|
| 556 | Montgomery.prototype.convert = montConvert;
|
|---|
| 557 | Montgomery.prototype.revert = montRevert;
|
|---|
| 558 | Montgomery.prototype.reduce = montReduce;
|
|---|
| 559 | Montgomery.prototype.mulTo = montMulTo;
|
|---|
| 560 | Montgomery.prototype.sqrTo = montSqrTo;
|
|---|
| 561 |
|
|---|
| 562 | // (protected) true iff this is even
|
|---|
| 563 | function bnpIsEven() { return ((this.t>0)?(this.data[0]&1):this.s) == 0; }
|
|---|
| 564 |
|
|---|
| 565 | // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
|
|---|
| 566 | function bnpExp(e,z) {
|
|---|
| 567 | if(e > 0xffffffff || e < 1) return BigInteger.ONE;
|
|---|
| 568 | var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
|
|---|
| 569 | g.copyTo(r);
|
|---|
| 570 | while(--i >= 0) {
|
|---|
| 571 | z.sqrTo(r,r2);
|
|---|
| 572 | if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
|
|---|
| 573 | else { var t = r; r = r2; r2 = t; }
|
|---|
| 574 | }
|
|---|
| 575 | return z.revert(r);
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | // (public) this^e % m, 0 <= e < 2^32
|
|---|
| 579 | function bnModPowInt(e,m) {
|
|---|
| 580 | var z;
|
|---|
| 581 | if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
|
|---|
| 582 | return this.exp(e,z);
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | // protected
|
|---|
| 586 | BigInteger.prototype.copyTo = bnpCopyTo;
|
|---|
| 587 | BigInteger.prototype.fromInt = bnpFromInt;
|
|---|
| 588 | BigInteger.prototype.fromString = bnpFromString;
|
|---|
| 589 | BigInteger.prototype.clamp = bnpClamp;
|
|---|
| 590 | BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
|
|---|
| 591 | BigInteger.prototype.drShiftTo = bnpDRShiftTo;
|
|---|
| 592 | BigInteger.prototype.lShiftTo = bnpLShiftTo;
|
|---|
| 593 | BigInteger.prototype.rShiftTo = bnpRShiftTo;
|
|---|
| 594 | BigInteger.prototype.subTo = bnpSubTo;
|
|---|
| 595 | BigInteger.prototype.multiplyTo = bnpMultiplyTo;
|
|---|
| 596 | BigInteger.prototype.squareTo = bnpSquareTo;
|
|---|
| 597 | BigInteger.prototype.divRemTo = bnpDivRemTo;
|
|---|
| 598 | BigInteger.prototype.invDigit = bnpInvDigit;
|
|---|
| 599 | BigInteger.prototype.isEven = bnpIsEven;
|
|---|
| 600 | BigInteger.prototype.exp = bnpExp;
|
|---|
| 601 |
|
|---|
| 602 | // public
|
|---|
| 603 | BigInteger.prototype.toString = bnToString;
|
|---|
| 604 | BigInteger.prototype.negate = bnNegate;
|
|---|
| 605 | BigInteger.prototype.abs = bnAbs;
|
|---|
| 606 | BigInteger.prototype.compareTo = bnCompareTo;
|
|---|
| 607 | BigInteger.prototype.bitLength = bnBitLength;
|
|---|
| 608 | BigInteger.prototype.mod = bnMod;
|
|---|
| 609 | BigInteger.prototype.modPowInt = bnModPowInt;
|
|---|
| 610 |
|
|---|
| 611 | // "constants"
|
|---|
| 612 | BigInteger.ZERO = nbv(0);
|
|---|
| 613 | BigInteger.ONE = nbv(1);
|
|---|
| 614 |
|
|---|
| 615 | // jsbn2 lib
|
|---|
| 616 |
|
|---|
| 617 | // Copyright (c) 2005-2009 Tom Wu
|
|---|
| 618 | // All Rights Reserved.
|
|---|
| 619 | // See "LICENSE" for details (See jsbn.js for LICENSE).
|
|---|
| 620 |
|
|---|
| 621 | // Extended JavaScript BN functions, required for RSA private ops.
|
|---|
| 622 |
|
|---|
| 623 | // Version 1.1: new BigInteger("0", 10) returns "proper" zero
|
|---|
| 624 | // Version 1.2: square() API, isProbablePrime fix
|
|---|
| 625 |
|
|---|
| 626 | // (public)
|
|---|
| 627 | function bnClone() { var r = nbi(); this.copyTo(r); return r; }
|
|---|
| 628 |
|
|---|
| 629 | // (public) return value as integer
|
|---|
| 630 | function bnIntValue() {
|
|---|
| 631 | if(this.s < 0) {
|
|---|
| 632 | if(this.t == 1) return this.data[0]-this.DV;
|
|---|
| 633 | else if(this.t == 0) return -1;
|
|---|
| 634 | }
|
|---|
| 635 | else if(this.t == 1) return this.data[0];
|
|---|
| 636 | else if(this.t == 0) return 0;
|
|---|
| 637 | // assumes 16 < DB < 32
|
|---|
| 638 | return ((this.data[1]&((1<<(32-this.DB))-1))<<this.DB)|this.data[0];
|
|---|
| 639 | }
|
|---|
| 640 |
|
|---|
| 641 | // (public) return value as byte
|
|---|
| 642 | function bnByteValue() { return (this.t==0)?this.s:(this.data[0]<<24)>>24; }
|
|---|
| 643 |
|
|---|
| 644 | // (public) return value as short (assumes DB>=16)
|
|---|
| 645 | function bnShortValue() { return (this.t==0)?this.s:(this.data[0]<<16)>>16; }
|
|---|
| 646 |
|
|---|
| 647 | // (protected) return x s.t. r^x < DV
|
|---|
| 648 | function bnpChunkSize(r) { return Math.floor(Math.LN2*this.DB/Math.log(r)); }
|
|---|
| 649 |
|
|---|
| 650 | // (public) 0 if this == 0, 1 if this > 0
|
|---|
| 651 | function bnSigNum() {
|
|---|
| 652 | if(this.s < 0) return -1;
|
|---|
| 653 | else if(this.t <= 0 || (this.t == 1 && this.data[0] <= 0)) return 0;
|
|---|
| 654 | else return 1;
|
|---|
| 655 | }
|
|---|
| 656 |
|
|---|
| 657 | // (protected) convert to radix string
|
|---|
| 658 | function bnpToRadix(b) {
|
|---|
| 659 | if(b == null) b = 10;
|
|---|
| 660 | if(this.signum() == 0 || b < 2 || b > 36) return "0";
|
|---|
| 661 | var cs = this.chunkSize(b);
|
|---|
| 662 | var a = Math.pow(b,cs);
|
|---|
| 663 | var d = nbv(a), y = nbi(), z = nbi(), r = "";
|
|---|
| 664 | this.divRemTo(d,y,z);
|
|---|
| 665 | while(y.signum() > 0) {
|
|---|
| 666 | r = (a+z.intValue()).toString(b).substr(1) + r;
|
|---|
| 667 | y.divRemTo(d,y,z);
|
|---|
| 668 | }
|
|---|
| 669 | return z.intValue().toString(b) + r;
|
|---|
| 670 | }
|
|---|
| 671 |
|
|---|
| 672 | // (protected) convert from radix string
|
|---|
| 673 | function bnpFromRadix(s,b) {
|
|---|
| 674 | this.fromInt(0);
|
|---|
| 675 | if(b == null) b = 10;
|
|---|
| 676 | var cs = this.chunkSize(b);
|
|---|
| 677 | var d = Math.pow(b,cs), mi = false, j = 0, w = 0;
|
|---|
| 678 | for(var i = 0; i < s.length; ++i) {
|
|---|
| 679 | var x = intAt(s,i);
|
|---|
| 680 | if(x < 0) {
|
|---|
| 681 | if(s.charAt(i) == "-" && this.signum() == 0) mi = true;
|
|---|
| 682 | continue;
|
|---|
| 683 | }
|
|---|
| 684 | w = b*w+x;
|
|---|
| 685 | if(++j >= cs) {
|
|---|
| 686 | this.dMultiply(d);
|
|---|
| 687 | this.dAddOffset(w,0);
|
|---|
| 688 | j = 0;
|
|---|
| 689 | w = 0;
|
|---|
| 690 | }
|
|---|
| 691 | }
|
|---|
| 692 | if(j > 0) {
|
|---|
| 693 | this.dMultiply(Math.pow(b,j));
|
|---|
| 694 | this.dAddOffset(w,0);
|
|---|
| 695 | }
|
|---|
| 696 | if(mi) BigInteger.ZERO.subTo(this,this);
|
|---|
| 697 | }
|
|---|
| 698 |
|
|---|
| 699 | // (protected) alternate constructor
|
|---|
| 700 | function bnpFromNumber(a,b,c) {
|
|---|
| 701 | if("number" == typeof b) {
|
|---|
| 702 | // new BigInteger(int,int,RNG)
|
|---|
| 703 | if(a < 2) this.fromInt(1);
|
|---|
| 704 | else {
|
|---|
| 705 | this.fromNumber(a,c);
|
|---|
| 706 | if(!this.testBit(a-1)) // force MSB set
|
|---|
| 707 | this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this);
|
|---|
| 708 | if(this.isEven()) this.dAddOffset(1,0); // force odd
|
|---|
| 709 | while(!this.isProbablePrime(b)) {
|
|---|
| 710 | this.dAddOffset(2,0);
|
|---|
| 711 | if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this);
|
|---|
| 712 | }
|
|---|
| 713 | }
|
|---|
| 714 | }
|
|---|
| 715 | else {
|
|---|
| 716 | // new BigInteger(int,RNG)
|
|---|
| 717 | var x = new Array(), t = a&7;
|
|---|
| 718 | x.length = (a>>3)+1;
|
|---|
| 719 | b.nextBytes(x);
|
|---|
| 720 | if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0;
|
|---|
| 721 | this.fromString(x,256);
|
|---|
| 722 | }
|
|---|
| 723 | }
|
|---|
| 724 |
|
|---|
| 725 | // (public) convert to bigendian byte array
|
|---|
| 726 | function bnToByteArray() {
|
|---|
| 727 | var i = this.t, r = new Array();
|
|---|
| 728 | r[0] = this.s;
|
|---|
| 729 | var p = this.DB-(i*this.DB)%8, d, k = 0;
|
|---|
| 730 | if(i-- > 0) {
|
|---|
| 731 | if(p < this.DB && (d = this.data[i]>>p) != (this.s&this.DM)>>p)
|
|---|
| 732 | r[k++] = d|(this.s<<(this.DB-p));
|
|---|
| 733 | while(i >= 0) {
|
|---|
| 734 | if(p < 8) {
|
|---|
| 735 | d = (this.data[i]&((1<<p)-1))<<(8-p);
|
|---|
| 736 | d |= this.data[--i]>>(p+=this.DB-8);
|
|---|
| 737 | }
|
|---|
| 738 | else {
|
|---|
| 739 | d = (this.data[i]>>(p-=8))&0xff;
|
|---|
| 740 | if(p <= 0) { p += this.DB; --i; }
|
|---|
| 741 | }
|
|---|
| 742 | if((d&0x80) != 0) d |= -256;
|
|---|
| 743 | if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
|
|---|
| 744 | if(k > 0 || d != this.s) r[k++] = d;
|
|---|
| 745 | }
|
|---|
| 746 | }
|
|---|
| 747 | return r;
|
|---|
| 748 | }
|
|---|
| 749 |
|
|---|
| 750 | function bnEquals(a) { return(this.compareTo(a)==0); }
|
|---|
| 751 | function bnMin(a) { return(this.compareTo(a)<0)?this:a; }
|
|---|
| 752 | function bnMax(a) { return(this.compareTo(a)>0)?this:a; }
|
|---|
| 753 |
|
|---|
| 754 | // (protected) r = this op a (bitwise)
|
|---|
| 755 | function bnpBitwiseTo(a,op,r) {
|
|---|
| 756 | var i, f, m = Math.min(a.t,this.t);
|
|---|
| 757 | for(i = 0; i < m; ++i) r.data[i] = op(this.data[i],a.data[i]);
|
|---|
| 758 | if(a.t < this.t) {
|
|---|
| 759 | f = a.s&this.DM;
|
|---|
| 760 | for(i = m; i < this.t; ++i) r.data[i] = op(this.data[i],f);
|
|---|
| 761 | r.t = this.t;
|
|---|
| 762 | }
|
|---|
| 763 | else {
|
|---|
| 764 | f = this.s&this.DM;
|
|---|
| 765 | for(i = m; i < a.t; ++i) r.data[i] = op(f,a.data[i]);
|
|---|
| 766 | r.t = a.t;
|
|---|
| 767 | }
|
|---|
| 768 | r.s = op(this.s,a.s);
|
|---|
| 769 | r.clamp();
|
|---|
| 770 | }
|
|---|
| 771 |
|
|---|
| 772 | // (public) this & a
|
|---|
| 773 | function op_and(x,y) { return x&y; }
|
|---|
| 774 | function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; }
|
|---|
| 775 |
|
|---|
| 776 | // (public) this | a
|
|---|
| 777 | function op_or(x,y) { return x|y; }
|
|---|
| 778 | function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; }
|
|---|
| 779 |
|
|---|
| 780 | // (public) this ^ a
|
|---|
| 781 | function op_xor(x,y) { return x^y; }
|
|---|
| 782 | function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; }
|
|---|
| 783 |
|
|---|
| 784 | // (public) this & ~a
|
|---|
| 785 | function op_andnot(x,y) { return x&~y; }
|
|---|
| 786 | function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; }
|
|---|
| 787 |
|
|---|
| 788 | // (public) ~this
|
|---|
| 789 | function bnNot() {
|
|---|
| 790 | var r = nbi();
|
|---|
| 791 | for(var i = 0; i < this.t; ++i) r.data[i] = this.DM&~this.data[i];
|
|---|
| 792 | r.t = this.t;
|
|---|
| 793 | r.s = ~this.s;
|
|---|
| 794 | return r;
|
|---|
| 795 | }
|
|---|
| 796 |
|
|---|
| 797 | // (public) this << n
|
|---|
| 798 | function bnShiftLeft(n) {
|
|---|
| 799 | var r = nbi();
|
|---|
| 800 | if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r);
|
|---|
| 801 | return r;
|
|---|
| 802 | }
|
|---|
| 803 |
|
|---|
| 804 | // (public) this >> n
|
|---|
| 805 | function bnShiftRight(n) {
|
|---|
| 806 | var r = nbi();
|
|---|
| 807 | if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r);
|
|---|
| 808 | return r;
|
|---|
| 809 | }
|
|---|
| 810 |
|
|---|
| 811 | // return index of lowest 1-bit in x, x < 2^31
|
|---|
| 812 | function lbit(x) {
|
|---|
| 813 | if(x == 0) return -1;
|
|---|
| 814 | var r = 0;
|
|---|
| 815 | if((x&0xffff) == 0) { x >>= 16; r += 16; }
|
|---|
| 816 | if((x&0xff) == 0) { x >>= 8; r += 8; }
|
|---|
| 817 | if((x&0xf) == 0) { x >>= 4; r += 4; }
|
|---|
| 818 | if((x&3) == 0) { x >>= 2; r += 2; }
|
|---|
| 819 | if((x&1) == 0) ++r;
|
|---|
| 820 | return r;
|
|---|
| 821 | }
|
|---|
| 822 |
|
|---|
| 823 | // (public) returns index of lowest 1-bit (or -1 if none)
|
|---|
| 824 | function bnGetLowestSetBit() {
|
|---|
| 825 | for(var i = 0; i < this.t; ++i)
|
|---|
| 826 | if(this.data[i] != 0) return i*this.DB+lbit(this.data[i]);
|
|---|
| 827 | if(this.s < 0) return this.t*this.DB;
|
|---|
| 828 | return -1;
|
|---|
| 829 | }
|
|---|
| 830 |
|
|---|
| 831 | // return number of 1 bits in x
|
|---|
| 832 | function cbit(x) {
|
|---|
| 833 | var r = 0;
|
|---|
| 834 | while(x != 0) { x &= x-1; ++r; }
|
|---|
| 835 | return r;
|
|---|
| 836 | }
|
|---|
| 837 |
|
|---|
| 838 | // (public) return number of set bits
|
|---|
| 839 | function bnBitCount() {
|
|---|
| 840 | var r = 0, x = this.s&this.DM;
|
|---|
| 841 | for(var i = 0; i < this.t; ++i) r += cbit(this.data[i]^x);
|
|---|
| 842 | return r;
|
|---|
| 843 | }
|
|---|
| 844 |
|
|---|
| 845 | // (public) true iff nth bit is set
|
|---|
| 846 | function bnTestBit(n) {
|
|---|
| 847 | var j = Math.floor(n/this.DB);
|
|---|
| 848 | if(j >= this.t) return(this.s!=0);
|
|---|
| 849 | return((this.data[j]&(1<<(n%this.DB)))!=0);
|
|---|
| 850 | }
|
|---|
| 851 |
|
|---|
| 852 | // (protected) this op (1<<n)
|
|---|
| 853 | function bnpChangeBit(n,op) {
|
|---|
| 854 | var r = BigInteger.ONE.shiftLeft(n);
|
|---|
| 855 | this.bitwiseTo(r,op,r);
|
|---|
| 856 | return r;
|
|---|
| 857 | }
|
|---|
| 858 |
|
|---|
| 859 | // (public) this | (1<<n)
|
|---|
| 860 | function bnSetBit(n) { return this.changeBit(n,op_or); }
|
|---|
| 861 |
|
|---|
| 862 | // (public) this & ~(1<<n)
|
|---|
| 863 | function bnClearBit(n) { return this.changeBit(n,op_andnot); }
|
|---|
| 864 |
|
|---|
| 865 | // (public) this ^ (1<<n)
|
|---|
| 866 | function bnFlipBit(n) { return this.changeBit(n,op_xor); }
|
|---|
| 867 |
|
|---|
| 868 | // (protected) r = this + a
|
|---|
| 869 | function bnpAddTo(a,r) {
|
|---|
| 870 | var i = 0, c = 0, m = Math.min(a.t,this.t);
|
|---|
| 871 | while(i < m) {
|
|---|
| 872 | c += this.data[i]+a.data[i];
|
|---|
| 873 | r.data[i++] = c&this.DM;
|
|---|
| 874 | c >>= this.DB;
|
|---|
| 875 | }
|
|---|
| 876 | if(a.t < this.t) {
|
|---|
| 877 | c += a.s;
|
|---|
| 878 | while(i < this.t) {
|
|---|
| 879 | c += this.data[i];
|
|---|
| 880 | r.data[i++] = c&this.DM;
|
|---|
| 881 | c >>= this.DB;
|
|---|
| 882 | }
|
|---|
| 883 | c += this.s;
|
|---|
| 884 | }
|
|---|
| 885 | else {
|
|---|
| 886 | c += this.s;
|
|---|
| 887 | while(i < a.t) {
|
|---|
| 888 | c += a.data[i];
|
|---|
| 889 | r.data[i++] = c&this.DM;
|
|---|
| 890 | c >>= this.DB;
|
|---|
| 891 | }
|
|---|
| 892 | c += a.s;
|
|---|
| 893 | }
|
|---|
| 894 | r.s = (c<0)?-1:0;
|
|---|
| 895 | if(c > 0) r.data[i++] = c;
|
|---|
| 896 | else if(c < -1) r.data[i++] = this.DV+c;
|
|---|
| 897 | r.t = i;
|
|---|
| 898 | r.clamp();
|
|---|
| 899 | }
|
|---|
| 900 |
|
|---|
| 901 | // (public) this + a
|
|---|
| 902 | function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; }
|
|---|
| 903 |
|
|---|
| 904 | // (public) this - a
|
|---|
| 905 | function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; }
|
|---|
| 906 |
|
|---|
| 907 | // (public) this * a
|
|---|
| 908 | function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; }
|
|---|
| 909 |
|
|---|
| 910 | // (public) this^2
|
|---|
| 911 | function bnSquare() { var r = nbi(); this.squareTo(r); return r; }
|
|---|
| 912 |
|
|---|
| 913 | // (public) this / a
|
|---|
| 914 | function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; }
|
|---|
| 915 |
|
|---|
| 916 | // (public) this % a
|
|---|
| 917 | function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; }
|
|---|
| 918 |
|
|---|
| 919 | // (public) [this/a,this%a]
|
|---|
| 920 | function bnDivideAndRemainder(a) {
|
|---|
| 921 | var q = nbi(), r = nbi();
|
|---|
| 922 | this.divRemTo(a,q,r);
|
|---|
| 923 | return new Array(q,r);
|
|---|
| 924 | }
|
|---|
| 925 |
|
|---|
| 926 | // (protected) this *= n, this >= 0, 1 < n < DV
|
|---|
| 927 | function bnpDMultiply(n) {
|
|---|
| 928 | this.data[this.t] = this.am(0,n-1,this,0,0,this.t);
|
|---|
| 929 | ++this.t;
|
|---|
| 930 | this.clamp();
|
|---|
| 931 | }
|
|---|
| 932 |
|
|---|
| 933 | // (protected) this += n << w words, this >= 0
|
|---|
| 934 | function bnpDAddOffset(n,w) {
|
|---|
| 935 | if(n == 0) return;
|
|---|
| 936 | while(this.t <= w) this.data[this.t++] = 0;
|
|---|
| 937 | this.data[w] += n;
|
|---|
| 938 | while(this.data[w] >= this.DV) {
|
|---|
| 939 | this.data[w] -= this.DV;
|
|---|
| 940 | if(++w >= this.t) this.data[this.t++] = 0;
|
|---|
| 941 | ++this.data[w];
|
|---|
| 942 | }
|
|---|
| 943 | }
|
|---|
| 944 |
|
|---|
| 945 | // A "null" reducer
|
|---|
| 946 | function NullExp() {}
|
|---|
| 947 | function nNop(x) { return x; }
|
|---|
| 948 | function nMulTo(x,y,r) { x.multiplyTo(y,r); }
|
|---|
| 949 | function nSqrTo(x,r) { x.squareTo(r); }
|
|---|
| 950 |
|
|---|
| 951 | NullExp.prototype.convert = nNop;
|
|---|
| 952 | NullExp.prototype.revert = nNop;
|
|---|
| 953 | NullExp.prototype.mulTo = nMulTo;
|
|---|
| 954 | NullExp.prototype.sqrTo = nSqrTo;
|
|---|
| 955 |
|
|---|
| 956 | // (public) this^e
|
|---|
| 957 | function bnPow(e) { return this.exp(e,new NullExp()); }
|
|---|
| 958 |
|
|---|
| 959 | // (protected) r = lower n words of "this * a", a.t <= n
|
|---|
| 960 | // "this" should be the larger one if appropriate.
|
|---|
| 961 | function bnpMultiplyLowerTo(a,n,r) {
|
|---|
| 962 | var i = Math.min(this.t+a.t,n);
|
|---|
| 963 | r.s = 0; // assumes a,this >= 0
|
|---|
| 964 | r.t = i;
|
|---|
| 965 | while(i > 0) r.data[--i] = 0;
|
|---|
| 966 | var j;
|
|---|
| 967 | for(j = r.t-this.t; i < j; ++i) r.data[i+this.t] = this.am(0,a.data[i],r,i,0,this.t);
|
|---|
| 968 | for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a.data[i],r,i,0,n-i);
|
|---|
| 969 | r.clamp();
|
|---|
| 970 | }
|
|---|
| 971 |
|
|---|
| 972 | // (protected) r = "this * a" without lower n words, n > 0
|
|---|
| 973 | // "this" should be the larger one if appropriate.
|
|---|
| 974 | function bnpMultiplyUpperTo(a,n,r) {
|
|---|
| 975 | --n;
|
|---|
| 976 | var i = r.t = this.t+a.t-n;
|
|---|
| 977 | r.s = 0; // assumes a,this >= 0
|
|---|
| 978 | while(--i >= 0) r.data[i] = 0;
|
|---|
| 979 | for(i = Math.max(n-this.t,0); i < a.t; ++i)
|
|---|
| 980 | r.data[this.t+i-n] = this.am(n-i,a.data[i],r,0,0,this.t+i-n);
|
|---|
| 981 | r.clamp();
|
|---|
| 982 | r.drShiftTo(1,r);
|
|---|
| 983 | }
|
|---|
| 984 |
|
|---|
| 985 | // Barrett modular reduction
|
|---|
| 986 | function Barrett(m) {
|
|---|
| 987 | // setup Barrett
|
|---|
| 988 | this.r2 = nbi();
|
|---|
| 989 | this.q3 = nbi();
|
|---|
| 990 | BigInteger.ONE.dlShiftTo(2*m.t,this.r2);
|
|---|
| 991 | this.mu = this.r2.divide(m);
|
|---|
| 992 | this.m = m;
|
|---|
| 993 | }
|
|---|
| 994 |
|
|---|
| 995 | function barrettConvert(x) {
|
|---|
| 996 | if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m);
|
|---|
| 997 | else if(x.compareTo(this.m) < 0) return x;
|
|---|
| 998 | else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
|
|---|
| 999 | }
|
|---|
| 1000 |
|
|---|
| 1001 | function barrettRevert(x) { return x; }
|
|---|
| 1002 |
|
|---|
| 1003 | // x = x mod m (HAC 14.42)
|
|---|
| 1004 | function barrettReduce(x) {
|
|---|
| 1005 | x.drShiftTo(this.m.t-1,this.r2);
|
|---|
| 1006 | if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
|
|---|
| 1007 | this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);
|
|---|
| 1008 | this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);
|
|---|
| 1009 | while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1);
|
|---|
| 1010 | x.subTo(this.r2,x);
|
|---|
| 1011 | while(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
|
|---|
| 1012 | }
|
|---|
| 1013 |
|
|---|
| 1014 | // r = x^2 mod m; x != r
|
|---|
| 1015 | function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
|
|---|
| 1016 |
|
|---|
| 1017 | // r = x*y mod m; x,y != r
|
|---|
| 1018 | function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
|
|---|
| 1019 |
|
|---|
| 1020 | Barrett.prototype.convert = barrettConvert;
|
|---|
| 1021 | Barrett.prototype.revert = barrettRevert;
|
|---|
| 1022 | Barrett.prototype.reduce = barrettReduce;
|
|---|
| 1023 | Barrett.prototype.mulTo = barrettMulTo;
|
|---|
| 1024 | Barrett.prototype.sqrTo = barrettSqrTo;
|
|---|
| 1025 |
|
|---|
| 1026 | // (public) this^e % m (HAC 14.85)
|
|---|
| 1027 | function bnModPow(e,m) {
|
|---|
| 1028 | var i = e.bitLength(), k, r = nbv(1), z;
|
|---|
| 1029 | if(i <= 0) return r;
|
|---|
| 1030 | else if(i < 18) k = 1;
|
|---|
| 1031 | else if(i < 48) k = 3;
|
|---|
| 1032 | else if(i < 144) k = 4;
|
|---|
| 1033 | else if(i < 768) k = 5;
|
|---|
| 1034 | else k = 6;
|
|---|
| 1035 | if(i < 8)
|
|---|
| 1036 | z = new Classic(m);
|
|---|
| 1037 | else if(m.isEven())
|
|---|
| 1038 | z = new Barrett(m);
|
|---|
| 1039 | else
|
|---|
| 1040 | z = new Montgomery(m);
|
|---|
| 1041 |
|
|---|
| 1042 | // precomputation
|
|---|
| 1043 | var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1;
|
|---|
| 1044 | g[1] = z.convert(this);
|
|---|
| 1045 | if(k > 1) {
|
|---|
| 1046 | var g2 = nbi();
|
|---|
| 1047 | z.sqrTo(g[1],g2);
|
|---|
| 1048 | while(n <= km) {
|
|---|
| 1049 | g[n] = nbi();
|
|---|
| 1050 | z.mulTo(g2,g[n-2],g[n]);
|
|---|
| 1051 | n += 2;
|
|---|
| 1052 | }
|
|---|
| 1053 | }
|
|---|
| 1054 |
|
|---|
| 1055 | var j = e.t-1, w, is1 = true, r2 = nbi(), t;
|
|---|
| 1056 | i = nbits(e.data[j])-1;
|
|---|
| 1057 | while(j >= 0) {
|
|---|
| 1058 | if(i >= k1) w = (e.data[j]>>(i-k1))&km;
|
|---|
| 1059 | else {
|
|---|
| 1060 | w = (e.data[j]&((1<<(i+1))-1))<<(k1-i);
|
|---|
| 1061 | if(j > 0) w |= e.data[j-1]>>(this.DB+i-k1);
|
|---|
| 1062 | }
|
|---|
| 1063 |
|
|---|
| 1064 | n = k;
|
|---|
| 1065 | while((w&1) == 0) { w >>= 1; --n; }
|
|---|
| 1066 | if((i -= n) < 0) { i += this.DB; --j; }
|
|---|
| 1067 | if(is1) { // ret == 1, don't bother squaring or multiplying it
|
|---|
| 1068 | g[w].copyTo(r);
|
|---|
| 1069 | is1 = false;
|
|---|
| 1070 | }
|
|---|
| 1071 | else {
|
|---|
| 1072 | while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; }
|
|---|
| 1073 | if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; }
|
|---|
| 1074 | z.mulTo(r2,g[w],r);
|
|---|
| 1075 | }
|
|---|
| 1076 |
|
|---|
| 1077 | while(j >= 0 && (e.data[j]&(1<<i)) == 0) {
|
|---|
| 1078 | z.sqrTo(r,r2); t = r; r = r2; r2 = t;
|
|---|
| 1079 | if(--i < 0) { i = this.DB-1; --j; }
|
|---|
| 1080 | }
|
|---|
| 1081 | }
|
|---|
| 1082 | return z.revert(r);
|
|---|
| 1083 | }
|
|---|
| 1084 |
|
|---|
| 1085 | // (public) gcd(this,a) (HAC 14.54)
|
|---|
| 1086 | function bnGCD(a) {
|
|---|
| 1087 | var x = (this.s<0)?this.negate():this.clone();
|
|---|
| 1088 | var y = (a.s<0)?a.negate():a.clone();
|
|---|
| 1089 | if(x.compareTo(y) < 0) { var t = x; x = y; y = t; }
|
|---|
| 1090 | var i = x.getLowestSetBit(), g = y.getLowestSetBit();
|
|---|
| 1091 | if(g < 0) return x;
|
|---|
| 1092 | if(i < g) g = i;
|
|---|
| 1093 | if(g > 0) {
|
|---|
| 1094 | x.rShiftTo(g,x);
|
|---|
| 1095 | y.rShiftTo(g,y);
|
|---|
| 1096 | }
|
|---|
| 1097 | while(x.signum() > 0) {
|
|---|
| 1098 | if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x);
|
|---|
| 1099 | if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y);
|
|---|
| 1100 | if(x.compareTo(y) >= 0) {
|
|---|
| 1101 | x.subTo(y,x);
|
|---|
| 1102 | x.rShiftTo(1,x);
|
|---|
| 1103 | }
|
|---|
| 1104 | else {
|
|---|
| 1105 | y.subTo(x,y);
|
|---|
| 1106 | y.rShiftTo(1,y);
|
|---|
| 1107 | }
|
|---|
| 1108 | }
|
|---|
| 1109 | if(g > 0) y.lShiftTo(g,y);
|
|---|
| 1110 | return y;
|
|---|
| 1111 | }
|
|---|
| 1112 |
|
|---|
| 1113 | // (protected) this % n, n < 2^26
|
|---|
| 1114 | function bnpModInt(n) {
|
|---|
| 1115 | if(n <= 0) return 0;
|
|---|
| 1116 | var d = this.DV%n, r = (this.s<0)?n-1:0;
|
|---|
| 1117 | if(this.t > 0)
|
|---|
| 1118 | if(d == 0) r = this.data[0]%n;
|
|---|
| 1119 | else for(var i = this.t-1; i >= 0; --i) r = (d*r+this.data[i])%n;
|
|---|
| 1120 | return r;
|
|---|
| 1121 | }
|
|---|
| 1122 |
|
|---|
| 1123 | // (public) 1/this % m (HAC 14.61)
|
|---|
| 1124 | function bnModInverse(m) {
|
|---|
| 1125 | // FORGE: jsbn fix
|
|---|
| 1126 | // avoid infinite loop
|
|---|
| 1127 | if(this.signum() == 0) {
|
|---|
| 1128 | // returning zero to align with similar behavior when no multiplicative
|
|---|
| 1129 | // inverse module m is found.
|
|---|
| 1130 | return BigInteger.ZERO;
|
|---|
| 1131 | }
|
|---|
| 1132 | var ac = m.isEven();
|
|---|
| 1133 | if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO;
|
|---|
| 1134 | var u = m.clone(), v = this.clone();
|
|---|
| 1135 | var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
|
|---|
| 1136 | while(u.signum() != 0) {
|
|---|
| 1137 | while(u.isEven()) {
|
|---|
| 1138 | u.rShiftTo(1,u);
|
|---|
| 1139 | if(ac) {
|
|---|
| 1140 | if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); }
|
|---|
| 1141 | a.rShiftTo(1,a);
|
|---|
| 1142 | }
|
|---|
| 1143 | else if(!b.isEven()) b.subTo(m,b);
|
|---|
| 1144 | b.rShiftTo(1,b);
|
|---|
| 1145 | }
|
|---|
| 1146 | while(v.isEven()) {
|
|---|
| 1147 | v.rShiftTo(1,v);
|
|---|
| 1148 | if(ac) {
|
|---|
| 1149 | if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); }
|
|---|
| 1150 | c.rShiftTo(1,c);
|
|---|
| 1151 | }
|
|---|
| 1152 | else if(!d.isEven()) d.subTo(m,d);
|
|---|
| 1153 | d.rShiftTo(1,d);
|
|---|
| 1154 | }
|
|---|
| 1155 | if(u.compareTo(v) >= 0) {
|
|---|
| 1156 | u.subTo(v,u);
|
|---|
| 1157 | if(ac) a.subTo(c,a);
|
|---|
| 1158 | b.subTo(d,b);
|
|---|
| 1159 | }
|
|---|
| 1160 | else {
|
|---|
| 1161 | v.subTo(u,v);
|
|---|
| 1162 | if(ac) c.subTo(a,c);
|
|---|
| 1163 | d.subTo(b,d);
|
|---|
| 1164 | }
|
|---|
| 1165 | }
|
|---|
| 1166 | if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
|
|---|
| 1167 | if(d.compareTo(m) >= 0) return d.subtract(m);
|
|---|
| 1168 | if(d.signum() < 0) d.addTo(m,d); else return d;
|
|---|
| 1169 | if(d.signum() < 0) return d.add(m); else return d;
|
|---|
| 1170 | }
|
|---|
| 1171 |
|
|---|
| 1172 | 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,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997];
|
|---|
| 1173 | var lplim = (1<<26)/lowprimes[lowprimes.length-1];
|
|---|
| 1174 |
|
|---|
| 1175 | // (public) test primality with certainty >= 1-.5^t
|
|---|
| 1176 | function bnIsProbablePrime(t) {
|
|---|
| 1177 | var i, x = this.abs();
|
|---|
| 1178 | if(x.t == 1 && x.data[0] <= lowprimes[lowprimes.length-1]) {
|
|---|
| 1179 | for(i = 0; i < lowprimes.length; ++i)
|
|---|
| 1180 | if(x.data[0] == lowprimes[i]) return true;
|
|---|
| 1181 | return false;
|
|---|
| 1182 | }
|
|---|
| 1183 | if(x.isEven()) return false;
|
|---|
| 1184 | i = 1;
|
|---|
| 1185 | while(i < lowprimes.length) {
|
|---|
| 1186 | var m = lowprimes[i], j = i+1;
|
|---|
| 1187 | while(j < lowprimes.length && m < lplim) m *= lowprimes[j++];
|
|---|
| 1188 | m = x.modInt(m);
|
|---|
| 1189 | while(i < j) if(m%lowprimes[i++] == 0) return false;
|
|---|
| 1190 | }
|
|---|
| 1191 | return x.millerRabin(t);
|
|---|
| 1192 | }
|
|---|
| 1193 |
|
|---|
| 1194 | // (protected) true if probably prime (HAC 4.24, Miller-Rabin)
|
|---|
| 1195 | function bnpMillerRabin(t) {
|
|---|
| 1196 | var n1 = this.subtract(BigInteger.ONE);
|
|---|
| 1197 | var k = n1.getLowestSetBit();
|
|---|
| 1198 | if(k <= 0) return false;
|
|---|
| 1199 | var r = n1.shiftRight(k);
|
|---|
| 1200 | var prng = bnGetPrng();
|
|---|
| 1201 | var a;
|
|---|
| 1202 | for(var i = 0; i < t; ++i) {
|
|---|
| 1203 | // select witness 'a' at random from between 1 and n1
|
|---|
| 1204 | do {
|
|---|
| 1205 | a = new BigInteger(this.bitLength(), prng);
|
|---|
| 1206 | }
|
|---|
| 1207 | while(a.compareTo(BigInteger.ONE) <= 0 || a.compareTo(n1) >= 0);
|
|---|
| 1208 | var y = a.modPow(r,this);
|
|---|
| 1209 | if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
|
|---|
| 1210 | var j = 1;
|
|---|
| 1211 | while(j++ < k && y.compareTo(n1) != 0) {
|
|---|
| 1212 | y = y.modPowInt(2,this);
|
|---|
| 1213 | if(y.compareTo(BigInteger.ONE) == 0) return false;
|
|---|
| 1214 | }
|
|---|
| 1215 | if(y.compareTo(n1) != 0) return false;
|
|---|
| 1216 | }
|
|---|
| 1217 | }
|
|---|
| 1218 | return true;
|
|---|
| 1219 | }
|
|---|
| 1220 |
|
|---|
| 1221 | // get pseudo random number generator
|
|---|
| 1222 | function bnGetPrng() {
|
|---|
| 1223 | // create prng with api that matches BigInteger secure random
|
|---|
| 1224 | return {
|
|---|
| 1225 | // x is an array to fill with bytes
|
|---|
| 1226 | nextBytes: function(x) {
|
|---|
| 1227 | for(var i = 0; i < x.length; ++i) {
|
|---|
| 1228 | x[i] = Math.floor(Math.random() * 0x0100);
|
|---|
| 1229 | }
|
|---|
| 1230 | }
|
|---|
| 1231 | };
|
|---|
| 1232 | }
|
|---|
| 1233 |
|
|---|
| 1234 | // protected
|
|---|
| 1235 | BigInteger.prototype.chunkSize = bnpChunkSize;
|
|---|
| 1236 | BigInteger.prototype.toRadix = bnpToRadix;
|
|---|
| 1237 | BigInteger.prototype.fromRadix = bnpFromRadix;
|
|---|
| 1238 | BigInteger.prototype.fromNumber = bnpFromNumber;
|
|---|
| 1239 | BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
|
|---|
| 1240 | BigInteger.prototype.changeBit = bnpChangeBit;
|
|---|
| 1241 | BigInteger.prototype.addTo = bnpAddTo;
|
|---|
| 1242 | BigInteger.prototype.dMultiply = bnpDMultiply;
|
|---|
| 1243 | BigInteger.prototype.dAddOffset = bnpDAddOffset;
|
|---|
| 1244 | BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
|
|---|
| 1245 | BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
|
|---|
| 1246 | BigInteger.prototype.modInt = bnpModInt;
|
|---|
| 1247 | BigInteger.prototype.millerRabin = bnpMillerRabin;
|
|---|
| 1248 |
|
|---|
| 1249 | // public
|
|---|
| 1250 | BigInteger.prototype.clone = bnClone;
|
|---|
| 1251 | BigInteger.prototype.intValue = bnIntValue;
|
|---|
| 1252 | BigInteger.prototype.byteValue = bnByteValue;
|
|---|
| 1253 | BigInteger.prototype.shortValue = bnShortValue;
|
|---|
| 1254 | BigInteger.prototype.signum = bnSigNum;
|
|---|
| 1255 | BigInteger.prototype.toByteArray = bnToByteArray;
|
|---|
| 1256 | BigInteger.prototype.equals = bnEquals;
|
|---|
| 1257 | BigInteger.prototype.min = bnMin;
|
|---|
| 1258 | BigInteger.prototype.max = bnMax;
|
|---|
| 1259 | BigInteger.prototype.and = bnAnd;
|
|---|
| 1260 | BigInteger.prototype.or = bnOr;
|
|---|
| 1261 | BigInteger.prototype.xor = bnXor;
|
|---|
| 1262 | BigInteger.prototype.andNot = bnAndNot;
|
|---|
| 1263 | BigInteger.prototype.not = bnNot;
|
|---|
| 1264 | BigInteger.prototype.shiftLeft = bnShiftLeft;
|
|---|
| 1265 | BigInteger.prototype.shiftRight = bnShiftRight;
|
|---|
| 1266 | BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
|
|---|
| 1267 | BigInteger.prototype.bitCount = bnBitCount;
|
|---|
| 1268 | BigInteger.prototype.testBit = bnTestBit;
|
|---|
| 1269 | BigInteger.prototype.setBit = bnSetBit;
|
|---|
| 1270 | BigInteger.prototype.clearBit = bnClearBit;
|
|---|
| 1271 | BigInteger.prototype.flipBit = bnFlipBit;
|
|---|
| 1272 | BigInteger.prototype.add = bnAdd;
|
|---|
| 1273 | BigInteger.prototype.subtract = bnSubtract;
|
|---|
| 1274 | BigInteger.prototype.multiply = bnMultiply;
|
|---|
| 1275 | BigInteger.prototype.divide = bnDivide;
|
|---|
| 1276 | BigInteger.prototype.remainder = bnRemainder;
|
|---|
| 1277 | BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
|
|---|
| 1278 | BigInteger.prototype.modPow = bnModPow;
|
|---|
| 1279 | BigInteger.prototype.modInverse = bnModInverse;
|
|---|
| 1280 | BigInteger.prototype.pow = bnPow;
|
|---|
| 1281 | BigInteger.prototype.gcd = bnGCD;
|
|---|
| 1282 | BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
|
|---|
| 1283 |
|
|---|
| 1284 | // JSBN-specific extension
|
|---|
| 1285 | BigInteger.prototype.square = bnSquare;
|
|---|
| 1286 |
|
|---|
| 1287 | // BigInteger interfaces not implemented in jsbn:
|
|---|
| 1288 |
|
|---|
| 1289 | // BigInteger(int signum, byte[] magnitude)
|
|---|
| 1290 | // double doubleValue()
|
|---|
| 1291 | // float floatValue()
|
|---|
| 1292 | // int hashCode()
|
|---|
| 1293 | // long longValue()
|
|---|
| 1294 | // static BigInteger valueOf(long val)
|
|---|