diff --git a/src/poly.c b/src/poly.c index e4c1d43..8e84534 100644 --- a/src/poly.c +++ b/src/poly.c @@ -362,7 +362,7 @@ OUT_OF_LOOP: return true; } - * Print the polynomial in a human readable format to stdout. + /* Print the polynomial in a human readable format to stdout. * * @param poly to draw */ diff --git a/src/rand.c b/src/rand.c index f535c54..f75ac87 100644 --- a/src/rand.c +++ b/src/rand.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -36,12 +37,14 @@ /* * static declarations */ -static unsigned long get_urnd_int_small(int *sign); +static mp_digit get_urnd_int_small(int *sign); +static mp_digit get_rnd_int_small(int *sign); /** - * Gets randomly a small integer + * Gets a random small integer * from the set {-1, 0, 1} using /dev/random. * A zero is signed positiv. + * *sig == 1 means positiv integer. * * @param sign stores the signness [out] * @return random small integer @@ -58,7 +61,7 @@ static mp_digit get_rnd_int_small(int *sign) ((char*) &random_int) + randomDataLen, (sizeof(random_int)) - randomDataLen); if (result < 0) { - NTRU_ABORT("Unable to read /dev/random"); + NTRU_ABORT("Unable to read /dev/random.\n"); } randomDataLen += result; } @@ -93,7 +96,7 @@ pb_poly *ntru_get_rnd_poly_small(ntru_context *ctx) init_polynom_size(poly, &chara, ctx->N); mp_clear(&chara); - for (int i = 0; i < ctx->N; i++) { + for (unsigned int i = 0; i < ctx->N; i++) { int sign; int c = get_rnd_int_small(&sign); @@ -109,13 +112,15 @@ pb_poly *ntru_get_rnd_poly_small(ntru_context *ctx) } /** - * Gets randomly a small integer + * Gets a random small integer * from the set {-1, 0, 1} using /dev/urandom. + * A zero is signed positiv. + * *sig == 1 means positiv integer. * * @param sign stores the signness [out] * @return random small integer */ -static unsigned long get_urnd_int_small(int *sign) +static mp_digit get_urnd_int_small(int *sign) { int random_data; mp_digit random_int; @@ -123,13 +128,14 @@ static unsigned long get_urnd_int_small(int *sign) random_data = open("/dev/urandom", O_RDONLY); if ((result = read(random_data, &random_int, sizeof(random_int))) < 0) - NTRU_ABORT("Unable to read /dev/urandom"); + NTRU_ABORT("Unable to read /dev/urandom.\n"); close(random_data); - if ((random_int % 2) == 0) { - random_int = 0; + random_int = random_int % 3; + + if (random_int == 1) { *sign = 0; - } else if (random_int % 3) { + } else if (random_int == 2) { random_int = 1; *sign = 1; } else { @@ -159,7 +165,7 @@ pb_poly *ntru_get_urnd_poly_small(ntru_context *ctx) int sign; unsigned long c = get_urnd_int_small(&sign); - mp_set_int(&(poly->terms[i]), c); + mp_set(&(poly->terms[i]), (mp_digit) c); if (sign == 1) poly->terms[i].sign = 1; diff --git a/src/rand.h b/src/rand.h index 6db9c7d..475830f 100644 --- a/src/rand.h +++ b/src/rand.h @@ -19,7 +19,6 @@ * MA 02110-1301 USA */ - #ifndef NTRU_RAND_H #define NTRU_RAND_H @@ -27,8 +26,21 @@ #include +/** + * The maximal integer that is given by + * ntru_get_urnd_poly_big and ntru_get_rnd_poly_big + */ +#define BIG_RAND_MAX 100 + +/** + * The minimal integer that is given by + * ntru_get_urnd_poly_big and ntru_get_rnd_poly_big + */ +#define BIG_RAND_MIN -100 pb_poly *ntru_get_urnd_poly_small(ntru_context *ctx); pb_poly *ntru_get_rnd_poly_small(ntru_context *ctx); +pb_poly *ntru_get_urnd_poly_big(ntru_context *ctx); +pb_poly *ntru_get_rnd_poly_big(ntru_context *ctx); #endif /* NTRU_RAND_H */