人民医院前端

jsbn.js 52KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  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. import { cbit, int2char, lbit, op_and, op_andnot, op_or, op_xor } from "./util";
  6. // Bits per digit
  7. var dbits;
  8. // JavaScript engine analysis
  9. var canary = 0xdeadbeefcafe;
  10. var j_lm = ((canary & 0xffffff) == 0xefcafe);
  11. //#region
  12. 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];
  13. var lplim = (1 << 26) / lowprimes[lowprimes.length - 1];
  14. //#endregion
  15. // (public) Constructor
  16. var BigInteger = /** @class */ (function () {
  17. function BigInteger(a, b, c) {
  18. if (a != null) {
  19. if ("number" == typeof a) {
  20. this.fromNumber(a, b, c);
  21. }
  22. else if (b == null && "string" != typeof a) {
  23. this.fromString(a, 256);
  24. }
  25. else {
  26. this.fromString(a, b);
  27. }
  28. }
  29. }
  30. //#region PUBLIC
  31. // BigInteger.prototype.toString = bnToString;
  32. // (public) return string representation in given radix
  33. BigInteger.prototype.toString = function (b) {
  34. if (this.s < 0) {
  35. return "-" + this.negate().toString(b);
  36. }
  37. var k;
  38. if (b == 16) {
  39. k = 4;
  40. }
  41. else if (b == 8) {
  42. k = 3;
  43. }
  44. else if (b == 2) {
  45. k = 1;
  46. }
  47. else if (b == 32) {
  48. k = 5;
  49. }
  50. else if (b == 4) {
  51. k = 2;
  52. }
  53. else {
  54. return this.toRadix(b);
  55. }
  56. var km = (1 << k) - 1;
  57. var d;
  58. var m = false;
  59. var r = "";
  60. var i = this.t;
  61. var p = this.DB - (i * this.DB) % k;
  62. if (i-- > 0) {
  63. if (p < this.DB && (d = this[i] >> p) > 0) {
  64. m = true;
  65. r = int2char(d);
  66. }
  67. while (i >= 0) {
  68. if (p < k) {
  69. d = (this[i] & ((1 << p) - 1)) << (k - p);
  70. d |= this[--i] >> (p += this.DB - k);
  71. }
  72. else {
  73. d = (this[i] >> (p -= k)) & km;
  74. if (p <= 0) {
  75. p += this.DB;
  76. --i;
  77. }
  78. }
  79. if (d > 0) {
  80. m = true;
  81. }
  82. if (m) {
  83. r += int2char(d);
  84. }
  85. }
  86. }
  87. return m ? r : "0";
  88. };
  89. // BigInteger.prototype.negate = bnNegate;
  90. // (public) -this
  91. BigInteger.prototype.negate = function () {
  92. var r = nbi();
  93. BigInteger.ZERO.subTo(this, r);
  94. return r;
  95. };
  96. // BigInteger.prototype.abs = bnAbs;
  97. // (public) |this|
  98. BigInteger.prototype.abs = function () {
  99. return (this.s < 0) ? this.negate() : this;
  100. };
  101. // BigInteger.prototype.compareTo = bnCompareTo;
  102. // (public) return + if this > a, - if this < a, 0 if equal
  103. BigInteger.prototype.compareTo = function (a) {
  104. var r = this.s - a.s;
  105. if (r != 0) {
  106. return r;
  107. }
  108. var i = this.t;
  109. r = i - a.t;
  110. if (r != 0) {
  111. return (this.s < 0) ? -r : r;
  112. }
  113. while (--i >= 0) {
  114. if ((r = this[i] - a[i]) != 0) {
  115. return r;
  116. }
  117. }
  118. return 0;
  119. };
  120. // BigInteger.prototype.bitLength = bnBitLength;
  121. // (public) return the number of bits in "this"
  122. BigInteger.prototype.bitLength = function () {
  123. if (this.t <= 0) {
  124. return 0;
  125. }
  126. return this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ (this.s & this.DM));
  127. };
  128. // BigInteger.prototype.mod = bnMod;
  129. // (public) this mod a
  130. BigInteger.prototype.mod = function (a) {
  131. var r = nbi();
  132. this.abs().divRemTo(a, null, r);
  133. if (this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) {
  134. a.subTo(r, r);
  135. }
  136. return r;
  137. };
  138. // BigInteger.prototype.modPowInt = bnModPowInt;
  139. // (public) this^e % m, 0 <= e < 2^32
  140. BigInteger.prototype.modPowInt = function (e, m) {
  141. var z;
  142. if (e < 256 || m.isEven()) {
  143. z = new Classic(m);
  144. }
  145. else {
  146. z = new Montgomery(m);
  147. }
  148. return this.exp(e, z);
  149. };
  150. // BigInteger.prototype.clone = bnClone;
  151. // (public)
  152. BigInteger.prototype.clone = function () {
  153. var r = nbi();
  154. this.copyTo(r);
  155. return r;
  156. };
  157. // BigInteger.prototype.intValue = bnIntValue;
  158. // (public) return value as integer
  159. BigInteger.prototype.intValue = function () {
  160. if (this.s < 0) {
  161. if (this.t == 1) {
  162. return this[0] - this.DV;
  163. }
  164. else if (this.t == 0) {
  165. return -1;
  166. }
  167. }
  168. else if (this.t == 1) {
  169. return this[0];
  170. }
  171. else if (this.t == 0) {
  172. return 0;
  173. }
  174. // assumes 16 < DB < 32
  175. return ((this[1] & ((1 << (32 - this.DB)) - 1)) << this.DB) | this[0];
  176. };
  177. // BigInteger.prototype.byteValue = bnByteValue;
  178. // (public) return value as byte
  179. BigInteger.prototype.byteValue = function () {
  180. return (this.t == 0) ? this.s : (this[0] << 24) >> 24;
  181. };
  182. // BigInteger.prototype.shortValue = bnShortValue;
  183. // (public) return value as short (assumes DB>=16)
  184. BigInteger.prototype.shortValue = function () {
  185. return (this.t == 0) ? this.s : (this[0] << 16) >> 16;
  186. };
  187. // BigInteger.prototype.signum = bnSigNum;
  188. // (public) 0 if this == 0, 1 if this > 0
  189. BigInteger.prototype.signum = function () {
  190. if (this.s < 0) {
  191. return -1;
  192. }
  193. else if (this.t <= 0 || (this.t == 1 && this[0] <= 0)) {
  194. return 0;
  195. }
  196. else {
  197. return 1;
  198. }
  199. };
  200. // BigInteger.prototype.toByteArray = bnToByteArray;
  201. // (public) convert to bigendian byte array
  202. BigInteger.prototype.toByteArray = function () {
  203. var i = this.t;
  204. var r = [];
  205. r[0] = this.s;
  206. var p = this.DB - (i * this.DB) % 8;
  207. var d;
  208. var k = 0;
  209. if (i-- > 0) {
  210. if (p < this.DB && (d = this[i] >> p) != (this.s & this.DM) >> p) {
  211. r[k++] = d | (this.s << (this.DB - p));
  212. }
  213. while (i >= 0) {
  214. if (p < 8) {
  215. d = (this[i] & ((1 << p) - 1)) << (8 - p);
  216. d |= this[--i] >> (p += this.DB - 8);
  217. }
  218. else {
  219. d = (this[i] >> (p -= 8)) & 0xff;
  220. if (p <= 0) {
  221. p += this.DB;
  222. --i;
  223. }
  224. }
  225. if ((d & 0x80) != 0) {
  226. d |= -256;
  227. }
  228. if (k == 0 && (this.s & 0x80) != (d & 0x80)) {
  229. ++k;
  230. }
  231. if (k > 0 || d != this.s) {
  232. r[k++] = d;
  233. }
  234. }
  235. }
  236. return r;
  237. };
  238. // BigInteger.prototype.equals = bnEquals;
  239. BigInteger.prototype.equals = function (a) {
  240. return (this.compareTo(a) == 0);
  241. };
  242. // BigInteger.prototype.min = bnMin;
  243. BigInteger.prototype.min = function (a) {
  244. return (this.compareTo(a) < 0) ? this : a;
  245. };
  246. // BigInteger.prototype.max = bnMax;
  247. BigInteger.prototype.max = function (a) {
  248. return (this.compareTo(a) > 0) ? this : a;
  249. };
  250. // BigInteger.prototype.and = bnAnd;
  251. BigInteger.prototype.and = function (a) {
  252. var r = nbi();
  253. this.bitwiseTo(a, op_and, r);
  254. return r;
  255. };
  256. // BigInteger.prototype.or = bnOr;
  257. BigInteger.prototype.or = function (a) {
  258. var r = nbi();
  259. this.bitwiseTo(a, op_or, r);
  260. return r;
  261. };
  262. // BigInteger.prototype.xor = bnXor;
  263. BigInteger.prototype.xor = function (a) {
  264. var r = nbi();
  265. this.bitwiseTo(a, op_xor, r);
  266. return r;
  267. };
  268. // BigInteger.prototype.andNot = bnAndNot;
  269. BigInteger.prototype.andNot = function (a) {
  270. var r = nbi();
  271. this.bitwiseTo(a, op_andnot, r);
  272. return r;
  273. };
  274. // BigInteger.prototype.not = bnNot;
  275. // (public) ~this
  276. BigInteger.prototype.not = function () {
  277. var r = nbi();
  278. for (var i = 0; i < this.t; ++i) {
  279. r[i] = this.DM & ~this[i];
  280. }
  281. r.t = this.t;
  282. r.s = ~this.s;
  283. return r;
  284. };
  285. // BigInteger.prototype.shiftLeft = bnShiftLeft;
  286. // (public) this << n
  287. BigInteger.prototype.shiftLeft = function (n) {
  288. var r = nbi();
  289. if (n < 0) {
  290. this.rShiftTo(-n, r);
  291. }
  292. else {
  293. this.lShiftTo(n, r);
  294. }
  295. return r;
  296. };
  297. // BigInteger.prototype.shiftRight = bnShiftRight;
  298. // (public) this >> n
  299. BigInteger.prototype.shiftRight = function (n) {
  300. var r = nbi();
  301. if (n < 0) {
  302. this.lShiftTo(-n, r);
  303. }
  304. else {
  305. this.rShiftTo(n, r);
  306. }
  307. return r;
  308. };
  309. // BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
  310. // (public) returns index of lowest 1-bit (or -1 if none)
  311. BigInteger.prototype.getLowestSetBit = function () {
  312. for (var i = 0; i < this.t; ++i) {
  313. if (this[i] != 0) {
  314. return i * this.DB + lbit(this[i]);
  315. }
  316. }
  317. if (this.s < 0) {
  318. return this.t * this.DB;
  319. }
  320. return -1;
  321. };
  322. // BigInteger.prototype.bitCount = bnBitCount;
  323. // (public) return number of set bits
  324. BigInteger.prototype.bitCount = function () {
  325. var r = 0;
  326. var x = this.s & this.DM;
  327. for (var i = 0; i < this.t; ++i) {
  328. r += cbit(this[i] ^ x);
  329. }
  330. return r;
  331. };
  332. // BigInteger.prototype.testBit = bnTestBit;
  333. // (public) true iff nth bit is set
  334. BigInteger.prototype.testBit = function (n) {
  335. var j = Math.floor(n / this.DB);
  336. if (j >= this.t) {
  337. return (this.s != 0);
  338. }
  339. return ((this[j] & (1 << (n % this.DB))) != 0);
  340. };
  341. // BigInteger.prototype.setBit = bnSetBit;
  342. // (public) this | (1<<n)
  343. BigInteger.prototype.setBit = function (n) {
  344. return this.changeBit(n, op_or);
  345. };
  346. // BigInteger.prototype.clearBit = bnClearBit;
  347. // (public) this & ~(1<<n)
  348. BigInteger.prototype.clearBit = function (n) {
  349. return this.changeBit(n, op_andnot);
  350. };
  351. // BigInteger.prototype.flipBit = bnFlipBit;
  352. // (public) this ^ (1<<n)
  353. BigInteger.prototype.flipBit = function (n) {
  354. return this.changeBit(n, op_xor);
  355. };
  356. // BigInteger.prototype.add = bnAdd;
  357. // (public) this + a
  358. BigInteger.prototype.add = function (a) {
  359. var r = nbi();
  360. this.addTo(a, r);
  361. return r;
  362. };
  363. // BigInteger.prototype.subtract = bnSubtract;
  364. // (public) this - a
  365. BigInteger.prototype.subtract = function (a) {
  366. var r = nbi();
  367. this.subTo(a, r);
  368. return r;
  369. };
  370. // BigInteger.prototype.multiply = bnMultiply;
  371. // (public) this * a
  372. BigInteger.prototype.multiply = function (a) {
  373. var r = nbi();
  374. this.multiplyTo(a, r);
  375. return r;
  376. };
  377. // BigInteger.prototype.divide = bnDivide;
  378. // (public) this / a
  379. BigInteger.prototype.divide = function (a) {
  380. var r = nbi();
  381. this.divRemTo(a, r, null);
  382. return r;
  383. };
  384. // BigInteger.prototype.remainder = bnRemainder;
  385. // (public) this % a
  386. BigInteger.prototype.remainder = function (a) {
  387. var r = nbi();
  388. this.divRemTo(a, null, r);
  389. return r;
  390. };
  391. // BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
  392. // (public) [this/a,this%a]
  393. BigInteger.prototype.divideAndRemainder = function (a) {
  394. var q = nbi();
  395. var r = nbi();
  396. this.divRemTo(a, q, r);
  397. return [q, r];
  398. };
  399. // BigInteger.prototype.modPow = bnModPow;
  400. // (public) this^e % m (HAC 14.85)
  401. BigInteger.prototype.modPow = function (e, m) {
  402. var i = e.bitLength();
  403. var k;
  404. var r = nbv(1);
  405. var z;
  406. if (i <= 0) {
  407. return r;
  408. }
  409. else if (i < 18) {
  410. k = 1;
  411. }
  412. else if (i < 48) {
  413. k = 3;
  414. }
  415. else if (i < 144) {
  416. k = 4;
  417. }
  418. else if (i < 768) {
  419. k = 5;
  420. }
  421. else {
  422. k = 6;
  423. }
  424. if (i < 8) {
  425. z = new Classic(m);
  426. }
  427. else if (m.isEven()) {
  428. z = new Barrett(m);
  429. }
  430. else {
  431. z = new Montgomery(m);
  432. }
  433. // precomputation
  434. var g = [];
  435. var n = 3;
  436. var k1 = k - 1;
  437. var km = (1 << k) - 1;
  438. g[1] = z.convert(this);
  439. if (k > 1) {
  440. var g2 = nbi();
  441. z.sqrTo(g[1], g2);
  442. while (n <= km) {
  443. g[n] = nbi();
  444. z.mulTo(g2, g[n - 2], g[n]);
  445. n += 2;
  446. }
  447. }
  448. var j = e.t - 1;
  449. var w;
  450. var is1 = true;
  451. var r2 = nbi();
  452. var t;
  453. i = nbits(e[j]) - 1;
  454. while (j >= 0) {
  455. if (i >= k1) {
  456. w = (e[j] >> (i - k1)) & km;
  457. }
  458. else {
  459. w = (e[j] & ((1 << (i + 1)) - 1)) << (k1 - i);
  460. if (j > 0) {
  461. w |= e[j - 1] >> (this.DB + i - k1);
  462. }
  463. }
  464. n = k;
  465. while ((w & 1) == 0) {
  466. w >>= 1;
  467. --n;
  468. }
  469. if ((i -= n) < 0) {
  470. i += this.DB;
  471. --j;
  472. }
  473. if (is1) { // ret == 1, don't bother squaring or multiplying it
  474. g[w].copyTo(r);
  475. is1 = false;
  476. }
  477. else {
  478. while (n > 1) {
  479. z.sqrTo(r, r2);
  480. z.sqrTo(r2, r);
  481. n -= 2;
  482. }
  483. if (n > 0) {
  484. z.sqrTo(r, r2);
  485. }
  486. else {
  487. t = r;
  488. r = r2;
  489. r2 = t;
  490. }
  491. z.mulTo(r2, g[w], r);
  492. }
  493. while (j >= 0 && (e[j] & (1 << i)) == 0) {
  494. z.sqrTo(r, r2);
  495. t = r;
  496. r = r2;
  497. r2 = t;
  498. if (--i < 0) {
  499. i = this.DB - 1;
  500. --j;
  501. }
  502. }
  503. }
  504. return z.revert(r);
  505. };
  506. // BigInteger.prototype.modInverse = bnModInverse;
  507. // (public) 1/this % m (HAC 14.61)
  508. BigInteger.prototype.modInverse = function (m) {
  509. var ac = m.isEven();
  510. if ((this.isEven() && ac) || m.signum() == 0) {
  511. return BigInteger.ZERO;
  512. }
  513. var u = m.clone();
  514. var v = this.clone();
  515. var a = nbv(1);
  516. var b = nbv(0);
  517. var c = nbv(0);
  518. var d = nbv(1);
  519. while (u.signum() != 0) {
  520. while (u.isEven()) {
  521. u.rShiftTo(1, u);
  522. if (ac) {
  523. if (!a.isEven() || !b.isEven()) {
  524. a.addTo(this, a);
  525. b.subTo(m, b);
  526. }
  527. a.rShiftTo(1, a);
  528. }
  529. else if (!b.isEven()) {
  530. b.subTo(m, b);
  531. }
  532. b.rShiftTo(1, b);
  533. }
  534. while (v.isEven()) {
  535. v.rShiftTo(1, v);
  536. if (ac) {
  537. if (!c.isEven() || !d.isEven()) {
  538. c.addTo(this, c);
  539. d.subTo(m, d);
  540. }
  541. c.rShiftTo(1, c);
  542. }
  543. else if (!d.isEven()) {
  544. d.subTo(m, d);
  545. }
  546. d.rShiftTo(1, d);
  547. }
  548. if (u.compareTo(v) >= 0) {
  549. u.subTo(v, u);
  550. if (ac) {
  551. a.subTo(c, a);
  552. }
  553. b.subTo(d, b);
  554. }
  555. else {
  556. v.subTo(u, v);
  557. if (ac) {
  558. c.subTo(a, c);
  559. }
  560. d.subTo(b, d);
  561. }
  562. }
  563. if (v.compareTo(BigInteger.ONE) != 0) {
  564. return BigInteger.ZERO;
  565. }
  566. if (d.compareTo(m) >= 0) {
  567. return d.subtract(m);
  568. }
  569. if (d.signum() < 0) {
  570. d.addTo(m, d);
  571. }
  572. else {
  573. return d;
  574. }
  575. if (d.signum() < 0) {
  576. return d.add(m);
  577. }
  578. else {
  579. return d;
  580. }
  581. };
  582. // BigInteger.prototype.pow = bnPow;
  583. // (public) this^e
  584. BigInteger.prototype.pow = function (e) {
  585. return this.exp(e, new NullExp());
  586. };
  587. // BigInteger.prototype.gcd = bnGCD;
  588. // (public) gcd(this,a) (HAC 14.54)
  589. BigInteger.prototype.gcd = function (a) {
  590. var x = (this.s < 0) ? this.negate() : this.clone();
  591. var y = (a.s < 0) ? a.negate() : a.clone();
  592. if (x.compareTo(y) < 0) {
  593. var t = x;
  594. x = y;
  595. y = t;
  596. }
  597. var i = x.getLowestSetBit();
  598. var g = y.getLowestSetBit();
  599. if (g < 0) {
  600. return x;
  601. }
  602. if (i < g) {
  603. g = i;
  604. }
  605. if (g > 0) {
  606. x.rShiftTo(g, x);
  607. y.rShiftTo(g, y);
  608. }
  609. while (x.signum() > 0) {
  610. if ((i = x.getLowestSetBit()) > 0) {
  611. x.rShiftTo(i, x);
  612. }
  613. if ((i = y.getLowestSetBit()) > 0) {
  614. y.rShiftTo(i, y);
  615. }
  616. if (x.compareTo(y) >= 0) {
  617. x.subTo(y, x);
  618. x.rShiftTo(1, x);
  619. }
  620. else {
  621. y.subTo(x, y);
  622. y.rShiftTo(1, y);
  623. }
  624. }
  625. if (g > 0) {
  626. y.lShiftTo(g, y);
  627. }
  628. return y;
  629. };
  630. // BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
  631. // (public) test primality with certainty >= 1-.5^t
  632. BigInteger.prototype.isProbablePrime = function (t) {
  633. var i;
  634. var x = this.abs();
  635. if (x.t == 1 && x[0] <= lowprimes[lowprimes.length - 1]) {
  636. for (i = 0; i < lowprimes.length; ++i) {
  637. if (x[0] == lowprimes[i]) {
  638. return true;
  639. }
  640. }
  641. return false;
  642. }
  643. if (x.isEven()) {
  644. return false;
  645. }
  646. i = 1;
  647. while (i < lowprimes.length) {
  648. var m = lowprimes[i];
  649. var j = i + 1;
  650. while (j < lowprimes.length && m < lplim) {
  651. m *= lowprimes[j++];
  652. }
  653. m = x.modInt(m);
  654. while (i < j) {
  655. if (m % lowprimes[i++] == 0) {
  656. return false;
  657. }
  658. }
  659. }
  660. return x.millerRabin(t);
  661. };
  662. //#endregion PUBLIC
  663. //#region PROTECTED
  664. // BigInteger.prototype.copyTo = bnpCopyTo;
  665. // (protected) copy this to r
  666. BigInteger.prototype.copyTo = function (r) {
  667. for (var i = this.t - 1; i >= 0; --i) {
  668. r[i] = this[i];
  669. }
  670. r.t = this.t;
  671. r.s = this.s;
  672. };
  673. // BigInteger.prototype.fromInt = bnpFromInt;
  674. // (protected) set from integer value x, -DV <= x < DV
  675. BigInteger.prototype.fromInt = function (x) {
  676. this.t = 1;
  677. this.s = (x < 0) ? -1 : 0;
  678. if (x > 0) {
  679. this[0] = x;
  680. }
  681. else if (x < -1) {
  682. this[0] = x + this.DV;
  683. }
  684. else {
  685. this.t = 0;
  686. }
  687. };
  688. // BigInteger.prototype.fromString = bnpFromString;
  689. // (protected) set from string and radix
  690. BigInteger.prototype.fromString = function (s, b) {
  691. var k;
  692. if (b == 16) {
  693. k = 4;
  694. }
  695. else if (b == 8) {
  696. k = 3;
  697. }
  698. else if (b == 256) {
  699. k = 8;
  700. /* byte array */
  701. }
  702. else if (b == 2) {
  703. k = 1;
  704. }
  705. else if (b == 32) {
  706. k = 5;
  707. }
  708. else if (b == 4) {
  709. k = 2;
  710. }
  711. else {
  712. this.fromRadix(s, b);
  713. return;
  714. }
  715. this.t = 0;
  716. this.s = 0;
  717. var i = s.length;
  718. var mi = false;
  719. var sh = 0;
  720. while (--i >= 0) {
  721. var x = (k == 8) ? (+s[i]) & 0xff : intAt(s, i);
  722. if (x < 0) {
  723. if (s.charAt(i) == "-") {
  724. mi = true;
  725. }
  726. continue;
  727. }
  728. mi = false;
  729. if (sh == 0) {
  730. this[this.t++] = x;
  731. }
  732. else if (sh + k > this.DB) {
  733. this[this.t - 1] |= (x & ((1 << (this.DB - sh)) - 1)) << sh;
  734. this[this.t++] = (x >> (this.DB - sh));
  735. }
  736. else {
  737. this[this.t - 1] |= x << sh;
  738. }
  739. sh += k;
  740. if (sh >= this.DB) {
  741. sh -= this.DB;
  742. }
  743. }
  744. if (k == 8 && ((+s[0]) & 0x80) != 0) {
  745. this.s = -1;
  746. if (sh > 0) {
  747. this[this.t - 1] |= ((1 << (this.DB - sh)) - 1) << sh;
  748. }
  749. }
  750. this.clamp();
  751. if (mi) {
  752. BigInteger.ZERO.subTo(this, this);
  753. }
  754. };
  755. // BigInteger.prototype.clamp = bnpClamp;
  756. // (protected) clamp off excess high words
  757. BigInteger.prototype.clamp = function () {
  758. var c = this.s & this.DM;
  759. while (this.t > 0 && this[this.t - 1] == c) {
  760. --this.t;
  761. }
  762. };
  763. // BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
  764. // (protected) r = this << n*DB
  765. BigInteger.prototype.dlShiftTo = function (n, r) {
  766. var i;
  767. for (i = this.t - 1; i >= 0; --i) {
  768. r[i + n] = this[i];
  769. }
  770. for (i = n - 1; i >= 0; --i) {
  771. r[i] = 0;
  772. }
  773. r.t = this.t + n;
  774. r.s = this.s;
  775. };
  776. // BigInteger.prototype.drShiftTo = bnpDRShiftTo;
  777. // (protected) r = this >> n*DB
  778. BigInteger.prototype.drShiftTo = function (n, r) {
  779. for (var i = n; i < this.t; ++i) {
  780. r[i - n] = this[i];
  781. }
  782. r.t = Math.max(this.t - n, 0);
  783. r.s = this.s;
  784. };
  785. // BigInteger.prototype.lShiftTo = bnpLShiftTo;
  786. // (protected) r = this << n
  787. BigInteger.prototype.lShiftTo = function (n, r) {
  788. var bs = n % this.DB;
  789. var cbs = this.DB - bs;
  790. var bm = (1 << cbs) - 1;
  791. var ds = Math.floor(n / this.DB);
  792. var c = (this.s << bs) & this.DM;
  793. for (var i = this.t - 1; i >= 0; --i) {
  794. r[i + ds + 1] = (this[i] >> cbs) | c;
  795. c = (this[i] & bm) << bs;
  796. }
  797. for (var i = ds - 1; i >= 0; --i) {
  798. r[i] = 0;
  799. }
  800. r[ds] = c;
  801. r.t = this.t + ds + 1;
  802. r.s = this.s;
  803. r.clamp();
  804. };
  805. // BigInteger.prototype.rShiftTo = bnpRShiftTo;
  806. // (protected) r = this >> n
  807. BigInteger.prototype.rShiftTo = function (n, r) {
  808. r.s = this.s;
  809. var ds = Math.floor(n / this.DB);
  810. if (ds >= this.t) {
  811. r.t = 0;
  812. return;
  813. }
  814. var bs = n % this.DB;
  815. var cbs = this.DB - bs;
  816. var bm = (1 << bs) - 1;
  817. r[0] = this[ds] >> bs;
  818. for (var i = ds + 1; i < this.t; ++i) {
  819. r[i - ds - 1] |= (this[i] & bm) << cbs;
  820. r[i - ds] = this[i] >> bs;
  821. }
  822. if (bs > 0) {
  823. r[this.t - ds - 1] |= (this.s & bm) << cbs;
  824. }
  825. r.t = this.t - ds;
  826. r.clamp();
  827. };
  828. // BigInteger.prototype.subTo = bnpSubTo;
  829. // (protected) r = this - a
  830. BigInteger.prototype.subTo = function (a, r) {
  831. var i = 0;
  832. var c = 0;
  833. var m = Math.min(a.t, this.t);
  834. while (i < m) {
  835. c += this[i] - a[i];
  836. r[i++] = c & this.DM;
  837. c >>= this.DB;
  838. }
  839. if (a.t < this.t) {
  840. c -= a.s;
  841. while (i < this.t) {
  842. c += this[i];
  843. r[i++] = c & this.DM;
  844. c >>= this.DB;
  845. }
  846. c += this.s;
  847. }
  848. else {
  849. c += this.s;
  850. while (i < a.t) {
  851. c -= a[i];
  852. r[i++] = c & this.DM;
  853. c >>= this.DB;
  854. }
  855. c -= a.s;
  856. }
  857. r.s = (c < 0) ? -1 : 0;
  858. if (c < -1) {
  859. r[i++] = this.DV + c;
  860. }
  861. else if (c > 0) {
  862. r[i++] = c;
  863. }
  864. r.t = i;
  865. r.clamp();
  866. };
  867. // BigInteger.prototype.multiplyTo = bnpMultiplyTo;
  868. // (protected) r = this * a, r != this,a (HAC 14.12)
  869. // "this" should be the larger one if appropriate.
  870. BigInteger.prototype.multiplyTo = function (a, r) {
  871. var x = this.abs();
  872. var y = a.abs();
  873. var i = x.t;
  874. r.t = i + y.t;
  875. while (--i >= 0) {
  876. r[i] = 0;
  877. }
  878. for (i = 0; i < y.t; ++i) {
  879. r[i + x.t] = x.am(0, y[i], r, i, 0, x.t);
  880. }
  881. r.s = 0;
  882. r.clamp();
  883. if (this.s != a.s) {
  884. BigInteger.ZERO.subTo(r, r);
  885. }
  886. };
  887. // BigInteger.prototype.squareTo = bnpSquareTo;
  888. // (protected) r = this^2, r != this (HAC 14.16)
  889. BigInteger.prototype.squareTo = function (r) {
  890. var x = this.abs();
  891. var i = r.t = 2 * x.t;
  892. while (--i >= 0) {
  893. r[i] = 0;
  894. }
  895. for (i = 0; i < x.t - 1; ++i) {
  896. var c = x.am(i, x[i], r, 2 * i, 0, 1);
  897. if ((r[i + x.t] += x.am(i + 1, 2 * x[i], r, 2 * i + 1, c, x.t - i - 1)) >= x.DV) {
  898. r[i + x.t] -= x.DV;
  899. r[i + x.t + 1] = 1;
  900. }
  901. }
  902. if (r.t > 0) {
  903. r[r.t - 1] += x.am(i, x[i], r, 2 * i, 0, 1);
  904. }
  905. r.s = 0;
  906. r.clamp();
  907. };
  908. // BigInteger.prototype.divRemTo = bnpDivRemTo;
  909. // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
  910. // r != q, this != m. q or r may be null.
  911. BigInteger.prototype.divRemTo = function (m, q, r) {
  912. var pm = m.abs();
  913. if (pm.t <= 0) {
  914. return;
  915. }
  916. var pt = this.abs();
  917. if (pt.t < pm.t) {
  918. if (q != null) {
  919. q.fromInt(0);
  920. }
  921. if (r != null) {
  922. this.copyTo(r);
  923. }
  924. return;
  925. }
  926. if (r == null) {
  927. r = nbi();
  928. }
  929. var y = nbi();
  930. var ts = this.s;
  931. var ms = m.s;
  932. var nsh = this.DB - nbits(pm[pm.t - 1]); // normalize modulus
  933. if (nsh > 0) {
  934. pm.lShiftTo(nsh, y);
  935. pt.lShiftTo(nsh, r);
  936. }
  937. else {
  938. pm.copyTo(y);
  939. pt.copyTo(r);
  940. }
  941. var ys = y.t;
  942. var y0 = y[ys - 1];
  943. if (y0 == 0) {
  944. return;
  945. }
  946. var yt = y0 * (1 << this.F1) + ((ys > 1) ? y[ys - 2] >> this.F2 : 0);
  947. var d1 = this.FV / yt;
  948. var d2 = (1 << this.F1) / yt;
  949. var e = 1 << this.F2;
  950. var i = r.t;
  951. var j = i - ys;
  952. var t = (q == null) ? nbi() : q;
  953. y.dlShiftTo(j, t);
  954. if (r.compareTo(t) >= 0) {
  955. r[r.t++] = 1;
  956. r.subTo(t, r);
  957. }
  958. BigInteger.ONE.dlShiftTo(ys, t);
  959. t.subTo(y, y); // "negative" y so we can replace sub with am later
  960. while (y.t < ys) {
  961. y[y.t++] = 0;
  962. }
  963. while (--j >= 0) {
  964. // Estimate quotient digit
  965. var qd = (r[--i] == y0) ? this.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2);
  966. if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) { // Try it out
  967. y.dlShiftTo(j, t);
  968. r.subTo(t, r);
  969. while (r[i] < --qd) {
  970. r.subTo(t, r);
  971. }
  972. }
  973. }
  974. if (q != null) {
  975. r.drShiftTo(ys, q);
  976. if (ts != ms) {
  977. BigInteger.ZERO.subTo(q, q);
  978. }
  979. }
  980. r.t = ys;
  981. r.clamp();
  982. if (nsh > 0) {
  983. r.rShiftTo(nsh, r);
  984. } // Denormalize remainder
  985. if (ts < 0) {
  986. BigInteger.ZERO.subTo(r, r);
  987. }
  988. };
  989. // BigInteger.prototype.invDigit = bnpInvDigit;
  990. // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
  991. // justification:
  992. // xy == 1 (mod m)
  993. // xy = 1+km
  994. // xy(2-xy) = (1+km)(1-km)
  995. // x[y(2-xy)] = 1-k^2m^2
  996. // x[y(2-xy)] == 1 (mod m^2)
  997. // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
  998. // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
  999. // JS multiply "overflows" differently from C/C++, so care is needed here.
  1000. BigInteger.prototype.invDigit = function () {
  1001. if (this.t < 1) {
  1002. return 0;
  1003. }
  1004. var x = this[0];
  1005. if ((x & 1) == 0) {
  1006. return 0;
  1007. }
  1008. var y = x & 3; // y == 1/x mod 2^2
  1009. y = (y * (2 - (x & 0xf) * y)) & 0xf; // y == 1/x mod 2^4
  1010. y = (y * (2 - (x & 0xff) * y)) & 0xff; // y == 1/x mod 2^8
  1011. y = (y * (2 - (((x & 0xffff) * y) & 0xffff))) & 0xffff; // y == 1/x mod 2^16
  1012. // last step - calculate inverse mod DV directly;
  1013. // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
  1014. y = (y * (2 - x * y % this.DV)) % this.DV; // y == 1/x mod 2^dbits
  1015. // we really want the negative inverse, and -DV < y < DV
  1016. return (y > 0) ? this.DV - y : -y;
  1017. };
  1018. // BigInteger.prototype.isEven = bnpIsEven;
  1019. // (protected) true iff this is even
  1020. BigInteger.prototype.isEven = function () {
  1021. return ((this.t > 0) ? (this[0] & 1) : this.s) == 0;
  1022. };
  1023. // BigInteger.prototype.exp = bnpExp;
  1024. // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
  1025. BigInteger.prototype.exp = function (e, z) {
  1026. if (e > 0xffffffff || e < 1) {
  1027. return BigInteger.ONE;
  1028. }
  1029. var r = nbi();
  1030. var r2 = nbi();
  1031. var g = z.convert(this);
  1032. var i = nbits(e) - 1;
  1033. g.copyTo(r);
  1034. while (--i >= 0) {
  1035. z.sqrTo(r, r2);
  1036. if ((e & (1 << i)) > 0) {
  1037. z.mulTo(r2, g, r);
  1038. }
  1039. else {
  1040. var t = r;
  1041. r = r2;
  1042. r2 = t;
  1043. }
  1044. }
  1045. return z.revert(r);
  1046. };
  1047. // BigInteger.prototype.chunkSize = bnpChunkSize;
  1048. // (protected) return x s.t. r^x < DV
  1049. BigInteger.prototype.chunkSize = function (r) {
  1050. return Math.floor(Math.LN2 * this.DB / Math.log(r));
  1051. };
  1052. // BigInteger.prototype.toRadix = bnpToRadix;
  1053. // (protected) convert to radix string
  1054. BigInteger.prototype.toRadix = function (b) {
  1055. if (b == null) {
  1056. b = 10;
  1057. }
  1058. if (this.signum() == 0 || b < 2 || b > 36) {
  1059. return "0";
  1060. }
  1061. var cs = this.chunkSize(b);
  1062. var a = Math.pow(b, cs);
  1063. var d = nbv(a);
  1064. var y = nbi();
  1065. var z = nbi();
  1066. var r = "";
  1067. this.divRemTo(d, y, z);
  1068. while (y.signum() > 0) {
  1069. r = (a + z.intValue()).toString(b).substr(1) + r;
  1070. y.divRemTo(d, y, z);
  1071. }
  1072. return z.intValue().toString(b) + r;
  1073. };
  1074. // BigInteger.prototype.fromRadix = bnpFromRadix;
  1075. // (protected) convert from radix string
  1076. BigInteger.prototype.fromRadix = function (s, b) {
  1077. this.fromInt(0);
  1078. if (b == null) {
  1079. b = 10;
  1080. }
  1081. var cs = this.chunkSize(b);
  1082. var d = Math.pow(b, cs);
  1083. var mi = false;
  1084. var j = 0;
  1085. var w = 0;
  1086. for (var i = 0; i < s.length; ++i) {
  1087. var x = intAt(s, i);
  1088. if (x < 0) {
  1089. if (s.charAt(i) == "-" && this.signum() == 0) {
  1090. mi = true;
  1091. }
  1092. continue;
  1093. }
  1094. w = b * w + x;
  1095. if (++j >= cs) {
  1096. this.dMultiply(d);
  1097. this.dAddOffset(w, 0);
  1098. j = 0;
  1099. w = 0;
  1100. }
  1101. }
  1102. if (j > 0) {
  1103. this.dMultiply(Math.pow(b, j));
  1104. this.dAddOffset(w, 0);
  1105. }
  1106. if (mi) {
  1107. BigInteger.ZERO.subTo(this, this);
  1108. }
  1109. };
  1110. // BigInteger.prototype.fromNumber = bnpFromNumber;
  1111. // (protected) alternate constructor
  1112. BigInteger.prototype.fromNumber = function (a, b, c) {
  1113. if ("number" == typeof b) {
  1114. // new BigInteger(int,int,RNG)
  1115. if (a < 2) {
  1116. this.fromInt(1);
  1117. }
  1118. else {
  1119. this.fromNumber(a, c);
  1120. if (!this.testBit(a - 1)) {
  1121. // force MSB set
  1122. this.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, this);
  1123. }
  1124. if (this.isEven()) {
  1125. this.dAddOffset(1, 0);
  1126. } // force odd
  1127. while (!this.isProbablePrime(b)) {
  1128. this.dAddOffset(2, 0);
  1129. if (this.bitLength() > a) {
  1130. this.subTo(BigInteger.ONE.shiftLeft(a - 1), this);
  1131. }
  1132. }
  1133. }
  1134. }
  1135. else {
  1136. // new BigInteger(int,RNG)
  1137. var x = [];
  1138. var t = a & 7;
  1139. x.length = (a >> 3) + 1;
  1140. b.nextBytes(x);
  1141. if (t > 0) {
  1142. x[0] &= ((1 << t) - 1);
  1143. }
  1144. else {
  1145. x[0] = 0;
  1146. }
  1147. this.fromString(x, 256);
  1148. }
  1149. };
  1150. // BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
  1151. // (protected) r = this op a (bitwise)
  1152. BigInteger.prototype.bitwiseTo = function (a, op, r) {
  1153. var i;
  1154. var f;
  1155. var m = Math.min(a.t, this.t);
  1156. for (i = 0; i < m; ++i) {
  1157. r[i] = op(this[i], a[i]);
  1158. }
  1159. if (a.t < this.t) {
  1160. f = a.s & this.DM;
  1161. for (i = m; i < this.t; ++i) {
  1162. r[i] = op(this[i], f);
  1163. }
  1164. r.t = this.t;
  1165. }
  1166. else {
  1167. f = this.s & this.DM;
  1168. for (i = m; i < a.t; ++i) {
  1169. r[i] = op(f, a[i]);
  1170. }
  1171. r.t = a.t;
  1172. }
  1173. r.s = op(this.s, a.s);
  1174. r.clamp();
  1175. };
  1176. // BigInteger.prototype.changeBit = bnpChangeBit;
  1177. // (protected) this op (1<<n)
  1178. BigInteger.prototype.changeBit = function (n, op) {
  1179. var r = BigInteger.ONE.shiftLeft(n);
  1180. this.bitwiseTo(r, op, r);
  1181. return r;
  1182. };
  1183. // BigInteger.prototype.addTo = bnpAddTo;
  1184. // (protected) r = this + a
  1185. BigInteger.prototype.addTo = function (a, r) {
  1186. var i = 0;
  1187. var c = 0;
  1188. var m = Math.min(a.t, this.t);
  1189. while (i < m) {
  1190. c += this[i] + a[i];
  1191. r[i++] = c & this.DM;
  1192. c >>= this.DB;
  1193. }
  1194. if (a.t < this.t) {
  1195. c += a.s;
  1196. while (i < this.t) {
  1197. c += this[i];
  1198. r[i++] = c & this.DM;
  1199. c >>= this.DB;
  1200. }
  1201. c += this.s;
  1202. }
  1203. else {
  1204. c += this.s;
  1205. while (i < a.t) {
  1206. c += a[i];
  1207. r[i++] = c & this.DM;
  1208. c >>= this.DB;
  1209. }
  1210. c += a.s;
  1211. }
  1212. r.s = (c < 0) ? -1 : 0;
  1213. if (c > 0) {
  1214. r[i++] = c;
  1215. }
  1216. else if (c < -1) {
  1217. r[i++] = this.DV + c;
  1218. }
  1219. r.t = i;
  1220. r.clamp();
  1221. };
  1222. // BigInteger.prototype.dMultiply = bnpDMultiply;
  1223. // (protected) this *= n, this >= 0, 1 < n < DV
  1224. BigInteger.prototype.dMultiply = function (n) {
  1225. this[this.t] = this.am(0, n - 1, this, 0, 0, this.t);
  1226. ++this.t;
  1227. this.clamp();
  1228. };
  1229. // BigInteger.prototype.dAddOffset = bnpDAddOffset;
  1230. // (protected) this += n << w words, this >= 0
  1231. BigInteger.prototype.dAddOffset = function (n, w) {
  1232. if (n == 0) {
  1233. return;
  1234. }
  1235. while (this.t <= w) {
  1236. this[this.t++] = 0;
  1237. }
  1238. this[w] += n;
  1239. while (this[w] >= this.DV) {
  1240. this[w] -= this.DV;
  1241. if (++w >= this.t) {
  1242. this[this.t++] = 0;
  1243. }
  1244. ++this[w];
  1245. }
  1246. };
  1247. // BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
  1248. // (protected) r = lower n words of "this * a", a.t <= n
  1249. // "this" should be the larger one if appropriate.
  1250. BigInteger.prototype.multiplyLowerTo = function (a, n, r) {
  1251. var i = Math.min(this.t + a.t, n);
  1252. r.s = 0; // assumes a,this >= 0
  1253. r.t = i;
  1254. while (i > 0) {
  1255. r[--i] = 0;
  1256. }
  1257. for (var j = r.t - this.t; i < j; ++i) {
  1258. r[i + this.t] = this.am(0, a[i], r, i, 0, this.t);
  1259. }
  1260. for (var j = Math.min(a.t, n); i < j; ++i) {
  1261. this.am(0, a[i], r, i, 0, n - i);
  1262. }
  1263. r.clamp();
  1264. };
  1265. // BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
  1266. // (protected) r = "this * a" without lower n words, n > 0
  1267. // "this" should be the larger one if appropriate.
  1268. BigInteger.prototype.multiplyUpperTo = function (a, n, r) {
  1269. --n;
  1270. var i = r.t = this.t + a.t - n;
  1271. r.s = 0; // assumes a,this >= 0
  1272. while (--i >= 0) {
  1273. r[i] = 0;
  1274. }
  1275. for (i = Math.max(n - this.t, 0); i < a.t; ++i) {
  1276. r[this.t + i - n] = this.am(n - i, a[i], r, 0, 0, this.t + i - n);
  1277. }
  1278. r.clamp();
  1279. r.drShiftTo(1, r);
  1280. };
  1281. // BigInteger.prototype.modInt = bnpModInt;
  1282. // (protected) this % n, n < 2^26
  1283. BigInteger.prototype.modInt = function (n) {
  1284. if (n <= 0) {
  1285. return 0;
  1286. }
  1287. var d = this.DV % n;
  1288. var r = (this.s < 0) ? n - 1 : 0;
  1289. if (this.t > 0) {
  1290. if (d == 0) {
  1291. r = this[0] % n;
  1292. }
  1293. else {
  1294. for (var i = this.t - 1; i >= 0; --i) {
  1295. r = (d * r + this[i]) % n;
  1296. }
  1297. }
  1298. }
  1299. return r;
  1300. };
  1301. // BigInteger.prototype.millerRabin = bnpMillerRabin;
  1302. // (protected) true if probably prime (HAC 4.24, Miller-Rabin)
  1303. BigInteger.prototype.millerRabin = function (t) {
  1304. var n1 = this.subtract(BigInteger.ONE);
  1305. var k = n1.getLowestSetBit();
  1306. if (k <= 0) {
  1307. return false;
  1308. }
  1309. var r = n1.shiftRight(k);
  1310. t = (t + 1) >> 1;
  1311. if (t > lowprimes.length) {
  1312. t = lowprimes.length;
  1313. }
  1314. var a = nbi();
  1315. for (var i = 0; i < t; ++i) {
  1316. // Pick bases at random, instead of starting at 2
  1317. a.fromInt(lowprimes[Math.floor(Math.random() * lowprimes.length)]);
  1318. var y = a.modPow(r, this);
  1319. if (y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
  1320. var j = 1;
  1321. while (j++ < k && y.compareTo(n1) != 0) {
  1322. y = y.modPowInt(2, this);
  1323. if (y.compareTo(BigInteger.ONE) == 0) {
  1324. return false;
  1325. }
  1326. }
  1327. if (y.compareTo(n1) != 0) {
  1328. return false;
  1329. }
  1330. }
  1331. }
  1332. return true;
  1333. };
  1334. // BigInteger.prototype.square = bnSquare;
  1335. // (public) this^2
  1336. BigInteger.prototype.square = function () {
  1337. var r = nbi();
  1338. this.squareTo(r);
  1339. return r;
  1340. };
  1341. //#region ASYNC
  1342. // Public API method
  1343. BigInteger.prototype.gcda = function (a, callback) {
  1344. var x = (this.s < 0) ? this.negate() : this.clone();
  1345. var y = (a.s < 0) ? a.negate() : a.clone();
  1346. if (x.compareTo(y) < 0) {
  1347. var t = x;
  1348. x = y;
  1349. y = t;
  1350. }
  1351. var i = x.getLowestSetBit();
  1352. var g = y.getLowestSetBit();
  1353. if (g < 0) {
  1354. callback(x);
  1355. return;
  1356. }
  1357. if (i < g) {
  1358. g = i;
  1359. }
  1360. if (g > 0) {
  1361. x.rShiftTo(g, x);
  1362. y.rShiftTo(g, y);
  1363. }
  1364. // Workhorse of the algorithm, gets called 200 - 800 times per 512 bit keygen.
  1365. var gcda1 = function () {
  1366. if ((i = x.getLowestSetBit()) > 0) {
  1367. x.rShiftTo(i, x);
  1368. }
  1369. if ((i = y.getLowestSetBit()) > 0) {
  1370. y.rShiftTo(i, y);
  1371. }
  1372. if (x.compareTo(y) >= 0) {
  1373. x.subTo(y, x);
  1374. x.rShiftTo(1, x);
  1375. }
  1376. else {
  1377. y.subTo(x, y);
  1378. y.rShiftTo(1, y);
  1379. }
  1380. if (!(x.signum() > 0)) {
  1381. if (g > 0) {
  1382. y.lShiftTo(g, y);
  1383. }
  1384. setTimeout(function () { callback(y); }, 0); // escape
  1385. }
  1386. else {
  1387. setTimeout(gcda1, 0);
  1388. }
  1389. };
  1390. setTimeout(gcda1, 10);
  1391. };
  1392. // (protected) alternate constructor
  1393. BigInteger.prototype.fromNumberAsync = function (a, b, c, callback) {
  1394. if ("number" == typeof b) {
  1395. if (a < 2) {
  1396. this.fromInt(1);
  1397. }
  1398. else {
  1399. this.fromNumber(a, c);
  1400. if (!this.testBit(a - 1)) {
  1401. this.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, this);
  1402. }
  1403. if (this.isEven()) {
  1404. this.dAddOffset(1, 0);
  1405. }
  1406. var bnp_1 = this;
  1407. var bnpfn1_1 = function () {
  1408. bnp_1.dAddOffset(2, 0);
  1409. if (bnp_1.bitLength() > a) {
  1410. bnp_1.subTo(BigInteger.ONE.shiftLeft(a - 1), bnp_1);
  1411. }
  1412. if (bnp_1.isProbablePrime(b)) {
  1413. setTimeout(function () { callback(); }, 0); // escape
  1414. }
  1415. else {
  1416. setTimeout(bnpfn1_1, 0);
  1417. }
  1418. };
  1419. setTimeout(bnpfn1_1, 0);
  1420. }
  1421. }
  1422. else {
  1423. var x = [];
  1424. var t = a & 7;
  1425. x.length = (a >> 3) + 1;
  1426. b.nextBytes(x);
  1427. if (t > 0) {
  1428. x[0] &= ((1 << t) - 1);
  1429. }
  1430. else {
  1431. x[0] = 0;
  1432. }
  1433. this.fromString(x, 256);
  1434. }
  1435. };
  1436. return BigInteger;
  1437. }());
  1438. export { BigInteger };
  1439. //#region REDUCERS
  1440. //#region NullExp
  1441. var NullExp = /** @class */ (function () {
  1442. function NullExp() {
  1443. }
  1444. // NullExp.prototype.convert = nNop;
  1445. NullExp.prototype.convert = function (x) {
  1446. return x;
  1447. };
  1448. // NullExp.prototype.revert = nNop;
  1449. NullExp.prototype.revert = function (x) {
  1450. return x;
  1451. };
  1452. // NullExp.prototype.mulTo = nMulTo;
  1453. NullExp.prototype.mulTo = function (x, y, r) {
  1454. x.multiplyTo(y, r);
  1455. };
  1456. // NullExp.prototype.sqrTo = nSqrTo;
  1457. NullExp.prototype.sqrTo = function (x, r) {
  1458. x.squareTo(r);
  1459. };
  1460. return NullExp;
  1461. }());
  1462. // Modular reduction using "classic" algorithm
  1463. var Classic = /** @class */ (function () {
  1464. function Classic(m) {
  1465. this.m = m;
  1466. }
  1467. // Classic.prototype.convert = cConvert;
  1468. Classic.prototype.convert = function (x) {
  1469. if (x.s < 0 || x.compareTo(this.m) >= 0) {
  1470. return x.mod(this.m);
  1471. }
  1472. else {
  1473. return x;
  1474. }
  1475. };
  1476. // Classic.prototype.revert = cRevert;
  1477. Classic.prototype.revert = function (x) {
  1478. return x;
  1479. };
  1480. // Classic.prototype.reduce = cReduce;
  1481. Classic.prototype.reduce = function (x) {
  1482. x.divRemTo(this.m, null, x);
  1483. };
  1484. // Classic.prototype.mulTo = cMulTo;
  1485. Classic.prototype.mulTo = function (x, y, r) {
  1486. x.multiplyTo(y, r);
  1487. this.reduce(r);
  1488. };
  1489. // Classic.prototype.sqrTo = cSqrTo;
  1490. Classic.prototype.sqrTo = function (x, r) {
  1491. x.squareTo(r);
  1492. this.reduce(r);
  1493. };
  1494. return Classic;
  1495. }());
  1496. //#endregion
  1497. //#region Montgomery
  1498. // Montgomery reduction
  1499. var Montgomery = /** @class */ (function () {
  1500. function Montgomery(m) {
  1501. this.m = m;
  1502. this.mp = m.invDigit();
  1503. this.mpl = this.mp & 0x7fff;
  1504. this.mph = this.mp >> 15;
  1505. this.um = (1 << (m.DB - 15)) - 1;
  1506. this.mt2 = 2 * m.t;
  1507. }
  1508. // Montgomery.prototype.convert = montConvert;
  1509. // xR mod m
  1510. Montgomery.prototype.convert = function (x) {
  1511. var r = nbi();
  1512. x.abs().dlShiftTo(this.m.t, r);
  1513. r.divRemTo(this.m, null, r);
  1514. if (x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) {
  1515. this.m.subTo(r, r);
  1516. }
  1517. return r;
  1518. };
  1519. // Montgomery.prototype.revert = montRevert;
  1520. // x/R mod m
  1521. Montgomery.prototype.revert = function (x) {
  1522. var r = nbi();
  1523. x.copyTo(r);
  1524. this.reduce(r);
  1525. return r;
  1526. };
  1527. // Montgomery.prototype.reduce = montReduce;
  1528. // x = x/R mod m (HAC 14.32)
  1529. Montgomery.prototype.reduce = function (x) {
  1530. while (x.t <= this.mt2) {
  1531. // pad x so am has enough room later
  1532. x[x.t++] = 0;
  1533. }
  1534. for (var i = 0; i < this.m.t; ++i) {
  1535. // faster way of calculating u0 = x[i]*mp mod DV
  1536. var j = x[i] & 0x7fff;
  1537. var u0 = (j * this.mpl + (((j * this.mph + (x[i] >> 15) * this.mpl) & this.um) << 15)) & x.DM;
  1538. // use am to combine the multiply-shift-add into one call
  1539. j = i + this.m.t;
  1540. x[j] += this.m.am(0, u0, x, i, 0, this.m.t);
  1541. // propagate carry
  1542. while (x[j] >= x.DV) {
  1543. x[j] -= x.DV;
  1544. x[++j]++;
  1545. }
  1546. }
  1547. x.clamp();
  1548. x.drShiftTo(this.m.t, x);
  1549. if (x.compareTo(this.m) >= 0) {
  1550. x.subTo(this.m, x);
  1551. }
  1552. };
  1553. // Montgomery.prototype.mulTo = montMulTo;
  1554. // r = "xy/R mod m"; x,y != r
  1555. Montgomery.prototype.mulTo = function (x, y, r) {
  1556. x.multiplyTo(y, r);
  1557. this.reduce(r);
  1558. };
  1559. // Montgomery.prototype.sqrTo = montSqrTo;
  1560. // r = "x^2/R mod m"; x != r
  1561. Montgomery.prototype.sqrTo = function (x, r) {
  1562. x.squareTo(r);
  1563. this.reduce(r);
  1564. };
  1565. return Montgomery;
  1566. }());
  1567. //#endregion Montgomery
  1568. //#region Barrett
  1569. // Barrett modular reduction
  1570. var Barrett = /** @class */ (function () {
  1571. function Barrett(m) {
  1572. this.m = m;
  1573. // setup Barrett
  1574. this.r2 = nbi();
  1575. this.q3 = nbi();
  1576. BigInteger.ONE.dlShiftTo(2 * m.t, this.r2);
  1577. this.mu = this.r2.divide(m);
  1578. }
  1579. // Barrett.prototype.convert = barrettConvert;
  1580. Barrett.prototype.convert = function (x) {
  1581. if (x.s < 0 || x.t > 2 * this.m.t) {
  1582. return x.mod(this.m);
  1583. }
  1584. else if (x.compareTo(this.m) < 0) {
  1585. return x;
  1586. }
  1587. else {
  1588. var r = nbi();
  1589. x.copyTo(r);
  1590. this.reduce(r);
  1591. return r;
  1592. }
  1593. };
  1594. // Barrett.prototype.revert = barrettRevert;
  1595. Barrett.prototype.revert = function (x) {
  1596. return x;
  1597. };
  1598. // Barrett.prototype.reduce = barrettReduce;
  1599. // x = x mod m (HAC 14.42)
  1600. Barrett.prototype.reduce = function (x) {
  1601. x.drShiftTo(this.m.t - 1, this.r2);
  1602. if (x.t > this.m.t + 1) {
  1603. x.t = this.m.t + 1;
  1604. x.clamp();
  1605. }
  1606. this.mu.multiplyUpperTo(this.r2, this.m.t + 1, this.q3);
  1607. this.m.multiplyLowerTo(this.q3, this.m.t + 1, this.r2);
  1608. while (x.compareTo(this.r2) < 0) {
  1609. x.dAddOffset(1, this.m.t + 1);
  1610. }
  1611. x.subTo(this.r2, x);
  1612. while (x.compareTo(this.m) >= 0) {
  1613. x.subTo(this.m, x);
  1614. }
  1615. };
  1616. // Barrett.prototype.mulTo = barrettMulTo;
  1617. // r = x*y mod m; x,y != r
  1618. Barrett.prototype.mulTo = function (x, y, r) {
  1619. x.multiplyTo(y, r);
  1620. this.reduce(r);
  1621. };
  1622. // Barrett.prototype.sqrTo = barrettSqrTo;
  1623. // r = x^2 mod m; x != r
  1624. Barrett.prototype.sqrTo = function (x, r) {
  1625. x.squareTo(r);
  1626. this.reduce(r);
  1627. };
  1628. return Barrett;
  1629. }());
  1630. //#endregion
  1631. //#endregion REDUCERS
  1632. // return new, unset BigInteger
  1633. export function nbi() { return new BigInteger(null); }
  1634. export function parseBigInt(str, r) {
  1635. return new BigInteger(str, r);
  1636. }
  1637. // am: Compute w_j += (x*this_i), propagate carries,
  1638. // c is initial carry, returns final carry.
  1639. // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
  1640. // We need to select the fastest one that works in this environment.
  1641. var inBrowser = typeof navigator !== "undefined";
  1642. if (inBrowser && j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
  1643. // am2 avoids a big mult-and-extract completely.
  1644. // Max digit bits should be <= 30 because we do bitwise ops
  1645. // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
  1646. BigInteger.prototype.am = function am2(i, x, w, j, c, n) {
  1647. var xl = x & 0x7fff;
  1648. var xh = x >> 15;
  1649. while (--n >= 0) {
  1650. var l = this[i] & 0x7fff;
  1651. var h = this[i++] >> 15;
  1652. var m = xh * l + h * xl;
  1653. l = xl * l + ((m & 0x7fff) << 15) + w[j] + (c & 0x3fffffff);
  1654. c = (l >>> 30) + (m >>> 15) + xh * h + (c >>> 30);
  1655. w[j++] = l & 0x3fffffff;
  1656. }
  1657. return c;
  1658. };
  1659. dbits = 30;
  1660. }
  1661. else if (inBrowser && j_lm && (navigator.appName != "Netscape")) {
  1662. // am1: use a single mult and divide to get the high bits,
  1663. // max digit bits should be 26 because
  1664. // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
  1665. BigInteger.prototype.am = function am1(i, x, w, j, c, n) {
  1666. while (--n >= 0) {
  1667. var v = x * this[i++] + w[j] + c;
  1668. c = Math.floor(v / 0x4000000);
  1669. w[j++] = v & 0x3ffffff;
  1670. }
  1671. return c;
  1672. };
  1673. dbits = 26;
  1674. }
  1675. else { // Mozilla/Netscape seems to prefer am3
  1676. // Alternately, set max digit bits to 28 since some
  1677. // browsers slow down when dealing with 32-bit numbers.
  1678. BigInteger.prototype.am = function am3(i, x, w, j, c, n) {
  1679. var xl = x & 0x3fff;
  1680. var xh = x >> 14;
  1681. while (--n >= 0) {
  1682. var l = this[i] & 0x3fff;
  1683. var h = this[i++] >> 14;
  1684. var m = xh * l + h * xl;
  1685. l = xl * l + ((m & 0x3fff) << 14) + w[j] + c;
  1686. c = (l >> 28) + (m >> 14) + xh * h;
  1687. w[j++] = l & 0xfffffff;
  1688. }
  1689. return c;
  1690. };
  1691. dbits = 28;
  1692. }
  1693. BigInteger.prototype.DB = dbits;
  1694. BigInteger.prototype.DM = ((1 << dbits) - 1);
  1695. BigInteger.prototype.DV = (1 << dbits);
  1696. var BI_FP = 52;
  1697. BigInteger.prototype.FV = Math.pow(2, BI_FP);
  1698. BigInteger.prototype.F1 = BI_FP - dbits;
  1699. BigInteger.prototype.F2 = 2 * dbits - BI_FP;
  1700. // Digit conversions
  1701. var BI_RC = [];
  1702. var rr;
  1703. var vv;
  1704. rr = "0".charCodeAt(0);
  1705. for (vv = 0; vv <= 9; ++vv) {
  1706. BI_RC[rr++] = vv;
  1707. }
  1708. rr = "a".charCodeAt(0);
  1709. for (vv = 10; vv < 36; ++vv) {
  1710. BI_RC[rr++] = vv;
  1711. }
  1712. rr = "A".charCodeAt(0);
  1713. for (vv = 10; vv < 36; ++vv) {
  1714. BI_RC[rr++] = vv;
  1715. }
  1716. export function intAt(s, i) {
  1717. var c = BI_RC[s.charCodeAt(i)];
  1718. return (c == null) ? -1 : c;
  1719. }
  1720. // return bigint initialized to value
  1721. export function nbv(i) {
  1722. var r = nbi();
  1723. r.fromInt(i);
  1724. return r;
  1725. }
  1726. // returns bit length of the integer x
  1727. export function nbits(x) {
  1728. var r = 1;
  1729. var t;
  1730. if ((t = x >>> 16) != 0) {
  1731. x = t;
  1732. r += 16;
  1733. }
  1734. if ((t = x >> 8) != 0) {
  1735. x = t;
  1736. r += 8;
  1737. }
  1738. if ((t = x >> 4) != 0) {
  1739. x = t;
  1740. r += 4;
  1741. }
  1742. if ((t = x >> 2) != 0) {
  1743. x = t;
  1744. r += 2;
  1745. }
  1746. if ((t = x >> 1) != 0) {
  1747. x = t;
  1748. r += 1;
  1749. }
  1750. return r;
  1751. }
  1752. // "constants"
  1753. BigInteger.ZERO = nbv(0);
  1754. BigInteger.ONE = nbv(1);