diff --git a/src/rand.c b/src/rand.c index 956d9ad..f535c54 100644 --- a/src/rand.c +++ b/src/rand.c @@ -36,8 +36,7 @@ /* * static declarations */ -static mp_digit get_urnd_int_small(int *sign); -static mp_digit get_rnd_int_small(int *sign); +static unsigned long get_urnd_int_small(int *sign); /** * Gets randomly a small integer @@ -112,12 +111,11 @@ pb_poly *ntru_get_rnd_poly_small(ntru_context *ctx) /** * Gets randomly a small integer * from the set {-1, 0, 1} using /dev/urandom. - * A zero is signed positiv. * * @param sign stores the signness [out] * @return random small integer */ -static mp_digit get_urnd_int_small(int *sign) +static unsigned long get_urnd_int_small(int *sign) { int random_data; mp_digit random_int; @@ -128,14 +126,14 @@ static mp_digit get_urnd_int_small(int *sign) NTRU_ABORT("Unable to read /dev/urandom"); close(random_data); - random_int = random_int % 3; - - if (random_int == 1) { + if ((random_int % 2) == 0) { + random_int = 0; *sign = 0; - } else if (random_int == 2) { + } else if (random_int % 3) { random_int = 1; *sign = 1; } else { + random_int = 1; *sign = 0; } @@ -157,17 +155,18 @@ pb_poly *ntru_get_urnd_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_urnd_int_small(&sign); + unsigned long c = get_urnd_int_small(&sign); - mp_set(&(poly->terms[i]), (mp_digit) c); + mp_set_int(&(poly->terms[i]), c); if (sign == 1) poly->terms[i].sign = 1; } poly->used = ctx->N; - //pb_clamp(poly); + pb_clamp(poly); return poly; } +