diff options
| author | J08nY | 2017-03-01 11:33:16 +0100 |
|---|---|---|
| committer | J08nY | 2017-03-01 11:33:16 +0100 |
| commit | fdd2bbaf35270c5ab63bf3c601ab199f2092ab2c (patch) | |
| tree | 6bb9eec9d4608545ec6bc61056061f7e92d7c409 /src | |
| parent | 8c230aa00002b57d47160756a183d65fb895a7b4 (diff) | |
| download | ecgen-fdd2bbaf35270c5ab63bf3c601ab199f2092ab2c.tar.gz ecgen-fdd2bbaf35270c5ab63bf3c601ab199f2092ab2c.tar.zst ecgen-fdd2bbaf35270c5ab63bf3c601ab199f2092ab2c.zip | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/exhaustive/exhaustive.c | 56 | ||||
| -rw-r--r-- | src/invalid/invalid.c | 4 | ||||
| -rw-r--r-- | src/io/cli.c | 21 | ||||
| -rw-r--r-- | src/io/cli.h | 6 | ||||
| -rw-r--r-- | src/io/input.c | 3 | ||||
| -rw-r--r-- | src/math/arg.c | 19 | ||||
| -rw-r--r-- | src/math/arg.h | 17 | ||||
| -rw-r--r-- | src/math/equation.c | 20 | ||||
| -rw-r--r-- | src/math/field.c | 19 | ||||
| -rw-r--r-- | src/math/field.h | 9 | ||||
| -rw-r--r-- | src/math/point.c | 4 | ||||
| -rw-r--r-- | src/math/point.h | 2 |
12 files changed, 148 insertions, 32 deletions
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c index 2a839ff..d7acb82 100644 --- a/src/exhaustive/exhaustive.c +++ b/src/exhaustive/exhaustive.c @@ -10,6 +10,7 @@ #include "math/gens.h" #include "math/order.h" #include "math/point.h" +#include "math/arg.h" #include "seed.h" void exhaustive_ginit(gen_t *generators, config_t *config) { @@ -61,15 +62,33 @@ void exhaustive_ginit(gen_t *generators, config_t *config) { generators[OFFSET_FIELD] = &field_input; } - generators[OFFSET_POINTS] = &points_prime; + switch (config->points.type) { + case POINTS_RANDOM: + if (config->points.amount) { + generators[OFFSET_POINTS] = &points_random; + } else { + generators[OFFSET_POINTS] = &point_random; + } + break; + case POINTS_PRIME: generators[OFFSET_POINTS] = &points_prime; + break; + } } -void exhaustive_vinit(arg_t *argss[], config_t *config) { - // TODO implement when for example points_random is used... +void exhaustive_ainit(arg_t **argss, config_t *config) { + for (size_t i = 0; i < OFFSET_END; ++i) { + argss[i] = NULL; + } + if (config->points.type == POINTS_RANDOM) { + arg_t *points_arg = arg_new(); + points_arg->args = &config->points.amount; + points_arg->nargs = 1; + argss[OFFSET_POINTS] = points_arg; + } } int exhaustive_gen(curve_t *curve, config_t *config, gen_t generators[], - arg_t *argss[], int start_offset, int end_offset) { + arg_t *argss[], int start_offset, int end_offset) { if (start_offset == end_offset) { return 1; } @@ -83,7 +102,7 @@ int exhaustive_gen(curve_t *curve, config_t *config, gen_t generators[], tops[state - start_offset] = avma; int diff = - generators[state](curve, config, argss ? argss[state] : NULL); + generators[state](curve, config, argss ? argss[state] : NULL); state += diff; if (diff == INT_MIN) { @@ -112,23 +131,32 @@ int exhaustive_gen(curve_t *curve, config_t *config, gen_t generators[], return 1; } -void exhaustive_quit(void) { equation_quit(); } +void exhaustive_quit(arg_t *argss[]) { + equation_quit(); + for (size_t i = 0; i < OFFSET_END; ++i) { + if (argss[i]) { + arg_free(&(argss[i])); + } + } +} int exhaustive_do(config_t *cfg) { gen_t generators[OFFSET_END]; arg_t *argss[OFFSET_END]; exhaustive_ginit(generators, cfg); - exhaustive_vinit(argss, cfg); + exhaustive_ainit(argss, cfg); - curve_t *curve = curve_new(); - if (!exhaustive_gen(curve, cfg, generators, argss, OFFSET_SEED, - OFFSET_END)) { + for (long i = 0; i < cfg->count; ++i) { + curve_t *curve = curve_new(); + if (!exhaustive_gen(curve, cfg, generators, argss, OFFSET_SEED, + OFFSET_END)) { + curve_free(&curve); + return 1; + } + output_o(curve, cfg); curve_free(&curve); - return 1; } - output_o(curve, cfg); - curve_free(&curve); - exhaustive_quit(); + exhaustive_quit(argss); return 0; } diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c index 469b20c..f8ec9a2 100644 --- a/src/invalid/invalid.c +++ b/src/invalid/invalid.c @@ -82,7 +82,7 @@ size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes, } else { invalid_gen[OFFSET_GENERATORS] = &gens_any; } - invalid_gen[OFFSET_POINTS] = &points_primet; + invalid_gen[OFFSET_POINTS] = &points_trial; arg_t *invalid_argss[OFFSET_END]; @@ -126,7 +126,7 @@ size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes, if (total > 0) { // only pass small primes that divide the curve order and those // where we dont have a curve yet. - // this is passed to points_primet which uses trial division to find + // this is passed to points_trial which uses trial division to find // a point with given prime order. size_t j = 0; pari_ulong dprimes[total]; diff --git a/src/io/cli.c b/src/io/cli.c index 798d596..374b949 100644 --- a/src/io/cli.c +++ b/src/io/cli.c @@ -26,7 +26,8 @@ enum opt_keys { OPT_APPEND = 'a', OPT_VERBOSE = 'v', OPT_FP = 1, - OPT_F2M = 2, + OPT_F2M, + OPT_POINTS }; // clang-format off @@ -42,7 +43,8 @@ struct argp_option options[] = { {"order", OPT_ORDER, "ORDER", 0, "Generate a curve with given order (using Complex Multiplication).", 2}, {"koblitz", OPT_KOBLITZ, 0, 0, "Generate a Koblitz curve (a = 0).", 2}, {"unique", OPT_UNIQUE, 0, 0, "Generate a curve with only one generator.", 2}, - //{"count", OPT_COUNT, "COUNT", 0, "Generate multiple (COUNT) curves."}, + {"points", OPT_POINTS, "TYPE", 0, "Generate points of given type (random/prime).", 2}, + {"count", OPT_COUNT, "COUNT", 0, "Generate multiple curves.", 2}, {0, 0, 0, 0, "Input/Output options:", 3}, {"format", OPT_FORMAT, "FORMAT", 0, "Format to output in. One of [csv,json], default is json.", 3}, {"input", OPT_INPUT, "FILE", 0, "Input from file.", 3}, @@ -109,6 +111,21 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) { break; case OPT_UNIQUE: cfg->unique = true; break; + case OPT_POINTS: + if (arg) { + if (strstr(arg, "random")) { + long pts = strtol(arg, NULL, 10); + cfg->points.type = POINTS_RANDOM; + cfg->points.amount = (size_t)pts; + } else if (strstr(arg, "prime")) { + cfg->points.type = POINTS_PRIME; + } else { + argp_failure(state, 1, 0, "Unknow point type"); + } + } else { + argp_failure(state, 1, 0, "You have to specify what points you want."); + } + break; case OPT_SEED: cfg->from_seed = true; if (arg) { // ANSI X9.62 specifies seed as at least 160 bits in length. diff --git a/src/io/cli.h b/src/io/cli.h index dd3d791..5f668f8 100644 --- a/src/io/cli.h +++ b/src/io/cli.h @@ -18,6 +18,11 @@ extern struct argp_option options[]; enum field_e { FIELD_PRIME, FIELD_BINARY }; enum format_e { FORMAT_JSON, FORMAT_CSV }; +enum points_e { POINTS_RANDOM, POINTS_PRIME }; +struct points_s { + enum points_e type; + size_t amount; +}; typedef struct config_t { enum field_e field; @@ -34,6 +39,7 @@ typedef struct config_t { bool from_seed; char *seed; bool unique; + struct points_s points; char *datadir; enum format_e format; diff --git a/src/io/input.c b/src/io/input.c index 34aeb5a..3865d79 100644 --- a/src/io/input.c +++ b/src/io/input.c @@ -22,8 +22,7 @@ GEN input_i(const char *prompt, long bits) { free(line); return gen_m1; } - for (size_t i = 0, j = 0; (line[j] = line[i]); j += !isspace(line[i++])) - ; + for (size_t i = 0, j = 0; (line[j] = line[i]); j += !isspace(line[i++])); pari_sp ltop = avma; GEN in = strtoi(line); diff --git a/src/math/arg.c b/src/math/arg.c index 1a0d1ca..be3f92e 100644 --- a/src/math/arg.c +++ b/src/math/arg.c @@ -3,4 +3,21 @@ * Copyright (C) 2017 J08nY */ #include "arg.h" -#include "types.h" + + +arg_t *arg_new() { + arg_t *arg = pari_malloc(sizeof(arg_t)); + if (!arg) { + perror("Couldn't malloc."); + exit(1); + } + memset(arg, 0, sizeof(arg_t)); + return arg; +} + +void arg_free(arg_t **arg) { + if (*arg) { + pari_free(*arg); + *arg = NULL; + } +} diff --git a/src/math/arg.h b/src/math/arg.h index 65ea311..55e1b19 100644 --- a/src/math/arg.h +++ b/src/math/arg.h @@ -2,7 +2,24 @@ * ecgen, tool for generating Elliptic curve domain parameters * Copyright (C) 2017 J08nY */ +/** + * @file arg.h + */ #ifndef ECGEN_ARG_H #define ECGEN_ARG_H +#include "types.h" + +/** + * @brief + * @return + */ +arg_t *arg_new(); + +/** + * @brief + * @param arg + */ +void arg_free (arg_t **arg); + #endif // ECGEN_ARG_H diff --git a/src/math/equation.c b/src/math/equation.c index 2f57d03..45b849b 100644 --- a/src/math/equation.c +++ b/src/math/equation.c @@ -17,26 +17,28 @@ int a_input(curve_t *curve, config_t *config, arg_t *args) { avma = ltop; return 0; } - curve->a = gerepilecopy(ltop, inp); + curve->a = inp; // TODO change a to a field element here?. a t_INTMOD or a t_FFELT. return 1; } static GEN a = NULL; +static curve_t *curve_a = NULL; int a_once(curve_t *curve, config_t *config, arg_t *args) { - if (a) { + if (a && curve_a == curve) { curve->a = gcopy(a); return 1; } int inp = a_input(curve, config, args); - if (inp) { + if (inp > 0) { a = gclone(curve->a); + curve_a = curve; + return 1; } else { return 0; } - return 1; } int a_zero(curve_t *curve, config_t *config, arg_t *args) { @@ -66,26 +68,28 @@ int b_input(curve_t *curve, config_t *config, arg_t *args) { avma = ltop; return 0; } - curve->b = gerepilecopy(ltop, inp); + curve->b = inp; // TODO change b to a field element here?. a t_INTMOD or a t_FFELT. return 1; } static GEN b = NULL; +static curve_t* curve_b = NULL; int b_once(curve_t *curve, config_t *config, arg_t *args) { - if (b) { + if (b && curve_b == curve) { curve->b = gcopy(b); return 1; } int inp = b_input(curve, config, args); - if (inp) { + if (inp > 0) { b = gclone(curve->b); + curve_b = curve; + return 1; } else { return 0; } - return 1; } int b_zero(curve_t *curve, config_t *config, arg_t *args) { diff --git a/src/math/field.c b/src/math/field.c index 983aa09..2d6d358 100644 --- a/src/math/field.c +++ b/src/math/field.c @@ -90,6 +90,25 @@ int field_input(curve_t *curve, config_t *config, arg_t *args) { } } +static GEN field = NULL; +static curve_t *curve_field = NULL; + +int field_once(curve_t *curve, config_t *cfg, arg_t *args) { + if (field && curve_field == curve) { + curve->field = gcopy(field); + return 1; + } + + int inp = field_input(curve, cfg, args); + if (inp > 0) { + field = gclone(curve->field); + curve_field = curve; + return 1; + } else { + return 0; + } +} + GEN field_params(GEN field) { pari_sp ltop = avma; diff --git a/src/math/field.h b/src/math/field.h index 2d6d92a..0491a83 100644 --- a/src/math/field.h +++ b/src/math/field.h @@ -37,6 +37,15 @@ int field_random(curve_t *curve, config_t *config, arg_t *args); int field_input(curve_t *curve, config_t *config, arg_t *args); /** + * + * @param curve + * @param cfg + * @param args + * @return + */ +int field_once(curve_t *curve, config_t *cfg, arg_t *args); + +/** * Extract a field representation from a field. * - char(field) == 2: * returns the vector of powers of middle coefficients of the reduction diff --git a/src/math/point.c b/src/math/point.c index 8505954..6368c60 100644 --- a/src/math/point.c +++ b/src/math/point.c @@ -110,10 +110,10 @@ int points_random(curve_t *curve, config_t *config, arg_t *args) { } */ -int points_primet(curve_t *curve, config_t *config, arg_t *args) { +int points_trial(curve_t *curve, config_t *config, arg_t *args) { // TODO stack code!!! if (!args) { - fprintf(stderr, "No args to an arged function. points_primet"); + fprintf(stderr, "No args to an arged function. points_trial"); return INT_MIN; } points_free_deep(&curve->points, curve->npoints); diff --git a/src/math/point.h b/src/math/point.h index 9a0dd89..76f095a 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -96,7 +96,7 @@ int points_random(curve_t *curve, config_t *config, arg_t *args); * @param args * @return state diff */ -int points_primet(curve_t *curve, config_t *config, arg_t *args); +int points_trial(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) |
