-
Notifications
You must be signed in to change notification settings - Fork 0
/
NTL-interface-test.cpp
343 lines (267 loc) · 8.02 KB
/
NTL-interface-test.cpp
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
/*============================================================================
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
===============================================================================*/
/****************************************************************************
NTL-interface-test.cpp: Test functions for conversion between NTL and FLINT format
Copyright (C) 2007, William Hart
*****************************************************************************/
#include <cstdio>
#include <cstring>
#undef ulong
#include <NTL/ZZ.h>
#include <NTL/ZZX.h>
#include <NTL/mat_ZZ.h>
#define ulong unsigned long
#include <gmp.h>
#include "NTL-interface.h"
#include "fmpz.h"
#include "F_mpz.h"
#include "F_mpz_mat.h"
#include "fmpz_poly.h"
#include "flint.h"
#include "mpz_poly.h"
#include "long_extras.h"
#include "memory-manager.h"
#include "test-support.h"
#define VARY_BITS 1
#define SIGNS 1
#define SPARSE 1
#define DEBUG 0 // prints debug information
#define DEBUG2 1
unsigned long randint(unsigned long randsup)
{
if (randsup == 0) return 0;
static unsigned long randval = 4035456057U;
randval = ((unsigned long)randval*1025416097U+286824428U)%(unsigned long)4294967291U;
return (unsigned long)randval%randsup;
}
void randpoly(mpz_poly_t pol, long length, unsigned long maxbits)
{
unsigned long bits;
mpz_t temp;
mpz_init(temp);
mpz_poly_zero(pol);
for (long i = 0; i < length; i++)
{
#if VARY_BITS
bits = randint(maxbits+1);
#else
bits = maxbits;
#endif
if (bits == 0) mpz_set_ui(temp,0);
else
{
#if SPARSE
if (randint(10) == 1) mpz_rrandomb(temp, randstate, bits);
else mpz_set_ui(temp, 0);
#else
mpz_rrandomb(temp, randstate, bits);
#endif
#if SIGNS
if (randint(2)) mpz_neg(temp,temp);
#endif
}
mpz_poly_set_coeff(pol, i, temp);
}
mpz_clear(temp);
}
void randpoly_unsigned(mpz_poly_t pol, long length, unsigned long maxbits)
{
unsigned long bits;
mpz_t temp;
mpz_init(temp);
mpz_poly_zero(pol);
for (long i = 0; i < length; i++)
{
#if VARY_BITS
bits = randint(maxbits+1);
#else
bits = maxbits;
#endif
if (bits == 0) mpz_set_ui(temp,0);
else
{
mpz_rrandomb(temp, randstate, bits);
}
mpz_poly_set_coeff(pol, i, temp);
}
mpz_clear(temp);
}
// generate a random mpz_mat_t with the given number of rows and columns and number of bits per entry
void mpz_randmat(mpz_mat_t mat, ulong r, ulong c, ulong maxbits)
{
ulong bits;
mpz_t temp;
mpz_init(temp);
for (long i = 0; i < r; i++)
{
for (long j = 0; j < c; j++)
{
#if VARY_BITS
bits = z_randint(maxbits+1);
#else
bits = maxbits;
#endif
if (bits == 0) mpz_set_ui(temp, 0);
else
{
#if SPARSE
if (z_randint(10) == 1) mpz_rrandomb(temp, randstate, bits);
else mpz_set_ui(temp, 0);
#else
mpz_rrandomb(temp, randstate, bits);
#endif
#if SIGNS
if (z_randint(2)) mpz_neg(temp, temp);
#endif
}
mpz_set(mat->entries[i*c+j], temp);
}
}
mpz_clear(temp);
}
void F_mpz_randmat(F_mpz_mat_t mat, ulong r, ulong c, ulong bits)
{
mpz_mat_t m_mat;
mpz_mat_init(m_mat, r, c);
mpz_randmat(m_mat, r, c, bits);
mpz_mat_to_F_mpz_mat(mat, m_mat);
mpz_mat_clear(m_mat);
}
int test_ZZ_to_fmpz()
{
int result = 1;
unsigned long limbs, limbs2, randlimbs;
fmpz_t int1, int2;
ZZ z;
for (unsigned long i = 0; (i < 1000000) && (result == 1); i++)
{
limbs = randint(100) + 1;
randlimbs = randint(limbs);
int1 = fmpz_init(limbs);
fmpz_random_limbs2(int1, randlimbs);
if (z_randint(2)) fmpz_neg(int1, int1);
fmpz_to_ZZ(z, int1);
limbs2 = ZZ_limbs(z);
int2 = fmpz_init(limbs2);
ZZ_to_fmpz(int2, z);
result = fmpz_equal(int1, int2);
fmpz_clear(int1);
fmpz_clear(int2);
}
return result;
}
int test_ZZ_to_F_mpz()
{
int result = 1;
unsigned long bits, limbs, limbs2, randbits;
F_mpz_t int1, int2;
ZZ z;
for (unsigned long i = 0; (i < 1000000) && (result == 1); i++)
{
bits = randint(1000)+1;
randbits = randint(bits);
limbs = (bits - 1)/FLINT_BITS + 1;
F_mpz_init2(int1, limbs);
F_mpz_random(int1, randbits);
if (z_randint(2)) F_mpz_neg(int1, int1);
F_mpz_to_ZZ(z, int1);
limbs2 = ZZ_limbs(z);
F_mpz_init2(int2, limbs2);
ZZ_to_F_mpz(int2, z);
result = F_mpz_equal(int1, int2);
F_mpz_clear(int1);
F_mpz_clear(int2);
}
return result;
}
int test_ZZX_to_fmpz_poly()
{
mpz_poly_t test_poly;
fmpz_poly_t test_fmpz_poly, test_fmpz_poly2;
int result = 1;
unsigned long bits, length;
mpz_poly_init(test_poly);
for (unsigned long count1 = 1; (count1 < 3000) && (result == 1) ; count1++)
{
bits = random_ulong(1000) + 1;
fmpz_poly_init2(test_fmpz_poly, 1, (bits-1)/FLINT_BITS+1);
fmpz_poly_init(test_fmpz_poly2);
for (unsigned long count2 = 0; (count2 < 10) && (result == 1); count2++)
{
ZZX ZZX_poly;
length = random_ulong(1000);
#if DEBUG
printf("%ld, %ld\n",length, bits);
#endif
fmpz_poly_fit_length(test_fmpz_poly, length);
randpoly(test_poly, length, bits);
mpz_poly_normalise(test_poly);
mpz_poly_to_fmpz_poly(test_fmpz_poly, test_poly);
fmpz_poly_to_ZZX(ZZX_poly, test_fmpz_poly);
ZZX_to_fmpz_poly(test_fmpz_poly2, ZZX_poly);
#if DEBUG
fmpz_poly_print(test_fmpz_poly); printf("\n");
fmpz_poly_print(test_fmpz_poly2); printf("\n");
#endif
result = fmpz_poly_equal(test_fmpz_poly, test_fmpz_poly2);
}
fmpz_poly_clear(test_fmpz_poly);
fmpz_poly_clear(test_fmpz_poly2);
}
mpz_poly_clear(test_poly);
return result;
}
int test_mat_ZZ_to_F_mpz_mat()
{
int result = 1;
unsigned long bits, r, c;
F_mpz_mat_t mat1, mat2;
for (unsigned long i = 0; (i < 10000) && (result == 1); i++)
{
bits = z_randint(200)+1;
r = z_randint(50);
c = z_randint(50);
F_mpz_mat_init(mat1, r, c);
F_mpz_mat_init(mat2, r, c);
mat_ZZ zmat;
zmat.SetDims(r, c);
F_mpz_randmat(mat1, r, c, bits);
F_mpz_mat_to_mat_ZZ(zmat, mat1);
mat_ZZ_to_F_mpz_mat(mat2, zmat);
result = F_mpz_mat_equal(mat1, mat2);
zmat.kill();
F_mpz_mat_clear(mat1);
F_mpz_mat_clear(mat2);
}
return result;
}
void fmpz_poly_test_all()
{
int success, all_success = 1;
RUN_TEST(ZZ_to_fmpz);
RUN_TEST(ZZ_to_F_mpz);
RUN_TEST(ZZX_to_fmpz_poly);
RUN_TEST(mat_ZZ_to_F_mpz_mat);
printf(all_success ? "\nAll tests passed\n" :
"\nAt least one test FAILED!\n");
}
int main()
{
test_support_init();
fmpz_poly_test_all();
test_support_cleanup();
flint_stack_cleanup();
return 0;
}