-
Notifications
You must be signed in to change notification settings - Fork 0
/
F_mpz_mod_poly.h
356 lines (240 loc) · 10.6 KB
/
F_mpz_mod_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
/*============================================================================
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
===============================================================================*/
/*****************************************************************************
F_mpz_mod_poly.h: Polynomials over F_mpz mod p.
Copyright (C) 2009, 2010 William Hart
Copyright (C) 2009, 2010 Andy Novocin
*****************************************************************************/
#include <gmp.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "flint.h"
#include "F_mpz_poly.h"
#include "memory-manager.h"
#include "mpn_extras.h"
#include "long_extras.h"
#ifndef _F_MPZ_MOD_POLY_H_
#define _F_MPZ_MOD_POLY_H_
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
F_mpz * coeffs;
ulong alloc;
ulong length;
F_mpz_t P;
} F_mpz_mod_poly_struct;
typedef F_mpz_mod_poly_struct F_mpz_mod_poly_t[1];
/****************************************************************************
Initialisation and memory management
****************************************************************************/
void F_mpz_mod_poly_init(F_mpz_mod_poly_t poly, const F_mpz_t P);
void F_mpz_mod_poly_init2(F_mpz_mod_poly_t poly, const F_mpz_t P, const ulong alloc);
void F_mpz_mod_poly_clear(F_mpz_mod_poly_t poly);
void F_mpz_mod_poly_realloc(F_mpz_mod_poly_t poly, const ulong alloc);
void F_mpz_mod_poly_fit_length(F_mpz_mod_poly_t poly, const ulong length);
/****************************************************************************
Normalisation/truncation
****************************************************************************/
void _F_mpz_mod_poly_normalise(F_mpz_mod_poly_t poly);
static inline
void _F_mpz_mod_poly_set_length(F_mpz_mod_poly_t poly, const ulong length)
{
if (poly->length > length) // demote coefficients beyond new length
{
for (ulong i = length; i < poly->length; i++)
_F_mpz_demote(poly->coeffs + i);
}
poly->length = length;
}
static inline
void F_mpz_mod_poly_truncate(F_mpz_mod_poly_t poly, const ulong length)
{
if (poly->length > length) // only truncate if necessary
{
for (ulong i = length; i < poly->length; i++)
_F_mpz_demote(poly->coeffs + i);
poly->length = length;
_F_mpz_mod_poly_normalise(poly);
}
}
/****************************************************************************
Zero
****************************************************************************/
static inline
void F_mpz_mod_poly_zero(F_mpz_mod_poly_t poly)
{
_F_mpz_mod_poly_set_length(poly, 0);
}
/****************************************************************************
Conversion
****************************************************************************/
void mpz_poly_to_F_mpz_mod_poly(F_mpz_mod_poly_t F_poly, const mpz_poly_t m_poly);
void F_mpz_mod_poly_to_mpz_poly(mpz_poly_t m_poly, const F_mpz_mod_poly_t F_poly);
void F_mpz_poly_to_F_mpz_mod_poly(F_mpz_mod_poly_t F_poly, const F_mpz_poly_t poly);
void F_mpz_mod_poly_to_F_mpz_poly(F_mpz_poly_t poly, const F_mpz_mod_poly_t F_poly);
void zmod_poly_to_F_mpz_mod_poly(F_mpz_mod_poly_t fpol, const zmod_poly_t zpol);
void F_mpz_mod_poly_to_zmod_poly(zmod_poly_t zpol, const F_mpz_mod_poly_t fpol);
/****************************************************************************
Attach
****************************************************************************/
/*
Attach an F_mpz_poly to an F_mpz_mod_poly. Allows one to temporarily treat
an F_mpz_mod_poly as though it were an F_mpz_poly.
*/
static inline
void _F_mpz_poly_attach_F_mpz_mod_poly(F_mpz_poly_t out, const F_mpz_mod_poly_t in)
{
out->coeffs = in->coeffs;
out->length = in->length;
out->alloc = in->alloc;
}
/*
Attach an F_mpz_mod_poly to an F_mpz_poly. Allows one to temporarily treat
an F_mpz_poly as an F_mpz_mod_poly.
*/
static inline
void _F_mpz_mod_poly_attach_F_mpz_poly(F_mpz_mod_poly_t out, const F_mpz_poly_t in)
{
out->coeffs = in->coeffs;
out->length = in->length;
out->alloc = in->alloc;
}
/*
Attach poly1 to poly2 as though poly2 had been shifted left by n first.
Assumes the polynomial poly1 and its modulus P are not modified while attached.
*/
static inline
void _F_mpz_mod_poly_attach_shift(F_mpz_mod_poly_t poly1, const F_mpz_mod_poly_t poly2, const ulong n)
{
poly1->coeffs = poly2->coeffs + n; // set coeffs to start at coeff n
if (poly2->length >= n) poly1->length = poly2->length - n; // check n is not too large
else poly1->length = 0;
*(poly1->P) = *(poly2->P);
}
/*
Attach poly1 to poly2 as though poly2 had been truncated and normalised first.
Assumes the polynomial poly1 and its modulus P are not modified while attached.
*/
static inline
void _F_mpz_mod_poly_attach_truncate(F_mpz_mod_poly_t poly1, const F_mpz_mod_poly_t poly2, const ulong n)
{
ulong length;
poly1->coeffs = poly2->coeffs; // set coeffs
if (poly2->length < n) length = poly2->length; // check that n is not too large
else length = n;
while (length && (poly1->coeffs[length - 1] == 0)) length--; // normalise
poly1->length = length;
*(poly1->P) = *(poly2->P);
}
/****************************************************************************
Reduce mod P
****************************************************************************/
static inline
void _F_mpz_mod_poly_reduce_coeffs(F_mpz_mod_poly_t poly)
{
for (ulong i = 0; i < poly->length; i++)
F_mpz_mod(poly->coeffs + i, poly->coeffs + i, poly->P);
_F_mpz_mod_poly_normalise(poly);
}
static inline
void _F_mpz_poly_reduce_coeffs(F_mpz_poly_t poly, const F_mpz_t P)
{
for (ulong i = 0; i < poly->length; i++)
F_mpz_mod(poly->coeffs + i, poly->coeffs + i, P);
}
/****************************************************************************
Assignment/swap
****************************************************************************/
void F_mpz_mod_poly_set(F_mpz_mod_poly_t poly1, const F_mpz_mod_poly_t poly2);
void F_mpz_mod_poly_swap(F_mpz_mod_poly_t poly1, F_mpz_mod_poly_t poly2);
/****************************************************************************
Comparison
****************************************************************************/
int F_mpz_mod_poly_equal(const F_mpz_mod_poly_t poly1, const F_mpz_mod_poly_t poly2);
/****************************************************************************
Input/output
****************************************************************************/
static inline
void F_mpz_mod_poly_print(F_mpz_mod_poly_t poly)
{
mpz_poly_t m_poly;
mpz_poly_init(m_poly);
F_mpz_poly_t pol;
_F_mpz_poly_attach_F_mpz_mod_poly(pol, poly);
F_mpz_poly_to_mpz_poly(m_poly, pol);
mpz_poly_print(m_poly);
printf(" : P = "); F_mpz_print(poly->P);
mpz_poly_clear(m_poly);
}
/*===============================================================================
Shifting
================================================================================*/
void F_mpz_mod_poly_left_shift(F_mpz_mod_poly_t res, const F_mpz_mod_poly_t poly, const ulong n);
void F_mpz_mod_poly_right_shift(F_mpz_mod_poly_t res, const F_mpz_mod_poly_t poly, const ulong n);
/****************************************************************************
Add/sub
****************************************************************************/
void _F_mpz_mod_poly_add(F_mpz_mod_poly_t res, const F_mpz_mod_poly_t pol1, const F_mpz_mod_poly_t pol2);
void _F_mpz_mod_poly_sub(F_mpz_mod_poly_t res, const F_mpz_mod_poly_t pol1, const F_mpz_mod_poly_t pol2);
void F_mpz_mod_poly_add(F_mpz_mod_poly_t res, const F_mpz_mod_poly_t poly1, const F_mpz_mod_poly_t poly2);
void F_mpz_mod_poly_sub(F_mpz_mod_poly_t res, const F_mpz_mod_poly_t poly1, const F_mpz_mod_poly_t poly2);
/****************************************************************************
Scalar multiplication
****************************************************************************/
void F_mpz_mod_poly_scalar_mul(F_mpz_mod_poly_t res, const F_mpz_mod_poly_t pol1, const F_mpz_t x);
/****************************************************************************
Multiplication
****************************************************************************/
void _F_mpz_mod_poly_mul(F_mpz_mod_poly_t res, const F_mpz_mod_poly_t pol1, const F_mpz_mod_poly_t pol2);
void F_mpz_mod_poly_mul(F_mpz_mod_poly_t res, const F_mpz_mod_poly_t pol1, const F_mpz_mod_poly_t pol2);
void _F_mpz_mod_poly_mul_trunc_left(F_mpz_mod_poly_t res, const F_mpz_mod_poly_t pol1, const F_mpz_mod_poly_t pol2, const ulong trunc);
void F_mpz_mod_poly_mul_trunc_left(F_mpz_mod_poly_t res, const F_mpz_mod_poly_t poly1, const F_mpz_mod_poly_t poly2, const ulong trunc);
/****************************************************************************
Division
****************************************************************************/
void F_mpz_mod_poly_divrem_basecase(F_mpz_mod_poly_t Q, F_mpz_mod_poly_t R, const F_mpz_mod_poly_t A, const F_mpz_mod_poly_t B);
void F_mpz_mod_poly_div_divconquer_recursive(F_mpz_mod_poly_t Q, F_mpz_mod_poly_t BQ, const F_mpz_mod_poly_t A, const F_mpz_mod_poly_t B);
void F_mpz_mod_poly_divrem_divconquer(F_mpz_mod_poly_t Q, F_mpz_mod_poly_t R, const F_mpz_mod_poly_t A, const F_mpz_mod_poly_t B);
static inline
void F_mpz_mod_poly_divrem(F_mpz_mod_poly_t Q, F_mpz_mod_poly_t R, const F_mpz_mod_poly_t A, const F_mpz_mod_poly_t B)
{
F_mpz_mod_poly_divrem_divconquer(Q, R, A, B);
}
static inline
void F_mpz_mod_poly_rem(F_mpz_mod_poly_t R, F_mpz_mod_poly_t A, F_mpz_mod_poly_t B)
{
F_mpz_mod_poly_t Q;
F_mpz_mod_poly_init(Q, A->P);
F_mpz_mod_poly_divrem(Q, R, A, B);
F_mpz_mod_poly_clear(Q);
}
static inline
void F_mpz_mod_poly_mulmod( F_mpz_mod_poly_t res, F_mpz_mod_poly_t A, F_mpz_mod_poly_t B, F_mpz_mod_poly_t C)
{
F_mpz_mod_poly_t Q;
F_mpz_mod_poly_init(Q, A->P);
F_mpz_mod_poly_mul(Q, A, B);
F_mpz_mod_poly_rem(res, Q, C);
F_mpz_mod_poly_clear(Q);
}
#ifdef __cplusplus
}
#endif
#endif /* _F_MPZ_MOD_POLY_H_ */