40 #include <sys/types.h>
42 #include <fmpz_poly.h>
68 fmpz_poly_t poly_tmp, two;
70 fmpz_poly_init(poly_tmp);
71 fmpz_poly_zero(poly_tmp);
73 fmpz_poly_set_coeff_ui(two, 0, 2);
75 while (v < (
int)(params->
q)) {
79 fmpz_poly_sub(poly_tmp, two, poly_tmp);
85 fmpz_poly_clear(poly_tmp);
102 fmpz_set_si(f_cmp, 0);
104 return fmpz_cmp_si(f_cmp, g);
114 fmpz_poly_init(new_poly);
116 for (uint32_t i = 0; i < len; i++)
117 fmpz_poly_set_coeff_si(new_poly, i, c[i]);
125 fmpz_poly_clear(poly);
135 while(poly_array[i]) {
151 fmpz_poly_struct *next_poly;
155 va_start(args, poly);
156 while (next_poly != NULL) {
158 next_poly = va_arg(args, fmpz_poly_struct*);
169 nmod_poly_t nmod_tmp;
171 nmod_poly_init(nmod_tmp, mod);
173 fmpz_poly_get_nmod_poly(nmod_tmp, a);
174 fmpz_poly_set_nmod_poly_unsigned(a, nmod_tmp);
176 nmod_poly_clear(nmod_tmp);
185 nmod_poly_t nmod_tmp;
187 nmod_poly_init(nmod_tmp, mod);
189 fmpz_poly_get_nmod_poly(nmod_tmp, a);
190 fmpz_poly_set_nmod_poly(a, nmod_tmp);
192 nmod_poly_clear(nmod_tmp);
202 fmpz_poly_set_coeff_fmpz(poly, n, x);
204 fmpz_poly_set_coeff_si(poly, n, 0);
214 fmpz_init_set_ui(modulus, mod);
216 return fmpz_invmod(f, g, modulus);
228 fmpz_add_ui(f, h, 0);
230 fmpz_add_ui(f, g, 0);
248 fmpz_poly_init(a_tmp);
249 fmpz_init(c_coeff_k);
252 fmpz_poly_set(a_tmp, a);
255 for (
int k = params->
N - 1; k >= 0; k--) {
260 fmpz_set_si(c_coeff_k, 0);
262 for (
int i = params->
N - 1; i >= 0; i--) {
266 if (j == (
int)(params->
N))
269 a_tmp_coeff_i = fmpz_poly_get_coeff_ptr(a_tmp, i);
270 b_coeff_j = fmpz_poly_get_coeff_ptr(b, j);
278 fmpz_mul(fmpz_tmp, a_tmp_coeff_i, b_coeff_j);
279 fmpz_add(fmpz_tmp, fmpz_tmp, c_coeff_k);
280 fmpz_mod_ui(c_coeff_k, fmpz_tmp, modulus);
282 fmpz_poly_set_coeff_fmpz(c, k, c_coeff_k);
284 fmpz_clear(fmpz_tmp);
288 fmpz_clear(c_coeff_k);
291 fmpz_poly_clear(a_tmp);
313 fmpz_poly_set_coeff_ui(b, 0, 1);
320 fmpz_poly_set_coeff_si(g, 0, -1);
321 fmpz_poly_set_coeff_si(g, params->
N, 1);
324 fmpz_poly_init(a_tmp);
325 fmpz_poly_set(a_tmp, a);
329 while (fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) {
330 for (uint32_t i = 1; i <= params->
N; i++) {
331 fmpz *f_coeff = fmpz_poly_get_coeff_ptr(f, i);
332 fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, params->
N - i);
343 fmpz_poly_set_coeff_si(f, params->
N, 0);
344 fmpz_poly_set_coeff_si(c, 0, 0);
348 if (fmpz_poly_degree(f) == -1)
352 if (fmpz_poly_degree(f) == 0)
355 if (fmpz_poly_degree(f) < fmpz_poly_degree(g)) {
356 fmpz_poly_swap(f, g);
357 fmpz_poly_swap(b, c);
360 fmpz_poly_add(f, g, f);
363 fmpz_poly_add(b, c, b);
369 b_last = fmpz_poly_get_coeff_ptr(b, params->
N);
374 for (
int i = params->
N - 1; i >= 0; i--) {
382 b_i = fmpz_poly_get_coeff_ptr(b, i);
389 fmpz_poly_set(a_tmp, a);
391 if (fmpz_poly_is_one(a_tmp))
397 fmpz_poly_clear(a_tmp);
425 fmpz_poly_set_coeff_ui(b, 0, 1);
432 fmpz_poly_set_coeff_si(g, 0, -1);
433 fmpz_poly_set_coeff_si(g, params->
N, 1);
436 fmpz_poly_init(a_tmp);
437 fmpz_poly_set(a_tmp, a);
441 while (fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) {
442 for (uint32_t i = 1; i <= params->
N; i++) {
443 fmpz *f_coeff = fmpz_poly_get_coeff_ptr(f, i);
444 fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, params->
N - i);
455 fmpz_poly_set_coeff_si(f, params->
N, 0);
456 fmpz_poly_set_coeff_si(c, 0, 0);
460 if (fmpz_poly_degree(f) == -1)
464 if (fmpz_poly_degree(f) == 0)
467 if (fmpz_poly_degree(f) < fmpz_poly_degree(g)) {
469 fmpz_poly_swap(f, g);
470 fmpz_poly_swap(b, c);
482 fmpz_init_set(mp_tmp, fmpz_poly_get_coeff_ptr(f, 0));
484 fmpz_poly_init(g_tmp);
485 fmpz_poly_set(g_tmp, g);
487 fmpz_poly_init(c_tmp);
488 fmpz_poly_set(c_tmp, c);
493 fmpz_poly_get_coeff_ptr(g, 0),
495 fmpz_mod_ui(mp_tmp, mp_tmp, params->
p);
496 fmpz_mul(u, mp_tmp, u);
497 fmpz_mod_ui(u, u, params->
p);
500 fmpz_poly_scalar_mul_fmpz(g_tmp, g_tmp, u);
501 fmpz_poly_sub(f, g_tmp, f);
505 fmpz_poly_scalar_mul_fmpz(c_tmp, c_tmp, u);
506 fmpz_poly_sub(b, c_tmp, b);
510 fmpz_poly_clear(g_tmp);
511 fmpz_poly_clear(c_tmp);
517 b_last = fmpz_poly_get_coeff_ptr(b, params->
N);
522 for (
int i = params->
N - 1; i >= 0; i--) {
532 fmpz_poly_get_coeff_ptr(f, 0),
535 if (fmpz_poly_get_coeff_ptr(b, i)) {
536 fmpz_mul(fmpz_poly_get_coeff_ptr(b, i),
537 fmpz_poly_get_coeff_ptr(b, i),
539 fmpz_mod_ui(fmpz_poly_get_coeff_ptr(b, i),
540 fmpz_poly_get_coeff_ptr(b, i),
549 b_i = fmpz_poly_get_coeff_ptr(b, i);
554 fmpz_poly_set(a_tmp, a);
556 if (fmpz_poly_is_one(a_tmp))
562 fmpz_poly_clear(a_tmp);
576 fmpz_poly_print(poly);
585 fmpz_poly_print_pretty(poly,
"x");
bool poly_inverse_poly_q(const fmpz_poly_t a, fmpz_poly_t Fq, const ntru_params *params)
void fmpz_poly_mod_unsigned(fmpz_poly_t a, const uint32_t mod)
void fmpz_poly_mod(fmpz_poly_t a, const uint32_t mod)
bool poly_inverse_poly_p(const fmpz_poly_t a, fmpz_poly_t Fp, const ntru_params *params)
int fmpz_invmod_ui(fmpz_t f, const fmpz_t g, const uint32_t mod)
void poly_draw(const fmpz_poly_t poly)
void fmpz_poly_set_coeff_fmpz_n(fmpz_poly_t poly, slong n, const fmpz_t x)
void poly_starmultiply(const fmpz_poly_t a, const fmpz_poly_t b, fmpz_poly_t c, const ntru_params *params, uint32_t modulus)
void poly_delete(fmpz_poly_t poly)
void poly_delete_all(fmpz_poly_t poly,...)
void poly_new(fmpz_poly_t new_poly, int const *const c, const size_t len)
void poly_delete_array(fmpz_poly_t **poly_array)
void fmpz_add_n(fmpz_t f, const fmpz_t g, const fmpz_t h)
int fmpz_cmp_si_n(const fmpz_t f, slong g)
void poly_draw_pretty(const fmpz_poly_t poly)
static void poly_mod2_to_modq(const fmpz_poly_t a, fmpz_poly_t Fq, const ntru_params *params)