009aaf802d15487085ea96e1fb4e9654f3ce7782
[strongswan.git] / src / libstrongswan / plugins / bliss / tests / suites / test_bliss_fft.c
1 /*
2 * Copyright (C) 2014 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "test_suite.h"
17
18 #include <bliss_fft.h>
19
20 static bliss_fft_params_t *fft_params[] = {
21 &bliss_fft_17_8,
22 &bliss_fft_12289_512
23 };
24
25 START_TEST(test_bliss_fft_impulse)
26 {
27 bliss_fft_t *fft;
28 uint16_t n = fft_params[_i]->n;
29 uint32_t x[n], X[n];
30 int i;
31
32 for (i = 0; i < n; i++)
33 {
34 x[i] = 0;
35 }
36 x[0] = 1;
37
38 fft = bliss_fft_create(fft_params[_i]);
39 fft->transform(fft, x, X, FALSE);
40
41 for (i = 0; i < n; i++)
42 {
43 ck_assert(X[i] == 1);
44 }
45 fft->transform(fft, X, x, TRUE);
46
47 for (i = 0; i < n; i++)
48 {
49 ck_assert(x[i] == (i == 0));
50 }
51 fft->destroy(fft);
52 }
53 END_TEST
54
55 START_TEST(test_bliss_fft_wrap)
56 {
57 bliss_fft_t *fft;
58 uint16_t n = fft_params[_i]->n;
59 uint16_t q = fft_params[_i]->q;
60 uint32_t x[n],y[n], X[n], Y[n];
61 int i, j;
62
63 for (i = 0; i < n; i++)
64 {
65 x[i] = i;
66 y[i] = 0;
67 }
68 fft = bliss_fft_create(fft_params[_i]);
69 ck_assert(fft->get_size(fft) == n);
70 ck_assert(fft->get_modulus(fft) == q);
71 fft->transform(fft, x, X, FALSE);
72
73 for (j = 0; j < n; j++)
74 {
75 y[j] = 1;
76 fft->transform(fft, y, Y, FALSE);
77
78 for (i = 0; i < n; i++)
79 {
80 Y[i] = (X[i] * Y[i]) % q;
81 }
82 fft->transform(fft, Y, Y, TRUE);
83
84 for (i = 0; i < n; i++)
85 {
86 ck_assert(Y[i] == ( i < j ? q - n - i + j : i - j));
87 }
88 y[j] = 0;
89 }
90 fft->destroy(fft);
91 }
92 END_TEST
93
94 Suite *bliss_fft_suite_create()
95 {
96 Suite *s;
97 TCase *tc;
98
99 s = suite_create("bliss_fft");
100
101 tc = tcase_create("impulse");
102 tcase_add_loop_test(tc, test_bliss_fft_impulse, 0, countof(fft_params));
103 suite_add_tcase(s, tc);
104
105 tc = tcase_create("negative_wrap");
106 tcase_add_loop_test(tc, test_bliss_fft_wrap, 0, countof(fft_params));
107 suite_add_tcase(s, tc);
108
109 return s;
110 }