2014-04-14 20:28:35 +00:00
|
|
|
|
/*
|
2014-04-15 11:35:04 +00:00
|
|
|
|
* Copyright (C) 2014 FH Bielefeld
|
2014-04-14 20:28:35 +00:00
|
|
|
|
*
|
2014-04-15 11:35:04 +00:00
|
|
|
|
* This file is part of a FH Bielefeld project.
|
2014-04-14 20:28:35 +00:00
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
|
*/
|
|
|
|
|
|
2014-05-25 00:15:24 +00:00
|
|
|
|
/**
|
2014-06-05 13:49:40 +00:00
|
|
|
|
* @file ntru_poly.c
|
2014-05-25 00:15:24 +00:00
|
|
|
|
* This files purpose is to handle polynomials
|
|
|
|
|
* in general, allowing modification, arithmetic
|
|
|
|
|
* and common algorithms like inverting them.
|
|
|
|
|
* @brief operations on polynomials
|
|
|
|
|
*/
|
|
|
|
|
|
2014-06-05 13:49:40 +00:00
|
|
|
|
#include "ntru_err.h"
|
|
|
|
|
#include "ntru_mem.h"
|
|
|
|
|
#include "ntru_params.h"
|
|
|
|
|
#include "ntru_poly.h"
|
2014-04-14 20:28:35 +00:00
|
|
|
|
|
2014-04-20 17:55:30 +00:00
|
|
|
|
#include <stdarg.h>
|
2014-04-15 16:49:17 +00:00
|
|
|
|
#include <stdbool.h>
|
2014-05-25 20:47:54 +00:00
|
|
|
|
#include <stdint.h>
|
2014-04-14 20:28:35 +00:00
|
|
|
|
#include <stdio.h>
|
2014-05-25 17:00:36 +00:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/types.h>
|
2014-04-14 20:28:35 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
#include <fmpz_poly.h>
|
|
|
|
|
#include <fmpz.h>
|
|
|
|
|
|
2014-04-17 21:43:29 +00:00
|
|
|
|
|
2014-04-14 20:28:35 +00:00
|
|
|
|
/**
|
2014-05-24 21:11:02 +00:00
|
|
|
|
* Find the inverse polynomial modulo a power of 2,
|
|
|
|
|
* which is q.
|
2014-04-14 20:28:35 +00:00
|
|
|
|
*
|
2014-05-24 21:11:02 +00:00
|
|
|
|
* @param a polynomial to invert
|
|
|
|
|
* @param Fq polynomial [out]
|
2014-06-05 13:33:57 +00:00
|
|
|
|
* @param params NTRU context
|
2014-04-14 20:28:35 +00:00
|
|
|
|
*/
|
2014-05-25 21:04:22 +00:00
|
|
|
|
static
|
2014-06-01 18:41:29 +00:00
|
|
|
|
void poly_mod2_to_modq(const fmpz_poly_t a,
|
2014-05-25 21:04:22 +00:00
|
|
|
|
fmpz_poly_t Fq,
|
2014-06-05 13:33:57 +00:00
|
|
|
|
const ntru_params *params);
|
2014-05-25 21:04:22 +00:00
|
|
|
|
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
static void
|
2014-06-01 18:41:29 +00:00
|
|
|
|
poly_mod2_to_modq(const fmpz_poly_t a,
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_t Fq,
|
2014-06-05 13:33:57 +00:00
|
|
|
|
const ntru_params *params)
|
2014-04-14 20:28:35 +00:00
|
|
|
|
{
|
2014-05-24 21:11:02 +00:00
|
|
|
|
int v = 2;
|
|
|
|
|
fmpz_poly_t poly_tmp, two;
|
2014-04-14 20:28:35 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_init(poly_tmp);
|
|
|
|
|
fmpz_poly_zero(poly_tmp);
|
|
|
|
|
fmpz_poly_init(two);
|
|
|
|
|
fmpz_poly_set_coeff_ui(two, 0, 2);
|
2014-04-30 15:18:08 +00:00
|
|
|
|
|
2014-06-05 13:33:57 +00:00
|
|
|
|
while (v < (int)(params->q)) {
|
2014-05-24 21:11:02 +00:00
|
|
|
|
v = v * 2;
|
2014-04-30 15:18:08 +00:00
|
|
|
|
|
2014-06-05 13:33:57 +00:00
|
|
|
|
poly_starmultiply(a, Fq, poly_tmp, params, v);
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_sub(poly_tmp, two, poly_tmp);
|
|
|
|
|
fmpz_poly_mod_unsigned(poly_tmp, v);
|
2014-06-05 13:33:57 +00:00
|
|
|
|
poly_starmultiply(Fq, poly_tmp, Fq, params, v);
|
2014-04-14 20:28:35 +00:00
|
|
|
|
|
|
|
|
|
}
|
2014-05-24 21:11:02 +00:00
|
|
|
|
|
|
|
|
|
fmpz_poly_clear(poly_tmp);
|
|
|
|
|
fmpz_poly_clear(two);
|
|
|
|
|
|
2014-04-14 20:28:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-06-02 20:02:53 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
void
|
|
|
|
|
poly_new(fmpz_poly_t new_poly,
|
2014-05-25 02:16:48 +00:00
|
|
|
|
int const * const c,
|
2014-04-30 15:22:25 +00:00
|
|
|
|
const size_t len)
|
2014-04-15 16:49:17 +00:00
|
|
|
|
{
|
2014-05-25 02:16:48 +00:00
|
|
|
|
fmpz_poly_init(new_poly);
|
2014-04-15 20:50:11 +00:00
|
|
|
|
|
2014-05-25 20:47:54 +00:00
|
|
|
|
for (uint32_t i = 0; i < len; i++)
|
2014-05-25 02:16:48 +00:00
|
|
|
|
fmpz_poly_set_coeff_si(new_poly, i, c[i]);
|
2014-04-17 15:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
void
|
|
|
|
|
poly_delete(fmpz_poly_t poly)
|
2014-04-15 12:22:46 +00:00
|
|
|
|
{
|
2014-05-25 02:16:48 +00:00
|
|
|
|
fmpz_poly_clear(poly);
|
2014-04-15 12:22:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
void
|
|
|
|
|
poly_delete_array(fmpz_poly_t **poly_array)
|
2014-05-25 17:22:17 +00:00
|
|
|
|
{
|
2014-05-25 20:47:54 +00:00
|
|
|
|
uint32_t i = 0;
|
2014-05-25 17:22:17 +00:00
|
|
|
|
|
|
|
|
|
while(poly_array[i]) {
|
|
|
|
|
poly_delete(*(poly_array[i]));
|
2014-05-30 17:54:50 +00:00
|
|
|
|
free(poly_array[i]);
|
2014-05-25 17:22:17 +00:00
|
|
|
|
i++;
|
|
|
|
|
}
|
2014-05-30 17:54:50 +00:00
|
|
|
|
|
|
|
|
|
/* avoid double free */
|
|
|
|
|
if (i > 1)
|
|
|
|
|
free(poly_array);
|
2014-05-25 17:22:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
void
|
|
|
|
|
poly_delete_all(fmpz_poly_t poly, ...)
|
2014-04-20 17:55:30 +00:00
|
|
|
|
{
|
2014-05-25 02:16:48 +00:00
|
|
|
|
fmpz_poly_struct *next_poly;
|
2014-04-20 17:55:30 +00:00
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
|
|
next_poly = poly;
|
|
|
|
|
va_start(args, poly);
|
|
|
|
|
while (next_poly != NULL) {
|
2014-05-24 21:11:02 +00:00
|
|
|
|
poly_delete(next_poly);
|
2014-05-25 02:16:48 +00:00
|
|
|
|
next_poly = va_arg(args, fmpz_poly_struct*);
|
2014-04-20 17:55:30 +00:00
|
|
|
|
}
|
|
|
|
|
va_end(args);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
void
|
|
|
|
|
fmpz_poly_mod_unsigned(fmpz_poly_t a,
|
2014-06-01 18:41:29 +00:00
|
|
|
|
const uint32_t mod)
|
2014-04-15 20:50:42 +00:00
|
|
|
|
{
|
2014-05-24 21:11:02 +00:00
|
|
|
|
nmod_poly_t nmod_tmp;
|
2014-04-15 20:50:42 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
nmod_poly_init(nmod_tmp, mod);
|
2014-04-15 20:50:42 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_get_nmod_poly(nmod_tmp, a);
|
|
|
|
|
fmpz_poly_set_nmod_poly_unsigned(a, nmod_tmp);
|
2014-04-15 20:50:42 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
nmod_poly_clear(nmod_tmp);
|
2014-04-15 20:50:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
void
|
|
|
|
|
fmpz_poly_mod(fmpz_poly_t a,
|
2014-06-01 18:41:29 +00:00
|
|
|
|
const uint32_t mod)
|
2014-04-30 15:19:58 +00:00
|
|
|
|
{
|
2014-05-24 21:11:02 +00:00
|
|
|
|
nmod_poly_t nmod_tmp;
|
2014-04-30 15:19:58 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
nmod_poly_init(nmod_tmp, mod);
|
2014-04-30 15:19:58 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_get_nmod_poly(nmod_tmp, a);
|
|
|
|
|
fmpz_poly_set_nmod_poly(a, nmod_tmp);
|
2014-04-30 15:19:58 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
nmod_poly_clear(nmod_tmp);
|
2014-04-30 15:19:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
void
|
|
|
|
|
fmpz_poly_set_coeff_fmpz_n(fmpz_poly_t poly, slong n,
|
2014-05-24 21:11:02 +00:00
|
|
|
|
const fmpz_t x)
|
2014-04-16 21:23:41 +00:00
|
|
|
|
{
|
2014-05-24 21:11:02 +00:00
|
|
|
|
if (x)
|
|
|
|
|
fmpz_poly_set_coeff_fmpz(poly, n, x);
|
|
|
|
|
else
|
|
|
|
|
fmpz_poly_set_coeff_si(poly, n, 0);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
int
|
2014-06-01 18:41:29 +00:00
|
|
|
|
fmpz_invmod_ui(fmpz_t f, const fmpz_t g, const uint32_t mod)
|
2014-04-17 21:43:29 +00:00
|
|
|
|
{
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_t modulus;
|
2014-04-17 21:43:29 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_init_set_ui(modulus, mod);
|
2014-04-17 21:43:29 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
return fmpz_invmod(f, g, modulus);
|
2014-04-17 21:43:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
void
|
|
|
|
|
fmpz_add_n(fmpz_t f, const fmpz_t g, const fmpz_t h)
|
2014-05-24 21:11:02 +00:00
|
|
|
|
{
|
|
|
|
|
if (!g && !h) {
|
|
|
|
|
fmpz_zero(f);
|
|
|
|
|
} else {
|
|
|
|
|
if (!g && h)
|
|
|
|
|
fmpz_add_ui(f, h, 0);
|
|
|
|
|
else if (g && !h)
|
|
|
|
|
fmpz_add_ui(f, g, 0);
|
|
|
|
|
else
|
|
|
|
|
fmpz_add(f, g, h);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
void
|
2014-06-01 18:41:29 +00:00
|
|
|
|
poly_starmultiply(const fmpz_poly_t a,
|
|
|
|
|
const fmpz_poly_t b,
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_t c,
|
2014-06-05 13:33:57 +00:00
|
|
|
|
const ntru_params *params,
|
2014-05-25 20:47:54 +00:00
|
|
|
|
uint32_t modulus)
|
2014-04-20 17:57:45 +00:00
|
|
|
|
{
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_t a_tmp;
|
|
|
|
|
fmpz_t c_coeff_k;
|
2014-04-20 17:57:45 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_init(a_tmp);
|
|
|
|
|
fmpz_init(c_coeff_k);
|
|
|
|
|
|
|
|
|
|
/* avoid side effects */
|
|
|
|
|
fmpz_poly_set(a_tmp, a);
|
|
|
|
|
fmpz_poly_zero(c);
|
|
|
|
|
|
2014-06-05 13:33:57 +00:00
|
|
|
|
for (int k = params->N - 1; k >= 0; k--) {
|
2014-05-24 21:11:02 +00:00
|
|
|
|
int j;
|
|
|
|
|
|
|
|
|
|
j = k + 1;
|
|
|
|
|
|
|
|
|
|
fmpz_set_si(c_coeff_k, 0);
|
|
|
|
|
|
2014-06-05 13:33:57 +00:00
|
|
|
|
for (int i = params->N - 1; i >= 0; i--) {
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz *a_tmp_coeff_i,
|
|
|
|
|
*b_coeff_j;
|
|
|
|
|
|
2014-06-05 13:33:57 +00:00
|
|
|
|
if (j == (int)(params->N))
|
2014-05-24 21:11:02 +00:00
|
|
|
|
j = 0;
|
|
|
|
|
|
|
|
|
|
a_tmp_coeff_i = fmpz_poly_get_coeff_ptr(a_tmp, i);
|
|
|
|
|
b_coeff_j = fmpz_poly_get_coeff_ptr(b, j);
|
|
|
|
|
|
2014-06-02 20:02:53 +00:00
|
|
|
|
if (fmpz_cmp_si_n(a_tmp_coeff_i, 0) &&
|
|
|
|
|
fmpz_cmp_si_n(b_coeff_j, 0)) {
|
2014-05-24 21:11:02 +00:00
|
|
|
|
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);
|
2014-04-20 17:57:45 +00:00
|
|
|
|
}
|
2014-05-24 21:11:02 +00:00
|
|
|
|
|
|
|
|
|
fmpz_poly_clear(a_tmp);
|
2014-04-20 17:57:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
bool
|
2014-06-01 18:41:29 +00:00
|
|
|
|
poly_inverse_poly_q(const fmpz_poly_t a,
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_t Fq,
|
2014-06-05 13:33:57 +00:00
|
|
|
|
const ntru_params *params)
|
2014-04-16 21:23:41 +00:00
|
|
|
|
{
|
2014-05-28 18:57:55 +00:00
|
|
|
|
bool retval = false;
|
2014-04-16 21:23:41 +00:00
|
|
|
|
int k = 0,
|
2014-04-20 17:57:45 +00:00
|
|
|
|
j = 0;
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz *b_last;
|
|
|
|
|
fmpz_poly_t a_tmp,
|
|
|
|
|
b,
|
|
|
|
|
c,
|
|
|
|
|
f,
|
|
|
|
|
g;
|
2014-04-16 21:23:41 +00:00
|
|
|
|
|
2014-04-29 12:27:15 +00:00
|
|
|
|
/* general initialization of temp variables */
|
2014-05-24 21:11:02 +00:00
|
|
|
|
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);
|
2014-04-29 12:27:15 +00:00
|
|
|
|
|
|
|
|
|
/* set g(x) = x^N − 1 */
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_init(g);
|
|
|
|
|
fmpz_poly_set_coeff_si(g, 0, -1);
|
2014-06-05 13:33:57 +00:00
|
|
|
|
fmpz_poly_set_coeff_si(g, params->N, 1);
|
2014-04-29 12:27:15 +00:00
|
|
|
|
|
2014-04-17 15:37:30 +00:00
|
|
|
|
/* avoid side effects */
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_init(a_tmp);
|
|
|
|
|
fmpz_poly_set(a_tmp, a);
|
|
|
|
|
fmpz_poly_zero(Fq);
|
2014-04-17 15:37:30 +00:00
|
|
|
|
|
2014-04-16 21:23:41 +00:00
|
|
|
|
while (1) {
|
2014-05-24 21:11:02 +00:00
|
|
|
|
while (fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) {
|
2014-06-05 13:33:57 +00:00
|
|
|
|
for (uint32_t i = 1; i <= params->N; i++) {
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz *f_coeff = fmpz_poly_get_coeff_ptr(f, i);
|
2014-06-05 13:33:57 +00:00
|
|
|
|
fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, params->N - i);
|
2014-05-24 21:11:02 +00:00
|
|
|
|
|
2014-04-29 12:27:15 +00:00
|
|
|
|
/* f(x) = f(x) / x */
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_set_coeff_fmpz_n(f, i - 1,
|
|
|
|
|
f_coeff);
|
|
|
|
|
|
2014-04-29 12:27:15 +00:00
|
|
|
|
/* c(x) = c(x) * x */
|
2014-06-05 13:33:57 +00:00
|
|
|
|
fmpz_poly_set_coeff_fmpz_n(c, params->N + 1 - i,
|
2014-05-24 21:11:02 +00:00
|
|
|
|
c_coeff);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
}
|
2014-05-24 21:11:02 +00:00
|
|
|
|
|
2014-06-05 13:33:57 +00:00
|
|
|
|
fmpz_poly_set_coeff_si(f, params->N, 0);
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_set_coeff_si(c, 0, 0);
|
|
|
|
|
|
2014-04-16 21:23:41 +00:00
|
|
|
|
k++;
|
2014-05-24 21:11:02 +00:00
|
|
|
|
|
2014-05-28 18:57:55 +00:00
|
|
|
|
if (fmpz_poly_degree(f) == -1)
|
|
|
|
|
goto _cleanup;
|
2014-04-16 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
if (fmpz_poly_degree(f) == 0)
|
2014-04-30 15:22:57 +00:00
|
|
|
|
break;
|
2014-04-16 21:23:41 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
if (fmpz_poly_degree(f) < fmpz_poly_degree(g)) {
|
|
|
|
|
fmpz_poly_swap(f, g);
|
|
|
|
|
fmpz_poly_swap(b, c);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_add(f, g, f);
|
|
|
|
|
fmpz_poly_mod_unsigned(f, 2);
|
|
|
|
|
|
|
|
|
|
fmpz_poly_add(b, c, b);
|
|
|
|
|
fmpz_poly_mod_unsigned(b, 2);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-05 13:33:57 +00:00
|
|
|
|
k = k % params->N;
|
2014-04-16 21:23:41 +00:00
|
|
|
|
|
2014-06-05 13:33:57 +00:00
|
|
|
|
b_last = fmpz_poly_get_coeff_ptr(b, params->N);
|
2014-06-02 20:02:53 +00:00
|
|
|
|
if (fmpz_cmp_si_n(b_last, 0))
|
2014-05-28 18:57:55 +00:00
|
|
|
|
goto _cleanup;
|
2014-05-12 22:20:46 +00:00
|
|
|
|
|
2014-04-29 12:27:15 +00:00
|
|
|
|
/* Fq(x) = x^(N-k) * b(x) */
|
2014-06-05 13:33:57 +00:00
|
|
|
|
for (int i = params->N - 1; i >= 0; i--) {
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz *b_i;
|
|
|
|
|
|
2014-04-16 21:23:41 +00:00
|
|
|
|
j = i - k;
|
2014-05-24 21:11:02 +00:00
|
|
|
|
|
2014-04-16 21:23:41 +00:00
|
|
|
|
if (j < 0)
|
2014-06-05 13:33:57 +00:00
|
|
|
|
j = j + params->N;
|
2014-05-24 21:11:02 +00:00
|
|
|
|
|
|
|
|
|
b_i = fmpz_poly_get_coeff_ptr(b, i);
|
|
|
|
|
fmpz_poly_set_coeff_fmpz_n(Fq, j, b_i);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-05 13:33:57 +00:00
|
|
|
|
poly_mod2_to_modq(a_tmp, Fq, params);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
/* check if the f * Fq = 1 (mod p) condition holds true */
|
|
|
|
|
fmpz_poly_set(a_tmp, a);
|
2014-06-05 13:33:57 +00:00
|
|
|
|
poly_starmultiply(a_tmp, Fq, a_tmp, params, params->q);
|
2014-05-28 18:57:55 +00:00
|
|
|
|
if (fmpz_poly_is_one(a_tmp))
|
|
|
|
|
retval = true;
|
|
|
|
|
else
|
|
|
|
|
fmpz_poly_zero(Fq);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
|
2014-05-28 18:57:55 +00:00
|
|
|
|
_cleanup:
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_clear(a_tmp);
|
|
|
|
|
fmpz_poly_clear(b);
|
|
|
|
|
fmpz_poly_clear(c);
|
|
|
|
|
fmpz_poly_clear(f);
|
|
|
|
|
fmpz_poly_clear(g);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
return retval;
|
2014-04-16 21:23:41 +00:00
|
|
|
|
}
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
bool
|
2014-06-01 18:41:29 +00:00
|
|
|
|
poly_inverse_poly_p(const fmpz_poly_t a,
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_t Fp,
|
2014-06-05 13:33:57 +00:00
|
|
|
|
const ntru_params *params)
|
2014-04-29 12:27:52 +00:00
|
|
|
|
{
|
2014-05-28 21:11:13 +00:00
|
|
|
|
bool retval = false;
|
2014-04-29 12:27:52 +00:00
|
|
|
|
int k = 0,
|
|
|
|
|
j = 0;
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz *b_last;
|
|
|
|
|
fmpz_poly_t a_tmp,
|
|
|
|
|
b,
|
|
|
|
|
c,
|
|
|
|
|
f,
|
|
|
|
|
g;
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
|
|
|
|
/* general initialization of temp variables */
|
2014-05-24 21:11:02 +00:00
|
|
|
|
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);
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
|
|
|
|
/* set g(x) = x^N − 1 */
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_init(g);
|
|
|
|
|
fmpz_poly_set_coeff_si(g, 0, -1);
|
2014-06-05 13:33:57 +00:00
|
|
|
|
fmpz_poly_set_coeff_si(g, params->N, 1);
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
|
|
|
|
/* avoid side effects */
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_init(a_tmp);
|
|
|
|
|
fmpz_poly_set(a_tmp, a);
|
|
|
|
|
fmpz_poly_zero(Fp);
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
|
|
|
|
while (1) {
|
2014-05-24 21:11:02 +00:00
|
|
|
|
while (fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) {
|
2014-06-05 13:33:57 +00:00
|
|
|
|
for (uint32_t i = 1; i <= params->N; i++) {
|
2014-05-30 16:42:09 +00:00
|
|
|
|
fmpz *f_coeff = fmpz_poly_get_coeff_ptr(f, i);
|
2014-06-05 13:33:57 +00:00
|
|
|
|
fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, params->N - i);
|
2014-05-24 21:11:02 +00:00
|
|
|
|
|
2014-04-29 12:27:52 +00:00
|
|
|
|
/* f(x) = f(x) / x */
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_set_coeff_fmpz_n(f, i - 1,
|
2014-05-30 16:42:09 +00:00
|
|
|
|
f_coeff);
|
2014-05-24 21:11:02 +00:00
|
|
|
|
|
2014-04-29 12:27:52 +00:00
|
|
|
|
/* c(x) = c(x) * x */
|
2014-06-05 13:33:57 +00:00
|
|
|
|
fmpz_poly_set_coeff_fmpz_n(c, params->N + 1 - i,
|
2014-05-30 16:42:09 +00:00
|
|
|
|
c_coeff);
|
2014-04-29 12:27:52 +00:00
|
|
|
|
}
|
2014-05-24 21:11:02 +00:00
|
|
|
|
|
2014-06-05 13:33:57 +00:00
|
|
|
|
fmpz_poly_set_coeff_si(f, params->N, 0);
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_set_coeff_si(c, 0, 0);
|
|
|
|
|
|
2014-04-29 12:27:52 +00:00
|
|
|
|
k++;
|
2014-05-24 21:11:02 +00:00
|
|
|
|
|
2014-05-28 21:11:13 +00:00
|
|
|
|
if (fmpz_poly_degree(f) == -1)
|
2014-05-24 21:11:02 +00:00
|
|
|
|
goto cleanup;
|
2014-04-29 12:27:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
if (fmpz_poly_degree(f) == 0)
|
2014-04-30 15:22:57 +00:00
|
|
|
|
break;
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
if (fmpz_poly_degree(f) < fmpz_poly_degree(g)) {
|
2014-04-29 17:59:59 +00:00
|
|
|
|
/* exchange f and g and exchange b and c */
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_swap(f, g);
|
|
|
|
|
fmpz_poly_swap(b, c);
|
2014-04-29 12:27:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2014-05-24 21:11:02 +00:00
|
|
|
|
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),
|
2014-06-05 13:33:57 +00:00
|
|
|
|
params->p);
|
|
|
|
|
fmpz_mod_ui(mp_tmp, mp_tmp, params->p);
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_mul(u, mp_tmp, u);
|
2014-06-05 13:33:57 +00:00
|
|
|
|
fmpz_mod_ui(u, u, params->p);
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
|
|
|
|
/* f = f - u * g mod p */
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_scalar_mul_fmpz(g_tmp, g_tmp, u);
|
|
|
|
|
fmpz_poly_sub(f, g_tmp, f);
|
2014-06-05 13:33:57 +00:00
|
|
|
|
fmpz_poly_mod_unsigned(f, params->p);
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
|
|
|
|
/* b = b - u * c mod p */
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_scalar_mul_fmpz(c_tmp, c_tmp, u);
|
|
|
|
|
fmpz_poly_sub(b, c_tmp, b);
|
2014-06-05 13:33:57 +00:00
|
|
|
|
fmpz_poly_mod_unsigned(b, params->p);
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_clear(u);
|
|
|
|
|
fmpz_poly_clear(g_tmp);
|
|
|
|
|
fmpz_poly_clear(c_tmp);
|
2014-04-29 12:27:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-05 13:33:57 +00:00
|
|
|
|
k = k % params->N;
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
2014-06-05 13:33:57 +00:00
|
|
|
|
b_last = fmpz_poly_get_coeff_ptr(b, params->N);
|
2014-06-02 20:02:53 +00:00
|
|
|
|
if (fmpz_cmp_si_n(b_last, 0))
|
2014-05-24 21:11:02 +00:00
|
|
|
|
goto cleanup;
|
|
|
|
|
|
2014-04-29 12:27:52 +00:00
|
|
|
|
/* Fp(x) = x^(N-k) * b(x) */
|
2014-06-05 13:33:57 +00:00
|
|
|
|
for (int i = params->N - 1; i >= 0; i--) {
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz *b_i;
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
|
|
|
|
/* b(X) = f[0]^(-1) * b(X) (mod p) */
|
|
|
|
|
{
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_t mp_tmp;
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_init(mp_tmp);
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_invmod_ui(mp_tmp,
|
|
|
|
|
fmpz_poly_get_coeff_ptr(f, 0),
|
2014-06-05 13:33:57 +00:00
|
|
|
|
params->p);
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
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),
|
2014-06-05 13:33:57 +00:00
|
|
|
|
params->p);
|
2014-05-24 21:11:02 +00:00
|
|
|
|
}
|
2014-04-16 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-29 12:27:52 +00:00
|
|
|
|
j = i - k;
|
|
|
|
|
if (j < 0)
|
2014-06-05 13:33:57 +00:00
|
|
|
|
j = j + params->N;
|
2014-04-29 12:27:52 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
b_i = fmpz_poly_get_coeff_ptr(b, i);
|
|
|
|
|
fmpz_poly_set_coeff_fmpz_n(Fp, j, b_i);
|
2014-04-29 12:27:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 01:57:51 +00:00
|
|
|
|
/* check if the f * Fp = 1 (mod p) condition holds true */
|
2014-05-24 21:11:02 +00:00
|
|
|
|
fmpz_poly_set(a_tmp, a);
|
2014-06-05 13:33:57 +00:00
|
|
|
|
poly_starmultiply(a_tmp, Fp, a_tmp, params, params->p);
|
2014-05-28 21:11:13 +00:00
|
|
|
|
if (fmpz_poly_is_one(a_tmp))
|
|
|
|
|
retval = true;
|
|
|
|
|
else
|
|
|
|
|
fmpz_poly_zero(Fp);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
cleanup:
|
|
|
|
|
fmpz_poly_clear(a_tmp);
|
|
|
|
|
fmpz_poly_clear(b);
|
|
|
|
|
fmpz_poly_clear(c);
|
|
|
|
|
fmpz_poly_clear(f);
|
|
|
|
|
fmpz_poly_clear(g);
|
2014-04-20 14:45:05 +00:00
|
|
|
|
|
2014-05-24 21:11:02 +00:00
|
|
|
|
return retval;
|
2014-05-12 11:46:59 +00:00
|
|
|
|
}
|
2014-05-24 23:40:07 +00:00
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
void
|
2014-06-01 18:41:29 +00:00
|
|
|
|
poly_draw(const fmpz_poly_t poly)
|
2014-05-24 23:40:07 +00:00
|
|
|
|
{
|
|
|
|
|
fmpz_poly_print(poly);
|
|
|
|
|
flint_printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
|
void
|
2014-06-01 18:41:29 +00:00
|
|
|
|
poly_draw_pretty(const fmpz_poly_t poly)
|
2014-05-24 23:40:07 +00:00
|
|
|
|
{
|
|
|
|
|
fmpz_poly_print_pretty(poly, "x");
|
|
|
|
|
flint_printf("\n");
|
|
|
|
|
}
|
2014-05-27 23:17:19 +00:00
|
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*/
|