From 30e18177e8aba8e3c5b4835379e3919f1aaf8323 Mon Sep 17 00:00:00 2001 From: hasufell Date: Tue, 13 May 2014 00:12:03 +0200 Subject: [PATCH] POLY: allow signed int in MP_SET_INT Now you can pass MP_SET_INT(&foo, -1) without having to call mp_neg() later etc. --- src/poly.c | 22 +++------------------- src/poly.h | 7 +++++-- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/poly.c b/src/poly.c index 5dd64c8..7f3aabd 100644 --- a/src/poly.c +++ b/src/poly.c @@ -138,22 +138,8 @@ pb_poly *build_polynom(int const * const c, /* 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) - mp_neg(&(new_poly->terms[i]), &(new_poly->terms[i])); - } + for (unsigned int i = 0; i < len; i++) + MP_SET_INT(&(new_poly->terms[i]), c[i]); } else { /* fill with 0 */ for (unsigned int i = 0; i < len; i++) MP_SET(&(new_poly->terms[i]), 0); @@ -391,7 +377,6 @@ bool pb_inverse_poly_q(pb_poly * const a, /* set g(x) = x^N − 1 */ g = build_polynom(NULL, ctx->N + 1); MP_SET(&(g->terms[0]), 1); - mp_neg(&(g->terms[0]), &(g->terms[0])); MP_SET(&(g->terms[ctx->N]), 1); /* avoid side effects */ @@ -476,8 +461,7 @@ bool pb_inverse_poly_p(pb_poly *a, /* set g(x) = x^N − 1 */ g = build_polynom(NULL, ctx->N + 1); - MP_SET(&(g->terms[0]), 1); - mp_neg(&(g->terms[0]), &(g->terms[0])); + MP_SET_INT(&(g->terms[0]), -1); MP_SET(&(g->terms[ctx->N]), 1); /* avoid side effects */ diff --git a/src/poly.h b/src/poly.h index fdf7450..7eacb21 100644 --- a/src/poly.h +++ b/src/poly.h @@ -30,16 +30,19 @@ #include #include #include +#include #define MP_SET(...) mp_set(__VA_ARGS__) -#define MP_SET_INT(...) \ +#define MP_SET_INT(a, b) \ { \ int result; \ - if ((result = mp_set_int(__VA_ARGS__)) != MP_OKAY) \ + if ((result = mp_set_int(a, (unsigned long)abs(b))) != MP_OKAY) \ NTRU_ABORT("Error setting long constant. %s", \ mp_error_to_string(result)); \ + if ((int)b < 0) \ + mp_neg(a, a); \ } #define MP_MUL(...) \