-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZmodF_poly.h
368 lines (250 loc) · 11 KB
/
ZmodF_poly.h
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*============================================================================
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
===============================================================================*/
/****************************************************************************
ZmodF_poly.h
Polynomials over Z/pZ, where p = the Fermat number B^n + 1, where
B = 2^FLINT_BITS. Routines for truncated Schoenhage-Strassen FFTs
and convolutions.
Copyright (C) 2007, William Hart and David Harvey
*****************************************************************************/
#ifndef FLINT_ZMODFPOLY_H
#define FLINT_ZMODFPOLY_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <stdio.h>
#include <gmp.h>
#include "memory-manager.h"
#include "ZmodF.h"
/****************************************************************************
ZmodF_poly_t
-----------
ZmodF_poly_t represents a polynomial with coefficients in Z/pZ, where
p = B^n + 1, B = 2^FLINT_BITS. Coefficients are represented in the
format described in ZmodF.h.
Each polynomial has a fixed transform length 2^depth, specified at creation
time, where depth >= 0.
A polynomial may be in either "coefficient representation" (list of
coefficients of the polynomial), or "fourier representation" (list of
fourier coefficients). The polynomial does not keep track of which form it
is in, this is just a conceptual distinction.
x.length indicates how many coefficients contain meaningful data. If x is in
coefficient representation, the remaining coefficients are assumed to be
*zero*. If x is in fourier representation, the remaining coefficients are not
necessarily zero, they are simply *unknown*.
Always 0 <= length <= 2^depth.
Each polynomial carries a number of additional scratch buffers. The number of
scratch buffers is set at creation time. Various routines require a certain
number of scratch buffers to be present. The scratch buffers and coefficient
buffers are allocated as one large block, and routines may *permute* them,
so that outputs may well end up in what was originally a scratch buffer.
*/
typedef struct
{
unsigned long depth;
unsigned long n;
unsigned long length;
// Single chunk of memory where all coefficients live.
mp_limb_t* storage;
// Array of pointers to coefficients (length 2^depth).
ZmodF_t* coeffs;
// Array of pointers to scratch buffers (length scratch_count).
unsigned long scratch_count;
ZmodF_t* scratch;
} ZmodF_poly_struct;
// ZmodF_poly_t allows reference-like semantics for ZpolyFPoly_struct:
typedef ZmodF_poly_struct ZmodF_poly_t[1];
typedef ZmodF_poly_struct * ZmodF_poly_p;
/****************************************************************************
Memory Management Routines
****************************************************************************/
/*
Initialises a ZmodF_poly_t with supplied parameters, and length = 0.
Coefficients are not zeroed out.
*/
void ZmodF_poly_init(ZmodF_poly_t poly, unsigned long depth, unsigned long n,
unsigned long scratch_count);
void ZmodF_poly_stack_init(ZmodF_poly_t poly, unsigned long depth, unsigned long n,
unsigned long scratch_count);
/*
Frees resources for the given polynomial.
*/
void ZmodF_poly_clear(ZmodF_poly_t poly);
void ZmodF_poly_stack_clear(ZmodF_poly_t poly);
/*
Decrease the number of limbs n that are meaningful in a ZmodF_poly_t.
The actual number of limbs allocated remains the same, only the field
n is adjusted.
*/
static inline
void ZmodF_poly_decrease_n(ZmodF_poly_t poly, unsigned long n)
{
FLINT_ASSERT(n <= poly->n);
poly->n = n;
}
/****************************************************************************
Basic Arithmetic Routines
****************************************************************************/
/*
Sets x := y.
Only y.length coefficients are copied.
PRECONDITIONS:
x and y must have compatible dimensions.
*/
void ZmodF_poly_set(ZmodF_poly_t x, ZmodF_poly_t y);
/*
Sets res := pointwise product of x and y mod p.
Only coefficients up to x.length are multiplied.
PRECONDITIONS:
Any combination of aliasing among res, x, y is allowed.
x, y, res must have compatible dimensions.
x and y must have the same length.
NOTE:
This function normalises the coefficients before multiplying.
*/
void ZmodF_poly_pointwise_mul(ZmodF_poly_t res, ZmodF_poly_t x, ZmodF_poly_t y);
/*
Sets res := x + y mod p.
Only coefficients up to x.length are added.
PRECONDITIONS:
Any combination of aliasing among res, x, y is allowed.
x, y, res must have compatible dimensions.
x and y must have the same length.
NOTE:
This function does *not* normalise before subtracting. Be careful
with the overflow limb.
*/
void ZmodF_poly_add(ZmodF_poly_t res, ZmodF_poly_t x, ZmodF_poly_t y);
/*
Sets res := x - y mod p.
Only coefficients up to x.length are subtracted.
PRECONDITIONS:
Any combination of aliasing among res, x, y is allowed.
x, y, res must have compatible dimensions.
x and y must have the same length.
NOTE:
This function does *not* normalise before subtracting. Be careful
with the overflow limb.
*/
void ZmodF_poly_sub(ZmodF_poly_t res, ZmodF_poly_t x, ZmodF_poly_t y);
/*
Normalises all coefficients (up to x.length) to be in the range [0, p).
*/
void ZmodF_poly_normalise(ZmodF_poly_t poly);
/*
Divides all coefficients by 2^depth mod p. This should be used after
running an inverse fourier transform.
*/
void ZmodF_poly_rescale(ZmodF_poly_t poly);
/*
Divides _trunc_ coefficients by 2^depth mod p. This can be used after
running an inverse fourier transform of one only wants the first trunc
coefficients.
*/
void ZmodF_poly_rescale_range(ZmodF_poly_t poly, unsigned long start, unsigned long n);
/****************************************************************************
Fourier Transform Routines
For the following routines, 2^depth must divide 4*n*FLINT_BITS. This
ensures that Z/pZ has enough roots of unity.
****************************************************************************/
/*
This is the threshold for switching from a plain iterative FFT to an FFT
factoring algorithm. It should be set to about the number of limbs in L1 cache.
*/
//#define ZMODFPOLY_FFT_FACTOR_THRESHOLD 7500
#define ZMODFPOLY_FFT_FACTOR_THRESHOLD 7000
/*
Converts from coefficient representation to fourier representation.
"length" is the desired number of fourier coefficients; x.length is set
to length when finished.
Output is inplace. (Note that in general *all* 2^depth coefficients will
get overwritten in intermediate steps.)
PRECONDITIONS:
0 <= length <= 2^poly.depth
poly.scratch_count >= 1
*/
void ZmodF_poly_FFT(ZmodF_poly_t poly, unsigned long length);
/*
Converts from fourier representation to coefficient representation.
It *assumes* that the supplied fourier coefficients are actually the fourier
transform of a polynomial whose coefficients beyond x.length are all zero.
Result is inplace, x.length is not modified. (Note: after it's finished, the
coefficients beyond x.length will contain garbage.)
The output will be a factor of 2^depth too big. See ZmodF_poly_rescale().
PRECONDITIONS:
poly.scratch_count >= 1
*/
void ZmodF_poly_IFFT(ZmodF_poly_t poly);
/*
Computes convolution of x and y, places result in res.
The resulting length will be x.length + y.length - 1. If this is more
than 2^depth, then the resulting length is 2^depth, and the convolution is
actually cyclic of length 2^depth.
PRECONDITIONS:
Any combination of aliasing among res, x, y is allowed.
x, y, res must have compatible dimensions.
NOTE:
x and y will both be converted to fourier representation.
If you don't like it, make a copy first.
PRECONDITIONS:
x.scratch_count >= 1
y.scratch_count >= 1
res.scratch_count >= 1
*/
void ZmodF_poly_convolution(ZmodF_poly_t res, ZmodF_poly_t x, ZmodF_poly_t y);
void ZmodF_poly_convolution_range(ZmodF_poly_t res, ZmodF_poly_t x,
ZmodF_poly_t y, unsigned long start, unsigned long n);
// internal functions
void _ZmodF_poly_FFT_iterative(
ZmodF_t* x, unsigned long depth,
unsigned long skip, unsigned long nonzero, unsigned long length,
unsigned long twist, unsigned long n, ZmodF_t* scratch);
void _ZmodF_poly_FFT_factor(
ZmodF_t* x, unsigned long rows_depth, unsigned long cols_depth,
unsigned long skip, unsigned long nonzero, unsigned long length,
unsigned long twist, unsigned long n, ZmodF_t* scratch);
void _ZmodF_poly_FFT(ZmodF_t* x, unsigned long depth, unsigned long skip,
unsigned long nonzero, unsigned long length,
unsigned long twist, unsigned long n,
ZmodF_t* scratch);
void _ZmodF_poly_IFFT_recursive(
ZmodF_t* x, unsigned long depth, unsigned long skip,
unsigned long nonzero, unsigned long length, int extra,
unsigned long twist, unsigned long n, ZmodF_t* scratch);
void _ZmodF_poly_IFFT_iterative(
ZmodF_t* x, unsigned long depth, unsigned long skip,
unsigned long twist, unsigned long n, ZmodF_t* scratch);
void _ZmodF_poly_IFFT(ZmodF_t* x, unsigned long depth, unsigned long skip,
unsigned long nonzero, unsigned long length, int extra,
unsigned long twist, unsigned long n,
ZmodF_t* scratch);
/****************************************************************************
Negacyclic Fourier Transform Routines
For the following routines, 2^(depth+1) must divide 4*n*FLINT_BITS.
This ensures that Z/pZ has enough roots of unity.
These routines are exactly the same as those listed in the previous section,
except that they evaluate at w^(2k+1), where w is a 2^(depth+1)-th root of
unity.
****************************************************************************/
void ZmodF_poly_negacyclic_FFT(ZmodF_poly_t poly);
void ZmodF_poly_negacyclic_IFFT(ZmodF_poly_t poly);
void ZmodF_poly_negacyclic_convolution(ZmodF_poly_t res,
ZmodF_poly_t x, ZmodF_poly_t y);
#ifdef __cplusplus
}
#endif
#endif
// end of file ****************************************************************