diff options
| -rw-r--r-- | src/exhaustive/ansi.c | 100 | ||||
| -rw-r--r-- | src/gen/seed.c | 6 | ||||
| -rw-r--r-- | src/gen/types.h | 4 | ||||
| -rw-r--r-- | test/src/exhaustive/test_ansi.c | 15 |
4 files changed, 70 insertions, 55 deletions
diff --git a/src/exhaustive/ansi.c b/src/exhaustive/ansi.c index 0477227..0b7e325 100644 --- a/src/exhaustive/ansi.c +++ b/src/exhaustive/ansi.c @@ -2,6 +2,7 @@ #include <io/config.h> #include "ansi.h" #include "gen/seed.h" +#include "gen/field.h" #include "util/bits.h" #include "util/memory.h" #include "io/output.h" @@ -35,52 +36,19 @@ static void seed_hash(seed_t *seed) { bits_sha1(seed->seed, seed->hash20); } -static void seed_W(seed_t *seed, const config_t *cfg) { +static void seed_tsh(seed_t *seed, const config_t *cfg) { pari_sp ltop = avma; - GEN t = utoi(cfg->bits); - GEN s = floorr(rdivii(subis(t, 1), stoi(160), DEFAULTPREC)); - GEN h = subii(t, mulis(s, 160)); - pari_printf("bits = %lu, t = %Pi, s = %Pi, h = %Pi\n", cfg->bits, t, s, h); - - bits_t *c0 = bits_from_raw(seed->hash20, 160); - printf("H = %s, len = %lu alloc = %lu\n", bits_to_hex(c0), c0->bitlen, - c0->allocated); - bits_shortenz(c0, 160 - itos(h)); - printf("c0 = %s\n", bits_to_hex(c0)); - - bits_t *W0 = bits_copy(c0); - SET_BIT(W0->bits, 0, 0); - - long is = itos(s); - seed->W = bits_copy(W0); - GEN two_g = int2n(seed->seed->bitlen); - for (long i = 1; i <= is; ++i) { - printf("doing i = %li\n", i); - pari_sp btop = avma; - GEN inner = bits_to_i(seed->seed); - inner = addis(inner, i); - inner = modii(inner, two_g); - - bits_t *to_hash = bits_from_i(inner); - unsigned char hashout[20]; - bits_sha1(to_hash, hashout); - bits_t *Wi = bits_from_raw(hashout, 160); - bits_concatz(seed->W, Wi, NULL); - bits_free(&to_hash); - bits_free(&Wi); - avma = btop; - } - - bits_free(&c0); - bits_free(&W0); - avma = ltop; + 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); } GENERATOR(ansi_gen_seed_random) { seed_t *seed = seed_new(); seed->seed = bits_from_i(random_int(160)); seed_hash(seed); - seed_W(seed, cfg); + seed_tsh(seed, cfg); curve->seed = seed; return 1; } @@ -89,7 +57,7 @@ GENERATOR(ansi_gen_seed_argument) { seed_t *seed = seed_new(); seed->seed = seed_stoi(cfg->seed); seed_hash(seed); - seed_W(seed, cfg); + seed_tsh(seed, cfg); curve->seed = seed; return 1; } @@ -108,17 +76,65 @@ GENERATOR(ansi_gen_seed_input) { seed_t *seed = seed_new(); seed->seed = seed_stoi(cstr); seed_hash(seed); - seed_W(seed, cfg); + seed_tsh(seed, cfg); curve->seed = seed; return 1; } +static bits_t *seed_process(seed_t *seed, const bits_t *first) { + pari_sp ltop = avma; + + bits_t *result = bits_copy(first); + + long is = itos(seed->s); + 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); + inner = modii(inner, two_g); + + bits_t *to_hash = bits_from_i(inner); + unsigned char hashout[20]; + bits_sha1(to_hash, hashout); + bits_t *Wi = bits_from_raw(hashout, 160); + bits_concatz(result, Wi, NULL); + + bits_free(&to_hash); + bits_free(&Wi); + avma = btop; + } + + avma = ltop; + return result; +} + 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_t *W0 = bits_copy(c0); + SET_BIT(W0->bits, 0, 0); + + bits_t *W = seed_process(curve->seed, W0); + return 0; } static GENERATOR(ansi_gen_equation_f2m) { - return 0; + bits_t *b0 = bits_from_raw(curve->seed->hash20, 160); + bits_shortenz(b0, 160 - itos(curve->seed->h)); + + bits_t *b = seed_process(curve->seed, b0); + GEN ib = bits_to_i(b); + if (gequal0(ib)) { + return -3; + } + GEN a = random_int(cfg->bits); + curve->a = field_ielement(curve->field, a); + curve->b = field_ielement(curve->field, ib); + return 1; } GENERATOR(ansi_gen_equation) { diff --git a/src/gen/seed.c b/src/gen/seed.c index 8d6c7cf..a962d03 100644 --- a/src/gen/seed.c +++ b/src/gen/seed.c @@ -19,9 +19,6 @@ seed_t *seed_copy(const seed_t *src, seed_t *dest) { if (src->hash20) { dest->hash20 = try_memdup(src->hash20, 20); } - if (src->W) { - dest->W = bits_copy(src->W); - } return dest; } @@ -47,9 +44,6 @@ void seed_free(seed_t **seed) { if ((*seed)->hash20) { try_free((*seed)->hash20); } - if ((*seed)->W) { - bits_free(&(*seed)->W); - } try_free(*seed); *seed = NULL; } diff --git a/src/gen/types.h b/src/gen/types.h index b5de2db..3e47e38 100644 --- a/src/gen/types.h +++ b/src/gen/types.h @@ -30,7 +30,9 @@ typedef struct { typedef struct seed_t { bits_t *seed; unsigned char *hash20; - bits_t *W; + GEN t; + GEN s; + GEN h; } seed_t; /** diff --git a/test/src/exhaustive/test_ansi.c b/test/src/exhaustive/test_ansi.c index de96b93..6c6ccb3 100644 --- a/test/src/exhaustive/test_ansi.c +++ b/test/src/exhaustive/test_ansi.c @@ -5,10 +5,11 @@ #include <criterion/criterion.h> #include <criterion/parameterized.h> -#include <gen/types.h> +#include "gen/types.h" #include "math/poly.h" #include "exhaustive/ansi.h" #include "gen/seed.h" +#include "gen/field.h" #include "test/default.h" #include "test/memory.h" #include "test/input.h" @@ -244,7 +245,7 @@ ParameterizedTestParameters(ansi, test_seed_binary_examples) { params[9].b = cr_strdup("2472E2D0197C49363F1FE7F5B6DB075D52B6947D135D8CA445805D39BC345626089687742B6329E70680231988"); size_t nb_params = sizeof(params) / sizeof(struct binary_params); - //size_t nb_params = 1; + //nb_params = 2; return cr_make_param_array(struct binary_params, params, nb_params, binary_params_cleanup); } ParameterizedTest(struct binary_params *param, ansi, test_seed_binary_examples) { @@ -257,11 +258,13 @@ ParameterizedTest(struct binary_params *param, ansi, test_seed_binary_examples) int ret = ansi_gen_seed_argument(&curve, &cfg, NULL); cr_assert_eq(ret, 1,); - bits_t *b = bits_from_hex(param->b); + 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)),); - //TODO: this is not a correct comparison, need to store c0 from the algo as well. - printf("%s %s\n", bits_to_hex(curve.seed->W), param->b); - cr_assert(bits_eq(b, curve.seed->W),); bits_free(&b); seed_free(&curve.seed); |
