aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJ08nY2017-10-19 00:31:24 +0200
committerJ08nY2017-10-19 00:31:24 +0200
commitd01ac5799cf7283de1b26cac07578ca039bd5047 (patch)
treec8305dbfbeaa0a8379712a87aaf129d3ca59a10d /src
parentb09586f3cca80ec3409c4e34a2fae22974c43d56 (diff)
downloadecgen-d01ac5799cf7283de1b26cac07578ca039bd5047.tar.gz
ecgen-d01ac5799cf7283de1b26cac07578ca039bd5047.tar.zst
ecgen-d01ac5799cf7283de1b26cac07578ca039bd5047.zip
Diffstat (limited to 'src')
-rw-r--r--src/ecgen.c4
-rw-r--r--src/exhaustive/exhaustive.c8
-rw-r--r--src/invalid/invalid.c2
-rw-r--r--src/io/cli.c318
-rw-r--r--src/misc/config.h31
5 files changed, 205 insertions, 158 deletions
diff --git a/src/ecgen.c b/src/ecgen.c
index 685d35f..1ae19a1 100644
--- a/src/ecgen.c
+++ b/src/ecgen.c
@@ -145,9 +145,9 @@ int main(int argc, char *argv[]) {
}
int status;
- if (cfg->cm) {
+ if (cfg->method == METHOD_CM) {
status = cm_do();
- } else if (cfg->invalid) {
+ } else if (cfg->method == METHOD_INVALID) {
status = invalid_do();
} else {
status = exhaustive_do();
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c
index 3bc9167..0122a59 100644
--- a/src/exhaustive/exhaustive.c
+++ b/src/exhaustive/exhaustive.c
@@ -67,7 +67,7 @@ static void exhaustive_ginit(gen_f *generators) {
// setup normal generators
generators[OFFSET_SEED] = &gen_skip;
- if (cfg->anomalous) {
+ if (cfg->method == METHOD_ANOMALOUS) {
generators[OFFSET_A] = &gen_skip;
generators[OFFSET_B] = &anomalous_gen_equation;
} else if (cfg->random) {
@@ -96,7 +96,7 @@ static void exhaustive_ginit(gen_f *generators) {
generators[OFFSET_ORDER] = &order_gen_prime;
} else if (cfg->cofactor) {
generators[OFFSET_ORDER] = &order_gen_smallfact;
- } else if (cfg->anomalous) {
+ } else if (cfg->method == METHOD_ANOMALOUS) {
generators[OFFSET_ORDER] = &anomalous_gen_order;
} else {
generators[OFFSET_ORDER] = &order_gen_any;
@@ -111,7 +111,7 @@ static void exhaustive_ginit(gen_f *generators) {
generators[OFFSET_GENERATORS] = &gens_gen_any;
}
- if (cfg->anomalous) {
+ if (cfg->method == METHOD_ANOMALOUS) {
generators[OFFSET_FIELD] = &anomalous_gen_field;
} else if (cfg->random) {
generators[OFFSET_FIELD] = &field_gen_random;
@@ -148,7 +148,7 @@ static void exhaustive_cinit(check_t **validators) {
}
static void exhaustive_ainit(arg_t **argss) {
- if (cfg->anomalous) {
+ if (cfg->method == METHOD_ANOMALOUS) {
arg_t *field_arg = arg_new();
arg_t *eq_arg = arg_new();
size_t *i = try_calloc(sizeof(size_t));
diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c
index 4533020..a02c6cd 100644
--- a/src/invalid/invalid.c
+++ b/src/invalid/invalid.c
@@ -341,7 +341,7 @@ int invalid_do() {
&invalid_setup);
}
debug_log_end("Finished generating invalid curves");
- output_o_end(cfg);
+ output_o_end();
for (size_t i = 0; i < ncurves; ++i) {
curve_free(&curves[i]);
diff --git a/src/io/cli.c b/src/io/cli.c
index a538827..2aa2998 100644
--- a/src/io/cli.c
+++ b/src/io/cli.c
@@ -3,6 +3,7 @@
* Copyright (C) 2017 J08nY
*/
#include "cli.h"
+#include <misc/config.h>
#include <string.h>
#include "exhaustive/ansi.h"
@@ -43,32 +44,34 @@ struct argp_option cli_options[] = {
{"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},
- {"koblitz", OPT_KOBLITZ, "A", OPTION_ARG_OPTIONAL,"Generate a Koblitz curve (a in {0, 1}, b = 1).", 2},
- {"unique", OPT_UNIQUE, 0, 0, "Generate a curve with only one generator.", 2},
+ {0, 0, 0, 0, "Generation methods:", 2},
+ {"order", OPT_ORDER, "ORDER", 0, "Generate a curve with given order (using Complex Multiplication). **NOT IMPLEMENTED**", 2},
{"anomalous", OPT_ANOMALOUS, 0, 0, "Generate an anomalous curve (of trace one, with field order equal to curve order).", 2},
- {"points", OPT_POINTS, "TYPE", 0, "Generate points of given type (random/prime/all/nonprime/none).", 2},
{"ansi", OPT_ANSI, "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). **NOT IMPLEMENTED**", 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, "Generation options:", 3},
+ {"random", OPT_RANDOM, 0, 0, "Generate a random curve (using Random approach).", 3},
+ {"prime", OPT_PRIME, 0, 0, "Generate a curve with prime order.", 3},
+ {"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},
+ {"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},
+
+ {0, 0, 0, 0, "Input/Output options:", 4},
+ {"format", OPT_FORMAT, "FORMAT", 0, "Format to output in. One of {csv, json}, default is json.", 4},
+ {"input", OPT_INPUT, "FILE", 0, "Input from file.", 4},
+ {"output", OPT_OUTPUT, "FILE", 0, "Output into file. Overwrites any existing file!", 4},
+ {"append", OPT_APPEND, 0, 0, "Append to output file (don't overwrite).", 4},
+ {"verbose", OPT_VERBOSE, "FILE", OPTION_ARG_OPTIONAL, "Verbose logging (to stdout or file).", 4},
- {0, 0, 0, 0, "Other:", 4},
- {"data-dir", OPT_DATADIR, "DIR", 0, "Set PARI/GP data directory (containing seadata package).", 4},
- {"memory", OPT_MEMORY, "SIZE", 0, "Use PARI stack of SIZE (can have suffix k/m/g).", 4},
- {"threads", OPT_THREADS, "NUM", 0, "Use NUM threads.", 4},
- {"thread-stack", OPT_TSTACK, "SIZE", 0, "Use PARI stack of SIZE (per thread, can have suffix k/m/g).", 4},
- {"timeout", OPT_TIMEOUT, "TIME", 0, "Timeout computation of a curve parameter after TIME (can have suffix s/m/h/d).", 4},
+ {0, 0, 0, 0, "Other:", 5},
+ {"data-dir", OPT_DATADIR, "DIR", 0, "Set PARI/GP data directory (containing seadata package).", 5},
+ {"memory", OPT_MEMORY, "SIZE", 0, "Use PARI stack of SIZE (can have suffix k/m/g).", 5},
+ {"threads", OPT_THREADS, "NUM", 0, "Use NUM threads.", 5},
+ {"thread-stack", OPT_TSTACK, "SIZE", 0, "Use PARI stack of SIZE (per thread, can have suffix k/m/g).", 5},
+ {"timeout", OPT_TIMEOUT, "TIME", 0, "Timeout computation of a curve parameter after TIME (can have suffix s/m/h/d).", 5},
{0}
};
// clang-format on
@@ -110,63 +113,119 @@ static unsigned long cli_parse_time(const char *str, struct argp_state *state) {
return read;
}
-error_t cli_parse(int key, char *arg, struct argp_state *state) {
- switch (key) {
- case OPT_DATADIR:
- cfg->datadir = arg;
+static void cli_end(struct argp_state *state) {
+ // validate all option states here.
+ // Only one field
+ if (cfg->field == 0 || cfg->field == (FIELD_PRIME | FIELD_BINARY)) {
+ argp_failure(state, 1, 0,
+ "Specify field type, prime or binary, with --fp / "
+ "--f2m (but not both).");
+ }
+ // Only one gen method
+ switch (cfg->method) {
+ case METHOD_DEFAULT:
break;
- case OPT_MEMORY:
- cfg->memory = cli_parse_memory(arg, state);
+ case METHOD_CM:
break;
- case OPT_TSTACK:
- cfg->thread_memory = cli_parse_memory(arg, state);
+ case METHOD_ANOMALOUS:
break;
- case OPT_TIMEOUT:
- cfg->timeout = cli_parse_time(arg, state);
+ case METHOD_SEED:
break;
- case OPT_THREADS:
- if (!strcmp(arg, "auto") || !strcmp(arg, "AUTO")) {
- long nprocs = sysconf(_SC_NPROCESSORS_ONLN);
- if (nprocs > 0) {
- cfg->threads = (unsigned long)nprocs;
- }
- } else {
- cfg->threads = strtoul(arg, NULL, 10);
- if (!cfg->threads) {
- argp_failure(state, 1, 0,
- "Invalid number of threads specified.");
- }
- }
+ case METHOD_INVALID:
break;
- case OPT_COUNT:
- cfg->count = strtoul(arg, NULL, 10);
+ default:
+ printf("%u\n", cfg->method);
+ argp_failure(state, 1, 0,
+ "Only one generation method can be specified.");
break;
- case OPT_FORMAT:
- if (!strcmp(arg, "csv")) {
- cfg->format = FORMAT_CSV;
- } else if (!strcmp(arg, "json")) {
- cfg->format = FORMAT_JSON;
- } else {
- argp_failure(state, 1, 0,
- "Invalid format specified. One of [csv, json] "
- "is valid.");
- }
+ }
+
+ /*
+ // Invalid is not prime or seed by definition.
+ if (cfg->invalid &&
+ (cfg->prime || cfg->seed_algo || cfg->cofactor)) {
+ // not seed, not prime
+ argp_failure(state, 1, 0,
+ "Invalid curve generation can not generate curves "
+ "from seed, exhaustive or prime order.");
+ }
+ if (cfg->cm && (cfg->prime || cfg->seed_algo || cfg->invalid ||
+ cfg->cofactor || cfg->anomalous)) {
+ 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.");
+ }
+ if (cfg->anomalous &&
+ (cfg->binary_field || cfg->cofactor || cfg->seed_algo ||
+ cfg->cm || cfg->invalid || cfg->koblitz)) {
+ argp_failure(
+ state, 1, 0,
+ "Anomalous curve generation can not generate "
+ "binary field curves, curves with a cofactor, from seed "
+ "with fixed order, invalid or Koblitz curves.");
+ }
+
+ */
+ // default values
+ if (!cfg->count) {
+ cfg->count = 1;
+ }
+ if (!cfg->memory) {
+ cfg->memory = 1000000000;
+ }
+ if (!cfg->threads) {
+ cfg->threads = 1;
+ }
+ if (!cfg->thread_memory) {
+ cfg->thread_memory = cfg->bits * 2000000;
+ }
+ if (!cfg->points.type) {
+ cfg->points.type = POINTS_PRIME;
+ }
+}
+
+error_t cli_parse(int key, char *arg, struct argp_state *state) {
+ switch (key) {
+ /* Field options */
+ case OPT_FP:
+ cfg->field |= FIELD_PRIME;
break;
- case OPT_INPUT:
- cfg->input = arg;
+ case OPT_F2M:
+ cfg->field |= FIELD_BINARY;
break;
- case OPT_OUTPUT:
- cfg->output = arg;
+
+ /* Generation method */
+ case OPT_INVALID:
+ cfg->method |= METHOD_INVALID;
break;
- case OPT_APPEND:
- cfg->append = true;
+ case OPT_ORDER:
+ cfg->method |= METHOD_CM;
+ if (arg) {
+ cfg->cm_order = arg;
+ }
break;
- case OPT_VERBOSE:
- cfg->verbose++;
+ case OPT_ANOMALOUS:
+ cfg->method |= METHOD_ANOMALOUS;
+ break;
+ case OPT_ANSI:
+ cfg->method |= METHOD_SEED;
+ cfg->seed_algo = SEED_ANSI;
if (arg) {
- cfg->verbose_log = arg;
+ if (!ansi_seed_valid(arg)) {
+ argp_failure(
+ state, 1, 0,
+ "SEED must be at least 160 bits (40 characters).");
+ }
+ cfg->seed = arg;
}
break;
+
+ /* Generation options */
+ case OPT_COUNT:
+ cfg->count = strtoul(arg, NULL, 10);
+ break;
case OPT_RANDOM:
cfg->random = true;
break;
@@ -177,15 +236,6 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) {
cfg->cofactor = true;
cfg->cofactor_bound = strtol(arg, NULL, 10);
break;
- case OPT_INVALID:
- cfg->invalid = true;
- break;
- case OPT_ORDER:
- cfg->cm = true;
- if (arg) {
- cfg->order = arg;
- }
- break;
case OPT_KOBLITZ:
cfg->koblitz = true;
if (arg) {
@@ -199,9 +249,6 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) {
case OPT_UNIQUE:
cfg->unique = true;
break;
- case OPT_ANOMALOUS:
- cfg->anomalous = true;
- break;
case OPT_POINTS: {
char *num_end;
long amount = strtol(arg, &num_end, 10);
@@ -221,25 +268,64 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) {
}
break;
}
- case OPT_ANSI:
- cfg->seed_algo = SEED_ANSI;
+
+ /* IO options */
+ case OPT_FORMAT:
+ if (!strcmp(arg, "csv")) {
+ cfg->format = FORMAT_CSV;
+ } else if (!strcmp(arg, "json")) {
+ cfg->format = FORMAT_JSON;
+ } else {
+ argp_failure(state, 1, 0,
+ "Invalid format specified. One of [csv, json] "
+ "is valid.");
+ }
+ break;
+ case OPT_INPUT:
+ cfg->input = arg;
+ break;
+ case OPT_OUTPUT:
+ cfg->output = arg;
+ break;
+ case OPT_APPEND:
+ cfg->append = true;
+ break;
+ case OPT_VERBOSE:
+ cfg->verbose++;
if (arg) {
- if (!ansi_seed_valid(arg)) {
- argp_failure(
- state, 1, 0,
- "SEED must be at least 160 bits (40 characters).");
- }
- cfg->seed = arg;
+ cfg->verbose_log = arg;
}
break;
- case OPT_FP:
- cfg->field = FIELD_PRIME;
- cfg->prime_field = true;
+
+ /* Misc options */
+ case OPT_DATADIR:
+ cfg->datadir = arg;
break;
- case OPT_F2M:
- cfg->field = FIELD_BINARY;
- cfg->binary_field = true;
+ case OPT_MEMORY:
+ cfg->memory = cli_parse_memory(arg, state);
break;
+ case OPT_TSTACK:
+ cfg->thread_memory = cli_parse_memory(arg, state);
+ break;
+ case OPT_TIMEOUT:
+ cfg->timeout = cli_parse_time(arg, state);
+ break;
+ case OPT_THREADS:
+ if (!strcmp(arg, "auto") || !strcmp(arg, "AUTO")) {
+ long nprocs = sysconf(_SC_NPROCESSORS_ONLN);
+ if (nprocs > 0) {
+ cfg->threads = (unsigned long)nprocs;
+ }
+ } else {
+ cfg->threads = strtoul(arg, NULL, 10);
+ if (!cfg->threads) {
+ argp_failure(state, 1, 0,
+ "Invalid number of threads specified.");
+ }
+ }
+ break;
+
+ /* Args */
case ARGP_KEY_ARG:
if (state->arg_num >= 1) {
argp_usage(state);
@@ -250,55 +336,7 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) {
2 * (cfg->bits / 8 + (cfg->bits % 8 != 0 ? 1 : 0));
break;
case ARGP_KEY_END:
- // validate all option states here.
- // 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).");
- }
- // Invalid is not prime or seed by definition.
- if (cfg->invalid &&
- (cfg->prime || cfg->seed_algo || cfg->cofactor)) {
- // not seed, not prime
- argp_failure(state, 1, 0,
- "Invalid curve generation can not generate curves "
- "from seed, exhaustive or prime order.");
- }
- if (cfg->cm && (cfg->prime || cfg->seed_algo || cfg->invalid ||
- cfg->cofactor || cfg->anomalous)) {
- 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.");
- }
- if (cfg->anomalous &&
- (cfg->binary_field || cfg->cofactor || cfg->seed_algo ||
- cfg->cm || cfg->invalid || cfg->koblitz)) {
- argp_failure(
- state, 1, 0,
- "Anomalous curve generation can not generate "
- "binary field curves, curves with a cofactor, from seed "
- "with fixed order, invalid or Koblitz curves.");
- }
-
- // default values
- if (!cfg->count) {
- cfg->count = 1;
- }
- if (!cfg->memory) {
- cfg->memory = 1000000000;
- }
- if (!cfg->threads) {
- cfg->threads = 1;
- }
- if (!cfg->thread_memory) {
- cfg->thread_memory = cfg->bits * 2000000;
- }
- if (!cfg->points.type) {
- cfg->points.type = POINTS_PRIME;
- }
+ cli_end(state);
break;
case ARGP_KEY_NO_ARGS:
argp_usage(state);
diff --git a/src/misc/config.h b/src/misc/config.h
index 2ad6736..859249c 100644
--- a/src/misc/config.h
+++ b/src/misc/config.h
@@ -11,7 +11,7 @@
#include <stdbool.h>
#include <stddef.h>
-enum field_e { FIELD_PRIME, FIELD_BINARY };
+enum field_e { FIELD_PRIME = 1 << 0, FIELD_BINARY = 1 << 1 };
enum format_e { FORMAT_JSON, FORMAT_CSV };
enum points_e {
POINTS_NONE = 0,
@@ -25,6 +25,20 @@ struct points_s {
size_t amount;
};
+/**
+ * @brief
+ */
+typedef enum {
+ METHOD_DEFAULT = 0,
+ METHOD_CM = 1 << 0,
+ METHOD_ANOMALOUS = 1 << 1,
+ METHOD_SEED = 1 << 2,
+ METHOD_INVALID = 1 << 3
+} method_e;
+
+/**
+ * @brief
+ */
typedef enum {
SEED_NONE = 0,
SEED_ANSI,
@@ -39,23 +53,18 @@ typedef enum {
typedef struct {
/** @brief What field should the curves be generated over. */
enum field_e field;
- bool binary_field;
- bool prime_field;
- /** @brief How many curves should be generater. */
+ /** @brief Generation method. */
+ method_e method;
+
+ /** @brief How many curves should be generated. */
long count;
/** @brief Whether the curves should be generated at random (no input). */
bool random;
/** @brief Whether the curves should have prime order. */
bool prime;
- /** @brief Whether the curves should be generated as invalid, for some
- * curve. */
- bool invalid;
/** @brief Whether the Complex Multiplication method should be used. */
- bool cm;
- char *order;
- /** @brief Whether the curves should be trace one (Anomalous). */
- bool anomalous;
+ char *cm_order;
/** @brief Whether the curves should be Koblitz (a \\in {0, 1}, b = 1). */
bool koblitz;
long koblitz_value;