forked from flintlib/flint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqrt.c
271 lines (228 loc) · 6.91 KB
/
sqrt.c
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
/*=============================================================================
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2011 Jan Tuitman
Copyright (C) 2011, 2012 Sebastian Pancratz
******************************************************************************/
#include "padic.h"
/*
Returns whether \code{op} has a square root modulo $p^N$ and if
so sets \code{rop} to such an element.
Assumes that \code{op} is a unit modulo $p^N$. Assumes $p$ is an
odd prime.
In the current implementation, allows aliasing.
*/
static int _padic_sqrt_p(fmpz_t rop, const fmpz_t op, const fmpz_t p, long N)
{
int ans;
if (N == 1)
{
ans = fmpz_sqrtmod(rop, op, p);
return ans;
}
else
{
long *e, i, n;
fmpz *W, *pow, *u;
n = FLINT_CLOG2(N) + 1;
/* Compute sequence of exponents */
e = flint_malloc(n * sizeof(long));
for (e[i = 0] = N; e[i] > 1; i++)
e[i + 1] = (e[i] + 1) / 2;
W = _fmpz_vec_init(2 + 2 * n);
pow = W + 2;
u = W + (2 + n);
/* Compute powers of p */
{
fmpz_one(W);
fmpz_set(pow + i, p);
}
for (i--; i >= 1; i--)
{
if (e[i] & 1L)
{
fmpz_mul(pow + i, W, pow + (i + 1));
fmpz_mul(W, W, W);
}
else
{
fmpz_mul(W, W, pow + (i + 1));
fmpz_mul(pow + i, pow + (i + 1), pow + (i + 1));
}
}
{
if (e[i] & 1L)
fmpz_mul(pow + i, W, pow + (i + 1));
else
fmpz_mul(pow + i, pow + (i + 1), pow + (i + 1));
}
/* Compute reduced units */
{
fmpz_mod(u, op, pow);
}
for (i = 1; i < n; i++)
{
fmpz_mod(u + i, u + (i - 1), pow + i);
}
/*
Run Newton iteration for the inverse square root,
using the update formula
z := z - z (u z^2 - 1) / 2
for all but the last step. The last step is
replaced with
b := u z mod p^{N'}
z := b + z (u - b^2) / 2 mod p^{N}.
*/
i = n - 1;
{
ans = fmpz_sqrtmod(rop, u + i, p);
if (!ans)
goto exit;
fmpz_invmod(rop, rop, p);
}
for (i--; i >= 1; i--)
{
fmpz_mul(W, rop, rop);
fmpz_mul(W + 1, u + i, W);
fmpz_sub_ui(W + 1, W + 1, 1);
if (fmpz_is_odd(W + 1))
fmpz_add(W + 1, W + 1, pow + i);
fmpz_fdiv_q_2exp(W + 1, W + 1, 1);
fmpz_mul(W, W + 1, rop);
fmpz_sub(rop, rop, W);
fmpz_mod(rop, rop, pow + i);
}
{
fmpz_mul(W, u + 1, rop);
fmpz_mul(W + 1, W, W);
fmpz_sub(W + 1, u + 0, W + 1);
if (fmpz_is_odd(W + 1))
fmpz_add(W + 1, W + 1, pow + 0);
fmpz_fdiv_q_2exp(W + 1, W + 1, 1);
fmpz_mul(rop, rop, W + 1);
fmpz_add(rop, W, rop);
fmpz_mod(rop, rop, pow + 0);
}
exit:
flint_free(e);
_fmpz_vec_clear(W, 2 + 2 * n);
return ans;
}
}
/*
Returns whether \code{op} has a square root modulo $2^N$ and if
so sets \code{rop} to such an element.
Assumes that \code{op} is a unit modulo $2^N$.
In the current implementation, allows aliasing.
*/
static int _padic_sqrt_2(fmpz_t rop, const fmpz_t op, long N)
{
if (fmpz_fdiv_ui(op, 8) != 1)
return 0;
if (N <= 3)
{
fmpz_one(rop);
}
else
{
long *e, i, n;
fmpz *W, *u;
i = FLINT_CLOG2(N);
/* Compute sequence of exponents */
e = flint_malloc((i + 2) * sizeof(long));
for (e[i = 0] = N; e[i] > 3; i++)
e[i + 1] = (e[i] + 3) / 2;
n = i + 1;
W = _fmpz_vec_init(2 + n);
u = W + 2;
/* Compute reduced units */
{
fmpz_fdiv_r_2exp(u, op, e[0]);
}
for (i = 1; i < n; i++)
{
fmpz_fdiv_r_2exp(u + i, u + (i - 1), e[i]);
}
/* Run Newton iteration */
fmpz_one(rop);
for (i = n - 2; i >= 1; i--) /* z := z - z (a z^2 - 1) / 2 */
{
fmpz_mul(W, rop, rop);
fmpz_mul(W + 1, u + i, W);
fmpz_sub_ui(W + 1, W + 1, 1);
fmpz_fdiv_q_2exp(W + 1, W + 1, 1);
fmpz_mul(W, W + 1, rop);
fmpz_sub(rop, rop, W);
fmpz_fdiv_r_2exp(rop, rop, e[i]);
}
{
fmpz_mul(W, u + 1, rop);
fmpz_mul(W + 1, W, W);
fmpz_sub(W + 1, u + 0, W + 1);
fmpz_fdiv_q_2exp(W + 1, W + 1, 1);
fmpz_mul(rop, rop, W + 1);
fmpz_add(rop, W, rop);
}
fmpz_fdiv_r_2exp(rop, rop, e[0]);
flint_free(e);
_fmpz_vec_clear(W, 2 + n);
}
return 1;
}
int _padic_sqrt(fmpz_t rop, const fmpz_t op, const fmpz_t p, long N)
{
if (*p == 2L)
{
return _padic_sqrt_2(rop, op, N);
}
else
{
return _padic_sqrt_p(rop, op, p, N);
}
}
int padic_sqrt(padic_t rop, const padic_t op, const padic_ctx_t ctx)
{
if (_padic_is_zero(op))
{
padic_zero(rop);
return 1;
}
if (padic_val(op) & 1L)
{
return 0;
}
padic_val(rop) = padic_val(op) / 2;
/*
In this case, if there is a square root it will be
zero modulo $p^N$. We only have to establish whether
or not the element \code{op} is a square.
*/
if (padic_val(rop) >= ctx->N)
{
int ans;
if (*(ctx->p) == 2L)
{
ans = (fmpz_fdiv_ui(padic_unit(op), 8) == 1);
}
else
{
ans = fmpz_sqrtmod(padic_unit(rop), padic_unit(op), ctx->p);
}
padic_zero(rop);
return ans;
}
return _padic_sqrt(padic_unit(rop),
padic_unit(op), ctx->p, ctx->N - padic_val(rop));
}