Example #1
0
string *
bin_poly_to_ascii(const fmpz_poly_t poly,
		const ntru_params *params)
{
	string *result_string = ntru_malloc(sizeof(*result_string));
	char *binary_rep = ntru_malloc(CHAR_SIZE * (params->N));
	uint32_t i = 0;

	for (i = 0; i < params->N; i++) {
		fmpz *coeff = fmpz_poly_get_coeff_ptr(poly, i);

		if (coeff) {
			if (!fmpz_cmp_si(coeff, 1))
				binary_rep[i] = '1';
			else if (!fmpz_cmp_si(coeff, -1))
				binary_rep[i] = '0';
		} else {
			break;
		}
	}

	result_string->ptr = binary_rep;
	result_string->len = i;

	return result_string;
}
Example #2
0
void 
test_1(fmpz_t n)
 {
  mpfr_t q0; mpfr_init(q0); 
  mpfr_t q1; mpfr_init(q1);
  fmpz_t m; fmpz_init(m);
  int c;
  
  fmpz_get_mpfr(     q0, n   , MPFR_RNDA );
  fmpz_get_mpfr_3arg(q1, n[0], MPFR_RNDA );
  assert( mpfr_equal_p( q0, q1 ) );
  fmpz_set_mpfr(m, q0, MPFR_RNDA);
  if( fmpz_cmp_ui( n, WORD(0) ) >= 0 )
   {
    if( fmpz_cmp(n,m) > 0 )
     {
      flint_printf("RNDA test failed, n="); fmpz_print(n);
      flint_printf(", m="); fmpz_print(m); flint_printf("\n");
      assert(0);
     }
    fmpz_sub(m, m, n);
    IF_MORE_THAN_2;
   }
  else 
   {
    if( fmpz_cmp(n,m) < 0 )
     {
      flint_printf("RNDA test failed, n="); fmpz_print(n);
      flint_printf(", m="); fmpz_print(m); flint_printf("\n");
      assert(0);
     }
    fmpz_sub(m, n, m);
    IF_MORE_THAN_2;
   }
   
  fmpz_get_mpfr( q0, n, MPFR_RNDZ );
  fmpz_set_mpfr( m, q0, MPFR_RNDZ );
  c=fmpz_cmp_si( n, WORD(0) );
  if(c==0)
   assert(0 == fmpz_cmp_si(m,WORD(0)));
  else
   {
    if(c<0)
     {
      fmpz_neg(n, n);
      fmpz_neg(m, m);
     }
    assert( fmpz_cmp(n,m) >= 0 );
    if(c)
     assert( fmpz_cmp_si(m,WORD(0)) >= 0 );
    fmpz_sub(m, n, m);
    IF_MORE_THAN_2_AGAIN;
   }
  
  fmpz_clear(m);
  mpfr_clear(q1);
  mpfr_clear(q0);
 }
Example #3
0
 void ARingZZ::syzygy(const ElementType& a, const ElementType& b,
                      ElementType& x, ElementType& y) const
 {
   M2_ASSERT(!is_zero(b));
   // First check the special cases a = 0, b = 1, -1.  Other cases: use gcd.
   if (is_zero(a))
     {
       set_from_long(x, 1);
       set_zero(y);
       return;
     }
   if (fmpz_cmp_ui(&b,1) == 0)
     {
       set_from_long(x, 1);
       negate(y, a);
       return;
     }
   if (fmpz_cmp_si(&b,-1) == 0)
     {
       set_from_long(x, 1);
       set(y, a);
       return;
     }
   ElementType g;
   init(g);
   fmpz_gcd(&g,&a,&b);
   divide(y,a,g);
   divide(x,b,g);
   if (fmpz_sgn(&x) > 0)
     negate(y,y);
   else
     negate(x,x);
   clear(g);
 }
Example #4
0
int
fmpz_cmp_si_n(const fmpz_t f, slong g)
{
	fmpz_t f_cmp;

	fmpz_init(f_cmp);

	if (f)
		fmpz_set(f_cmp, f);
	else
		fmpz_set_si(f_cmp, 0);

	return fmpz_cmp_si(f_cmp, g);
}
Example #5
0
 void ARingZZ::elem_text_out(buffer &o, 
                             const ElementType& a,
                             bool p_one,
                             bool p_plus, 
                             bool p_parens) const
 {
   char *str;
   
   bool is_neg = (fmpz_cmp_si(&a, 0) == -1);
   bool is_one = (fmpz_cmp_si(&a, 1) == 0 || fmpz_cmp_si(&a, -1) == 0);
   
   if (!is_neg && p_plus) o << '+';
   if (is_one)
     {
       if (is_neg) o << '-';
       if (p_one) o << '1';
     }
   else
     {
       str = fmpz_get_str(static_cast<char*>(0), 10, &a);
       o << str;
     }
 }
Example #6
0
int check_ceil(renf_elem_t a, renf_t nf, int ans, const char * s)
{
    fmpz_t n;
    int test;

    fmpz_init(n);
    renf_elem_ceil(n, a, nf);
    test = fmpz_cmp_si(n, ans);
    fmpz_clear(n);

    if (test)
    {
        printf("FAIL:\n");
        printf("a = "); renf_elem_print_pretty(a, s, nf, 10, EANTIC_STR_ALG & EANTIC_STR_D); printf("\n");
        printf("got n = "); fmpz_print(n); printf(" but expected %d\n", ans);
        abort();
    }
    return 0;
}
Example #7
0
int
arf_cmp_2exp_si(const arf_t x, long e)
{
    if (arf_is_special(x))
    {
        if (arf_is_zero(x)) return -1;
        if (arf_is_pos_inf(x)) return 1;
        if (arf_is_neg_inf(x)) return -1;
        return 0;
    }

    if (ARF_SGNBIT(x))
        return -1;

    /* Fast path. */
    if (!COEFF_IS_MPZ(ARF_EXP(x)))
    {
        if (ARF_IS_POW2(x) && (ARF_EXP(x) - 1 == e))
            return 0;
        else
            return (ARF_EXP(x) <= e) ? -1 : 1;
    }

    if (ARF_IS_POW2(x))
    {
        fmpz_t t;
        fmpz_init(t);
        fmpz_one(t);
        fmpz_add_si(t, t, e);
        if (fmpz_equal(ARF_EXPREF(x), t))
        {
            fmpz_clear(t);
            return 0;
        }
        fmpz_clear(t);
    }

    return (fmpz_cmp_si(ARF_EXPREF(x), e) <= 0) ? -1 : 1;
}
Example #8
0
long _fmpz_poly_hensel_start_lift(fmpz_poly_factor_t lifted_fac, long *link, 
    fmpz_poly_t *v, fmpz_poly_t *w, const fmpz_poly_t f, 
    const nmod_poly_factor_t local_fac, long N)
{
    const long r = local_fac->num;

    long i, preve;
    fmpz_t p, P;
    fmpz_poly_t monic_f;

    fmpz_init(p);
    fmpz_init(P);
    fmpz_poly_init(monic_f);

    fmpz_set_ui(p, (local_fac->p + 0)->mod.n);
    fmpz_pow_ui(P, p, N);

    if (fmpz_is_one(fmpz_poly_lead(f)))
    {
        fmpz_poly_set(monic_f, f);
    }
    else if (fmpz_cmp_si(fmpz_poly_lead(f), -1) == 0)
    {
        fmpz_poly_neg(monic_f, f);
    }
    else
    {
        fmpz_t t;

        fmpz_init(t);
        fmpz_mod(t, fmpz_poly_lead(f), P);

        if (fmpz_invmod(t, t, P) == 0)
        {
            printf("Exception in fmpz_poly_start_hensel_lift.\n");
            abort();
        }

        fmpz_poly_scalar_mul_fmpz(monic_f, f, t);
        fmpz_poly_scalar_mod_fmpz(monic_f, monic_f, P);
        fmpz_clear(t);
    }

    fmpz_poly_hensel_build_tree(link, v, w, local_fac);

    {
        long *e, n = FLINT_CLOG2(N) + 1;

        e = flint_malloc(n * sizeof(long));
        for (e[i = 0] = N; e[i] > 1; i++)
            e[i + 1] = (e[i] + 1) / 2;

        for (i--; i > 0; i--)
        {
            fmpz_poly_hensel_lift_tree(link, v, w, monic_f, r, 
                p, e[i+1], e[i], 1);
        }
        if (N > 1)
        {
            fmpz_poly_hensel_lift_tree(link, v, w, monic_f, r, 
                p, e[i+1], e[i], 0);
        }

        preve = e[i+1];

        flint_free(e);
    }

    /*
        Now everything is lifted to p^N, we just need to 
        insert the factors into their correct places in lifted_fac.
     */
    fmpz_poly_factor_fit_length(lifted_fac, r);

    for (i = 0; i < 2*r - 2; i++)
    { 
        if (link[i] < 0)
        {
            fmpz_poly_scalar_smod_fmpz(lifted_fac->p + (- link[i] - 1), v[i], P);
            lifted_fac->exp[- link[i] - 1] = 1L; 
        }
    }
    lifted_fac->num = r;

    fmpz_clear(p);
    fmpz_clear(P);
    fmpz_poly_clear(monic_f);

    return preve;
}
Example #9
0
int main()
{
    slong iter;
    flint_rand_t state;

    flint_printf("abs_bound_lt_2exp_si....");
    fflush(stdout);

    flint_randinit(state);

    for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++)
    {
        fmpr_t x;
        fmpz_t b;
        slong c;

        fmpr_init(x);
        fmpz_init(b);

        fmpr_randtest_special(x, state, 2 + n_randint(state, 1000), 100);
        fmpr_abs_bound_lt_2exp_fmpz(b, x);
        c = fmpr_abs_bound_lt_2exp_si(x);

        if (c == -FMPR_PREC_EXACT)
        {
            if (!(fmpr_is_zero(x) || fmpz_cmp_si(b, -FMPR_PREC_EXACT) <= 0))
            {
                flint_printf("FAIL (small/zero)\n\n");
                abort();
            }
        }
        else if (c == FMPR_PREC_EXACT)
        {
            if (!(fmpr_is_inf(x) || fmpr_is_nan(x) ||
                fmpz_cmp_si(b, FMPR_PREC_EXACT) >= 0))
            {
                flint_printf("FAIL (large/inf/nan)\n\n");
                abort();
            }
        }
        else
        {
            if (fmpz_cmp_si(b, c) != 0)
            {
                flint_printf("FAIL (normal)\n\n");
                flint_printf("x = "); fmpr_print(x); flint_printf("\n\n");
                flint_printf("b = "); fmpz_print(b); flint_printf("\n\n");
                flint_printf("c = %wd\n\n", c);
                abort();
            }
        }

        fmpr_clear(x);
        fmpz_clear(b);
    }

    flint_randclear(state);
    flint_cleanup();
    flint_printf("PASS\n");
    return EXIT_SUCCESS;
}
Example #10
0
 bool is_pm_one(const ElementType& f) const {
   return fmpq_is_one(&f) or ((fmpz_cmp_si(fmpq_numref(&f), -1) == 0) and fmpz_is_one(fmpq_denref(&f)));
 }