/*! (c) tom wu | http://www-cs-students.stanford.edu/~tjw/jsbn/ */ // copyright (c) 2005-2009 tom wu // all rights reserved. // see "license" for details. // extended javascript bn functions, required for rsa private ops. // version 1.1: new biginteger("0", 10) returns "proper" zero // version 1.2: square() api, isprobableprime fix // (public) function bnclone() { var r = nbi(); this.copyto(r); return r; } // (public) return value as integer function bnintvalue() { if(this.s < 0) { if(this.t == 1) return this[0]-this.dv; else if(this.t == 0) return -1; } else if(this.t == 1) return this[0]; else if(this.t == 0) return 0; // assumes 16 < db < 32 return ((this[1]&((1<<(32-this.db))-1))<>24; } // (public) return value as short (assumes db>=16) function bnshortvalue() { return (this.t==0)?this.s:(this[0]<<16)>>16; } // (protected) return x s.t. r^x < dv function bnpchunksize(r) { return math.floor(math.ln2*this.db/math.log(r)); } // (public) 0 if this == 0, 1 if this > 0 function bnsignum() { if(this.s < 0) return -1; else if(this.t <= 0 || (this.t == 1 && this[0] <= 0)) return 0; else return 1; } // (protected) convert to radix string function bnptoradix(b) { if(b == null) b = 10; if(this.signum() == 0 || b < 2 || b > 36) return "0"; var cs = this.chunksize(b); var a = math.pow(b,cs); var d = nbv(a), y = nbi(), z = nbi(), r = ""; this.divremto(d,y,z); while(y.signum() > 0) { r = (a+z.intvalue()).tostring(b).substr(1) + r; y.divremto(d,y,z); } return z.intvalue().tostring(b) + r; } // (protected) convert from radix string function bnpfromradix(s,b) { this.fromint(0); if(b == null) b = 10; var cs = this.chunksize(b); var d = math.pow(b,cs), mi = false, j = 0, w = 0; for(var i = 0; i < s.length; ++i) { var x = intat(s,i); if(x < 0) { if(s.charat(i) == "-" && this.signum() == 0) mi = true; continue; } w = b*w+x; if(++j >= cs) { this.dmultiply(d); this.daddoffset(w,0); j = 0; w = 0; } } if(j > 0) { this.dmultiply(math.pow(b,j)); this.daddoffset(w,0); } if(mi) biginteger.zero.subto(this,this); } // (protected) alternate constructor function bnpfromnumber(a,b,c) { if("number" == typeof b) { // new biginteger(int,int,rng) if(a < 2) this.fromint(1); else { this.fromnumber(a,c); if(!this.testbit(a-1)) // force msb set this.bitwiseto(biginteger.one.shiftleft(a-1),op_or,this); if(this.iseven()) this.daddoffset(1,0); // force odd while(!this.isprobableprime(b)) { this.daddoffset(2,0); if(this.bitlength() > a) this.subto(biginteger.one.shiftleft(a-1),this); } } } else { // new biginteger(int,rng) var x = new array(), t = a&7; x.length = (a>>3)+1; b.nextbytes(x); if(t > 0) x[0] &= ((1< 0) { if(p < this.db && (d = this[i]>>p) != (this.s&this.dm)>>p) r[k++] = d|(this.s<<(this.db-p)); while(i >= 0) { if(p < 8) { d = (this[i]&((1<>(p+=this.db-8); } else { d = (this[i]>>(p-=8))&0xff; if(p <= 0) { p += this.db; --i; } } if((d&0x80) != 0) d |= -256; if(k == 0 && (this.s&0x80) != (d&0x80)) ++k; if(k > 0 || d != this.s) r[k++] = d; } } return r; } function bnequals(a) { return(this.compareto(a)==0); } function bnmin(a) { return(this.compareto(a)<0)?this:a; } function bnmax(a) { return(this.compareto(a)>0)?this:a; } // (protected) r = this op a (bitwise) function bnpbitwiseto(a,op,r) { var i, f, m = math.min(a.t,this.t); for(i = 0; i < m; ++i) r[i] = op(this[i],a[i]); if(a.t < this.t) { f = a.s&this.dm; for(i = m; i < this.t; ++i) r[i] = op(this[i],f); r.t = this.t; } else { f = this.s&this.dm; for(i = m; i < a.t; ++i) r[i] = op(f,a[i]); r.t = a.t; } r.s = op(this.s,a.s); r.clamp(); } // (public) this & a function op_and(x,y) { return x&y; } function bnand(a) { var r = nbi(); this.bitwiseto(a,op_and,r); return r; } // (public) this | a function op_or(x,y) { return x|y; } function bnor(a) { var r = nbi(); this.bitwiseto(a,op_or,r); return r; } // (public) this ^ a function op_xor(x,y) { return x^y; } function bnxor(a) { var r = nbi(); this.bitwiseto(a,op_xor,r); return r; } // (public) this & ~a function op_andnot(x,y) { return x&~y; } function bnandnot(a) { var r = nbi(); this.bitwiseto(a,op_andnot,r); return r; } // (public) ~this function bnnot() { var r = nbi(); for(var i = 0; i < this.t; ++i) r[i] = this.dm&~this[i]; r.t = this.t; r.s = ~this.s; return r; } // (public) this << n function bnshiftleft(n) { var r = nbi(); if(n < 0) this.rshiftto(-n,r); else this.lshiftto(n,r); return r; } // (public) this >> n function bnshiftright(n) { var r = nbi(); if(n < 0) this.lshiftto(-n,r); else this.rshiftto(n,r); return r; } // return index of lowest 1-bit in x, x < 2^31 function lbit(x) { if(x == 0) return -1; var r = 0; if((x&0xffff) == 0) { x >>= 16; r += 16; } if((x&0xff) == 0) { x >>= 8; r += 8; } if((x&0xf) == 0) { x >>= 4; r += 4; } if((x&3) == 0) { x >>= 2; r += 2; } if((x&1) == 0) ++r; return r; } // (public) returns index of lowest 1-bit (or -1 if none) function bngetlowestsetbit() { for(var i = 0; i < this.t; ++i) if(this[i] != 0) return i*this.db+lbit(this[i]); if(this.s < 0) return this.t*this.db; return -1; } // return number of 1 bits in x function cbit(x) { var r = 0; while(x != 0) { x &= x-1; ++r; } return r; } // (public) return number of set bits function bnbitcount() { var r = 0, x = this.s&this.dm; for(var i = 0; i < this.t; ++i) r += cbit(this[i]^x); return r; } // (public) true iff nth bit is set function bntestbit(n) { var j = math.floor(n/this.db); if(j >= this.t) return(this.s!=0); return((this[j]&(1<<(n%this.db)))!=0); } // (protected) this op (1<>= this.db; } if(a.t < this.t) { c += a.s; while(i < this.t) { c += this[i]; r[i++] = c&this.dm; c >>= this.db; } c += this.s; } else { c += this.s; while(i < a.t) { c += a[i]; r[i++] = c&this.dm; c >>= this.db; } c += a.s; } r.s = (c<0)?-1:0; if(c > 0) r[i++] = c; else if(c < -1) r[i++] = this.dv+c; r.t = i; r.clamp(); } // (public) this + a function bnadd(a) { var r = nbi(); this.addto(a,r); return r; } // (public) this - a function bnsubtract(a) { var r = nbi(); this.subto(a,r); return r; } // (public) this * a function bnmultiply(a) { var r = nbi(); this.multiplyto(a,r); return r; } // (public) this^2 function bnsquare() { var r = nbi(); this.squareto(r); return r; } // (public) this / a function bndivide(a) { var r = nbi(); this.divremto(a,r,null); return r; } // (public) this % a function bnremainder(a) { var r = nbi(); this.divremto(a,null,r); return r; } // (public) [this/a,this%a] function bndivideandremainder(a) { var q = nbi(), r = nbi(); this.divremto(a,q,r); return new array(q,r); } // (protected) this *= n, this >= 0, 1 < n < dv function bnpdmultiply(n) { this[this.t] = this.am(0,n-1,this,0,0,this.t); ++this.t; this.clamp(); } // (protected) this += n << w words, this >= 0 function bnpdaddoffset(n,w) { if(n == 0) return; while(this.t <= w) this[this.t++] = 0; this[w] += n; while(this[w] >= this.dv) { this[w] -= this.dv; if(++w >= this.t) this[this.t++] = 0; ++this[w]; } } // a "null" reducer function nullexp() {} function nnop(x) { return x; } function nmulto(x,y,r) { x.multiplyto(y,r); } function nsqrto(x,r) { x.squareto(r); } nullexp.prototype.convert = nnop; nullexp.prototype.revert = nnop; nullexp.prototype.multo = nmulto; nullexp.prototype.sqrto = nsqrto; // (public) this^e function bnpow(e) { return this.exp(e,new nullexp()); } // (protected) r = lower n words of "this * a", a.t <= n // "this" should be the larger one if appropriate. function bnpmultiplylowerto(a,n,r) { var i = math.min(this.t+a.t,n); r.s = 0; // assumes a,this >= 0 r.t = i; while(i > 0) r[--i] = 0; var j; for(j = r.t-this.t; i < j; ++i) r[i+this.t] = this.am(0,a[i],r,i,0,this.t); for(j = math.min(a.t,n); i < j; ++i) this.am(0,a[i],r,i,0,n-i); r.clamp(); } // (protected) r = "this * a" without lower n words, n > 0 // "this" should be the larger one if appropriate. function bnpmultiplyupperto(a,n,r) { --n; var i = r.t = this.t+a.t-n; r.s = 0; // assumes a,this >= 0 while(--i >= 0) r[i] = 0; for(i = math.max(n-this.t,0); i < a.t; ++i) r[this.t+i-n] = this.am(n-i,a[i],r,0,0,this.t+i-n); r.clamp(); r.drshiftto(1,r); } // barrett modular reduction function barrett(m) { // setup barrett this.r2 = nbi(); this.q3 = nbi(); biginteger.one.dlshiftto(2*m.t,this.r2); this.mu = this.r2.divide(m); this.m = m; } function barrettconvert(x) { if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m); else if(x.compareto(this.m) < 0) return x; else { var r = nbi(); x.copyto(r); this.reduce(r); return r; } } function barrettrevert(x) { return x; } // x = x mod m (hac 14.42) function barrettreduce(x) { x.drshiftto(this.m.t-1,this.r2); if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); } this.mu.multiplyupperto(this.r2,this.m.t+1,this.q3); this.m.multiplylowerto(this.q3,this.m.t+1,this.r2); while(x.compareto(this.r2) < 0) x.daddoffset(1,this.m.t+1); x.subto(this.r2,x); while(x.compareto(this.m) >= 0) x.subto(this.m,x); } // r = x^2 mod m; x != r function barrettsqrto(x,r) { x.squareto(r); this.reduce(r); } // r = x*y mod m; x,y != r function barrettmulto(x,y,r) { x.multiplyto(y,r); this.reduce(r); } barrett.prototype.convert = barrettconvert; barrett.prototype.revert = barrettrevert; barrett.prototype.reduce = barrettreduce; barrett.prototype.multo = barrettmulto; barrett.prototype.sqrto = barrettsqrto; // (public) this^e % m (hac 14.85) function bnmodpow(e,m) { var i = e.bitlength(), k, r = nbv(1), z; if(i <= 0) return r; else if(i < 18) k = 1; else if(i < 48) k = 3; else if(i < 144) k = 4; else if(i < 768) k = 5; else k = 6; if(i < 8) z = new classic(m); else if(m.iseven()) z = new barrett(m); else z = new montgomery(m); // precomputation var g = new array(), n = 3, k1 = k-1, km = (1< 1) { var g2 = nbi(); z.sqrto(g[1],g2); while(n <= km) { g[n] = nbi(); z.multo(g2,g[n-2],g[n]); n += 2; } } var j = e.t-1, w, is1 = true, r2 = nbi(), t; i = nbits(e[j])-1; while(j >= 0) { if(i >= k1) w = (e[j]>>(i-k1))&km; else { w = (e[j]&((1<<(i+1))-1))<<(k1-i); if(j > 0) w |= e[j-1]>>(this.db+i-k1); } n = k; while((w&1) == 0) { w >>= 1; --n; } if((i -= n) < 0) { i += this.db; --j; } if(is1) { // ret == 1, don't bother squaring or multiplying it g[w].copyto(r); is1 = false; } else { while(n > 1) { z.sqrto(r,r2); z.sqrto(r2,r); n -= 2; } if(n > 0) z.sqrto(r,r2); else { t = r; r = r2; r2 = t; } z.multo(r2,g[w],r); } while(j >= 0 && (e[j]&(1< 0) { x.rshiftto(g,x); y.rshiftto(g,y); } while(x.signum() > 0) { if((i = x.getlowestsetbit()) > 0) x.rshiftto(i,x); if((i = y.getlowestsetbit()) > 0) y.rshiftto(i,y); if(x.compareto(y) >= 0) { x.subto(y,x); x.rshiftto(1,x); } else { y.subto(x,y); y.rshiftto(1,y); } } if(g > 0) y.lshiftto(g,y); return y; } // (protected) this % n, n < 2^26 function bnpmodint(n) { if(n <= 0) return 0; var d = this.dv%n, r = (this.s<0)?n-1:0; if(this.t > 0) if(d == 0) r = this[0]%n; else for(var i = this.t-1; i >= 0; --i) r = (d*r+this[i])%n; return r; } // (public) 1/this % m (hac 14.61) function bnmodinverse(m) { var ac = m.iseven(); if((this.iseven() && ac) || m.signum() == 0) return biginteger.zero; var u = m.clone(), v = this.clone(); var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1); while(u.signum() != 0) { while(u.iseven()) { u.rshiftto(1,u); if(ac) { if(!a.iseven() || !b.iseven()) { a.addto(this,a); b.subto(m,b); } a.rshiftto(1,a); } else if(!b.iseven()) b.subto(m,b); b.rshiftto(1,b); } while(v.iseven()) { v.rshiftto(1,v); if(ac) { if(!c.iseven() || !d.iseven()) { c.addto(this,c); d.subto(m,d); } c.rshiftto(1,c); } else if(!d.iseven()) d.subto(m,d); d.rshiftto(1,d); } if(u.compareto(v) >= 0) { u.subto(v,u); if(ac) a.subto(c,a); b.subto(d,b); } else { v.subto(u,v); if(ac) c.subto(a,c); d.subto(b,d); } } if(v.compareto(biginteger.one) != 0) return biginteger.zero; if(d.compareto(m) >= 0) return d.subtract(m); if(d.signum() < 0) d.addto(m,d); else return d; if(d.signum() < 0) return d.add(m); else return d; } 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]; var lplim = (1<<26)/lowprimes[lowprimes.length-1]; // (public) test primality with certainty >= 1-.5^t function bnisprobableprime(t) { var i, x = this.abs(); if(x.t == 1 && x[0] <= lowprimes[lowprimes.length-1]) { for(i = 0; i < lowprimes.length; ++i) if(x[0] == lowprimes[i]) return true; return false; } if(x.iseven()) return false; i = 1; while(i < lowprimes.length) { var m = lowprimes[i], j = i+1; while(j < lowprimes.length && m < lplim) m *= lowprimes[j++]; m = x.modint(m); while(i < j) if(m%lowprimes[i++] == 0) return false; } return x.millerrabin(t); } // (protected) true if probably prime (hac 4.24, miller-rabin) function bnpmillerrabin(t) { var n1 = this.subtract(biginteger.one); var k = n1.getlowestsetbit(); if(k <= 0) return false; var r = n1.shiftright(k); t = (t+1)>>1; if(t > lowprimes.length) t = lowprimes.length; var a = nbi(); for(var i = 0; i < t; ++i) { //pick bases at random, instead of starting at 2 a.fromint(lowprimes[math.floor(math.random()*lowprimes.length)]); var y = a.modpow(r,this); if(y.compareto(biginteger.one) != 0 && y.compareto(n1) != 0) { var j = 1; while(j++ < k && y.compareto(n1) != 0) { y = y.modpowint(2,this); if(y.compareto(biginteger.one) == 0) return false; } if(y.compareto(n1) != 0) return false; } } return true; } // protected biginteger.prototype.chunksize = bnpchunksize; biginteger.prototype.toradix = bnptoradix; biginteger.prototype.fromradix = bnpfromradix; biginteger.prototype.fromnumber = bnpfromnumber; biginteger.prototype.bitwiseto = bnpbitwiseto; biginteger.prototype.changebit = bnpchangebit; biginteger.prototype.addto = bnpaddto; biginteger.prototype.dmultiply = bnpdmultiply; biginteger.prototype.daddoffset = bnpdaddoffset; biginteger.prototype.multiplylowerto = bnpmultiplylowerto; biginteger.prototype.multiplyupperto = bnpmultiplyupperto; biginteger.prototype.modint = bnpmodint; biginteger.prototype.millerrabin = bnpmillerrabin; // public biginteger.prototype.clone = bnclone; biginteger.prototype.intvalue = bnintvalue; biginteger.prototype.bytevalue = bnbytevalue; biginteger.prototype.shortvalue = bnshortvalue; biginteger.prototype.signum = bnsignum; biginteger.prototype.tobytearray = bntobytearray; biginteger.prototype.equals = bnequals; biginteger.prototype.min = bnmin; biginteger.prototype.max = bnmax; biginteger.prototype.and = bnand; biginteger.prototype.or = bnor; biginteger.prototype.xor = bnxor; biginteger.prototype.andnot = bnandnot; biginteger.prototype.not = bnnot; biginteger.prototype.shiftleft = bnshiftleft; biginteger.prototype.shiftright = bnshiftright; biginteger.prototype.getlowestsetbit = bngetlowestsetbit; biginteger.prototype.bitcount = bnbitcount; biginteger.prototype.testbit = bntestbit; biginteger.prototype.setbit = bnsetbit; biginteger.prototype.clearbit = bnclearbit; biginteger.prototype.flipbit = bnflipbit; biginteger.prototype.add = bnadd; biginteger.prototype.subtract = bnsubtract; biginteger.prototype.multiply = bnmultiply; biginteger.prototype.divide = bndivide; biginteger.prototype.remainder = bnremainder; biginteger.prototype.divideandremainder = bndivideandremainder; biginteger.prototype.modpow = bnmodpow; biginteger.prototype.modinverse = bnmodinverse; biginteger.prototype.pow = bnpow; biginteger.prototype.gcd = bngcd; biginteger.prototype.isprobableprime = bnisprobableprime; // jsbn-specific extension biginteger.prototype.square = bnsquare; // biginteger interfaces not implemented in jsbn: // biginteger(int signum, byte[] magnitude) // double doublevalue() // float floatvalue() // int hashcode() // long longvalue() // static biginteger valueof(long val)