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-04-15 16:49:17 +00:00
|
|
|
|
#include "context.h"
|
2014-04-14 20:28:35 +00:00
|
|
|
|
#include "err.h"
|
2014-04-16 21:23:41 +00:00
|
|
|
|
#include "poly.h"
|
2014-04-14 20:28:35 +00:00
|
|
|
|
|
2014-04-15 16:49:17 +00:00
|
|
|
|
#include <stdbool.h>
|
2014-04-14 20:28:35 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <tompoly.h>
|
|
|
|
|
#include <tommath.h>
|
2014-04-16 21:23:41 +00:00
|
|
|
|
#include <stdbool.h>
|
2014-04-14 20:28:35 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize a mp_int and check if this was successful, the
|
2014-04-15 14:56:38 +00:00
|
|
|
|
* caller must free new_int with mp_clear().
|
2014-04-14 20:28:35 +00:00
|
|
|
|
*
|
|
|
|
|
* @param new_int a pointer to the mp_int you want to initialize
|
|
|
|
|
*/
|
|
|
|
|
void init_integer(mp_int *new_int)
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
if ((result = mp_init(new_int)) != MP_OKAY) {
|
|
|
|
|
NTRU_ABORT("Error initializing the number. %s",
|
|
|
|
|
mp_error_to_string(result));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize a Polynom with a pb_poly and a mp_int as characteristic.
|
2014-04-15 14:56:38 +00:00
|
|
|
|
* Checks if everything went fine. The caller must free new_poly
|
|
|
|
|
* with pb_clear().
|
2014-04-14 20:28:35 +00:00
|
|
|
|
*
|
|
|
|
|
* @param new_poly the pb_poly you want to initialize
|
|
|
|
|
* @param chara the characteristic
|
|
|
|
|
*/
|
|
|
|
|
void init_polynom(pb_poly *new_poly, mp_int *chara)
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
if ((result = pb_init(new_poly, chara)) != MP_OKAY) {
|
|
|
|
|
NTRU_ABORT("Error initializing the number. %s",
|
|
|
|
|
mp_error_to_string(result));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-15 16:47:58 +00:00
|
|
|
|
* Initialize a Polynom with a pb_poly and an mp_int as characteristic
|
2014-04-15 12:16:30 +00:00
|
|
|
|
* with size. Checks if everything went fine. The caller must free
|
2014-04-15 14:56:38 +00:00
|
|
|
|
* new_poly with pb_clear().
|
2014-04-14 20:28:35 +00:00
|
|
|
|
*
|
|
|
|
|
* @param new_poly the pb_poly you want to initialize
|
|
|
|
|
* @param chara the characteristic
|
|
|
|
|
* @param size the size of the polynomial
|
|
|
|
|
*/
|
2014-04-15 16:20:08 +00:00
|
|
|
|
void init_polynom_size(pb_poly *new_poly, mp_int *chara, size_t size)
|
2014-04-14 20:28:35 +00:00
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
if ((result = pb_init_size(new_poly, chara, size)) != MP_OKAY) {
|
|
|
|
|
NTRU_ABORT("Error initializing the number. %s",
|
|
|
|
|
mp_error_to_string(result));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-15 16:49:17 +00:00
|
|
|
|
/**
|
|
|
|
|
* Initializes and builds a polynomial with the
|
|
|
|
|
* coefficient values of c[] of size len within NTRU
|
|
|
|
|
* context ctx and returns a newly allocated polynomial
|
2014-04-15 20:50:11 +00:00
|
|
|
|
* pointer which is not clamped.
|
|
|
|
|
*
|
|
|
|
|
* If you want to fill a polyonmial of length 11 with zeros,
|
|
|
|
|
* call build_polynom(NULL, 11, ctx).
|
2014-04-15 16:49:17 +00:00
|
|
|
|
*
|
|
|
|
|
* @param c array of polynomial coefficients, can be NULL
|
|
|
|
|
* @param len size of the coefficient array, can be 0
|
|
|
|
|
* @param ctx NTRU context
|
|
|
|
|
* @return newly allocated polynomial pointer, must be freed
|
|
|
|
|
* with delete_polynom()
|
|
|
|
|
*/
|
|
|
|
|
pb_poly *build_polynom(int const * const c,
|
|
|
|
|
const size_t len,
|
|
|
|
|
ntru_context *ctx)
|
|
|
|
|
{
|
|
|
|
|
pb_poly *new_poly;
|
|
|
|
|
mp_int chara;
|
|
|
|
|
|
|
|
|
|
new_poly = malloc(sizeof(*new_poly));
|
|
|
|
|
init_integer(&chara);
|
|
|
|
|
init_polynom_size(new_poly, &chara, len);
|
|
|
|
|
mp_clear(&chara);
|
|
|
|
|
|
|
|
|
|
/* fill the polynom if c is not NULL */
|
|
|
|
|
if (c) {
|
|
|
|
|
for (unsigned int i = 0; i < len; i++) {
|
|
|
|
|
bool sign = false;
|
|
|
|
|
unsigned long unsigned_c;
|
|
|
|
|
|
|
|
|
|
if (c[i] < 0) {
|
|
|
|
|
unsigned_c = 0 - c[i];
|
|
|
|
|
sign = true;
|
|
|
|
|
} else {
|
|
|
|
|
unsigned_c = c[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mp_set_int(&(new_poly->terms[i]), unsigned_c);
|
|
|
|
|
|
|
|
|
|
if (sign == true)
|
2014-04-17 15:33:05 +00:00
|
|
|
|
mp_neg(&(new_poly->terms[i]), &(new_poly->terms[i]));
|
2014-04-15 16:49:17 +00:00
|
|
|
|
}
|
2014-04-15 20:50:11 +00:00
|
|
|
|
} else { /* fill with zeros */
|
|
|
|
|
for (unsigned int i = 0; i < len; i++)
|
|
|
|
|
mp_set(&(new_poly->terms[i]), 0);
|
2014-04-15 16:49:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-15 20:50:11 +00:00
|
|
|
|
new_poly->used = len;
|
|
|
|
|
|
2014-04-15 16:49:17 +00:00
|
|
|
|
return new_poly;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-17 15:34:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Sets all the polynomial coefficients to +0.
|
|
|
|
|
*
|
|
|
|
|
* @param poly the polynomial
|
|
|
|
|
* @param len the length of the polynomial
|
|
|
|
|
*/
|
|
|
|
|
void erase_polynom(pb_poly *poly, size_t len)
|
|
|
|
|
{
|
|
|
|
|
for (unsigned int i = 0; i < len ; i++) {
|
|
|
|
|
mp_set(&(poly->terms[i]), 0);
|
|
|
|
|
mp_abs(&(poly->terms[i]), &(poly->terms[i]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-15 12:22:46 +00:00
|
|
|
|
/**
|
|
|
|
|
* This deletes the internal structure of a polynomial,
|
|
|
|
|
* and frees the pointer. Don't call this on stack variables,
|
|
|
|
|
* this is intended for use after ntru_ functions, that
|
|
|
|
|
* return a polynomial pointer.
|
|
|
|
|
*
|
|
|
|
|
* @param poly the polynomial to delete
|
|
|
|
|
*/
|
|
|
|
|
void delete_polynom(pb_poly *poly)
|
|
|
|
|
{
|
|
|
|
|
pb_clear(poly);
|
|
|
|
|
free(poly);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-15 20:50:42 +00:00
|
|
|
|
/**
|
|
|
|
|
* Starmultiplication, as follows:
|
2014-04-17 15:35:20 +00:00
|
|
|
|
* c = a * b mod (x^N − 1)
|
2014-04-15 20:50:42 +00:00
|
|
|
|
*
|
2014-04-17 15:35:20 +00:00
|
|
|
|
* @param a polynom to multiply (can be the same as c)
|
2014-04-15 20:50:42 +00:00
|
|
|
|
* @param b polynom to multiply
|
|
|
|
|
* @param c polynom [out]
|
|
|
|
|
* @param ctx NTRU context
|
|
|
|
|
* @param modulus whether we use p or q
|
|
|
|
|
*/
|
|
|
|
|
void pb_starmultiply(pb_poly *a,
|
|
|
|
|
pb_poly *b,
|
|
|
|
|
pb_poly *c,
|
|
|
|
|
ntru_context *ctx,
|
|
|
|
|
unsigned int modulus)
|
|
|
|
|
{
|
|
|
|
|
for (int k = ctx->N - 1; k >= 0; k--) {
|
|
|
|
|
int j;
|
|
|
|
|
j = k + 1;
|
|
|
|
|
|
|
|
|
|
for (int i = ctx->N - 1; i >= 0; i--) {
|
|
|
|
|
if (j == (int)(ctx->N))
|
|
|
|
|
j = 0;
|
2014-04-16 21:18:38 +00:00
|
|
|
|
if (mp_cmp_d(&(a->terms[i]), 0) != MP_EQ &&
|
|
|
|
|
mp_cmp_d(&(b->terms[j]), 0) != MP_EQ) {
|
2014-04-15 20:50:42 +00:00
|
|
|
|
int result;
|
|
|
|
|
mp_int mp_modulus;
|
|
|
|
|
mp_int mp_tmp;
|
|
|
|
|
|
|
|
|
|
init_integer(&mp_tmp);
|
|
|
|
|
init_integer(&mp_modulus);
|
|
|
|
|
mp_set_int(&mp_modulus, (unsigned long)(modulus));
|
|
|
|
|
|
2014-04-17 00:09:49 +00:00
|
|
|
|
MP_MUL(&(a->terms[i]), &(b->terms[j]), &mp_tmp);
|
|
|
|
|
MP_ADD(&(c->terms[k]), &mp_tmp, &(c->terms[k]));
|
|
|
|
|
MP_MOD(&(c->terms[k]), &mp_modulus, &(c->terms[k]));
|
2014-04-15 20:50:42 +00:00
|
|
|
|
|
|
|
|
|
mp_clear(&mp_modulus);
|
|
|
|
|
mp_clear(&mp_tmp);
|
|
|
|
|
}
|
|
|
|
|
j++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-14 20:28:35 +00:00
|
|
|
|
/**
|
2014-04-16 21:23:41 +00:00
|
|
|
|
* c = a XOR b
|
|
|
|
|
*
|
|
|
|
|
* @param a polynom (is allowed to be the same as param c)
|
|
|
|
|
* @param b polynom
|
|
|
|
|
* @param c polynom [out]
|
|
|
|
|
* @param len max size of the polynoms, make sure all are
|
|
|
|
|
* properly allocated
|
|
|
|
|
*/
|
|
|
|
|
void pb_xor(pb_poly *a,
|
|
|
|
|
pb_poly *b,
|
|
|
|
|
pb_poly *c,
|
|
|
|
|
const size_t len)
|
|
|
|
|
{
|
|
|
|
|
for (unsigned int i = 0; i < len; i++)
|
2014-04-17 00:09:49 +00:00
|
|
|
|
MP_XOR(&(a->terms[i]), &(b->terms[i]), &(c->terms[i]));
|
2014-04-16 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Invert the polynomial a modulo q.
|
|
|
|
|
*
|
|
|
|
|
* @param a polynomial to invert (is allowed to be the same as param Fq)
|
|
|
|
|
* @param Fq polynomial [out]
|
|
|
|
|
* @param ctx NTRU context
|
|
|
|
|
* @return true/false for success/failure
|
|
|
|
|
*/
|
|
|
|
|
bool pb_inverse_poly_q(pb_poly * const a,
|
|
|
|
|
pb_poly *Fq,
|
|
|
|
|
ntru_context *ctx)
|
|
|
|
|
{
|
|
|
|
|
int k = 0,
|
|
|
|
|
j = 0,
|
|
|
|
|
v = 2;
|
|
|
|
|
int zero_poly_val = 1;
|
|
|
|
|
pb_poly *a_tmp, *b, *c, *f, *g, *degree_zero_poly;
|
|
|
|
|
|
|
|
|
|
degree_zero_poly = build_polynom(&zero_poly_val, 1, ctx);
|
|
|
|
|
b = build_polynom(NULL, ctx->N, ctx);
|
|
|
|
|
mp_set(&(b->terms[0]), 1);
|
|
|
|
|
c = build_polynom(NULL, ctx->N, ctx);
|
|
|
|
|
f = build_polynom(NULL, ctx->N, ctx);
|
2014-04-17 00:09:49 +00:00
|
|
|
|
PB_COPY(a, f);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
a_tmp = build_polynom(NULL, ctx->N, ctx);
|
2014-04-17 00:09:49 +00:00
|
|
|
|
PB_COPY(a, a_tmp);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
g = build_polynom(NULL, ctx->N, ctx);
|
|
|
|
|
mp_set(&(g->terms[0]), 1);
|
2014-04-17 15:33:05 +00:00
|
|
|
|
mp_neg(&(g->terms[0]), &(g->terms[0]));
|
2014-04-16 21:23:41 +00:00
|
|
|
|
mp_set(&(g->terms[ctx->N]), 1);
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
while (mp_cmp_d(&(f->terms[0]), 0) == MP_EQ) {
|
|
|
|
|
for (unsigned int i = 1; i <= ctx->N; i++) {
|
2014-04-17 00:09:49 +00:00
|
|
|
|
MP_COPY(&(f->terms[i]), &(f->terms[i - 1]));
|
|
|
|
|
MP_COPY(&(c->terms[ctx->N - i]), &(c->terms[ctx->N + 1 - i]));
|
2014-04-16 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
mp_set(&(f->terms[ctx->N]), 0);
|
|
|
|
|
mp_set(&(c->terms[0]), 0);
|
|
|
|
|
k++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* hope this does not blow up in our face */
|
|
|
|
|
pb_clamp(degree_zero_poly);
|
|
|
|
|
pb_clamp(f);
|
|
|
|
|
if (pb_cmp(f, degree_zero_poly) == PB_DEG_EQ)
|
|
|
|
|
goto OUT_OF_LOOP;
|
|
|
|
|
|
|
|
|
|
pb_clamp(g);
|
|
|
|
|
if (pb_cmp(f, g) == PB_DEG_LT) {
|
|
|
|
|
pb_exch(f, g);
|
|
|
|
|
pb_exch(b, c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* draw_polynom(f); */
|
|
|
|
|
/* draw_polynom(b); */
|
|
|
|
|
pb_xor(f, g, f, ctx->N);
|
|
|
|
|
pb_xor(b, c, b, ctx->N);
|
|
|
|
|
/* draw_polynom(f); */
|
|
|
|
|
/* draw_polynom(b); */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OUT_OF_LOOP:
|
|
|
|
|
k = k % ctx->N;
|
|
|
|
|
|
|
|
|
|
for (int i = ctx->N - 1; i >= 0; i--) {
|
|
|
|
|
j = i - k;
|
|
|
|
|
if (j < 0)
|
|
|
|
|
j = j + ctx->N;
|
2014-04-17 00:09:49 +00:00
|
|
|
|
MP_COPY(&(b->terms[i]), &(Fq->terms[j]));
|
2014-04-16 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
draw_polynom(Fq);
|
|
|
|
|
|
|
|
|
|
while (v < (int)(ctx->q)) {
|
|
|
|
|
pb_poly *pb_tmp,
|
|
|
|
|
*pb_tmp2;
|
2014-04-17 00:09:49 +00:00
|
|
|
|
mp_int tmp_v;
|
2014-04-16 21:23:41 +00:00
|
|
|
|
pb_tmp = build_polynom(NULL, ctx->N, ctx);
|
|
|
|
|
v = v * 2;
|
2014-04-17 00:09:49 +00:00
|
|
|
|
init_integer(&tmp_v);
|
|
|
|
|
mp_set_int(&tmp_v, v);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
pb_tmp2 = build_polynom(NULL, ctx->N, ctx);
|
|
|
|
|
mp_set_int(&(pb_tmp2->terms[0]), 2);
|
|
|
|
|
|
|
|
|
|
/* hope this does not blow up in our face */
|
|
|
|
|
pb_starmultiply(a_tmp, Fq, pb_tmp, ctx, v);
|
2014-04-17 00:09:49 +00:00
|
|
|
|
PB_SUB(pb_tmp2, pb_tmp, pb_tmp);
|
|
|
|
|
PB_MOD(pb_tmp, &tmp_v, pb_tmp, ctx);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
pb_starmultiply(Fq, pb_tmp, Fq, ctx, v);
|
|
|
|
|
|
2014-04-17 00:09:49 +00:00
|
|
|
|
mp_clear(&tmp_v);
|
2014-04-16 21:23:41 +00:00
|
|
|
|
delete_polynom(pb_tmp);
|
|
|
|
|
delete_polynom(pb_tmp2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = ctx->N - 1; i >= 0; i--)
|
|
|
|
|
if (mp_cmp_d(&(Fq->terms[i]), 0) == MP_LT) {
|
|
|
|
|
mp_int mp_tmp;
|
|
|
|
|
init_integer(&mp_tmp);
|
|
|
|
|
mp_set_int(&mp_tmp, ctx->q);
|
2014-04-17 00:09:49 +00:00
|
|
|
|
MP_ADD(&(Fq->terms[i]), &mp_tmp, &(Fq->terms[i]));
|
2014-04-16 21:23:41 +00:00
|
|
|
|
mp_clear(&mp_tmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete_polynom(a_tmp);
|
|
|
|
|
delete_polynom(b);
|
|
|
|
|
delete_polynom(c);
|
|
|
|
|
delete_polynom(f);
|
|
|
|
|
delete_polynom(g);
|
|
|
|
|
delete_polynom(degree_zero_poly);
|
|
|
|
|
|
|
|
|
|
/* TODO: check if the f * Fq = 1 (mod p) condition holds true */
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-04-14 20:28:35 +00:00
|
|
|
|
* Print the polynomial in a human readable format to stdout.
|
|
|
|
|
*
|
|
|
|
|
* @param poly to draw
|
|
|
|
|
*/
|
|
|
|
|
void draw_polynom(pb_poly * const poly)
|
|
|
|
|
{
|
|
|
|
|
int x;
|
|
|
|
|
char buf[8192];
|
|
|
|
|
|
|
|
|
|
if (poly->used == 0) {
|
|
|
|
|
printf("0");
|
|
|
|
|
} else {
|
|
|
|
|
for (x = poly->used - 1; x >= 0; x--) {
|
|
|
|
|
if (mp_iszero(&(poly->terms[x])) == MP_YES)
|
|
|
|
|
continue;
|
|
|
|
|
mp_toradix(&(poly->terms[x]), buf, 10);
|
|
|
|
|
if ((x != poly->used - 1) && poly->terms[x].sign == MP_ZPOS) {
|
|
|
|
|
printf("+");
|
|
|
|
|
}
|
|
|
|
|
printf(" %sx^%d ", buf, x);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (mp_iszero(&(poly->characteristic)) == MP_NO) {
|
|
|
|
|
mp_toradix(&(poly->characteristic), buf, 10);
|
|
|
|
|
printf(" (mod %s)", buf);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|