void fmpz_poly_q_canonicalise(fmpz_poly_q_t rop) { fmpz_poly_t gcd; if (fmpz_poly_is_zero(rop->den)) { flint_printf("Exception (fmpz_poly_q_canonicalise). Denominator is zero.\n"); abort(); } if (fmpz_poly_is_one(rop->den)) return; fmpz_poly_init(gcd); fmpz_poly_gcd(gcd, rop->num, rop->den); if (!fmpz_poly_is_unit(gcd)) { fmpz_poly_div(rop->num, rop->num, gcd); fmpz_poly_div(rop->den, rop->den, gcd); } fmpz_poly_clear(gcd); if (fmpz_sgn(fmpz_poly_lead(rop->den)) < 0) { fmpz_poly_neg(rop->num, rop->num); fmpz_poly_neg(rop->den, rop->den); } }
void dgsl_rot_mp_clear(dgsl_rot_mp_t *self) { if (!self) return; fmpz_poly_clear(self->c_z); fmpq_poly_clear(self->c); if (!fmpz_poly_is_zero(self->B)) fmpz_poly_clear(self->B); if (!fmpq_poly_is_zero(self->B_inv)) fmpq_poly_clear(self->B_inv); if(self->call == dgsl_rot_mp_call_identity) { if(self->D) { dgs_disc_gauss_mp_clear(self->D[0]); free(self->D); } } if(self->call == dgsl_rot_mp_call_gpv_inlattice) { } if(self->call == dgsl_rot_mp_call_inlattice) { mpfr_clear(self->r_f); fmpq_poly_clear(self->sigma_sqrt); } mpfr_clear(self->sigma); free(self); }
void fmpz_poly_randtest_not_zero(fmpz_poly_t f, flint_rand_t state, long len, mp_bitcnt_t bits) { if ((bits == 0) || (len == 0)) { printf("Exception: 0 passed to fmpz_poly_randtest_not_zero\n"); abort(); } fmpz_poly_randtest(f, state, len, bits); if (fmpz_poly_is_zero(f)) fmpz_poly_set_ui(f, 1); }
long fmpz_poly_mat_find_pivot_any(const fmpz_poly_mat_t mat, long start_row, long end_row, long c) { long r; for (r = start_row; r < end_row; r++) { if (!fmpz_poly_is_zero(fmpz_poly_mat_entry(mat, r, c))) return r; } return -1; }
static long fmpz_poly_mat_ord_p(const fmpz_poly_mat_t A, const fmpz_t p) { long i, j, v, w = LONG_MAX; for (i = 0; i < A->r; i++) for (j = 0; j < A->c; j++) { const fmpz_poly_struct *poly = fmpz_poly_mat_entry(A, i, j); if (!fmpz_poly_is_zero(poly)) { v = _fmpz_vec_ord_p(poly->coeffs, poly->length, p); w = FLINT_MIN(v, w); if (w == 0) return 0; } } return w; }
static void fmpz_poly_evaluate_qadic(qadic_t rop, const fmpz_poly_t op1, const qadic_t op2, const qadic_ctx_t ctx) { const long N = qadic_prec(rop); const long d = qadic_ctx_degree(ctx); if (N <= 0 || op2->val != 0 || rop == op2) { printf("Exception (fmpz_poly_evaluate_qadic):\n"); printf("Currently assumes that N > 0 and op2->val == 0, \n"); printf("and does not support aliasing.\n"); abort(); } if (fmpz_poly_is_zero(op1)) { qadic_zero(rop); } else if (qadic_is_zero(op2)) { padic_poly_set_fmpz(rop, op1->coeffs + 0, &ctx->pctx); } else { fmpz_t pN; fmpz_init(pN); fmpz_pow_ui(pN, (&ctx->pctx)->p, N); padic_poly_fit_length(rop, d); _fmpz_mod_poly_compose_smod(rop->coeffs, op1->coeffs, op1->length, op2->coeffs, op2->length, ctx->a, ctx->j, ctx->len, pN); _padic_poly_set_length(rop, d); qadic_reduce(rop, ctx); fmpz_clear(pN); } }
int main(void) { int i, result; flint_rand_t state; printf("revert_series...."); fflush(stdout); flint_randinit(state); /* Check aliasing */ for (i = 0; i < 50; i++) { fmpz_poly_t f, g; long n; fmpz_poly_init(f); fmpz_poly_init(g); fmpz_poly_randtest(g, state, n_randint(state, 50), 1+n_randint(state,100)); fmpz_poly_set_coeff_ui(g, 0, 0); fmpz_poly_set_coeff_ui(g, 1, 1); if (n_randlimb(state) % 2) fmpz_poly_neg(g, g); /* get -x term */ n = n_randint(state, 50); fmpz_poly_revert_series(f, g, n); fmpz_poly_revert_series(g, g, n); result = (fmpz_poly_equal(f, g)); if (!result) { printf("FAIL (aliasing):\n"); fmpz_poly_print(f), printf("\n\n"); fmpz_poly_print(g), printf("\n\n"); abort(); } fmpz_poly_clear(f); fmpz_poly_clear(g); } /* Check f(f^(-1)) = id */ for (i = 0; i < 50; i++) { fmpz_poly_t f, g, h; long n; fmpz_poly_init(f); fmpz_poly_init(g); fmpz_poly_init(h); fmpz_poly_randtest(g, state, n_randint(state, 50), 10); fmpz_poly_set_coeff_ui(g, 0, 0); fmpz_poly_set_coeff_ui(g, 1, 1); if (n_randlimb(state) % 2) fmpz_poly_neg(g, g); /* get -x term */ n = n_randint(state, 50); fmpz_poly_revert_series(f, g, n); fmpz_poly_compose_series(h, g, f, n); result = ((n <= 1 && fmpz_poly_is_zero(h)) || (h->length == 2 && fmpz_is_zero(h->coeffs + 0) && fmpz_is_one(h->coeffs + 1))); if (!result) { printf("FAIL (comparison):\n"); fmpz_poly_print(f), printf("\n\n"); fmpz_poly_print(g), printf("\n\n"); fmpz_poly_print(h), printf("\n\n"); abort(); } fmpz_poly_clear(f); fmpz_poly_clear(g); fmpz_poly_clear(h); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("revert_series_lagrange_fast...."); fflush(stdout); /* Check aliasing */ for (i = 0; i < 10 * flint_test_multiplier(); i++) { fmpz_poly_t f, g; slong n; fmpz_poly_init(f); fmpz_poly_init(g); fmpz_poly_randtest(g, state, n_randint(state, 50), 1+n_randint(state,100)); fmpz_poly_set_coeff_ui(g, 0, 0); fmpz_poly_set_coeff_ui(g, 1, 1); if (n_randlimb(state) % 2) fmpz_poly_neg(g, g); /* get -x term */ n = n_randint(state, 50); fmpz_poly_revert_series_lagrange_fast(f, g, n); fmpz_poly_revert_series_lagrange_fast(g, g, n); result = (fmpz_poly_equal(f, g)); if (!result) { flint_printf("FAIL (aliasing):\n"); fmpz_poly_print(f), flint_printf("\n\n"); fmpz_poly_print(g), flint_printf("\n\n"); abort(); } fmpz_poly_clear(f); fmpz_poly_clear(g); } /* Check f(f^(-1)) = id */ for (i = 0; i < 10 * flint_test_multiplier(); i++) { fmpz_poly_t f, g, h; slong n; fmpz_poly_init(f); fmpz_poly_init(g); fmpz_poly_init(h); fmpz_poly_randtest(g, state, n_randint(state, 50), 1+n_randint(state,100)); fmpz_poly_set_coeff_ui(g, 0, 0); fmpz_poly_set_coeff_ui(g, 1, 1); if (n_randlimb(state) % 2) fmpz_poly_neg(g, g); /* get -x term */ n = n_randint(state, 50); fmpz_poly_revert_series_lagrange_fast(f, g, n); fmpz_poly_compose_series(h, g, f, n); result = ((n <= 1 && fmpz_poly_is_zero(h)) || (h->length == 2 && fmpz_is_zero(h->coeffs + 0) && fmpz_is_one(h->coeffs + 1))); if (!result) { flint_printf("FAIL (comparison):\n"); fmpz_poly_print(f), flint_printf("\n\n"); fmpz_poly_print(g), flint_printf("\n\n"); fmpz_poly_print(h), flint_printf("\n\n"); abort(); } fmpz_poly_clear(f); fmpz_poly_clear(g); fmpz_poly_clear(h); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int fmpz_poly_mat_inv(fmpz_poly_mat_t Ainv, fmpz_poly_t den, const fmpz_poly_mat_t A) { long n = fmpz_poly_mat_nrows(A); if (n == 0) { fmpz_poly_one(den); return 1; } else if (n == 1) { fmpz_poly_set(den, E(A, 0, 0)); fmpz_poly_one(E(Ainv, 0, 0)); return !fmpz_poly_is_zero(den); } else if (n == 2) { fmpz_poly_mat_det(den, A); if (fmpz_poly_is_zero(den)) { return 0; } else if (Ainv == A) { fmpz_poly_swap(E(A, 0, 0), E(A, 1, 1)); fmpz_poly_neg(E(A, 0, 1), E(A, 0, 1)); fmpz_poly_neg(E(A, 1, 0), E(A, 1, 0)); return 1; } else { fmpz_poly_set(E(Ainv, 0, 0), E(A, 1, 1)); fmpz_poly_set(E(Ainv, 1, 1), E(A, 0, 0)); fmpz_poly_neg(E(Ainv, 0, 1), E(A, 0, 1)); fmpz_poly_neg(E(Ainv, 1, 0), E(A, 1, 0)); return 1; } } else { fmpz_poly_mat_t LU, I; long * perm; int result; perm = _perm_init(n); fmpz_poly_mat_init_set(LU, A); result = (fmpz_poly_mat_fflu(LU, den, perm, LU, 1) == n); if (result) { fmpz_poly_mat_init(I, n, n); fmpz_poly_mat_one(I); fmpz_poly_mat_solve_fflu_precomp(Ainv, perm, LU, I); fmpz_poly_mat_clear(I); } else fmpz_poly_zero(den); if (_perm_parity(perm, n)) { fmpz_poly_mat_neg(Ainv, Ainv); fmpz_poly_neg(den, den); } _perm_clear(perm); fmpz_poly_mat_clear(LU); return result; } }
bool poly_inverse_poly_p(fmpz_poly_t Fp, const fmpz_poly_t a, const ntru_params *params) { bool retval = false; int k = 0, j = 0; fmpz *b_last; fmpz_poly_t a_tmp, b, c, f, g; /* general initialization of temp variables */ fmpz_poly_init(b); fmpz_poly_set_coeff_ui(b, 0, 1); fmpz_poly_init(c); fmpz_poly_init(f); fmpz_poly_set(f, a); /* set g(x) = x^N − 1 */ fmpz_poly_init(g); fmpz_poly_set_coeff_si(g, 0, -1); fmpz_poly_set_coeff_si(g, params->N, 1); /* avoid side effects */ fmpz_poly_init(a_tmp); fmpz_poly_set(a_tmp, a); fmpz_poly_zero(Fp); while (1) { while (fmpz_poly_get_coeff_ptr(f, 0) && fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) { for (uint32_t i = 1; i <= params->N; i++) { fmpz *f_coeff = fmpz_poly_get_coeff_ptr(f, i); fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, params->N - i); /* f(x) = f(x) / x */ fmpz_poly_set_coeff_fmpz_n(f, i - 1, f_coeff); /* c(x) = c(x) * x */ fmpz_poly_set_coeff_fmpz_n(c, params->N + 1 - i, c_coeff); } fmpz_poly_set_coeff_si(f, params->N, 0); fmpz_poly_set_coeff_si(c, 0, 0); k++; if (fmpz_poly_degree(f) == -1) goto cleanup; } if (fmpz_poly_is_zero(g) == 1) goto cleanup; if (fmpz_poly_degree(f) == 0) break; if (fmpz_poly_degree(f) < fmpz_poly_degree(g)) { /* exchange f and g and exchange b and c */ fmpz_poly_swap(f, g); fmpz_poly_swap(b, c); } { fmpz_poly_t c_tmp, g_tmp; fmpz_t u, mp_tmp; fmpz_init(u); fmpz_zero(u); fmpz_init_set(mp_tmp, fmpz_poly_get_coeff_ptr(f, 0)); fmpz_poly_init(g_tmp); fmpz_poly_set(g_tmp, g); fmpz_poly_init(c_tmp); fmpz_poly_set(c_tmp, c); /* u = f[0] * g[0]^(-1) mod p */ /* = (f[0] mod p) * (g[0] inverse mod p) mod p */ fmpz_invmod_ui(u, fmpz_poly_get_coeff_ptr(g, 0), params->p); fmpz_mod_ui(mp_tmp, mp_tmp, params->p); fmpz_mul(u, mp_tmp, u); fmpz_mod_ui(u, u, params->p); /* f = f - u * g mod p */ fmpz_poly_scalar_mul_fmpz(g_tmp, g_tmp, u); fmpz_poly_sub(f, f, g_tmp); fmpz_poly_mod_unsigned(f, params->p); /* b = b - u * c mod p */ fmpz_poly_scalar_mul_fmpz(c_tmp, c_tmp, u); fmpz_poly_sub(b, b, c_tmp); fmpz_poly_mod_unsigned(b, params->p); fmpz_clear(u); fmpz_poly_clear(g_tmp); fmpz_poly_clear(c_tmp); } } k = k % params->N; b_last = fmpz_poly_get_coeff_ptr(b, params->N); if (fmpz_cmp_si_n(b_last, 0)) goto cleanup; /* Fp(x) = x^(N-k) * b(x) */ for (int i = params->N - 1; i >= 0; i--) { fmpz *b_i; /* b(X) = f[0]^(-1) * b(X) (mod p) */ { fmpz_t mp_tmp; fmpz_init(mp_tmp); fmpz_invmod_ui(mp_tmp, fmpz_poly_get_coeff_ptr(f, 0), params->p); if (fmpz_poly_get_coeff_ptr(b, i)) { fmpz_mul(fmpz_poly_get_coeff_ptr(b, i), fmpz_poly_get_coeff_ptr(b, i), mp_tmp); fmpz_mod_ui(fmpz_poly_get_coeff_ptr(b, i), fmpz_poly_get_coeff_ptr(b, i), params->p); } } j = i - k; if (j < 0) j = j + params->N; b_i = fmpz_poly_get_coeff_ptr(b, i); fmpz_poly_set_coeff_fmpz_n(Fp, j, b_i); } /* check if the f * Fp = 1 (mod p) condition holds true */ fmpz_poly_set(a_tmp, a); poly_starmultiply(a_tmp, a_tmp, Fp, params, params->p); if (fmpz_poly_is_one(a_tmp)) retval = true; else fmpz_poly_zero(Fp); cleanup: fmpz_poly_clear(a_tmp); fmpz_poly_clear(b); fmpz_poly_clear(c); fmpz_poly_clear(f); fmpz_poly_clear(g); return retval; }
bool poly_inverse_poly_q(fmpz_poly_t Fq, const fmpz_poly_t a, const ntru_params *params) { bool retval = false; int k = 0, j = 0; fmpz *b_last; fmpz_poly_t a_tmp, b, c, f, g; /* general initialization of temp variables */ fmpz_poly_init(b); fmpz_poly_set_coeff_ui(b, 0, 1); fmpz_poly_init(c); fmpz_poly_init(f); fmpz_poly_set(f, a); /* set g(x) = x^N − 1 */ fmpz_poly_init(g); fmpz_poly_set_coeff_si(g, 0, -1); fmpz_poly_set_coeff_si(g, params->N, 1); /* avoid side effects */ fmpz_poly_init(a_tmp); fmpz_poly_set(a_tmp, a); fmpz_poly_zero(Fq); while (1) { while (fmpz_poly_get_coeff_ptr(f, 0) && fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) { for (uint32_t i = 1; i <= params->N; i++) { fmpz *f_coeff = fmpz_poly_get_coeff_ptr(f, i); fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, params->N - i); /* f(x) = f(x) / x */ fmpz_poly_set_coeff_fmpz_n(f, i - 1, f_coeff); /* c(x) = c(x) * x */ fmpz_poly_set_coeff_fmpz_n(c, params->N + 1 - i, c_coeff); } fmpz_poly_set_coeff_si(f, params->N, 0); fmpz_poly_set_coeff_si(c, 0, 0); k++; if (fmpz_poly_degree(f) == -1) goto cleanup; } if (fmpz_poly_is_zero(g) == 1) goto cleanup; if (fmpz_poly_degree(f) == 0) break; if (fmpz_poly_degree(f) < fmpz_poly_degree(g)) { fmpz_poly_swap(f, g); fmpz_poly_swap(b, c); } fmpz_poly_add(f, g, f); fmpz_poly_mod_unsigned(f, 2); fmpz_poly_add(b, c, b); fmpz_poly_mod_unsigned(b, 2); } k = k % params->N; b_last = fmpz_poly_get_coeff_ptr(b, params->N); if (fmpz_cmp_si_n(b_last, 0)) goto cleanup; /* Fq(x) = x^(N-k) * b(x) */ for (int i = params->N - 1; i >= 0; i--) { fmpz *b_i; j = i - k; if (j < 0) j = j + params->N; b_i = fmpz_poly_get_coeff_ptr(b, i); fmpz_poly_set_coeff_fmpz_n(Fq, j, b_i); } poly_mod2_to_modq(Fq, a_tmp, params); /* check if the f * Fq = 1 (mod p) condition holds true */ fmpz_poly_set(a_tmp, a); poly_starmultiply(a_tmp, a_tmp, Fq, params, params->q); if (fmpz_poly_is_one(a_tmp)) retval = true; else fmpz_poly_zero(Fq); cleanup: fmpz_poly_clear(a_tmp); fmpz_poly_clear(b); fmpz_poly_clear(c); fmpz_poly_clear(f); fmpz_poly_clear(g); return retval; }