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
/*
Copyright (C) 2015 Kushagra Singh
Copyright (C) 2025 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 "ulong_extras.h"
#include "nmod.h"
static ulong
n_sqr_and_add_a_fast(ulong y, ulong a, nmod_redc_ctx_t ctx)
{
y = nmod_redc_fast_mul(y, y, ctx);
/* Has a negligible chance of not being reduced, which is fine. */
y += a;
return y;
}
static ulong
n_sqr_and_add_a(ulong y, ulong a, nmod_redc_ctx_t ctx)
{
y = nmod_redc_mul(y, y, ctx);
/* Has a negligible chance of not being reduced, which is fine. */
y += a;
return y;
}
static ulong
n_absdiff(ulong x, ulong y)
{
if (x > y)
return x - y;
else
return y - x;
}
int
n_factor_pollard_brent_single(ulong *factor, ulong n,
ulong ai, ulong xi, ulong max_iters)
{
ulong iter, i, k, j, minval, m, x, y, a, q, ys;
int ret;
int fast;
if (n < 4 || n % 2 == 0)
return 0;
nmod_redc_ctx_t ctx;
nmod_redc_ctx_init_ui(ctx, n);
fast = nmod_redc_can_use_fast(ctx);
q = 1;
(*factor) = 1;
y = xi;
a = ai;
m = 100;
iter = 1;
do {
x = y;
k = 0;
if (fast)
{
for (i = 0; i < iter; i++)
y = n_sqr_and_add_a_fast(y, a, ctx);
}
else
{
for (i = 0; i < iter; i++)
y = n_sqr_and_add_a(y, a, ctx);
}
do {
minval = iter - k;
if (m < minval)
minval = m;
ys = y;
if (fast)
{
for (i = 0; i < minval; i++)
{
y = n_sqr_and_add_a_fast(y, a, ctx);
q = nmod_redc_fast_mul(q, n_absdiff(x, y), ctx);
}
}
else
{
for (i = 0; i < minval; i++)
{
y = n_sqr_and_add_a(y, a, ctx);
q = nmod_redc_mul(q, n_absdiff(x, y), ctx);
}
}
if (q == 0)
(*factor) = n;
else
(*factor) = n_gcd(q, n);
k += m;
j = ((*factor) == 1);
} while ((k < iter) && (j));
if (iter > max_iters)
break;
iter *= 2;
} while (j);
if ((*factor) == n)
{
if (fast)
{
do {
ys = n_sqr_and_add_a_fast(ys, a, ctx);
if (q == 0)
(*factor) = n;
else
(*factor) = n_gcd(q, n);
(*factor) = n_gcd(n_absdiff(x, ys), n);
} while ((*factor) == 1); /* gcd == 1 */
}
else
{
do {
ys = n_sqr_and_add_a(ys, a, ctx);
if (q == 0)
(*factor) = n;
else
(*factor) = n_gcd(q, n);
(*factor) = n_gcd(n_absdiff(x, ys), n);
} while ((*factor) == 1); /* gcd == 1 */
}
}
ret = 1;
if ((*factor) == 1) /* gcd == 1 */
ret = 0;
else if ((*factor) == n) /* gcd == n*/
ret = 0;
return ret;
}
int
n_factor_pollard_brent(ulong *factor, flint_rand_t state, ulong n,
ulong max_tries, ulong max_iters)
{
ulong a, x;
int ret = 0;
while (max_tries--)
{
/* Keep a small so that additions without reduction have a high
chance of remaining valid. */
a = n_randint(state, FLINT_MIN(n - 4, 1024)) + 1; /* 1 <= a <= n - 3 */
x = n_randint(state, n - 2) + 1; /* 1 <= a <= n - 3 */
ret = n_factor_pollard_brent_single(factor, n, a, x, max_iters);
if (ret == 1)
return 1;
a += 1;
}
return ret;
}