1 /*
2     Copyright (C) 2009 William Hart
3     Copyright (C) 2011 Fredrik Johansson
4     Copyright (C) 2012 Lina Kulakova
5     Copyright (C) 2013 Mike Hansen
6 
7     This file is part of FLINT.
8 
9     FLINT is free software: you can redistribute it and/or modify it under
10     the terms of the GNU Lesser General Public License (LGPL) as published
11     by the Free Software Foundation; either version 2.1 of the License, or
12     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
13 */
14 
15 #ifdef T
16 
17 #include "templates.h"
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <gmp.h>
22 #include "flint.h"
23 #include "ulong_extras.h"
24 int
main(void)25 main(void)
26 {
27     int i, result;
28     FLINT_TEST_INIT(state);
29 
30     flint_printf("powmod_fmpz_binexp....");
31     fflush(stdout);
32 
33     /* Aliasing of res and a */
34     for (i = 0; i < 5 * flint_test_multiplier(); i++)
35     {
36         TEMPLATE(T, ctx_t) ctx;
37         TEMPLATE(T, poly_t) a, res, t, f;
38         ulong exp;
39         fmpz_t expz;
40 
41         TEMPLATE(T, ctx_randtest) (ctx, state);
42 
43         exp = n_randint(state, 50);
44         fmpz_init_set_ui(expz, exp);
45 
46         TEMPLATE(T, poly_init) (a, ctx);
47         TEMPLATE(T, poly_init) (f, ctx);
48         TEMPLATE(T, poly_init) (res, ctx);
49         TEMPLATE(T, poly_init) (t, ctx);
50 
51         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 20), ctx);
52         TEMPLATE(T, poly_randtest_not_zero) (f, state,
53                                              n_randint(state, 20) + 1, ctx);
54 
55         TEMPLATE(T, poly_powmod_fmpz_binexp) (res, a, expz, f, ctx);
56         TEMPLATE(T, poly_powmod_fmpz_binexp) (a, a, expz, f, ctx);
57 
58         result = (TEMPLATE(T, poly_equal) (res, a, ctx));
59         if (!result)
60         {
61             flint_printf("FAIL:\n");
62             flint_printf("a:\n");
63             TEMPLATE(T, poly_print) (a, ctx), flint_printf("\n\n");
64             flint_printf("f:\n");
65             TEMPLATE(T, poly_print) (f, ctx), flint_printf("\n\n");
66             flint_printf("res:\n");
67             TEMPLATE(T, poly_print) (res, ctx), flint_printf("\n\n");
68             abort();
69         }
70 
71         TEMPLATE(T, poly_clear) (a, ctx);
72         TEMPLATE(T, poly_clear) (f, ctx);
73         TEMPLATE(T, poly_clear) (res, ctx);
74         TEMPLATE(T, poly_clear) (t, ctx);
75         fmpz_clear(expz);
76 
77         TEMPLATE(T, ctx_clear) (ctx);
78     }
79 
80     /* Aliasing of res and f */
81     for (i = 0; i < 5 * flint_test_multiplier(); i++)
82     {
83         TEMPLATE(T, ctx_t) ctx;
84         TEMPLATE(T, poly_t) a, res, t, f;
85         ulong exp;
86         fmpz_t expz;
87 
88         TEMPLATE(T, ctx_randtest) (ctx, state);
89 
90         exp = n_randint(state, 50);
91         fmpz_init_set_ui(expz, exp);
92 
93         TEMPLATE(T, poly_init) (a, ctx);
94         TEMPLATE(T, poly_init) (f, ctx);
95         TEMPLATE(T, poly_init) (res, ctx);
96         TEMPLATE(T, poly_init) (t, ctx);
97 
98         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 20), ctx);
99         TEMPLATE(T, poly_randtest_not_zero) (f, state,
100                                              n_randint(state, 20) + 1, ctx);
101 
102         TEMPLATE(T, poly_powmod_fmpz_binexp) (res, a, expz, f, ctx);
103         TEMPLATE(T, poly_powmod_fmpz_binexp) (f, a, expz, f, ctx);
104 
105         result = (TEMPLATE(T, poly_equal) (res, f, ctx));
106         if (!result)
107         {
108             flint_printf("FAIL:\n");
109             flint_printf("a:\n");
110             TEMPLATE(T, poly_print) (a, ctx), flint_printf("\n\n");
111             flint_printf("f:\n");
112             TEMPLATE(T, poly_print) (f, ctx), flint_printf("\n\n");
113             flint_printf("res:\n");
114             TEMPLATE(T, poly_print) (res, ctx), flint_printf("\n\n");
115             abort();
116         }
117 
118         TEMPLATE(T, poly_clear) (a, ctx);
119         TEMPLATE(T, poly_clear) (f, ctx);
120         TEMPLATE(T, poly_clear) (res, ctx);
121         TEMPLATE(T, poly_clear) (t, ctx);
122         fmpz_clear(expz);
123 
124         TEMPLATE(T, ctx_clear) (ctx);
125     }
126 
127     /* No aliasing */
128     for (i = 0; i < 5 * flint_test_multiplier(); i++)
129     {
130         TEMPLATE(T, ctx_t) ctx;
131         TEMPLATE(T, poly_t) a, res1, res2, t, f;
132         ulong exp;
133         fmpz_t expz;
134 
135         TEMPLATE(T, ctx_randtest) (ctx, state);
136 
137         exp = n_randint(state, 50);
138 
139         TEMPLATE(T, poly_init) (a, ctx);
140         TEMPLATE(T, poly_init) (f, ctx);
141         TEMPLATE(T, poly_init) (res1, ctx);
142         TEMPLATE(T, poly_init) (res2, ctx);
143         TEMPLATE(T, poly_init) (t, ctx);
144 
145         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 20), ctx);
146         TEMPLATE(T, poly_randtest_not_zero) (f, state,
147                                              n_randint(state, 20) + 1, ctx);
148         fmpz_init_set_ui(expz, exp);
149 
150         TEMPLATE(T, poly_powmod_fmpz_binexp) (res1, a, expz, f, ctx);
151         TEMPLATE(T, poly_powmod_ui_binexp) (res2, a, exp, f, ctx);
152 
153         result = (TEMPLATE(T, poly_equal) (res1, res2, ctx));
154         if (!result)
155         {
156             flint_printf("FAIL:\n");
157             flint_printf("a:\n");
158             TEMPLATE(T, poly_print) (a, ctx), flint_printf("\n\n");
159             flint_printf("f:\n");
160             TEMPLATE(T, poly_print) (f, ctx), flint_printf("\n\n");
161             flint_printf("res1:\n");
162             TEMPLATE(T, poly_print) (res1, ctx), flint_printf("\n\n");
163             flint_printf("res2:\n");
164             TEMPLATE(T, poly_print) (res2, ctx), flint_printf("\n\n");
165             abort();
166         }
167 
168         TEMPLATE(T, poly_clear) (a, ctx);
169         TEMPLATE(T, poly_clear) (f, ctx);
170         TEMPLATE(T, poly_clear) (res1, ctx);
171         TEMPLATE(T, poly_clear) (res2, ctx);
172         TEMPLATE(T, poly_clear) (t, ctx);
173         fmpz_clear(expz);
174 
175         TEMPLATE(T, ctx_clear) (ctx);
176     }
177 
178     /* Check that a^(b+c) = a^b * a^c */
179     for (i = 0; i < 5 * flint_test_multiplier(); i++)
180     {
181         TEMPLATE(T, ctx_t) ctx;
182         TEMPLATE(T, poly_t) a, res1, res2, res3, res4, t, f;
183         fmpz_t exp1, exp2, exp3;
184 
185         TEMPLATE(T, ctx_randtest) (ctx, state);
186 
187         fmpz_init(exp1);
188         fmpz_init(exp2);
189         fmpz_randtest(exp1, state, 200);
190         if (fmpz_sgn(exp1) == -1)
191             fmpz_neg(exp1, exp1);
192         fmpz_randtest(exp2, state, 200);
193         if (fmpz_sgn(exp2) == -1)
194             fmpz_neg(exp2, exp2);
195 
196         TEMPLATE(T, poly_init) (a, ctx);
197         TEMPLATE(T, poly_init) (f, ctx);
198         TEMPLATE(T, poly_init) (res1, ctx);
199         TEMPLATE(T, poly_init) (res2, ctx);
200         TEMPLATE(T, poly_init) (res3, ctx);
201         TEMPLATE(T, poly_init) (res4, ctx);
202         TEMPLATE(T, poly_init) (t, ctx);
203 
204         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 20), ctx);
205         TEMPLATE(T, poly_randtest_not_zero) (f, state,
206                                              n_randint(state, 20) + 1, ctx);
207 
208         TEMPLATE(T, poly_powmod_fmpz_binexp) (res1, a, exp1, f, ctx);
209         TEMPLATE(T, poly_powmod_fmpz_binexp) (res2, a, exp2, f, ctx);
210         TEMPLATE(T, poly_mulmod) (res4, res1, res2, f, ctx);
211         fmpz_init(exp3);
212         fmpz_add(exp3, exp1, exp2);
213         TEMPLATE(T, poly_powmod_fmpz_binexp) (res3, a, exp3, f, ctx);
214 
215         result = (TEMPLATE(T, poly_equal) (res4, res3, ctx));
216         if (!result)
217         {
218             flint_printf("FAIL:\n");
219             flint_printf("a:\n");
220             TEMPLATE(T, poly_print) (a, ctx), flint_printf("\n\n");
221             flint_printf("f:\n");
222             TEMPLATE(T, poly_print) (f, ctx), flint_printf("\n\n");
223             flint_printf("res3:\n");
224             TEMPLATE(T, poly_print) (res3, ctx), flint_printf("\n\n");
225             flint_printf("res4:\n");
226             TEMPLATE(T, poly_print) (res4, ctx), flint_printf("\n\n");
227             abort();
228         }
229 
230         TEMPLATE(T, poly_clear) (a, ctx);
231         TEMPLATE(T, poly_clear) (f, ctx);
232         TEMPLATE(T, poly_clear) (res1, ctx);
233         TEMPLATE(T, poly_clear) (res2, ctx);
234         TEMPLATE(T, poly_clear) (res3, ctx);
235         TEMPLATE(T, poly_clear) (res4, ctx);
236         TEMPLATE(T, poly_clear) (t, ctx);
237         fmpz_clear(exp1);
238         fmpz_clear(exp2);
239         fmpz_clear(exp3);
240 
241         TEMPLATE(T, ctx_clear) (ctx);
242     }
243 
244     FLINT_TEST_CLEANUP(state);
245     flint_printf("PASS\n");
246     return 0;
247 }
248 
249 
250 #endif
251