@@ -42,35 +42,35 @@ static int gaIClz(uint64_t n);
4242
4343/**
4444 * @brief Integer Modular Addition.
45- *
45+ *
4646 * Computes
47- *
47+ *
4848 * $$a+b \pmod m$$
49- *
49+ *
5050 * efficiently for 64-bit unsigned integers a, b, m.
5151 */
5252
5353static uint64_t gaIAddMod (uint64_t a , uint64_t b , uint64_t m );
5454
5555/**
5656 * @brief Integer Modular Subtraction.
57- *
57+ *
5858 * Computes
59- *
59+ *
6060 * $$a-b \pmod m$$
61- *
61+ *
6262 * efficiently for 64-bit unsigned integers a, b, m.
6363 */
6464
6565static uint64_t gaISubMod (uint64_t a , uint64_t b , uint64_t m );
6666
6767/**
6868 * @brief Integer Modular Average.
69- *
69+ *
7070 * Computes
71- *
71+ *
7272 * $$\frac{a+b}{2} \pmod m$$
73- *
73+ *
7474 * efficiently for 64-bit unsigned integers a, b, m.
7575 */
7676
@@ -102,19 +102,19 @@ static uint64_t gaIPowMod (uint64_t x, uint64_t a, uint64_t m);
102102
103103/**
104104 * @brief Jacobi Symbol
105- *
105+ *
106106 * Computes the Jacobi symbol, notated
107- *
107+ *
108108 * $$(a/n)$$
109- *
109+ *
110110 * efficiently for 64-bit unsigned integers a, n.
111111 */
112112
113113static int gaIJacobiSymbol (uint64_t a , uint64_t n );
114114
115115/**
116116 * @brief Strong Fermat base-a probable prime test.
117- *
117+ *
118118 * @param [in] n An odd integer >= 3.
119119 * @param [in] a A witness integer > 0.
120120 * @return Non-zero if n is a strong probable prime to base a and zero if n is
@@ -125,9 +125,9 @@ static int gaIIsPrimeStrongFermat(uint64_t n, uint64_t a);
125125
126126/**
127127 * @brief Strong Lucas probable prime test.
128- *
128+ *
129129 * The function uses Selfridge's Method A for selecting D,P,Q.
130- *
130+ *
131131 * @param [in] n An odd integer >= 3.
132132 * @return Non-zero if n is a strong probable prime and zero if n is composite.
133133 */
@@ -224,7 +224,7 @@ static int gaIClz (uint64_t n){
224224static uint64_t gaIAddMod (uint64_t a , uint64_t b , uint64_t m ){
225225 a %= m ;
226226 b %= m ;
227-
227+
228228 if (m - a > b ){
229229 return a + b ;
230230 }else {
@@ -235,7 +235,7 @@ static uint64_t gaIAddMod (uint64_t a, uint64_t b, uint64_t m){
235235static uint64_t gaISubMod (uint64_t a , uint64_t b , uint64_t m ){
236236 a %= m ;
237237 b %= m ;
238-
238+
239239 if (a >= b ){
240240 return a - b ;
241241 }else {
@@ -245,7 +245,7 @@ static uint64_t gaISubMod (uint64_t a, uint64_t b, uint64_t m){
245245
246246static uint64_t gaIAvgMod (uint64_t a , uint64_t b , uint64_t m ){
247247 uint64_t s = gaIAddMod (a ,b ,m );
248-
248+
249249 if (s & 1 ){
250250 return (s >>1 )+ (m >>1 )+ (s & m & 1 );
251251 }else {
@@ -389,32 +389,32 @@ static uint64_t gaIPowMod (uint64_t x, uint64_t a, uint64_t m){
389389static int gaIJacobiSymbol (uint64_t a , uint64_t n ){
390390 int s = 0 ;
391391 uint64_t e , a1 , n1 ;
392-
392+
393393 a %= n ;
394-
394+
395395 if (a == 1 || n == 1 ){
396396 return 1 ;
397397 }
398-
398+
399399 if (a == 0 ){
400400 return 0 ;
401401 }
402-
402+
403403 e = gaICtz (a );
404404 a1 = a >> e ;
405-
405+
406406 if (e %2 == 0 ){
407407 s = 1 ;
408408 }else if (n %8 == 1 || n %8 == 7 ){
409409 s = 1 ;
410410 }else if (n %8 == 3 || n %8 == 5 ){
411411 s = -1 ;
412412 }
413-
413+
414414 if (n %4 == 3 && a1 %4 == 3 ){
415415 s = - s ;
416416 }
417-
417+
418418 n1 = n %a1 ;
419419 return s * gaIJacobiSymbol (n1 ,a1 );
420420}
@@ -426,32 +426,32 @@ static int gaIIsPrimeStrongFermat(uint64_t n, uint64_t a){
426426 * Should it fail to prove an integer composite, it reports the number as
427427 * "probably prime". However, if the witnesses are chosen carefully, the
428428 * Miller-Rabin test can be made deterministic below a chosen threshold.
429- *
429+ *
430430 * One can use the primes 2 to 37 in order to ensure the correctness of the
431431 * identifications for integers under 2^64.
432- *
432+ *
433433 * Jim Sinclair has found that the seven witnesses
434434 * 2, 325, 9375, 28178, 450775, 9780504, 1795265022
435435 * also deterministically classify all integers <2^64.
436- *
437- *
436+ *
437+ *
438438 * The Fermat strong probable prime test states that, for integers
439439 * n = d*2^s+1, d odd, s integer >= 0
440440 * a integer (chosen witness)
441441 * n is a Fermat strong probable prime if
442442 * a^(d ) = 1 mod n or
443443 * a^(d*2^r) = -1 mod n for any integer r, 0 <= r < s.
444- *
445- *
444+ *
445+ *
446446 * The justification for this comes from Fermat's Little Theorem: If n is
447447 * prime and a is any integer, then the following always holds:
448448 * a^n = a mod n
449449 * If n is prime and a is coprime to n, then the following always holds:
450450 * a^(n-1) = 1 mod n
451- *
452- *
451+ *
452+ *
453453 * In effect, the logic goes
454- *
454+ *
455455 * A: The number n is prime. (Statement)
456456 * B: The number n does not divide a. (Statement)
457457 * C: a^( n-1) = 1 mod n (Statement)
@@ -466,7 +466,7 @@ static int gaIIsPrimeStrongFermat(uint64_t n, uint64_t a){
466466 * L: a^(d*2^(r+1)) = 1 mod n for some 0 <= r < s. (Statement)
467467 * M: a^(d*2^r) != +-1 mod n AND (Statement)
468468 * a^(d*2^(r+1)) = 1 mod n for some 0 <= r < s.
469- *
469+ *
470470 * A&B --> C (Proposition: Fermat's Little Theorem)
471471 * !C --> !(A&B) = !A|!B (Contrapositive: Fermat's Little Theorem)
472472 * A <-> D (Proposition)
@@ -501,7 +501,7 @@ static int gaIIsPrimeStrongFermat(uint64_t n, uint64_t a){
501501 * ***** Conclusions: *****
502502 * H&I&M --> !A
503503 * H&I&!(J|K)&B --> !A
504- *
504+ *
505505 * Broadly speaking, what the above tells us is:
506506 * - We can't prove n prime (A), but we can prove it composite (!A).
507507 * - Either H&I&M or H&I&!(J|K)&B prove compositeness.
@@ -510,23 +510,23 @@ static int gaIIsPrimeStrongFermat(uint64_t n, uint64_t a){
510510 * conclusions about the truth-value of A can be made. The test is
511511 * inconclusive. Thus this function returns "probably prime".
512512 */
513-
513+
514514 uint64_t d , x ;
515515 int64_t s , r ;
516-
516+
517517 a %= n ;
518518 if (a == 0 ){
519519 return GA_IS_PROBABLY_PRIME ;
520520 }
521-
521+
522522 s = gaICtz (n - 1 );
523523 d = (n - 1 ) >> s ;
524524 x = gaIPowMod (a ,d ,n );
525-
525+
526526 if (x == 1 || x == n - 1 ){
527527 return GA_IS_PROBABLY_PRIME ;
528528 }
529-
529+
530530 for (r = 0 ;r < s - 1 ;r ++ ){
531531 x = gaIMulMod (x ,x ,n );
532532 if (x == 1 ){
@@ -535,72 +535,72 @@ static int gaIIsPrimeStrongFermat(uint64_t n, uint64_t a){
535535 return GA_IS_PROBABLY_PRIME ;
536536 }
537537 }
538-
538+
539539 return GA_IS_COMPOSITE ;
540540}
541541
542542static int gaIIsPrimeStrongLucas (uint64_t n ){
543543 uint64_t Dp , Dm , D , K , U , Ut , V , Vt ;
544544 int J , r , i ;
545-
545+
546546 /**
547547 * FIPS 186-4 C.3.3 (General) Lucas Probabilistic Primality Test
548- *
548+ *
549549 * 1. Test if n is perfect square. If so, return "composite".
550- *
550+ *
551551 * NOTE: The only strong base-2 Fermat pseudoprime squares are
552552 * 1194649 and 12327121;
553553 */
554-
554+
555555 if (n == 1194649 || n == 12327121 ){
556556 return GA_IS_COMPOSITE ;
557557 }
558-
558+
559559 /**
560560 * 2. Find first D in sequence 5,-7,9,-11,... s.t. Jacobi symbol (D/n) < 1.
561561 * Iff Jacobi symbol is 0, return "composite".
562562 */
563-
563+
564564 Dp = gaIAddMod (0 , 5 , n );
565565 Dm = gaISubMod (0 , 7 , n );
566566 while (1 ){
567567 J = gaIJacobiSymbol (Dp , n );
568568 if (J == 0 ){return GA_IS_COMPOSITE ;}
569569 else if (J == -1 ){D = Dp ;break ;}
570-
570+
571571 J = gaIJacobiSymbol (Dm , n );
572572 if (J == 0 ){return GA_IS_COMPOSITE ;}
573573 else if (J == -1 ){D = Dm ;break ;}
574-
574+
575575 Dp = gaIAddMod (Dp , 4 , n );
576576 Dm = gaISubMod (Dm , 4 , n );
577577 }
578-
578+
579579 /**
580580 * 3. K = n+1
581- *
581+ *
582582 * NOTE: Cannot overflow, since 2^64-1 is eliminated by strong Fermat
583583 * base-2 test.
584584 */
585-
585+
586586 K = n + 1 ;
587-
587+
588588 /**
589589 * 4. Let Kr, Kr–1, ..., K0 be the binary expansion of K, with Kr = 1.
590590 */
591-
591+
592592 r = 63 - gaIClz (K );
593-
593+
594594 /**
595595 * 5. Set Ur = 1 and Vr = 1.
596596 */
597-
597+
598598 U = V = 1 ;
599-
599+
600600 /**
601601 * 6. For i=r–1 to 0, do
602602 */
603-
603+
604604 for (i = r - 1 ;i >=0 ;i -- ){
605605 Ut = gaIMulMod (U ,V ,n );
606606 Vt = gaIAvgMod (gaIMulMod (V ,V ,n ), gaIMulMod (D ,gaIMulMod (U ,U ,n ),n ), n );
@@ -612,11 +612,11 @@ static int gaIIsPrimeStrongLucas(uint64_t n){
612612 V = Vt ;
613613 }
614614 }
615-
615+
616616 /**
617617 * 7. If U0==0, then return "probably prime". Otherwise, return "composite".
618618 */
619-
619+
620620 return U == 0 ? GA_IS_PROBABLY_PRIME : GA_IS_COMPOSITE ;
621621}
622622
@@ -761,7 +761,7 @@ int gaIFactorize (uint64_t n, uint64_t maxN, uint64_t k, ga_factor_list* fl
761761
762762 /**
763763 * Master loop.
764- *
764+ *
765765 * We arrive here with finite slack and all optimal 2-, 3- and 5-smooth
766766 * factorizers unable to produce a factorization whose product is less
767767 * than or equal to maxN.
@@ -817,7 +817,7 @@ int gaIFactorize (uint64_t n, uint64_t maxN, uint64_t k, ga_factor_list* fl
817817 }else {
818818 p = gaIFLGetProduct (fl );
819819 newX = n /p ;
820- newX += newX * p < n ;
820+ newX += newX * p < n ;
821821 if (newX < x ){
822822 x = newX ;
823823 goto subfactorize ;
0 commit comments