diff options
| author | J08nY | 2017-03-09 13:18:15 +0100 |
|---|---|---|
| committer | J08nY | 2017-03-09 13:18:15 +0100 |
| commit | 8c65683537ea57a435a2f987f540bcc2ec5db678 (patch) | |
| tree | 18d6e8893c885d98392d9a4cc9a9d98e67003f18 /src/io | |
| parent | 7eff89c0095da4246a7b9c5a6654dc0d7692325e (diff) | |
| download | ecgen-8c65683537ea57a435a2f987f540bcc2ec5db678.tar.gz ecgen-8c65683537ea57a435a2f987f540bcc2ec5db678.tar.zst ecgen-8c65683537ea57a435a2f987f540bcc2ec5db678.zip | |
Diffstat (limited to 'src/io')
| -rw-r--r-- | src/io/cli.c | 140 | ||||
| -rw-r--r-- | src/io/cli.h | 2 | ||||
| -rw-r--r-- | src/io/input.c | 3 | ||||
| -rw-r--r-- | src/io/output.c | 26 | ||||
| -rw-r--r-- | src/io/output.h | 18 |
5 files changed, 108 insertions, 81 deletions
diff --git a/src/io/cli.c b/src/io/cli.c index 374b949..efebfbc 100644 --- a/src/io/cli.c +++ b/src/io/cli.c @@ -6,19 +6,20 @@ #include <string.h> char doc[] = - "ecgen, tool for generating Elliptic curve domain parameters.\v(C) 2017 " - "Eastern Seaboard Phishing Authority"; + "ecgen, tool for generating Elliptic curve domain parameters.\v(C) 2017 " + "Eastern Seaboard Phishing Authority"; char args_doc[] = "bits"; enum opt_keys { OPT_DATADIR = 'd', OPT_COUNT = 'c', OPT_PRIME = 'p', + OPT_COFACTOR = 'k', OPT_RANDOM = 'r', OPT_SEED = 's', OPT_INVALID = 'i', OPT_ORDER = 'n', - OPT_KOBLITZ = 'k', + OPT_KOBLITZ = 'K', OPT_UNIQUE = 'u', OPT_FORMAT = 't', OPT_OUTPUT = 'o', @@ -32,27 +33,28 @@ enum opt_keys { // clang-format off struct argp_option options[] = { - {0, 0, 0, 0, "Field specification:", 1}, - {"fp", OPT_FP, 0, 0, "Prime field.", 1}, - {"f2m", OPT_F2M, 0, 0, "Binary field.", 1}, - {0, 0, 0, 0, "Generation options:", 2}, - {"random", OPT_RANDOM, 0, 0, "Generate a random curve.", 2}, - {"prime", OPT_PRIME, 0, 0, "Generate a curve with prime order.", 2}, - {"seed", OPT_SEED, "SEED", OPTION_ARG_OPTIONAL, "Generate a curve from SEED (ANSI X9.62 verifiable procedure).", 2}, - {"invalid", OPT_INVALID, 0, 0, "Generate a set of invalid curves (for a given curve).", 2}, - {"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}, - {"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}, - {"output", OPT_OUTPUT, "FILE", 0, "Output into file. Overwrites any existing file!", 3}, - {"append", OPT_APPEND, 0, 0, "Append to output file (don't overwrite).", 3}, - {"verbose", OPT_VERBOSE, "FILE", OPTION_ARG_OPTIONAL, "Verbose logging (to stdout or file).", 3}, - {0, 0, 0, 0, "Other:", 4}, - {"data-dir", OPT_DATADIR, "DIR", 0, "PARI/GP data directory (containing seadata package).", 4}, + {0, 0, 0, 0, "Field specification:", 1}, + {"fp", OPT_FP, 0, 0, "Prime field.", 1}, + {"f2m", OPT_F2M, 0, 0, "Binary field.", 1}, + {0, 0, 0, 0, "Generation options:", 2}, + {"random", OPT_RANDOM, 0, 0, "Generate a random curve (using Random approach).", 2}, + {"prime", OPT_PRIME, 0, 0, "Generate a curve with prime order.", 2}, + {"cofactor", OPT_COFACTOR, "BOUND", 0, "Generate a curve with cofactor up to BOUND.", 2}, + {"seed", OPT_SEED, "SEED", OPTION_ARG_OPTIONAL, "Generate a curve from SEED (ANSI X9.62 verifiable procedure).", 2}, + {"invalid", OPT_INVALID, 0, 0, "Generate a set of invalid curves, for a given curve (using Invalid curve algorithm).", 2}, + {"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}, + {"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}, + {"output", OPT_OUTPUT, "FILE", 0, "Output into file. Overwrites any existing file!", 3}, + {"append", OPT_APPEND, 0, 0, "Append to output file (don't overwrite).", 3}, + {"verbose", OPT_VERBOSE, "FILE", OPTION_ARG_OPTIONAL, "Verbose logging (to stdout or file).", 3}, + {0, 0, 0, 0, "Other:", 4}, + {"data-dir", OPT_DATADIR, "DIR", 0, "PARI/GP data directory (containing seadata package).", 4}, {0} }; // clang-format on @@ -61,7 +63,8 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) { struct config_t *cfg = state->input; switch (key) { - case OPT_DATADIR: cfg->datadir = arg; + case OPT_DATADIR: + cfg->datadir = arg; break; case OPT_COUNT: if (arg) { @@ -76,40 +79,55 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) { cfg->format = FORMAT_JSON; } else { argp_failure(state, 1, 0, - "Invalid format specified. One of [csv, json] " - "is valid."); + "Invalid format specified. One of [csv, json] " + "is valid."); } } else { argp_failure( - state, 1, 0, - "You have to specify a format with the format option."); + state, 1, 0, + "You have to specify a format with the format option."); } break; - case OPT_INPUT: cfg->input = arg; + case OPT_INPUT: + cfg->input = arg; break; - case OPT_OUTPUT: cfg->output = arg; + case OPT_OUTPUT: + cfg->output = arg; break; - case OPT_APPEND: cfg->append = true; + case OPT_APPEND: + cfg->append = true; break; - case OPT_VERBOSE: cfg->verbose++; + case OPT_VERBOSE: + cfg->verbose++; if (arg) { cfg->debug = arg; } break; - case OPT_RANDOM: cfg->random = true; + case OPT_RANDOM: + cfg->random = true; break; - case OPT_PRIME: cfg->prime = true; + case OPT_PRIME: + cfg->prime = true; break; - case OPT_INVALID: cfg->invalid = true; + case OPT_COFACTOR: + cfg->cofactor = true; + if (arg) { + cfg->cofactor_bound = strtol(arg, NULL, 10); + } + case OPT_INVALID: + cfg->invalid = true; break; - case OPT_ORDER: cfg->cm = true; + case OPT_ORDER: + cfg->cm = true; if (arg) { cfg->order = arg; } break; - case OPT_KOBLITZ: cfg->koblitz = true; + case OPT_KOBLITZ: + cfg->koblitz = true; break; - case OPT_UNIQUE: cfg->unique = true; + case OPT_UNIQUE: + cfg->unique = true; break; case OPT_POINTS: if (arg) { @@ -123,24 +141,28 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) { argp_failure(state, 1, 0, "Unknow point type"); } } else { - argp_failure(state, 1, 0, "You have to specify what points you want."); + argp_failure(state, 1, 0, + "You have to specify what points you want."); } break; - case OPT_SEED: cfg->from_seed = true; + case OPT_SEED: + cfg->from_seed = true; if (arg) { // ANSI X9.62 specifies seed as at least 160 bits in length. if (strlen(arg) < 20) { argp_failure( - state, 1, 0, - "SEED must be at least 160 bits (20 characters)."); + state, 1, 0, + "SEED must be at least 160 bits (20 characters)."); } cfg->seed = arg; } break; - case OPT_FP: cfg->field = FIELD_PRIME; + case OPT_FP: + cfg->field = FIELD_PRIME; cfg->prime_field = true; break; - case OPT_F2M: cfg->field = FIELD_BINARY; + case OPT_F2M: + cfg->field = FIELD_BINARY; cfg->binary_field = true; break; case ARGP_KEY_ARG: @@ -155,31 +177,35 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) { // Only one field if (!cfg->prime_field && !cfg->binary_field) { argp_failure(state, 1, 0, - "Specify field type, prime or binary, with --fp / " - "--f2m (but not both)."); + "Specify field type, prime or binary, with --fp / " + "--f2m (but not both)."); } // Invalid is not prime or seed by definition. - if (cfg->invalid && (cfg->prime || cfg->from_seed)) { + if (cfg->invalid && + (cfg->prime || cfg->from_seed || cfg->cofactor)) { // not seed, not prime argp_failure(state, 1, 0, - "Invalid curve generation can not generate curves " - "from seed, exhaustive or prime order."); + "Invalid curve generation can not generate curves " + "from seed, exhaustive or prime order."); } - if (cfg->cm && (cfg->prime || cfg->from_seed || cfg->invalid)) { + if (cfg->cm && (cfg->prime || cfg->from_seed || cfg->invalid || + cfg->cofactor)) { argp_failure(state, 1, 0, - "Fixed order curve generation can not generate " - "curves from seed, or invalid curves. Prime order " - "also doesn't make sense if the given one isn't " - "prime."); + "Fixed order curve generation can not generate " + "curves from seed, or invalid curves. Prime order " + "also doesn't make sense if the given one isn't " + "prime."); } // default values if (!cfg->count) { cfg->count = 1; } break; - case ARGP_KEY_NO_ARGS: argp_usage(state); + case ARGP_KEY_NO_ARGS: + argp_usage(state); break; - default: return ARGP_ERR_UNKNOWN; + default: + return ARGP_ERR_UNKNOWN; } return 0; } diff --git a/src/io/cli.h b/src/io/cli.h index 3facaff..c65f125 100644 --- a/src/io/cli.h +++ b/src/io/cli.h @@ -36,6 +36,8 @@ typedef struct config_t { bool cm; char *order; bool koblitz; + bool cofactor; + long cofactor_bound; bool from_seed; char *seed; bool unique; diff --git a/src/io/input.c b/src/io/input.c index 3865d79..34aeb5a 100644 --- a/src/io/input.c +++ b/src/io/input.c @@ -22,7 +22,8 @@ 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/io/output.c b/src/io/output.c index 88b120e..cb718fa 100644 --- a/src/io/output.c +++ b/src/io/output.c @@ -11,7 +11,7 @@ FILE *out; FILE *debug; -char *output_scsv(curve_t *curve, config_t *config) { +char *output_scsv(curve_t *curve, config_t *cfg) { pari_sp ltop = avma; GEN vector = curve_params(curve); @@ -48,23 +48,21 @@ char *output_scsv(curve_t *curve, config_t *config) { return result; } -void output_fcsv(FILE *out, curve_t *curve, config_t *config) { - char *string = output_scsv(curve, config); +void output_fcsv(FILE *out, curve_t *curve, config_t *cfg) { + char *string = output_scsv(curve, cfg); fprintf(out, "%s\n", string); free(string); } -void output_csv(curve_t *curve, config_t *config) { - output_fcsv(out, curve, config); -} +void output_csv(curve_t *curve, config_t *cfg) { output_fcsv(out, curve, cfg); } -JSON_Value *output_jjson(curve_t *curve, config_t *config) { +JSON_Value *output_jjson(curve_t *curve, config_t *cfg) { pari_sp ltop = avma; // root object/value is curve JSON_Value *root_value = json_value_init_object(); JSON_Object *root_object = json_value_get_object(root_value); - switch (config->field) { + switch (cfg->field) { case FIELD_PRIME: { char *prime = pari_sprintf("%P#x", curve->field); json_object_dotset_string(root_object, "field.p", prime); @@ -168,22 +166,22 @@ JSON_Value *output_jjson(curve_t *curve, config_t *config) { return root_value; } -char *output_sjson(curve_t *curve, config_t *config) { - JSON_Value *root_value = output_jjson(curve, config); +char *output_sjson(curve_t *curve, config_t *cfg) { + JSON_Value *root_value = output_jjson(curve, cfg); char *result = json_serialize_to_string_pretty(root_value); json_value_free(root_value); return result; } -void output_fjson(FILE *out, curve_t *curve, config_t *config) { - char *s = output_sjson(curve, config); +void output_fjson(FILE *out, curve_t *curve, config_t *cfg) { + char *s = output_sjson(curve, cfg); fprintf(out, "%s\n", s); json_free_serialized_string(s); } -void output_json(curve_t *curve, config_t *config) { - output_fjson(out, curve, config); +void output_json(curve_t *curve, config_t *cfg) { + output_fjson(out, curve, cfg); } void output_init(config_t *cfg) { diff --git a/src/io/output.h b/src/io/output.h index ea9a97e..283b701 100644 --- a/src/io/output.h +++ b/src/io/output.h @@ -18,7 +18,7 @@ * @param config * @return */ -char *output_scsv(curve_t *curve, config_t *config); +char *output_scsv(curve_t *curve, config_t *cfg); /** * @@ -26,14 +26,14 @@ char *output_scsv(curve_t *curve, config_t *config); * @param curve * @param config */ -void output_fcsv(FILE *out, curve_t *curve, config_t *config); +void output_fcsv(FILE *out, curve_t *curve, config_t *cfg); /** * * @param curve * @param config */ -void output_csv(curve_t *curve, config_t *config); +void output_csv(curve_t *curve, config_t *cfg); /** * @@ -41,7 +41,7 @@ void output_csv(curve_t *curve, config_t *config); * @param config * @return */ -char *output_sjson(curve_t *curve, config_t *config); +char *output_sjson(curve_t *curve, config_t *cfg); /** * @@ -49,14 +49,14 @@ char *output_sjson(curve_t *curve, config_t *config); * @param curve * @param config */ -void output_fjson(FILE *out, curve_t *curve, config_t *config); +void output_fjson(FILE *out, curve_t *curve, config_t *cfg); /** * * @param curve * @param config */ -void output_json(curve_t *curve, config_t *config); +void output_json(curve_t *curve, config_t *cfg); /** * @@ -64,7 +64,7 @@ void output_json(curve_t *curve, config_t *config); * @param config * @return */ -char *(*output_s)(curve_t *curve, config_t *config); +char *(*output_s)(curve_t *curve, config_t *cfg); /** * @@ -72,14 +72,14 @@ char *(*output_s)(curve_t *curve, config_t *config); * @param curve * @param config */ -void (*output_f)(FILE *out, curve_t *curve, config_t *config); +void (*output_f)(FILE *out, curve_t *curve, config_t *cfg); /** * * @param curve * @param config */ -void (*output_o)(curve_t *curve, config_t *config); +void (*output_o)(curve_t *curve, config_t *cfg); /** * |
