diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/exhaustive/exhaustive.c | 69 | ||||
| -rw-r--r-- | src/exhaustive/exhaustive.h | 3 | ||||
| -rw-r--r-- | src/gen/hex.c | 57 | ||||
| -rw-r--r-- | src/invalid/invalid.c | 13 | ||||
| -rw-r--r-- | src/invalid/invalid_thread.c | 2 | ||||
| -rw-r--r-- | src/io/cli.c | 33 | ||||
| -rw-r--r-- | src/misc/config.h | 8 |
7 files changed, 126 insertions, 59 deletions
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c index 0122a59..ea1dc63 100644 --- a/src/exhaustive/exhaustive.c +++ b/src/exhaustive/exhaustive.c @@ -10,6 +10,7 @@ #include "gen/equation.h" #include "gen/field.h" #include "gen/gens.h" +#include "gen/hex.h" #include "gen/order.h" #include "gen/point.h" #include "gen/seed.h" @@ -23,9 +24,14 @@ void exhaustive_clear(exhaustive_t *setup) { check_free(&setup->validators[i]); } } - if (setup->argss) { + if (setup->gen_argss) { for (size_t i = 0; i < OFFSET_END; ++i) { - arg_free(&setup->argss[i]); + arg_free(&setup->gen_argss[i]); + } + } + if (setup->check_argss) { + for (size_t i = 0; i < OFFSET_END; ++i) { + arg_free(&setup->check_argss[i]); } } } @@ -145,9 +151,14 @@ static void exhaustive_ginit(gen_f *generators) { static void exhaustive_cinit(check_t **validators) { check_t *curve_check = check_new(curve_check_nonzero, NULL); validators[OFFSET_CURVE] = curve_check; + + if (cfg->hex_check) { + check_t *hex_check = check_new(hex_check_param, NULL); + validators[OFFSET_POINTS] = hex_check; + } } -static void exhaustive_ainit(arg_t **argss) { +static void exhaustive_ainit(arg_t **gen_argss, arg_t **check_argss) { if (cfg->method == METHOD_ANOMALOUS) { arg_t *field_arg = arg_new(); arg_t *eq_arg = arg_new(); @@ -158,14 +169,14 @@ static void exhaustive_ainit(arg_t **argss) { eq_arg->args = i; eq_arg->nargs = 1; eq_arg->allocd = i; - argss[OFFSET_FIELD] = field_arg; - argss[OFFSET_B] = eq_arg; + gen_argss[OFFSET_FIELD] = field_arg; + gen_argss[OFFSET_B] = eq_arg; } if (cfg->points.type == POINTS_RANDOM) { arg_t *points_arg = arg_new(); points_arg->args = &cfg->points.amount; points_arg->nargs = 1; - argss[OFFSET_POINTS] = points_arg; + gen_argss[OFFSET_POINTS] = points_arg; } if (cfg->cofactor) { arg_t *order_arg = arg_new(); @@ -174,8 +185,15 @@ static void exhaustive_ainit(arg_t **argss) { order_arg->nargs = 1; gens_arg->args = &cfg->cofactor_bound; gens_arg->nargs = 1; - argss[OFFSET_ORDER] = order_arg; - argss[OFFSET_GENERATORS] = gens_arg; + gen_argss[OFFSET_ORDER] = order_arg; + gen_argss[OFFSET_GENERATORS] = gens_arg; + } + + if (cfg->hex_check) { + arg_t *point_arg = arg_new(); + point_arg->args = cfg->hex_check; + point_arg->nargs = 1; + check_argss[OFFSET_POINTS] = point_arg; } } @@ -203,10 +221,6 @@ int exhaustive_gen_retry(curve_t *curve, const exhaustive_t *setup, if (start_offset > end_offset) { return 0; } - gen_f *generators = setup->generators; - check_t **validators = setup->validators; - arg_t **argss = setup->argss; - unroll_f *unrolls = setup->unrolls; pari_sp stack_tops[OFFSET_END] = {0}; int gen_tries[OFFSET_END] = {0}; @@ -215,7 +229,9 @@ int exhaustive_gen_retry(curve_t *curve, const exhaustive_t *setup, while (state < end_offset) { stack_tops[state] = avma; - arg_t *arg = argss ? argss[state] : NULL; + arg_t *gen_arg = setup->gen_argss ? setup->gen_argss[state] : NULL; + arg_t *check_arg = + setup->check_argss ? setup->check_argss[state] : NULL; int diff; bool timeout = false; @@ -225,17 +241,14 @@ int exhaustive_gen_retry(curve_t *curve, const exhaustive_t *setup, timeout = true; } else { - diff = generators[state](curve, arg, (offset_e)state); + diff = setup->generators[state](curve, gen_arg, (offset_e)state); } timeout_stop(); - int new_state = state + diff; - if (new_state < start_offset) new_state = start_offset; - - if (diff > 0 && validators && validators[state]) { - check_t *validator = validators[state]; + if (diff > 0 && setup->validators && setup->validators[state]) { + check_t *validator = setup->validators[state]; for (size_t i = 0; i < validator->nchecks; ++i) { int new_diff = - validator->checks[i](curve, arg, (offset_e)state); + validator->checks[i](curve, check_arg, (offset_e)state); if (new_diff <= 0) { diff = new_diff; break; @@ -243,6 +256,9 @@ int exhaustive_gen_retry(curve_t *curve, const exhaustive_t *setup, } } + int new_state = state + diff; + if (new_state < start_offset) new_state = start_offset; + if (diff <= 0) { if (diff == INT_MIN || state + diff < 0) { fprintf(err, "Error generating a curve. state = %i\n", state); @@ -260,8 +276,9 @@ int exhaustive_gen_retry(curve_t *curve, const exhaustive_t *setup, // unroll for (int i = state; i > new_state;) { - if (unrolls && unrolls[i]) { - i += unrolls[i](curve, stack_tops[i], stack_tops[i - 1]); + if (setup->unrolls && setup->unrolls[i]) { + i += setup->unrolls[i](curve, stack_tops[i], + stack_tops[i - 1]); } else { --i; } @@ -299,7 +316,7 @@ int exhaustive_gen(curve_t *curve, const exhaustive_t *setup, static void exhaustive_init(exhaustive_t *setup) { exhaustive_ginit(setup->generators); exhaustive_cinit(setup->validators); - exhaustive_ainit(setup->argss); + exhaustive_ainit(setup->gen_argss, setup->check_argss); exhaustive_uinit(setup->unrolls); anomalous_init(); } @@ -315,13 +332,15 @@ int exhaustive_do() { debug_log_start("Starting Exhaustive method"); gen_f generators[OFFSET_END] = {NULL}; + arg_t *gen_argss[OFFSET_END] = {NULL}; check_t *validators[OFFSET_END] = {NULL}; - arg_t *argss[OFFSET_END] = {NULL}; + arg_t *check_argss[OFFSET_END] = {NULL}; unroll_f unrolls[OFFSET_END] = {NULL}; exhaustive_t setup = {.generators = generators, + .gen_argss = gen_argss, .validators = validators, - .argss = argss, + .check_argss = check_argss, .unrolls = unrolls}; exhaustive_init(&setup); diff --git a/src/exhaustive/exhaustive.h b/src/exhaustive/exhaustive.h index 720dab9..302a788 100644 --- a/src/exhaustive/exhaustive.h +++ b/src/exhaustive/exhaustive.h @@ -15,8 +15,9 @@ */ typedef struct { gen_f *generators; + arg_t **gen_argss; check_t **validators; - arg_t **argss; + arg_t **check_argss; unroll_f *unrolls; } exhaustive_t; diff --git a/src/gen/hex.c b/src/gen/hex.c index 2a6fea4..d643bfe 100644 --- a/src/gen/hex.c +++ b/src/gen/hex.c @@ -9,29 +9,24 @@ #include "util/memory.h" static char *hex_points(point_t *points[], size_t len) { - char *x[len]; - char *y[len]; + char *p[len]; for (size_t i = 0; i < len; ++i) { point_t *pt = points[i]; - bits_t *x_bits = bits_from_i(field_elementi(gel(pt->point, 1))); - bits_t *y_bits = bits_from_i(field_elementi(gel(pt->point, 2))); - x[i] = bits_to_hex(x_bits); - y[i] = bits_to_hex(y_bits); - bits_free(&x_bits); - bits_free(&y_bits); + GEN fx = field_elementi(gel(pt->point, 1)); + GEN fy = field_elementi(gel(pt->point, 2)); + p[i] = pari_sprintf("%P0#*x,%P0#*x,", cfg->hex_digits, fx, + cfg->hex_digits, fy); } - size_t total = 0; + size_t total = 1; for (size_t i = 0; i < len; ++i) { - total += strlen(x[i]) + strlen(y[i]); + total += strlen(p[i]); } char *result = try_calloc(total); for (size_t i = 0; i < len; ++i) { - strcat(result, x[i]); - strcat(result, y[i]); - try_free(x[i]); - try_free(y[i]); + strcat(result, p[i]); + pari_free(p[i]); } return result; } @@ -44,36 +39,39 @@ CHECK(hex_check_param) { for (; *p; ++p) *p = (char)tolower(*p); char *params[OFFSET_END] = {NULL}; + bool pari[OFFSET_END] = {false}; if (state >= OFFSET_SEED) { - params[OFFSET_SEED] = bits_to_hex(curve->seed->seed); + if (curve->seed && curve->seed->seed) { + params[OFFSET_SEED] = bits_to_hex(curve->seed->seed); + } } if (state >= OFFSET_FIELD) { if (cfg->field == FIELD_PRIME) { - bits_t *temp = bits_from_i(curve->field); - params[OFFSET_FIELD] = bits_to_hex(temp); - bits_free(&temp); + params[OFFSET_FIELD] = + pari_sprintf("%P0#*x", cfg->hex_digits, curve->field); + pari[OFFSET_FIELD] = true; } else if (cfg->field == FIELD_BINARY) { } } if (state >= OFFSET_A) { - bits_t *temp = bits_from_i(field_elementi(curve->a)); - params[OFFSET_A] = bits_to_hex(temp); - bits_free(&temp); + params[OFFSET_A] = + pari_sprintf("%P0#*x", cfg->hex_digits, field_elementi(curve->a)); + pari[OFFSET_A] = true; } if (state >= OFFSET_B) { - bits_t *temp = bits_from_i(field_elementi(curve->b)); - params[OFFSET_B] = bits_to_hex(temp); - bits_free(&temp); + params[OFFSET_B] = + pari_sprintf("%P0#*x", cfg->hex_digits, field_elementi(curve->b)); + pari[OFFSET_B] = true; } if (state >= OFFSET_ORDER) { - bits_t *temp = bits_from_i(curve->order); - params[OFFSET_ORDER] = bits_to_hex(temp); - bits_free(&temp); + params[OFFSET_ORDER] = + pari_sprintf("%P0#*x", cfg->hex_digits, curve->order); + pari[OFFSET_ORDER] = true; } if (state >= OFFSET_GENERATORS) { @@ -91,6 +89,11 @@ CHECK(hex_check_param) { result = 1; break; } + if (pari[i]) { + pari_free(params[i]); + } else { + try_free(params[i]); + } } } try_free(search_hex); diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c index a02c6cd..cacd459 100644 --- a/src/invalid/invalid.c +++ b/src/invalid/invalid.c @@ -3,6 +3,7 @@ * Copyright (C) 2017 J08nY */ #include "invalid.h" +#include <exhaustive/exhaustive.h> #include "exhaustive/arg.h" #include "exhaustive/check.h" #include "exhaustive/exhaustive.h" @@ -96,8 +97,9 @@ static size_t invalid_curves_single(const curve_t *curve, pari_ulong *primes, exhaustive_t *setup) { arg_t *invalid_argss[OFFSET_END] = {NULL}; exhaustive_t invalid_setup = {.generators = setup->generators, + .gen_argss = invalid_argss, .validators = setup->validators, - .argss = invalid_argss, + .check_argss = setup->check_argss, .unrolls = setup->unrolls}; curve_t *invalid = curve_new(); @@ -301,19 +303,22 @@ int invalid_do() { gen_f original_gens[OFFSET_END] = {NULL}; gen_f invalid_gens[OFFSET_END] = {NULL}; check_t *common_validators[OFFSET_END] = {NULL}; - arg_t *common_argss[OFFSET_END] = {NULL}; + arg_t *common_gen_argss[OFFSET_END] = {NULL}; + arg_t *common_check_argss[OFFSET_END] = {NULL}; unroll_f common_unrolls[OFFSET_END] = {NULL}; exhaustive_t original_setup = {.generators = original_gens, + .gen_argss = common_gen_argss, .validators = common_validators, - .argss = common_argss, + .check_argss = common_check_argss, .unrolls = common_unrolls}; invalid_init(&original_setup); invalid_original_ginit(original_gens); exhaustive_t invalid_setup = {.generators = invalid_gens, + .gen_argss = common_gen_argss, .validators = common_validators, - .argss = common_argss, + .check_argss = common_check_argss, .unrolls = common_unrolls}; invalid_invalid_ginit(invalid_gens); diff --git a/src/invalid/invalid_thread.c b/src/invalid/invalid_thread.c index 255c61f..ac1d2f2 100644 --- a/src/invalid/invalid_thread.c +++ b/src/invalid/invalid_thread.c @@ -14,7 +14,7 @@ void *invalid_thread(void *arg) { arg_t *invalid_argss[OFFSET_END] = {NULL}; exhaustive_t invalid_setup = {.generators = thread->setup->generators, .validators = thread->setup->validators, - .argss = invalid_argss, + .gen_argss = invalid_argss, .unrolls = thread->setup->unrolls}; curve_t *invalid = curve_new(); diff --git a/src/io/cli.c b/src/io/cli.c index 2aa2998..3c3dccd 100644 --- a/src/io/cli.c +++ b/src/io/cli.c @@ -35,7 +35,10 @@ enum opt_keys { OPT_THREADS, OPT_TSTACK, OPT_TIMEOUT, - OPT_ANOMALOUS + OPT_ANOMALOUS, + OPT_GPGEN, + OPT_GPCHECK, + OPT_HEXCHECK }; // clang-format off @@ -56,6 +59,9 @@ struct argp_option cli_options[] = { {"cofactor", OPT_COFACTOR, "BOUND", 0, "Generate a curve with cofactor up to BOUND.", 3}, {"koblitz", OPT_KOBLITZ, "A", OPTION_ARG_OPTIONAL,"Generate a Koblitz curve (a in {0, 1}, b = 1).", 3}, {"unique", OPT_UNIQUE, 0, 0, "Generate a curve with only one generator.", 3}, + {"gp-gen", OPT_GPGEN, "FUNC", 0, "Generate a curve param using a GP function. **NOT IMPLEMENTED**", 3}, + {"gp-check", OPT_GPCHECK, "FUNC", 0, "Check a generated curve param using a GP function. **NOT IMPLEMENTED**", 3}, + {"hex-check", OPT_HEXCHECK, "HEX", 0, "Check a generated curve param hex expansion for the HEX string.", 3}, {"points", OPT_POINTS, "TYPE", 0, "Generate points of given type (random/prime/all/nonprime/none).", 3}, {"count", OPT_COUNT, "COUNT", 0, "Generate multiple curves.", 3}, @@ -249,6 +255,31 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) { case OPT_UNIQUE: cfg->unique = true; break; + case OPT_GPGEN: + cfg->gp_gens[cfg->gp_gens_size++] = arg; + break; + case OPT_GPCHECK: + cfg->gp_checks[cfg->gp_checks_size++] = arg; + break; + case OPT_HEXCHECK: { + char *str_start = arg; + if (strlen(arg) > 2) { + if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X')) { + str_start = arg + 2; + } + } + char *p = str_start; + while (*p != 0) { + char c = *p++; + if (!isxdigit(c)) { + argp_failure( + state, 1, 0, + "Hex check argument contains non hex char '%c'", c); + } + } + cfg->hex_check = str_start; + break; + } case OPT_POINTS: { char *num_end; long amount = strtol(arg, &num_end, 10); diff --git a/src/misc/config.h b/src/misc/config.h index 859249c..a876b25 100644 --- a/src/misc/config.h +++ b/src/misc/config.h @@ -78,6 +78,14 @@ typedef struct { /** @brief Whether the curves should be uniquely generated (one generator). */ bool unique; + /** @brief The GP gen functions. */ + char *gp_gens[10]; + size_t gp_gens_size; + /** @brief The GP check functions. */ + char *gp_checks[10]; + size_t gp_checks_size; + /** @brief */ + char *hex_check; /** @brief What points to generate on the curves. */ struct points_s points; |
