11cb0ef41Sopenharmony_ci(function(){ 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci // Copyright (c) 2005 Tom Wu 41cb0ef41Sopenharmony_ci // All Rights Reserved. 51cb0ef41Sopenharmony_ci // See "LICENSE" for details. 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci // Basic JavaScript BN library - subset useful for RSA encryption. 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci // Bits per digit 101cb0ef41Sopenharmony_ci var dbits; 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci // JavaScript engine analysis 131cb0ef41Sopenharmony_ci var canary = 0xdeadbeefcafe; 141cb0ef41Sopenharmony_ci var j_lm = ((canary&0xffffff)==0xefcafe); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci // (public) Constructor 171cb0ef41Sopenharmony_ci function BigInteger(a,b,c) { 181cb0ef41Sopenharmony_ci if(a != null) 191cb0ef41Sopenharmony_ci if("number" == typeof a) this.fromNumber(a,b,c); 201cb0ef41Sopenharmony_ci else if(b == null && "string" != typeof a) this.fromString(a,256); 211cb0ef41Sopenharmony_ci else this.fromString(a,b); 221cb0ef41Sopenharmony_ci } 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci // return new, unset BigInteger 251cb0ef41Sopenharmony_ci function nbi() { return new BigInteger(null); } 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci // am: Compute w_j += (x*this_i), propagate carries, 281cb0ef41Sopenharmony_ci // c is initial carry, returns final carry. 291cb0ef41Sopenharmony_ci // c < 3*dvalue, x < 2*dvalue, this_i < dvalue 301cb0ef41Sopenharmony_ci // We need to select the fastest one that works in this environment. 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci // am1: use a single mult and divide to get the high bits, 331cb0ef41Sopenharmony_ci // max digit bits should be 26 because 341cb0ef41Sopenharmony_ci // max internal value = 2*dvalue^2-2*dvalue (< 2^53) 351cb0ef41Sopenharmony_ci function am1(i,x,w,j,c,n) { 361cb0ef41Sopenharmony_ci while(--n >= 0) { 371cb0ef41Sopenharmony_ci var v = x*this[i++]+w[j]+c; 381cb0ef41Sopenharmony_ci c = Math.floor(v/0x4000000); 391cb0ef41Sopenharmony_ci w[j++] = v&0x3ffffff; 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci return c; 421cb0ef41Sopenharmony_ci } 431cb0ef41Sopenharmony_ci // am2 avoids a big mult-and-extract completely. 441cb0ef41Sopenharmony_ci // Max digit bits should be <= 30 because we do bitwise ops 451cb0ef41Sopenharmony_ci // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31) 461cb0ef41Sopenharmony_ci function am2(i,x,w,j,c,n) { 471cb0ef41Sopenharmony_ci var xl = x&0x7fff, xh = x>>15; 481cb0ef41Sopenharmony_ci while(--n >= 0) { 491cb0ef41Sopenharmony_ci var l = this[i]&0x7fff; 501cb0ef41Sopenharmony_ci var h = this[i++]>>15; 511cb0ef41Sopenharmony_ci var m = xh*l+h*xl; 521cb0ef41Sopenharmony_ci l = xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff); 531cb0ef41Sopenharmony_ci c = (l>>>30)+(m>>>15)+xh*h+(c>>>30); 541cb0ef41Sopenharmony_ci w[j++] = l&0x3fffffff; 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci return c; 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci // Alternately, set max digit bits to 28 since some 591cb0ef41Sopenharmony_ci // browsers slow down when dealing with 32-bit numbers. 601cb0ef41Sopenharmony_ci function am3(i,x,w,j,c,n) { 611cb0ef41Sopenharmony_ci var xl = x&0x3fff, xh = x>>14; 621cb0ef41Sopenharmony_ci while(--n >= 0) { 631cb0ef41Sopenharmony_ci var l = this[i]&0x3fff; 641cb0ef41Sopenharmony_ci var h = this[i++]>>14; 651cb0ef41Sopenharmony_ci var m = xh*l+h*xl; 661cb0ef41Sopenharmony_ci l = xl*l+((m&0x3fff)<<14)+w[j]+c; 671cb0ef41Sopenharmony_ci c = (l>>28)+(m>>14)+xh*h; 681cb0ef41Sopenharmony_ci w[j++] = l&0xfffffff; 691cb0ef41Sopenharmony_ci } 701cb0ef41Sopenharmony_ci return c; 711cb0ef41Sopenharmony_ci } 721cb0ef41Sopenharmony_ci var inBrowser = typeof navigator !== "undefined"; 731cb0ef41Sopenharmony_ci if(inBrowser && j_lm && (navigator.appName == "Microsoft Internet Explorer")) { 741cb0ef41Sopenharmony_ci BigInteger.prototype.am = am2; 751cb0ef41Sopenharmony_ci dbits = 30; 761cb0ef41Sopenharmony_ci } 771cb0ef41Sopenharmony_ci else if(inBrowser && j_lm && (navigator.appName != "Netscape")) { 781cb0ef41Sopenharmony_ci BigInteger.prototype.am = am1; 791cb0ef41Sopenharmony_ci dbits = 26; 801cb0ef41Sopenharmony_ci } 811cb0ef41Sopenharmony_ci else { // Mozilla/Netscape seems to prefer am3 821cb0ef41Sopenharmony_ci BigInteger.prototype.am = am3; 831cb0ef41Sopenharmony_ci dbits = 28; 841cb0ef41Sopenharmony_ci } 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci BigInteger.prototype.DB = dbits; 871cb0ef41Sopenharmony_ci BigInteger.prototype.DM = ((1<<dbits)-1); 881cb0ef41Sopenharmony_ci BigInteger.prototype.DV = (1<<dbits); 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci var BI_FP = 52; 911cb0ef41Sopenharmony_ci BigInteger.prototype.FV = Math.pow(2,BI_FP); 921cb0ef41Sopenharmony_ci BigInteger.prototype.F1 = BI_FP-dbits; 931cb0ef41Sopenharmony_ci BigInteger.prototype.F2 = 2*dbits-BI_FP; 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci // Digit conversions 961cb0ef41Sopenharmony_ci var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz"; 971cb0ef41Sopenharmony_ci var BI_RC = new Array(); 981cb0ef41Sopenharmony_ci var rr,vv; 991cb0ef41Sopenharmony_ci rr = "0".charCodeAt(0); 1001cb0ef41Sopenharmony_ci for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv; 1011cb0ef41Sopenharmony_ci rr = "a".charCodeAt(0); 1021cb0ef41Sopenharmony_ci for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv; 1031cb0ef41Sopenharmony_ci rr = "A".charCodeAt(0); 1041cb0ef41Sopenharmony_ci for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv; 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci function int2char(n) { return BI_RM.charAt(n); } 1071cb0ef41Sopenharmony_ci function intAt(s,i) { 1081cb0ef41Sopenharmony_ci var c = BI_RC[s.charCodeAt(i)]; 1091cb0ef41Sopenharmony_ci return (c==null)?-1:c; 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci // (protected) copy this to r 1131cb0ef41Sopenharmony_ci function bnpCopyTo(r) { 1141cb0ef41Sopenharmony_ci for(var i = this.t-1; i >= 0; --i) r[i] = this[i]; 1151cb0ef41Sopenharmony_ci r.t = this.t; 1161cb0ef41Sopenharmony_ci r.s = this.s; 1171cb0ef41Sopenharmony_ci } 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci // (protected) set from integer value x, -DV <= x < DV 1201cb0ef41Sopenharmony_ci function bnpFromInt(x) { 1211cb0ef41Sopenharmony_ci this.t = 1; 1221cb0ef41Sopenharmony_ci this.s = (x<0)?-1:0; 1231cb0ef41Sopenharmony_ci if(x > 0) this[0] = x; 1241cb0ef41Sopenharmony_ci else if(x < -1) this[0] = x+this.DV; 1251cb0ef41Sopenharmony_ci else this.t = 0; 1261cb0ef41Sopenharmony_ci } 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ci // return bigint initialized to value 1291cb0ef41Sopenharmony_ci function nbv(i) { var r = nbi(); r.fromInt(i); return r; } 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci // (protected) set from string and radix 1321cb0ef41Sopenharmony_ci function bnpFromString(s,b) { 1331cb0ef41Sopenharmony_ci var k; 1341cb0ef41Sopenharmony_ci if(b == 16) k = 4; 1351cb0ef41Sopenharmony_ci else if(b == 8) k = 3; 1361cb0ef41Sopenharmony_ci else if(b == 256) k = 8; // byte array 1371cb0ef41Sopenharmony_ci else if(b == 2) k = 1; 1381cb0ef41Sopenharmony_ci else if(b == 32) k = 5; 1391cb0ef41Sopenharmony_ci else if(b == 4) k = 2; 1401cb0ef41Sopenharmony_ci else { this.fromRadix(s,b); return; } 1411cb0ef41Sopenharmony_ci this.t = 0; 1421cb0ef41Sopenharmony_ci this.s = 0; 1431cb0ef41Sopenharmony_ci var i = s.length, mi = false, sh = 0; 1441cb0ef41Sopenharmony_ci while(--i >= 0) { 1451cb0ef41Sopenharmony_ci var x = (k==8)?s[i]&0xff:intAt(s,i); 1461cb0ef41Sopenharmony_ci if(x < 0) { 1471cb0ef41Sopenharmony_ci if(s.charAt(i) == "-") mi = true; 1481cb0ef41Sopenharmony_ci continue; 1491cb0ef41Sopenharmony_ci } 1501cb0ef41Sopenharmony_ci mi = false; 1511cb0ef41Sopenharmony_ci if(sh == 0) 1521cb0ef41Sopenharmony_ci this[this.t++] = x; 1531cb0ef41Sopenharmony_ci else if(sh+k > this.DB) { 1541cb0ef41Sopenharmony_ci this[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh; 1551cb0ef41Sopenharmony_ci this[this.t++] = (x>>(this.DB-sh)); 1561cb0ef41Sopenharmony_ci } 1571cb0ef41Sopenharmony_ci else 1581cb0ef41Sopenharmony_ci this[this.t-1] |= x<<sh; 1591cb0ef41Sopenharmony_ci sh += k; 1601cb0ef41Sopenharmony_ci if(sh >= this.DB) sh -= this.DB; 1611cb0ef41Sopenharmony_ci } 1621cb0ef41Sopenharmony_ci if(k == 8 && (s[0]&0x80) != 0) { 1631cb0ef41Sopenharmony_ci this.s = -1; 1641cb0ef41Sopenharmony_ci if(sh > 0) this[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh; 1651cb0ef41Sopenharmony_ci } 1661cb0ef41Sopenharmony_ci this.clamp(); 1671cb0ef41Sopenharmony_ci if(mi) BigInteger.ZERO.subTo(this,this); 1681cb0ef41Sopenharmony_ci } 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci // (protected) clamp off excess high words 1711cb0ef41Sopenharmony_ci function bnpClamp() { 1721cb0ef41Sopenharmony_ci var c = this.s&this.DM; 1731cb0ef41Sopenharmony_ci while(this.t > 0 && this[this.t-1] == c) --this.t; 1741cb0ef41Sopenharmony_ci } 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci // (public) return string representation in given radix 1771cb0ef41Sopenharmony_ci function bnToString(b) { 1781cb0ef41Sopenharmony_ci if(this.s < 0) return "-"+this.negate().toString(b); 1791cb0ef41Sopenharmony_ci var k; 1801cb0ef41Sopenharmony_ci if(b == 16) k = 4; 1811cb0ef41Sopenharmony_ci else if(b == 8) k = 3; 1821cb0ef41Sopenharmony_ci else if(b == 2) k = 1; 1831cb0ef41Sopenharmony_ci else if(b == 32) k = 5; 1841cb0ef41Sopenharmony_ci else if(b == 4) k = 2; 1851cb0ef41Sopenharmony_ci else return this.toRadix(b); 1861cb0ef41Sopenharmony_ci var km = (1<<k)-1, d, m = false, r = "", i = this.t; 1871cb0ef41Sopenharmony_ci var p = this.DB-(i*this.DB)%k; 1881cb0ef41Sopenharmony_ci if(i-- > 0) { 1891cb0ef41Sopenharmony_ci if(p < this.DB && (d = this[i]>>p) > 0) { m = true; r = int2char(d); } 1901cb0ef41Sopenharmony_ci while(i >= 0) { 1911cb0ef41Sopenharmony_ci if(p < k) { 1921cb0ef41Sopenharmony_ci d = (this[i]&((1<<p)-1))<<(k-p); 1931cb0ef41Sopenharmony_ci d |= this[--i]>>(p+=this.DB-k); 1941cb0ef41Sopenharmony_ci } 1951cb0ef41Sopenharmony_ci else { 1961cb0ef41Sopenharmony_ci d = (this[i]>>(p-=k))&km; 1971cb0ef41Sopenharmony_ci if(p <= 0) { p += this.DB; --i; } 1981cb0ef41Sopenharmony_ci } 1991cb0ef41Sopenharmony_ci if(d > 0) m = true; 2001cb0ef41Sopenharmony_ci if(m) r += int2char(d); 2011cb0ef41Sopenharmony_ci } 2021cb0ef41Sopenharmony_ci } 2031cb0ef41Sopenharmony_ci return m?r:"0"; 2041cb0ef41Sopenharmony_ci } 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci // (public) -this 2071cb0ef41Sopenharmony_ci function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; } 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ci // (public) |this| 2101cb0ef41Sopenharmony_ci function bnAbs() { return (this.s<0)?this.negate():this; } 2111cb0ef41Sopenharmony_ci 2121cb0ef41Sopenharmony_ci // (public) return + if this > a, - if this < a, 0 if equal 2131cb0ef41Sopenharmony_ci function bnCompareTo(a) { 2141cb0ef41Sopenharmony_ci var r = this.s-a.s; 2151cb0ef41Sopenharmony_ci if(r != 0) return r; 2161cb0ef41Sopenharmony_ci var i = this.t; 2171cb0ef41Sopenharmony_ci r = i-a.t; 2181cb0ef41Sopenharmony_ci if(r != 0) return (this.s<0)?-r:r; 2191cb0ef41Sopenharmony_ci while(--i >= 0) if((r=this[i]-a[i]) != 0) return r; 2201cb0ef41Sopenharmony_ci return 0; 2211cb0ef41Sopenharmony_ci } 2221cb0ef41Sopenharmony_ci 2231cb0ef41Sopenharmony_ci // returns bit length of the integer x 2241cb0ef41Sopenharmony_ci function nbits(x) { 2251cb0ef41Sopenharmony_ci var r = 1, t; 2261cb0ef41Sopenharmony_ci if((t=x>>>16) != 0) { x = t; r += 16; } 2271cb0ef41Sopenharmony_ci if((t=x>>8) != 0) { x = t; r += 8; } 2281cb0ef41Sopenharmony_ci if((t=x>>4) != 0) { x = t; r += 4; } 2291cb0ef41Sopenharmony_ci if((t=x>>2) != 0) { x = t; r += 2; } 2301cb0ef41Sopenharmony_ci if((t=x>>1) != 0) { x = t; r += 1; } 2311cb0ef41Sopenharmony_ci return r; 2321cb0ef41Sopenharmony_ci } 2331cb0ef41Sopenharmony_ci 2341cb0ef41Sopenharmony_ci // (public) return the number of bits in "this" 2351cb0ef41Sopenharmony_ci function bnBitLength() { 2361cb0ef41Sopenharmony_ci if(this.t <= 0) return 0; 2371cb0ef41Sopenharmony_ci return this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM)); 2381cb0ef41Sopenharmony_ci } 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_ci // (protected) r = this << n*DB 2411cb0ef41Sopenharmony_ci function bnpDLShiftTo(n,r) { 2421cb0ef41Sopenharmony_ci var i; 2431cb0ef41Sopenharmony_ci for(i = this.t-1; i >= 0; --i) r[i+n] = this[i]; 2441cb0ef41Sopenharmony_ci for(i = n-1; i >= 0; --i) r[i] = 0; 2451cb0ef41Sopenharmony_ci r.t = this.t+n; 2461cb0ef41Sopenharmony_ci r.s = this.s; 2471cb0ef41Sopenharmony_ci } 2481cb0ef41Sopenharmony_ci 2491cb0ef41Sopenharmony_ci // (protected) r = this >> n*DB 2501cb0ef41Sopenharmony_ci function bnpDRShiftTo(n,r) { 2511cb0ef41Sopenharmony_ci for(var i = n; i < this.t; ++i) r[i-n] = this[i]; 2521cb0ef41Sopenharmony_ci r.t = Math.max(this.t-n,0); 2531cb0ef41Sopenharmony_ci r.s = this.s; 2541cb0ef41Sopenharmony_ci } 2551cb0ef41Sopenharmony_ci 2561cb0ef41Sopenharmony_ci // (protected) r = this << n 2571cb0ef41Sopenharmony_ci function bnpLShiftTo(n,r) { 2581cb0ef41Sopenharmony_ci var bs = n%this.DB; 2591cb0ef41Sopenharmony_ci var cbs = this.DB-bs; 2601cb0ef41Sopenharmony_ci var bm = (1<<cbs)-1; 2611cb0ef41Sopenharmony_ci var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i; 2621cb0ef41Sopenharmony_ci for(i = this.t-1; i >= 0; --i) { 2631cb0ef41Sopenharmony_ci r[i+ds+1] = (this[i]>>cbs)|c; 2641cb0ef41Sopenharmony_ci c = (this[i]&bm)<<bs; 2651cb0ef41Sopenharmony_ci } 2661cb0ef41Sopenharmony_ci for(i = ds-1; i >= 0; --i) r[i] = 0; 2671cb0ef41Sopenharmony_ci r[ds] = c; 2681cb0ef41Sopenharmony_ci r.t = this.t+ds+1; 2691cb0ef41Sopenharmony_ci r.s = this.s; 2701cb0ef41Sopenharmony_ci r.clamp(); 2711cb0ef41Sopenharmony_ci } 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ci // (protected) r = this >> n 2741cb0ef41Sopenharmony_ci function bnpRShiftTo(n,r) { 2751cb0ef41Sopenharmony_ci r.s = this.s; 2761cb0ef41Sopenharmony_ci var ds = Math.floor(n/this.DB); 2771cb0ef41Sopenharmony_ci if(ds >= this.t) { r.t = 0; return; } 2781cb0ef41Sopenharmony_ci var bs = n%this.DB; 2791cb0ef41Sopenharmony_ci var cbs = this.DB-bs; 2801cb0ef41Sopenharmony_ci var bm = (1<<bs)-1; 2811cb0ef41Sopenharmony_ci r[0] = this[ds]>>bs; 2821cb0ef41Sopenharmony_ci for(var i = ds+1; i < this.t; ++i) { 2831cb0ef41Sopenharmony_ci r[i-ds-1] |= (this[i]&bm)<<cbs; 2841cb0ef41Sopenharmony_ci r[i-ds] = this[i]>>bs; 2851cb0ef41Sopenharmony_ci } 2861cb0ef41Sopenharmony_ci if(bs > 0) r[this.t-ds-1] |= (this.s&bm)<<cbs; 2871cb0ef41Sopenharmony_ci r.t = this.t-ds; 2881cb0ef41Sopenharmony_ci r.clamp(); 2891cb0ef41Sopenharmony_ci } 2901cb0ef41Sopenharmony_ci 2911cb0ef41Sopenharmony_ci // (protected) r = this - a 2921cb0ef41Sopenharmony_ci function bnpSubTo(a,r) { 2931cb0ef41Sopenharmony_ci var i = 0, c = 0, m = Math.min(a.t,this.t); 2941cb0ef41Sopenharmony_ci while(i < m) { 2951cb0ef41Sopenharmony_ci c += this[i]-a[i]; 2961cb0ef41Sopenharmony_ci r[i++] = c&this.DM; 2971cb0ef41Sopenharmony_ci c >>= this.DB; 2981cb0ef41Sopenharmony_ci } 2991cb0ef41Sopenharmony_ci if(a.t < this.t) { 3001cb0ef41Sopenharmony_ci c -= a.s; 3011cb0ef41Sopenharmony_ci while(i < this.t) { 3021cb0ef41Sopenharmony_ci c += this[i]; 3031cb0ef41Sopenharmony_ci r[i++] = c&this.DM; 3041cb0ef41Sopenharmony_ci c >>= this.DB; 3051cb0ef41Sopenharmony_ci } 3061cb0ef41Sopenharmony_ci c += this.s; 3071cb0ef41Sopenharmony_ci } 3081cb0ef41Sopenharmony_ci else { 3091cb0ef41Sopenharmony_ci c += this.s; 3101cb0ef41Sopenharmony_ci while(i < a.t) { 3111cb0ef41Sopenharmony_ci c -= a[i]; 3121cb0ef41Sopenharmony_ci r[i++] = c&this.DM; 3131cb0ef41Sopenharmony_ci c >>= this.DB; 3141cb0ef41Sopenharmony_ci } 3151cb0ef41Sopenharmony_ci c -= a.s; 3161cb0ef41Sopenharmony_ci } 3171cb0ef41Sopenharmony_ci r.s = (c<0)?-1:0; 3181cb0ef41Sopenharmony_ci if(c < -1) r[i++] = this.DV+c; 3191cb0ef41Sopenharmony_ci else if(c > 0) r[i++] = c; 3201cb0ef41Sopenharmony_ci r.t = i; 3211cb0ef41Sopenharmony_ci r.clamp(); 3221cb0ef41Sopenharmony_ci } 3231cb0ef41Sopenharmony_ci 3241cb0ef41Sopenharmony_ci // (protected) r = this * a, r != this,a (HAC 14.12) 3251cb0ef41Sopenharmony_ci // "this" should be the larger one if appropriate. 3261cb0ef41Sopenharmony_ci function bnpMultiplyTo(a,r) { 3271cb0ef41Sopenharmony_ci var x = this.abs(), y = a.abs(); 3281cb0ef41Sopenharmony_ci var i = x.t; 3291cb0ef41Sopenharmony_ci r.t = i+y.t; 3301cb0ef41Sopenharmony_ci while(--i >= 0) r[i] = 0; 3311cb0ef41Sopenharmony_ci for(i = 0; i < y.t; ++i) r[i+x.t] = x.am(0,y[i],r,i,0,x.t); 3321cb0ef41Sopenharmony_ci r.s = 0; 3331cb0ef41Sopenharmony_ci r.clamp(); 3341cb0ef41Sopenharmony_ci if(this.s != a.s) BigInteger.ZERO.subTo(r,r); 3351cb0ef41Sopenharmony_ci } 3361cb0ef41Sopenharmony_ci 3371cb0ef41Sopenharmony_ci // (protected) r = this^2, r != this (HAC 14.16) 3381cb0ef41Sopenharmony_ci function bnpSquareTo(r) { 3391cb0ef41Sopenharmony_ci var x = this.abs(); 3401cb0ef41Sopenharmony_ci var i = r.t = 2*x.t; 3411cb0ef41Sopenharmony_ci while(--i >= 0) r[i] = 0; 3421cb0ef41Sopenharmony_ci for(i = 0; i < x.t-1; ++i) { 3431cb0ef41Sopenharmony_ci var c = x.am(i,x[i],r,2*i,0,1); 3441cb0ef41Sopenharmony_ci if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1)) >= x.DV) { 3451cb0ef41Sopenharmony_ci r[i+x.t] -= x.DV; 3461cb0ef41Sopenharmony_ci r[i+x.t+1] = 1; 3471cb0ef41Sopenharmony_ci } 3481cb0ef41Sopenharmony_ci } 3491cb0ef41Sopenharmony_ci if(r.t > 0) r[r.t-1] += x.am(i,x[i],r,2*i,0,1); 3501cb0ef41Sopenharmony_ci r.s = 0; 3511cb0ef41Sopenharmony_ci r.clamp(); 3521cb0ef41Sopenharmony_ci } 3531cb0ef41Sopenharmony_ci 3541cb0ef41Sopenharmony_ci // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20) 3551cb0ef41Sopenharmony_ci // r != q, this != m. q or r may be null. 3561cb0ef41Sopenharmony_ci function bnpDivRemTo(m,q,r) { 3571cb0ef41Sopenharmony_ci var pm = m.abs(); 3581cb0ef41Sopenharmony_ci if(pm.t <= 0) return; 3591cb0ef41Sopenharmony_ci var pt = this.abs(); 3601cb0ef41Sopenharmony_ci if(pt.t < pm.t) { 3611cb0ef41Sopenharmony_ci if(q != null) q.fromInt(0); 3621cb0ef41Sopenharmony_ci if(r != null) this.copyTo(r); 3631cb0ef41Sopenharmony_ci return; 3641cb0ef41Sopenharmony_ci } 3651cb0ef41Sopenharmony_ci if(r == null) r = nbi(); 3661cb0ef41Sopenharmony_ci var y = nbi(), ts = this.s, ms = m.s; 3671cb0ef41Sopenharmony_ci var nsh = this.DB-nbits(pm[pm.t-1]); // normalize modulus 3681cb0ef41Sopenharmony_ci if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); } 3691cb0ef41Sopenharmony_ci else { pm.copyTo(y); pt.copyTo(r); } 3701cb0ef41Sopenharmony_ci var ys = y.t; 3711cb0ef41Sopenharmony_ci var y0 = y[ys-1]; 3721cb0ef41Sopenharmony_ci if(y0 == 0) return; 3731cb0ef41Sopenharmony_ci var yt = y0*(1<<this.F1)+((ys>1)?y[ys-2]>>this.F2:0); 3741cb0ef41Sopenharmony_ci var d1 = this.FV/yt, d2 = (1<<this.F1)/yt, e = 1<<this.F2; 3751cb0ef41Sopenharmony_ci var i = r.t, j = i-ys, t = (q==null)?nbi():q; 3761cb0ef41Sopenharmony_ci y.dlShiftTo(j,t); 3771cb0ef41Sopenharmony_ci if(r.compareTo(t) >= 0) { 3781cb0ef41Sopenharmony_ci r[r.t++] = 1; 3791cb0ef41Sopenharmony_ci r.subTo(t,r); 3801cb0ef41Sopenharmony_ci } 3811cb0ef41Sopenharmony_ci BigInteger.ONE.dlShiftTo(ys,t); 3821cb0ef41Sopenharmony_ci t.subTo(y,y); // "negative" y so we can replace sub with am later 3831cb0ef41Sopenharmony_ci while(y.t < ys) y[y.t++] = 0; 3841cb0ef41Sopenharmony_ci while(--j >= 0) { 3851cb0ef41Sopenharmony_ci // Estimate quotient digit 3861cb0ef41Sopenharmony_ci var qd = (r[--i]==y0)?this.DM:Math.floor(r[i]*d1+(r[i-1]+e)*d2); 3871cb0ef41Sopenharmony_ci if((r[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out 3881cb0ef41Sopenharmony_ci y.dlShiftTo(j,t); 3891cb0ef41Sopenharmony_ci r.subTo(t,r); 3901cb0ef41Sopenharmony_ci while(r[i] < --qd) r.subTo(t,r); 3911cb0ef41Sopenharmony_ci } 3921cb0ef41Sopenharmony_ci } 3931cb0ef41Sopenharmony_ci if(q != null) { 3941cb0ef41Sopenharmony_ci r.drShiftTo(ys,q); 3951cb0ef41Sopenharmony_ci if(ts != ms) BigInteger.ZERO.subTo(q,q); 3961cb0ef41Sopenharmony_ci } 3971cb0ef41Sopenharmony_ci r.t = ys; 3981cb0ef41Sopenharmony_ci r.clamp(); 3991cb0ef41Sopenharmony_ci if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder 4001cb0ef41Sopenharmony_ci if(ts < 0) BigInteger.ZERO.subTo(r,r); 4011cb0ef41Sopenharmony_ci } 4021cb0ef41Sopenharmony_ci 4031cb0ef41Sopenharmony_ci // (public) this mod a 4041cb0ef41Sopenharmony_ci function bnMod(a) { 4051cb0ef41Sopenharmony_ci var r = nbi(); 4061cb0ef41Sopenharmony_ci this.abs().divRemTo(a,null,r); 4071cb0ef41Sopenharmony_ci if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r); 4081cb0ef41Sopenharmony_ci return r; 4091cb0ef41Sopenharmony_ci } 4101cb0ef41Sopenharmony_ci 4111cb0ef41Sopenharmony_ci // Modular reduction using "classic" algorithm 4121cb0ef41Sopenharmony_ci function Classic(m) { this.m = m; } 4131cb0ef41Sopenharmony_ci function cConvert(x) { 4141cb0ef41Sopenharmony_ci if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m); 4151cb0ef41Sopenharmony_ci else return x; 4161cb0ef41Sopenharmony_ci } 4171cb0ef41Sopenharmony_ci function cRevert(x) { return x; } 4181cb0ef41Sopenharmony_ci function cReduce(x) { x.divRemTo(this.m,null,x); } 4191cb0ef41Sopenharmony_ci function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } 4201cb0ef41Sopenharmony_ci function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); } 4211cb0ef41Sopenharmony_ci 4221cb0ef41Sopenharmony_ci Classic.prototype.convert = cConvert; 4231cb0ef41Sopenharmony_ci Classic.prototype.revert = cRevert; 4241cb0ef41Sopenharmony_ci Classic.prototype.reduce = cReduce; 4251cb0ef41Sopenharmony_ci Classic.prototype.mulTo = cMulTo; 4261cb0ef41Sopenharmony_ci Classic.prototype.sqrTo = cSqrTo; 4271cb0ef41Sopenharmony_ci 4281cb0ef41Sopenharmony_ci // (protected) return "-1/this % 2^DB"; useful for Mont. reduction 4291cb0ef41Sopenharmony_ci // justification: 4301cb0ef41Sopenharmony_ci // xy == 1 (mod m) 4311cb0ef41Sopenharmony_ci // xy = 1+km 4321cb0ef41Sopenharmony_ci // xy(2-xy) = (1+km)(1-km) 4331cb0ef41Sopenharmony_ci // x[y(2-xy)] = 1-k^2m^2 4341cb0ef41Sopenharmony_ci // x[y(2-xy)] == 1 (mod m^2) 4351cb0ef41Sopenharmony_ci // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2 4361cb0ef41Sopenharmony_ci // should reduce x and y(2-xy) by m^2 at each step to keep size bounded. 4371cb0ef41Sopenharmony_ci // JS multiply "overflows" differently from C/C++, so care is needed here. 4381cb0ef41Sopenharmony_ci function bnpInvDigit() { 4391cb0ef41Sopenharmony_ci if(this.t < 1) return 0; 4401cb0ef41Sopenharmony_ci var x = this[0]; 4411cb0ef41Sopenharmony_ci if((x&1) == 0) return 0; 4421cb0ef41Sopenharmony_ci var y = x&3; // y == 1/x mod 2^2 4431cb0ef41Sopenharmony_ci y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4 4441cb0ef41Sopenharmony_ci y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8 4451cb0ef41Sopenharmony_ci y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16 4461cb0ef41Sopenharmony_ci // last step - calculate inverse mod DV directly; 4471cb0ef41Sopenharmony_ci // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints 4481cb0ef41Sopenharmony_ci y = (y*(2-x*y%this.DV))%this.DV; // y == 1/x mod 2^dbits 4491cb0ef41Sopenharmony_ci // we really want the negative inverse, and -DV < y < DV 4501cb0ef41Sopenharmony_ci return (y>0)?this.DV-y:-y; 4511cb0ef41Sopenharmony_ci } 4521cb0ef41Sopenharmony_ci 4531cb0ef41Sopenharmony_ci // Montgomery reduction 4541cb0ef41Sopenharmony_ci function Montgomery(m) { 4551cb0ef41Sopenharmony_ci this.m = m; 4561cb0ef41Sopenharmony_ci this.mp = m.invDigit(); 4571cb0ef41Sopenharmony_ci this.mpl = this.mp&0x7fff; 4581cb0ef41Sopenharmony_ci this.mph = this.mp>>15; 4591cb0ef41Sopenharmony_ci this.um = (1<<(m.DB-15))-1; 4601cb0ef41Sopenharmony_ci this.mt2 = 2*m.t; 4611cb0ef41Sopenharmony_ci } 4621cb0ef41Sopenharmony_ci 4631cb0ef41Sopenharmony_ci // xR mod m 4641cb0ef41Sopenharmony_ci function montConvert(x) { 4651cb0ef41Sopenharmony_ci var r = nbi(); 4661cb0ef41Sopenharmony_ci x.abs().dlShiftTo(this.m.t,r); 4671cb0ef41Sopenharmony_ci r.divRemTo(this.m,null,r); 4681cb0ef41Sopenharmony_ci if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r); 4691cb0ef41Sopenharmony_ci return r; 4701cb0ef41Sopenharmony_ci } 4711cb0ef41Sopenharmony_ci 4721cb0ef41Sopenharmony_ci // x/R mod m 4731cb0ef41Sopenharmony_ci function montRevert(x) { 4741cb0ef41Sopenharmony_ci var r = nbi(); 4751cb0ef41Sopenharmony_ci x.copyTo(r); 4761cb0ef41Sopenharmony_ci this.reduce(r); 4771cb0ef41Sopenharmony_ci return r; 4781cb0ef41Sopenharmony_ci } 4791cb0ef41Sopenharmony_ci 4801cb0ef41Sopenharmony_ci // x = x/R mod m (HAC 14.32) 4811cb0ef41Sopenharmony_ci function montReduce(x) { 4821cb0ef41Sopenharmony_ci while(x.t <= this.mt2) // pad x so am has enough room later 4831cb0ef41Sopenharmony_ci x[x.t++] = 0; 4841cb0ef41Sopenharmony_ci for(var i = 0; i < this.m.t; ++i) { 4851cb0ef41Sopenharmony_ci // faster way of calculating u0 = x[i]*mp mod DV 4861cb0ef41Sopenharmony_ci var j = x[i]&0x7fff; 4871cb0ef41Sopenharmony_ci var u0 = (j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.DM; 4881cb0ef41Sopenharmony_ci // use am to combine the multiply-shift-add into one call 4891cb0ef41Sopenharmony_ci j = i+this.m.t; 4901cb0ef41Sopenharmony_ci x[j] += this.m.am(0,u0,x,i,0,this.m.t); 4911cb0ef41Sopenharmony_ci // propagate carry 4921cb0ef41Sopenharmony_ci while(x[j] >= x.DV) { x[j] -= x.DV; x[++j]++; } 4931cb0ef41Sopenharmony_ci } 4941cb0ef41Sopenharmony_ci x.clamp(); 4951cb0ef41Sopenharmony_ci x.drShiftTo(this.m.t,x); 4961cb0ef41Sopenharmony_ci if(x.compareTo(this.m) >= 0) x.subTo(this.m,x); 4971cb0ef41Sopenharmony_ci } 4981cb0ef41Sopenharmony_ci 4991cb0ef41Sopenharmony_ci // r = "x^2/R mod m"; x != r 5001cb0ef41Sopenharmony_ci function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); } 5011cb0ef41Sopenharmony_ci 5021cb0ef41Sopenharmony_ci // r = "xy/R mod m"; x,y != r 5031cb0ef41Sopenharmony_ci function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } 5041cb0ef41Sopenharmony_ci 5051cb0ef41Sopenharmony_ci Montgomery.prototype.convert = montConvert; 5061cb0ef41Sopenharmony_ci Montgomery.prototype.revert = montRevert; 5071cb0ef41Sopenharmony_ci Montgomery.prototype.reduce = montReduce; 5081cb0ef41Sopenharmony_ci Montgomery.prototype.mulTo = montMulTo; 5091cb0ef41Sopenharmony_ci Montgomery.prototype.sqrTo = montSqrTo; 5101cb0ef41Sopenharmony_ci 5111cb0ef41Sopenharmony_ci // (protected) true iff this is even 5121cb0ef41Sopenharmony_ci function bnpIsEven() { return ((this.t>0)?(this[0]&1):this.s) == 0; } 5131cb0ef41Sopenharmony_ci 5141cb0ef41Sopenharmony_ci // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79) 5151cb0ef41Sopenharmony_ci function bnpExp(e,z) { 5161cb0ef41Sopenharmony_ci if(e > 0xffffffff || e < 1) return BigInteger.ONE; 5171cb0ef41Sopenharmony_ci var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1; 5181cb0ef41Sopenharmony_ci g.copyTo(r); 5191cb0ef41Sopenharmony_ci while(--i >= 0) { 5201cb0ef41Sopenharmony_ci z.sqrTo(r,r2); 5211cb0ef41Sopenharmony_ci if((e&(1<<i)) > 0) z.mulTo(r2,g,r); 5221cb0ef41Sopenharmony_ci else { var t = r; r = r2; r2 = t; } 5231cb0ef41Sopenharmony_ci } 5241cb0ef41Sopenharmony_ci return z.revert(r); 5251cb0ef41Sopenharmony_ci } 5261cb0ef41Sopenharmony_ci 5271cb0ef41Sopenharmony_ci // (public) this^e % m, 0 <= e < 2^32 5281cb0ef41Sopenharmony_ci function bnModPowInt(e,m) { 5291cb0ef41Sopenharmony_ci var z; 5301cb0ef41Sopenharmony_ci if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m); 5311cb0ef41Sopenharmony_ci return this.exp(e,z); 5321cb0ef41Sopenharmony_ci } 5331cb0ef41Sopenharmony_ci 5341cb0ef41Sopenharmony_ci // protected 5351cb0ef41Sopenharmony_ci BigInteger.prototype.copyTo = bnpCopyTo; 5361cb0ef41Sopenharmony_ci BigInteger.prototype.fromInt = bnpFromInt; 5371cb0ef41Sopenharmony_ci BigInteger.prototype.fromString = bnpFromString; 5381cb0ef41Sopenharmony_ci BigInteger.prototype.clamp = bnpClamp; 5391cb0ef41Sopenharmony_ci BigInteger.prototype.dlShiftTo = bnpDLShiftTo; 5401cb0ef41Sopenharmony_ci BigInteger.prototype.drShiftTo = bnpDRShiftTo; 5411cb0ef41Sopenharmony_ci BigInteger.prototype.lShiftTo = bnpLShiftTo; 5421cb0ef41Sopenharmony_ci BigInteger.prototype.rShiftTo = bnpRShiftTo; 5431cb0ef41Sopenharmony_ci BigInteger.prototype.subTo = bnpSubTo; 5441cb0ef41Sopenharmony_ci BigInteger.prototype.multiplyTo = bnpMultiplyTo; 5451cb0ef41Sopenharmony_ci BigInteger.prototype.squareTo = bnpSquareTo; 5461cb0ef41Sopenharmony_ci BigInteger.prototype.divRemTo = bnpDivRemTo; 5471cb0ef41Sopenharmony_ci BigInteger.prototype.invDigit = bnpInvDigit; 5481cb0ef41Sopenharmony_ci BigInteger.prototype.isEven = bnpIsEven; 5491cb0ef41Sopenharmony_ci BigInteger.prototype.exp = bnpExp; 5501cb0ef41Sopenharmony_ci 5511cb0ef41Sopenharmony_ci // public 5521cb0ef41Sopenharmony_ci BigInteger.prototype.toString = bnToString; 5531cb0ef41Sopenharmony_ci BigInteger.prototype.negate = bnNegate; 5541cb0ef41Sopenharmony_ci BigInteger.prototype.abs = bnAbs; 5551cb0ef41Sopenharmony_ci BigInteger.prototype.compareTo = bnCompareTo; 5561cb0ef41Sopenharmony_ci BigInteger.prototype.bitLength = bnBitLength; 5571cb0ef41Sopenharmony_ci BigInteger.prototype.mod = bnMod; 5581cb0ef41Sopenharmony_ci BigInteger.prototype.modPowInt = bnModPowInt; 5591cb0ef41Sopenharmony_ci 5601cb0ef41Sopenharmony_ci // "constants" 5611cb0ef41Sopenharmony_ci BigInteger.ZERO = nbv(0); 5621cb0ef41Sopenharmony_ci BigInteger.ONE = nbv(1); 5631cb0ef41Sopenharmony_ci 5641cb0ef41Sopenharmony_ci // Copyright (c) 2005-2009 Tom Wu 5651cb0ef41Sopenharmony_ci // All Rights Reserved. 5661cb0ef41Sopenharmony_ci // See "LICENSE" for details. 5671cb0ef41Sopenharmony_ci 5681cb0ef41Sopenharmony_ci // Extended JavaScript BN functions, required for RSA private ops. 5691cb0ef41Sopenharmony_ci 5701cb0ef41Sopenharmony_ci // Version 1.1: new BigInteger("0", 10) returns "proper" zero 5711cb0ef41Sopenharmony_ci // Version 1.2: square() API, isProbablePrime fix 5721cb0ef41Sopenharmony_ci 5731cb0ef41Sopenharmony_ci // (public) 5741cb0ef41Sopenharmony_ci function bnClone() { var r = nbi(); this.copyTo(r); return r; } 5751cb0ef41Sopenharmony_ci 5761cb0ef41Sopenharmony_ci // (public) return value as integer 5771cb0ef41Sopenharmony_ci function bnIntValue() { 5781cb0ef41Sopenharmony_ci if(this.s < 0) { 5791cb0ef41Sopenharmony_ci if(this.t == 1) return this[0]-this.DV; 5801cb0ef41Sopenharmony_ci else if(this.t == 0) return -1; 5811cb0ef41Sopenharmony_ci } 5821cb0ef41Sopenharmony_ci else if(this.t == 1) return this[0]; 5831cb0ef41Sopenharmony_ci else if(this.t == 0) return 0; 5841cb0ef41Sopenharmony_ci // assumes 16 < DB < 32 5851cb0ef41Sopenharmony_ci return ((this[1]&((1<<(32-this.DB))-1))<<this.DB)|this[0]; 5861cb0ef41Sopenharmony_ci } 5871cb0ef41Sopenharmony_ci 5881cb0ef41Sopenharmony_ci // (public) return value as byte 5891cb0ef41Sopenharmony_ci function bnByteValue() { return (this.t==0)?this.s:(this[0]<<24)>>24; } 5901cb0ef41Sopenharmony_ci 5911cb0ef41Sopenharmony_ci // (public) return value as short (assumes DB>=16) 5921cb0ef41Sopenharmony_ci function bnShortValue() { return (this.t==0)?this.s:(this[0]<<16)>>16; } 5931cb0ef41Sopenharmony_ci 5941cb0ef41Sopenharmony_ci // (protected) return x s.t. r^x < DV 5951cb0ef41Sopenharmony_ci function bnpChunkSize(r) { return Math.floor(Math.LN2*this.DB/Math.log(r)); } 5961cb0ef41Sopenharmony_ci 5971cb0ef41Sopenharmony_ci // (public) 0 if this == 0, 1 if this > 0 5981cb0ef41Sopenharmony_ci function bnSigNum() { 5991cb0ef41Sopenharmony_ci if(this.s < 0) return -1; 6001cb0ef41Sopenharmony_ci else if(this.t <= 0 || (this.t == 1 && this[0] <= 0)) return 0; 6011cb0ef41Sopenharmony_ci else return 1; 6021cb0ef41Sopenharmony_ci } 6031cb0ef41Sopenharmony_ci 6041cb0ef41Sopenharmony_ci // (protected) convert to radix string 6051cb0ef41Sopenharmony_ci function bnpToRadix(b) { 6061cb0ef41Sopenharmony_ci if(b == null) b = 10; 6071cb0ef41Sopenharmony_ci if(this.signum() == 0 || b < 2 || b > 36) return "0"; 6081cb0ef41Sopenharmony_ci var cs = this.chunkSize(b); 6091cb0ef41Sopenharmony_ci var a = Math.pow(b,cs); 6101cb0ef41Sopenharmony_ci var d = nbv(a), y = nbi(), z = nbi(), r = ""; 6111cb0ef41Sopenharmony_ci this.divRemTo(d,y,z); 6121cb0ef41Sopenharmony_ci while(y.signum() > 0) { 6131cb0ef41Sopenharmony_ci r = (a+z.intValue()).toString(b).substr(1) + r; 6141cb0ef41Sopenharmony_ci y.divRemTo(d,y,z); 6151cb0ef41Sopenharmony_ci } 6161cb0ef41Sopenharmony_ci return z.intValue().toString(b) + r; 6171cb0ef41Sopenharmony_ci } 6181cb0ef41Sopenharmony_ci 6191cb0ef41Sopenharmony_ci // (protected) convert from radix string 6201cb0ef41Sopenharmony_ci function bnpFromRadix(s,b) { 6211cb0ef41Sopenharmony_ci this.fromInt(0); 6221cb0ef41Sopenharmony_ci if(b == null) b = 10; 6231cb0ef41Sopenharmony_ci var cs = this.chunkSize(b); 6241cb0ef41Sopenharmony_ci var d = Math.pow(b,cs), mi = false, j = 0, w = 0; 6251cb0ef41Sopenharmony_ci for(var i = 0; i < s.length; ++i) { 6261cb0ef41Sopenharmony_ci var x = intAt(s,i); 6271cb0ef41Sopenharmony_ci if(x < 0) { 6281cb0ef41Sopenharmony_ci if(s.charAt(i) == "-" && this.signum() == 0) mi = true; 6291cb0ef41Sopenharmony_ci continue; 6301cb0ef41Sopenharmony_ci } 6311cb0ef41Sopenharmony_ci w = b*w+x; 6321cb0ef41Sopenharmony_ci if(++j >= cs) { 6331cb0ef41Sopenharmony_ci this.dMultiply(d); 6341cb0ef41Sopenharmony_ci this.dAddOffset(w,0); 6351cb0ef41Sopenharmony_ci j = 0; 6361cb0ef41Sopenharmony_ci w = 0; 6371cb0ef41Sopenharmony_ci } 6381cb0ef41Sopenharmony_ci } 6391cb0ef41Sopenharmony_ci if(j > 0) { 6401cb0ef41Sopenharmony_ci this.dMultiply(Math.pow(b,j)); 6411cb0ef41Sopenharmony_ci this.dAddOffset(w,0); 6421cb0ef41Sopenharmony_ci } 6431cb0ef41Sopenharmony_ci if(mi) BigInteger.ZERO.subTo(this,this); 6441cb0ef41Sopenharmony_ci } 6451cb0ef41Sopenharmony_ci 6461cb0ef41Sopenharmony_ci // (protected) alternate constructor 6471cb0ef41Sopenharmony_ci function bnpFromNumber(a,b,c) { 6481cb0ef41Sopenharmony_ci if("number" == typeof b) { 6491cb0ef41Sopenharmony_ci // new BigInteger(int,int,RNG) 6501cb0ef41Sopenharmony_ci if(a < 2) this.fromInt(1); 6511cb0ef41Sopenharmony_ci else { 6521cb0ef41Sopenharmony_ci this.fromNumber(a,c); 6531cb0ef41Sopenharmony_ci if(!this.testBit(a-1)) // force MSB set 6541cb0ef41Sopenharmony_ci this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this); 6551cb0ef41Sopenharmony_ci if(this.isEven()) this.dAddOffset(1,0); // force odd 6561cb0ef41Sopenharmony_ci while(!this.isProbablePrime(b)) { 6571cb0ef41Sopenharmony_ci this.dAddOffset(2,0); 6581cb0ef41Sopenharmony_ci if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this); 6591cb0ef41Sopenharmony_ci } 6601cb0ef41Sopenharmony_ci } 6611cb0ef41Sopenharmony_ci } 6621cb0ef41Sopenharmony_ci else { 6631cb0ef41Sopenharmony_ci // new BigInteger(int,RNG) 6641cb0ef41Sopenharmony_ci var x = new Array(), t = a&7; 6651cb0ef41Sopenharmony_ci x.length = (a>>3)+1; 6661cb0ef41Sopenharmony_ci b.nextBytes(x); 6671cb0ef41Sopenharmony_ci if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0; 6681cb0ef41Sopenharmony_ci this.fromString(x,256); 6691cb0ef41Sopenharmony_ci } 6701cb0ef41Sopenharmony_ci } 6711cb0ef41Sopenharmony_ci 6721cb0ef41Sopenharmony_ci // (public) convert to bigendian byte array 6731cb0ef41Sopenharmony_ci function bnToByteArray() { 6741cb0ef41Sopenharmony_ci var i = this.t, r = new Array(); 6751cb0ef41Sopenharmony_ci r[0] = this.s; 6761cb0ef41Sopenharmony_ci var p = this.DB-(i*this.DB)%8, d, k = 0; 6771cb0ef41Sopenharmony_ci if(i-- > 0) { 6781cb0ef41Sopenharmony_ci if(p < this.DB && (d = this[i]>>p) != (this.s&this.DM)>>p) 6791cb0ef41Sopenharmony_ci r[k++] = d|(this.s<<(this.DB-p)); 6801cb0ef41Sopenharmony_ci while(i >= 0) { 6811cb0ef41Sopenharmony_ci if(p < 8) { 6821cb0ef41Sopenharmony_ci d = (this[i]&((1<<p)-1))<<(8-p); 6831cb0ef41Sopenharmony_ci d |= this[--i]>>(p+=this.DB-8); 6841cb0ef41Sopenharmony_ci } 6851cb0ef41Sopenharmony_ci else { 6861cb0ef41Sopenharmony_ci d = (this[i]>>(p-=8))&0xff; 6871cb0ef41Sopenharmony_ci if(p <= 0) { p += this.DB; --i; } 6881cb0ef41Sopenharmony_ci } 6891cb0ef41Sopenharmony_ci if((d&0x80) != 0) d |= -256; 6901cb0ef41Sopenharmony_ci if(k == 0 && (this.s&0x80) != (d&0x80)) ++k; 6911cb0ef41Sopenharmony_ci if(k > 0 || d != this.s) r[k++] = d; 6921cb0ef41Sopenharmony_ci } 6931cb0ef41Sopenharmony_ci } 6941cb0ef41Sopenharmony_ci return r; 6951cb0ef41Sopenharmony_ci } 6961cb0ef41Sopenharmony_ci 6971cb0ef41Sopenharmony_ci function bnEquals(a) { return(this.compareTo(a)==0); } 6981cb0ef41Sopenharmony_ci function bnMin(a) { return(this.compareTo(a)<0)?this:a; } 6991cb0ef41Sopenharmony_ci function bnMax(a) { return(this.compareTo(a)>0)?this:a; } 7001cb0ef41Sopenharmony_ci 7011cb0ef41Sopenharmony_ci // (protected) r = this op a (bitwise) 7021cb0ef41Sopenharmony_ci function bnpBitwiseTo(a,op,r) { 7031cb0ef41Sopenharmony_ci var i, f, m = Math.min(a.t,this.t); 7041cb0ef41Sopenharmony_ci for(i = 0; i < m; ++i) r[i] = op(this[i],a[i]); 7051cb0ef41Sopenharmony_ci if(a.t < this.t) { 7061cb0ef41Sopenharmony_ci f = a.s&this.DM; 7071cb0ef41Sopenharmony_ci for(i = m; i < this.t; ++i) r[i] = op(this[i],f); 7081cb0ef41Sopenharmony_ci r.t = this.t; 7091cb0ef41Sopenharmony_ci } 7101cb0ef41Sopenharmony_ci else { 7111cb0ef41Sopenharmony_ci f = this.s&this.DM; 7121cb0ef41Sopenharmony_ci for(i = m; i < a.t; ++i) r[i] = op(f,a[i]); 7131cb0ef41Sopenharmony_ci r.t = a.t; 7141cb0ef41Sopenharmony_ci } 7151cb0ef41Sopenharmony_ci r.s = op(this.s,a.s); 7161cb0ef41Sopenharmony_ci r.clamp(); 7171cb0ef41Sopenharmony_ci } 7181cb0ef41Sopenharmony_ci 7191cb0ef41Sopenharmony_ci // (public) this & a 7201cb0ef41Sopenharmony_ci function op_and(x,y) { return x&y; } 7211cb0ef41Sopenharmony_ci function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; } 7221cb0ef41Sopenharmony_ci 7231cb0ef41Sopenharmony_ci // (public) this | a 7241cb0ef41Sopenharmony_ci function op_or(x,y) { return x|y; } 7251cb0ef41Sopenharmony_ci function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; } 7261cb0ef41Sopenharmony_ci 7271cb0ef41Sopenharmony_ci // (public) this ^ a 7281cb0ef41Sopenharmony_ci function op_xor(x,y) { return x^y; } 7291cb0ef41Sopenharmony_ci function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; } 7301cb0ef41Sopenharmony_ci 7311cb0ef41Sopenharmony_ci // (public) this & ~a 7321cb0ef41Sopenharmony_ci function op_andnot(x,y) { return x&~y; } 7331cb0ef41Sopenharmony_ci function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; } 7341cb0ef41Sopenharmony_ci 7351cb0ef41Sopenharmony_ci // (public) ~this 7361cb0ef41Sopenharmony_ci function bnNot() { 7371cb0ef41Sopenharmony_ci var r = nbi(); 7381cb0ef41Sopenharmony_ci for(var i = 0; i < this.t; ++i) r[i] = this.DM&~this[i]; 7391cb0ef41Sopenharmony_ci r.t = this.t; 7401cb0ef41Sopenharmony_ci r.s = ~this.s; 7411cb0ef41Sopenharmony_ci return r; 7421cb0ef41Sopenharmony_ci } 7431cb0ef41Sopenharmony_ci 7441cb0ef41Sopenharmony_ci // (public) this << n 7451cb0ef41Sopenharmony_ci function bnShiftLeft(n) { 7461cb0ef41Sopenharmony_ci var r = nbi(); 7471cb0ef41Sopenharmony_ci if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r); 7481cb0ef41Sopenharmony_ci return r; 7491cb0ef41Sopenharmony_ci } 7501cb0ef41Sopenharmony_ci 7511cb0ef41Sopenharmony_ci // (public) this >> n 7521cb0ef41Sopenharmony_ci function bnShiftRight(n) { 7531cb0ef41Sopenharmony_ci var r = nbi(); 7541cb0ef41Sopenharmony_ci if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r); 7551cb0ef41Sopenharmony_ci return r; 7561cb0ef41Sopenharmony_ci } 7571cb0ef41Sopenharmony_ci 7581cb0ef41Sopenharmony_ci // return index of lowest 1-bit in x, x < 2^31 7591cb0ef41Sopenharmony_ci function lbit(x) { 7601cb0ef41Sopenharmony_ci if(x == 0) return -1; 7611cb0ef41Sopenharmony_ci var r = 0; 7621cb0ef41Sopenharmony_ci if((x&0xffff) == 0) { x >>= 16; r += 16; } 7631cb0ef41Sopenharmony_ci if((x&0xff) == 0) { x >>= 8; r += 8; } 7641cb0ef41Sopenharmony_ci if((x&0xf) == 0) { x >>= 4; r += 4; } 7651cb0ef41Sopenharmony_ci if((x&3) == 0) { x >>= 2; r += 2; } 7661cb0ef41Sopenharmony_ci if((x&1) == 0) ++r; 7671cb0ef41Sopenharmony_ci return r; 7681cb0ef41Sopenharmony_ci } 7691cb0ef41Sopenharmony_ci 7701cb0ef41Sopenharmony_ci // (public) returns index of lowest 1-bit (or -1 if none) 7711cb0ef41Sopenharmony_ci function bnGetLowestSetBit() { 7721cb0ef41Sopenharmony_ci for(var i = 0; i < this.t; ++i) 7731cb0ef41Sopenharmony_ci if(this[i] != 0) return i*this.DB+lbit(this[i]); 7741cb0ef41Sopenharmony_ci if(this.s < 0) return this.t*this.DB; 7751cb0ef41Sopenharmony_ci return -1; 7761cb0ef41Sopenharmony_ci } 7771cb0ef41Sopenharmony_ci 7781cb0ef41Sopenharmony_ci // return number of 1 bits in x 7791cb0ef41Sopenharmony_ci function cbit(x) { 7801cb0ef41Sopenharmony_ci var r = 0; 7811cb0ef41Sopenharmony_ci while(x != 0) { x &= x-1; ++r; } 7821cb0ef41Sopenharmony_ci return r; 7831cb0ef41Sopenharmony_ci } 7841cb0ef41Sopenharmony_ci 7851cb0ef41Sopenharmony_ci // (public) return number of set bits 7861cb0ef41Sopenharmony_ci function bnBitCount() { 7871cb0ef41Sopenharmony_ci var r = 0, x = this.s&this.DM; 7881cb0ef41Sopenharmony_ci for(var i = 0; i < this.t; ++i) r += cbit(this[i]^x); 7891cb0ef41Sopenharmony_ci return r; 7901cb0ef41Sopenharmony_ci } 7911cb0ef41Sopenharmony_ci 7921cb0ef41Sopenharmony_ci // (public) true iff nth bit is set 7931cb0ef41Sopenharmony_ci function bnTestBit(n) { 7941cb0ef41Sopenharmony_ci var j = Math.floor(n/this.DB); 7951cb0ef41Sopenharmony_ci if(j >= this.t) return(this.s!=0); 7961cb0ef41Sopenharmony_ci return((this[j]&(1<<(n%this.DB)))!=0); 7971cb0ef41Sopenharmony_ci } 7981cb0ef41Sopenharmony_ci 7991cb0ef41Sopenharmony_ci // (protected) this op (1<<n) 8001cb0ef41Sopenharmony_ci function bnpChangeBit(n,op) { 8011cb0ef41Sopenharmony_ci var r = BigInteger.ONE.shiftLeft(n); 8021cb0ef41Sopenharmony_ci this.bitwiseTo(r,op,r); 8031cb0ef41Sopenharmony_ci return r; 8041cb0ef41Sopenharmony_ci } 8051cb0ef41Sopenharmony_ci 8061cb0ef41Sopenharmony_ci // (public) this | (1<<n) 8071cb0ef41Sopenharmony_ci function bnSetBit(n) { return this.changeBit(n,op_or); } 8081cb0ef41Sopenharmony_ci 8091cb0ef41Sopenharmony_ci // (public) this & ~(1<<n) 8101cb0ef41Sopenharmony_ci function bnClearBit(n) { return this.changeBit(n,op_andnot); } 8111cb0ef41Sopenharmony_ci 8121cb0ef41Sopenharmony_ci // (public) this ^ (1<<n) 8131cb0ef41Sopenharmony_ci function bnFlipBit(n) { return this.changeBit(n,op_xor); } 8141cb0ef41Sopenharmony_ci 8151cb0ef41Sopenharmony_ci // (protected) r = this + a 8161cb0ef41Sopenharmony_ci function bnpAddTo(a,r) { 8171cb0ef41Sopenharmony_ci var i = 0, c = 0, m = Math.min(a.t,this.t); 8181cb0ef41Sopenharmony_ci while(i < m) { 8191cb0ef41Sopenharmony_ci c += this[i]+a[i]; 8201cb0ef41Sopenharmony_ci r[i++] = c&this.DM; 8211cb0ef41Sopenharmony_ci c >>= this.DB; 8221cb0ef41Sopenharmony_ci } 8231cb0ef41Sopenharmony_ci if(a.t < this.t) { 8241cb0ef41Sopenharmony_ci c += a.s; 8251cb0ef41Sopenharmony_ci while(i < this.t) { 8261cb0ef41Sopenharmony_ci c += this[i]; 8271cb0ef41Sopenharmony_ci r[i++] = c&this.DM; 8281cb0ef41Sopenharmony_ci c >>= this.DB; 8291cb0ef41Sopenharmony_ci } 8301cb0ef41Sopenharmony_ci c += this.s; 8311cb0ef41Sopenharmony_ci } 8321cb0ef41Sopenharmony_ci else { 8331cb0ef41Sopenharmony_ci c += this.s; 8341cb0ef41Sopenharmony_ci while(i < a.t) { 8351cb0ef41Sopenharmony_ci c += a[i]; 8361cb0ef41Sopenharmony_ci r[i++] = c&this.DM; 8371cb0ef41Sopenharmony_ci c >>= this.DB; 8381cb0ef41Sopenharmony_ci } 8391cb0ef41Sopenharmony_ci c += a.s; 8401cb0ef41Sopenharmony_ci } 8411cb0ef41Sopenharmony_ci r.s = (c<0)?-1:0; 8421cb0ef41Sopenharmony_ci if(c > 0) r[i++] = c; 8431cb0ef41Sopenharmony_ci else if(c < -1) r[i++] = this.DV+c; 8441cb0ef41Sopenharmony_ci r.t = i; 8451cb0ef41Sopenharmony_ci r.clamp(); 8461cb0ef41Sopenharmony_ci } 8471cb0ef41Sopenharmony_ci 8481cb0ef41Sopenharmony_ci // (public) this + a 8491cb0ef41Sopenharmony_ci function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; } 8501cb0ef41Sopenharmony_ci 8511cb0ef41Sopenharmony_ci // (public) this - a 8521cb0ef41Sopenharmony_ci function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; } 8531cb0ef41Sopenharmony_ci 8541cb0ef41Sopenharmony_ci // (public) this * a 8551cb0ef41Sopenharmony_ci function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; } 8561cb0ef41Sopenharmony_ci 8571cb0ef41Sopenharmony_ci // (public) this^2 8581cb0ef41Sopenharmony_ci function bnSquare() { var r = nbi(); this.squareTo(r); return r; } 8591cb0ef41Sopenharmony_ci 8601cb0ef41Sopenharmony_ci // (public) this / a 8611cb0ef41Sopenharmony_ci function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; } 8621cb0ef41Sopenharmony_ci 8631cb0ef41Sopenharmony_ci // (public) this % a 8641cb0ef41Sopenharmony_ci function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; } 8651cb0ef41Sopenharmony_ci 8661cb0ef41Sopenharmony_ci // (public) [this/a,this%a] 8671cb0ef41Sopenharmony_ci function bnDivideAndRemainder(a) { 8681cb0ef41Sopenharmony_ci var q = nbi(), r = nbi(); 8691cb0ef41Sopenharmony_ci this.divRemTo(a,q,r); 8701cb0ef41Sopenharmony_ci return new Array(q,r); 8711cb0ef41Sopenharmony_ci } 8721cb0ef41Sopenharmony_ci 8731cb0ef41Sopenharmony_ci // (protected) this *= n, this >= 0, 1 < n < DV 8741cb0ef41Sopenharmony_ci function bnpDMultiply(n) { 8751cb0ef41Sopenharmony_ci this[this.t] = this.am(0,n-1,this,0,0,this.t); 8761cb0ef41Sopenharmony_ci ++this.t; 8771cb0ef41Sopenharmony_ci this.clamp(); 8781cb0ef41Sopenharmony_ci } 8791cb0ef41Sopenharmony_ci 8801cb0ef41Sopenharmony_ci // (protected) this += n << w words, this >= 0 8811cb0ef41Sopenharmony_ci function bnpDAddOffset(n,w) { 8821cb0ef41Sopenharmony_ci if(n == 0) return; 8831cb0ef41Sopenharmony_ci while(this.t <= w) this[this.t++] = 0; 8841cb0ef41Sopenharmony_ci this[w] += n; 8851cb0ef41Sopenharmony_ci while(this[w] >= this.DV) { 8861cb0ef41Sopenharmony_ci this[w] -= this.DV; 8871cb0ef41Sopenharmony_ci if(++w >= this.t) this[this.t++] = 0; 8881cb0ef41Sopenharmony_ci ++this[w]; 8891cb0ef41Sopenharmony_ci } 8901cb0ef41Sopenharmony_ci } 8911cb0ef41Sopenharmony_ci 8921cb0ef41Sopenharmony_ci // A "null" reducer 8931cb0ef41Sopenharmony_ci function NullExp() {} 8941cb0ef41Sopenharmony_ci function nNop(x) { return x; } 8951cb0ef41Sopenharmony_ci function nMulTo(x,y,r) { x.multiplyTo(y,r); } 8961cb0ef41Sopenharmony_ci function nSqrTo(x,r) { x.squareTo(r); } 8971cb0ef41Sopenharmony_ci 8981cb0ef41Sopenharmony_ci NullExp.prototype.convert = nNop; 8991cb0ef41Sopenharmony_ci NullExp.prototype.revert = nNop; 9001cb0ef41Sopenharmony_ci NullExp.prototype.mulTo = nMulTo; 9011cb0ef41Sopenharmony_ci NullExp.prototype.sqrTo = nSqrTo; 9021cb0ef41Sopenharmony_ci 9031cb0ef41Sopenharmony_ci // (public) this^e 9041cb0ef41Sopenharmony_ci function bnPow(e) { return this.exp(e,new NullExp()); } 9051cb0ef41Sopenharmony_ci 9061cb0ef41Sopenharmony_ci // (protected) r = lower n words of "this * a", a.t <= n 9071cb0ef41Sopenharmony_ci // "this" should be the larger one if appropriate. 9081cb0ef41Sopenharmony_ci function bnpMultiplyLowerTo(a,n,r) { 9091cb0ef41Sopenharmony_ci var i = Math.min(this.t+a.t,n); 9101cb0ef41Sopenharmony_ci r.s = 0; // assumes a,this >= 0 9111cb0ef41Sopenharmony_ci r.t = i; 9121cb0ef41Sopenharmony_ci while(i > 0) r[--i] = 0; 9131cb0ef41Sopenharmony_ci var j; 9141cb0ef41Sopenharmony_ci for(j = r.t-this.t; i < j; ++i) r[i+this.t] = this.am(0,a[i],r,i,0,this.t); 9151cb0ef41Sopenharmony_ci for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a[i],r,i,0,n-i); 9161cb0ef41Sopenharmony_ci r.clamp(); 9171cb0ef41Sopenharmony_ci } 9181cb0ef41Sopenharmony_ci 9191cb0ef41Sopenharmony_ci // (protected) r = "this * a" without lower n words, n > 0 9201cb0ef41Sopenharmony_ci // "this" should be the larger one if appropriate. 9211cb0ef41Sopenharmony_ci function bnpMultiplyUpperTo(a,n,r) { 9221cb0ef41Sopenharmony_ci --n; 9231cb0ef41Sopenharmony_ci var i = r.t = this.t+a.t-n; 9241cb0ef41Sopenharmony_ci r.s = 0; // assumes a,this >= 0 9251cb0ef41Sopenharmony_ci while(--i >= 0) r[i] = 0; 9261cb0ef41Sopenharmony_ci for(i = Math.max(n-this.t,0); i < a.t; ++i) 9271cb0ef41Sopenharmony_ci r[this.t+i-n] = this.am(n-i,a[i],r,0,0,this.t+i-n); 9281cb0ef41Sopenharmony_ci r.clamp(); 9291cb0ef41Sopenharmony_ci r.drShiftTo(1,r); 9301cb0ef41Sopenharmony_ci } 9311cb0ef41Sopenharmony_ci 9321cb0ef41Sopenharmony_ci // Barrett modular reduction 9331cb0ef41Sopenharmony_ci function Barrett(m) { 9341cb0ef41Sopenharmony_ci // setup Barrett 9351cb0ef41Sopenharmony_ci this.r2 = nbi(); 9361cb0ef41Sopenharmony_ci this.q3 = nbi(); 9371cb0ef41Sopenharmony_ci BigInteger.ONE.dlShiftTo(2*m.t,this.r2); 9381cb0ef41Sopenharmony_ci this.mu = this.r2.divide(m); 9391cb0ef41Sopenharmony_ci this.m = m; 9401cb0ef41Sopenharmony_ci } 9411cb0ef41Sopenharmony_ci 9421cb0ef41Sopenharmony_ci function barrettConvert(x) { 9431cb0ef41Sopenharmony_ci if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m); 9441cb0ef41Sopenharmony_ci else if(x.compareTo(this.m) < 0) return x; 9451cb0ef41Sopenharmony_ci else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; } 9461cb0ef41Sopenharmony_ci } 9471cb0ef41Sopenharmony_ci 9481cb0ef41Sopenharmony_ci function barrettRevert(x) { return x; } 9491cb0ef41Sopenharmony_ci 9501cb0ef41Sopenharmony_ci // x = x mod m (HAC 14.42) 9511cb0ef41Sopenharmony_ci function barrettReduce(x) { 9521cb0ef41Sopenharmony_ci x.drShiftTo(this.m.t-1,this.r2); 9531cb0ef41Sopenharmony_ci if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); } 9541cb0ef41Sopenharmony_ci this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3); 9551cb0ef41Sopenharmony_ci this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2); 9561cb0ef41Sopenharmony_ci while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1); 9571cb0ef41Sopenharmony_ci x.subTo(this.r2,x); 9581cb0ef41Sopenharmony_ci while(x.compareTo(this.m) >= 0) x.subTo(this.m,x); 9591cb0ef41Sopenharmony_ci } 9601cb0ef41Sopenharmony_ci 9611cb0ef41Sopenharmony_ci // r = x^2 mod m; x != r 9621cb0ef41Sopenharmony_ci function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); } 9631cb0ef41Sopenharmony_ci 9641cb0ef41Sopenharmony_ci // r = x*y mod m; x,y != r 9651cb0ef41Sopenharmony_ci function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } 9661cb0ef41Sopenharmony_ci 9671cb0ef41Sopenharmony_ci Barrett.prototype.convert = barrettConvert; 9681cb0ef41Sopenharmony_ci Barrett.prototype.revert = barrettRevert; 9691cb0ef41Sopenharmony_ci Barrett.prototype.reduce = barrettReduce; 9701cb0ef41Sopenharmony_ci Barrett.prototype.mulTo = barrettMulTo; 9711cb0ef41Sopenharmony_ci Barrett.prototype.sqrTo = barrettSqrTo; 9721cb0ef41Sopenharmony_ci 9731cb0ef41Sopenharmony_ci // (public) this^e % m (HAC 14.85) 9741cb0ef41Sopenharmony_ci function bnModPow(e,m) { 9751cb0ef41Sopenharmony_ci var i = e.bitLength(), k, r = nbv(1), z; 9761cb0ef41Sopenharmony_ci if(i <= 0) return r; 9771cb0ef41Sopenharmony_ci else if(i < 18) k = 1; 9781cb0ef41Sopenharmony_ci else if(i < 48) k = 3; 9791cb0ef41Sopenharmony_ci else if(i < 144) k = 4; 9801cb0ef41Sopenharmony_ci else if(i < 768) k = 5; 9811cb0ef41Sopenharmony_ci else k = 6; 9821cb0ef41Sopenharmony_ci if(i < 8) 9831cb0ef41Sopenharmony_ci z = new Classic(m); 9841cb0ef41Sopenharmony_ci else if(m.isEven()) 9851cb0ef41Sopenharmony_ci z = new Barrett(m); 9861cb0ef41Sopenharmony_ci else 9871cb0ef41Sopenharmony_ci z = new Montgomery(m); 9881cb0ef41Sopenharmony_ci 9891cb0ef41Sopenharmony_ci // precomputation 9901cb0ef41Sopenharmony_ci var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1; 9911cb0ef41Sopenharmony_ci g[1] = z.convert(this); 9921cb0ef41Sopenharmony_ci if(k > 1) { 9931cb0ef41Sopenharmony_ci var g2 = nbi(); 9941cb0ef41Sopenharmony_ci z.sqrTo(g[1],g2); 9951cb0ef41Sopenharmony_ci while(n <= km) { 9961cb0ef41Sopenharmony_ci g[n] = nbi(); 9971cb0ef41Sopenharmony_ci z.mulTo(g2,g[n-2],g[n]); 9981cb0ef41Sopenharmony_ci n += 2; 9991cb0ef41Sopenharmony_ci } 10001cb0ef41Sopenharmony_ci } 10011cb0ef41Sopenharmony_ci 10021cb0ef41Sopenharmony_ci var j = e.t-1, w, is1 = true, r2 = nbi(), t; 10031cb0ef41Sopenharmony_ci i = nbits(e[j])-1; 10041cb0ef41Sopenharmony_ci while(j >= 0) { 10051cb0ef41Sopenharmony_ci if(i >= k1) w = (e[j]>>(i-k1))&km; 10061cb0ef41Sopenharmony_ci else { 10071cb0ef41Sopenharmony_ci w = (e[j]&((1<<(i+1))-1))<<(k1-i); 10081cb0ef41Sopenharmony_ci if(j > 0) w |= e[j-1]>>(this.DB+i-k1); 10091cb0ef41Sopenharmony_ci } 10101cb0ef41Sopenharmony_ci 10111cb0ef41Sopenharmony_ci n = k; 10121cb0ef41Sopenharmony_ci while((w&1) == 0) { w >>= 1; --n; } 10131cb0ef41Sopenharmony_ci if((i -= n) < 0) { i += this.DB; --j; } 10141cb0ef41Sopenharmony_ci if(is1) { // ret == 1, don't bother squaring or multiplying it 10151cb0ef41Sopenharmony_ci g[w].copyTo(r); 10161cb0ef41Sopenharmony_ci is1 = false; 10171cb0ef41Sopenharmony_ci } 10181cb0ef41Sopenharmony_ci else { 10191cb0ef41Sopenharmony_ci while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; } 10201cb0ef41Sopenharmony_ci if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; } 10211cb0ef41Sopenharmony_ci z.mulTo(r2,g[w],r); 10221cb0ef41Sopenharmony_ci } 10231cb0ef41Sopenharmony_ci 10241cb0ef41Sopenharmony_ci while(j >= 0 && (e[j]&(1<<i)) == 0) { 10251cb0ef41Sopenharmony_ci z.sqrTo(r,r2); t = r; r = r2; r2 = t; 10261cb0ef41Sopenharmony_ci if(--i < 0) { i = this.DB-1; --j; } 10271cb0ef41Sopenharmony_ci } 10281cb0ef41Sopenharmony_ci } 10291cb0ef41Sopenharmony_ci return z.revert(r); 10301cb0ef41Sopenharmony_ci } 10311cb0ef41Sopenharmony_ci 10321cb0ef41Sopenharmony_ci // (public) gcd(this,a) (HAC 14.54) 10331cb0ef41Sopenharmony_ci function bnGCD(a) { 10341cb0ef41Sopenharmony_ci var x = (this.s<0)?this.negate():this.clone(); 10351cb0ef41Sopenharmony_ci var y = (a.s<0)?a.negate():a.clone(); 10361cb0ef41Sopenharmony_ci if(x.compareTo(y) < 0) { var t = x; x = y; y = t; } 10371cb0ef41Sopenharmony_ci var i = x.getLowestSetBit(), g = y.getLowestSetBit(); 10381cb0ef41Sopenharmony_ci if(g < 0) return x; 10391cb0ef41Sopenharmony_ci if(i < g) g = i; 10401cb0ef41Sopenharmony_ci if(g > 0) { 10411cb0ef41Sopenharmony_ci x.rShiftTo(g,x); 10421cb0ef41Sopenharmony_ci y.rShiftTo(g,y); 10431cb0ef41Sopenharmony_ci } 10441cb0ef41Sopenharmony_ci while(x.signum() > 0) { 10451cb0ef41Sopenharmony_ci if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x); 10461cb0ef41Sopenharmony_ci if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y); 10471cb0ef41Sopenharmony_ci if(x.compareTo(y) >= 0) { 10481cb0ef41Sopenharmony_ci x.subTo(y,x); 10491cb0ef41Sopenharmony_ci x.rShiftTo(1,x); 10501cb0ef41Sopenharmony_ci } 10511cb0ef41Sopenharmony_ci else { 10521cb0ef41Sopenharmony_ci y.subTo(x,y); 10531cb0ef41Sopenharmony_ci y.rShiftTo(1,y); 10541cb0ef41Sopenharmony_ci } 10551cb0ef41Sopenharmony_ci } 10561cb0ef41Sopenharmony_ci if(g > 0) y.lShiftTo(g,y); 10571cb0ef41Sopenharmony_ci return y; 10581cb0ef41Sopenharmony_ci } 10591cb0ef41Sopenharmony_ci 10601cb0ef41Sopenharmony_ci // (protected) this % n, n < 2^26 10611cb0ef41Sopenharmony_ci function bnpModInt(n) { 10621cb0ef41Sopenharmony_ci if(n <= 0) return 0; 10631cb0ef41Sopenharmony_ci var d = this.DV%n, r = (this.s<0)?n-1:0; 10641cb0ef41Sopenharmony_ci if(this.t > 0) 10651cb0ef41Sopenharmony_ci if(d == 0) r = this[0]%n; 10661cb0ef41Sopenharmony_ci else for(var i = this.t-1; i >= 0; --i) r = (d*r+this[i])%n; 10671cb0ef41Sopenharmony_ci return r; 10681cb0ef41Sopenharmony_ci } 10691cb0ef41Sopenharmony_ci 10701cb0ef41Sopenharmony_ci // (public) 1/this % m (HAC 14.61) 10711cb0ef41Sopenharmony_ci function bnModInverse(m) { 10721cb0ef41Sopenharmony_ci var ac = m.isEven(); 10731cb0ef41Sopenharmony_ci if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO; 10741cb0ef41Sopenharmony_ci var u = m.clone(), v = this.clone(); 10751cb0ef41Sopenharmony_ci var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1); 10761cb0ef41Sopenharmony_ci while(u.signum() != 0) { 10771cb0ef41Sopenharmony_ci while(u.isEven()) { 10781cb0ef41Sopenharmony_ci u.rShiftTo(1,u); 10791cb0ef41Sopenharmony_ci if(ac) { 10801cb0ef41Sopenharmony_ci if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); } 10811cb0ef41Sopenharmony_ci a.rShiftTo(1,a); 10821cb0ef41Sopenharmony_ci } 10831cb0ef41Sopenharmony_ci else if(!b.isEven()) b.subTo(m,b); 10841cb0ef41Sopenharmony_ci b.rShiftTo(1,b); 10851cb0ef41Sopenharmony_ci } 10861cb0ef41Sopenharmony_ci while(v.isEven()) { 10871cb0ef41Sopenharmony_ci v.rShiftTo(1,v); 10881cb0ef41Sopenharmony_ci if(ac) { 10891cb0ef41Sopenharmony_ci if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); } 10901cb0ef41Sopenharmony_ci c.rShiftTo(1,c); 10911cb0ef41Sopenharmony_ci } 10921cb0ef41Sopenharmony_ci else if(!d.isEven()) d.subTo(m,d); 10931cb0ef41Sopenharmony_ci d.rShiftTo(1,d); 10941cb0ef41Sopenharmony_ci } 10951cb0ef41Sopenharmony_ci if(u.compareTo(v) >= 0) { 10961cb0ef41Sopenharmony_ci u.subTo(v,u); 10971cb0ef41Sopenharmony_ci if(ac) a.subTo(c,a); 10981cb0ef41Sopenharmony_ci b.subTo(d,b); 10991cb0ef41Sopenharmony_ci } 11001cb0ef41Sopenharmony_ci else { 11011cb0ef41Sopenharmony_ci v.subTo(u,v); 11021cb0ef41Sopenharmony_ci if(ac) c.subTo(a,c); 11031cb0ef41Sopenharmony_ci d.subTo(b,d); 11041cb0ef41Sopenharmony_ci } 11051cb0ef41Sopenharmony_ci } 11061cb0ef41Sopenharmony_ci if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO; 11071cb0ef41Sopenharmony_ci if(d.compareTo(m) >= 0) return d.subtract(m); 11081cb0ef41Sopenharmony_ci if(d.signum() < 0) d.addTo(m,d); else return d; 11091cb0ef41Sopenharmony_ci if(d.signum() < 0) return d.add(m); else return d; 11101cb0ef41Sopenharmony_ci } 11111cb0ef41Sopenharmony_ci 11121cb0ef41Sopenharmony_ci 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]; 11131cb0ef41Sopenharmony_ci var lplim = (1<<26)/lowprimes[lowprimes.length-1]; 11141cb0ef41Sopenharmony_ci 11151cb0ef41Sopenharmony_ci // (public) test primality with certainty >= 1-.5^t 11161cb0ef41Sopenharmony_ci function bnIsProbablePrime(t) { 11171cb0ef41Sopenharmony_ci var i, x = this.abs(); 11181cb0ef41Sopenharmony_ci if(x.t == 1 && x[0] <= lowprimes[lowprimes.length-1]) { 11191cb0ef41Sopenharmony_ci for(i = 0; i < lowprimes.length; ++i) 11201cb0ef41Sopenharmony_ci if(x[0] == lowprimes[i]) return true; 11211cb0ef41Sopenharmony_ci return false; 11221cb0ef41Sopenharmony_ci } 11231cb0ef41Sopenharmony_ci if(x.isEven()) return false; 11241cb0ef41Sopenharmony_ci i = 1; 11251cb0ef41Sopenharmony_ci while(i < lowprimes.length) { 11261cb0ef41Sopenharmony_ci var m = lowprimes[i], j = i+1; 11271cb0ef41Sopenharmony_ci while(j < lowprimes.length && m < lplim) m *= lowprimes[j++]; 11281cb0ef41Sopenharmony_ci m = x.modInt(m); 11291cb0ef41Sopenharmony_ci while(i < j) if(m%lowprimes[i++] == 0) return false; 11301cb0ef41Sopenharmony_ci } 11311cb0ef41Sopenharmony_ci return x.millerRabin(t); 11321cb0ef41Sopenharmony_ci } 11331cb0ef41Sopenharmony_ci 11341cb0ef41Sopenharmony_ci // (protected) true if probably prime (HAC 4.24, Miller-Rabin) 11351cb0ef41Sopenharmony_ci function bnpMillerRabin(t) { 11361cb0ef41Sopenharmony_ci var n1 = this.subtract(BigInteger.ONE); 11371cb0ef41Sopenharmony_ci var k = n1.getLowestSetBit(); 11381cb0ef41Sopenharmony_ci if(k <= 0) return false; 11391cb0ef41Sopenharmony_ci var r = n1.shiftRight(k); 11401cb0ef41Sopenharmony_ci t = (t+1)>>1; 11411cb0ef41Sopenharmony_ci if(t > lowprimes.length) t = lowprimes.length; 11421cb0ef41Sopenharmony_ci var a = nbi(); 11431cb0ef41Sopenharmony_ci for(var i = 0; i < t; ++i) { 11441cb0ef41Sopenharmony_ci //Pick bases at random, instead of starting at 2 11451cb0ef41Sopenharmony_ci a.fromInt(lowprimes[Math.floor(Math.random()*lowprimes.length)]); 11461cb0ef41Sopenharmony_ci var y = a.modPow(r,this); 11471cb0ef41Sopenharmony_ci if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) { 11481cb0ef41Sopenharmony_ci var j = 1; 11491cb0ef41Sopenharmony_ci while(j++ < k && y.compareTo(n1) != 0) { 11501cb0ef41Sopenharmony_ci y = y.modPowInt(2,this); 11511cb0ef41Sopenharmony_ci if(y.compareTo(BigInteger.ONE) == 0) return false; 11521cb0ef41Sopenharmony_ci } 11531cb0ef41Sopenharmony_ci if(y.compareTo(n1) != 0) return false; 11541cb0ef41Sopenharmony_ci } 11551cb0ef41Sopenharmony_ci } 11561cb0ef41Sopenharmony_ci return true; 11571cb0ef41Sopenharmony_ci } 11581cb0ef41Sopenharmony_ci 11591cb0ef41Sopenharmony_ci // protected 11601cb0ef41Sopenharmony_ci BigInteger.prototype.chunkSize = bnpChunkSize; 11611cb0ef41Sopenharmony_ci BigInteger.prototype.toRadix = bnpToRadix; 11621cb0ef41Sopenharmony_ci BigInteger.prototype.fromRadix = bnpFromRadix; 11631cb0ef41Sopenharmony_ci BigInteger.prototype.fromNumber = bnpFromNumber; 11641cb0ef41Sopenharmony_ci BigInteger.prototype.bitwiseTo = bnpBitwiseTo; 11651cb0ef41Sopenharmony_ci BigInteger.prototype.changeBit = bnpChangeBit; 11661cb0ef41Sopenharmony_ci BigInteger.prototype.addTo = bnpAddTo; 11671cb0ef41Sopenharmony_ci BigInteger.prototype.dMultiply = bnpDMultiply; 11681cb0ef41Sopenharmony_ci BigInteger.prototype.dAddOffset = bnpDAddOffset; 11691cb0ef41Sopenharmony_ci BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo; 11701cb0ef41Sopenharmony_ci BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo; 11711cb0ef41Sopenharmony_ci BigInteger.prototype.modInt = bnpModInt; 11721cb0ef41Sopenharmony_ci BigInteger.prototype.millerRabin = bnpMillerRabin; 11731cb0ef41Sopenharmony_ci 11741cb0ef41Sopenharmony_ci // public 11751cb0ef41Sopenharmony_ci BigInteger.prototype.clone = bnClone; 11761cb0ef41Sopenharmony_ci BigInteger.prototype.intValue = bnIntValue; 11771cb0ef41Sopenharmony_ci BigInteger.prototype.byteValue = bnByteValue; 11781cb0ef41Sopenharmony_ci BigInteger.prototype.shortValue = bnShortValue; 11791cb0ef41Sopenharmony_ci BigInteger.prototype.signum = bnSigNum; 11801cb0ef41Sopenharmony_ci BigInteger.prototype.toByteArray = bnToByteArray; 11811cb0ef41Sopenharmony_ci BigInteger.prototype.equals = bnEquals; 11821cb0ef41Sopenharmony_ci BigInteger.prototype.min = bnMin; 11831cb0ef41Sopenharmony_ci BigInteger.prototype.max = bnMax; 11841cb0ef41Sopenharmony_ci BigInteger.prototype.and = bnAnd; 11851cb0ef41Sopenharmony_ci BigInteger.prototype.or = bnOr; 11861cb0ef41Sopenharmony_ci BigInteger.prototype.xor = bnXor; 11871cb0ef41Sopenharmony_ci BigInteger.prototype.andNot = bnAndNot; 11881cb0ef41Sopenharmony_ci BigInteger.prototype.not = bnNot; 11891cb0ef41Sopenharmony_ci BigInteger.prototype.shiftLeft = bnShiftLeft; 11901cb0ef41Sopenharmony_ci BigInteger.prototype.shiftRight = bnShiftRight; 11911cb0ef41Sopenharmony_ci BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit; 11921cb0ef41Sopenharmony_ci BigInteger.prototype.bitCount = bnBitCount; 11931cb0ef41Sopenharmony_ci BigInteger.prototype.testBit = bnTestBit; 11941cb0ef41Sopenharmony_ci BigInteger.prototype.setBit = bnSetBit; 11951cb0ef41Sopenharmony_ci BigInteger.prototype.clearBit = bnClearBit; 11961cb0ef41Sopenharmony_ci BigInteger.prototype.flipBit = bnFlipBit; 11971cb0ef41Sopenharmony_ci BigInteger.prototype.add = bnAdd; 11981cb0ef41Sopenharmony_ci BigInteger.prototype.subtract = bnSubtract; 11991cb0ef41Sopenharmony_ci BigInteger.prototype.multiply = bnMultiply; 12001cb0ef41Sopenharmony_ci BigInteger.prototype.divide = bnDivide; 12011cb0ef41Sopenharmony_ci BigInteger.prototype.remainder = bnRemainder; 12021cb0ef41Sopenharmony_ci BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder; 12031cb0ef41Sopenharmony_ci BigInteger.prototype.modPow = bnModPow; 12041cb0ef41Sopenharmony_ci BigInteger.prototype.modInverse = bnModInverse; 12051cb0ef41Sopenharmony_ci BigInteger.prototype.pow = bnPow; 12061cb0ef41Sopenharmony_ci BigInteger.prototype.gcd = bnGCD; 12071cb0ef41Sopenharmony_ci BigInteger.prototype.isProbablePrime = bnIsProbablePrime; 12081cb0ef41Sopenharmony_ci 12091cb0ef41Sopenharmony_ci // JSBN-specific extension 12101cb0ef41Sopenharmony_ci BigInteger.prototype.square = bnSquare; 12111cb0ef41Sopenharmony_ci 12121cb0ef41Sopenharmony_ci // Expose the Barrett function 12131cb0ef41Sopenharmony_ci BigInteger.prototype.Barrett = Barrett 12141cb0ef41Sopenharmony_ci 12151cb0ef41Sopenharmony_ci // BigInteger interfaces not implemented in jsbn: 12161cb0ef41Sopenharmony_ci 12171cb0ef41Sopenharmony_ci // BigInteger(int signum, byte[] magnitude) 12181cb0ef41Sopenharmony_ci // double doubleValue() 12191cb0ef41Sopenharmony_ci // float floatValue() 12201cb0ef41Sopenharmony_ci // int hashCode() 12211cb0ef41Sopenharmony_ci // long longValue() 12221cb0ef41Sopenharmony_ci // static BigInteger valueOf(long val) 12231cb0ef41Sopenharmony_ci 12241cb0ef41Sopenharmony_ci // Random number generator - requires a PRNG backend, e.g. prng4.js 12251cb0ef41Sopenharmony_ci 12261cb0ef41Sopenharmony_ci // For best results, put code like 12271cb0ef41Sopenharmony_ci // <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'> 12281cb0ef41Sopenharmony_ci // in your main HTML document. 12291cb0ef41Sopenharmony_ci 12301cb0ef41Sopenharmony_ci var rng_state; 12311cb0ef41Sopenharmony_ci var rng_pool; 12321cb0ef41Sopenharmony_ci var rng_pptr; 12331cb0ef41Sopenharmony_ci 12341cb0ef41Sopenharmony_ci // Mix in a 32-bit integer into the pool 12351cb0ef41Sopenharmony_ci function rng_seed_int(x) { 12361cb0ef41Sopenharmony_ci rng_pool[rng_pptr++] ^= x & 255; 12371cb0ef41Sopenharmony_ci rng_pool[rng_pptr++] ^= (x >> 8) & 255; 12381cb0ef41Sopenharmony_ci rng_pool[rng_pptr++] ^= (x >> 16) & 255; 12391cb0ef41Sopenharmony_ci rng_pool[rng_pptr++] ^= (x >> 24) & 255; 12401cb0ef41Sopenharmony_ci if(rng_pptr >= rng_psize) rng_pptr -= rng_psize; 12411cb0ef41Sopenharmony_ci } 12421cb0ef41Sopenharmony_ci 12431cb0ef41Sopenharmony_ci // Mix in the current time (w/milliseconds) into the pool 12441cb0ef41Sopenharmony_ci function rng_seed_time() { 12451cb0ef41Sopenharmony_ci rng_seed_int(new Date().getTime()); 12461cb0ef41Sopenharmony_ci } 12471cb0ef41Sopenharmony_ci 12481cb0ef41Sopenharmony_ci // Initialize the pool with junk if needed. 12491cb0ef41Sopenharmony_ci if(rng_pool == null) { 12501cb0ef41Sopenharmony_ci rng_pool = new Array(); 12511cb0ef41Sopenharmony_ci rng_pptr = 0; 12521cb0ef41Sopenharmony_ci var t; 12531cb0ef41Sopenharmony_ci if(typeof window !== "undefined" && window.crypto) { 12541cb0ef41Sopenharmony_ci if (window.crypto.getRandomValues) { 12551cb0ef41Sopenharmony_ci // Use webcrypto if available 12561cb0ef41Sopenharmony_ci var ua = new Uint8Array(32); 12571cb0ef41Sopenharmony_ci window.crypto.getRandomValues(ua); 12581cb0ef41Sopenharmony_ci for(t = 0; t < 32; ++t) 12591cb0ef41Sopenharmony_ci rng_pool[rng_pptr++] = ua[t]; 12601cb0ef41Sopenharmony_ci } 12611cb0ef41Sopenharmony_ci else if(navigator.appName == "Netscape" && navigator.appVersion < "5") { 12621cb0ef41Sopenharmony_ci // Extract entropy (256 bits) from NS4 RNG if available 12631cb0ef41Sopenharmony_ci var z = window.crypto.random(32); 12641cb0ef41Sopenharmony_ci for(t = 0; t < z.length; ++t) 12651cb0ef41Sopenharmony_ci rng_pool[rng_pptr++] = z.charCodeAt(t) & 255; 12661cb0ef41Sopenharmony_ci } 12671cb0ef41Sopenharmony_ci } 12681cb0ef41Sopenharmony_ci while(rng_pptr < rng_psize) { // extract some randomness from Math.random() 12691cb0ef41Sopenharmony_ci t = Math.floor(65536 * Math.random()); 12701cb0ef41Sopenharmony_ci rng_pool[rng_pptr++] = t >>> 8; 12711cb0ef41Sopenharmony_ci rng_pool[rng_pptr++] = t & 255; 12721cb0ef41Sopenharmony_ci } 12731cb0ef41Sopenharmony_ci rng_pptr = 0; 12741cb0ef41Sopenharmony_ci rng_seed_time(); 12751cb0ef41Sopenharmony_ci //rng_seed_int(window.screenX); 12761cb0ef41Sopenharmony_ci //rng_seed_int(window.screenY); 12771cb0ef41Sopenharmony_ci } 12781cb0ef41Sopenharmony_ci 12791cb0ef41Sopenharmony_ci function rng_get_byte() { 12801cb0ef41Sopenharmony_ci if(rng_state == null) { 12811cb0ef41Sopenharmony_ci rng_seed_time(); 12821cb0ef41Sopenharmony_ci rng_state = prng_newstate(); 12831cb0ef41Sopenharmony_ci rng_state.init(rng_pool); 12841cb0ef41Sopenharmony_ci for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr) 12851cb0ef41Sopenharmony_ci rng_pool[rng_pptr] = 0; 12861cb0ef41Sopenharmony_ci rng_pptr = 0; 12871cb0ef41Sopenharmony_ci //rng_pool = null; 12881cb0ef41Sopenharmony_ci } 12891cb0ef41Sopenharmony_ci // TODO: allow reseeding after first request 12901cb0ef41Sopenharmony_ci return rng_state.next(); 12911cb0ef41Sopenharmony_ci } 12921cb0ef41Sopenharmony_ci 12931cb0ef41Sopenharmony_ci function rng_get_bytes(ba) { 12941cb0ef41Sopenharmony_ci var i; 12951cb0ef41Sopenharmony_ci for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte(); 12961cb0ef41Sopenharmony_ci } 12971cb0ef41Sopenharmony_ci 12981cb0ef41Sopenharmony_ci function SecureRandom() {} 12991cb0ef41Sopenharmony_ci 13001cb0ef41Sopenharmony_ci SecureRandom.prototype.nextBytes = rng_get_bytes; 13011cb0ef41Sopenharmony_ci 13021cb0ef41Sopenharmony_ci // prng4.js - uses Arcfour as a PRNG 13031cb0ef41Sopenharmony_ci 13041cb0ef41Sopenharmony_ci function Arcfour() { 13051cb0ef41Sopenharmony_ci this.i = 0; 13061cb0ef41Sopenharmony_ci this.j = 0; 13071cb0ef41Sopenharmony_ci this.S = new Array(); 13081cb0ef41Sopenharmony_ci } 13091cb0ef41Sopenharmony_ci 13101cb0ef41Sopenharmony_ci // Initialize arcfour context from key, an array of ints, each from [0..255] 13111cb0ef41Sopenharmony_ci function ARC4init(key) { 13121cb0ef41Sopenharmony_ci var i, j, t; 13131cb0ef41Sopenharmony_ci for(i = 0; i < 256; ++i) 13141cb0ef41Sopenharmony_ci this.S[i] = i; 13151cb0ef41Sopenharmony_ci j = 0; 13161cb0ef41Sopenharmony_ci for(i = 0; i < 256; ++i) { 13171cb0ef41Sopenharmony_ci j = (j + this.S[i] + key[i % key.length]) & 255; 13181cb0ef41Sopenharmony_ci t = this.S[i]; 13191cb0ef41Sopenharmony_ci this.S[i] = this.S[j]; 13201cb0ef41Sopenharmony_ci this.S[j] = t; 13211cb0ef41Sopenharmony_ci } 13221cb0ef41Sopenharmony_ci this.i = 0; 13231cb0ef41Sopenharmony_ci this.j = 0; 13241cb0ef41Sopenharmony_ci } 13251cb0ef41Sopenharmony_ci 13261cb0ef41Sopenharmony_ci function ARC4next() { 13271cb0ef41Sopenharmony_ci var t; 13281cb0ef41Sopenharmony_ci this.i = (this.i + 1) & 255; 13291cb0ef41Sopenharmony_ci this.j = (this.j + this.S[this.i]) & 255; 13301cb0ef41Sopenharmony_ci t = this.S[this.i]; 13311cb0ef41Sopenharmony_ci this.S[this.i] = this.S[this.j]; 13321cb0ef41Sopenharmony_ci this.S[this.j] = t; 13331cb0ef41Sopenharmony_ci return this.S[(t + this.S[this.i]) & 255]; 13341cb0ef41Sopenharmony_ci } 13351cb0ef41Sopenharmony_ci 13361cb0ef41Sopenharmony_ci Arcfour.prototype.init = ARC4init; 13371cb0ef41Sopenharmony_ci Arcfour.prototype.next = ARC4next; 13381cb0ef41Sopenharmony_ci 13391cb0ef41Sopenharmony_ci // Plug in your RNG constructor here 13401cb0ef41Sopenharmony_ci function prng_newstate() { 13411cb0ef41Sopenharmony_ci return new Arcfour(); 13421cb0ef41Sopenharmony_ci } 13431cb0ef41Sopenharmony_ci 13441cb0ef41Sopenharmony_ci // Pool size must be a multiple of 4 and greater than 32. 13451cb0ef41Sopenharmony_ci // An array of bytes the size of the pool will be passed to init() 13461cb0ef41Sopenharmony_ci var rng_psize = 256; 13471cb0ef41Sopenharmony_ci 13481cb0ef41Sopenharmony_ci if (typeof exports !== 'undefined') { 13491cb0ef41Sopenharmony_ci exports = module.exports = { 13501cb0ef41Sopenharmony_ci default: BigInteger, 13511cb0ef41Sopenharmony_ci BigInteger: BigInteger, 13521cb0ef41Sopenharmony_ci SecureRandom: SecureRandom, 13531cb0ef41Sopenharmony_ci }; 13541cb0ef41Sopenharmony_ci } else { 13551cb0ef41Sopenharmony_ci this.jsbn = { 13561cb0ef41Sopenharmony_ci BigInteger: BigInteger, 13571cb0ef41Sopenharmony_ci SecureRandom: SecureRandom 13581cb0ef41Sopenharmony_ci }; 13591cb0ef41Sopenharmony_ci } 13601cb0ef41Sopenharmony_ci 13611cb0ef41Sopenharmony_ci}).call(this); 1362