jsbn.js 41 KB

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