From 629ba8254039945dbf1fe7126611ac8b72010429 Mon Sep 17 00:00:00 2001 From: J08nY Date: Wed, 13 Dec 2017 14:06:54 +0100 Subject: Add tests for brainpool generation, and fix it. --- src/exhaustive/brainpool.c | 11 +++-- test/src/exhaustive/test_ansi.c | 22 +++++----- test/src/exhaustive/test_brainpool.c | 83 ++++++++++++++++++++++++++++++++++++ test/src/gen/test_point.c | 4 -- test/src/util/test_bits.c | 13 ++++++ 5 files changed, 114 insertions(+), 19 deletions(-) create mode 100644 test/src/exhaustive/test_brainpool.c diff --git a/src/exhaustive/brainpool.c b/src/exhaustive/brainpool.c index 0e3fa83..5557bc8 100644 --- a/src/exhaustive/brainpool.c +++ b/src/exhaustive/brainpool.c @@ -4,11 +4,11 @@ */ #include "brainpool.h" -#include #include "gen/seed.h" #include "io/output.h" #include "util/bits.h" #include "util/str.h" +#include "util/memory.h" static seed_t *brainpool_new() { seed_t *result = seed_new(); @@ -20,9 +20,9 @@ static seed_t *brainpool_new() { static void seed_wv(seed_t *seed) { pari_sp ltop = avma; GEN L = utoi(cfg->bits); - seed->brainpool.v = itou(floorr(divis(subis(L, 1), 160))); + seed->brainpool.v = itou(gfloor(gdivgs(subis(L, 1), 160))); seed->brainpool.w = - itou(floorr(subis(subis(L, 160 * seed->brainpool.v), 1))); + itou(subis(subis(L, 160 * seed->brainpool.v), 1)); avma = ltop; } @@ -91,7 +91,7 @@ GENERATOR(brainpool_gen_seed_input) { seed->seed = bits_from_hex(str_is_hex(cstr)); seed_wv(seed); curve->seed = seed; - return INT_MIN; + return 1; } GENERATOR(brainpool_gen_equation) { @@ -112,6 +112,7 @@ GENERATOR(brainpool_gen_equation) { z = Fp_sqrtn(a, stoi(4), curve->field, NULL); if (z == NULL) { brainpool_update_seed(seed->seed); + avma = btop; continue; } seed->brainpool.seed_a = bits_copy(seed->seed); @@ -125,6 +126,7 @@ GENERATOR(brainpool_gen_equation) { if (!Fp_issquare(b, curve->field)) { brainpool_update_seed(seed->seed); bits_free(&seed->brainpool.seed_a); + avma = btop; continue; } seed->brainpool.seed_b = bits_copy(seed->seed); @@ -137,6 +139,7 @@ GENERATOR(brainpool_gen_equation) { brainpool_update_seed(seed->seed); bits_free(&seed->brainpool.seed_a); bits_free(&seed->brainpool.seed_b); + avma = btop; continue; } diff --git a/test/src/exhaustive/test_ansi.c b/test/src/exhaustive/test_ansi.c index 5b58102..bbbe37a 100644 --- a/test/src/exhaustive/test_ansi.c +++ b/test/src/exhaustive/test_ansi.c @@ -16,7 +16,7 @@ TestSuite(ansi, .init = io_setup, .fini = io_teardown); -Test(ansi, test_seed_random) { +Test(ansi, test_ansi_seed_random) { curve_t curve = {0}; cfg->bits = 256; int ret = ansi_gen_seed_random(&curve, NULL, OFFSET_SEED); @@ -27,7 +27,7 @@ Test(ansi, test_seed_random) { seed_free(&curve.seed); } -Test(ansi, test_seed_argument) { +Test(ansi, test_ansi_seed_argument) { curve_t curve = {0}; char *seed = "abcdefabcdefabcdefabcdefabcdefabcdefabcd"; cfg->seed = seed; @@ -43,7 +43,7 @@ Test(ansi, test_seed_argument) { seed_free(&curve.seed); } -Test(ansi, test_seed_argument_hex) { +Test(ansi, test_ansi_seed_argument_hex) { curve_t curve = {0}; char *seed = "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd"; cfg->seed = seed; @@ -59,7 +59,7 @@ Test(ansi, test_seed_argument_hex) { seed_free(&curve.seed); } -Test(ansi, test_seed_input) { +Test(ansi, test_ansi_seed_input) { curve_t curve = {0}; char *seed = "abcdefabcdefabcdefabcdefabcdefabcdefabcd"; cfg->bits = 256; @@ -75,7 +75,7 @@ Test(ansi, test_seed_input) { seed_free(&curve.seed); } -Test(ansi, test_seed_input_short) { +Test(ansi, test_ansi_seed_input_short) { curve_t curve = {0}; char *seed = "abcdef"; fprintf(write_in, "%s\n", seed); @@ -102,7 +102,7 @@ void prime_params_cleanup(struct criterion_test_params *ctp) { cr_free(params->b); } -ParameterizedTestParameters(ansi, test_seed_prime_examples) { +ParameterizedTestParameters(ansi, test_ansi_seed_prime_examples) { static struct prime_params params[7] = {{0}}; // Taken from ANSI X9.62 J.5.1 - J.5.3; p. 115 - 117 // clang-format off @@ -153,7 +153,7 @@ ParameterizedTestParameters(ansi, test_seed_prime_examples) { size_t nb_params = sizeof(params) / sizeof(struct prime_params); return cr_make_param_array(struct prime_params, params, nb_params, NULL); } -ParameterizedTest(struct prime_params *param, ansi, test_seed_prime_examples) { +ParameterizedTest(struct prime_params *param, ansi, test_ansi_seed_prime_examples) { cfg->bits = param->bits; cfg->field = FIELD_PRIME; cfg->seed = param->seed; @@ -164,7 +164,7 @@ ParameterizedTest(struct prime_params *param, ansi, test_seed_prime_examples) { int ret = ansi_gen_seed_argument(&curve, NULL, OFFSET_SEED); cr_assert_eq(ret, 1, ); - ret = ansi_gen_equation(&curve, NULL, OFFSET_SEED); + ret = ansi_gen_equation(&curve, NULL, OFFSET_B); cr_assert_eq(ret, 1, ); GEN expected_r = bits_to_i(bits_from_hex(param->r)); cr_assert(gequal(curve.seed->ansi.r, expected_r), ); @@ -188,7 +188,7 @@ void binary_params_cleanup(struct criterion_test_params *ctp) { cr_free(params->b); } -ParameterizedTestParameters(ansi, test_seed_binary_examples) { +ParameterizedTestParameters(ansi, test_ansi_seed_binary_examples) { static struct binary_params params[10] = {{0}}; // Taken from ANSI X9.62 J.4.1, J.4.3, J.4.5 and J.4.8; p. 107 - 113 // clang-format off @@ -253,7 +253,7 @@ ParameterizedTestParameters(ansi, test_seed_binary_examples) { binary_params_cleanup); } ParameterizedTest(struct binary_params *param, ansi, - test_seed_binary_examples) { + test_ansi_seed_binary_examples) { cfg->bits = param->bits; cfg->field = FIELD_BINARY; cfg->seed = param->seed; @@ -266,7 +266,7 @@ ParameterizedTest(struct binary_params *param, ansi, int ret = ansi_gen_seed_argument(&curve, NULL, OFFSET_SEED); cr_assert_eq(ret, 1, ); - ret = ansi_gen_equation(&curve, NULL, OFFSET_SEED); + ret = ansi_gen_equation(&curve, NULL, OFFSET_B); cr_assert_eq(ret, 1, ); GEN curve_b = field_elementi(curve.b); cr_assert(gequal(curve_b, expected_b), ); diff --git a/test/src/exhaustive/test_brainpool.c b/test/src/exhaustive/test_brainpool.c new file mode 100644 index 0000000..912d286 --- /dev/null +++ b/test/src/exhaustive/test_brainpool.c @@ -0,0 +1,83 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ + +#include +#include +#include "exhaustive/brainpool.h" +#include "test/io.h" +#include "gen/seed.h" +#include "util/bits.h" +#include "util/memory.h" + +TestSuite(brainpool, .init = io_setup, .fini = io_teardown); + +Test(brainpool, test_brainpool_seed_random) { + curve_t curve = {0}; + cfg->bits = 256; + int ret = brainpool_gen_seed_random(&curve, NULL, OFFSET_SEED); + + cr_assert_eq(ret, 1, ); + cr_assert_not_null(curve.seed, ); + + seed_free(&curve.seed); +} + +Test(brainpool, test_brainpool_seed_argument) { + curve_t curve = {0}; + char *seed = "abcdefabcdefabcdefabcdefabcdefabcdefabcd"; + cfg->seed = seed; + cfg->bits = 256; + int ret = brainpool_gen_seed_argument(&curve, NULL, OFFSET_SEED); + + cr_assert_eq(ret, 1, ); + cr_assert_not_null(curve.seed, ); + char *hex = bits_to_hex(curve.seed->seed); + cr_assert_str_eq(hex, seed, ); + + try_free(hex); + seed_free(&curve.seed); +} + +Test(brainpool, test_brainpool_seed_input) { + curve_t curve = {0}; + char *seed = "abcdefabcdefabcdefabcdefabcdefabcdefabcd"; + cfg->bits = 256; + fprintf(write_in, "%s\n", seed); + int ret = brainpool_gen_seed_input(&curve, NULL, OFFSET_SEED); + + cr_assert_eq(ret, 1, ); + cr_assert_not_null(curve.seed, ); + char *hex = bits_to_hex(curve.seed->seed); + cr_assert_str_eq(hex, seed, ); + + try_free(hex); + seed_free(&curve.seed); +} + +Test(brainpool, test_brainpool_seed_input_short) { + curve_t curve = {0}; + char *seed = "abcdef"; + fprintf(write_in, "%s\n", seed); + int ret = brainpool_gen_seed_input(&curve, NULL, OFFSET_SEED); + + cr_assert_eq(ret, 0, ); +} + +Test(brainpool, test_brainpool_equation) { + curve_t curve = {0}; + char *seed = "abcdefabcdefabcdefabcdefabcdefabcdefabcd"; + cfg->seed = seed; + cfg->bits = 256; + + brainpool_gen_seed_argument(&curve, NULL, OFFSET_SEED); + + char *prime = "caa002c44829499e8239f049bddcaca373258a175fa337a82e0521744392cff1"; + bits_t *prime_bits = bits_from_hex(prime); + curve.field = bits_to_i(prime_bits); + bits_free(&prime_bits); + + int ret = brainpool_gen_equation(&curve, NULL, OFFSET_B); + cr_assert_eq(ret, 1, ); +} \ No newline at end of file diff --git a/test/src/gen/test_point.c b/test/src/gen/test_point.c index e90bf9b..19869c5 100644 --- a/test/src/gen/test_point.c +++ b/test/src/gen/test_point.c @@ -5,7 +5,6 @@ #include #include "gen/point.h" -#include "test/default.h" #include "test/io.h" TestSuite(point, .init = io_setup, .fini = io_teardown); @@ -115,9 +114,7 @@ Test(point, test_points_all) { .order = stoi(27), .curve = e, .ngens = 1, .generators = gens}; GEN orders = mkvec3s(3, 9, 27); size_t npoints = 3; - printf("allgroups\n"); int ret = points_gen_allgroups(&curve, NULL, OFFSET_POINTS); - printf("after\n"); cr_assert_eq(ret, 1, "Points weren't generated."); cr_assert_eq(curve.npoints, npoints, "Incorrect number of points."); @@ -129,7 +126,6 @@ Test(point, test_points_all) { "Point has wrong order set."); cr_assert(gequal(point->order, gel(orders, i + 1)), "Point has wrong order."); - printf("%lu\n", i); } points_free_deep(&curve.points, 1); diff --git a/test/src/util/test_bits.c b/test/src/util/test_bits.c index 27da58b..8a356b0 100644 --- a/test/src/util/test_bits.c +++ b/test/src/util/test_bits.c @@ -40,6 +40,19 @@ Test(bits, test_bits_new_rand) { bits_free(&bits); } +Test(bits, test_bits_cpy) { + bits_t *bits = bits_new(10); + bits->bits[0] = 0b10101010; + bits->bits[1] = 0b11000000; + + bits_t *other = bits_new(5); + bits_cpy(other, bits); + cr_assert_eq(other->bitlen, bits->bitlen, ); + cr_assert_eq(other->allocated, bits->allocated, ); + cr_assert_eq(other->bits[0], bits->bits[0], ); + cr_assert_eq(other->bits[1], bits->bits[1], ); +} + Test(bits, test_bits_copy) { bits_t *bits = bits_new(10); bits->bits[0] = 0b10101010; -- cgit v1.2.3-70-g09d2