RAND: use unsigned long instead of mp_digit

if we use mp_set_int() instead of mp_set(), then we can use
full unsigned long integers instead of single digits. This
seems a lot safer, especially for future versions of the
random algorithm.

RAND: merged a conflict
This commit is contained in:
hasufell 2014-04-15 18:21:42 +02:00 committed by malte
parent 0470a5fa6e
commit 417ce9c0f2
1 changed files with 11 additions and 12 deletions

View File

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