diff options
author | Andreas Steffen <andreas.steffen@strongswan.org> | 2016-07-24 19:57:54 +0200 |
---|---|---|
committer | Andreas Steffen <andreas.steffen@strongswan.org> | 2016-07-29 12:36:15 +0200 |
commit | d305f251a53520db97c899ba1c1f40e239fb68fa (patch) | |
tree | 99c8096c3338f56c1220fca3c2953420e4f8badb /src/libstrongswan/math/libnttfft/tests | |
parent | 65f2ecb86ddb4fab2cdbc7ac4e9416ff4c26222c (diff) | |
download | strongswan-d305f251a53520db97c899ba1c1f40e239fb68fa.tar.bz2 strongswan-d305f251a53520db97c899ba1c1f40e239fb68fa.tar.xz |
Created libnttfft
This makes Number Theoretic Transforms (NTT) based on the efficient
Fast-Fourier-Transform (FFT) available to multiple plugins.
Diffstat (limited to 'src/libstrongswan/math/libnttfft/tests')
5 files changed, 253 insertions, 0 deletions
diff --git a/src/libstrongswan/math/libnttfft/tests/.gitignore b/src/libstrongswan/math/libnttfft/tests/.gitignore new file mode 100644 index 000000000..da0c7d56d --- /dev/null +++ b/src/libstrongswan/math/libnttfft/tests/.gitignore @@ -0,0 +1 @@ +ntt_fft_tests diff --git a/src/libstrongswan/math/libnttfft/tests/Makefile.am b/src/libstrongswan/math/libnttfft/tests/Makefile.am new file mode 100644 index 000000000..55e6fff94 --- /dev/null +++ b/src/libstrongswan/math/libnttfft/tests/Makefile.am @@ -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 diff --git a/src/libstrongswan/math/libnttfft/tests/ntt_fft_tests.c b/src/libstrongswan/math/libnttfft/tests/ntt_fft_tests.c new file mode 100644 index 000000000..71f566426 --- /dev/null +++ b/src/libstrongswan/math/libnttfft/tests/ntt_fft_tests.c @@ -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); +} diff --git a/src/libstrongswan/math/libnttfft/tests/ntt_fft_tests.h b/src/libstrongswan/math/libnttfft/tests/ntt_fft_tests.h new file mode 100644 index 000000000..200b5b087 --- /dev/null +++ b/src/libstrongswan/math/libnttfft/tests/ntt_fft_tests.h @@ -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) + diff --git a/src/libstrongswan/math/libnttfft/tests/suites/test_ntt_fft.c b/src/libstrongswan/math/libnttfft/tests/suites/test_ntt_fft.c new file mode 100644 index 000000000..3a8b02087 --- /dev/null +++ b/src/libstrongswan/math/libnttfft/tests/suites/test_ntt_fft.c @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2014-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_suite.h" + +#include <ntt_fft.h> +#include <ntt_fft_reduce.h> + +#include <time.h> + +static ntt_fft_params_t *fft_params[] = { + &ntt_fft_17_8, + &ntt_fft_12289_512, + &ntt_fft_12289_1024 +}; + +START_TEST(test_ntt_fft_impulse) +{ + ntt_fft_t *fft; + uint16_t n = fft_params[_i]->n; + uint32_t rq = (1 << fft_params[_i]->rlog) % fft_params[_i]->q; + uint32_t x[n], X[n]; + int i; + + for (i = 0; i < n; i++) + { + x[i] = 0; + } + x[0] = 1; + + fft = ntt_fft_create(fft_params[_i]); + fft->transform(fft, x, X, FALSE); + + for (i = 0; i < n; i++) + { + ck_assert(X[i] == rq); + } + fft->transform(fft, X, x, TRUE); + + for (i = 0; i < n; i++) + { + ck_assert(x[i] == (i == 0)); + } + fft->destroy(fft); +} +END_TEST + +START_TEST(test_ntt_fft_wrap) +{ + ntt_fft_t *fft; + uint16_t n = fft_params[_i]->n; + uint16_t q = fft_params[_i]->q; + uint32_t x[n],y[n], X[n], Y[n]; + int i, j; + + for (i = 0; i < n; i++) + { + x[i] = i; + y[i] = 0; + } + fft = ntt_fft_create(fft_params[_i]); + ck_assert(fft->get_size(fft) == n); + ck_assert(fft->get_modulus(fft) == q); + fft->transform(fft, x, X, FALSE); + + for (j = 0; j < n; j++) + { + y[j] = 1; + fft->transform(fft, y, Y, FALSE); + + for (i = 0; i < n; i++) + { + Y[i] = ntt_fft_mreduce(X[i] * Y[i], fft_params[_i]); + } + fft->transform(fft, Y, Y, TRUE); + + for (i = 0; i < n; i++) + { + ck_assert(Y[i] == ( i < j ? q - n - i + j : i - j)); + } + y[j] = 0; + } + fft->destroy(fft); +} +END_TEST + +START_TEST(test_ntt_fft_speed) +{ + ntt_fft_t *fft; + struct timespec start, stop; + int i, m, count = 10000; + int n = fft_params[_i]->n; + uint32_t x[n], X[n]; + + for (i = 0; i < n; i++) + { + x[i] = i; + } + fft = ntt_fft_create(fft_params[_i]); + + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start); + for (m = 0; m < count; m++) + { + fft->transform(fft, x, X, FALSE); + fft->transform(fft, X, x, TRUE); + } + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &stop); + + DBG0(DBG_LIB, "%d FFT-%d loops in %d ms\n", count, n, + (stop.tv_nsec - start.tv_nsec) / 1000000 + + (stop.tv_sec - start.tv_sec) * 1000); + + for (i = 0; i < n; i++) + { + ck_assert(x[i] == i); + } + fft->destroy(fft); +} +END_TEST + +Suite *ntt_fft_suite_create() +{ + Suite *s; + TCase *tc; + + s = suite_create("ntt_fft"); + + tc = tcase_create("impulse"); + tcase_add_loop_test(tc, test_ntt_fft_impulse, 0, countof(fft_params)); + suite_add_tcase(s, tc); + + tc = tcase_create("negative_wrap"); + tcase_add_loop_test(tc, test_ntt_fft_wrap, 0, countof(fft_params)); + suite_add_tcase(s, tc); + + tc = tcase_create("speed"); + tcase_set_timeout(tc, 10); + tcase_add_loop_test(tc, test_ntt_fft_speed, 1, countof(fft_params)); + suite_add_tcase(s, tc); + + return s; +} |