michael@0: /* michael@0: * Copyright (c) 2003-2005 Tom Wu michael@0: * All Rights Reserved. michael@0: * michael@0: * Permission is hereby granted, free of charge, to any person obtaining michael@0: * a copy of this software and associated documentation files (the michael@0: * "Software"), to deal in the Software without restriction, including michael@0: * without limitation the rights to use, copy, modify, merge, publish, michael@0: * distribute, sublicense, and/or sell copies of the Software, and to michael@0: * permit persons to whom the Software is furnished to do so, subject to michael@0: * the following conditions: michael@0: * michael@0: * The above copyright notice and this permission notice shall be michael@0: * included in all copies or substantial portions of the Software. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, michael@0: * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY michael@0: * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. michael@0: * michael@0: * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL, michael@0: * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER michael@0: * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF michael@0: * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT michael@0: * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. michael@0: * michael@0: * In addition, the following condition applies: michael@0: * michael@0: * All redistributions must retain an intact copy of this copyright notice michael@0: * and disclaimer. michael@0: */ michael@0: michael@0: michael@0: // The code has been adapted for use as a benchmark by Google. michael@0: var Crypto = new BenchmarkSuite('Crypto', 266181, [ michael@0: new Benchmark("Encrypt", encrypt), michael@0: new Benchmark("Decrypt", decrypt) michael@0: ]); michael@0: michael@0: michael@0: // Basic JavaScript BN library - subset useful for RSA encryption. michael@0: michael@0: // Bits per digit michael@0: var dbits; michael@0: var BI_DB; michael@0: var BI_DM; michael@0: var BI_DV; michael@0: michael@0: var BI_FP; michael@0: var BI_FV; michael@0: var BI_F1; michael@0: var BI_F2; michael@0: michael@0: // JavaScript engine analysis michael@0: var canary = 0xdeadbeefcafe; michael@0: var j_lm = ((canary&0xffffff)==0xefcafe); michael@0: michael@0: // (public) Constructor michael@0: function BigInteger(a,b,c) { michael@0: this.array = new Array(); michael@0: if(a != null) michael@0: if("number" == typeof a) this.fromNumber(a,b,c); michael@0: else if(b == null && "string" != typeof a) this.fromString(a,256); michael@0: else this.fromString(a,b); michael@0: } michael@0: michael@0: // return new, unset BigInteger michael@0: function nbi() { return new BigInteger(null); } michael@0: michael@0: // am: Compute w_j += (x*this_i), propagate carries, michael@0: // c is initial carry, returns final carry. michael@0: // c < 3*dvalue, x < 2*dvalue, this_i < dvalue michael@0: // We need to select the fastest one that works in this environment. michael@0: michael@0: // am1: use a single mult and divide to get the high bits, michael@0: // max digit bits should be 26 because michael@0: // max internal value = 2*dvalue^2-2*dvalue (< 2^53) michael@0: function am1(i,x,w,j,c,n) { michael@0: var this_array = this.array; michael@0: var w_array = w.array; michael@0: while(--n >= 0) { michael@0: var v = x*this_array[i++]+w_array[j]+c; michael@0: c = Math.floor(v/0x4000000); michael@0: w_array[j++] = v&0x3ffffff; michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: // am2 avoids a big mult-and-extract completely. michael@0: // Max digit bits should be <= 30 because we do bitwise ops michael@0: // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31) michael@0: function am2(i,x,w,j,c,n) { michael@0: var this_array = this.array; michael@0: var w_array = w.array; michael@0: var xl = x&0x7fff, xh = x>>15; michael@0: while(--n >= 0) { michael@0: var l = this_array[i]&0x7fff; michael@0: var h = this_array[i++]>>15; michael@0: var m = xh*l+h*xl; michael@0: l = xl*l+((m&0x7fff)<<15)+w_array[j]+(c&0x3fffffff); michael@0: c = (l>>>30)+(m>>>15)+xh*h+(c>>>30); michael@0: w_array[j++] = l&0x3fffffff; michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: // Alternately, set max digit bits to 28 since some michael@0: // browsers slow down when dealing with 32-bit numbers. michael@0: function am3(i,x,w,j,c,n) { michael@0: var this_array = this.array; michael@0: var w_array = w.array; michael@0: michael@0: var xl = x&0x3fff, xh = x>>14; michael@0: while(--n >= 0) { michael@0: var l = this_array[i]&0x3fff; michael@0: var h = this_array[i++]>>14; michael@0: var m = xh*l+h*xl; michael@0: l = xl*l+((m&0x3fff)<<14)+w_array[j]+c; michael@0: c = (l>>28)+(m>>14)+xh*h; michael@0: w_array[j++] = l&0xfffffff; michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: // This is tailored to VMs with 2-bit tagging. It makes sure michael@0: // that all the computations stay within the 29 bits available. michael@0: function am4(i,x,w,j,c,n) { michael@0: var this_array = this.array; michael@0: var w_array = w.array; michael@0: michael@0: var xl = x&0x1fff, xh = x>>13; michael@0: while(--n >= 0) { michael@0: var l = this_array[i]&0x1fff; michael@0: var h = this_array[i++]>>13; michael@0: var m = xh*l+h*xl; michael@0: l = xl*l+((m&0x1fff)<<13)+w_array[j]+c; michael@0: c = (l>>26)+(m>>13)+xh*h; michael@0: w_array[j++] = l&0x3ffffff; michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: // am3/28 is best for SM, Rhino, but am4/26 is best for v8. michael@0: // Kestrel (Opera 9.5) gets its best result with am4/26. michael@0: // IE7 does 9% better with am3/28 than with am4/26. michael@0: // Firefox (SM) gets 10% faster with am3/28 than with am4/26. michael@0: michael@0: setupEngine = function(fn, bits) { michael@0: BigInteger.prototype.am = fn; michael@0: dbits = bits; michael@0: michael@0: BI_DB = dbits; michael@0: BI_DM = ((1<= 0; --i) r_array[i] = this_array[i]; michael@0: r.t = this.t; michael@0: r.s = this.s; michael@0: } michael@0: michael@0: // (protected) set from integer value x, -DV <= x < DV michael@0: function bnpFromInt(x) { michael@0: var this_array = this.array; michael@0: this.t = 1; michael@0: this.s = (x<0)?-1:0; michael@0: if(x > 0) this_array[0] = x; michael@0: else if(x < -1) this_array[0] = x+DV; michael@0: else this.t = 0; michael@0: } michael@0: michael@0: // return bigint initialized to value michael@0: function nbv(i) { var r = nbi(); r.fromInt(i); return r; } michael@0: michael@0: // (protected) set from string and radix michael@0: function bnpFromString(s,b) { michael@0: var this_array = this.array; michael@0: var k; michael@0: if(b == 16) k = 4; michael@0: else if(b == 8) k = 3; michael@0: else if(b == 256) k = 8; // byte array michael@0: else if(b == 2) k = 1; michael@0: else if(b == 32) k = 5; michael@0: else if(b == 4) k = 2; michael@0: else { this.fromRadix(s,b); return; } michael@0: this.t = 0; michael@0: this.s = 0; michael@0: var i = s.length, mi = false, sh = 0; michael@0: while(--i >= 0) { michael@0: var x = (k==8)?s[i]&0xff:intAt(s,i); michael@0: if(x < 0) { michael@0: if(s.charAt(i) == "-") mi = true; michael@0: continue; michael@0: } michael@0: mi = false; michael@0: if(sh == 0) michael@0: this_array[this.t++] = x; michael@0: else if(sh+k > BI_DB) { michael@0: this_array[this.t-1] |= (x&((1<<(BI_DB-sh))-1))<>(BI_DB-sh)); michael@0: } michael@0: else michael@0: this_array[this.t-1] |= x<= BI_DB) sh -= BI_DB; michael@0: } michael@0: if(k == 8 && (s[0]&0x80) != 0) { michael@0: this.s = -1; michael@0: if(sh > 0) this_array[this.t-1] |= ((1<<(BI_DB-sh))-1)< 0 && this_array[this.t-1] == c) --this.t; michael@0: } michael@0: michael@0: // (public) return string representation in given radix michael@0: function bnToString(b) { michael@0: var this_array = this.array; michael@0: if(this.s < 0) return "-"+this.negate().toString(b); michael@0: var k; michael@0: if(b == 16) k = 4; michael@0: else if(b == 8) k = 3; michael@0: else if(b == 2) k = 1; michael@0: else if(b == 32) k = 5; michael@0: else if(b == 4) k = 2; michael@0: else return this.toRadix(b); michael@0: var km = (1< 0) { michael@0: if(p < BI_DB && (d = this_array[i]>>p) > 0) { m = true; r = int2char(d); } michael@0: while(i >= 0) { michael@0: if(p < k) { michael@0: d = (this_array[i]&((1<>(p+=BI_DB-k); michael@0: } michael@0: else { michael@0: d = (this_array[i]>>(p-=k))&km; michael@0: if(p <= 0) { p += BI_DB; --i; } michael@0: } michael@0: if(d > 0) m = true; michael@0: if(m) r += int2char(d); michael@0: } michael@0: } michael@0: return m?r:"0"; michael@0: } michael@0: michael@0: // (public) -this michael@0: function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; } michael@0: michael@0: // (public) |this| michael@0: function bnAbs() { return (this.s<0)?this.negate():this; } michael@0: michael@0: // (public) return + if this > a, - if this < a, 0 if equal michael@0: function bnCompareTo(a) { michael@0: var this_array = this.array; michael@0: var a_array = a.array; michael@0: michael@0: var r = this.s-a.s; michael@0: if(r != 0) return r; michael@0: var i = this.t; michael@0: r = i-a.t; michael@0: if(r != 0) return r; michael@0: while(--i >= 0) if((r=this_array[i]-a_array[i]) != 0) return r; michael@0: return 0; michael@0: } michael@0: michael@0: // returns bit length of the integer x michael@0: function nbits(x) { michael@0: var r = 1, t; michael@0: if((t=x>>>16) != 0) { x = t; r += 16; } michael@0: if((t=x>>8) != 0) { x = t; r += 8; } michael@0: if((t=x>>4) != 0) { x = t; r += 4; } michael@0: if((t=x>>2) != 0) { x = t; r += 2; } michael@0: if((t=x>>1) != 0) { x = t; r += 1; } michael@0: return r; michael@0: } michael@0: michael@0: // (public) return the number of bits in "this" michael@0: function bnBitLength() { michael@0: var this_array = this.array; michael@0: if(this.t <= 0) return 0; michael@0: return BI_DB*(this.t-1)+nbits(this_array[this.t-1]^(this.s&BI_DM)); michael@0: } michael@0: michael@0: // (protected) r = this << n*DB michael@0: function bnpDLShiftTo(n,r) { michael@0: var this_array = this.array; michael@0: var r_array = r.array; michael@0: var i; michael@0: for(i = this.t-1; i >= 0; --i) r_array[i+n] = this_array[i]; michael@0: for(i = n-1; i >= 0; --i) r_array[i] = 0; michael@0: r.t = this.t+n; michael@0: r.s = this.s; michael@0: } michael@0: michael@0: // (protected) r = this >> n*DB michael@0: function bnpDRShiftTo(n,r) { michael@0: var this_array = this.array; michael@0: var r_array = r.array; michael@0: for(var i = n; i < this.t; ++i) r_array[i-n] = this_array[i]; michael@0: r.t = Math.max(this.t-n,0); michael@0: r.s = this.s; michael@0: } michael@0: michael@0: // (protected) r = this << n michael@0: function bnpLShiftTo(n,r) { michael@0: var this_array = this.array; michael@0: var r_array = r.array; michael@0: var bs = n%BI_DB; michael@0: var cbs = BI_DB-bs; michael@0: var bm = (1<= 0; --i) { michael@0: r_array[i+ds+1] = (this_array[i]>>cbs)|c; michael@0: c = (this_array[i]&bm)<= 0; --i) r_array[i] = 0; michael@0: r_array[ds] = c; michael@0: r.t = this.t+ds+1; michael@0: r.s = this.s; michael@0: r.clamp(); michael@0: } michael@0: michael@0: // (protected) r = this >> n michael@0: function bnpRShiftTo(n,r) { michael@0: var this_array = this.array; michael@0: var r_array = r.array; michael@0: r.s = this.s; michael@0: var ds = Math.floor(n/BI_DB); michael@0: if(ds >= this.t) { r.t = 0; return; } michael@0: var bs = n%BI_DB; michael@0: var cbs = BI_DB-bs; michael@0: var bm = (1<>bs; michael@0: for(var i = ds+1; i < this.t; ++i) { michael@0: r_array[i-ds-1] |= (this_array[i]&bm)<>bs; michael@0: } michael@0: if(bs > 0) r_array[this.t-ds-1] |= (this.s&bm)<>= BI_DB; michael@0: } michael@0: if(a.t < this.t) { michael@0: c -= a.s; michael@0: while(i < this.t) { michael@0: c += this_array[i]; michael@0: r_array[i++] = c&BI_DM; michael@0: c >>= BI_DB; michael@0: } michael@0: c += this.s; michael@0: } michael@0: else { michael@0: c += this.s; michael@0: while(i < a.t) { michael@0: c -= a_array[i]; michael@0: r_array[i++] = c&BI_DM; michael@0: c >>= BI_DB; michael@0: } michael@0: c -= a.s; michael@0: } michael@0: r.s = (c<0)?-1:0; michael@0: if(c < -1) r_array[i++] = BI_DV+c; michael@0: else if(c > 0) r_array[i++] = c; michael@0: r.t = i; michael@0: r.clamp(); michael@0: } michael@0: michael@0: // (protected) r = this * a, r != this,a (HAC 14.12) michael@0: // "this" should be the larger one if appropriate. michael@0: function bnpMultiplyTo(a,r) { michael@0: var this_array = this.array; michael@0: var r_array = r.array; michael@0: var x = this.abs(), y = a.abs(); michael@0: var y_array = y.array; michael@0: michael@0: var i = x.t; michael@0: r.t = i+y.t; michael@0: while(--i >= 0) r_array[i] = 0; michael@0: for(i = 0; i < y.t; ++i) r_array[i+x.t] = x.am(0,y_array[i],r,i,0,x.t); michael@0: r.s = 0; michael@0: r.clamp(); michael@0: if(this.s != a.s) BigInteger.ZERO.subTo(r,r); michael@0: } michael@0: michael@0: // (protected) r = this^2, r != this (HAC 14.16) michael@0: function bnpSquareTo(r) { michael@0: var x = this.abs(); michael@0: var x_array = x.array; michael@0: var r_array = r.array; michael@0: michael@0: var i = r.t = 2*x.t; michael@0: while(--i >= 0) r_array[i] = 0; michael@0: for(i = 0; i < x.t-1; ++i) { michael@0: var c = x.am(i,x_array[i],r,2*i,0,1); michael@0: if((r_array[i+x.t]+=x.am(i+1,2*x_array[i],r,2*i+1,c,x.t-i-1)) >= BI_DV) { michael@0: r_array[i+x.t] -= BI_DV; michael@0: r_array[i+x.t+1] = 1; michael@0: } michael@0: } michael@0: if(r.t > 0) r_array[r.t-1] += x.am(i,x_array[i],r,2*i,0,1); michael@0: r.s = 0; michael@0: r.clamp(); michael@0: } michael@0: michael@0: // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20) michael@0: // r != q, this != m. q or r may be null. michael@0: function bnpDivRemTo(m,q,r) { michael@0: var pm = m.abs(); michael@0: if(pm.t <= 0) return; michael@0: var pt = this.abs(); michael@0: if(pt.t < pm.t) { michael@0: if(q != null) q.fromInt(0); michael@0: if(r != null) this.copyTo(r); michael@0: return; michael@0: } michael@0: if(r == null) r = nbi(); michael@0: var y = nbi(), ts = this.s, ms = m.s; michael@0: var pm_array = pm.array; michael@0: var nsh = BI_DB-nbits(pm_array[pm.t-1]); // normalize modulus michael@0: if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); } michael@0: else { pm.copyTo(y); pt.copyTo(r); } michael@0: var ys = y.t; michael@0: michael@0: var y_array = y.array; michael@0: var y0 = y_array[ys-1]; michael@0: if(y0 == 0) return; michael@0: var yt = y0*(1<1)?y_array[ys-2]>>BI_F2:0); michael@0: var d1 = BI_FV/yt, d2 = (1<= 0) { michael@0: r_array[r.t++] = 1; michael@0: r.subTo(t,r); michael@0: } michael@0: BigInteger.ONE.dlShiftTo(ys,t); michael@0: t.subTo(y,y); // "negative" y so we can replace sub with am later michael@0: while(y.t < ys) y_array[y.t++] = 0; michael@0: while(--j >= 0) { michael@0: // Estimate quotient digit michael@0: var qd = (r_array[--i]==y0)?BI_DM:Math.floor(r_array[i]*d1+(r_array[i-1]+e)*d2); michael@0: if((r_array[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out michael@0: y.dlShiftTo(j,t); michael@0: r.subTo(t,r); michael@0: while(r_array[i] < --qd) r.subTo(t,r); michael@0: } michael@0: } michael@0: if(q != null) { michael@0: r.drShiftTo(ys,q); michael@0: if(ts != ms) BigInteger.ZERO.subTo(q,q); michael@0: } michael@0: r.t = ys; michael@0: r.clamp(); michael@0: if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder michael@0: if(ts < 0) BigInteger.ZERO.subTo(r,r); michael@0: } michael@0: michael@0: // (public) this mod a michael@0: function bnMod(a) { michael@0: var r = nbi(); michael@0: this.abs().divRemTo(a,null,r); michael@0: if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r); michael@0: return r; michael@0: } michael@0: michael@0: // Modular reduction using "classic" algorithm michael@0: function Classic(m) { this.m = m; } michael@0: function cConvert(x) { michael@0: if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m); michael@0: else return x; michael@0: } michael@0: function cRevert(x) { return x; } michael@0: function cReduce(x) { x.divRemTo(this.m,null,x); } michael@0: function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } michael@0: function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); } michael@0: michael@0: Classic.prototype.convert = cConvert; michael@0: Classic.prototype.revert = cRevert; michael@0: Classic.prototype.reduce = cReduce; michael@0: Classic.prototype.mulTo = cMulTo; michael@0: Classic.prototype.sqrTo = cSqrTo; michael@0: michael@0: // (protected) return "-1/this % 2^DB"; useful for Mont. reduction michael@0: // justification: michael@0: // xy == 1 (mod m) michael@0: // xy = 1+km michael@0: // xy(2-xy) = (1+km)(1-km) michael@0: // x[y(2-xy)] = 1-k^2m^2 michael@0: // x[y(2-xy)] == 1 (mod m^2) michael@0: // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2 michael@0: // should reduce x and y(2-xy) by m^2 at each step to keep size bounded. michael@0: // JS multiply "overflows" differently from C/C++, so care is needed here. michael@0: function bnpInvDigit() { michael@0: var this_array = this.array; michael@0: if(this.t < 1) return 0; michael@0: var x = this_array[0]; michael@0: if((x&1) == 0) return 0; michael@0: var y = x&3; // y == 1/x mod 2^2 michael@0: y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4 michael@0: y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8 michael@0: y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16 michael@0: // last step - calculate inverse mod DV directly; michael@0: // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints michael@0: y = (y*(2-x*y%BI_DV))%BI_DV; // y == 1/x mod 2^dbits michael@0: // we really want the negative inverse, and -DV < y < DV michael@0: return (y>0)?BI_DV-y:-y; michael@0: } michael@0: michael@0: // Montgomery reduction michael@0: function Montgomery(m) { michael@0: this.m = m; michael@0: this.mp = m.invDigit(); michael@0: this.mpl = this.mp&0x7fff; michael@0: this.mph = this.mp>>15; michael@0: this.um = (1<<(BI_DB-15))-1; michael@0: this.mt2 = 2*m.t; michael@0: } michael@0: michael@0: // xR mod m michael@0: function montConvert(x) { michael@0: var r = nbi(); michael@0: x.abs().dlShiftTo(this.m.t,r); michael@0: r.divRemTo(this.m,null,r); michael@0: if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r); michael@0: return r; michael@0: } michael@0: michael@0: // x/R mod m michael@0: function montRevert(x) { michael@0: var r = nbi(); michael@0: x.copyTo(r); michael@0: this.reduce(r); michael@0: return r; michael@0: } michael@0: michael@0: // x = x/R mod m (HAC 14.32) michael@0: function montReduce(x) { michael@0: var x_array = x.array; michael@0: while(x.t <= this.mt2) // pad x so am has enough room later michael@0: x_array[x.t++] = 0; michael@0: for(var i = 0; i < this.m.t; ++i) { michael@0: // faster way of calculating u0 = x[i]*mp mod DV michael@0: var j = x_array[i]&0x7fff; michael@0: var u0 = (j*this.mpl+(((j*this.mph+(x_array[i]>>15)*this.mpl)&this.um)<<15))&BI_DM; michael@0: // use am to combine the multiply-shift-add into one call michael@0: j = i+this.m.t; michael@0: x_array[j] += this.m.am(0,u0,x,i,0,this.m.t); michael@0: // propagate carry michael@0: while(x_array[j] >= BI_DV) { x_array[j] -= BI_DV; x_array[++j]++; } michael@0: } michael@0: x.clamp(); michael@0: x.drShiftTo(this.m.t,x); michael@0: if(x.compareTo(this.m) >= 0) x.subTo(this.m,x); michael@0: } michael@0: michael@0: // r = "x^2/R mod m"; x != r michael@0: function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); } michael@0: michael@0: // r = "xy/R mod m"; x,y != r michael@0: function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } michael@0: michael@0: Montgomery.prototype.convert = montConvert; michael@0: Montgomery.prototype.revert = montRevert; michael@0: Montgomery.prototype.reduce = montReduce; michael@0: Montgomery.prototype.mulTo = montMulTo; michael@0: Montgomery.prototype.sqrTo = montSqrTo; michael@0: michael@0: // (protected) true iff this is even michael@0: function bnpIsEven() { michael@0: var this_array = this.array; michael@0: return ((this.t>0)?(this_array[0]&1):this.s) == 0; michael@0: } michael@0: michael@0: // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79) michael@0: function bnpExp(e,z) { michael@0: if(e > 0xffffffff || e < 1) return BigInteger.ONE; michael@0: var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1; michael@0: g.copyTo(r); michael@0: while(--i >= 0) { michael@0: z.sqrTo(r,r2); michael@0: if((e&(1< 0) z.mulTo(r2,g,r); michael@0: else { var t = r; r = r2; r2 = t; } michael@0: } michael@0: return z.revert(r); michael@0: } michael@0: michael@0: // (public) this^e % m, 0 <= e < 2^32 michael@0: function bnModPowInt(e,m) { michael@0: var z; michael@0: if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m); michael@0: return this.exp(e,z); michael@0: } michael@0: michael@0: // protected michael@0: BigInteger.prototype.copyTo = bnpCopyTo; michael@0: BigInteger.prototype.fromInt = bnpFromInt; michael@0: BigInteger.prototype.fromString = bnpFromString; michael@0: BigInteger.prototype.clamp = bnpClamp; michael@0: BigInteger.prototype.dlShiftTo = bnpDLShiftTo; michael@0: BigInteger.prototype.drShiftTo = bnpDRShiftTo; michael@0: BigInteger.prototype.lShiftTo = bnpLShiftTo; michael@0: BigInteger.prototype.rShiftTo = bnpRShiftTo; michael@0: BigInteger.prototype.subTo = bnpSubTo; michael@0: BigInteger.prototype.multiplyTo = bnpMultiplyTo; michael@0: BigInteger.prototype.squareTo = bnpSquareTo; michael@0: BigInteger.prototype.divRemTo = bnpDivRemTo; michael@0: BigInteger.prototype.invDigit = bnpInvDigit; michael@0: BigInteger.prototype.isEven = bnpIsEven; michael@0: BigInteger.prototype.exp = bnpExp; michael@0: michael@0: // public michael@0: BigInteger.prototype.toString = bnToString; michael@0: BigInteger.prototype.negate = bnNegate; michael@0: BigInteger.prototype.abs = bnAbs; michael@0: BigInteger.prototype.compareTo = bnCompareTo; michael@0: BigInteger.prototype.bitLength = bnBitLength; michael@0: BigInteger.prototype.mod = bnMod; michael@0: BigInteger.prototype.modPowInt = bnModPowInt; michael@0: michael@0: // "constants" michael@0: BigInteger.ZERO = nbv(0); michael@0: BigInteger.ONE = nbv(1); michael@0: // Copyright (c) 2005 Tom Wu michael@0: // All Rights Reserved. michael@0: // See "LICENSE" for details. michael@0: michael@0: // Extended JavaScript BN functions, required for RSA private ops. michael@0: michael@0: // (public) michael@0: function bnClone() { var r = nbi(); this.copyTo(r); return r; } michael@0: michael@0: // (public) return value as integer michael@0: function bnIntValue() { michael@0: var this_array = this.array; michael@0: if(this.s < 0) { michael@0: if(this.t == 1) return this_array[0]-BI_DV; michael@0: else if(this.t == 0) return -1; michael@0: } michael@0: else if(this.t == 1) return this_array[0]; michael@0: else if(this.t == 0) return 0; michael@0: // assumes 16 < DB < 32 michael@0: return ((this_array[1]&((1<<(32-BI_DB))-1))<>24; michael@0: } michael@0: michael@0: // (public) return value as short (assumes DB>=16) michael@0: function bnShortValue() { michael@0: var this_array = this.array; michael@0: return (this.t==0)?this.s:(this_array[0]<<16)>>16; michael@0: } michael@0: michael@0: // (protected) return x s.t. r^x < DV michael@0: function bnpChunkSize(r) { return Math.floor(Math.LN2*BI_DB/Math.log(r)); } michael@0: michael@0: // (public) 0 if this == 0, 1 if this > 0 michael@0: function bnSigNum() { michael@0: var this_array = this.array; michael@0: if(this.s < 0) return -1; michael@0: else if(this.t <= 0 || (this.t == 1 && this_array[0] <= 0)) return 0; michael@0: else return 1; michael@0: } michael@0: michael@0: // (protected) convert to radix string michael@0: function bnpToRadix(b) { michael@0: if(b == null) b = 10; michael@0: if(this.signum() == 0 || b < 2 || b > 36) return "0"; michael@0: var cs = this.chunkSize(b); michael@0: var a = Math.pow(b,cs); michael@0: var d = nbv(a), y = nbi(), z = nbi(), r = ""; michael@0: this.divRemTo(d,y,z); michael@0: while(y.signum() > 0) { michael@0: r = (a+z.intValue()).toString(b).substr(1) + r; michael@0: y.divRemTo(d,y,z); michael@0: } michael@0: return z.intValue().toString(b) + r; michael@0: } michael@0: michael@0: // (protected) convert from radix string michael@0: function bnpFromRadix(s,b) { michael@0: this.fromInt(0); michael@0: if(b == null) b = 10; michael@0: var cs = this.chunkSize(b); michael@0: var d = Math.pow(b,cs), mi = false, j = 0, w = 0; michael@0: for(var i = 0; i < s.length; ++i) { michael@0: var x = intAt(s,i); michael@0: if(x < 0) { michael@0: if(s.charAt(i) == "-" && this.signum() == 0) mi = true; michael@0: continue; michael@0: } michael@0: w = b*w+x; michael@0: if(++j >= cs) { michael@0: this.dMultiply(d); michael@0: this.dAddOffset(w,0); michael@0: j = 0; michael@0: w = 0; michael@0: } michael@0: } michael@0: if(j > 0) { michael@0: this.dMultiply(Math.pow(b,j)); michael@0: this.dAddOffset(w,0); michael@0: } michael@0: if(mi) BigInteger.ZERO.subTo(this,this); michael@0: } michael@0: michael@0: // (protected) alternate constructor michael@0: function bnpFromNumber(a,b,c) { michael@0: if("number" == typeof b) { michael@0: // new BigInteger(int,int,RNG) michael@0: if(a < 2) this.fromInt(1); michael@0: else { michael@0: this.fromNumber(a,c); michael@0: if(!this.testBit(a-1)) // force MSB set michael@0: this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this); michael@0: if(this.isEven()) this.dAddOffset(1,0); // force odd michael@0: while(!this.isProbablePrime(b)) { michael@0: this.dAddOffset(2,0); michael@0: if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this); michael@0: } michael@0: } michael@0: } michael@0: else { michael@0: // new BigInteger(int,RNG) michael@0: var x = new Array(), t = a&7; michael@0: x.length = (a>>3)+1; michael@0: b.nextBytes(x); michael@0: if(t > 0) x[0] &= ((1< 0) { michael@0: if(p < BI_DB && (d = this_array[i]>>p) != (this.s&BI_DM)>>p) michael@0: r[k++] = d|(this.s<<(BI_DB-p)); michael@0: while(i >= 0) { michael@0: if(p < 8) { michael@0: d = (this_array[i]&((1<>(p+=BI_DB-8); michael@0: } michael@0: else { michael@0: d = (this_array[i]>>(p-=8))&0xff; michael@0: if(p <= 0) { p += BI_DB; --i; } michael@0: } michael@0: if((d&0x80) != 0) d |= -256; michael@0: if(k == 0 && (this.s&0x80) != (d&0x80)) ++k; michael@0: if(k > 0 || d != this.s) r[k++] = d; michael@0: } michael@0: } michael@0: return r; michael@0: } michael@0: michael@0: function bnEquals(a) { return(this.compareTo(a)==0); } michael@0: function bnMin(a) { return(this.compareTo(a)<0)?this:a; } michael@0: function bnMax(a) { return(this.compareTo(a)>0)?this:a; } michael@0: michael@0: // (protected) r = this op a (bitwise) michael@0: function bnpBitwiseTo(a,op,r) { michael@0: var this_array = this.array; michael@0: var a_array = a.array; michael@0: var r_array = r.array; michael@0: var i, f, m = Math.min(a.t,this.t); michael@0: for(i = 0; i < m; ++i) r_array[i] = op(this_array[i],a_array[i]); michael@0: if(a.t < this.t) { michael@0: f = a.s&BI_DM; michael@0: for(i = m; i < this.t; ++i) r_array[i] = op(this_array[i],f); michael@0: r.t = this.t; michael@0: } michael@0: else { michael@0: f = this.s&BI_DM; michael@0: for(i = m; i < a.t; ++i) r_array[i] = op(f,a_array[i]); michael@0: r.t = a.t; michael@0: } michael@0: r.s = op(this.s,a.s); michael@0: r.clamp(); michael@0: } michael@0: michael@0: // (public) this & a michael@0: function op_and(x,y) { return x&y; } michael@0: function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; } michael@0: michael@0: // (public) this | a michael@0: function op_or(x,y) { return x|y; } michael@0: function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; } michael@0: michael@0: // (public) this ^ a michael@0: function op_xor(x,y) { return x^y; } michael@0: function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; } michael@0: michael@0: // (public) this & ~a michael@0: function op_andnot(x,y) { return x&~y; } michael@0: function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; } michael@0: michael@0: // (public) ~this michael@0: function bnNot() { michael@0: var this_array = this.array; michael@0: var r = nbi(); michael@0: var r_array = r.array; michael@0: michael@0: for(var i = 0; i < this.t; ++i) r_array[i] = BI_DM&~this_array[i]; michael@0: r.t = this.t; michael@0: r.s = ~this.s; michael@0: return r; michael@0: } michael@0: michael@0: // (public) this << n michael@0: function bnShiftLeft(n) { michael@0: var r = nbi(); michael@0: if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r); michael@0: return r; michael@0: } michael@0: michael@0: // (public) this >> n michael@0: function bnShiftRight(n) { michael@0: var r = nbi(); michael@0: if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r); michael@0: return r; michael@0: } michael@0: michael@0: // return index of lowest 1-bit in x, x < 2^31 michael@0: function lbit(x) { michael@0: if(x == 0) return -1; michael@0: var r = 0; michael@0: if((x&0xffff) == 0) { x >>= 16; r += 16; } michael@0: if((x&0xff) == 0) { x >>= 8; r += 8; } michael@0: if((x&0xf) == 0) { x >>= 4; r += 4; } michael@0: if((x&3) == 0) { x >>= 2; r += 2; } michael@0: if((x&1) == 0) ++r; michael@0: return r; michael@0: } michael@0: michael@0: // (public) returns index of lowest 1-bit (or -1 if none) michael@0: function bnGetLowestSetBit() { michael@0: var this_array = this.array; michael@0: for(var i = 0; i < this.t; ++i) michael@0: if(this_array[i] != 0) return i*BI_DB+lbit(this_array[i]); michael@0: if(this.s < 0) return this.t*BI_DB; michael@0: return -1; michael@0: } michael@0: michael@0: // return number of 1 bits in x michael@0: function cbit(x) { michael@0: var r = 0; michael@0: while(x != 0) { x &= x-1; ++r; } michael@0: return r; michael@0: } michael@0: michael@0: // (public) return number of set bits michael@0: function bnBitCount() { michael@0: var r = 0, x = this.s&BI_DM; michael@0: for(var i = 0; i < this.t; ++i) r += cbit(this_array[i]^x); michael@0: return r; michael@0: } michael@0: michael@0: // (public) true iff nth bit is set michael@0: function bnTestBit(n) { michael@0: var this_array = this.array; michael@0: var j = Math.floor(n/BI_DB); michael@0: if(j >= this.t) return(this.s!=0); michael@0: return((this_array[j]&(1<<(n%BI_DB)))!=0); michael@0: } michael@0: michael@0: // (protected) this op (1<>= BI_DB; michael@0: } michael@0: if(a.t < this.t) { michael@0: c += a.s; michael@0: while(i < this.t) { michael@0: c += this_array[i]; michael@0: r_array[i++] = c&BI_DM; michael@0: c >>= BI_DB; michael@0: } michael@0: c += this.s; michael@0: } michael@0: else { michael@0: c += this.s; michael@0: while(i < a.t) { michael@0: c += a_array[i]; michael@0: r_array[i++] = c&BI_DM; michael@0: c >>= BI_DB; michael@0: } michael@0: c += a.s; michael@0: } michael@0: r.s = (c<0)?-1:0; michael@0: if(c > 0) r_array[i++] = c; michael@0: else if(c < -1) r_array[i++] = BI_DV+c; michael@0: r.t = i; michael@0: r.clamp(); michael@0: } michael@0: michael@0: // (public) this + a michael@0: function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; } michael@0: michael@0: // (public) this - a michael@0: function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; } michael@0: michael@0: // (public) this * a michael@0: function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; } michael@0: michael@0: // (public) this / a michael@0: function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; } michael@0: michael@0: // (public) this % a michael@0: function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; } michael@0: michael@0: // (public) [this/a,this%a] michael@0: function bnDivideAndRemainder(a) { michael@0: var q = nbi(), r = nbi(); michael@0: this.divRemTo(a,q,r); michael@0: return new Array(q,r); michael@0: } michael@0: michael@0: // (protected) this *= n, this >= 0, 1 < n < DV michael@0: function bnpDMultiply(n) { michael@0: var this_array = this.array; michael@0: this_array[this.t] = this.am(0,n-1,this,0,0,this.t); michael@0: ++this.t; michael@0: this.clamp(); michael@0: } michael@0: michael@0: // (protected) this += n << w words, this >= 0 michael@0: function bnpDAddOffset(n,w) { michael@0: var this_array = this.array; michael@0: while(this.t <= w) this_array[this.t++] = 0; michael@0: this_array[w] += n; michael@0: while(this_array[w] >= BI_DV) { michael@0: this_array[w] -= BI_DV; michael@0: if(++w >= this.t) this_array[this.t++] = 0; michael@0: ++this_array[w]; michael@0: } michael@0: } michael@0: michael@0: // A "null" reducer michael@0: function NullExp() {} michael@0: function nNop(x) { return x; } michael@0: function nMulTo(x,y,r) { x.multiplyTo(y,r); } michael@0: function nSqrTo(x,r) { x.squareTo(r); } michael@0: michael@0: NullExp.prototype.convert = nNop; michael@0: NullExp.prototype.revert = nNop; michael@0: NullExp.prototype.mulTo = nMulTo; michael@0: NullExp.prototype.sqrTo = nSqrTo; michael@0: michael@0: // (public) this^e michael@0: function bnPow(e) { return this.exp(e,new NullExp()); } michael@0: michael@0: // (protected) r = lower n words of "this * a", a.t <= n michael@0: // "this" should be the larger one if appropriate. michael@0: function bnpMultiplyLowerTo(a,n,r) { michael@0: var r_array = r.array; michael@0: var a_array = a.array; michael@0: var i = Math.min(this.t+a.t,n); michael@0: r.s = 0; // assumes a,this >= 0 michael@0: r.t = i; michael@0: while(i > 0) r_array[--i] = 0; michael@0: var j; michael@0: for(j = r.t-this.t; i < j; ++i) r_array[i+this.t] = this.am(0,a_array[i],r,i,0,this.t); michael@0: for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a_array[i],r,i,0,n-i); michael@0: r.clamp(); michael@0: } michael@0: michael@0: // (protected) r = "this * a" without lower n words, n > 0 michael@0: // "this" should be the larger one if appropriate. michael@0: function bnpMultiplyUpperTo(a,n,r) { michael@0: var r_array = r.array; michael@0: var a_array = a.array; michael@0: --n; michael@0: var i = r.t = this.t+a.t-n; michael@0: r.s = 0; // assumes a,this >= 0 michael@0: while(--i >= 0) r_array[i] = 0; michael@0: for(i = Math.max(n-this.t,0); i < a.t; ++i) michael@0: r_array[this.t+i-n] = this.am(n-i,a_array[i],r,0,0,this.t+i-n); michael@0: r.clamp(); michael@0: r.drShiftTo(1,r); michael@0: } michael@0: michael@0: // Barrett modular reduction michael@0: function Barrett(m) { michael@0: // setup Barrett michael@0: this.r2 = nbi(); michael@0: this.q3 = nbi(); michael@0: BigInteger.ONE.dlShiftTo(2*m.t,this.r2); michael@0: this.mu = this.r2.divide(m); michael@0: this.m = m; michael@0: } michael@0: michael@0: function barrettConvert(x) { michael@0: if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m); michael@0: else if(x.compareTo(this.m) < 0) return x; michael@0: else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; } michael@0: } michael@0: michael@0: function barrettRevert(x) { return x; } michael@0: michael@0: // x = x mod m (HAC 14.42) michael@0: function barrettReduce(x) { michael@0: x.drShiftTo(this.m.t-1,this.r2); michael@0: if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); } michael@0: this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3); michael@0: this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2); michael@0: while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1); michael@0: x.subTo(this.r2,x); michael@0: while(x.compareTo(this.m) >= 0) x.subTo(this.m,x); michael@0: } michael@0: michael@0: // r = x^2 mod m; x != r michael@0: function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); } michael@0: michael@0: // r = x*y mod m; x,y != r michael@0: function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } michael@0: michael@0: Barrett.prototype.convert = barrettConvert; michael@0: Barrett.prototype.revert = barrettRevert; michael@0: Barrett.prototype.reduce = barrettReduce; michael@0: Barrett.prototype.mulTo = barrettMulTo; michael@0: Barrett.prototype.sqrTo = barrettSqrTo; michael@0: michael@0: // (public) this^e % m (HAC 14.85) michael@0: function bnModPow(e,m) { michael@0: var e_array = e.array; michael@0: var i = e.bitLength(), k, r = nbv(1), z; michael@0: if(i <= 0) return r; michael@0: else if(i < 18) k = 1; michael@0: else if(i < 48) k = 3; michael@0: else if(i < 144) k = 4; michael@0: else if(i < 768) k = 5; michael@0: else k = 6; michael@0: if(i < 8) michael@0: z = new Classic(m); michael@0: else if(m.isEven()) michael@0: z = new Barrett(m); michael@0: else michael@0: z = new Montgomery(m); michael@0: michael@0: // precomputation michael@0: var g = new Array(), n = 3, k1 = k-1, km = (1< 1) { michael@0: var g2 = nbi(); michael@0: z.sqrTo(g[1],g2); michael@0: while(n <= km) { michael@0: g[n] = nbi(); michael@0: z.mulTo(g2,g[n-2],g[n]); michael@0: n += 2; michael@0: } michael@0: } michael@0: michael@0: var j = e.t-1, w, is1 = true, r2 = nbi(), t; michael@0: i = nbits(e_array[j])-1; michael@0: while(j >= 0) { michael@0: if(i >= k1) w = (e_array[j]>>(i-k1))&km; michael@0: else { michael@0: w = (e_array[j]&((1<<(i+1))-1))<<(k1-i); michael@0: if(j > 0) w |= e_array[j-1]>>(BI_DB+i-k1); michael@0: } michael@0: michael@0: n = k; michael@0: while((w&1) == 0) { w >>= 1; --n; } michael@0: if((i -= n) < 0) { i += BI_DB; --j; } michael@0: if(is1) { // ret == 1, don't bother squaring or multiplying it michael@0: g[w].copyTo(r); michael@0: is1 = false; michael@0: } michael@0: else { michael@0: while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; } michael@0: if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; } michael@0: z.mulTo(r2,g[w],r); michael@0: } michael@0: michael@0: while(j >= 0 && (e_array[j]&(1< 0) { michael@0: x.rShiftTo(g,x); michael@0: y.rShiftTo(g,y); michael@0: } michael@0: while(x.signum() > 0) { michael@0: if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x); michael@0: if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y); michael@0: if(x.compareTo(y) >= 0) { michael@0: x.subTo(y,x); michael@0: x.rShiftTo(1,x); michael@0: } michael@0: else { michael@0: y.subTo(x,y); michael@0: y.rShiftTo(1,y); michael@0: } michael@0: } michael@0: if(g > 0) y.lShiftTo(g,y); michael@0: return y; michael@0: } michael@0: michael@0: // (protected) this % n, n < 2^26 michael@0: function bnpModInt(n) { michael@0: var this_array = this.array; michael@0: if(n <= 0) return 0; michael@0: var d = BI_DV%n, r = (this.s<0)?n-1:0; michael@0: if(this.t > 0) michael@0: if(d == 0) r = this_array[0]%n; michael@0: else for(var i = this.t-1; i >= 0; --i) r = (d*r+this_array[i])%n; michael@0: return r; michael@0: } michael@0: michael@0: // (public) 1/this % m (HAC 14.61) michael@0: function bnModInverse(m) { michael@0: var ac = m.isEven(); michael@0: if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO; michael@0: var u = m.clone(), v = this.clone(); michael@0: var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1); michael@0: while(u.signum() != 0) { michael@0: while(u.isEven()) { michael@0: u.rShiftTo(1,u); michael@0: if(ac) { michael@0: if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); } michael@0: a.rShiftTo(1,a); michael@0: } michael@0: else if(!b.isEven()) b.subTo(m,b); michael@0: b.rShiftTo(1,b); michael@0: } michael@0: while(v.isEven()) { michael@0: v.rShiftTo(1,v); michael@0: if(ac) { michael@0: if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); } michael@0: c.rShiftTo(1,c); michael@0: } michael@0: else if(!d.isEven()) d.subTo(m,d); michael@0: d.rShiftTo(1,d); michael@0: } michael@0: if(u.compareTo(v) >= 0) { michael@0: u.subTo(v,u); michael@0: if(ac) a.subTo(c,a); michael@0: b.subTo(d,b); michael@0: } michael@0: else { michael@0: v.subTo(u,v); michael@0: if(ac) c.subTo(a,c); michael@0: d.subTo(b,d); michael@0: } michael@0: } michael@0: if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO; michael@0: if(d.compareTo(m) >= 0) return d.subtract(m); michael@0: if(d.signum() < 0) d.addTo(m,d); else return d; michael@0: if(d.signum() < 0) return d.add(m); else return d; michael@0: } michael@0: michael@0: var lowprimes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509]; michael@0: var lplim = (1<<26)/lowprimes[lowprimes.length-1]; michael@0: michael@0: // (public) test primality with certainty >= 1-.5^t michael@0: function bnIsProbablePrime(t) { michael@0: var i, x = this.abs(); michael@0: var x_array = x.array; michael@0: if(x.t == 1 && x_array[0] <= lowprimes[lowprimes.length-1]) { michael@0: for(i = 0; i < lowprimes.length; ++i) michael@0: if(x_array[0] == lowprimes[i]) return true; michael@0: return false; michael@0: } michael@0: if(x.isEven()) return false; michael@0: i = 1; michael@0: while(i < lowprimes.length) { michael@0: var m = lowprimes[i], j = i+1; michael@0: while(j < lowprimes.length && m < lplim) m *= lowprimes[j++]; michael@0: m = x.modInt(m); michael@0: while(i < j) if(m%lowprimes[i++] == 0) return false; michael@0: } michael@0: return x.millerRabin(t); michael@0: } michael@0: michael@0: // (protected) true if probably prime (HAC 4.24, Miller-Rabin) michael@0: function bnpMillerRabin(t) { michael@0: var n1 = this.subtract(BigInteger.ONE); michael@0: var k = n1.getLowestSetBit(); michael@0: if(k <= 0) return false; michael@0: var r = n1.shiftRight(k); michael@0: t = (t+1)>>1; michael@0: if(t > lowprimes.length) t = lowprimes.length; michael@0: var a = nbi(); michael@0: for(var i = 0; i < t; ++i) { michael@0: a.fromInt(lowprimes[i]); michael@0: var y = a.modPow(r,this); michael@0: if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) { michael@0: var j = 1; michael@0: while(j++ < k && y.compareTo(n1) != 0) { michael@0: y = y.modPowInt(2,this); michael@0: if(y.compareTo(BigInteger.ONE) == 0) return false; michael@0: } michael@0: if(y.compareTo(n1) != 0) return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: // protected michael@0: BigInteger.prototype.chunkSize = bnpChunkSize; michael@0: BigInteger.prototype.toRadix = bnpToRadix; michael@0: BigInteger.prototype.fromRadix = bnpFromRadix; michael@0: BigInteger.prototype.fromNumber = bnpFromNumber; michael@0: BigInteger.prototype.bitwiseTo = bnpBitwiseTo; michael@0: BigInteger.prototype.changeBit = bnpChangeBit; michael@0: BigInteger.prototype.addTo = bnpAddTo; michael@0: BigInteger.prototype.dMultiply = bnpDMultiply; michael@0: BigInteger.prototype.dAddOffset = bnpDAddOffset; michael@0: BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo; michael@0: BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo; michael@0: BigInteger.prototype.modInt = bnpModInt; michael@0: BigInteger.prototype.millerRabin = bnpMillerRabin; michael@0: michael@0: // public michael@0: BigInteger.prototype.clone = bnClone; michael@0: BigInteger.prototype.intValue = bnIntValue; michael@0: BigInteger.prototype.byteValue = bnByteValue; michael@0: BigInteger.prototype.shortValue = bnShortValue; michael@0: BigInteger.prototype.signum = bnSigNum; michael@0: BigInteger.prototype.toByteArray = bnToByteArray; michael@0: BigInteger.prototype.equals = bnEquals; michael@0: BigInteger.prototype.min = bnMin; michael@0: BigInteger.prototype.max = bnMax; michael@0: BigInteger.prototype.and = bnAnd; michael@0: BigInteger.prototype.or = bnOr; michael@0: BigInteger.prototype.xor = bnXor; michael@0: BigInteger.prototype.andNot = bnAndNot; michael@0: BigInteger.prototype.not = bnNot; michael@0: BigInteger.prototype.shiftLeft = bnShiftLeft; michael@0: BigInteger.prototype.shiftRight = bnShiftRight; michael@0: BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit; michael@0: BigInteger.prototype.bitCount = bnBitCount; michael@0: BigInteger.prototype.testBit = bnTestBit; michael@0: BigInteger.prototype.setBit = bnSetBit; michael@0: BigInteger.prototype.clearBit = bnClearBit; michael@0: BigInteger.prototype.flipBit = bnFlipBit; michael@0: BigInteger.prototype.add = bnAdd; michael@0: BigInteger.prototype.subtract = bnSubtract; michael@0: BigInteger.prototype.multiply = bnMultiply; michael@0: BigInteger.prototype.divide = bnDivide; michael@0: BigInteger.prototype.remainder = bnRemainder; michael@0: BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder; michael@0: BigInteger.prototype.modPow = bnModPow; michael@0: BigInteger.prototype.modInverse = bnModInverse; michael@0: BigInteger.prototype.pow = bnPow; michael@0: BigInteger.prototype.gcd = bnGCD; michael@0: BigInteger.prototype.isProbablePrime = bnIsProbablePrime; michael@0: michael@0: // BigInteger interfaces not implemented in jsbn: michael@0: michael@0: // BigInteger(int signum, byte[] magnitude) michael@0: // double doubleValue() michael@0: // float floatValue() michael@0: // int hashCode() michael@0: // long longValue() michael@0: // static BigInteger valueOf(long val) michael@0: // prng4.js - uses Arcfour as a PRNG michael@0: michael@0: function Arcfour() { michael@0: this.i = 0; michael@0: this.j = 0; michael@0: this.S = new Array(); michael@0: } michael@0: michael@0: // Initialize arcfour context from key, an array of ints, each from [0..255] michael@0: function ARC4init(key) { michael@0: var i, j, t; michael@0: for(i = 0; i < 256; ++i) michael@0: this.S[i] = i; michael@0: j = 0; michael@0: for(i = 0; i < 256; ++i) { michael@0: j = (j + this.S[i] + key[i % key.length]) & 255; michael@0: t = this.S[i]; michael@0: this.S[i] = this.S[j]; michael@0: this.S[j] = t; michael@0: } michael@0: this.i = 0; michael@0: this.j = 0; michael@0: } michael@0: michael@0: function ARC4next() { michael@0: var t; michael@0: this.i = (this.i + 1) & 255; michael@0: this.j = (this.j + this.S[this.i]) & 255; michael@0: t = this.S[this.i]; michael@0: this.S[this.i] = this.S[this.j]; michael@0: this.S[this.j] = t; michael@0: return this.S[(t + this.S[this.i]) & 255]; michael@0: } michael@0: michael@0: Arcfour.prototype.init = ARC4init; michael@0: Arcfour.prototype.next = ARC4next; michael@0: michael@0: // Plug in your RNG constructor here michael@0: function prng_newstate() { michael@0: return new Arcfour(); michael@0: } michael@0: michael@0: // Pool size must be a multiple of 4 and greater than 32. michael@0: // An array of bytes the size of the pool will be passed to init() michael@0: var rng_psize = 256; michael@0: // Random number generator - requires a PRNG backend, e.g. prng4.js michael@0: michael@0: // For best results, put code like michael@0: // michael@0: // in your main HTML document. michael@0: michael@0: var rng_state; michael@0: var rng_pool; michael@0: var rng_pptr; michael@0: michael@0: // Mix in a 32-bit integer into the pool michael@0: function rng_seed_int(x) { michael@0: rng_pool[rng_pptr++] ^= x & 255; michael@0: rng_pool[rng_pptr++] ^= (x >> 8) & 255; michael@0: rng_pool[rng_pptr++] ^= (x >> 16) & 255; michael@0: rng_pool[rng_pptr++] ^= (x >> 24) & 255; michael@0: if(rng_pptr >= rng_psize) rng_pptr -= rng_psize; michael@0: } michael@0: michael@0: // Mix in the current time (w/milliseconds) into the pool michael@0: function rng_seed_time() { michael@0: // Use pre-computed date to avoid making the benchmark michael@0: // results dependent on the current date. michael@0: rng_seed_int(1122926989487); michael@0: } michael@0: michael@0: // Initialize the pool with junk if needed. michael@0: if(rng_pool == null) { michael@0: rng_pool = new Array(); michael@0: rng_pptr = 0; michael@0: var t; michael@0: while(rng_pptr < rng_psize) { // extract some randomness from Math.random() michael@0: t = Math.floor(65536 * Math.random()); michael@0: rng_pool[rng_pptr++] = t >>> 8; michael@0: rng_pool[rng_pptr++] = t & 255; michael@0: } michael@0: rng_pptr = 0; michael@0: rng_seed_time(); michael@0: //rng_seed_int(window.screenX); michael@0: //rng_seed_int(window.screenY); michael@0: } michael@0: michael@0: function rng_get_byte() { michael@0: if(rng_state == null) { michael@0: rng_seed_time(); michael@0: rng_state = prng_newstate(); michael@0: rng_state.init(rng_pool); michael@0: for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr) michael@0: rng_pool[rng_pptr] = 0; michael@0: rng_pptr = 0; michael@0: //rng_pool = null; michael@0: } michael@0: // TODO: allow reseeding after first request michael@0: return rng_state.next(); michael@0: } michael@0: michael@0: function rng_get_bytes(ba) { michael@0: var i; michael@0: for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte(); michael@0: } michael@0: michael@0: function SecureRandom() {} michael@0: michael@0: SecureRandom.prototype.nextBytes = rng_get_bytes; michael@0: // Depends on jsbn.js and rng.js michael@0: michael@0: // convert a (hex) string to a bignum object michael@0: function parseBigInt(str,r) { michael@0: return new BigInteger(str,r); michael@0: } michael@0: michael@0: function linebrk(s,n) { michael@0: var ret = ""; michael@0: var i = 0; michael@0: while(i + n < s.length) { michael@0: ret += s.substring(i,i+n) + "\n"; michael@0: i += n; michael@0: } michael@0: return ret + s.substring(i,s.length); michael@0: } michael@0: michael@0: function byte2Hex(b) { michael@0: if(b < 0x10) michael@0: return "0" + b.toString(16); michael@0: else michael@0: return b.toString(16); michael@0: } michael@0: michael@0: // PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint michael@0: function pkcs1pad2(s,n) { michael@0: if(n < s.length + 11) { michael@0: alert("Message too long for RSA"); michael@0: return null; michael@0: } michael@0: var ba = new Array(); michael@0: var i = s.length - 1; michael@0: while(i >= 0 && n > 0) ba[--n] = s.charCodeAt(i--); michael@0: ba[--n] = 0; michael@0: var rng = new SecureRandom(); michael@0: var x = new Array(); michael@0: while(n > 2) { // random non-zero pad michael@0: x[0] = 0; michael@0: while(x[0] == 0) rng.nextBytes(x); michael@0: ba[--n] = x[0]; michael@0: } michael@0: ba[--n] = 2; michael@0: ba[--n] = 0; michael@0: return new BigInteger(ba); michael@0: } michael@0: michael@0: // "empty" RSA key constructor michael@0: function RSAKey() { michael@0: this.n = null; michael@0: this.e = 0; michael@0: this.d = null; michael@0: this.p = null; michael@0: this.q = null; michael@0: this.dmp1 = null; michael@0: this.dmq1 = null; michael@0: this.coeff = null; michael@0: } michael@0: michael@0: // Set the public key fields N and e from hex strings michael@0: function RSASetPublic(N,E) { michael@0: if(N != null && E != null && N.length > 0 && E.length > 0) { michael@0: this.n = parseBigInt(N,16); michael@0: this.e = parseInt(E,16); michael@0: } michael@0: else michael@0: alert("Invalid RSA public key"); michael@0: } michael@0: michael@0: // Perform raw public operation on "x": return x^e (mod n) michael@0: function RSADoPublic(x) { michael@0: return x.modPowInt(this.e, this.n); michael@0: } michael@0: michael@0: // Return the PKCS#1 RSA encryption of "text" as an even-length hex string michael@0: function RSAEncrypt(text) { michael@0: var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3); michael@0: if(m == null) return null; michael@0: var c = this.doPublic(m); michael@0: if(c == null) return null; michael@0: var h = c.toString(16); michael@0: if((h.length & 1) == 0) return h; else return "0" + h; michael@0: } michael@0: michael@0: // Return the PKCS#1 RSA encryption of "text" as a Base64-encoded string michael@0: //function RSAEncryptB64(text) { michael@0: // var h = this.encrypt(text); michael@0: // if(h) return hex2b64(h); else return null; michael@0: //} michael@0: michael@0: // protected michael@0: RSAKey.prototype.doPublic = RSADoPublic; michael@0: michael@0: // public michael@0: RSAKey.prototype.setPublic = RSASetPublic; michael@0: RSAKey.prototype.encrypt = RSAEncrypt; michael@0: //RSAKey.prototype.encrypt_b64 = RSAEncryptB64; michael@0: // Depends on rsa.js and jsbn2.js michael@0: michael@0: // Undo PKCS#1 (type 2, random) padding and, if valid, return the plaintext michael@0: function pkcs1unpad2(d,n) { michael@0: var b = d.toByteArray(); michael@0: var i = 0; michael@0: while(i < b.length && b[i] == 0) ++i; michael@0: if(b.length-i != n-1 || b[i] != 2) michael@0: return null; michael@0: ++i; michael@0: while(b[i] != 0) michael@0: if(++i >= b.length) return null; michael@0: var ret = ""; michael@0: while(++i < b.length) michael@0: ret += String.fromCharCode(b[i]); michael@0: return ret; michael@0: } michael@0: michael@0: // Set the private key fields N, e, and d from hex strings michael@0: function RSASetPrivate(N,E,D) { michael@0: if(N != null && E != null && N.length > 0 && E.length > 0) { michael@0: this.n = parseBigInt(N,16); michael@0: this.e = parseInt(E,16); michael@0: this.d = parseBigInt(D,16); michael@0: } michael@0: else michael@0: alert("Invalid RSA private key"); michael@0: } michael@0: michael@0: // Set the private key fields N, e, d and CRT params from hex strings michael@0: function RSASetPrivateEx(N,E,D,P,Q,DP,DQ,C) { michael@0: if(N != null && E != null && N.length > 0 && E.length > 0) { michael@0: this.n = parseBigInt(N,16); michael@0: this.e = parseInt(E,16); michael@0: this.d = parseBigInt(D,16); michael@0: this.p = parseBigInt(P,16); michael@0: this.q = parseBigInt(Q,16); michael@0: this.dmp1 = parseBigInt(DP,16); michael@0: this.dmq1 = parseBigInt(DQ,16); michael@0: this.coeff = parseBigInt(C,16); michael@0: } michael@0: else michael@0: alert("Invalid RSA private key"); michael@0: } michael@0: michael@0: // Generate a new random private key B bits long, using public expt E michael@0: function RSAGenerate(B,E) { michael@0: var rng = new SecureRandom(); michael@0: var qs = B>>1; michael@0: this.e = parseInt(E,16); michael@0: var ee = new BigInteger(E,16); michael@0: for(;;) { michael@0: for(;;) { michael@0: this.p = new BigInteger(B-qs,1,rng); michael@0: if(this.p.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.p.isProbablePrime(10)) break; michael@0: } michael@0: for(;;) { michael@0: this.q = new BigInteger(qs,1,rng); michael@0: if(this.q.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.q.isProbablePrime(10)) break; michael@0: } michael@0: if(this.p.compareTo(this.q) <= 0) { michael@0: var t = this.p; michael@0: this.p = this.q; michael@0: this.q = t; michael@0: } michael@0: var p1 = this.p.subtract(BigInteger.ONE); michael@0: var q1 = this.q.subtract(BigInteger.ONE); michael@0: var phi = p1.multiply(q1); michael@0: if(phi.gcd(ee).compareTo(BigInteger.ONE) == 0) { michael@0: this.n = this.p.multiply(this.q); michael@0: this.d = ee.modInverse(phi); michael@0: this.dmp1 = this.d.mod(p1); michael@0: this.dmq1 = this.d.mod(q1); michael@0: this.coeff = this.q.modInverse(this.p); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Perform raw private operation on "x": return x^d (mod n) michael@0: function RSADoPrivate(x) { michael@0: if(this.p == null || this.q == null) michael@0: return x.modPow(this.d, this.n); michael@0: michael@0: // TODO: re-calculate any missing CRT params michael@0: var xp = x.mod(this.p).modPow(this.dmp1, this.p); michael@0: var xq = x.mod(this.q).modPow(this.dmq1, this.q); michael@0: michael@0: while(xp.compareTo(xq) < 0) michael@0: xp = xp.add(this.p); michael@0: return xp.subtract(xq).multiply(this.coeff).mod(this.p).multiply(this.q).add(xq); michael@0: } michael@0: michael@0: // Return the PKCS#1 RSA decryption of "ctext". michael@0: // "ctext" is an even-length hex string and the output is a plain string. michael@0: function RSADecrypt(ctext) { michael@0: var c = parseBigInt(ctext, 16); michael@0: var m = this.doPrivate(c); michael@0: if(m == null) return null; michael@0: return pkcs1unpad2(m, (this.n.bitLength()+7)>>3); michael@0: } michael@0: michael@0: // Return the PKCS#1 RSA decryption of "ctext". michael@0: // "ctext" is a Base64-encoded string and the output is a plain string. michael@0: //function RSAB64Decrypt(ctext) { michael@0: // var h = b64tohex(ctext); michael@0: // if(h) return this.decrypt(h); else return null; michael@0: //} michael@0: michael@0: // protected michael@0: RSAKey.prototype.doPrivate = RSADoPrivate; michael@0: michael@0: // public michael@0: RSAKey.prototype.setPrivate = RSASetPrivate; michael@0: RSAKey.prototype.setPrivateEx = RSASetPrivateEx; michael@0: RSAKey.prototype.generate = RSAGenerate; michael@0: RSAKey.prototype.decrypt = RSADecrypt; michael@0: //RSAKey.prototype.b64_decrypt = RSAB64Decrypt; michael@0: michael@0: michael@0: nValue="a5261939975948bb7a58dffe5ff54e65f0498f9175f5a09288810b8975871e99af3b5dd94057b0fc07535f5f97444504fa35169d461d0d30cf0192e307727c065168c788771c561a9400fb49175e9e6aa4e23fe11af69e9412dd23b0cb6684c4c2429bce139e848ab26d0829073351f4acd36074eafd036a5eb83359d2a698d3"; michael@0: eValue="10001"; michael@0: dValue="8e9912f6d3645894e8d38cb58c0db81ff516cf4c7e5a14c7f1eddb1459d2cded4d8d293fc97aee6aefb861859c8b6a3d1dfe710463e1f9ddc72048c09751971c4a580aa51eb523357a3cc48d31cfad1d4a165066ed92d4748fb6571211da5cb14bc11b6e2df7c1a559e6d5ac1cd5c94703a22891464fba23d0d965086277a161"; michael@0: pValue="d090ce58a92c75233a6486cb0a9209bf3583b64f540c76f5294bb97d285eed33aec220bde14b2417951178ac152ceab6da7090905b478195498b352048f15e7d"; michael@0: qValue="cab575dc652bb66df15a0359609d51d1db184750c00c6698b90ef3465c99655103edbf0d54c56aec0ce3c4d22592338092a126a0cc49f65a4a30d222b411e58f"; michael@0: dmp1Value="1a24bca8e273df2f0e47c199bbf678604e7df7215480c77c8db39f49b000ce2cf7500038acfff5433b7d582a01f1826e6f4d42e1c57f5e1fef7b12aabc59fd25"; michael@0: dmq1Value="3d06982efbbe47339e1f6d36b1216b8a741d410b0c662f54f7118b27b9a4ec9d914337eb39841d8666f3034408cf94f5b62f11c402fc994fe15a05493150d9fd"; michael@0: coeffValue="3a3e731acd8960b7ff9eb81a7ff93bd1cfa74cbd56987db58b4594fb09c09084db1734c8143f98b602b981aaa9243ca28deb69b5b280ee8dcee0fd2625e53250"; michael@0: michael@0: setupEngine(am3, 28); michael@0: michael@0: var TEXT = "The quick brown fox jumped over the extremely lazy frog! " + michael@0: "Now is the time for all good men to come to the party."; michael@0: var encrypted; michael@0: michael@0: function encrypt() { michael@0: var RSA = new RSAKey(); michael@0: RSA.setPublic(nValue, eValue); michael@0: RSA.setPrivateEx(nValue, eValue, dValue, pValue, qValue, dmp1Value, dmq1Value, coeffValue); michael@0: encrypted = RSA.encrypt(TEXT); michael@0: } michael@0: michael@0: function decrypt() { michael@0: var RSA = new RSAKey(); michael@0: RSA.setPublic(nValue, eValue); michael@0: RSA.setPrivateEx(nValue, eValue, dValue, pValue, qValue, dmp1Value, dmq1Value, coeffValue); michael@0: var decrypted = RSA.decrypt(encrypted); michael@0: if (decrypted != TEXT) { michael@0: throw new Error("Crypto operation failed"); michael@0: } michael@0: }