Merge branch 'master' into 'master'
a few cleanups, memory management and more macros for readability
This commit is contained in:
commit
b2db30e540
@ -4,7 +4,7 @@ PKG_CONFIG ?= pkg-config
|
|||||||
|
|
||||||
# flags
|
# flags
|
||||||
CFLAGS ?= -march=native -O2 -pipe
|
CFLAGS ?= -march=native -O2 -pipe
|
||||||
CFLAGS += -std=c99 -pedantic -Wall -Wextra -Werror -Wno-unused-variable -Wno-unused-parameter
|
CFLAGS += -std=c99 -pedantic -Wall -Wextra -Werror -Wno-unused-variable -Wno-unused-parameter -Wno-unused-function
|
||||||
ifeq ($(shell $(CC) -v 2>&1 | grep 'gcc version' &>/dev/null && echo 1),1)
|
ifeq ($(shell $(CC) -v 2>&1 | grep 'gcc version' &>/dev/null && echo 1),1)
|
||||||
CFLAGS += -Wno-unused-but-set-variable
|
CFLAGS += -Wno-unused-but-set-variable
|
||||||
endif
|
endif
|
||||||
@ -37,8 +37,8 @@ endif
|
|||||||
LIBS += -L.
|
LIBS += -L.
|
||||||
|
|
||||||
# objects
|
# objects
|
||||||
PQC_OBJS = rand.o poly.o
|
PQC_OBJS = rand.o poly.o keypair.o mem.o
|
||||||
PQC_HEADERS = err.h rand.h poly.h context.h
|
PQC_HEADERS = err.h rand.h poly.h context.h keypair.h
|
||||||
# CUNIT_OBJS = cunit.o
|
# CUNIT_OBJS = cunit.o
|
||||||
|
|
||||||
# includes
|
# includes
|
||||||
|
48
src/mem.c
Normal file
48
src/mem.c
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2014 FH Bielefeld
|
||||||
|
*
|
||||||
|
* This file is part of a FH Bielefeld project.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "mem.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocate memory of size and return
|
||||||
|
* a void pointer.
|
||||||
|
*
|
||||||
|
* @param size of the memory to allocate in bytes
|
||||||
|
* @return void pointer to the beginning of the allocated memory block
|
||||||
|
*/
|
||||||
|
void *ntru_malloc(size_t size)
|
||||||
|
{
|
||||||
|
void *ptr;
|
||||||
|
|
||||||
|
ptr = malloc(size);
|
||||||
|
|
||||||
|
if (size)
|
||||||
|
if (!ptr) {
|
||||||
|
fprintf(stderr, "failed to allocate memory, aborting!");
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ptr;
|
||||||
|
}
|
29
src/mem.h
Normal file
29
src/mem.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2014 FH Bielefeld
|
||||||
|
*
|
||||||
|
* This file is part of a FH Bielefeld project.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef NTRU_MEM_H
|
||||||
|
#define NTRU_MEM_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void *ntru_malloc(size_t size);
|
||||||
|
|
||||||
|
#endif /* NTRU_MEM_H */
|
118
src/poly.c
118
src/poly.c
@ -21,8 +21,10 @@
|
|||||||
|
|
||||||
#include "context.h"
|
#include "context.h"
|
||||||
#include "err.h"
|
#include "err.h"
|
||||||
|
#include "mem.h"
|
||||||
#include "poly.h"
|
#include "poly.h"
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <tompoly.h>
|
#include <tompoly.h>
|
||||||
@ -34,6 +36,9 @@
|
|||||||
* static declarations
|
* static declarations
|
||||||
*/
|
*/
|
||||||
static unsigned int get_degree(pb_poly const * const poly);
|
static unsigned int get_degree(pb_poly const * const poly);
|
||||||
|
static void pb_mod2_to_modq(pb_poly * const a,
|
||||||
|
pb_poly *Fq,
|
||||||
|
ntru_context *ctx);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -108,7 +113,7 @@ pb_poly *build_polynom(int const * const c,
|
|||||||
pb_poly *new_poly;
|
pb_poly *new_poly;
|
||||||
mp_int chara;
|
mp_int chara;
|
||||||
|
|
||||||
new_poly = malloc(sizeof(*new_poly));
|
new_poly = ntru_malloc(sizeof(*new_poly));
|
||||||
init_integer(&chara);
|
init_integer(&chara);
|
||||||
init_polynom_size(new_poly, &chara, len);
|
init_polynom_size(new_poly, &chara, len);
|
||||||
mp_clear(&chara);
|
mp_clear(&chara);
|
||||||
@ -126,14 +131,14 @@ pb_poly *build_polynom(int const * const c,
|
|||||||
unsigned_c = c[i];
|
unsigned_c = c[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_set_int(&(new_poly->terms[i]), unsigned_c);
|
MP_SET_INT(&(new_poly->terms[i]), unsigned_c);
|
||||||
|
|
||||||
if (sign == true)
|
if (sign == true)
|
||||||
mp_neg(&(new_poly->terms[i]), &(new_poly->terms[i]));
|
mp_neg(&(new_poly->terms[i]), &(new_poly->terms[i]));
|
||||||
}
|
}
|
||||||
} else { /* fill with zeros */
|
} else { /* fill with zeros */
|
||||||
for (unsigned int i = 0; i < len; i++)
|
for (unsigned int i = 0; i < len; i++)
|
||||||
mp_set(&(new_poly->terms[i]), 0);
|
MP_SET(&(new_poly->terms[i]), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
new_poly->used = len;
|
new_poly->used = len;
|
||||||
@ -150,7 +155,7 @@ pb_poly *build_polynom(int const * const c,
|
|||||||
void erase_polynom(pb_poly *poly, size_t len)
|
void erase_polynom(pb_poly *poly, size_t len)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < len ; i++) {
|
for (unsigned int i = 0; i < len ; i++) {
|
||||||
mp_set(&(poly->terms[i]), 0);
|
MP_SET(&(poly->terms[i]), 0);
|
||||||
mp_abs(&(poly->terms[i]), &(poly->terms[i]));
|
mp_abs(&(poly->terms[i]), &(poly->terms[i]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -169,6 +174,30 @@ void delete_polynom(pb_poly *poly)
|
|||||||
free(poly);
|
free(poly);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This deletes the internal structure of all polynomials,
|
||||||
|
* and frees the pointers. Don't call this on stack variables,
|
||||||
|
* this is intended for use after ntru_ functions, that
|
||||||
|
* return a polynomial pointer.
|
||||||
|
* You must call this with NULL as last argument!
|
||||||
|
*
|
||||||
|
* @param poly the polynomial to delete
|
||||||
|
* @param ... follow up polynomials
|
||||||
|
*/
|
||||||
|
void delete_polynom_multi(pb_poly *poly, ...)
|
||||||
|
{
|
||||||
|
pb_poly *next_poly;
|
||||||
|
va_list args;
|
||||||
|
|
||||||
|
next_poly = poly;
|
||||||
|
va_start(args, poly);
|
||||||
|
while (next_poly != NULL) {
|
||||||
|
delete_polynom(next_poly);
|
||||||
|
next_poly = va_arg(args, pb_poly*);
|
||||||
|
}
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starmultiplication, as follows:
|
* Starmultiplication, as follows:
|
||||||
* c = a * b mod (x^N − 1)
|
* c = a * b mod (x^N − 1)
|
||||||
@ -189,7 +218,7 @@ void pb_starmultiply(pb_poly *a,
|
|||||||
mp_int mp_modulus;
|
mp_int mp_modulus;
|
||||||
|
|
||||||
init_integer(&mp_modulus);
|
init_integer(&mp_modulus);
|
||||||
mp_set_int(&mp_modulus, (unsigned long)(modulus));
|
MP_SET_INT(&mp_modulus, (unsigned long)(modulus));
|
||||||
|
|
||||||
/* avoid side effects */
|
/* avoid side effects */
|
||||||
a_tmp = build_polynom(NULL, ctx->N, ctx);
|
a_tmp = build_polynom(NULL, ctx->N, ctx);
|
||||||
@ -257,6 +286,42 @@ static unsigned int get_degree(pb_poly const * const poly)
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the inverse polynomial modulo a power of 2,
|
||||||
|
* which is q.
|
||||||
|
*
|
||||||
|
* @param a polynomial to invert
|
||||||
|
* @param Fq polynomial [out]
|
||||||
|
* @param ctx NTRU context
|
||||||
|
*/
|
||||||
|
static void pb_mod2_to_modq(pb_poly * const a,
|
||||||
|
pb_poly *Fq,
|
||||||
|
ntru_context *ctx)
|
||||||
|
{
|
||||||
|
int v = 2;
|
||||||
|
|
||||||
|
while (v < (int)(ctx->q)) {
|
||||||
|
pb_poly *pb_tmp,
|
||||||
|
*pb_tmp2;
|
||||||
|
mp_int tmp_v;
|
||||||
|
pb_tmp = build_polynom(NULL, ctx->N, ctx);
|
||||||
|
v = v * 2;
|
||||||
|
init_integer(&tmp_v);
|
||||||
|
MP_SET_INT(&tmp_v, v);
|
||||||
|
pb_tmp2 = build_polynom(NULL, ctx->N, ctx);
|
||||||
|
MP_SET_INT(&(pb_tmp2->terms[0]), 2);
|
||||||
|
|
||||||
|
/* mod after sub or before? */
|
||||||
|
pb_starmultiply(a, Fq, pb_tmp, ctx, v);
|
||||||
|
PB_SUB(pb_tmp2, pb_tmp, pb_tmp);
|
||||||
|
PB_MOD(pb_tmp, &tmp_v, pb_tmp, ctx->N);
|
||||||
|
pb_starmultiply(Fq, pb_tmp, Fq, ctx, v);
|
||||||
|
|
||||||
|
mp_clear(&tmp_v);
|
||||||
|
delete_polynom_multi(pb_tmp, pb_tmp2, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invert the polynomial a modulo q.
|
* Invert the polynomial a modulo q.
|
||||||
*
|
*
|
||||||
@ -270,19 +335,18 @@ bool pb_inverse_poly_q(pb_poly * const a,
|
|||||||
ntru_context *ctx)
|
ntru_context *ctx)
|
||||||
{
|
{
|
||||||
int k = 0,
|
int k = 0,
|
||||||
j = 0,
|
j = 0;
|
||||||
v = 2;
|
|
||||||
pb_poly *a_tmp, *b, *c, *f, *g;
|
pb_poly *a_tmp, *b, *c, *f, *g;
|
||||||
|
|
||||||
b = build_polynom(NULL, ctx->N + 1, ctx);
|
b = build_polynom(NULL, ctx->N + 1, ctx);
|
||||||
mp_set(&(b->terms[0]), 1);
|
MP_SET(&(b->terms[0]), 1);
|
||||||
c = build_polynom(NULL, ctx->N + 1, ctx);
|
c = build_polynom(NULL, ctx->N + 1, ctx);
|
||||||
f = build_polynom(NULL, ctx->N + 1, ctx);
|
f = build_polynom(NULL, ctx->N + 1, ctx);
|
||||||
PB_COPY(a, f);
|
PB_COPY(a, f);
|
||||||
g = build_polynom(NULL, ctx->N + 1, ctx);
|
g = build_polynom(NULL, ctx->N + 1, ctx);
|
||||||
mp_set(&(g->terms[0]), 1);
|
MP_SET(&(g->terms[0]), 1);
|
||||||
mp_neg(&(g->terms[0]), &(g->terms[0]));
|
mp_neg(&(g->terms[0]), &(g->terms[0]));
|
||||||
mp_set(&(g->terms[ctx->N]), 1);
|
MP_SET(&(g->terms[ctx->N]), 1);
|
||||||
/* avoid side effects */
|
/* avoid side effects */
|
||||||
a_tmp = build_polynom(NULL, ctx->N, ctx);
|
a_tmp = build_polynom(NULL, ctx->N, ctx);
|
||||||
PB_COPY(a, a_tmp);
|
PB_COPY(a, a_tmp);
|
||||||
@ -294,8 +358,8 @@ bool pb_inverse_poly_q(pb_poly * const a,
|
|||||||
MP_COPY(&(f->terms[i]), &(f->terms[i - 1]));
|
MP_COPY(&(f->terms[i]), &(f->terms[i - 1]));
|
||||||
MP_COPY(&(c->terms[ctx->N - i]), &(c->terms[ctx->N + 1 - i]));
|
MP_COPY(&(c->terms[ctx->N - i]), &(c->terms[ctx->N + 1 - i]));
|
||||||
}
|
}
|
||||||
mp_set(&(f->terms[ctx->N]), 0);
|
MP_SET(&(f->terms[ctx->N]), 0);
|
||||||
mp_set(&(c->terms[0]), 0);
|
MP_SET(&(c->terms[0]), 0);
|
||||||
k++;
|
k++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -321,42 +385,18 @@ OUT_OF_LOOP:
|
|||||||
MP_COPY(&(b->terms[i]), &(Fq->terms[j]));
|
MP_COPY(&(b->terms[i]), &(Fq->terms[j]));
|
||||||
}
|
}
|
||||||
|
|
||||||
while (v < (int)(ctx->q)) {
|
pb_mod2_to_modq(a_tmp, Fq, ctx);
|
||||||
pb_poly *pb_tmp,
|
|
||||||
*pb_tmp2;
|
|
||||||
mp_int tmp_v;
|
|
||||||
pb_tmp = build_polynom(NULL, ctx->N, ctx);
|
|
||||||
v = v * 2;
|
|
||||||
init_integer(&tmp_v);
|
|
||||||
mp_set_int(&tmp_v, v);
|
|
||||||
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);
|
|
||||||
PB_SUB(pb_tmp2, pb_tmp, pb_tmp);
|
|
||||||
PB_MOD(pb_tmp, &tmp_v, pb_tmp, ctx->N);
|
|
||||||
pb_starmultiply(Fq, pb_tmp, Fq, ctx, v);
|
|
||||||
|
|
||||||
mp_clear(&tmp_v);
|
|
||||||
delete_polynom(pb_tmp);
|
|
||||||
delete_polynom(pb_tmp2);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = ctx->N - 1; i >= 0; i--)
|
for (int i = ctx->N - 1; i >= 0; i--)
|
||||||
if (mp_cmp_d(&(Fq->terms[i]), 0) == MP_LT) {
|
if (mp_cmp_d(&(Fq->terms[i]), 0) == MP_LT) {
|
||||||
mp_int mp_tmp;
|
mp_int mp_tmp;
|
||||||
init_integer(&mp_tmp);
|
init_integer(&mp_tmp);
|
||||||
mp_set_int(&mp_tmp, ctx->q);
|
MP_SET_INT(&mp_tmp, ctx->q);
|
||||||
MP_ADD(&(Fq->terms[i]), &mp_tmp, &(Fq->terms[i]));
|
MP_ADD(&(Fq->terms[i]), &mp_tmp, &(Fq->terms[i]));
|
||||||
mp_clear(&mp_tmp);
|
mp_clear(&mp_tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete_polynom(a_tmp);
|
delete_polynom_multi(a_tmp, b, c, f, g, NULL);
|
||||||
delete_polynom(b);
|
|
||||||
delete_polynom(c);
|
|
||||||
delete_polynom(f);
|
|
||||||
delete_polynom(g);
|
|
||||||
|
|
||||||
/* TODO: check if the f * Fq = 1 (mod p) condition holds true */
|
/* TODO: check if the f * Fq = 1 (mod p) condition holds true */
|
||||||
|
|
||||||
|
29
src/poly.h
29
src/poly.h
@ -28,8 +28,19 @@
|
|||||||
|
|
||||||
#include <tompoly.h>
|
#include <tompoly.h>
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
#include <stdarg.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#define MP_SET(...) mp_set(__VA_ARGS__)
|
||||||
|
|
||||||
|
#define MP_SET_INT(...) \
|
||||||
|
{ \
|
||||||
|
int result; \
|
||||||
|
if ((result = mp_set_int(__VA_ARGS__)) != MP_OKAY) \
|
||||||
|
NTRU_ABORT("Error setting long constant. %s", \
|
||||||
|
mp_error_to_string(result)); \
|
||||||
|
}
|
||||||
|
|
||||||
#define MP_MUL(...) \
|
#define MP_MUL(...) \
|
||||||
{ \
|
{ \
|
||||||
int result; \
|
int result; \
|
||||||
@ -86,6 +97,22 @@
|
|||||||
mp_error_to_string(result)); \
|
mp_error_to_string(result)); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define MP_EXPTMOD(...) \
|
||||||
|
{ \
|
||||||
|
int result; \
|
||||||
|
if ((result = mp_exptmod(__VA_ARGS__)) != MP_OKAY) \
|
||||||
|
NTRU_ABORT("Error computing modular exponentiation. %s", \
|
||||||
|
mp_error_to_string(result)); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define MP_EXPT_D(...) \
|
||||||
|
{ \
|
||||||
|
int result; \
|
||||||
|
if ((result = mp_expt_d(__VA_ARGS__)) != MP_OKAY) \
|
||||||
|
NTRU_ABORT("Error computing modular exponentiation. %s", \
|
||||||
|
mp_error_to_string(result)); \
|
||||||
|
}
|
||||||
|
|
||||||
#define PB_MUL(...) \
|
#define PB_MUL(...) \
|
||||||
{ \
|
{ \
|
||||||
int result; \
|
int result; \
|
||||||
@ -138,6 +165,8 @@ void erase_polynom(pb_poly *poly, size_t len);
|
|||||||
|
|
||||||
void delete_polynom(pb_poly *new_poly);
|
void delete_polynom(pb_poly *new_poly);
|
||||||
|
|
||||||
|
void delete_polynom_multi(pb_poly *poly, ...);
|
||||||
|
|
||||||
void pb_starmultiply(pb_poly *a,
|
void pb_starmultiply(pb_poly *a,
|
||||||
pb_poly *b,
|
pb_poly *b,
|
||||||
pb_poly *c,
|
pb_poly *c,
|
||||||
|
Loading…
Reference in New Issue
Block a user