From 476379b67515d746060b4de1dd2759eeddcac23e Mon Sep 17 00:00:00 2001 From: hasufell Date: Sun, 20 Apr 2014 19:53:30 +0200 Subject: [PATCH] POLY: use our MACROS for error handling All mp_* and pb_* functions that return an error code should only be called via a MACRO which handles the error. --- src/poly.c | 20 ++++++++++---------- src/poly.h | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/poly.c b/src/poly.c index 72a5345..1380942 100644 --- a/src/poly.c +++ b/src/poly.c @@ -127,14 +127,14 @@ pb_poly *build_polynom(int const * const c, 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) mp_neg(&(new_poly->terms[i]), &(new_poly->terms[i])); } } else { /* fill with zeros */ 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; @@ -151,7 +151,7 @@ pb_poly *build_polynom(int const * const c, void erase_polynom(pb_poly *poly, size_t len) { 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])); } } @@ -190,7 +190,7 @@ void pb_starmultiply(pb_poly *a, mp_int 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 */ a_tmp = build_polynom(NULL, ctx->N, ctx); @@ -276,14 +276,14 @@ bool pb_inverse_poly_q(pb_poly * const a, pb_poly *a_tmp, *b, *c, *f, *g; 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); f = build_polynom(NULL, ctx->N + 1, ctx); PB_COPY(a, f); 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_set(&(g->terms[ctx->N]), 1); + MP_SET(&(g->terms[ctx->N]), 1); /* avoid side effects */ a_tmp = build_polynom(NULL, ctx->N, ctx); PB_COPY(a, a_tmp); @@ -295,8 +295,8 @@ bool pb_inverse_poly_q(pb_poly * const a, MP_COPY(&(f->terms[i]), &(f->terms[i - 1])); MP_COPY(&(c->terms[ctx->N - i]), &(c->terms[ctx->N + 1 - i])); } - mp_set(&(f->terms[ctx->N]), 0); - mp_set(&(c->terms[0]), 0); + MP_SET(&(f->terms[ctx->N]), 0); + MP_SET(&(c->terms[0]), 0); k++; } @@ -348,7 +348,7 @@ OUT_OF_LOOP: 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); + MP_SET_INT(&mp_tmp, ctx->q); MP_ADD(&(Fq->terms[i]), &mp_tmp, &(Fq->terms[i])); mp_clear(&mp_tmp); } diff --git a/src/poly.h b/src/poly.h index f60ab26..4d9aa04 100644 --- a/src/poly.h +++ b/src/poly.h @@ -30,6 +30,16 @@ #include #include +#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(...) \ { \ int result; \ @@ -86,6 +96,22 @@ 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(...) \ { \ int result; \