jsbn.js 34 KB

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