From 04d5d6d38f605249105f507fa26715f357e7dd07 Mon Sep 17 00:00:00 2001 From: Malte Date: Sat, 19 Apr 2014 18:35:51 +0200 Subject: [PATCH] rand:ntru_get_rnd_poly_small: written a function to generate a random polynom like get_urnd_int_small but with the random source from /dev/random instead of /dev/urandom --- src/Makefile | 2 +- src/rand.c | 36 ++++++++++++++++++++++++++++++++++-- src/rand.h | 1 + 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Makefile b/src/Makefile index fd3ce31..95b5320 100644 --- a/src/Makefile +++ b/src/Makefile @@ -57,7 +57,7 @@ endif %.o: %.c $(CC) -fPIC $(CFLAGS) $(CPPFLAGS) $(INCS) -c $*.c -all: libpqc.a libpqc.so main +all: libpqc.a libpqc.so # test: $(CUNIT_OBJS) $(PQC_LIBS) diff --git a/src/rand.c b/src/rand.c index 0f88a85..956d9ad 100644 --- a/src/rand.c +++ b/src/rand.c @@ -42,6 +42,7 @@ static mp_digit get_rnd_int_small(int *sign); /** * Gets randomly a small integer * from the set {-1, 0, 1} using /dev/random. + * A zero is signed positiv. * * @param sign stores the signness [out] * @return random small integer @@ -77,9 +78,41 @@ static mp_digit get_rnd_int_small(int *sign) return random_int; } +/** + * Gets a random polynomial with coefficients + * from the set {-1 ,0 ,1} using /dev/random. + * + * + * @param ctx the NTRU context + * @return newly allocated polynomial, must be freed with delete_polynom() + */ +pb_poly *ntru_get_rnd_poly_small(ntru_context *ctx) +{ + mp_int chara; + init_integer(&chara); + pb_poly *poly = malloc(sizeof(pb_poly)); + init_polynom_size(poly, &chara, ctx->N); + mp_clear(&chara); + + for (int i = 0; i < ctx->N; i++) { + int sign; + int c = get_rnd_int_small(&sign); + + mp_set(&(poly->terms[i]), (mp_digit) c); + + if (sign == 1) + poly->terms[i].sign = 1; + } + poly->used = ctx->N; + //pb_clamp(poly); + + return poly; +} + /** * 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 @@ -134,8 +167,7 @@ pb_poly *ntru_get_urnd_poly_small(ntru_context *ctx) poly->terms[i].sign = 1; } poly->used = ctx->N; - pb_clamp(poly); + //pb_clamp(poly); return poly; } - diff --git a/src/rand.h b/src/rand.h index adafdf8..6db9c7d 100644 --- a/src/rand.h +++ b/src/rand.h @@ -29,5 +29,6 @@ pb_poly *ntru_get_urnd_poly_small(ntru_context *ctx); +pb_poly *ntru_get_rnd_poly_small(ntru_context *ctx); #endif /* NTRU_RAND_H */