1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright (C) 2014 William Hart
Copyright (C) 2020 Fredrik Johansson
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "mpn_extras.h"
#include "ulong_extras.h"
#include "nmod_vec.h"
#include "fmpz.h"
#include "aprcl.h"
static int _fmpz_is_prime(const fmpz_t n, int proved)
{
double logd;
ulong p, ppi, limit;
ulong * pp1, * pm1;
slong i, l, num, num_pp1, num_pm1;
const ulong * primes;
const double * pinv;
fmpz_t F1, Fsqr, Fcub, R;
int res = -1;
if (!COEFF_IS_MPZ(*n))
{
slong v = *n;
if (v <= 1)
return 0;
/* Note: n_is_prime and n_is_probabprime are currently identical,
so we ignore the proved flag. */
return n_is_prime(v);
}
else
{
mpz_ptr z;
nn_ptr d;
slong size, bits, trial_primes;
z = COEFF_TO_PTR(*n);
size = z->_mp_size;
d = z->_mp_d;
if (size < 0)
return 0;
if (size == 1)
return n_is_prime(d[0]); /* As above */
if (d[0] % 2 == 0)
return 0;
if (size == 2 && FLINT_BITS == 64)
{
/* n_ll_is_prime does trial division, a base-2 sprp test, and may
additionally be able to certify some inputs. Currently the
certifications in n_ll_is_prime are faster than a Lucas test,
so use it even when !proved. */
res = n_ll_is_prime(d[1], d[0]);
if (res != -1)
return res;
}
else
{
bits = size * FLINT_BITS + FLINT_BIT_COUNT(d[size-1]);
trial_primes = bits;
if (flint_mpn_factor_trial(d, size, 1, trial_primes))
return 0;
/* todo: use fmpz_is_perfect_power? */
if (fmpz_is_square(n))
return 0;
/* Do a single base-2 test to rule out most composites */
fmpz base2 = 2;
if (!fmpz_is_strong_probabprime(n, &base2))
return 0;
}
}
/* At this point n has no small factor and is at least a base-2 sprp.
Adding a Lucas test makes this a BPSW test. */
if (!proved)
return fmpz_is_probabprime_lucas(n);
logd = fmpz_dlog(n);
limit = (ulong) (logd*logd*logd/100.0) + 20;
fmpz_init(F1);
fmpz_init(R);
fmpz_init(Fsqr);
fmpz_init(Fcub);
for (l = 0; l < 4 && res == -1; l++, limit *= 10)
{
num_pm1 = num_pp1 = 0;
/* number of primes multiplied that will fit in a word */
num = FLINT_BITS/FLINT_BIT_COUNT(limit);
/* compute remainders of n mod p for primes p up to limit (approx.) */
n_prime_pi_bounds(&ppi, &ppi, limit); /* precompute primes */
primes = n_primes_arr_readonly(ppi + FLINT_BITS);
pinv = n_prime_inverses_arr_readonly(ppi + FLINT_BITS);
pm1 = _nmod_vec_init(2 + (ulong) logd); /* space for primes dividing n - 1 */
pp1 = _nmod_vec_init(2 + (ulong) logd); /* space for primes dividing n + 1 */
while (primes[0] < limit)
{
/* multiply batch of primes */
p = primes[0];
for (i = 1; i < num; i++)
p *= primes[i];
/* multi-modular reduction */
p = fmpz_tdiv_ui(n, p);
/* check for factors */
for (i = 0; i < num; i++)
{
ulong r = n_mod2_precomp(p, primes[i], pinv[i]);
if (r == 1) /* n - 1 = 0 mod p */
pm1[num_pm1++] = primes[i];
if (r == primes[i] - 1) /* n + 1 = 0 mod p */
pp1[num_pp1++] = primes[i];
}
/* get next batch of primes */
primes += num;
pinv += num;
}
/* p - 1 test */
res = fmpz_is_prime_pocklington(F1, R, n, pm1, num_pm1);
if (res == 1)
{
fmpz_mul(Fsqr, F1, F1);
if (fmpz_cmp(Fsqr, n) < 0)
{
fmpz_mul(Fcub, Fsqr, F1);
if (fmpz_cmp(Fcub, n) >= 0) /* Brillhart, Lehmer, Selfridge test */
{
fmpz_t n1, c2, c1;
fmpz_init(n1);
fmpz_init(c2);
fmpz_init(c1);
fmpz_sub_ui(n1, n, 1); /* n is 1 mod F1 */
fmpz_tdiv_q(n1, n1, F1);
fmpz_tdiv_qr(c2, c1, n1, F1); /* Let n = c2*F^2 + c1*F + 1 */
fmpz_mul(c1, c1, c1); /* check if c1^2 - 4*c2 is a square */
fmpz_submul_ui(c1, c2, 4);
if (fmpz_is_square(c1))
res = 0;
/* else n is prime (res == 1) */
fmpz_clear(n1);
fmpz_clear(c2);
fmpz_clear(c1);
} else /* p + 1 test */
{
fmpz_t F2, Fm1;
fmpz_init(F2);
fmpz_init(Fm1);
res = fmpz_is_prime_morrison(F2, R, n, pp1, num_pp1);
if (res == 1)
{
fmpz_sub_ui(Fm1, F2, 1); /* need F2 - 1 > sqrt(n) */
fmpz_mul(Fsqr, Fm1, Fm1);
if (fmpz_cmp(Fsqr, n) <= 0)
{
fmpz_mul(Fcub, Fsqr, Fm1);
if (fmpz_cmp(Fcub, n) > 0) /* Improved n + 1 test */
{
fmpz_t r1, r0, b, r, t;
fmpz_init(r1);
fmpz_init(r0);
fmpz_init(b);
fmpz_init(r);
fmpz_init(t);
fmpz_tdiv_qr(r1, r0, R, F2); /* R = r1*F2 + r0 */
/* check if x^2 + r0*x - r1 has positive integral root */
fmpz_mul(t, r0, r0); /* b = sqrt(r0^2 - 4(-r1)) */
fmpz_addmul_ui(t, r1, 4);
fmpz_sqrtrem(b, r, t);
if (fmpz_is_zero(r) && fmpz_cmp(b, r0) > 0) /* if so, composite */
res = 0;
/* check if x^2 + (r0 - F2)*x - r1 - 1 has positive integral root */
fmpz_sub(r0, r0, F2);
fmpz_add_ui(r1, r1, 1);
fmpz_mul(t, r0, r0); /* b = sqrt((r0 - F2)^2 - 4(-r1 - 1)) */
fmpz_addmul_ui(t, r1, 4);
fmpz_sqrtrem(b, r, t);
if (fmpz_is_zero(r) && fmpz_cmp(b, r0) > 0) /* if so, composite */
res = 0;
fmpz_clear(t);
fmpz_clear(b);
fmpz_clear(r);
fmpz_clear(r1);
fmpz_clear(r0);
} else /* Brillhart, Lehmer, Selfridge combined p-1, p+1 test */
{
fmpz_t F, nmodF;
fmpz_init(F);
fmpz_mul(F, F1, F2); /* F = lcm(F1, F2), F1 | n - 1, F2 | n + 1 */
if (fmpz_is_even(F1) && fmpz_is_even(F2))
fmpz_tdiv_q_2exp(F, F, 1);
fmpz_mul(Fsqr, F, F);
if (fmpz_cmp(Fsqr, n) > 0) /* lcm(F1, F2) > sqrt(n) */
{
fmpz_init(nmodF);
fmpz_mod(nmodF, n, F); /* check n mod F not factor of n */
if (!fmpz_equal(nmodF, n) && !fmpz_is_one(nmodF)
&& fmpz_divisible(n, nmodF))
res = 0;
fmpz_clear(nmodF);
} else
{
fmpz_t d;
fmpz_init(d);
fmpz_mul(Fcub, Fsqr, F);
if (fmpz_cmp(Fcub, n) > 0) /* Lenstra's divisors in residue class */
{
fmpz_t r;
fmpz_init(r);
fmpz_set_ui(r, 1);
if (fmpz_divisor_in_residue_class_lenstra(d, n, r, F))
res = 0;
fmpz_mod(r, n, F);
if (fmpz_divisor_in_residue_class_lenstra(d, n, r, F))
res = 0;
fmpz_clear(r);
} else /* apr-cl primality test */
{
res = aprcl_is_prime(n);
}
fmpz_clear(d);
}
fmpz_clear(F);
}
}
/* else n is prime, i.e. res = 1 */
}
fmpz_clear(F2);
fmpz_clear(Fm1);
}
}
}
_nmod_vec_clear(pm1);
_nmod_vec_clear(pp1);
}
/* aprcl_is_prime() actually throws, but it does not hurt to have
this fallback here */
if (res < 0)
{
flint_throw(FLINT_ERROR, "Failed to prove %s prime or composite\n", fmpz_get_str(NULL, 10, n));
}
fmpz_clear(F1);
fmpz_clear(R);
fmpz_clear(Fsqr);
fmpz_clear(Fcub);
return res;
}
int
fmpz_is_probabprime(const fmpz_t n)
{
return _fmpz_is_prime(n, 0);
}
int
fmpz_is_prime(const fmpz_t n)
{
return _fmpz_is_prime(n, 1);
}