Created libnttfft

This makes Number Theoretic Transforms (NTT) based on the efficient
Fast-Fourier-Transform (FFT) available to multiple plugins.
This commit is contained in:
Andreas Steffen
2016-07-29 12:36:15 +02:00
parent 65f2ecb86d
commit d305f251a5
20 changed files with 261 additions and 121 deletions
+3
View File
@@ -1630,6 +1630,7 @@ AM_CONDITIONAL(USE_CONFTEST, test x$conftest = xtrue)
AM_CONDITIONAL(USE_LIBSTRONGSWAN, test x$charon = xtrue -o x$pki = xtrue -o x$scepclient = xtrue -o x$conftest = xtrue -o x$fast = xtrue -o x$imcv = xtrue -o x$nm = xtrue -o x$tkm = xtrue -o x$cmd = xtrue -o x$tls = xtrue -o x$tnc_tnccs = xtrue -o x$aikgen = xtrue -o x$aikpub2 = xtrue -o x$svc = xtrue -o x$systemd = xtrue) AM_CONDITIONAL(USE_LIBSTRONGSWAN, test x$charon = xtrue -o x$pki = xtrue -o x$scepclient = xtrue -o x$conftest = xtrue -o x$fast = xtrue -o x$imcv = xtrue -o x$nm = xtrue -o x$tkm = xtrue -o x$cmd = xtrue -o x$tls = xtrue -o x$tnc_tnccs = xtrue -o x$aikgen = xtrue -o x$aikpub2 = xtrue -o x$svc = xtrue -o x$systemd = xtrue)
AM_CONDITIONAL(USE_LIBCHARON, test x$charon = xtrue -o x$conftest = xtrue -o x$nm = xtrue -o x$tkm = xtrue -o x$cmd = xtrue -o x$svc = xtrue -o x$systemd = xtrue) AM_CONDITIONAL(USE_LIBCHARON, test x$charon = xtrue -o x$conftest = xtrue -o x$nm = xtrue -o x$tkm = xtrue -o x$cmd = xtrue -o x$svc = xtrue -o x$systemd = xtrue)
AM_CONDITIONAL(USE_LIBIPSEC, test x$libipsec = xtrue) AM_CONDITIONAL(USE_LIBIPSEC, test x$libipsec = xtrue)
AM_CONDITIONAL(USE_LIBNTTFFT, test x$bliss = xtrue)
AM_CONDITIONAL(USE_LIBTNCIF, test x$tnc_tnccs = xtrue -o x$imcv = xtrue) AM_CONDITIONAL(USE_LIBTNCIF, test x$tnc_tnccs = xtrue -o x$imcv = xtrue)
AM_CONDITIONAL(USE_LIBTNCCS, test x$tnc_tnccs = xtrue) AM_CONDITIONAL(USE_LIBTNCCS, test x$tnc_tnccs = xtrue)
AM_CONDITIONAL(USE_LIBPTTLS, test x$tnc_tnccs = xtrue) AM_CONDITIONAL(USE_LIBPTTLS, test x$tnc_tnccs = xtrue)
@@ -1722,6 +1723,8 @@ AC_CONFIG_FILES([
src/Makefile src/Makefile
src/include/Makefile src/include/Makefile
src/libstrongswan/Makefile src/libstrongswan/Makefile
src/libstrongswan/math/libnttfft/Makefile
src/libstrongswan/math/libnttfft/tests/Makefile
src/libstrongswan/plugins/aes/Makefile src/libstrongswan/plugins/aes/Makefile
src/libstrongswan/plugins/cmac/Makefile src/libstrongswan/plugins/cmac/Makefile
src/libstrongswan/plugins/des/Makefile src/libstrongswan/plugins/des/Makefile
+19 -4
View File
@@ -221,16 +221,22 @@ $(srcdir)/crypto/proposal/proposal_keywords_static.c: $(srcdir)/crypto/proposal/
$(GPERF) -N proposal_get_token_static -m 10 -C -G -c -t -D < \ $(GPERF) -N proposal_get_token_static -m 10 -C -G -c -t -D < \
$(srcdir)/crypto/proposal/proposal_keywords_static.txt > $@ $(srcdir)/crypto/proposal/proposal_keywords_static.txt > $@
# build plugins with their own Makefile
#######################################
if MONOLITHIC if MONOLITHIC
SUBDIRS = SUBDIRS =
else else
SUBDIRS = . SUBDIRS = .
endif endif
# build libnttfft used by some plugins
######################################
if USE_LIBNTTFFT
SUBDIRS += math/libnttfft
endif
# build plugins with their own Makefile
#######################################
if USE_AF_ALG if USE_AF_ALG
SUBDIRS += plugins/af_alg SUBDIRS += plugins/af_alg
if MONOLITHIC if MONOLITHIC
@@ -605,7 +611,16 @@ endif
if MONOLITHIC if MONOLITHIC
SUBDIRS += . SUBDIRS += .
endif endif
# build unit tests
##################
SUBDIRS += tests SUBDIRS += tests
if USE_LIBNTTFFT
SUBDIRS += math/libnttfft/tests
endif
if USE_BLISS if USE_BLISS
SUBDIRS += plugins/bliss/tests SUBDIRS += plugins/bliss/tests
endif endif
@@ -0,0 +1,15 @@
AM_CPPFLAGS = \
-I$(top_srcdir)/src/libstrongswan
AM_CFLAGS = \
@COVERAGE_CFLAGS@
AM_LDFLAGS = \
-no-undefined
ipseclib_LTLIBRARIES = libnttfft.la
libnttfft_la_SOURCES = \
ntt_fft_reduce.h ntt_fft.h ntt_fft.c \
ntt_fft_params.h ntt_fft_params.c
@@ -13,36 +13,36 @@
* for more details. * for more details.
*/ */
#include "bliss_fft.h" #include "ntt_fft.h"
#include "bliss_reduce.h" #include "ntt_fft_reduce.h"
typedef struct private_bliss_fft_t private_bliss_fft_t; typedef struct private_ntt_fft_t private_ntt_fft_t;
/** /**
* Private data structure for bliss_fft_t object * Private data structure for ntt_fft_t object
*/ */
struct private_bliss_fft_t { struct private_ntt_fft_t {
/** /**
* Public interface. * Public interface.
*/ */
bliss_fft_t public; ntt_fft_t public;
/** /**
* FFT parameter set used as constants * FFT parameter set used as constants
*/ */
bliss_fft_params_t *p; ntt_fft_params_t *p;
}; };
METHOD(bliss_fft_t, get_size, uint16_t, METHOD(ntt_fft_t, get_size, uint16_t,
private_bliss_fft_t *this) private_ntt_fft_t *this)
{ {
return this->p->n; return this->p->n;
} }
METHOD(bliss_fft_t, get_modulus, uint16_t, METHOD(ntt_fft_t, get_modulus, uint16_t,
private_bliss_fft_t *this) private_ntt_fft_t *this)
{ {
return this->p->q; return this->p->q;
} }
@@ -56,8 +56,7 @@ METHOD(bliss_fft_t, get_modulus, uint16_t,
* x[i2] ---|-|--|*|-- x[i2] * x[i2] ---|-|--|*|-- x[i2]
* *
*/ */
static void butterfly(private_bliss_fft_t *this, uint32_t *x, int i1,int i2, static void butterfly(private_ntt_fft_t *this, uint32_t *x, int i1,int i2, int iw)
int iw)
{ {
uint32_t xp, xm; uint32_t xp, xm;
@@ -68,13 +67,13 @@ static void butterfly(private_bliss_fft_t *this, uint32_t *x, int i1,int i2,
xp -= this->p->q; xp -= this->p->q;
} }
x[i1] = xp; x[i1] = xp;
x[i2] = bliss_mreduce(xm * this->p->wr[iw], this->p); x[i2] = ntt_fft_mreduce(xm * this->p->wr[iw], this->p);
} }
/** /**
* Trivial butterfly operation of last FFT stage * Trivial butterfly operation of last FFT stage
*/ */
static void butterfly_last(private_bliss_fft_t *this, uint32_t *x, int i1) static void butterfly_last(private_ntt_fft_t *this, uint32_t *x, int i1)
{ {
uint32_t xp, xm; uint32_t xp, xm;
int i2 = i1 + 1; int i2 = i1 + 1;
@@ -93,8 +92,8 @@ static void butterfly_last(private_bliss_fft_t *this, uint32_t *x, int i1)
x[i2] = xm; x[i2] = xm;
} }
METHOD(bliss_fft_t, transform, void, METHOD(ntt_fft_t, transform, void,
private_bliss_fft_t *this, uint32_t *a, uint32_t *b, bool inverse) private_ntt_fft_t *this, uint32_t *a, uint32_t *b, bool inverse)
{ {
int stage, i, j, k, m, n, s, t, iw, i_rev; int stage, i, j, k, m, n, s, t, iw, i_rev;
uint32_t tmp; uint32_t tmp;
@@ -108,7 +107,7 @@ METHOD(bliss_fft_t, transform, void,
/* apply linear phase needed for negative wrapped convolution */ /* apply linear phase needed for negative wrapped convolution */
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
b[i] = bliss_mreduce(a[i] * this->p->wf[s*i], this->p); b[i] = ntt_fft_mreduce(a[i] * this->p->wf[s*i], this->p);
} }
} }
else if (a != b) else if (a != b)
@@ -168,13 +167,13 @@ METHOD(bliss_fft_t, transform, void,
{ {
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
b[i] = bliss_mreduce(b[i] * this->p->wi[i], this->p); b[i] = ntt_fft_mreduce(b[i] * this->p->wi[i], this->p);
} }
} }
} }
METHOD(bliss_fft_t, destroy, void, METHOD(ntt_fft_t, destroy, void,
private_bliss_fft_t *this) private_ntt_fft_t *this)
{ {
free(this); free(this);
} }
@@ -182,9 +181,9 @@ METHOD(bliss_fft_t, destroy, void,
/** /**
* See header. * See header.
*/ */
bliss_fft_t *bliss_fft_create(bliss_fft_params_t *params) ntt_fft_t *ntt_fft_create(ntt_fft_params_t *params)
{ {
private_bliss_fft_t *this; private_ntt_fft_t *this;
INIT(this, INIT(this,
.public = { .public = {
@@ -14,37 +14,37 @@
*/ */
/** /**
* @defgroup bliss_fft bliss_fft * @defgroup ntt_fft ntt_fft
* @{ @ingroup bliss_p * @{ @ingroup bliss_p
*/ */
#ifndef BLISS_FFT_H_ #ifndef NTT_FFT_H_
#define BLISS_FFT_H_ #define NTT_FFT_H_
#include "bliss_fft_params.h" #include "ntt_fft_params.h"
#include <library.h> #include <library.h>
typedef struct bliss_fft_t bliss_fft_t; typedef struct ntt_fft_t ntt_fft_t;
/** /**
* Implements a Number Theoretic Transform (NTT) via the FFT algorithm * Implements a Number Theoretic Transform (NTT) via the FFT algorithm
*/ */
struct bliss_fft_t { struct ntt_fft_t {
/** /**
* Get the size of the Number Theoretic Transform * Get the size of the Number Theoretic Transform
* *
* @result Transform size * @result Transform size
*/ */
uint16_t (*get_size)(bliss_fft_t *this); uint16_t (*get_size)(ntt_fft_t *this);
/** /**
* Get the prime modulus of the Number Theoretic Transform * Get the prime modulus of the Number Theoretic Transform
* *
* @result Prime modulus * @result Prime modulus
*/ */
uint16_t (*get_modulus)(bliss_fft_t *this); uint16_t (*get_modulus)(ntt_fft_t *this);
/** /**
* Compute the [inverse] NTT of a polynomial * Compute the [inverse] NTT of a polynomial
@@ -53,19 +53,19 @@ struct bliss_fft_t {
* @param b Coefficient of output polynomial * @param b Coefficient of output polynomial
* @param inverse TRUE if the inverse NTT has to be computed * @param inverse TRUE if the inverse NTT has to be computed
*/ */
void (*transform)(bliss_fft_t *this, uint32_t *a, uint32_t *b, bool inverse); void (*transform)(ntt_fft_t *this, uint32_t *a, uint32_t *b, bool inverse);
/** /**
* Destroy bliss_fft_t object * Destroy ntt_fft_t object
*/ */
void (*destroy)(bliss_fft_t *this); void (*destroy)(ntt_fft_t *this);
}; };
/** /**
* Create a bliss_fft_t object for a given FFT parameter set * Create a ntt_fft_t object for a given FFT parameter set
* *
* @param params FFT parameters * @param params FFT parameters
*/ */
bliss_fft_t *bliss_fft_create(bliss_fft_params_t *params); ntt_fft_t *ntt_fft_create(ntt_fft_params_t *params);
#endif /** BLISS_FFT_H_ @}*/ #endif /** NTT_FFT_H_ @}*/
@@ -13,7 +13,7 @@
* for more details. * for more details.
*/ */
#include "bliss_fft_params.h" #include "ntt_fft_params.h"
/** /**
* FFT twiddle factors in Montgomery form for q = 12289 and n = 1024 * FFT twiddle factors in Montgomery form for q = 12289 and n = 1024
@@ -491,7 +491,7 @@ static uint16_t rev_1024[] = {
255, 767, 511, 1023 255, 767, 511, 1023
}; };
bliss_fft_params_t bliss_fft_12289_1024 = { ntt_fft_params_t ntt_fft_12289_1024 = {
12289, 12287, 18, 3186, (1<<18)-1, 1024, 12277, 10, 12289, 12287, 18, 3186, (1<<18)-1, 1024, 12277, 10,
wr_12289_1024, wf_12289_1024, wi_12289_1024, 1, rev_1024 wr_12289_1024, wf_12289_1024, wi_12289_1024, 1, rev_1024
}; };
@@ -622,7 +622,7 @@ static uint16_t rev_512[] = {
255, 511 255, 511
}; };
bliss_fft_params_t bliss_fft_12289_512 = { ntt_fft_params_t ntt_fft_12289_512 = {
12289, 12287, 18, 3186, (1<<18)-1, 512, 12265, 9, 12289, 12287, 18, 3186, (1<<18)-1, 512, 12265, 9,
wr_12289_1024, wf_12289_1024, wi_12289_512, 2, rev_512 wr_12289_1024, wf_12289_1024, wi_12289_512, 2, rev_512
}; };
@@ -647,6 +647,6 @@ static uint16_t wi_17_8[] = { 15, 5, 13, 10, 9, 3, 1, 6 };
*/ */
static uint16_t rev_8[] = { 0, 4, 2, 6, 1, 5, 3, 7 }; static uint16_t rev_8[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
bliss_fft_params_t bliss_fft_17_8 = { ntt_fft_params_t ntt_fft_17_8 = {
17, 15, 5, 4, (1<<5)-1, 8, 15, 3, wr_17_8, wf_17_8, wi_17_8, 1, rev_8 17, 15, 5, 4, (1<<5)-1, 8, 15, 3, wr_17_8, wf_17_8, wi_17_8, 1, rev_8
}; };
@@ -14,21 +14,21 @@
*/ */
/** /**
* @defgroup bliss_fft_params bliss_fft_params * @defgroup ntt_fft_params ntt_fft_params
* @{ @ingroup bliss_p * @{ @ingroup ntt_p
*/ */
#ifndef BLISS_FFT_PARAMS_H_ #ifndef NTT_FFT_PARAMS_H_
#define BLISS_FFT_PARAMS_H_ #define NTT_FFT_PARAMS_H_
#include <library.h> #include <library.h>
typedef struct bliss_fft_params_t bliss_fft_params_t; typedef struct ntt_fft_params_t ntt_fft_params_t;
/** /**
* Defines the parameters for an NTT computed via the FFT algorithm * Defines the parameters for an NTT computed via the FFT algorithm
*/ */
struct bliss_fft_params_t { struct ntt_fft_params_t {
/** /**
* Prime modulus * Prime modulus
@@ -100,16 +100,16 @@ struct bliss_fft_params_t {
/** /**
* FFT parameters for q = 12289 and n = 1024 * FFT parameters for q = 12289 and n = 1024
*/ */
extern bliss_fft_params_t bliss_fft_12289_1024; extern ntt_fft_params_t ntt_fft_12289_1024;
/** /**
* FFT parameters for q = 12289 and n = 512 * FFT parameters for q = 12289 and n = 512
*/ */
extern bliss_fft_params_t bliss_fft_12289_512; extern ntt_fft_params_t ntt_fft_12289_512;
/** /**
* FFT parameters for q = 17 and n = 8 * FFT parameters for q = 17 and n = 8
*/ */
extern bliss_fft_params_t bliss_fft_17_8; extern ntt_fft_params_t ntt_fft_17_8;
#endif /** BLISS_FFT_PARAMS_H_ @}*/ #endif /** NTT_FFT_PARAMS_H_ @}*/
@@ -14,14 +14,14 @@
*/ */
/** /**
* @defgroup bliss_fft bliss_fft * @defgroup ntt_fft ntt_fft
* @{ @ingroup bliss_p * @{ @ingroup ntt_p
*/ */
#ifndef BLISS_REDUCE_H_ #ifndef NTT_REDUCE_H_
#define BLISS_REDUCE_H_ #define NTT_REDUCE_H_
#include "bliss_fft_params.h" #include "ntt_fft_params.h"
/** /**
* Montgomery Reduction * Montgomery Reduction
@@ -29,7 +29,7 @@
* Montgomery, P. L. Modular multiplication without trial division. * Montgomery, P. L. Modular multiplication without trial division.
* Mathematics of Computation 44, 170 (1985), 519521. * Mathematics of Computation 44, 170 (1985), 519521.
*/ */
static inline uint32_t bliss_mreduce(uint32_t x, bliss_fft_params_t *p) static inline uint32_t ntt_fft_mreduce(uint32_t x, ntt_fft_params_t *p)
{ {
uint32_t m, t; uint32_t m, t;
@@ -39,4 +39,4 @@ static inline uint32_t bliss_mreduce(uint32_t x, bliss_fft_params_t *p)
return (t < p->q) ? t : t - p->q; return (t < p->q) ? t : t - p->q;
} }
#endif /** BLISS_REDUCE_H_ @}*/ #endif /** NTT_REDUCE_H_ @}*/
@@ -0,0 +1 @@
ntt_fft_tests
@@ -0,0 +1,21 @@
TESTS = ntt_fft_tests
check_PROGRAMS = $(TESTS)
ntt_fft_tests_SOURCES = \
suites/test_ntt_fft.c \
ntt_fft_tests.h ntt_fft_tests.c
ntt_fft_tests_CFLAGS = \
-I$(top_srcdir)/src/libstrongswan \
-I$(top_srcdir)/src/libstrongswan/tests \
-I$(top_srcdir)/src/libstrongswan/math/libnttfft \
-DPLUGINDIR=\""$(abs_top_builddir)/src/libstrongswan/plugins\"" \
-DPLUGINS=\""${s_plugins}\"" \
@COVERAGE_CFLAGS@
ntt_fft_tests_LDFLAGS = @COVERAGE_LDFLAGS@
ntt_fft_tests_LDADD = \
$(top_builddir)/src/libstrongswan/libstrongswan.la \
$(top_builddir)/src/libstrongswan/tests/libtest.la \
../libnttfft.la
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2016 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program 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 General Public License
* for more details.
*/
#include <test_runner.h>
#include <library.h>
/* declare test suite constructors */
#define TEST_SUITE(x) test_suite_t* x();
#include "ntt_fft_tests.h"
#undef TEST_SUITE
static test_configuration_t tests[] = {
#define TEST_SUITE(x) \
{ .suite = x, },
#include "ntt_fft_tests.h"
{ .suite = NULL, }
};
static bool test_runner_init(bool init)
{
if (init)
{
char *plugins, *plugindir;
plugins = lib->settings->get_str(lib->settings,
"tests.load", PLUGINS);
plugindir = lib->settings->get_str(lib->settings,
"tests.plugindir", PLUGINDIR);
plugin_loader_add_plugindirs(plugindir, plugins);
if (!lib->plugins->load(lib->plugins, plugins))
{
return FALSE;
}
}
else
{
lib->processor->set_threads(lib->processor, 0);
lib->processor->cancel(lib->processor);
lib->plugins->unload(lib->plugins);
}
return TRUE;
}
int main(int argc, char *argv[])
{
return test_runner_run("ntt_fft", tests, test_runner_init);
}
@@ -0,0 +1,17 @@
/*
* Copyright (C) 2016 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program 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 General Public License
* for more details.
*/
TEST_SUITE(ntt_fft_suite_create)
@@ -15,20 +15,20 @@
#include "test_suite.h" #include "test_suite.h"
#include <bliss_fft.h> #include <ntt_fft.h>
#include <bliss_reduce.h> #include <ntt_fft_reduce.h>
#include <time.h> #include <time.h>
static bliss_fft_params_t *fft_params[] = { static ntt_fft_params_t *fft_params[] = {
&bliss_fft_17_8, &ntt_fft_17_8,
&bliss_fft_12289_512, &ntt_fft_12289_512,
&bliss_fft_12289_1024 &ntt_fft_12289_1024
}; };
START_TEST(test_bliss_fft_impulse) START_TEST(test_ntt_fft_impulse)
{ {
bliss_fft_t *fft; ntt_fft_t *fft;
uint16_t n = fft_params[_i]->n; uint16_t n = fft_params[_i]->n;
uint32_t rq = (1 << fft_params[_i]->rlog) % fft_params[_i]->q; uint32_t rq = (1 << fft_params[_i]->rlog) % fft_params[_i]->q;
uint32_t x[n], X[n]; uint32_t x[n], X[n];
@@ -40,7 +40,7 @@ START_TEST(test_bliss_fft_impulse)
} }
x[0] = 1; x[0] = 1;
fft = bliss_fft_create(fft_params[_i]); fft = ntt_fft_create(fft_params[_i]);
fft->transform(fft, x, X, FALSE); fft->transform(fft, x, X, FALSE);
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
@@ -57,9 +57,9 @@ START_TEST(test_bliss_fft_impulse)
} }
END_TEST END_TEST
START_TEST(test_bliss_fft_wrap) START_TEST(test_ntt_fft_wrap)
{ {
bliss_fft_t *fft; ntt_fft_t *fft;
uint16_t n = fft_params[_i]->n; uint16_t n = fft_params[_i]->n;
uint16_t q = fft_params[_i]->q; uint16_t q = fft_params[_i]->q;
uint32_t x[n],y[n], X[n], Y[n]; uint32_t x[n],y[n], X[n], Y[n];
@@ -70,7 +70,7 @@ START_TEST(test_bliss_fft_wrap)
x[i] = i; x[i] = i;
y[i] = 0; y[i] = 0;
} }
fft = bliss_fft_create(fft_params[_i]); fft = ntt_fft_create(fft_params[_i]);
ck_assert(fft->get_size(fft) == n); ck_assert(fft->get_size(fft) == n);
ck_assert(fft->get_modulus(fft) == q); ck_assert(fft->get_modulus(fft) == q);
fft->transform(fft, x, X, FALSE); fft->transform(fft, x, X, FALSE);
@@ -82,7 +82,7 @@ START_TEST(test_bliss_fft_wrap)
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
Y[i] = bliss_mreduce(X[i] * Y[i], fft_params[_i]); Y[i] = ntt_fft_mreduce(X[i] * Y[i], fft_params[_i]);
} }
fft->transform(fft, Y, Y, TRUE); fft->transform(fft, Y, Y, TRUE);
@@ -96,9 +96,9 @@ START_TEST(test_bliss_fft_wrap)
} }
END_TEST END_TEST
START_TEST(test_bliss_fft_speed) START_TEST(test_ntt_fft_speed)
{ {
bliss_fft_t *fft; ntt_fft_t *fft;
struct timespec start, stop; struct timespec start, stop;
int i, m, count = 10000; int i, m, count = 10000;
int n = fft_params[_i]->n; int n = fft_params[_i]->n;
@@ -108,7 +108,7 @@ START_TEST(test_bliss_fft_speed)
{ {
x[i] = i; x[i] = i;
} }
fft = bliss_fft_create(fft_params[_i]); fft = ntt_fft_create(fft_params[_i]);
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start); clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start);
for (m = 0; m < count; m++) for (m = 0; m < count; m++)
@@ -130,24 +130,24 @@ START_TEST(test_bliss_fft_speed)
} }
END_TEST END_TEST
Suite *bliss_fft_suite_create() Suite *ntt_fft_suite_create()
{ {
Suite *s; Suite *s;
TCase *tc; TCase *tc;
s = suite_create("bliss_fft"); s = suite_create("ntt_fft");
tc = tcase_create("impulse"); tc = tcase_create("impulse");
tcase_add_loop_test(tc, test_bliss_fft_impulse, 0, countof(fft_params)); tcase_add_loop_test(tc, test_ntt_fft_impulse, 0, countof(fft_params));
suite_add_tcase(s, tc); suite_add_tcase(s, tc);
tc = tcase_create("negative_wrap"); tc = tcase_create("negative_wrap");
tcase_add_loop_test(tc, test_bliss_fft_wrap, 0, countof(fft_params)); tcase_add_loop_test(tc, test_ntt_fft_wrap, 0, countof(fft_params));
suite_add_tcase(s, tc); suite_add_tcase(s, tc);
tc = tcase_create("speed"); tc = tcase_create("speed");
tcase_set_timeout(tc, 10); tcase_set_timeout(tc, 10);
tcase_add_loop_test(tc, test_bliss_fft_speed, 1, countof(fft_params)); tcase_add_loop_test(tc, test_ntt_fft_speed, 1, countof(fft_params));
suite_add_tcase(s, tc); suite_add_tcase(s, tc);
return s; return s;
+15 -6
View File
@@ -1,5 +1,6 @@
AM_CPPFLAGS = \ AM_CPPFLAGS = \
-I$(top_srcdir)/src/libstrongswan -I$(top_srcdir)/src/libstrongswan \
-I$(top_srcdir)/src/libstrongswan/math/libnttfft
AM_CFLAGS = \ AM_CFLAGS = \
$(PLUGIN_CFLAGS) \ $(PLUGIN_CFLAGS) \
@@ -7,9 +8,12 @@ AM_CFLAGS = \
# these file are also used by bliss_huffman # these file are also used by bliss_huffman
noinst_LTLIBRARIES = libbliss-params.la noinst_LTLIBRARIES = libbliss-params.la
libbliss_params_la_SOURCES = \ libbliss_params_la_SOURCES = \
bliss_param_set.h bliss_param_set.c \ bliss_param_set.h bliss_param_set.c
bliss_fft_params.h bliss_fft_params.c
libbliss_params_la_LIBADD = \
$(top_builddir)/src/libstrongswan/math/libnttfft/libnttfft.la
# these files are also used by the tests, we can't directly refer to them # these files are also used by the tests, we can't directly refer to them
# because of the subdirectory, which would cause distclean to fail # because of the subdirectory, which would cause distclean to fail
@@ -20,12 +24,14 @@ libbliss_la_SOURCES = \
bliss_signature.h bliss_signature.c \ bliss_signature.h bliss_signature.c \
bliss_utils.h bliss_utils.c \ bliss_utils.h bliss_utils.c \
bliss_bitpacker.h bliss_bitpacker.c \ bliss_bitpacker.h bliss_bitpacker.c \
bliss_reduce.h bliss_fft.h bliss_fft.c \
bliss_huffman_code.h bliss_huffman_code.c \ bliss_huffman_code.h bliss_huffman_code.c \
bliss_huffman_code_1.c bliss_huffman_code_3.c bliss_huffman_code_4.c \ bliss_huffman_code_1.c bliss_huffman_code_3.c bliss_huffman_code_4.c \
bliss_huffman_coder.h bliss_huffman_coder.c \ bliss_huffman_coder.h bliss_huffman_coder.c \
bliss_sampler.h bliss_sampler.c bliss_sampler.h bliss_sampler.c
libbliss_la_LIBADD = libbliss-params.la
libbliss_la_LIBADD = \
$(top_builddir)/src/libstrongswan/math/libnttfft/libnttfft.la \
libbliss-params.la
if MONOLITHIC if MONOLITHIC
noinst_LTLIBRARIES += libstrongswan-bliss.la noinst_LTLIBRARIES += libstrongswan-bliss.la
@@ -43,7 +49,10 @@ libstrongswan_bliss_la_LIBADD = libbliss.la
noinst_PROGRAMS = bliss_huffman noinst_PROGRAMS = bliss_huffman
bliss_huffman_SOURCES = bliss_huffman.c bliss_huffman_SOURCES = bliss_huffman.c
bliss_huffman_LDADD = -lm libbliss-params.la
bliss_huffman_LDADD = -lm \
$(top_builddir)/src/libstrongswan/math/libnttfft/libnttfft.la \
libbliss-params.la
recreate-bliss-huffman : bliss_huffman bliss_huffman_code.h recreate-bliss-huffman : bliss_huffman bliss_huffman_code.h
$(AM_V_GEN) \ $(AM_V_GEN) \
@@ -131,7 +131,7 @@ static bliss_param_set_t bliss_param_sets[] = {
.q2_inv = 6145, .q2_inv = 6145,
.n = 512, .n = 512,
.n_bits = 9, .n_bits = 9,
.fft_params = &bliss_fft_12289_512, .fft_params = &ntt_fft_12289_512,
.non_zero1 = 154, .non_zero1 = 154,
.non_zero2 = 0, .non_zero2 = 0,
.kappa = 23, .kappa = 23,
@@ -161,7 +161,7 @@ static bliss_param_set_t bliss_param_sets[] = {
.q2_inv = 6145, .q2_inv = 6145,
.n = 512, .n = 512,
.n_bits = 9, .n_bits = 9,
.fft_params = &bliss_fft_12289_512, .fft_params = &ntt_fft_12289_512,
.non_zero1 = 216, .non_zero1 = 216,
.non_zero2 = 16, .non_zero2 = 16,
.kappa = 30, .kappa = 30,
@@ -191,7 +191,7 @@ static bliss_param_set_t bliss_param_sets[] = {
.q2_inv = 6145, .q2_inv = 6145,
.n = 512, .n = 512,
.n_bits = 9, .n_bits = 9,
.fft_params = &bliss_fft_12289_512, .fft_params = &ntt_fft_12289_512,
.non_zero1 = 231, .non_zero1 = 231,
.non_zero2 = 31, .non_zero2 = 31,
.kappa = 39, .kappa = 39,
@@ -221,7 +221,7 @@ static bliss_param_set_t bliss_param_sets[] = {
.q2_inv = 6145, .q2_inv = 6145,
.n = 512, .n = 512,
.n_bits = 9, .n_bits = 9,
.fft_params = &bliss_fft_12289_512, .fft_params = &ntt_fft_12289_512,
.non_zero1 = 154, .non_zero1 = 154,
.non_zero2 = 0, .non_zero2 = 0,
.kappa = 23, .kappa = 23,
@@ -251,7 +251,7 @@ static bliss_param_set_t bliss_param_sets[] = {
.q2_inv = 6145, .q2_inv = 6145,
.n = 512, .n = 512,
.n_bits = 9, .n_bits = 9,
.fft_params = &bliss_fft_12289_512, .fft_params = &ntt_fft_12289_512,
.non_zero1 = 216, .non_zero1 = 216,
.non_zero2 = 16, .non_zero2 = 16,
.kappa = 30, .kappa = 30,
@@ -281,7 +281,7 @@ static bliss_param_set_t bliss_param_sets[] = {
.q2_inv = 6145, .q2_inv = 6145,
.n = 512, .n = 512,
.n_bits = 9, .n_bits = 9,
.fft_params = &bliss_fft_12289_512, .fft_params = &ntt_fft_12289_512,
.non_zero1 = 231, .non_zero1 = 231,
.non_zero2 = 31, .non_zero2 = 31,
.kappa = 39, .kappa = 39,
@@ -24,7 +24,7 @@
typedef enum bliss_param_set_id_t bliss_param_set_id_t; typedef enum bliss_param_set_id_t bliss_param_set_id_t;
typedef struct bliss_param_set_t bliss_param_set_t; typedef struct bliss_param_set_t bliss_param_set_t;
#include "bliss_fft_params.h" #include "ntt_fft_params.h"
#include "bliss_huffman_code.h" #include "bliss_huffman_code.h"
#include <library.h> #include <library.h>
@@ -93,7 +93,7 @@ struct bliss_param_set_t {
/** /**
* FFT parameters * FFT parameters
*/ */
bliss_fft_params_t *fft_params; ntt_fft_params_t *fft_params;
/** /**
* Number of [-1, +1] secret key coefficients * Number of [-1, +1] secret key coefficients
@@ -20,8 +20,8 @@
#include "bliss_sampler.h" #include "bliss_sampler.h"
#include "bliss_signature.h" #include "bliss_signature.h"
#include "bliss_bitpacker.h" #include "bliss_bitpacker.h"
#include "bliss_fft.h" #include "ntt_fft.h"
#include "bliss_reduce.h" #include "ntt_fft_reduce.h"
#include <crypto/mgf1/mgf1_bitspender.h> #include <crypto/mgf1/mgf1_bitspender.h>
#include <asn1/asn1.h> #include <asn1/asn1.h>
@@ -169,7 +169,7 @@ static void greedy_sc(int8_t *s1, int8_t *s2, int n, uint16_t *c_indices,
static bool sign_bliss(private_bliss_private_key_t *this, hash_algorithm_t alg, static bool sign_bliss(private_bliss_private_key_t *this, hash_algorithm_t alg,
chunk_t data, chunk_t *signature) chunk_t data, chunk_t *signature)
{ {
bliss_fft_t *fft; ntt_fft_t *fft;
bliss_signature_t *sig; bliss_signature_t *sig;
bliss_sampler_t *sampler = NULL; bliss_sampler_t *sampler = NULL;
rng_t *rng; rng_t *rng;
@@ -247,7 +247,7 @@ static bool sign_bliss(private_bliss_private_key_t *this, hash_algorithm_t alg,
y2 = z2; y2 = z2;
ud = z2d; ud = z2d;
fft = bliss_fft_create(this->set->fft_params); fft = ntt_fft_create(this->set->fft_params);
/* Use of the enhanced BLISS-B signature algorithm? */ /* Use of the enhanced BLISS-B signature algorithm? */
switch (this->set->id) switch (this->set->id)
@@ -343,7 +343,7 @@ static bool sign_bliss(private_bliss_private_key_t *this, hash_algorithm_t alg,
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
ay[i] = bliss_mreduce(this->Ar[i] * ay[i], this->set->fft_params); ay[i] = ntt_fft_mreduce(this->Ar[i] * ay[i], this->set->fft_params);
} }
fft->transform(fft, ay, ay, TRUE); fft->transform(fft, ay, ay, TRUE);
@@ -819,11 +819,11 @@ static uint32_t invert(private_bliss_private_key_t *this, uint32_t x)
} }
for (i = 1; i <= i_max; i++) for (i = 1; i <= i_max; i++)
{ {
x2 = bliss_mreduce(x2 * x2, this->set->fft_params); x2 = ntt_fft_mreduce(x2 * x2, this->set->fft_params);
if (q2 & (1 << i)) if (q2 & (1 << i))
{ {
x1 = bliss_mreduce(x1 * x2, this->set->fft_params); x1 = ntt_fft_mreduce(x1 * x2, this->set->fft_params);
} }
} }
@@ -1008,7 +1008,7 @@ bliss_private_key_t *bliss_private_key_gen(key_type_t type, va_list args)
uint16_t q; uint16_t q;
bool success = FALSE; bool success = FALSE;
bliss_param_set_t *set; bliss_param_set_t *set;
bliss_fft_t *fft; ntt_fft_t *fft;
rng_t *rng; rng_t *rng;
while (TRUE) while (TRUE)
@@ -1069,7 +1069,7 @@ bliss_private_key_t *bliss_private_key_gen(key_type_t type, va_list args)
this->set = set; this->set = set;
/* We derive the public key from the private key using the FFT */ /* We derive the public key from the private key using the FFT */
fft = bliss_fft_create(set->fft_params); fft = ntt_fft_create(set->fft_params);
/* Some vectors needed to derive the publi key */ /* Some vectors needed to derive the publi key */
S1 = malloc(n * sizeof(uint32_t)); S1 = malloc(n * sizeof(uint32_t));
@@ -1113,8 +1113,8 @@ bliss_private_key_t *bliss_private_key_gen(key_type_t type, va_list args)
break; break;
} }
this->Ar[i] = invert(this, S1[i]); this->Ar[i] = invert(this, S1[i]);
this->Ar[i] = bliss_mreduce(S2[i] * this->Ar[i], set->fft_params); this->Ar[i] = ntt_fft_mreduce(S2[i] * this->Ar[i], set->fft_params);
this->A[i] = bliss_mreduce(this->Ar[i], set->fft_params); this->A[i] = ntt_fft_mreduce(this->Ar[i], set->fft_params);
} }
} }
while (!success && trials < SECRET_KEY_TRIALS_MAX); while (!success && trials < SECRET_KEY_TRIALS_MAX);
@@ -1131,7 +1131,7 @@ bliss_private_key_t *bliss_private_key_gen(key_type_t type, va_list args)
{ {
DBG4(DBG_LIB, "%4d %3d %3d %5u %5u %5u %5u", DBG4(DBG_LIB, "%4d %3d %3d %5u %5u %5u %5u",
i, this->s1[i], this->s2[i], i, this->s1[i], this->s2[i],
bliss_mreduce(a[i], set->fft_params), ntt_fft_mreduce(a[i], set->fft_params),
S1[i], S2[i], this->A[i]); S1[i], S2[i], this->A[i]);
} }
} }
@@ -1265,8 +1265,8 @@ bliss_private_key_t *bliss_private_key_load(key_type_t type, va_list args)
for (i = 0; i < this->set->n; i++) for (i = 0; i < this->set->n; i++)
{ {
this->Ar[i] = bliss_mreduce(this->A[i] * r2, this->Ar[i] = ntt_fft_mreduce(this->A[i] * r2,
this->set->fft_params); this->set->fft_params);
} }
break; break;
case PRIV_KEY_SECRET1: case PRIV_KEY_SECRET1:
@@ -16,8 +16,8 @@
#include "bliss_public_key.h" #include "bliss_public_key.h"
#include "bliss_signature.h" #include "bliss_signature.h"
#include "bliss_bitpacker.h" #include "bliss_bitpacker.h"
#include "bliss_fft.h" #include "ntt_fft.h"
#include "bliss_reduce.h" #include "ntt_fft_reduce.h"
#include "bliss_utils.h" #include "bliss_utils.h"
#include <asn1/asn1.h> #include <asn1/asn1.h>
@@ -77,7 +77,7 @@ static bool verify_bliss(private_bliss_public_key_t *this, hash_algorithm_t alg,
chunk_t data_hash; chunk_t data_hash;
hasher_t *hasher; hasher_t *hasher;
hash_algorithm_t oracle_alg; hash_algorithm_t oracle_alg;
bliss_fft_t *fft; ntt_fft_t *fft;
bliss_signature_t *sig; bliss_signature_t *sig;
bool success = FALSE; bool success = FALSE;
@@ -126,12 +126,12 @@ static bool verify_bliss(private_bliss_public_key_t *this, hash_algorithm_t alg,
{ {
az[i] = z1[i] < 0 ? q + z1[i] : z1[i]; az[i] = z1[i] < 0 ? q + z1[i] : z1[i];
} }
fft = bliss_fft_create(this->set->fft_params); fft = ntt_fft_create(this->set->fft_params);
fft->transform(fft, az, az, FALSE); fft->transform(fft, az, az, FALSE);
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
az[i] = bliss_mreduce(this->Ar[i] * az[i], this->set->fft_params); az[i] = ntt_fft_mreduce(this->Ar[i] * az[i], this->set->fft_params);
} }
fft->transform(fft, az, az, TRUE); fft->transform(fft, az, az, TRUE);
@@ -393,8 +393,8 @@ bliss_public_key_t *bliss_public_key_load(key_type_t type, va_list args)
for (i = 0; i < this->set->n; i++) for (i = 0; i < this->set->n; i++)
{ {
this->Ar[i] = bliss_mreduce(this->A[i] * r2, this->Ar[i] = ntt_fft_mreduce(this->A[i] * r2,
this->set->fft_params); this->set->fft_params);
} }
break; break;
} }
@@ -3,7 +3,6 @@ TESTS = bliss_tests
check_PROGRAMS = $(TESTS) check_PROGRAMS = $(TESTS)
bliss_tests_SOURCES = \ bliss_tests_SOURCES = \
suites/test_bliss_fft.c \
suites/test_bliss_bitpacker.c \ suites/test_bliss_bitpacker.c \
suites/test_bliss_huffman.c \ suites/test_bliss_huffman.c \
suites/test_bliss_keys.c \ suites/test_bliss_keys.c \
@@ -15,6 +14,7 @@ bliss_tests_SOURCES = \
bliss_tests_CFLAGS = \ bliss_tests_CFLAGS = \
-I$(top_srcdir)/src/libstrongswan \ -I$(top_srcdir)/src/libstrongswan \
-I$(top_srcdir)/src/libstrongswan/tests \ -I$(top_srcdir)/src/libstrongswan/tests \
-I$(top_srcdir)/src/libstrongswan/math/libnttfft \
-I$(top_srcdir)/src/libstrongswan/plugins/bliss \ -I$(top_srcdir)/src/libstrongswan/plugins/bliss \
-DPLUGINDIR=\""$(abs_top_builddir)/src/libstrongswan/plugins\"" \ -DPLUGINDIR=\""$(abs_top_builddir)/src/libstrongswan/plugins\"" \
-DPLUGINS=\""${s_plugins}\"" \ -DPLUGINS=\""${s_plugins}\"" \
@@ -24,4 +24,5 @@ bliss_tests_LDFLAGS = @COVERAGE_LDFLAGS@
bliss_tests_LDADD = \ bliss_tests_LDADD = \
$(top_builddir)/src/libstrongswan/libstrongswan.la \ $(top_builddir)/src/libstrongswan/libstrongswan.la \
$(top_builddir)/src/libstrongswan/tests/libtest.la \ $(top_builddir)/src/libstrongswan/tests/libtest.la \
$(top_builddir)/src/libstrongswan/math/libnttfft/libnttfft.la \
../libbliss.la ../libbliss.la
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2014-2015 Andreas Steffen * Copyright (C) 2014-2016 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil * HSR Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
@@ -13,7 +13,6 @@
* for more details. * for more details.
*/ */
TEST_SUITE(bliss_fft_suite_create)
TEST_SUITE(bliss_bitpacker_suite_create) TEST_SUITE(bliss_bitpacker_suite_create)
TEST_SUITE(bliss_huffman_suite_create) TEST_SUITE(bliss_huffman_suite_create)
TEST_SUITE(bliss_keys_suite_create) TEST_SUITE(bliss_keys_suite_create)