Esempio n. 1
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);
}
Esempio n. 2
0
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;
}
Esempio n. 3
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;
}
Esempio n. 4
0
int
main(void)
{
    int i, result;
    FLINT_TEST_INIT(state);

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

    

    for (i = 0; i < 1000 * flint_test_multiplier(); i++)
    {
        fmpz_poly_t A;
        fmpz_t a;
        slong n = n_randint(state, 100);

        fmpz_poly_init(A);
        fmpz_poly_randtest(A, state, n_randint(state, 100), 100);
        fmpz_init(a);

        fmpz_poly_get_coeff_fmpz(a, A, n);

        result = n < fmpz_poly_length(A) ? 
                     fmpz_equal(a, fmpz_poly_get_coeff_ptr(A, n)) : 
                     fmpz_poly_get_coeff_ptr(A, n) == NULL;
        if (!result)
        {
            flint_printf("FAIL:\n");
            flint_printf("A = "), fmpz_poly_print(A), flint_printf("\n\n");
            flint_printf("a = "), fmpz_print(a), flint_printf("\n\n");
            flint_printf("n = %wd\n\n", n);
            abort();
        }

        fmpz_poly_clear(A);
        fmpz_clear(a);
    }

    FLINT_TEST_CLEANUP(state);
    
    flint_printf("PASS\n");
    return 0;
}
Esempio n. 5
0
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;
}
Esempio n. 6
0
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;
}