Added static mp_digit get_rnd_int_small(int *sign)

but it takes about ~40minutes to generate a x^500
polynom with /dev/random.
This commit is contained in:
Malte 2014-04-17 18:26:18 +02:00 committed by malte
parent 3902209626
commit 71b8f4cbeb
2 changed files with 44 additions and 6 deletions

View File

@ -57,7 +57,7 @@ endif
%.o: %.c %.o: %.c
$(CC) -fPIC $(CFLAGS) $(CPPFLAGS) $(INCS) -c $*.c $(CC) -fPIC $(CFLAGS) $(CPPFLAGS) $(INCS) -c $*.c
all: libpqc.a libpqc.so all: libpqc.a libpqc.so main
# test: $(CUNIT_OBJS) $(PQC_LIBS) # test: $(CUNIT_OBJS) $(PQC_LIBS)

View File

@ -37,7 +37,45 @@
* static declarations * static declarations
*/ */
static mp_digit 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
* from the set {-1, 0, 1} using /dev/random.
*
* @param sign stores the signness [out]
* @return random small integer
*/
static mp_digit get_rnd_int_small(int *sign)
{
int random_data;
mp_digit random_int;
size_t randomDataLen = 0;
random_data = open("/dev/random", O_RDONLY);
while (randomDataLen < sizeof(random_int)) {
ssize_t result = read(random_data,
((char*) &random_int) + randomDataLen,
(sizeof(random_int)) - randomDataLen);
if (result < 0) {
NTRU_ABORT("Unable to read /dev/random");
}
randomDataLen += result;
}
close(random_data);
random_int = random_int % 3;
if (random_int == 1) {
*sign = 0;
} else if (random_int == 2) {
random_int = 1;
*sign = 1;
} else {
*sign = 0;
}
return random_int;
}
/** /**
* Gets randomly a small integer * Gets randomly a small integer
@ -57,14 +95,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);
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 {
random_int = 1;
*sign = 0; *sign = 0;
} }
@ -90,7 +128,7 @@ pb_poly *ntru_get_urnd_poly_small(ntru_context *ctx)
int sign; int sign;
int c = get_urnd_int_small(&sign); int c = get_urnd_int_small(&sign);
mp_set(&(poly->terms[i]), (mp_digit)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;