bliss: Fix monolithic build

This requires moving test files so that the Makefile for the tests can be
included after building libstrongswan, which requires the plugin when
building monolithically.  Due to this a static helper library is required
as directly referring to object files (or source files) is not possible.

It's also necessary to avoid any link-time dependency on libstrongswan in
bliss_huffman, to avoid circular dependencies (bliss_huffman -> libstrongswan
-> bliss -> bliss_huffman).
This commit is contained in:
Tobias Brunner 2014-12-05 16:36:13 +01:00 committed by Andreas Steffen
parent f55a03a2de
commit 700df23886
13 changed files with 123 additions and 100 deletions

View File

@ -1645,6 +1645,7 @@ AC_CONFIG_FILES([
src/libstrongswan/plugins/af_alg/Makefile
src/libstrongswan/plugins/ntru/Makefile
src/libstrongswan/plugins/bliss/Makefile
src/libstrongswan/plugins/bliss/tests/Makefile
src/libstrongswan/plugins/test_vectors/Makefile
src/libstrongswan/tests/Makefile
src/libhydra/Makefile

View File

@ -546,6 +546,13 @@ if MONOLITHIC
endif
endif
if USE_BLISS
SUBDIRS += plugins/bliss
if MONOLITHIC
libstrongswan_la_LIBADD += plugins/bliss/libstrongswan-bliss.la
endif
endif
if USE_TEST_VECTORS
SUBDIRS += plugins/test_vectors
if MONOLITHIC
@ -557,12 +564,6 @@ if MONOLITHIC
SUBDIRS += .
endif
SUBDIRS += tests
if USE_BLISS
SUBDIRS += plugins/bliss
if MONOLITHIC
libstrongswan_la_LIBADD += plugins/bliss/libstrongswan-bliss.la
SUBDIRS += plugins/bliss/tests
endif
endif

View File

@ -1,4 +1,3 @@
bliss_tests
bliss_huffman
bliss_huffman_code_1.c
bliss_huffman_code_3.c

View File

@ -5,75 +5,56 @@ AM_CFLAGS = \
$(PLUGIN_CFLAGS) \
@COVERAGE_CFLAGS@
if MONOLITHIC
noinst_LTLIBRARIES = libstrongswan-bliss.la
else
plugin_LTLIBRARIES = libstrongswan-bliss.la
endif
libstrongswan_bliss_la_SOURCES = \
bliss_plugin.h bliss_plugin.c \
# these file are also used by bliss_huffman
noinst_LTLIBRARIES = libbliss-params.la
libbliss_params_la_SOURCES = \
bliss_param_set.h bliss_param_set.c \
bliss_fft_params.h bliss_fft_params.c
# 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
noinst_LTLIBRARIES += libbliss.la
libbliss_la_SOURCES = \
bliss_private_key.h bliss_private_key.c \
bliss_public_key.h bliss_public_key.c \
bliss_signature.h bliss_signature.c \
bliss_utils.h bliss_utils.c \
bliss_bitpacker.h bliss_bitpacker.c \
bliss_fft.h bliss_fft.c \
bliss_fft_params.h bliss_fft_params.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_sampler.h bliss_sampler.c
libbliss_la_LIBADD = libbliss-params.la
CLEANFILES = \
bliss_huffman_code_1.c bliss_huffman_code_3.c bliss_huffman_code_4.c
if MONOLITHIC
noinst_LTLIBRARIES += libstrongswan-bliss.la
else
plugin_LTLIBRARIES = libstrongswan-bliss.la
endif
libstrongswan_bliss_la_SOURCES = \
bliss_plugin.h bliss_plugin.c
libstrongswan_bliss_la_LDFLAGS = -module -avoid-version
libstrongswan_bliss_la_LIBADD = libbliss.la
noinst_PROGRAMS = bliss_huffman
bliss_huffman_SOURCES = bliss_huffman.c
bliss_huffman_LDADD = \
$(top_builddir)/src/libstrongswan/libstrongswan.la -lm \
bliss_param_set.o bliss_fft_params.o
bliss_huffman_LDADD = -lm libbliss-params.la
bliss_huffman_code_1.c : bliss_huffman bliss_huffman_code.h
$(AM_V_GEN) \
./bliss_huffman 1 8 > $@
./bliss_huffman 1 8 > $@ 2>/dev/null
bliss_huffman_code_3.c : bliss_huffman bliss_huffman_code.h
$(AM_V_GEN) \
./bliss_huffman 3 16 > $@
./bliss_huffman 3 16 > $@ 2>/dev/null
bliss_huffman_code_4.c : bliss_huffman bliss_huffman_code.h
$(AM_V_GEN) \
./bliss_huffman 4 32 > $@
TESTS = bliss_tests
check_PROGRAMS = $(TESTS)
bliss_tests_SOURCES = \
suites/test_bliss_fft.c \
suites/test_bliss_bitpacker.c \
suites/test_bliss_sampler.c \
suites/test_bliss_sign.c \
bliss_fft_params.c \
bliss_fft.c \
bliss_private_key.c \
bliss_public_key.c \
bliss_param_set.c \
bliss_sampler.c \
bliss_signature.c \
bliss_utils.c \
bliss_bitpacker.c \
bliss_tests.h bliss_tests.c
bliss_tests_CFLAGS = \
-I$(top_srcdir)/src/libstrongswan \
-I$(top_srcdir)/src/libstrongswan/tests \
-DPLUGINDIR=\""$(abs_top_builddir)/src/libstrongswan/plugins\"" \
-DPLUGINS=\""${s_plugins}\"" \
@COVERAGE_CFLAGS@
bliss_tests_LDFLAGS = @COVERAGE_LDFLAGS@
bliss_tests_LDADD = \
$(top_builddir)/src/libstrongswan/libstrongswan.la \
$(top_builddir)/src/libstrongswan/tests/libtest.la
./bliss_huffman 4 32 > $@ 2>/dev/null

View File

@ -1,4 +1,5 @@
/*
* Copyright (C) 2014 Tobias Brunner
* Copyright (C) 2014 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
@ -33,6 +34,7 @@ struct tuple_t {
typedef struct node_t node_t;
struct node_t {
node_t *next;
node_t *l;
node_t *r;
tuple_t *tuple;
@ -119,8 +121,7 @@ static void write_code_tables(int bliss_type, int n_z1, int n_z2, node_t *nodes,
printf(" * Copyright (C) 2014 Andreas Steffen\n");
printf(" * HSR Hochschule fuer Technik Rapperswil\n");
printf(" *\n");
printf(" * Optimum Huffman code for %N signatures\n",
bliss_param_set_id_names, bliss_type);
printf(" * Optimum Huffman code for BLISS-X signatures\n");
printf(" *\n");
printf(" * This file has been automatically generated by the"
" bliss_huffman utility\n");
@ -182,6 +183,25 @@ static void destroy_node(node_t *node)
free(node);
}
static void remove_node(node_t *list, node_t **last, node_t *node)
{
node_t *current, *prev;
for (current = list->next, prev = list; current;
prev = current, current = current->next)
{
if (current == node)
{
prev->next = current->next;
if (*last == current)
{
*last = prev->next ?: prev;
}
break;
}
}
}
/**
* Generate a Huffman code for the optimum encoding of BLISS signatures
*/
@ -190,14 +210,13 @@ int main(int argc, char *argv[])
bliss_param_set_t *set;
int dx, bliss_type, depth = 1, groups, groups_left, pairs = 1;
int i_max = 9, k_max = 8, index_max = (2*k_max - 1) * i_max;
int i, i_top, k, k_top;
int i, i_top, k, k_top;
uint16_t index;
double p, p_z1[i_max], p_z2[k_max], x_z1[i_max], x_z2[k_max];
double t, x, x0, p_sum, entropy = 0, erf_i, erf_k, erf_0 = 0;
tuple_t *tuple, *tuples[index_max];
node_t *node, *node_l, *node_r, *nodes = NULL;
linked_list_t *node_list;
enumerator_t *enumerator;
node_t *node_list, *node_last;
if (argc < 2)
{
@ -211,10 +230,6 @@ int main(int argc, char *argv[])
fprintf(stderr, "%d code pairs with constant length\n\n", pairs);
groups_left = groups = pairs >> 1;
library_init(NULL, "bliss_huffman");
lib->plugins->load(lib->plugins, "bliss");
atexit(library_deinit);
bliss_type = atoi(argv[1]);
set = bliss_param_set_get_by_id(bliss_type);
if (!set)
@ -222,7 +237,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "bliss type %d unsupported\n");
exit(1);
}
t = 1/(sqrt(2) * set->sigma);
/* Probability distribution for z1 */
@ -261,7 +276,7 @@ int main(int argc, char *argv[])
for (k = 0; k < k_top; k++)
{
erf_k = erf(t*x) / 2;
p_z2[k] = (k == 0) ? 2*erf_k : erf_k - erf_0;
p_sum += (k == 0) ? p_z2[k] : 2*p_z2[k];
@ -275,18 +290,18 @@ int main(int argc, char *argv[])
{
p_z2[k] /= p_sum;
}
/* Print the probability distribution for z2 */
fprintf(stderr, " k p_z2[k] dx = %d\n", dx);
for (k = 1 - k_top; k < k_top; k++)
{
fprintf(stderr, "%2d %18.16f ",k, p_z2[abs(k)]);
if (k < 0)
{
fprintf(stderr, "%7.1f ..%7.1f\n", -x_z2[-k], -x_z2[-k-1]);
}
}
else if (k == 0)
{
fprintf(stderr, "%7.1f ..%7.1f\n", -x_z2[k], x_z2[k]);
@ -294,12 +309,13 @@ int main(int argc, char *argv[])
else
{
fprintf(stderr, "%7.1f ..%7.1f\n", x_z2[k-1], x_z2[k]);
}
}
}
fprintf(stderr, "\n");
/* Compute probabilities of tuples (z1, z2) */
node_list =linked_list_create();
INIT(node_list);
node_last = node_list;
fprintf(stderr, "(i, k) p\n");
p_sum =0;
index = 0;
@ -313,18 +329,19 @@ int main(int argc, char *argv[])
p_sum += p;
entropy += -log(p) * p;
tuple = malloc_thing(tuple_t);
node = malloc_thing(node_t);
tuple->z1 = i;
tuple->z2 = k;
tuple->index = index;
INIT(tuple,
.z1 = i,
.z2 = k,
.index = index,
);
tuples[index++] = tuple;
node->p = p;
node->tuple = tuple;
node->depth = 0;
node->r = NULL;
node->l = NULL;
node_list->insert_last(node_list, node);
INIT(node,
.p = p,
.tuple = tuple,
);
node_last->next = node;
node_last = node;
}
}
entropy /= log(2);
@ -332,12 +349,11 @@ int main(int argc, char *argv[])
p_sum, entropy, (int)(512 * entropy));
/* Build Huffman tree */
while (node_list->get_count(node_list) > 1)
while (node_list->next != node_last)
{
node_r = node_l = NULL;
enumerator = node_list->create_enumerator(node_list);
while (enumerator->enumerate(enumerator, &node))
for (node = node_list->next; node; node = node->next)
{
if (pairs > 0)
{
@ -363,23 +379,22 @@ int main(int argc, char *argv[])
node_l = node;
}
}
enumerator->destroy(enumerator);
node = malloc_thing(node_t);
node->l = node_l;
node->r = node_r;
node->p = node_l->p + node_r->p;
node->depth = 1 + max(node_l->depth, node_r->depth);
node->tuple = NULL;
INIT(node,
.l = node_l,
.r = node_r,
.p = node_l->p + node_r->p,
.depth = 1 + max(node_l->depth, node_r->depth),
.tuple = NULL,
);
print_node(node_r);
print_node(node_l);
fprintf(stderr, " %18.16f", node->p);
node_list->remove(node_list, node_l, NULL);
node_list->remove(node_list, node_r, NULL);
node_list->insert_last(node_list, node);
remove_node(node_list, &node_last, node_l);
remove_node(node_list, &node_last, node_r);
node_last->next = node;
node_last = node;
if (pairs > 0)
{
@ -394,15 +409,16 @@ int main(int argc, char *argv[])
depth++;
}
}
fprintf(stderr, " %3d\n\n", node_list->get_count(node_list));
fprintf(stderr, "\n\n");
}
node_list->remove_first(node_list, (void**)&nodes);
node_list->destroy(node_list);
nodes = node_list->next;
write_code_tables(bliss_type, i_top, k_top, nodes, tuples);
destroy_node(nodes);
destroy_node(node_list);
exit(0);
}

View File

@ -0,0 +1 @@
bliss_tests

View File

@ -0,0 +1,24 @@
TESTS = bliss_tests
check_PROGRAMS = $(TESTS)
bliss_tests_SOURCES = \
suites/test_bliss_fft.c \
suites/test_bliss_bitpacker.c \
suites/test_bliss_sampler.c \
suites/test_bliss_sign.c \
bliss_tests.h bliss_tests.c
bliss_tests_CFLAGS = \
-I$(top_srcdir)/src/libstrongswan \
-I$(top_srcdir)/src/libstrongswan/tests \
-I$(top_srcdir)/src/libstrongswan/plugins/bliss \
-DPLUGINDIR=\""$(abs_top_builddir)/src/libstrongswan/plugins\"" \
-DPLUGINS=\""${s_plugins}\"" \
@COVERAGE_CFLAGS@
bliss_tests_LDFLAGS = @COVERAGE_LDFLAGS@
bliss_tests_LDADD = \
$(top_builddir)/src/libstrongswan/libstrongswan.la \
$(top_builddir)/src/libstrongswan/tests/libtest.la \
../libbliss.la