@@ -25,7 +25,7 @@ static int _fmpz_is_prime(const fmpz_t n, int proved)
2525 const ulong * primes ;
2626 const double * pinv ;
2727
28- fmpz_t F1 , Fsqr , Fcub , R , t ;
28+ fmpz_t F1 , Fsqr , Fcub , R ;
2929 int res = -1 ;
3030
3131 if (!COEFF_IS_MPZ (* n ))
@@ -34,9 +34,8 @@ static int _fmpz_is_prime(const fmpz_t n, int proved)
3434 if (v <= 1 )
3535 return 0 ;
3636
37- /* Note: we want n_is_prime rather than n_is_probabprime
38- even when proved == 0, because ns_is_prime handles general
39- input faster. */
37+ /* Note: n_is_prime and n_is_probabprime are currently identical,
38+ so we ignore the proved flag. */
4039 return n_is_prime (v );
4140 }
4241 else
@@ -57,45 +56,39 @@ static int _fmpz_is_prime(const fmpz_t n, int proved)
5756 if (d [0 ] % 2 == 0 )
5857 return 0 ;
5958
60- bits = size * FLINT_BITS + FLINT_BIT_COUNT (d [size - 1 ]);
61- trial_primes = bits ;
59+ if (size == 2 && FLINT_BITS == 64 )
60+ {
61+ /* n_ll_is_prime does trial division, a base-2 sprp test, and may
62+ additionally be able to certify some inputs. Currently the
63+ certifications in n_ll_is_prime are faster than a Lucas test,
64+ so use it even when !proved. */
65+ res = n_ll_is_prime (d [1 ], d [0 ]);
66+ if (res != -1 )
67+ return res ;
68+ }
69+ else
70+ {
71+ bits = size * FLINT_BITS + FLINT_BIT_COUNT (d [size - 1 ]);
72+ trial_primes = bits ;
6273
63- if (flint_mpn_factor_trial (d , size , 1 , trial_primes ))
64- return 0 ;
65- }
74+ if (flint_mpn_factor_trial (d , size , 1 , trial_primes ))
75+ return 0 ;
6676
67- /* todo: use fmpz_is_perfect_power? */
68- if (fmpz_is_square (n ))
69- return 0 ;
77+ /* todo: use fmpz_is_perfect_power? */
78+ if (fmpz_is_square (n ))
79+ return 0 ;
7080
71- if (!proved )
72- return fmpz_is_probabprime_BPSW (n );
73-
74- /* Fast deterministic Miller-Rabin test up to about 81 bits. This choice of
75- bases certifies primality for n < 3317044064679887385961981;
76- see https://doi.org/10.1090/mcom/3134 */
77- fmpz_init (t );
78- fmpz_tdiv_q_2exp (t , n , 64 );
79- if (fmpz_cmp_ui (t , 179817 ) < 0 )
80- {
81- static const char bases [] = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 0 };
82-
83- for (i = 0 ; bases [i ] != 0 ; i ++ )
84- {
85- fmpz_set_ui (t , bases [i ]);
86- if (!fmpz_is_strong_probabprime (n , t ))
87- return 0 ; /* no need to clear t since it is small */
81+ /* Do a single base-2 test to rule out most composites */
82+ fmpz base2 = 2 ;
83+ if (!fmpz_is_strong_probabprime (n , & base2 ))
84+ return 0 ;
8885 }
89-
90- return 1 ;
9186 }
9287
93- /* Do a single base-2 test to rule out most composites */
94- fmpz_set_ui (t , 2 );
95- if (!fmpz_is_strong_probabprime (n , t ))
96- return 0 ;
97-
98- fmpz_clear (t );
88+ /* At this point n has no small factor and is at least a base-2 sprp.
89+ Adding a Lucas test makes this a BPSW test. */
90+ if (!proved )
91+ return fmpz_is_probabprime_lucas (n );
9992
10093 logd = fmpz_dlog (n );
10194 limit = (ulong ) (logd * logd * logd /100.0 ) + 20 ;
0 commit comments