void fmpz_poly_hensel_lift_once(fmpz_poly_factor_t lifted_fac, const fmpz_poly_t f, const nmod_poly_factor_t local_fac, long N) { const long r = local_fac->num; long i; long *link; fmpz_poly_t *v, *w; link = flint_malloc((2*r - 2) * sizeof(long)); v = flint_malloc(2*(2*r - 2) * sizeof(fmpz_poly_t)); w = v + (2*r - 2); for(i = 0; i < 2*r - 2; i++) { fmpz_poly_init(v[i]); fmpz_poly_init(w[i]); } _fmpz_poly_hensel_start_lift(lifted_fac, link, v, w, f, local_fac, N); for (i = 0; i < 2*r - 2; i++) { fmpz_poly_clear(v[i]); fmpz_poly_clear(w[i]); } flint_free(link); flint_free(v); }
/** * Test error handling of importing private key. */ void test_import_private_key2(void) { keypair pair; fmpz_poly_t f, g, priv, priv_inv; int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 }; int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; ntru_params params; fmpz_poly_init(priv); fmpz_poly_init(priv_inv); params.N = 11; params.p = 3; params.q = 32; poly_new(f, f_c, 11); poly_new(g, g_c, 11); ntru_create_keypair(&pair, f, g, ¶ms); export_priv_key("priv.key", pair.priv, ¶ms); import_priv_key(priv, priv_inv, ".", ¶ms); remove("priv.key"); CU_ASSERT_NOT_EQUAL(1, fmpz_poly_equal(priv, pair.priv)); }
static void poly_mod2_to_modq(fmpz_poly_t Fq, const fmpz_poly_t a, const ntru_params *params) { int v = 2; fmpz_poly_t poly_tmp, two; fmpz_poly_init(poly_tmp); fmpz_poly_zero(poly_tmp); fmpz_poly_init(two); fmpz_poly_set_coeff_ui(two, 0, 2); while (v < (int)(params->q)) { v = v * 2; poly_starmultiply(poly_tmp, a, Fq, params, v); fmpz_poly_sub(poly_tmp, two, poly_tmp); fmpz_poly_mod_unsigned(poly_tmp, v); poly_starmultiply(Fq, Fq, poly_tmp, params, v); } fmpz_poly_clear(poly_tmp); fmpz_poly_clear(two); }
int dgsl_rot_mp_call_gpv_inlattice(fmpz_poly_t rop, const dgsl_rot_mp_t *self, gmp_randstate_t state) { assert(rop); assert(self); const long n = self->n; mpz_t tmp_z; mpz_init(tmp_z); fmpz_t tmp; fmpz_init(tmp); fmpz_poly_zero(rop); fmpz_poly_t tmp_poly; fmpz_poly_init(tmp_poly); fmpz_poly_set(tmp_poly, self->B); fmpz_poly_realloc(tmp_poly, n); tmp_poly->length = n; fmpz_poly_t tmp2; fmpz_poly_init(tmp2); for(long i=0; i<n; i++) { self->D[i]->call(tmp_z, self->D[i], state); fmpz_set_mpz(tmp, tmp_z); fmpz_poly_scalar_mul_fmpz(tmp2, tmp_poly, tmp); fmpz_poly_add(rop, rop, tmp2); _fmpz_vec_rot_left_neg(tmp_poly->coeffs, tmp_poly->coeffs, n); } fmpz_poly_clear(tmp_poly); fmpz_poly_add(rop, rop, self->c_z); fmpz_poly_clear(tmp2); mpz_clear(tmp_z); fmpz_clear(tmp); return 0; }
void hypgeom_precompute(hypgeom_t hyp) { if (fmpz_poly_is_one(hyp->A) && fmpz_poly_is_one(hyp->B)) { _hypgeom_precompute(hyp, hyp->P, hyp->Q); } else { fmpz_poly_t P2, Q2; fmpz_poly_init(P2); fmpz_poly_init(Q2); hypgeom_standardize(P2, Q2, hyp->A, hyp->B, hyp->P, hyp->Q); _hypgeom_precompute(hyp, P2, Q2); { fmpz_t t; fmpz_init(t); fmpz_poly_evaluate_si(t, hyp->A, 0); mag_mul_fmpz(hyp->MK, hyp->MK, t); fmpz_poly_evaluate_si(t, hyp->B, 0); mag_div_fmpz(hyp->MK, hyp->MK, t); fmpz_clear(t); } fmpz_poly_clear(P2); fmpz_poly_clear(Q2); } }
void fmpz_poly_q_init(fmpz_poly_q_t rop) { rop->num = flint_malloc(sizeof(fmpz_poly_struct)); rop->den = flint_malloc(sizeof(fmpz_poly_struct)); fmpz_poly_init(rop->num); fmpz_poly_init(rop->den); fmpz_poly_set_si(rop->den, 1); }
int main(void) { int iter; FLINT_TEST_INIT(state); flint_printf("sgn_eval_at_half...."); fflush(stdout); /* Check aliasing */ for (iter = 0; iter < 1000 * flint_test_multiplier(); iter++) { fmpz_t a; fmpz_poly_t f, g; long i, d; int s1, s2; fmpz_init(a); fmpz_poly_init(f); fmpz_poly_init(g); fmpz_poly_randtest(f, state, n_randint(state, 100), 200); s1 = fmpz_poly_sgn_eval_at_half(f); fmpz_poly_set(g, f); d = fmpz_poly_degree(g); for (i = 0; i <= d; i++) { fmpz_mul_2exp(fmpz_poly_get_coeff_ptr(g, i), fmpz_poly_get_coeff_ptr(g, i), d - i); } fmpz_one(a); fmpz_poly_evaluate_fmpz(a, g, a); s2 = fmpz_sgn(a); if (s1 != s2) { flint_printf("FAIL:\n"); fmpz_poly_print(f); printf("\n\n"); printf("s1 = %d, s2 = %d\n\n", s1, s2); abort(); } fmpz_clear(a); fmpz_poly_clear(f); fmpz_poly_clear(g); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
void F_mpz_poly_power(F_mpz_poly_t output, const F_mpz_poly_t poly, const unsigned long exp){ fmpz_poly_t foutput, fpoly; fmpz_poly_init(foutput); fmpz_poly_init(fpoly); F_mpz_poly_to_fmpz_poly(fpoly, poly); fmpz_poly_power(foutput, fpoly, exp); fmpz_poly_to_F_mpz_poly(output, foutput); fmpz_poly_clear(fpoly); fmpz_poly_clear(foutput); }
void fmpz_poly_factor_realloc(fmpz_poly_factor_t fac, long alloc) { if (alloc == 0) /* Clear up, reinitialise */ { fmpz_poly_factor_clear(fac); fmpz_poly_factor_init(fac); } else if (fac->alloc) /* Realloc */ { if (fac->alloc > alloc) { long i; for (i = alloc; i < fac->num; i++) fmpz_poly_clear(fac->p + i); fac->p = flint_realloc(fac->p, alloc * sizeof(fmpz_poly_struct)); fac->exp = flint_realloc(fac->exp, alloc * sizeof(long)); fac->alloc = alloc; } else if (fac->alloc < alloc) { long i; fac->p = flint_realloc(fac->p, alloc * sizeof(fmpz_poly_struct)); fac->exp = flint_realloc(fac->exp, alloc * sizeof(long)); for (i = fac->alloc; i < alloc; i++) { fmpz_poly_init(fac->p + i); fac->exp[i] = 0L; } fac->alloc = alloc; } } else /* Nothing allocated already so do it now */ { long i; fac->p = flint_malloc(alloc * sizeof(fmpz_poly_struct)); fac->exp = flint_calloc(alloc, sizeof(long)); for (i = 0; i < alloc; i++) fmpz_poly_init(fac->p + i); fac->num = 0; fac->alloc = alloc; } }
/** * Test error handling of importing public key. */ void test_import_public_key2(void) { keypair pair; fmpz_poly_t f, g, pub; int f_c[] = { -1, 1, 1, 0, -1, 0, 1, 0, 0, 1, -1 }; int g_c[] = { -1, 0, 1, 1, 0, 1, 0, 0, -1, 0, -1 }; ntru_params params; fmpz_poly_init(pub); params.N = 11; params.p = 3; params.q = 32; poly_new(f, f_c, 11); poly_new(g, g_c, 11); ntru_create_keypair(&pair, f, g, ¶ms); export_public_key("pub.key", pair.pub, ¶ms); import_public_key(pub, "foo", ¶ms); remove("pub.key"); CU_ASSERT_NOT_EQUAL(1, fmpz_poly_equal(pub, pair.pub)); }
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); } }
int main(int argc, char *argv[]) { assert(argc>=3); const long n = atol(argv[1]); const long prec = atol(argv[2]); flint_rand_t randstate; flint_randinit_seed(randstate, 0x1337, 1); mpfr_t sigma; mpfr_init2(sigma, prec); mpfr_set_d(sigma, _gghlite_sigma(n), MPFR_RNDN); fmpz_poly_t g; fmpz_poly_init(g); fmpz_poly_sample_sigma(g, n, sigma, randstate); fmpq_poly_t gq; fmpq_poly_init(gq); fmpq_poly_set_fmpz_poly(gq, g); fmpq_poly_t g_inv; fmpq_poly_init(g_inv); fmpq_poly_t modulus; fmpq_poly_init_oz_modulus(modulus, n); printf(" n: %4ld, log σ': %7.2f, ",n, log2(_gghlite_sigma(n))); uint64_t t = ggh_walltime(0); fmpq_poly_invert_mod(g_inv, gq, modulus); t = ggh_walltime(t); printf("xgcd: %7.3f, ", t/1000000.0); fflush(0); t = ggh_walltime(0); _fmpq_poly_oz_invert_approx(g_inv, gq, n, 160); t = ggh_walltime(t); printf("%4ld: %7.3f, ", prec, t/1000000.0); fflush(0); t = ggh_walltime(0); fmpq_poly_oz_invert_approx(g_inv, gq, n, 160, 0); t = ggh_walltime(t); printf("%4lditer: %7.3f, ", prec, t/1000000.0); fflush(0); t = ggh_walltime(0); _fmpq_poly_oz_invert_approx(g_inv, gq, n, 0); t = ggh_walltime(t); printf("∞: %7.3f.", t/1000000.0); printf("\n"); mpfr_clear(sigma); fmpz_poly_clear(g); fmpq_poly_clear(gq); fmpq_poly_clear(g_inv); fmpq_poly_clear(modulus); flint_randclear(randstate); flint_cleanup(); return 0; }
void test_xor_bits(){ unsigned m, aux; mpz_t c0, c1; fmpz_poly_t poly_c; mpz_init(c0); mpz_init(c1); fmpz_poly_init(poly_c); fhe_pk_t pk; fhe_sk_t sk; fhe_pk_init(pk); fhe_sk_init(sk); fhe_keygen(pk, sk); int j, nbits; struct timeval start, end; long mtime, seconds, useconds; //clock_t START_eval; //double T_Elapsed4; m= 2147483648 ; for(int i=0; i< 250; i++){ m= m + 2; j=0; //printf("--------->%i\n", m % 2); fhe_encrypt(c0, pk, m % 2); fmpz_poly_set_coeff_mpz ( poly_c , 0 , c0 ); aux = m; aux = aux >> 1; do { // printf("--------->%i\n", aux % 2); fhe_encrypt(c0, pk, aux % 2); aux = aux >> 1; j++; fmpz_poly_set_coeff_mpz ( poly_c , j , c0 ); //fhe_add(c0, c0, c1, pk); }while(aux != 0); nbits= j+1; //START_eval = clock(); gettimeofday(&start, NULL); mpz_set(c1, c0); while(j>0){ j--; fmpz_poly_get_coeff_mpz ( c0 , poly_c , j ); fhe_add(c1, c0, c1, pk); } gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; //printf("Elapsed time in KeyGen : %ld ms \n", mtime ); aux = fhe_decrypt(c1, sk); printf("xor de %u: %d bits, runtime: %ld ms\n", m, nbits, mtime); } mpz_clear(c0); mpz_clear(c1); fmpz_poly_clear(poly_c); fhe_pk_clear(pk); fhe_sk_clear(sk); }
int fmpz_poly_sqrt_classical(fmpz_poly_t b, const fmpz_poly_t a) { long blen, len = a->length; int result; if (len % 2 == 0) { fmpz_poly_zero(b); return len == 0; } if (b == a) { fmpz_poly_t tmp; fmpz_poly_init(tmp); result = fmpz_poly_sqrt(tmp, a); fmpz_poly_swap(b, tmp); fmpz_poly_clear(tmp); return result; } blen = len / 2 + 1; fmpz_poly_fit_length(b, blen); _fmpz_poly_set_length(b, blen); result = _fmpz_poly_sqrt_classical(b->coeffs, a->coeffs, len); if (!result) _fmpz_poly_set_length(b, 0); return result; }
void test_sum_integers(unsigned a , unsigned b, fhe_pk_t pk, fhe_sk_t sk){ unsigned res; fmpz_poly_t s; // mpz_t c; fmpz_poly_init(s); mpz_init(c); // Integers to be added printf("a: %u , b: %u ", a,b); // sum32_ui(s, a, b, pk, sk); //decrypt res=0; for(int i=fmpz_poly_degree(s); i >= 0; i--){ fmpz_poly_get_coeff_mpz(c,s,i); res = res * 2 + fhe_decrypt(c, sk); } printf("sum: %u ", res); assert(res == a + b); // fmpz_poly_clear(s); mpz_clear(c); }
/* Compute a pure ratio P2(k)/Q2(k) for the term A(k)/B(k) * [P(1)P(2)...P(k)] / [Q(1)Q(2)...Q(k)] */ void hypgeom_standardize(fmpz_poly_t P2, fmpz_poly_t Q2, const fmpz_poly_t A, const fmpz_poly_t B, const fmpz_poly_t P, const fmpz_poly_t Q) { fmpz_t s; fmpz_poly_t T; fmpz_init(s); fmpz_poly_init(T); fmpz_set_si(s, -WORD(1)); /* P = A * B(k-1) * P */ fmpz_poly_taylor_shift(T, B, s); fmpz_poly_mul(P2, A, T); fmpz_poly_mul(P2, P2, P); /* Q = B * A(k-1) * Q */ fmpz_poly_taylor_shift(T, A, s); fmpz_poly_mul(Q2, B, T); fmpz_poly_mul(Q2, Q2, Q); fmpz_clear(s); fmpz_poly_clear(T); }
void fmpz_poly_realloc(fmpz_poly_t poly, long alloc) { if (alloc == 0) /* Clear up, reinitialise */ { fmpz_poly_clear(poly); fmpz_poly_init(poly); return; } if (poly->alloc) /* Realloc */ { fmpz_poly_truncate(poly, alloc); poly->coeffs = (fmpz *) flint_realloc(poly->coeffs, alloc * sizeof(fmpz)); if (alloc > poly->alloc) mpn_zero((mp_ptr) (poly->coeffs + poly->alloc), alloc - poly->alloc); } else /* Nothing allocated already so do it now */ { poly->coeffs = (fmpz *) flint_calloc(alloc, sizeof(fmpz)); } poly->alloc = alloc; }
void fmpz_poly_mat_sqrlow(fmpz_poly_mat_t B, const fmpz_poly_mat_t A, slong len) { slong n = A->r; if (n == 0) return; if (len < 1) { fmpz_poly_mat_zero(B); return; } if (n == 1) { fmpz_poly_sqrlow(E(B, 0, 0), E(A, 0, 0), len); return; } if (n == 2) { fmpz_poly_t t, u; fmpz_poly_init(t); fmpz_poly_init(u); fmpz_poly_addlow(t, E(A, 0, 0), E(A, 1, 1), len); fmpz_poly_mullow(u, E(A, 0, 1), E(A, 1, 0), len); fmpz_poly_sqrlow(E(B, 0, 0), E(A, 0, 0), len); fmpz_poly_addlow(E(B, 0, 0), E(B, 0, 0), u, len); fmpz_poly_sqrlow(E(B, 1, 1), E(A, 1, 1), len); fmpz_poly_addlow(E(B, 1, 1), E(B, 1, 1), u, len); fmpz_poly_mullow(E(B, 0, 1), E(A, 0, 1), t, len); fmpz_poly_mullow(E(B, 1, 0), E(A, 1, 0), t, len); fmpz_poly_clear(t); fmpz_poly_clear(u); return; } fmpz_poly_mat_mullow(B, A, A, len); }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("swap...."); fflush(stdout); for (i = 0; i < 1000 * flint_test_multiplier(); i++) { fmpz_poly_t a, b, c; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(c); fmpz_poly_randtest(a, state, n_randint(state, 100), 200); fmpz_poly_randtest(b, state, n_randint(state, 100), 200); fmpz_poly_set(c, b); fmpz_poly_swap(a, b); result = (fmpz_poly_equal(a, c)); if (!result) { flint_printf("FAIL:\n"); fmpz_poly_print(a), flint_printf("\n\n"); fmpz_poly_print(b), flint_printf("\n\n"); fmpz_poly_print(c), flint_printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(c); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
void poly_starmultiply(fmpz_poly_t c, const fmpz_poly_t a, const fmpz_poly_t b, const ntru_params *params, uint32_t modulus) { fmpz_poly_t a_tmp; fmpz_t c_coeff_k; fmpz_poly_init(a_tmp); fmpz_init(c_coeff_k); /* avoid side effects */ fmpz_poly_set(a_tmp, a); fmpz_poly_zero(c); for (int k = params->N - 1; k >= 0; k--) { int j; j = k + 1; fmpz_set_si(c_coeff_k, 0); for (int i = params->N - 1; i >= 0; i--) { fmpz *a_tmp_coeff_i, *b_coeff_j; if (j == (int)(params->N)) j = 0; a_tmp_coeff_i = fmpz_poly_get_coeff_ptr(a_tmp, i); b_coeff_j = fmpz_poly_get_coeff_ptr(b, j); if (fmpz_cmp_si_n(a_tmp_coeff_i, 0) && fmpz_cmp_si_n(b_coeff_j, 0)) { fmpz_t fmpz_tmp; fmpz_init(fmpz_tmp); fmpz_mul(fmpz_tmp, a_tmp_coeff_i, b_coeff_j); fmpz_add(fmpz_tmp, fmpz_tmp, c_coeff_k); fmpz_mod_ui(c_coeff_k, fmpz_tmp, modulus); fmpz_poly_set_coeff_fmpz(c, k, c_coeff_k); fmpz_clear(fmpz_tmp); } j++; } fmpz_clear(c_coeff_k); } fmpz_poly_clear(a_tmp); }
void renf_randtest(renf_t nf, flint_rand_t state, slong len, slong prec, mp_bitcnt_t bits) { fmpz_poly_t p; fmpq_poly_t p2; fmpz * c_array; slong * k_array; slong n_interval, n_exact; ulong i; arb_t emb; /* compute a random irreducible polynomial */ if (len <= 1) { fprintf(stderr, "ERROR (renf_randtest): got length < 2\n"); abort(); } fmpz_poly_init(p); do{ fmpz_poly_randtest_irreducible(p, state, len, bits); }while(!fmpz_poly_has_real_root(p)); /* pick a random real root */ c_array = _fmpz_vec_init(p->length); k_array = malloc((p->length) * sizeof(slong)); n_interval = 0; fmpz_poly_isolate_real_roots(NULL, &n_exact, c_array, k_array, &n_interval, p); if (n_interval == 0) { fprintf(stderr, "Runtime error\n"); abort(); } i = n_randint(state, n_interval); /* construct the associated number field */ arb_init(emb); arb_from_interval(emb, c_array+i, k_array[i], fmpz_bits(c_array + i) + FLINT_MAX(k_array[i], 0) + 2); fmpq_poly_init(p2); fmpq_poly_set_fmpz_poly(p2, p); /* NOTE: renf init might not be happy with the ball emb */ renf_init(nf, p2, emb, prec); _fmpz_vec_clear(c_array, p->length); free(k_array); fmpz_poly_clear(p); fmpq_poly_clear(p2); arb_clear(emb); }
int main() { flint_printf("hilbert_class_poly...."); fflush(stdout); { slong i; mp_limb_t c; fmpz_poly_t hd; nmod_poly_t hdp; fmpz_poly_init(hd); nmod_poly_init(hdp, 31337); for (i = 0; i < 1000; i++) { acb_modular_hilbert_class_poly(hd, -i); fmpz_poly_get_nmod_poly(hdp, hd); c = nmod_poly_evaluate_nmod(hdp, 2015); if (c != hilbert_poly_values[i]) { flint_printf("FAIL!\n"); flint_printf("D = %wd\n", -i); flint_printf("c = %wu\n", c); flint_printf("expected = %d\n", (int) hilbert_poly_values[i]); abort(); } } for (i = 0; hilbert_poly_values_2[2 * i] != 0; i++) { acb_modular_hilbert_class_poly(hd, hilbert_poly_values_2[2 * i]); fmpz_poly_get_nmod_poly(hdp, hd); c = nmod_poly_evaluate_nmod(hdp, 2015); if (c != hilbert_poly_values_2[2 * i + 1]) { flint_printf("FAIL!\n"); flint_printf("D = %wd\n", -i); flint_printf("c = %wu\n", c); flint_printf("expected = %d\n", (int) hilbert_poly_values_2[2*i+1]); abort(); } } fmpz_poly_clear(hd); nmod_poly_clear(hdp); } flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int dgsl_rot_mp_call_plus_fmpz_poly(fmpz_poly_t rop, const dgsl_rot_mp_t *self, const fmpz_poly_t c, gmp_randstate_t state) { fmpz_poly_t t; fmpz_poly_init(t); fmpz_poly_set(t, c); fmpq_poly_t tq; fmpq_poly_init(tq); // == 0 fmpq_poly_set_fmpz_poly(tq, t); fmpq_poly_neg(tq, tq); dgsl_rot_mp_call_recenter_fmpq_poly(rop, self, tq, state); fmpz_poly_add(rop, rop, t); fmpq_poly_clear(tq); fmpz_poly_clear(t); return 0; }
int main(void) { int i, result; flint_rand_t state; printf("evaluate_mod...."); fflush(stdout); flint_randinit(state); /* Compare with evaluation over the integers */ for (i = 0; i < 10000; i++) { fmpz_t b, s; fmpz_poly_t f; mp_limb_t a, n, r; fmpz_poly_init(f); fmpz_poly_randtest(f, state, n_randint(state, 10), 20); n = n_randtest_not_zero(state); a = n_randint(state, n); fmpz_init(b); fmpz_init(s); fmpz_set_ui(b, a); r = fmpz_poly_evaluate_mod(f, a, n); fmpz_poly_evaluate_fmpz(s, f, b); result = (r == fmpz_mod_ui(s, s, n)); if (!result) { printf("FAIL:\n"); fmpz_poly_print(f), printf("\n\n"); gmp_printf("a = %Mu\n\n", a); gmp_printf("n = %Mu\n\n", n); gmp_printf("r = %Mu\n\n", r); printf("s = "), fmpz_print(s), printf("\n\n"); abort(); } fmpz_poly_clear(f); fmpz_clear(b); fmpz_clear(s); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
void poly_new(fmpz_poly_t new_poly, int const * const c, const size_t len) { if (!new_poly) NTRU_ABORT_DEBUG("Unexpected NULL parameter in"); fmpz_poly_init(new_poly); for (uint32_t i = 0; i < len; i++) fmpz_poly_set_coeff_si(new_poly, i, c[i]); }
void _fmpz_ramanujan_tau(fmpz_t res, fmpz_factor_t factors) { fmpz_poly_t poly; fmpz_t tau_p, p_11, next, this, prev; long k, r; ulong max_prime; max_prime = 1UL; for (k = 0; k < factors->length; k++) { /* TODO: handle overflow properly */ max_prime = FLINT_MAX(max_prime, fmpz_get_ui(factors->p + k)); } fmpz_poly_init(poly); fmpz_poly_ramanujan_tau(poly, max_prime + 1); fmpz_set_ui(res, 1); fmpz_init(tau_p); fmpz_init(p_11); fmpz_init(next); fmpz_init(this); fmpz_init(prev); for (k = 0; k < factors->length; k++) { ulong p = fmpz_get_ui(factors->p + k); fmpz_set(tau_p, poly->coeffs + p); fmpz_set_ui(p_11, p); fmpz_pow_ui(p_11, p_11, 11); fmpz_set_ui(prev, 1); fmpz_set(this, tau_p); for (r = 1; r < fmpz_get_ui(factors->exp + k); r++) { fmpz_mul(next, tau_p, this); fmpz_submul(next, p_11, prev); fmpz_set(prev, this); fmpz_set(this, next); } fmpz_mul(res, res, this); } fmpz_clear(tau_p); fmpz_clear(p_11); fmpz_clear(next); fmpz_clear(this); fmpz_clear(prev); fmpz_poly_clear(poly); }
void fmpz_poly_mat_mullow(fmpz_poly_mat_t C, const fmpz_poly_mat_t A, const fmpz_poly_mat_t B, long len) { long ar, bc, br; long i, j, k; fmpz_poly_t t; ar = A->r; br = B->r; bc = B->c; if (br == 0 || len < 1) { fmpz_poly_mat_zero(C); return; } if (C == A || C == B) { fmpz_poly_mat_t T; fmpz_poly_mat_init(T, ar, bc); fmpz_poly_mat_mullow(T, A, B, len); fmpz_poly_mat_swap(C, T); fmpz_poly_mat_clear(T); return; } fmpz_poly_init(t); for (i = 0; i < ar; i++) { for (j = 0; j < bc; j++) { fmpz_poly_mullow(fmpz_poly_mat_entry(C, i, j), fmpz_poly_mat_entry(A, i, 0), fmpz_poly_mat_entry(B, 0, j), len); for (k = 1; k < br; k++) { fmpz_poly_mullow(t, fmpz_poly_mat_entry(A, i, k), fmpz_poly_mat_entry(B, k, j), len); fmpz_poly_add(fmpz_poly_mat_entry(C, i, j), fmpz_poly_mat_entry(C, i, j), t); } } } fmpz_poly_clear(t); }
int main() { fmpz_poly_t T, U; slong n; FLINT_TEST_INIT(state); flint_printf("chebyshev_u_polynomial...."); fflush(stdout); fmpz_poly_init(T); fmpz_poly_init(U); for (n = 0; n <= 500; n++) { arith_chebyshev_u_polynomial(U, n); arith_chebyshev_t_polynomial(T, n + 1); fmpz_poly_derivative(T, T); fmpz_poly_scalar_divexact_ui(T, T, n + 1); if (!fmpz_poly_equal(T, U)) { flint_printf("FAIL: n = %wd\n", n); flint_printf("T: "); fmpz_poly_print_pretty(T, "x"); flint_printf("\n"); flint_printf("U: "); fmpz_poly_print_pretty(U, "x"); flint_printf("\n"); abort(); } } fmpz_poly_clear(T); fmpz_poly_clear(U); FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
void sum32_ui(fmpz_poly_t s, unsigned a , unsigned b, fhe_pk_t pk , fhe_sk_t sk){ unsigned aux1 = a, aux2 = b; int i=0; mpz_t c1, c2; mpz_init(c1); mpz_init(c2); fmpz_poly_t poly_a, poly_b; fmpz_poly_init(poly_a); fmpz_poly_init(poly_b); // //printf("aux1[i] -----> %i\t", aux1 % 2); //printf("aux2[i] -----> %i\n", aux2 % 2); fhe_encrypt(c1, pk, aux1 % 2); fhe_encrypt(c2, pk, aux2 % 2); fmpz_poly_set_coeff_mpz( poly_a , i , c1 ); fmpz_poly_set_coeff_mpz( poly_b , i , c2 ); aux1 = aux1 >> 1; aux2 = aux2 >> 1; // while( aux1 != 0 || aux2 != 0){ //printf("aux1[i] -----> %i\t", aux1 % 2); //printf("aux2[i] -----> %i\n", aux2 % 2); fhe_encrypt(c1, pk, aux1 % 2); fhe_encrypt(c2, pk, aux2 % 2); i++; fmpz_poly_set_coeff_mpz (poly_a , i , c1 ); fmpz_poly_set_coeff_mpz (poly_b , i , c2 ); aux1 = aux1 >> 1; aux2 = aux2 >> 1; } // test_sum_mpz_t(s, poly_a, poly_b, pk , sk ); printf("nb de bits %d ", i+1); // mpz_clear(c1); mpz_clear(c2); fmpz_poly_clear(poly_a); fmpz_poly_clear(poly_b); }
void generate() { int ip, f, in, im; int flag = 0; flint_printf("["); for (ip = 0; ip < np; ip++) { slong prec = vp[ip] + 50; slong digits = ((double)prec * .30103) + 5; flint_printf("< %ld, [", digits); for (f = 0; f < nf; f++) { for (in = 0; in < nn; in++) { fmpz_poly_t pol; abel_jacobi_t aj; fmpz_poly_init(pol); vf[f](pol, vn[in]); abel_jacobi_init_poly(aj, 2, pol); /* should not be necessary */ abel_jacobi_compute(aj, flag, prec); /* print roots */ flint_printf("< ["); _acb_vec_arf_printd(aj->roots, vn[in], digits, ", "); flint_printf("]"); /* print exponents */ flint_printf(",[%ld",vm[0]); for (im = 1; im < nm; im++) flint_printf(",%ld",vm[im]); flint_printf("]>,\n"); fmpz_poly_clear(pol); abel_jacobi_clear(aj); } } flint_printf("] >\n"); } flint_printf("]\n"); }