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:
parent
e29064a666
commit
30e18177e8
22
src/poly.c
22
src/poly.c
@ -138,22 +138,8 @@ pb_poly *build_polynom(int const * const c,
|
|||||||
|
|
||||||
/* fill the polynom if c is not NULL */
|
/* fill the polynom if c is not NULL */
|
||||||
if (c) {
|
if (c) {
|
||||||
for (unsigned int i = 0; i < len; i++) {
|
for (unsigned int i = 0; i < len; i++)
|
||||||
bool sign = false;
|
MP_SET_INT(&(new_poly->terms[i]), c[i]);
|
||||||
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]));
|
|
||||||
}
|
|
||||||
} else { /* fill with 0 */
|
} else { /* fill with 0 */
|
||||||
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);
|
||||||
@ -391,7 +377,6 @@ bool pb_inverse_poly_q(pb_poly * const a,
|
|||||||
/* set g(x) = x^N − 1 */
|
/* set g(x) = x^N − 1 */
|
||||||
g = build_polynom(NULL, ctx->N + 1);
|
g = build_polynom(NULL, ctx->N + 1);
|
||||||
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 */
|
/* avoid side effects */
|
||||||
@ -476,8 +461,7 @@ bool pb_inverse_poly_p(pb_poly *a,
|
|||||||
|
|
||||||
/* set g(x) = x^N − 1 */
|
/* set g(x) = x^N − 1 */
|
||||||
g = build_polynom(NULL, ctx->N + 1);
|
g = build_polynom(NULL, ctx->N + 1);
|
||||||
MP_SET(&(g->terms[0]), 1);
|
MP_SET_INT(&(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 */
|
/* avoid side effects */
|
||||||
|
@ -30,16 +30,19 @@
|
|||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
#define MP_SET(...) mp_set(__VA_ARGS__)
|
#define MP_SET(...) mp_set(__VA_ARGS__)
|
||||||
|
|
||||||
#define MP_SET_INT(...) \
|
#define MP_SET_INT(a, b) \
|
||||||
{ \
|
{ \
|
||||||
int result; \
|
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", \
|
NTRU_ABORT("Error setting long constant. %s", \
|
||||||
mp_error_to_string(result)); \
|
mp_error_to_string(result)); \
|
||||||
|
if ((int)b < 0) \
|
||||||
|
mp_neg(a, a); \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MP_MUL(...) \
|
#define MP_MUL(...) \
|
||||||
|
Loading…
Reference in New Issue
Block a user