rand: getting small and big polynoms is working

pb_poly *ntru_get_urnd_poly_big(ntru_context *ctx)
pb_poly *ntru_get_rnd_poly_big(ntru_context *ctx)
are written but the testing isen working and
i think it is a bad idea to not use function poniters
at the moment we have about 4 functions that do all the same thing
and differ only in the subfunction call.
But at the moment i cat decide of we shold use
function pinter

RAND: merged conflict
This commit is contained in:
Malte 2014-04-20 16:42:33 +02:00 committed by malte
parent 99ebda181a
commit 90a01a03ec
3 changed files with 31 additions and 13 deletions

View File

@ -362,7 +362,7 @@ OUT_OF_LOOP:
return true; 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 * @param poly to draw
*/ */

View File

@ -28,6 +28,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <tompoly.h> #include <tompoly.h>
#include <tommath.h> #include <tommath.h>
#include <math.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
@ -36,12 +37,14 @@
/* /*
* static declarations * 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. * from the set {-1, 0, 1} using /dev/random.
* A zero is signed positiv. * A zero is signed positiv.
* *sig == 1 means positiv integer.
* *
* @param sign stores the signness [out] * @param sign stores the signness [out]
* @return random small integer * @return random small integer
@ -58,7 +61,7 @@ static mp_digit get_rnd_int_small(int *sign)
((char*) &random_int) + randomDataLen, ((char*) &random_int) + randomDataLen,
(sizeof(random_int)) - randomDataLen); (sizeof(random_int)) - randomDataLen);
if (result < 0) { if (result < 0) {
NTRU_ABORT("Unable to read /dev/random"); NTRU_ABORT("Unable to read /dev/random.\n");
} }
randomDataLen += result; randomDataLen += result;
} }
@ -93,7 +96,7 @@ pb_poly *ntru_get_rnd_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_rnd_int_small(&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. * 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] * @param sign stores the signness [out]
* @return random small integer * @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; int random_data;
mp_digit random_int; mp_digit random_int;
@ -123,13 +128,14 @@ static unsigned long get_urnd_int_small(int *sign)
random_data = open("/dev/urandom", O_RDONLY); random_data = open("/dev/urandom", O_RDONLY);
if ((result = read(random_data, &random_int, sizeof(random_int))) < 0) 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); close(random_data);
if ((random_int % 2) == 0) { random_int = random_int % 3;
random_int = 0;
if (random_int == 1) {
*sign = 0; *sign = 0;
} else if (random_int % 3) { } else if (random_int == 2) {
random_int = 1; random_int = 1;
*sign = 1; *sign = 1;
} else { } else {
@ -159,7 +165,7 @@ pb_poly *ntru_get_urnd_poly_small(ntru_context *ctx)
int sign; int sign;
unsigned long c = get_urnd_int_small(&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) if (sign == 1)
poly->terms[i].sign = 1; poly->terms[i].sign = 1;

View File

@ -19,7 +19,6 @@
* MA 02110-1301 USA * MA 02110-1301 USA
*/ */
#ifndef NTRU_RAND_H #ifndef NTRU_RAND_H
#define NTRU_RAND_H #define NTRU_RAND_H
@ -27,8 +26,21 @@
#include <tompoly.h> #include <tompoly.h>
/**
* 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_urnd_poly_small(ntru_context *ctx);
pb_poly *ntru_get_rnd_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 */ #endif /* NTRU_RAND_H */