bigint.js 37 KB

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