bigint_old.js 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. //globals
  2. bpe=0; //bits stored per array element
  3. mask=0; //AND this with an array element to chop it down to bpe bits
  4. radix=mask+1; //equals 2^bpe. A single 1 bit to the left of the last bit of mask.
  5. //the digits for converting to different bases
  6. digitsStr='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=!@#$%^&*()[]{}|;:,.<>/?`~ \\\'\"+-';
  7. digitsStr= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ!@#$%^&*()[]{}|;:,.<>/?`~+-¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿";
  8. //initialize the global variables
  9. for (bpe=0; (1<<(bpe+1)) > (1<<bpe); bpe++); //bpe=number of bits in the mantissa on this platform
  10. bpe>>=1; //bpe=number of bits in one element of the array representing the bigInt
  11. mask=(1<<bpe)-1; //AND the mask with an integer to get its bpe least significant bits
  12. radix=mask+1; //2^bpe. a single 1 bit to the left of the first bit of mask
  13. one=int2bigInt(1,1,1); //constant used in powMod_()
  14. //the following global variables are scratchpad memory to
  15. //reduce dynamic memory allocation in the inner loop
  16. t=new Array(0);
  17. ss=t; //used in mult_()
  18. s0=t; //used in multMod_(), squareMod_()
  19. s1=t; //used in powMod_(), multMod_(), squareMod_()
  20. s2=t; //used in powMod_(), multMod_()
  21. s3=t; //used in powMod_()
  22. s4=t; s5=t; //used in mod_()
  23. s6=t; //used in bigInt2str()
  24. s7=t; //used in powMod_()
  25. T=t; //used in GCD_()
  26. sa=t; //used in mont_()
  27. mr_x1=t; mr_r=t; mr_a=t; //used in millerRabin()
  28. eg_v=t; eg_u=t; eg_A=t; eg_B=t; eg_C=t; eg_D=t; //used in eGCD_(), inverseMod_()
  29. md_q1=t; md_q2=t; md_q3=t; md_r=t; md_r1=t; md_r2=t; md_tt=t; //used in mod_()
  30. primes=t; pows=t; s_i=t; s_i2=t; s_R=t; s_rm=t; s_q=t; s_n1=t;
  31. s_a=t; s_r2=t; s_n=t; s_b=t; s_d=t; s_x1=t; s_x2=t, s_aa=t; //used in randTruePrime_()
  32. rpprb=t; //used in randProbPrimeRounds() (which also uses "primes")
  33. ////////////////////////////////////////////////////////////////////////////////////////
  34. //return array of all primes less than integer n
  35. function findPrimes(n) {
  36. var i,s,p,ans;
  37. s=new Array(n);
  38. for (i=0;i<n;i++)
  39. s[i]=0;
  40. s[0]=2;
  41. p=0; //first p elements of s are primes, the rest are a sieve
  42. for(;s[p]<n;) { //s[p] is the pth prime
  43. for(i=s[p]*s[p]; i<n; i+=s[p]) //mark multiples of s[p]
  44. s[i]=1;
  45. p++;
  46. s[p]=s[p-1]+1;
  47. for(; s[p]<n && s[s[p]]; s[p]++); //find next prime (where s[p]==0)
  48. }
  49. ans=new Array(p);
  50. for(i=0;i<p;i++)
  51. ans[i]=s[i];
  52. return ans;
  53. }
  54. //does a single round of Miller-Rabin base b consider x to be a possible prime?
  55. //x is a bigInt, and b is an integer, with b<x
  56. function millerRabinInt(x,b) {
  57. if (mr_x1.length!=x.length) {
  58. mr_x1=dup(x);
  59. mr_r=dup(x);
  60. mr_a=dup(x);
  61. }
  62. copyInt_(mr_a,b);
  63. return millerRabin(x,mr_a);
  64. }
  65. //does a single round of Miller-Rabin base b consider x to be a possible prime?
  66. //x and b are bigInts with b<x
  67. function millerRabin(x,b) {
  68. var i,j,k,s;
  69. if (mr_x1.length!=x.length) {
  70. mr_x1=dup(x);
  71. mr_r=dup(x);
  72. mr_a=dup(x);
  73. }
  74. copy_(mr_a,b);
  75. copy_(mr_r,x);
  76. copy_(mr_x1,x);
  77. addInt_(mr_r,-1);
  78. addInt_(mr_x1,-1);
  79. //s=the highest power of two that divides mr_r
  80. k=0;
  81. for (i=0;i<mr_r.length;i++)
  82. for (j=1;j<mask;j<<=1)
  83. if (x[i] & j) {
  84. s=(k<mr_r.length+bpe ? k : 0);
  85. i=mr_r.length;
  86. j=mask;
  87. } else
  88. k++;
  89. if (s)
  90. rightShift_(mr_r,s);
  91. powMod_(mr_a,mr_r,x);
  92. if (!equalsInt(mr_a,1) && !equals(mr_a,mr_x1)) {
  93. j=1;
  94. while (j<=s-1 && !equals(mr_a,mr_x1)) {
  95. squareMod_(mr_a,x);
  96. if (equalsInt(mr_a,1)) {
  97. return 0;
  98. }
  99. j++;
  100. }
  101. if (!equals(mr_a,mr_x1)) {
  102. return 0;
  103. }
  104. }
  105. return 1;
  106. }
  107. //returns how many bits long the bigInt is, not counting leading zeros.
  108. function bitSize(x) {
  109. var j,z,w;
  110. for (j=x.length-1; (x[j]==0) && (j>0); j--);
  111. for (z=0,w=x[j]; w; (w>>=1),z++);
  112. z+=bpe*j;
  113. return z;
  114. }
  115. //return a copy of x with at least n elements, adding leading zeros if needed
  116. function expand(x,n) {
  117. var ans=int2bigInt(0,(x.length>n ? x.length : n)*bpe,0);
  118. copy_(ans,x);
  119. return ans;
  120. }
  121. //return a k-bit true random prime using Maurer's algorithm.
  122. function randTruePrime(k) {
  123. var ans=int2bigInt(0,k,0);
  124. randTruePrime_(ans,k);
  125. return trim(ans,1);
  126. }
  127. //return a k-bit random probable prime with probability of error < 2^-80
  128. function randProbPrime(k) {
  129. if (k>=600) return randProbPrimeRounds(k,2); //numbers from HAC table 4.3
  130. if (k>=550) return randProbPrimeRounds(k,4);
  131. if (k>=500) return randProbPrimeRounds(k,5);
  132. if (k>=400) return randProbPrimeRounds(k,6);
  133. if (k>=350) return randProbPrimeRounds(k,7);
  134. if (k>=300) return randProbPrimeRounds(k,9);
  135. if (k>=250) return randProbPrimeRounds(k,12); //numbers from HAC table 4.4
  136. if (k>=200) return randProbPrimeRounds(k,15);
  137. if (k>=150) return randProbPrimeRounds(k,18);
  138. if (k>=100) return randProbPrimeRounds(k,27);
  139. return randProbPrimeRounds(k,40); //number from HAC remark 4.26 (only an estimate)
  140. }
  141. //return a k-bit probable random prime using n rounds of Miller Rabin (after trial division with small primes)
  142. function randProbPrimeRounds(k,n) {
  143. var ans, i, divisible, B;
  144. B=30000; //B is largest prime to use in trial division
  145. ans=int2bigInt(0,k,0);
  146. //optimization: try larger and smaller B to find the best limit.
  147. if (primes.length==0)
  148. primes=findPrimes(30000); //check for divisibility by primes <=30000
  149. if (rpprb.length!=ans.length)
  150. rpprb=dup(ans);
  151. for (;;) { //keep trying random values for ans until one appears to be prime
  152. //optimization: pick a random number times L=2*3*5*...*p, plus a
  153. // random element of the list of all numbers in [0,L) not divisible by any prime up to p.
  154. // This can reduce the amount of random number generation.
  155. randBigInt_(ans,k,0); //ans = a random odd number to check
  156. ans[0] |= 1;
  157. divisible=0;
  158. //check ans for divisibility by small primes up to B
  159. for (i=0; (i<primes.length) && (primes[i]<=B); i++)
  160. if (modInt(ans,primes[i])==0 && !equalsInt(ans,primes[i])) {
  161. divisible=1;
  162. break;
  163. }
  164. //optimization: change millerRabin so the base can be bigger than the number being checked, then eliminate the while here.
  165. //do n rounds of Miller Rabin, with random bases less than ans
  166. for (i=0; i<n && !divisible; i++) {
  167. randBigInt_(rpprb,k,0);
  168. while(!greater(ans,rpprb)) //pick a random rpprb that's < ans
  169. randBigInt_(rpprb,k,0);
  170. if (!millerRabin(ans,rpprb))
  171. divisible=1;
  172. }
  173. if(!divisible)
  174. return ans;
  175. }
  176. }
  177. //return a new bigInt equal to (x mod n) for bigInts x and n.
  178. function mod(x,n) {
  179. var ans=dup(x);
  180. mod_(ans,n);
  181. return trim(ans,1);
  182. }
  183. //return (x+n) where x is a bigInt and n is an integer.
  184. function addInt(x,n) {
  185. var ans=expand(x,x.length+1);
  186. addInt_(ans,n);
  187. return trim(ans,1);
  188. }
  189. //return x*y for bigInts x and y. This is faster when y<x.
  190. function mult(x,y) {
  191. var ans=expand(x,x.length+y.length);
  192. mult_(ans,y);
  193. return trim(ans,1);
  194. }
  195. //return (x**y mod n) where x,y,n are bigInts and ** is exponentiation. 0**0=1. Faster for odd n.
  196. function powMod(x,y,n) {
  197. var ans=expand(x,n.length);
  198. powMod_(ans,trim(y,2),trim(n,2),0); //this should work without the trim, but doesn't
  199. return trim(ans,1);
  200. }
  201. //return (x-y) for bigInts x and y. Negative answers will be 2s complement
  202. function sub(x,y) {
  203. var ans=expand(x,(x.length>y.length ? x.length+1 : y.length+1));
  204. sub_(ans,y);
  205. return trim(ans,1);
  206. }
  207. //return (x+y) for bigInts x and y.
  208. function add(x,y) {
  209. var ans=expand(x,(x.length>y.length ? x.length+1 : y.length+1));
  210. add_(ans,y);
  211. return trim(ans,1);
  212. }
  213. //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null
  214. function inverseMod(x,n) {
  215. var ans=expand(x,n.length);
  216. var s;
  217. s=inverseMod_(ans,n);
  218. return s ? trim(ans,1) : null;
  219. }
  220. //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x.
  221. function multMod(x,y,n) {
  222. var ans=expand(x,n.length);
  223. multMod_(ans,y,n);
  224. return trim(ans,1);
  225. }
  226. //generate a k-bit true random prime using Maurer's algorithm,
  227. //and put it into ans. The bigInt ans must be large enough to hold it.
  228. function randTruePrime_(ans,k) {
  229. var c,m,pm,dd,j,r,B,divisible,z,zz,recSize;
  230. if (primes.length==0)
  231. primes=findPrimes(30000); //check for divisibility by primes <=30000
  232. if (pows.length==0) {
  233. pows=new Array(512);
  234. for (j=0;j<512;j++) {
  235. pows[j]=Math.pow(2,j/511.-1.);
  236. }
  237. }
  238. //c and m should be tuned for a particular machine and value of k, to maximize speed
  239. c=0.1; //c=0.1 in HAC
  240. m=20; //generate this k-bit number by first recursively generating a number that has between k/2 and k-m bits
  241. recLimit=20; //stop recursion when k <=recLimit. Must have recLimit >= 2
  242. if (s_i2.length!=ans.length) {
  243. s_i2=dup(ans);
  244. s_R =dup(ans);
  245. s_n1=dup(ans);
  246. s_r2=dup(ans);
  247. s_d =dup(ans);
  248. s_x1=dup(ans);
  249. s_x2=dup(ans);
  250. s_b =dup(ans);
  251. s_n =dup(ans);
  252. s_i =dup(ans);
  253. s_rm=dup(ans);
  254. s_q =dup(ans);
  255. s_a =dup(ans);
  256. s_aa=dup(ans);
  257. }
  258. if (k <= recLimit) { //generate small random primes by trial division up to its square root
  259. pm=(1<<((k+2)>>1))-1; //pm is binary number with all ones, just over sqrt(2^k)
  260. copyInt_(ans,0);
  261. for (dd=1;dd;) {
  262. dd=0;
  263. ans[0]= 1 | (1<<(k-1)) | Math.floor(Math.random()*(1<<k)); //random, k-bit, odd integer, with msb 1
  264. for (j=1;(j<primes.length) && ((primes[j]&pm)==primes[j]);j++) { //trial division by all primes 3...sqrt(2^k)
  265. if (0==(ans[0]%primes[j])) {
  266. dd=1;
  267. break;
  268. }
  269. }
  270. }
  271. carry_(ans);
  272. return;
  273. }
  274. B=c*k*k; //try small primes up to B (or all the primes[] array if the largest is less than B).
  275. if (k>2*m) //generate this k-bit number by first recursively generating a number that has between k/2 and k-m bits
  276. for (r=1; k-k*r<=m; )
  277. r=pows[Math.floor(Math.random()*512)]; //r=Math.pow(2,Math.random()-1);
  278. else
  279. r=.5;
  280. //simulation suggests the more complex algorithm using r=.333 is only slightly faster.
  281. recSize=Math.floor(r*k)+1;
  282. randTruePrime_(s_q,recSize);
  283. copyInt_(s_i2,0);
  284. s_i2[Math.floor((k-2)/bpe)] |= (1<<((k-2)%bpe)); //s_i2=2^(k-2)
  285. divide_(s_i2,s_q,s_i,s_rm); //s_i=floor((2^(k-1))/(2q))
  286. z=bitSize(s_i);
  287. for (;;) {
  288. for (;;) { //generate z-bit numbers until one falls in the range [0,s_i-1]
  289. randBigInt_(s_R,z,0);
  290. if (greater(s_i,s_R))
  291. break;
  292. } //now s_R is in the range [0,s_i-1]
  293. addInt_(s_R,1); //now s_R is in the range [1,s_i]
  294. add_(s_R,s_i); //now s_R is in the range [s_i+1,2*s_i]
  295. copy_(s_n,s_q);
  296. mult_(s_n,s_R);
  297. multInt_(s_n,2);
  298. addInt_(s_n,1); //s_n=2*s_R*s_q+1
  299. copy_(s_r2,s_R);
  300. multInt_(s_r2,2); //s_r2=2*s_R
  301. //check s_n for divisibility by small primes up to B
  302. for (divisible=0,j=0; (j<primes.length) && (primes[j]<B); j++)
  303. if (modInt(s_n,primes[j])==0 && !equalsInt(s_n,primes[j])) {
  304. divisible=1;
  305. break;
  306. }
  307. if (!divisible) //if it passes small primes check, then try a single Miller-Rabin base 2
  308. if (!millerRabinInt(s_n,2)) //this line represents 75% of the total runtime for randTruePrime_
  309. divisible=1;
  310. if (!divisible) { //if it passes that test, continue checking s_n
  311. addInt_(s_n,-3);
  312. for (j=s_n.length-1;(s_n[j]==0) && (j>0); j--); //strip leading zeros
  313. for (zz=0,w=s_n[j]; w; (w>>=1),zz++);
  314. zz+=bpe*j; //zz=number of bits in s_n, ignoring leading zeros
  315. for (;;) { //generate z-bit numbers until one falls in the range [0,s_n-1]
  316. randBigInt_(s_a,zz,0);
  317. if (greater(s_n,s_a))
  318. break;
  319. } //now s_a is in the range [0,s_n-1]
  320. addInt_(s_n,3); //now s_a is in the range [0,s_n-4]
  321. addInt_(s_a,2); //now s_a is in the range [2,s_n-2]
  322. copy_(s_b,s_a);
  323. copy_(s_n1,s_n);
  324. addInt_(s_n1,-1);
  325. powMod_(s_b,s_n1,s_n); //s_b=s_a^(s_n-1) modulo s_n
  326. addInt_(s_b,-1);
  327. if (isZero(s_b)) {
  328. copy_(s_b,s_a);
  329. powMod_(s_b,s_r2,s_n);
  330. addInt_(s_b,-1);
  331. copy_(s_aa,s_n);
  332. copy_(s_d,s_b);
  333. GCD_(s_d,s_n); //if s_b and s_n are relatively prime, then s_n is a prime
  334. if (equalsInt(s_d,1)) {
  335. copy_(ans,s_aa);
  336. return; //if we've made it this far, then s_n is absolutely guaranteed to be prime
  337. }
  338. }
  339. }
  340. }
  341. }
  342. //Return an n-bit random BigInt (n>=1). If s=1, then the most significant of those n bits is set to 1.
  343. function randBigInt(n,s) {
  344. var a,b;
  345. a=Math.floor((n-1)/bpe)+2; //# array elements to hold the BigInt with a leading 0 element
  346. b=int2bigInt(0,0,a);
  347. randBigInt_(b,n,s);
  348. return b;
  349. }
  350. //Set b to an n-bit random BigInt. If s=1, then the most significant of those n bits is set to 1.
  351. //Array b must be big enough to hold the result. Must have n>=1
  352. function randBigInt_(b,n,s) {
  353. var i,a;
  354. for (i=0;i<b.length;i++)
  355. b[i]=0;
  356. a=Math.floor((n-1)/bpe)+1; //# array elements to hold the BigInt
  357. for (i=0;i<a;i++) {
  358. b[i]=Math.floor(Math.random()*(1<<(bpe-1)));
  359. }
  360. b[a-1] &= (2<<((n-1)%bpe))-1;
  361. if (s==1)
  362. b[a-1] |= (1<<((n-1)%bpe));
  363. }
  364. //Return the greatest common divisor of bigInts x and y (each with same number of elements).
  365. function GCD(x,y) {
  366. var xc,yc;
  367. xc=dup(x);
  368. yc=dup(y);
  369. GCD_(xc,yc);
  370. return xc;
  371. }
  372. //set x to the greatest common divisor of bigInts x and y (each with same number of elements).
  373. //y is destroyed.
  374. function GCD_(x,y) {
  375. var i,xp,yp,A,B,C,D,q,sing;
  376. if (T.length!=x.length)
  377. T=dup(x);
  378. sing=1;
  379. while (sing) { //while y has nonzero elements other than y[0]
  380. sing=0;
  381. for (i=1;i<y.length;i++) //check if y has nonzero elements other than 0
  382. if (y[i]) {
  383. sing=1;
  384. break;
  385. }
  386. if (!sing) break; //quit when y all zero elements except possibly y[0]
  387. for (i=x.length;!x[i] && i>=0;i--); //find most significant element of x
  388. xp=x[i];
  389. yp=y[i];
  390. A=1; B=0; C=0; D=1;
  391. while ((yp+C) && (yp+D)) {
  392. q =Math.floor((xp+A)/(yp+C));
  393. qp=Math.floor((xp+B)/(yp+D));
  394. if (q!=qp)
  395. break;
  396. t= A-q*C; A=C; C=t; // do (A,B,xp, C,D,yp) = (C,D,yp, A,B,xp) - q*(0,0,0, C,D,yp)
  397. t= B-q*D; B=D; D=t;
  398. t=xp-q*yp; xp=yp; yp=t;
  399. }
  400. if (B) {
  401. copy_(T,x);
  402. linComb_(x,y,A,B); //x=A*x+B*y
  403. linComb_(y,T,D,C); //y=D*y+C*T
  404. } else {
  405. mod_(x,y);
  406. copy_(T,x);
  407. copy_(x,y);
  408. copy_(y,T);
  409. }
  410. }
  411. if (y[0]==0)
  412. return;
  413. t=modInt(x,y[0]);
  414. copyInt_(x,y[0]);
  415. y[0]=t;
  416. while (y[0]) {
  417. x[0]%=y[0];
  418. t=x[0]; x[0]=y[0]; y[0]=t;
  419. }
  420. }
  421. //do x=x**(-1) mod n, for bigInts x and n.
  422. //If no inverse exists, it sets x to zero and returns 0, else it returns 1.
  423. //The x array must be at least as large as the n array.
  424. function inverseMod_(x,n) {
  425. var k=1+2*Math.max(x.length,n.length);
  426. if(!(x[0]&1) && !(n[0]&1)) { //if both inputs are even, then inverse doesn't exist
  427. copyInt_(x,0);
  428. return 0;
  429. }
  430. if (eg_u.length!=k) {
  431. eg_u=new Array(k);
  432. eg_v=new Array(k);
  433. eg_A=new Array(k);
  434. eg_B=new Array(k);
  435. eg_C=new Array(k);
  436. eg_D=new Array(k);
  437. }
  438. copy_(eg_u,x);
  439. copy_(eg_v,n);
  440. copyInt_(eg_A,1);
  441. copyInt_(eg_B,0);
  442. copyInt_(eg_C,0);
  443. copyInt_(eg_D,1);
  444. for (;;) {
  445. while(!(eg_u[0]&1)) { //while eg_u is even
  446. halve_(eg_u);
  447. if (!(eg_A[0]&1) && !(eg_B[0]&1)) { //if eg_A==eg_B==0 mod 2
  448. halve_(eg_A);
  449. halve_(eg_B);
  450. } else {
  451. add_(eg_A,n); halve_(eg_A);
  452. sub_(eg_B,x); halve_(eg_B);
  453. }
  454. }
  455. while (!(eg_v[0]&1)) { //while eg_v is even
  456. halve_(eg_v);
  457. if (!(eg_C[0]&1) && !(eg_D[0]&1)) { //if eg_C==eg_D==0 mod 2
  458. halve_(eg_C);
  459. halve_(eg_D);
  460. } else {
  461. add_(eg_C,n); halve_(eg_C);
  462. sub_(eg_D,x); halve_(eg_D);
  463. }
  464. }
  465. if (!greater(eg_v,eg_u)) { //eg_v <= eg_u
  466. sub_(eg_u,eg_v);
  467. sub_(eg_A,eg_C);
  468. sub_(eg_B,eg_D);
  469. } else { //eg_v > eg_u
  470. sub_(eg_v,eg_u);
  471. sub_(eg_C,eg_A);
  472. sub_(eg_D,eg_B);
  473. }
  474. if (equalsInt(eg_u,0)) {
  475. while (negative(eg_C)) //make sure answer is nonnegative
  476. add_(eg_C,n);
  477. copy_(x,eg_C);
  478. if (!equalsInt(eg_v,1)) { //if GCD_(x,n)!=1, then there is no inverse
  479. copyInt_(x,0);
  480. return 0;
  481. }
  482. return 1;
  483. }
  484. }
  485. }
  486. //return x**(-1) mod n, for integers x and n. Return 0 if there is no inverse
  487. function inverseModInt(x,n) {
  488. var a=1,b=0,t;
  489. for (;;) {
  490. if (x==1) return a;
  491. if (x==0) return 0;
  492. b-=a*Math.floor(n/x);
  493. n%=x;
  494. if (n==1) return b; //to avoid negatives, change this b to n-b, and each -= to +=
  495. if (n==0) return 0;
  496. a-=b*Math.floor(x/n);
  497. x%=n;
  498. }
  499. }
  500. //this deprecated function is for backward compatibility only.
  501. function inverseModInt_(x,n) {
  502. return inverseModInt(x,n);
  503. }
  504. //Given positive bigInts x and y, change the bigints v, a, and b to positive bigInts such that:
  505. // v = GCD_(x,y) = a*x-b*y
  506. //The bigInts v, a, b, must have exactly as many elements as the larger of x and y.
  507. function eGCD_(x,y,v,a,b) {
  508. var g=0;
  509. var k=Math.max(x.length,y.length);
  510. if (eg_u.length!=k) {
  511. eg_u=new Array(k);
  512. eg_A=new Array(k);
  513. eg_B=new Array(k);
  514. eg_C=new Array(k);
  515. eg_D=new Array(k);
  516. }
  517. while(!(x[0]&1) && !(y[0]&1)) { //while x and y both even
  518. halve_(x);
  519. halve_(y);
  520. g++;
  521. }
  522. copy_(eg_u,x);
  523. copy_(v,y);
  524. copyInt_(eg_A,1);
  525. copyInt_(eg_B,0);
  526. copyInt_(eg_C,0);
  527. copyInt_(eg_D,1);
  528. for (;;) {
  529. while(!(eg_u[0]&1)) { //while u is even
  530. halve_(eg_u);
  531. if (!(eg_A[0]&1) && !(eg_B[0]&1)) { //if A==B==0 mod 2
  532. halve_(eg_A);
  533. halve_(eg_B);
  534. } else {
  535. add_(eg_A,y); halve_(eg_A);
  536. sub_(eg_B,x); halve_(eg_B);
  537. }
  538. }
  539. while (!(v[0]&1)) { //while v is even
  540. halve_(v);
  541. if (!(eg_C[0]&1) && !(eg_D[0]&1)) { //if C==D==0 mod 2
  542. halve_(eg_C);
  543. halve_(eg_D);
  544. } else {
  545. add_(eg_C,y); halve_(eg_C);
  546. sub_(eg_D,x); halve_(eg_D);
  547. }
  548. }
  549. if (!greater(v,eg_u)) { //v<=u
  550. sub_(eg_u,v);
  551. sub_(eg_A,eg_C);
  552. sub_(eg_B,eg_D);
  553. } else { //v>u
  554. sub_(v,eg_u);
  555. sub_(eg_C,eg_A);
  556. sub_(eg_D,eg_B);
  557. }
  558. if (equalsInt(eg_u,0)) {
  559. while (negative(eg_C)) { //make sure a (C) is nonnegative
  560. add_(eg_C,y);
  561. sub_(eg_D,x);
  562. }
  563. multInt_(eg_D,-1); ///make sure b (D) is nonnegative
  564. copy_(a,eg_C);
  565. copy_(b,eg_D);
  566. leftShift_(v,g);
  567. return;
  568. }
  569. }
  570. }
  571. //is bigInt x negative?
  572. function negative(x) {
  573. return ((x[x.length-1]>>(bpe-1))&1);
  574. }
  575. //is (x << (shift*bpe)) > y?
  576. //x and y are nonnegative bigInts
  577. //shift is a nonnegative integer
  578. function greaterShift(x,y,shift) {
  579. var i, kx=x.length, ky=y.length;
  580. k=((kx+shift)<ky) ? (kx+shift) : ky;
  581. for (i=ky-1-shift; i<kx && i>=0; i++) {
  582. if (x[i]>0)
  583. return 1; //if there are nonzeros in x to the left of the first column of y, then x is bigger
  584. }
  585. for (i=kx-1+shift; i<ky; i++){
  586. if (y[i]>0)
  587. return 0; //if there are nonzeros in y to the left of the first column of x, then x is not bigger
  588. }
  589. for (i=k-1; i>=shift; i--){
  590. if (x[i-shift]>y[i]) {
  591. return 1;
  592. } else if(x[i-shift]<y[i]) {
  593. return 0 ;
  594. }
  595. }
  596. return 0;
  597. }
  598. //is x > y? (x and y both nonnegative)
  599. function greater(x,y) {
  600. var i;
  601. var k=(x.length<y.length) ? x.length : y.length;
  602. for (i=x.length;i<y.length;i++)
  603. if (y[i])
  604. return 0; //y has more digits
  605. for (i=y.length;i<x.length;i++){
  606. if (x[i])
  607. return 1; //x has more digits
  608. }
  609. for (i=k-1;i>=0;i--){
  610. if (x[i]>y[i]){
  611. return 1;
  612. } else if (x[i]<y[i]){
  613. return 0;
  614. }
  615. }
  616. return 0;
  617. }
  618. //divide x by y giving quotient q and remainder r. (q=floor(x/y), r=x mod y). All 4 are bigints.
  619. //x must have at least one leading zero element.
  620. //y must be nonzero.
  621. //q and r must be arrays that are exactly the same length as x. (Or q can have more).
  622. //Must have x.length >= y.length >= 2.
  623. function divide_(x,y,q,r) {
  624. var kx, ky;
  625. var i,j,y1,y2,c,a,b;
  626. copy_(r,x);
  627. for (ky=y.length;y[ky-1]==0;ky--); //ky is number of elements in y, not including leading zeros
  628. //normalize: ensure the most significant element of y has its highest bit set
  629. b=y[ky-1];
  630. for (a=0; b; a++)
  631. b>>=1;
  632. a=bpe-a; //a is how many bits to shift so that the high order bit of y is leftmost in its array element
  633. leftShift_(y,a); //multiply both by 1<<a now, then divide both by that at the end
  634. leftShift_(r,a);
  635. //Rob Visser discovered a bug: the following line was originally just before the normalization.
  636. for (kx=r.length;r[kx-1]==0 && kx>ky;kx--); //kx is number of elements in normalized x, not including leading zeros
  637. copyInt_(q,0); // q=0
  638. while (!greaterShift(y,r,kx-ky)) { // while (leftShift_(y,kx-ky) <= r) {
  639. subShift_(r,y,kx-ky); // r=r-leftShift_(y,kx-ky)
  640. q[kx-ky]++; // q[kx-ky]++;
  641. } // }
  642. for (i=kx-1; i>=ky; i--) {
  643. if (r[i]==y[ky-1]){
  644. q[i-ky]=mask;
  645. }
  646. else{
  647. q[i-ky]=Math.floor((r[i]*radix+r[i-1])/y[ky-1]);
  648. }
  649. //The following for(;;) loop is equivalent to the commented while loop,
  650. //except that the uncommented version avoids overflow.
  651. //The commented loop comes from HAC, which assumes r[-1]==y[-1]==0
  652. // while (q[i-ky]*(y[ky-1]*radix+y[ky-2]) > r[i]*radix*radix+r[i-1]*radix+r[i-2])
  653. // q[i-ky]--;
  654. for (;;) {
  655. y2=(ky>1 ? y[ky-2] : 0)*q[i-ky];
  656. c=y2>>bpe;
  657. y2=y2 & mask;
  658. y1=c+q[i-ky]*y[ky-1];
  659. c=y1>>bpe;
  660. y1=y1 & mask;
  661. if (c==r[i] ? y1==r[i-1] ? y2>(i>1 ? r[i-2] : 0) : y1>r[i-1] : c>r[i]){
  662. q[i-ky]--;
  663. }
  664. else{
  665. break;
  666. }
  667. }
  668. linCombShift_(r,y,-q[i-ky],i-ky); //r=r-q[i-ky]*leftShift_(y,i-ky)
  669. if (negative(r)) {
  670. addShift_(r,y,i-ky); //r=r+leftShift_(y,i-ky)
  671. q[i-ky]--;
  672. }
  673. }
  674. rightShift_(y,a); //undo the normalization step
  675. rightShift_(r,a); //undo the normalization step
  676. }
  677. //do carries and borrows so each element of the bigInt x fits in bpe bits.
  678. function carry_(x) {
  679. var i,k,c,b;
  680. k=x.length;
  681. c=0;
  682. for (i=0;i<k;i++) {
  683. c+=x[i];
  684. b=0;
  685. if (c<0) {
  686. b=-(c>>bpe);
  687. c+=b*radix;
  688. }
  689. x[i]=c & mask;
  690. c=(c>>bpe)-b;
  691. }
  692. }
  693. //return x mod n for bigInt x and integer n.
  694. function modInt(x,n) {
  695. var i,c=0;
  696. for (i=x.length-1; i>=0; i--)
  697. c=(c*radix+x[i])%n;
  698. return c;
  699. }
  700. //convert the integer t into a bigInt with at least the given number of bits.
  701. //the returned array stores the bigInt in bpe-bit chunks, little endian (buff[0] is least significant word)
  702. //Pad the array with leading zeros so that it has at least minSize elements.
  703. //There will always be at least one leading 0 element.
  704. function int2bigInt(t,bits,minSize) {
  705. var i,k;
  706. k=Math.ceil(bits/bpe)+1;
  707. k=minSize>k ? minSize : k;
  708. buff=new Array(k);
  709. copyInt_(buff,t);
  710. return buff;
  711. }
  712. //return the bigInt given a string representation in a given base.
  713. //Pad the array with leading zeros so that it has at least minSize elements.
  714. //If base=-1, then it reads in a space-separated list of array elements in decimal.
  715. //The array will always have at least one leading zero, unless base=-1.
  716. function str2bigInt(s,base,minSize) {
  717. var d, i, j, x, y, kk;
  718. var k=s.length;
  719. if (base==-1) { //comma-separated list of array elements in decimal
  720. x=new Array(0);
  721. for (;;) {
  722. y=new Array(x.length+1);
  723. for (i=0;i<x.length;i++)
  724. y[i+1]=x[i];
  725. y[0]=parseInt(s,10);
  726. x=y;
  727. d=s.indexOf(',',0);
  728. if (d<1)
  729. break;
  730. s=s.substring(d+1);
  731. if (s.length==0)
  732. break;
  733. }
  734. if (x.length<minSize) {
  735. y=new Array(minSize);
  736. copy_(y,x);
  737. return y;
  738. }
  739. return x;
  740. }
  741. x=int2bigInt(0,base*k,0);
  742. for (i=0;i<k;i++) {
  743. d=digitsStr.indexOf(s.substring(i,i+1),0);
  744. if (base<=36 && d>=36) //convert lowercase to uppercase if base<=36
  745. d-=26;
  746. if (d>=base || d<0) { //stop at first illegal character
  747. break;
  748. }
  749. multInt_(x,base);
  750. addInt_(x,d);
  751. }
  752. for (k=x.length;k>0 && !x[k-1];k--); //strip off leading zeros
  753. k=minSize>k+1 ? minSize : k+1;
  754. y=new Array(k);
  755. kk=k<x.length ? k : x.length;
  756. for (i=0;i<kk;i++)
  757. y[i]=x[i];
  758. for (;i<k;i++)
  759. y[i]=0;
  760. return y;
  761. }
  762. //is bigint x equal to integer y?
  763. //y must have less than bpe bits
  764. function equalsInt(x,y) {
  765. var i;
  766. if (x[0]!=y)
  767. return 0;
  768. for (i=1;i<x.length;i++)
  769. if (x[i])
  770. return 0;
  771. return 1;
  772. }
  773. //are bigints x and y equal?
  774. //this works even if x and y are different lengths and have arbitrarily many leading zeros
  775. function equals(x,y) {
  776. var i;
  777. var k=x.length<y.length ? x.length : y.length;
  778. for (i=0;i<k;i++)
  779. if (x[i]!=y[i])
  780. return 0;
  781. if (x.length>y.length) {
  782. for (;i<x.length;i++)
  783. if (x[i])
  784. return 0;
  785. } else {
  786. for (;i<y.length;i++)
  787. if (y[i])
  788. return 0;
  789. }
  790. return 1;
  791. }
  792. //is the bigInt x equal to zero?
  793. function isZero(x) {
  794. var i;
  795. for (i=0;i<x.length;i++)
  796. if (x[i])
  797. return 0;
  798. return 1;
  799. }
  800. //convert a bigInt into a string in a given base, from base 2 up to base 95.
  801. //Base -1 prints the contents of the array representing the number.
  802. function bigInt2str(x,base) {
  803. if(digitsStr.length-1 < base) {
  804. var str = "base "+(digitsStr.length-1)+" is max";
  805. throw new Error(str);
  806. }
  807. var i,t,s="";
  808. if (s6.length!=x.length) {
  809. s6=dup(x);
  810. }
  811. else{
  812. copy_(s6,x);
  813. }
  814. if (base==-1) { //return the list of array contents
  815. for (i=x.length-1;i>0;i--){
  816. s+=x[i]+',';
  817. }
  818. s+=x[0];
  819. }
  820. else { //return it in the given base
  821. while (!isZero(s6)) {
  822. t=divInt_(s6,base); //t=s6 % base; s6=floor(s6/base);
  823. s=digitsStr.substring(t,t+1)+s;
  824. }
  825. }
  826. if (s.length==0){
  827. s="0";
  828. }
  829. return s;
  830. }
  831. //returns a duplicate of bigInt x
  832. function dup(x) {
  833. var i;
  834. buff=new Array(x.length);
  835. copy_(buff,x);
  836. return buff;
  837. }
  838. //do x=y on bigInts x and y. x must be an array at least as big as y (not counting the leading zeros in y).
  839. function copy_(x,y) {
  840. var i;
  841. var k=x.length<y.length ? x.length : y.length;
  842. for (i=0;i<k;i++)
  843. x[i]=y[i];
  844. for (i=k;i<x.length;i++)
  845. x[i]=0;
  846. }
  847. //do x=y on bigInt x and integer y.
  848. function copyInt_(x,n) {
  849. var i,c;
  850. for (c=n,i=0;i<x.length;i++) {
  851. x[i]=c & mask;
  852. c>>=bpe;
  853. }
  854. }
  855. //do x=x+n where x is a bigInt and n is an integer.
  856. //x must be large enough to hold the result.
  857. function addInt_(x,n) {
  858. var i,k,c,b;
  859. x[0]+=n;
  860. k=x.length;
  861. c=0;
  862. for (i=0;i<k;i++) {
  863. c+=x[i];
  864. b=0;
  865. if (c<0) {
  866. b=-(c>>bpe);
  867. c+=b*radix;
  868. }
  869. x[i]=c & mask;
  870. c=(c>>bpe)-b;
  871. if (!c) return; //stop carrying as soon as the carry is zero
  872. }
  873. }
  874. //right shift bigInt x by n bits. 0 <= n < bpe.
  875. function rightShift_(x,n) {
  876. var i;
  877. var k=Math.floor(n/bpe);
  878. if (k) {
  879. for (i=0;i<x.length-k;i++) //right shift x by k elements
  880. x[i]=x[i+k];
  881. for (;i<x.length;i++)
  882. x[i]=0;
  883. n%=bpe;
  884. }
  885. for (i=0;i<x.length-1;i++) {
  886. x[i]=mask & ((x[i+1]<<(bpe-n)) | (x[i]>>n));
  887. }
  888. x[i]>>=n;
  889. }
  890. //do x=floor(|x|/2)*sgn(x) for bigInt x in 2's complement
  891. function halve_(x) {
  892. var i;
  893. for (i=0;i<x.length-1;i++) {
  894. x[i]=mask & ((x[i+1]<<(bpe-1)) | (x[i]>>1));
  895. }
  896. x[i]=(x[i]>>1) | (x[i] & (radix>>1)); //most significant bit stays the same
  897. }
  898. //left shift bigInt x by n bits.
  899. function leftShift_(x,n) {
  900. var i;
  901. var k=Math.floor(n/bpe);
  902. if (k) {
  903. for (i=x.length; i>=k; i--) //left shift x by k elements
  904. x[i]=x[i-k];
  905. for (;i>=0;i--)
  906. x[i]=0;
  907. n%=bpe;
  908. }
  909. if (!n)
  910. return;
  911. for (i=x.length-1;i>0;i--) {
  912. x[i]=mask & ((x[i]<<n) | (x[i-1]>>(bpe-n)));
  913. }
  914. x[i]=mask & (x[i]<<n);
  915. }
  916. //do x=x*n where x is a bigInt and n is an integer.
  917. //x must be large enough to hold the result.
  918. function multInt_(x,n) {
  919. var i,k,c,b;
  920. if (!n)
  921. return;
  922. k=x.length;
  923. c=0;
  924. for (i=0;i<k;i++) {
  925. c+=x[i]*n;
  926. b=0;
  927. if (c<0) {
  928. b=-(c>>bpe);
  929. c+=b*radix;
  930. }
  931. x[i]=c & mask;
  932. c=(c>>bpe)-b;
  933. }
  934. }
  935. //do x=floor(x/n) for bigInt x and integer n, and return the remainder
  936. function divInt_(x,n) {
  937. var i,r=0,s;
  938. for (i=x.length-1;i>=0;i--) {
  939. s=r*radix+x[i];
  940. x[i]=Math.floor(s/n);
  941. r=s%n;
  942. }
  943. return r;
  944. }
  945. //do the linear combination x=a*x+b*y for bigInts x and y, and integers a and b.
  946. //x must be large enough to hold the answer.
  947. function linComb_(x,y,a,b) {
  948. var i,c,k,kk;
  949. k=x.length<y.length ? x.length : y.length;
  950. kk=x.length;
  951. for (c=0,i=0;i<k;i++) {
  952. c+=a*x[i]+b*y[i];
  953. x[i]=c & mask;
  954. c>>=bpe;
  955. }
  956. for (i=k;i<kk;i++) {
  957. c+=a*x[i];
  958. x[i]=c & mask;
  959. c>>=bpe;
  960. }
  961. }
  962. //do the linear combination x=a*x+b*(y<<(ys*bpe)) for bigInts x and y, and integers a, b and ys.
  963. //x must be large enough to hold the answer.
  964. function linCombShift_(x,y,b,ys) {
  965. var i,c,k,kk;
  966. k=x.length<ys+y.length ? x.length : ys+y.length;
  967. kk=x.length;
  968. for (c=0,i=ys;i<k;i++) {
  969. c+=x[i]+b*y[i-ys];
  970. x[i]=c & mask;
  971. c>>=bpe;
  972. }
  973. for (i=k;c && i<kk;i++) {
  974. c+=x[i];
  975. x[i]=c & mask;
  976. c>>=bpe;
  977. }
  978. }
  979. //do x=x+(y<<(ys*bpe)) for bigInts x and y, and integers a,b and ys.
  980. //x must be large enough to hold the answer.
  981. function addShift_(x,y,ys) {
  982. var i,c,k,kk;
  983. k=x.length<ys+y.length ? x.length : ys+y.length;
  984. kk=x.length;
  985. for (c=0,i=ys;i<k;i++) {
  986. c+=x[i]+y[i-ys];
  987. x[i]=c & mask;
  988. c>>=bpe;
  989. }
  990. for (i=k;c && i<kk;i++) {
  991. c+=x[i];
  992. x[i]=c & mask;
  993. c>>=bpe;
  994. }
  995. }
  996. //do x=x-(y<<(ys*bpe)) for bigInts x and y, and integers a,b and ys.
  997. //x must be large enough to hold the answer.
  998. function subShift_(x,y,ys) {
  999. var i,c,k,kk;
  1000. k=x.length<ys+y.length ? x.length : ys+y.length;
  1001. kk=x.length;
  1002. for (c=0,i=ys;i<k;i++) {
  1003. c+=x[i]-y[i-ys];
  1004. x[i]=c & mask;
  1005. c>>=bpe;
  1006. }
  1007. for (i=k;c && i<kk;i++) {
  1008. c+=x[i];
  1009. x[i]=c & mask;
  1010. c>>=bpe;
  1011. }
  1012. }
  1013. //do x=x-y for bigInts x and y.
  1014. //x must be large enough to hold the answer.
  1015. //negative answers will be 2s complement
  1016. function sub_(x,y) {
  1017. var i,c,k,kk;
  1018. k=x.length<y.length ? x.length : y.length;
  1019. for (c=0,i=0;i<k;i++) {
  1020. c+=x[i]-y[i];
  1021. x[i]=c & mask;
  1022. c>>=bpe;
  1023. }
  1024. for (i=k;c && i<x.length;i++) {
  1025. c+=x[i];
  1026. x[i]=c & mask;
  1027. c>>=bpe;
  1028. }
  1029. }
  1030. //do x=x+y for bigInts x and y.
  1031. //x must be large enough to hold the answer.
  1032. function add_(x,y) {
  1033. var i,c,k,kk;
  1034. k=x.length<y.length ? x.length : y.length;
  1035. for (c=0,i=0;i<k;i++) {
  1036. c+=x[i]+y[i];
  1037. x[i]=c & mask;
  1038. c>>=bpe;
  1039. }
  1040. for (i=k;c && i<x.length;i++) {
  1041. c+=x[i];
  1042. x[i]=c & mask;
  1043. c>>=bpe;
  1044. }
  1045. }
  1046. //do x=x*y for bigInts x and y. This is faster when y<x.
  1047. function mult_(x,y) {
  1048. var i;
  1049. if (ss.length!=2*x.length)
  1050. ss=new Array(2*x.length);
  1051. copyInt_(ss,0);
  1052. for (i=0;i<y.length;i++)
  1053. if (y[i])
  1054. linCombShift_(ss,x,y[i],i); //ss=1*ss+y[i]*(x<<(i*bpe))
  1055. copy_(x,ss);
  1056. }
  1057. //do x=x mod n for bigInts x and n.
  1058. function mod_(x,n) {
  1059. if (s4.length!=x.length){
  1060. s4=dup(x);
  1061. }
  1062. else{
  1063. copy_(s4,x);
  1064. }
  1065. if (s5.length!=x.length){
  1066. s5=dup(x);
  1067. }
  1068. divide_(s4,n,s5,x); //x = remainder of s4 / n
  1069. }
  1070. //do x=x*y mod n for bigInts x,y,n.
  1071. //for greater speed, let y<x.
  1072. function multMod_(x,y,n) {
  1073. var i;
  1074. if (s0.length!=2*x.length)
  1075. s0=new Array(2*x.length);
  1076. copyInt_(s0,0);
  1077. for (i=0;i<y.length;i++)
  1078. if (y[i])
  1079. linCombShift_(s0,x,y[i],i); //s0=1*s0+y[i]*(x<<(i*bpe))
  1080. mod_(s0,n);
  1081. copy_(x,s0);
  1082. }
  1083. //do x=x*x mod n for bigInts x,n.
  1084. function squareMod_(x,n) {
  1085. var i,j,d,c,kx,kn,k;
  1086. for (kx=x.length; kx>0 && !x[kx-1]; kx--); //ignore leading zeros in x
  1087. k=kx>n.length ? 2*kx : 2*n.length; //k=# elements in the product, which is twice the elements in the larger of x and n
  1088. if (s0.length!=k)
  1089. s0=new Array(k);
  1090. copyInt_(s0,0);
  1091. for (i=0;i<kx;i++) {
  1092. c=s0[2*i]+x[i]*x[i];
  1093. s0[2*i]=c & mask;
  1094. c>>=bpe;
  1095. for (j=i+1;j<kx;j++) {
  1096. c=s0[i+j]+2*x[i]*x[j]+c;
  1097. s0[i+j]=(c & mask);
  1098. c>>=bpe;
  1099. }
  1100. s0[i+kx]=c;
  1101. }
  1102. mod_(s0,n);
  1103. copy_(x,s0);
  1104. }
  1105. //return x with exactly k leading zero elements
  1106. function trim(x,k) {
  1107. var i,y;
  1108. for (i=x.length; i>0 && !x[i-1]; i--);
  1109. y=new Array(i+k);
  1110. copy_(y,x);
  1111. return y;
  1112. }
  1113. //do x=x**y mod n, where x,y,n are bigInts and ** is exponentiation. 0**0=1.
  1114. //this is faster when n is odd. x usually needs to have as many elements as n.
  1115. function powMod_(x,y,n) {
  1116. var k1,k2,kn,np;
  1117. if(s7.length!=n.length)
  1118. s7=dup(n);
  1119. //for even modulus, use a simple square-and-multiply algorithm,
  1120. //rather than using the more complex Montgomery algorithm.
  1121. if ((n[0]&1)==0) {
  1122. copy_(s7,x);
  1123. copyInt_(x,1);
  1124. while(!equalsInt(y,0)) {
  1125. if (y[0]&1)
  1126. multMod_(x,s7,n);
  1127. divInt_(y,2);
  1128. squareMod_(s7,n);
  1129. }
  1130. return;
  1131. }
  1132. //calculate np from n for the Montgomery multiplications
  1133. copyInt_(s7,0);
  1134. for (kn=n.length;kn>0 && !n[kn-1];kn--);
  1135. np=radix-inverseModInt(modInt(n,radix),radix);
  1136. s7[kn]=1;
  1137. multMod_(x ,s7,n); // x = x * 2**(kn*bp) mod n
  1138. if (s3.length!=x.length){
  1139. s3=dup(x);
  1140. }
  1141. else{
  1142. copy_(s3,x);
  1143. }
  1144. for (k1=y.length-1;k1>0 & !y[k1]; k1--); //k1=first nonzero element of y
  1145. if (y[k1]==0) { //anything to the 0th power is 1
  1146. copyInt_(x,1);
  1147. return;
  1148. }
  1149. for (k2=1<<(bpe-1);k2 && !(y[k1] & k2); k2>>=1); //k2=position of first 1 bit in y[k1]
  1150. for (;;) {
  1151. if (!(k2>>=1)) { //look at next bit of y
  1152. k1--;
  1153. if (k1<0) {
  1154. mont_(x,one,n,np);
  1155. return;
  1156. }
  1157. k2=1<<(bpe-1);
  1158. }
  1159. mont_(x,x,n,np);
  1160. if (k2 & y[k1]) //if next bit is a 1
  1161. mont_(x,s3,n,np);
  1162. }
  1163. }
  1164. //do x=x*y*Ri mod n for bigInts x,y,n,
  1165. // where Ri = 2**(-kn*bpe) mod n, and kn is the
  1166. // number of elements in the n array, not
  1167. // counting leading zeros.
  1168. //x array must have at least as many elemnts as the n array
  1169. //It's OK if x and y are the same variable.
  1170. //must have:
  1171. // x,y < n
  1172. // n is odd
  1173. // np = -(n^(-1)) mod radix
  1174. function mont_(x,y,n,np) {
  1175. var i,j,c,ui,t,ks;
  1176. var kn=n.length;
  1177. var ky=y.length;
  1178. if (sa.length!=kn)
  1179. sa=new Array(kn);
  1180. copyInt_(sa,0);
  1181. for (;kn>0 && n[kn-1]==0;kn--); //ignore leading zeros of n
  1182. for (;ky>0 && y[ky-1]==0;ky--); //ignore leading zeros of y
  1183. ks=sa.length-1; //sa will never have more than this many nonzero elements.
  1184. //the following loop consumes 95% of the runtime for randTruePrime_() and powMod_() for large numbers
  1185. for (i=0; i<kn; i++) {
  1186. t=sa[0]+x[i]*y[0];
  1187. ui=((t & mask) * np) & mask; //the inner "& mask" was needed on Safari (but not MSIE) at one time
  1188. c=(t+ui*n[0]) >> bpe;
  1189. t=x[i];
  1190. //do sa=(sa+x[i]*y+ui*n)/b where b=2**bpe. Loop is unrolled 5-fold for speed
  1191. j=1;
  1192. for (;j<ky-4;) { c+=sa[j]+ui*n[j]+t*y[j]; sa[j-1]=c & mask; c>>=bpe; j++;
  1193. c+=sa[j]+ui*n[j]+t*y[j]; sa[j-1]=c & mask; c>>=bpe; j++;
  1194. c+=sa[j]+ui*n[j]+t*y[j]; sa[j-1]=c & mask; c>>=bpe; j++;
  1195. c+=sa[j]+ui*n[j]+t*y[j]; sa[j-1]=c & mask; c>>=bpe; j++;
  1196. c+=sa[j]+ui*n[j]+t*y[j]; sa[j-1]=c & mask; c>>=bpe; j++; }
  1197. for (;j<ky;) { c+=sa[j]+ui*n[j]+t*y[j]; sa[j-1]=c & mask; c>>=bpe; j++; }
  1198. for (;j<kn-4;) { c+=sa[j]+ui*n[j]; sa[j-1]=c & mask; c>>=bpe; j++;
  1199. c+=sa[j]+ui*n[j]; sa[j-1]=c & mask; c>>=bpe; j++;
  1200. c+=sa[j]+ui*n[j]; sa[j-1]=c & mask; c>>=bpe; j++;
  1201. c+=sa[j]+ui*n[j]; sa[j-1]=c & mask; c>>=bpe; j++;
  1202. c+=sa[j]+ui*n[j]; sa[j-1]=c & mask; c>>=bpe; j++; }
  1203. for (;j<kn;) { c+=sa[j]+ui*n[j]; sa[j-1]=c & mask; c>>=bpe; j++; }
  1204. for (;j<ks;) { c+=sa[j]; sa[j-1]=c & mask; c>>=bpe; j++; }
  1205. sa[j-1]=c & mask;
  1206. }
  1207. if (!greater(n,sa))
  1208. sub_(sa,n);
  1209. copy_(x,sa);
  1210. }
  1211. BigInt = module.exports = {
  1212. 'add': add,
  1213. 'addInt': addInt,
  1214. 'bigInt2str': bigInt2str,
  1215. 'bitSize': bitSize,
  1216. 'copy': copy_,
  1217. 'copyInt': copyInt_,
  1218. 'dup': dup,
  1219. 'div': divide_,
  1220. 'equals': equals,
  1221. 'equalsInt': equalsInt,
  1222. 'expand': expand,
  1223. 'findPrimes': findPrimes,
  1224. 'GCD': GCD,
  1225. 'greater': greater,
  1226. 'greaterShift': greaterShift,
  1227. 'int2bigInt': int2bigInt,
  1228. 'inverseMod': inverseMod,
  1229. 'inverseModInt': inverseModInt,
  1230. 'isZero': isZero,
  1231. 'millerRabin': millerRabin,
  1232. 'millerRabinInt': millerRabinInt,
  1233. 'mod': mod,
  1234. 'modInt': modInt,
  1235. 'mult': mult_,
  1236. 'multMod': multMod,
  1237. 'negative': negative,
  1238. 'powMod': powMod,
  1239. 'randBigInt': randBigInt,
  1240. 'randTruePrime': randTruePrime,
  1241. 'randProbPrime': randProbPrime,
  1242. 'leftShift': leftShift_,
  1243. 'rightShift': rightShift_,
  1244. 'str2bigInt': str2bigInt,
  1245. 'sub': sub,
  1246. 'trim': trim
  1247. };