From 9b71469b51bddd57e03517103646f509aead12be Mon Sep 17 00:00:00 2001 From: hasufell Date: Tue, 3 Jun 2014 18:01:13 +0200 Subject: [PATCH] RND: first try of random module Currently only works with specifying the number of negative and positive ones. --- src/Makefile | 8 +++- src/rnd.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/rnd.h | 54 +++++++++++++++++++++++++ 3 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 src/rnd.c create mode 100644 src/rnd.h diff --git a/src/Makefile b/src/Makefile index 7cf0db2..50d0df9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -10,7 +10,8 @@ PQC_SOURCES = poly.c \ ascii_poly.c \ file.c \ ntru_string.c \ - poly_ascii.c + poly_ascii.c \ + rnd.c PQC_OBJS = $(patsubst %.c, %.o, $(PQC_SOURCES)) @@ -24,7 +25,8 @@ PQC_HEADERS = err.h \ common.h \ file.h \ ntru_string.h \ - poly_ascii.h + poly_ascii.h \ + rnd.h # libs LIBS += -L. -lgmp -lmpfr -lflint $(shell $(PKG_CONFIG) --libs glib-2.0) -lm @@ -32,6 +34,8 @@ LIBS += -L. -lgmp -lmpfr -lflint $(shell $(PKG_CONFIG) --libs glib-2.0) -lm # includes INCS = -I. -I/usr/include/flint $(shell $(PKG_CONFIG) --cflags glib-2.0) +CFLAGS += -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED + %.o: %.c $(CC) -fPIC $(CFLAGS) $(CPPFLAGS) $(INCS) -c $*.c diff --git a/src/rnd.c b/src/rnd.c new file mode 100644 index 0000000..ddbe3a0 --- /dev/null +++ b/src/rnd.c @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +/** + * @file rnd.c + * This file allows generation of random polynomials. + * @brief random polynomials + */ + +#include "context.h" +#include "err.h" +#include "math.h" +#include "poly.h" + +#include +#include +#include +#include + + +/*------------------------------------------------------------------------*/ + +static int +get_rnd_int(void) +{ + int rnd_data = open("/dev/random", O_RDONLY); + int rnd_int; + size_t rnd_len = 0; + + while (rnd_len < sizeof(rnd_int)) { + ssize_t result = read(rnd_data, ((char*)&rnd_int) + rnd_len, + sizeof(rnd_int) - rnd_len); + + if (result < 0) + NTRU_ABORT("Unable to read /dev/random!\n"); + + rnd_len += result; + } + + close(rnd_data); + + return rnd_int; +} + +/*------------------------------------------------------------------------*/ + +static int +get_urnd_int(void) +{ + int rnd_data = open("/dev/urandom", O_RDONLY); + int rnd_int; + ssize_t result; + + result = read(rnd_data, ((char*)&rnd_int), + sizeof(rnd_int)); + + if (result < 0) + NTRU_ABORT("Unable to read /dev/urandom!\n"); + + close(rnd_data); + + return rnd_int; +} + +/*------------------------------------------------------------------------*/ + +void +ntru_get_rnd_tern_poly_num(fmpz_poly_t poly, + const ntru_context *ctx, + uint32_t num_ones, + uint32_t num_neg_ones) +{ + if (!poly || ! ctx) + NTRU_ABORT("unexpected NULL parameters in" + "ntru_get_rnd_tern_poly_num()!\n"); + + fmpz_poly_zero(poly); + + while (num_ones != 0 || num_neg_ones != 0) { + int32_t pos = get_rnd_int() % ctx->N; + + if (!fmpz_cmp_si_n(fmpz_poly_get_coeff_ptr(poly, pos), 0)) { + if (num_ones > 0) { + fmpz_poly_set_coeff_si(poly, pos, 1); + num_ones--; + } else if (num_neg_ones > 0) { + fmpz_poly_set_coeff_si(poly, pos, -1); + num_neg_ones--; + } + } + } +} + +/*------------------------------------------------------------------------*/ diff --git a/src/rnd.h b/src/rnd.h new file mode 100644 index 0000000..c4e7167 --- /dev/null +++ b/src/rnd.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +/** + * @file rnd.h + * Header for the internal API of rnd.c. + * @brief header for rnd.c + */ + +#ifndef NTRU_RND_H +#define NTRU_RND_H + +#include "context.h" + +#include + +#include + + +/** + * Get a random ternary polynomial with specified numbers + * of 1 coefficients and -1 coefficients. + * + * @param poly the resulting random polynomial [out] + * @param ctx the NTRU context + * @param num_ones the number of 1 coefficients + * @param num_neg_ones the number of -1 coefficients + */ +void +ntru_get_rnd_tern_poly_num(fmpz_poly_t poly, + const ntru_context *ctx, + uint32_t num_ones, + uint32_t num_neg_ones); + + +#endif /* NTRU_RND_H */