1 // Copyright (c) 2005 Tom Wu
2 // All Rights Reserved.
3 // See "LICENSE" for details.
5 // Basic JavaScript BN library - subset useful for RSA encryption.
10 // JavaScript engine analysis
11 var canary = 0xdeadbeefcafe;
12 var j_lm = ((canary&0xffffff)==0xefcafe);
14 // (public) Constructor
15 function BigInteger(a,b,c) {
17 if("number" == typeof a) this.fromNumber(a,b,c);
18 else if(b == null && "string" != typeof a) this.fromString(a,256);
19 else this.fromString(a,b);
22 // return new, unset BigInteger
23 function nbi() { return new BigInteger(null); }
25 // am: Compute w_j += (x*this_i), propagate carries,
26 // c is initial carry, returns final carry.
27 // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
28 // We need to select the fastest one that works in this environment.
30 // am1: use a single mult and divide to get the high bits,
31 // max digit bits should be 26 because
32 // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
33 function am1(i,x,w,j,c,n) {
35 var v = x*this[i++]+w[j]+c;
36 c = Math.floor(v/0x4000000);
41 // am2 avoids a big mult-and-extract completely.
42 // Max digit bits should be <= 30 because we do bitwise ops
43 // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
44 function am2(i,x,w,j,c,n) {
45 var xl = x&0x7fff, xh = x>>15;
47 var l = this[i]&0x7fff;
48 var h = this[i++]>>15;
50 l = xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff);
51 c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
52 w[j++] = l&0x3fffffff;
56 // Alternately, set max digit bits to 28 since some
57 // browsers slow down when dealing with 32-bit numbers.
58 function am3(i,x,w,j,c,n) {
59 var xl = x&0x3fff, xh = x>>14;
61 var l = this[i]&0x3fff;
62 var h = this[i++]>>14;
64 l = xl*l+((m&0x3fff)<<14)+w[j]+c;
65 c = (l>>28)+(m>>14)+xh*h;
70 if(j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
71 BigInteger.prototype.am = am2;
74 else if(j_lm && (navigator.appName != "Netscape")) {
75 BigInteger.prototype.am = am1;
78 else { // Mozilla/Netscape seems to prefer am3
79 BigInteger.prototype.am = am3;
83 BigInteger.prototype.DB = dbits;
84 BigInteger.prototype.DM = ((1<<dbits)-1);
85 BigInteger.prototype.DV = (1<<dbits);
88 BigInteger.prototype.FV = Math.pow(2,BI_FP);
89 BigInteger.prototype.F1 = BI_FP-dbits;
90 BigInteger.prototype.F2 = 2*dbits-BI_FP;
93 var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
94 var BI_RC = new Array();
96 rr = "0".charCodeAt(0);
97 for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
98 rr = "a".charCodeAt(0);
99 for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
100 rr = "A".charCodeAt(0);
101 for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
103 function int2char(n) { return BI_RM.charAt(n); }
104 function intAt(s,i) {
105 var c = BI_RC[s.charCodeAt(i)];
106 return (c==null)?-1:c;
109 // (protected) copy this to r
110 function bnpCopyTo(r) {
111 for(var i = this.t-1; i >= 0; --i) r[i] = this[i];
116 // (protected) set from integer value x, -DV <= x < DV
117 function bnpFromInt(x) {
120 if(x > 0) this[0] = x;
121 else if(x < -1) this[0] = x+DV;
125 // return bigint initialized to value
126 function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
128 // (protected) set from string and radix
129 function bnpFromString(s,b) {
132 else if(b == 8) k = 3;
133 else if(b == 256) k = 8; // byte array
134 else if(b == 2) k = 1;
135 else if(b == 32) k = 5;
136 else if(b == 4) k = 2;
137 else { this.fromRadix(s,b); return; }
140 var i = s.length, mi = false, sh = 0;
142 var x = (k==8)?s[i]&0xff:intAt(s,i);
144 if(s.charAt(i) == "-") mi = true;
150 else if(sh+k > this.DB) {
151 this[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh;
152 this[this.t++] = (x>>(this.DB-sh));
155 this[this.t-1] |= x<<sh;
157 if(sh >= this.DB) sh -= this.DB;
159 if(k == 8 && (s[0]&0x80) != 0) {
161 if(sh > 0) this[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh;
164 if(mi) BigInteger.ZERO.subTo(this,this);
167 // (protected) clamp off excess high words
168 function bnpClamp() {
169 var c = this.s&this.DM;
170 while(this.t > 0 && this[this.t-1] == c) --this.t;
173 // (public) return string representation in given radix
174 function bnToString(b) {
175 if(this.s < 0) return "-"+this.negate().toString(b);
178 else if(b == 8) k = 3;
179 else if(b == 2) k = 1;
180 else if(b == 32) k = 5;
181 else if(b == 4) k = 2;
182 else return this.toRadix(b);
183 var km = (1<<k)-1, d, m = false, r = "", i = this.t;
184 var p = this.DB-(i*this.DB)%k;
186 if(p < this.DB && (d = this[i]>>p) > 0) { m = true; r = int2char(d); }
189 d = (this[i]&((1<<p)-1))<<(k-p);
190 d |= this[--i]>>(p+=this.DB-k);
193 d = (this[i]>>(p-=k))&km;
194 if(p <= 0) { p += this.DB; --i; }
197 if(m) r += int2char(d);
204 function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
207 function bnAbs() { return (this.s<0)?this.negate():this; }
209 // (public) return + if this > a, - if this < a, 0 if equal
210 function bnCompareTo(a) {
216 while(--i >= 0) if((r=this[i]-a[i]) != 0) return r;
220 // returns bit length of the integer x
223 if((t=x>>>16) != 0) { x = t; r += 16; }
224 if((t=x>>8) != 0) { x = t; r += 8; }
225 if((t=x>>4) != 0) { x = t; r += 4; }
226 if((t=x>>2) != 0) { x = t; r += 2; }
227 if((t=x>>1) != 0) { x = t; r += 1; }
231 // (public) return the number of bits in "this"
232 function bnBitLength() {
233 if(this.t <= 0) return 0;
234 return this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM));
237 // (protected) r = this << n*DB
238 function bnpDLShiftTo(n,r) {
240 for(i = this.t-1; i >= 0; --i) r[i+n] = this[i];
241 for(i = n-1; i >= 0; --i) r[i] = 0;
246 // (protected) r = this >> n*DB
247 function bnpDRShiftTo(n,r) {
248 for(var i = n; i < this.t; ++i) r[i-n] = this[i];
249 r.t = Math.max(this.t-n,0);
253 // (protected) r = this << n
254 function bnpLShiftTo(n,r) {
256 var cbs = this.DB-bs;
258 var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i;
259 for(i = this.t-1; i >= 0; --i) {
260 r[i+ds+1] = (this[i]>>cbs)|c;
261 c = (this[i]&bm)<<bs;
263 for(i = ds-1; i >= 0; --i) r[i] = 0;
270 // (protected) r = this >> n
271 function bnpRShiftTo(n,r) {
273 var ds = Math.floor(n/this.DB);
274 if(ds >= this.t) { r.t = 0; return; }
276 var cbs = this.DB-bs;
279 for(var i = ds+1; i < this.t; ++i) {
280 r[i-ds-1] |= (this[i]&bm)<<cbs;
281 r[i-ds] = this[i]>>bs;
283 if(bs > 0) r[this.t-ds-1] |= (this.s&bm)<<cbs;
288 // (protected) r = this - a
289 function bnpSubTo(a,r) {
290 var i = 0, c = 0, m = Math.min(a.t,this.t);
315 if(c < -1) r[i++] = this.DV+c;
316 else if(c > 0) r[i++] = c;
321 // (protected) r = this * a, r != this,a (HAC 14.12)
322 // "this" should be the larger one if appropriate.
323 function bnpMultiplyTo(a,r) {
324 var x = this.abs(), y = a.abs();
327 while(--i >= 0) r[i] = 0;
328 for(i = 0; i < y.t; ++i) r[i+x.t] = x.am(0,y[i],r,i,0,x.t);
331 if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
334 // (protected) r = this^2, r != this (HAC 14.16)
335 function bnpSquareTo(r) {
338 while(--i >= 0) r[i] = 0;
339 for(i = 0; i < x.t-1; ++i) {
340 var c = x.am(i,x[i],r,2*i,0,1);
341 if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1)) >= x.DV) {
346 if(r.t > 0) r[r.t-1] += x.am(i,x[i],r,2*i,0,1);
351 // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
352 // r != q, this != m. q or r may be null.
353 function bnpDivRemTo(m,q,r) {
355 if(pm.t <= 0) return;
358 if(q != null) q.fromInt(0);
359 if(r != null) this.copyTo(r);
362 if(r == null) r = nbi();
363 var y = nbi(), ts = this.s, ms = m.s;
364 var nsh = this.DB-nbits(pm[pm.t-1]); // normalize modulus
365 if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
366 else { pm.copyTo(y); pt.copyTo(r); }
370 var yt = y0*(1<<this.F1)+((ys>1)?y[ys-2]>>this.F2:0);
371 var d1 = this.FV/yt, d2 = (1<<this.F1)/yt, e = 1<<this.F2;
372 var i = r.t, j = i-ys, t = (q==null)?nbi():q;
374 if(r.compareTo(t) >= 0) {
378 BigInteger.ONE.dlShiftTo(ys,t);
379 t.subTo(y,y); // "negative" y so we can replace sub with am later
380 while(y.t < ys) y[y.t++] = 0;
382 // Estimate quotient digit
383 var qd = (r[--i]==y0)?this.DM:Math.floor(r[i]*d1+(r[i-1]+e)*d2);
384 if((r[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out
387 while(r[i] < --qd) r.subTo(t,r);
392 if(ts != ms) BigInteger.ZERO.subTo(q,q);
396 if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder
397 if(ts < 0) BigInteger.ZERO.subTo(r,r);
400 // (public) this mod a
403 this.abs().divRemTo(a,null,r);
404 if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r);
408 // Modular reduction using "classic" algorithm
409 function Classic(m) { this.m = m; }
410 function cConvert(x) {
411 if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
414 function cRevert(x) { return x; }
415 function cReduce(x) { x.divRemTo(this.m,null,x); }
416 function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
417 function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
419 Classic.prototype.convert = cConvert;
420 Classic.prototype.revert = cRevert;
421 Classic.prototype.reduce = cReduce;
422 Classic.prototype.mulTo = cMulTo;
423 Classic.prototype.sqrTo = cSqrTo;
425 // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
429 // xy(2-xy) = (1+km)(1-km)
430 // x[y(2-xy)] = 1-k^2m^2
431 // x[y(2-xy)] == 1 (mod m^2)
432 // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
433 // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
434 // JS multiply "overflows" differently from C/C++, so care is needed here.
435 function bnpInvDigit() {
436 if(this.t < 1) return 0;
438 if((x&1) == 0) return 0;
439 var y = x&3; // y == 1/x mod 2^2
440 y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4
441 y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8
442 y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16
443 // last step - calculate inverse mod DV directly;
444 // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
445 y = (y*(2-x*y%this.DV))%this.DV; // y == 1/x mod 2^dbits
446 // we really want the negative inverse, and -DV < y < DV
447 return (y>0)?this.DV-y:-y;
450 // Montgomery reduction
451 function Montgomery(m) {
453 this.mp = m.invDigit();
454 this.mpl = this.mp&0x7fff;
455 this.mph = this.mp>>15;
456 this.um = (1<<(m.DB-15))-1;
461 function montConvert(x) {
463 x.abs().dlShiftTo(this.m.t,r);
464 r.divRemTo(this.m,null,r);
465 if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r);
470 function montRevert(x) {
477 // x = x/R mod m (HAC 14.32)
478 function montReduce(x) {
479 while(x.t <= this.mt2) // pad x so am has enough room later
481 for(var i = 0; i < this.m.t; ++i) {
482 // faster way of calculating u0 = x[i]*mp mod DV
484 var u0 = (j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.DM;
485 // use am to combine the multiply-shift-add into one call
487 x[j] += this.m.am(0,u0,x,i,0,this.m.t);
489 while(x[j] >= x.DV) { x[j] -= x.DV; x[++j]++; }
492 x.drShiftTo(this.m.t,x);
493 if(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
496 // r = "x^2/R mod m"; x != r
497 function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
499 // r = "xy/R mod m"; x,y != r
500 function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
502 Montgomery.prototype.convert = montConvert;
503 Montgomery.prototype.revert = montRevert;
504 Montgomery.prototype.reduce = montReduce;
505 Montgomery.prototype.mulTo = montMulTo;
506 Montgomery.prototype.sqrTo = montSqrTo;
508 // (protected) true iff this is even
509 function bnpIsEven() { return ((this.t>0)?(this[0]&1):this.s) == 0; }
511 // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
512 function bnpExp(e,z) {
513 if(e > 0xffffffff || e < 1) return BigInteger.ONE;
514 var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
518 if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
519 else { var t = r; r = r2; r2 = t; }
524 // (public) this^e % m, 0 <= e < 2^32
525 function bnModPowInt(e,m) {
527 if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
528 return this.exp(e,z);
532 BigInteger.prototype.copyTo = bnpCopyTo;
533 BigInteger.prototype.fromInt = bnpFromInt;
534 BigInteger.prototype.fromString = bnpFromString;
535 BigInteger.prototype.clamp = bnpClamp;
536 BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
537 BigInteger.prototype.drShiftTo = bnpDRShiftTo;
538 BigInteger.prototype.lShiftTo = bnpLShiftTo;
539 BigInteger.prototype.rShiftTo = bnpRShiftTo;
540 BigInteger.prototype.subTo = bnpSubTo;
541 BigInteger.prototype.multiplyTo = bnpMultiplyTo;
542 BigInteger.prototype.squareTo = bnpSquareTo;
543 BigInteger.prototype.divRemTo = bnpDivRemTo;
544 BigInteger.prototype.invDigit = bnpInvDigit;
545 BigInteger.prototype.isEven = bnpIsEven;
546 BigInteger.prototype.exp = bnpExp;
549 BigInteger.prototype.toString = bnToString;
550 BigInteger.prototype.negate = bnNegate;
551 BigInteger.prototype.abs = bnAbs;
552 BigInteger.prototype.compareTo = bnCompareTo;
553 BigInteger.prototype.bitLength = bnBitLength;
554 BigInteger.prototype.mod = bnMod;
555 BigInteger.prototype.modPowInt = bnModPowInt;
558 BigInteger.ZERO = nbv(0);
559 BigInteger.ONE = nbv(1);