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.
This commit is contained in:
hasufell 2014-05-13 00:12:03 +02:00
parent e29064a666
commit 30e18177e8
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
2 changed files with 8 additions and 21 deletions

View File

@ -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 */

View File

@ -30,16 +30,19 @@
#include <tommath.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#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(...) \