RND: first try of random module
Currently only works with specifying the number of negative and positive ones.
This commit is contained in:
parent
2eb7af07dc
commit
9b71469b51
@ -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
|
||||
|
112
src/rnd.c
Normal file
112
src/rnd.c
Normal file
@ -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 <fmpz_poly.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
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--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
54
src/rnd.h
Normal file
54
src/rnd.h
Normal file
@ -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 <stdlib.h>
|
||||
|
||||
#include <fmpz_poly.h>
|
||||
|
||||
|
||||
/**
|
||||
* 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 */
|
Loading…
Reference in New Issue
Block a user