diff options
| author | J08nY | 2017-09-19 11:13:42 +0200 |
|---|---|---|
| committer | J08nY | 2017-09-19 11:13:42 +0200 |
| commit | 6adddf44e235fa272d82e8dcde748d6e7a8db14a (patch) | |
| tree | f2c78c14bc80169bb0af06188251cd24c6ed5a1e /src | |
| parent | 38ab890296ec07317cdbfe7f71a443c46ec30dd2 (diff) | |
| download | ecgen-6adddf44e235fa272d82e8dcde748d6e7a8db14a.tar.gz ecgen-6adddf44e235fa272d82e8dcde748d6e7a8db14a.tar.zst ecgen-6adddf44e235fa272d82e8dcde748d6e7a8db14a.zip | |
Fix ANSI X9.62 binary field generation method.
Diffstat (limited to 'src')
| -rw-r--r-- | src/exhaustive/ansi.c | 27 | ||||
| -rw-r--r-- | src/exhaustive/ansi.h | 10 | ||||
| -rw-r--r-- | src/exhaustive/exhaustive.c | 7 | ||||
| -rw-r--r-- | src/gen/types.h | 19 |
4 files changed, 47 insertions, 16 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)); |
