aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2017-09-19 11:13:42 +0200
committerJ08nY2017-09-19 11:13:42 +0200
commit6adddf44e235fa272d82e8dcde748d6e7a8db14a (patch)
treef2c78c14bc80169bb0af06188251cd24c6ed5a1e
parent38ab890296ec07317cdbfe7f71a443c46ec30dd2 (diff)
downloadecgen-6adddf44e235fa272d82e8dcde748d6e7a8db14a.tar.gz
ecgen-6adddf44e235fa272d82e8dcde748d6e7a8db14a.tar.zst
ecgen-6adddf44e235fa272d82e8dcde748d6e7a8db14a.zip
-rw-r--r--src/exhaustive/ansi.c27
-rw-r--r--src/exhaustive/ansi.h10
-rw-r--r--src/exhaustive/exhaustive.c7
-rw-r--r--src/gen/types.h19
-rw-r--r--test/src/exhaustive/test_ansi.c11
5 files changed, 54 insertions, 20 deletions
diff --git a/src/exhaustive/ansi.c b/src/exhaustive/ansi.c
index 0b7e325..30db530 100644
--- a/src/exhaustive/ansi.c
+++ b/src/exhaustive/ansi.c
@@ -1,5 +1,6 @@
#include <io/config.h>
+#include <gen/types.h>
#include "ansi.h"
#include "gen/seed.h"
#include "gen/field.h"
@@ -38,10 +39,10 @@ static void seed_hash(seed_t *seed) {
static void seed_tsh(seed_t *seed, const config_t *cfg) {
pari_sp ltop = avma;
- seed->t = utoi(cfg->bits);
- seed->s = floorr(rdivii(subis(seed->t, 1), stoi(160), DEFAULTPREC));
- seed->h = subii(seed->t, mulis(seed->s, 160));
- gerepileall(ltop, 3, &seed->t, &seed->s, &seed->h);
+ seed->ansi.t = utoi(cfg->bits);
+ seed->ansi.s = floorr(rdivii(subis(seed->ansi.t, 1), stoi(160), DEFAULTPREC));
+ seed->ansi.h = subii(seed->ansi.t, mulis(seed->ansi.s, 160));
+ gerepileall(ltop, 3, &seed->ansi.t, &seed->ansi.s, &seed->ansi.h);
}
GENERATOR(ansi_gen_seed_random) {
@@ -86,16 +87,19 @@ static bits_t *seed_process(seed_t *seed, const bits_t *first) {
bits_t *result = bits_copy(first);
- long is = itos(seed->s);
+ long is = itos(seed->ansi.s);
+ GEN seedi = bits_to_i(seed->seed);
GEN two_g = int2n(seed->seed->bitlen);
for (long i = 1; i <= is; ++i) {
pari_sp btop = avma;
- GEN inner = bits_to_i(seed->seed);
- inner = addis(inner, i);
+ GEN inner = addis(seedi, i);
inner = modii(inner, two_g);
bits_t *to_hash = bits_from_i(inner);
+ if (to_hash->bitlen < seed->seed->bitlen) {
+ bits_lengthenz(to_hash, seed->seed->bitlen - to_hash->bitlen);
+ }
unsigned char hashout[20];
bits_sha1(to_hash, hashout);
bits_t *Wi = bits_from_raw(hashout, 160);
@@ -110,9 +114,14 @@ static bits_t *seed_process(seed_t *seed, const bits_t *first) {
return result;
}
+UNROLL(ansi_unroll_seed) {
+ seed_free(&curve->seed);
+ return -1;
+}
+
static GENERATOR(ansi_gen_equation_fp) {
bits_t *c0 = bits_from_raw(curve->seed->hash20, 160);
- bits_shortenz(c0, 160 - itos(curve->seed->h));
+ bits_shortenz(c0, 160 - itos(curve->seed->ansi.h));
bits_t *W0 = bits_copy(c0);
SET_BIT(W0->bits, 0, 0);
@@ -124,7 +133,7 @@ static GENERATOR(ansi_gen_equation_fp) {
static GENERATOR(ansi_gen_equation_f2m) {
bits_t *b0 = bits_from_raw(curve->seed->hash20, 160);
- bits_shortenz(b0, 160 - itos(curve->seed->h));
+ bits_shortenz(b0, 160 - itos(curve->seed->ansi.h));
bits_t *b = seed_process(curve->seed, b0);
GEN ib = bits_to_i(b);
diff --git a/src/exhaustive/ansi.h b/src/exhaustive/ansi.h
index 8bd39fb..e395691 100644
--- a/src/exhaustive/ansi.h
+++ b/src/exhaustive/ansi.h
@@ -35,6 +35,16 @@ GENERATOR(ansi_gen_seed_input);
/**
* @brief
+ * @param curve
+ * @param cfg
+ * @param from
+ * @param to
+ * @return
+ */
+UNROLL(ansi_unroll_seed);
+
+/**
+ * @brief
* @param curve A curve_t being generated
* @param cfg An application config
* @param args unused
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c
index ff30ecb..4638827 100644
--- a/src/exhaustive/exhaustive.c
+++ b/src/exhaustive/exhaustive.c
@@ -2,6 +2,7 @@
* ecgen, tool for generating Elliptic curve domain parameters
* Copyright (C) 2017 J08nY
*/
+#include <io/config.h>
#include "exhaustive.h"
#include "anomalous.h"
#include "ansi.h"
@@ -142,7 +143,11 @@ static void exhaustive_ainit(arg_t **argss, const config_t *cfg) {
}
void exhaustive_uinit(unroll_t *unrolls, const config_t *cfg) {
- unrolls[OFFSET_SEED] = &unroll_skip;
+ if (cfg->from_seed) {
+ unrolls[OFFSET_SEED] = &ansi_unroll_seed;
+ } else {
+ unrolls[OFFSET_SEED] = &unroll_skip;
+ }
unrolls[OFFSET_FIELD] = &unroll_skip;
unrolls[OFFSET_A] = &unroll_skip;
unrolls[OFFSET_B] = &unroll_skip;
diff --git a/src/gen/types.h b/src/gen/types.h
index 3e47e38..8b53da7 100644
--- a/src/gen/types.h
+++ b/src/gen/types.h
@@ -30,9 +30,16 @@ typedef struct {
typedef struct seed_t {
bits_t *seed;
unsigned char *hash20;
- GEN t;
- GEN s;
- GEN h;
+ union {
+ struct {
+ GEN t;
+ GEN s;
+ GEN h;
+ } ansi;
+ struct {
+ bits_t *f;
+ } brainpool;
+ };
} seed_t;
/**
@@ -105,7 +112,7 @@ typedef struct {
* @return state diff
*/
#define GENERATOR(gen_name) \
- int gen_name(curve_t *curve, const config_t *cfg, arg_t *args)
+ int gen_name(curve_t *curve, const config_t *cfg, arg_t *args)
typedef GENERATOR((*gen_t));
@@ -118,8 +125,8 @@ typedef GENERATOR((*gen_t));
* @return
*/
#define UNROLL(unroll_name) \
- int unroll_name(curve_t *curve, const config_t *cfg, pari_sp from, \
- pari_sp to)
+ int unroll_name(curve_t *curve, const config_t *cfg, pari_sp from, \
+ pari_sp to)
typedef UNROLL((*unroll_t));
diff --git a/test/src/exhaustive/test_ansi.c b/test/src/exhaustive/test_ansi.c
index 6c6ccb3..996f3a5 100644
--- a/test/src/exhaustive/test_ansi.c
+++ b/test/src/exhaustive/test_ansi.c
@@ -189,6 +189,7 @@ void binary_params_cleanup(struct criterion_test_params *ctp) {
ParameterizedTestParameters(ansi, test_seed_binary_examples) {
static struct binary_params params[10] = {};
+ // Taken from ANSI X9.62 J.4.1, J.4.3, J.4.5 and J.4.8; p. 107 - 113
polynomial_t p163 = {163, 9, 3, 2};
params[0].bits = 163;
params[0].field = p163;
@@ -245,7 +246,7 @@ ParameterizedTestParameters(ansi, test_seed_binary_examples) {
params[9].b = cr_strdup("2472E2D0197C49363F1FE7F5B6DB075D52B6947D135D8CA445805D39BC345626089687742B6329E70680231988");
size_t nb_params = sizeof(params) / sizeof(struct binary_params);
- //nb_params = 2;
+ //nb_params = 1;
return cr_make_param_array(struct binary_params, params, nb_params, binary_params_cleanup);
}
ParameterizedTest(struct binary_params *param, ansi, test_seed_binary_examples) {
@@ -256,14 +257,16 @@ ParameterizedTest(struct binary_params *param, ansi, test_seed_binary_examples)
curve_t curve = {};
curve.field = poly_gen(&param->field);
+ GEN expected_b = bits_to_i(bits_from_hex(param->b));
+ bits_t *b = bits_from_i(expected_b);
+
int ret = ansi_gen_seed_argument(&curve, &cfg, NULL);
cr_assert_eq(ret, 1,);
- bits_t *b = bits_from_i(bits_to_i(bits_from_hex(param->b)));
+
ret = ansi_gen_equation(&curve, &cfg, NULL);
cr_assert_eq(ret, 1,);
GEN curve_b = field_elementi(curve.b);
- printf("\n******************************\n\n%lu\n%s\n%s\n********************\n", cfg.bits, bits_to_bin(b), bits_to_bin(bits_from_i(curve_b)));
- cr_assert(gequal(curve_b, bits_to_i(b)),);
+ cr_assert(gequal(curve_b, expected_b),);
bits_free(&b);