diff options
| author | J08nY | 2017-02-09 04:07:37 +0100 |
|---|---|---|
| committer | J08nY | 2017-02-09 04:07:37 +0100 |
| commit | 79b29481b1c4d13063dd8b6ee6a1d0d70a54faab (patch) | |
| tree | 007da84bc4133c656f2f66df541f74c6b55bfb11 | |
| parent | 0b5d1cca9c78869c6cffa2932297c1d70ba142e2 (diff) | |
| download | ecgen-79b29481b1c4d13063dd8b6ee6a1d0d70a54faab.tar.gz ecgen-79b29481b1c4d13063dd8b6ee6a1d0d70a54faab.tar.zst ecgen-79b29481b1c4d13063dd8b6ee6a1d0d70a54faab.zip | |
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/Makefile | 16 | ||||
| -rw-r--r-- | src/cm/cm.c | 5 | ||||
| -rw-r--r-- | src/cm/cm.h | 8 | ||||
| -rw-r--r-- | src/ecgen.c | 33 | ||||
| -rw-r--r-- | src/equation.c | 47 | ||||
| -rw-r--r-- | src/equation.h | 27 | ||||
| -rw-r--r-- | src/gp/equation.gp | 28 | ||||
| -rw-r--r-- | src/gp/field.gp | 33 | ||||
| -rw-r--r-- | src/invalid/invalid.c | 5 | ||||
| -rw-r--r-- | src/invalid/invalid.h | 8 | ||||
| -rw-r--r-- | src/io/cli.c (renamed from src/cli.c) | 40 | ||||
| -rw-r--r-- | src/io/cli.h (renamed from src/cli.h) | 4 | ||||
| -rw-r--r-- | src/io/input.c (renamed from src/input.c) | 19 | ||||
| -rw-r--r-- | src/io/input.h (renamed from src/input.h) | 2 | ||||
| -rw-r--r-- | src/io/output.c (renamed from src/output.c) | 7 | ||||
| -rw-r--r-- | src/io/output.h (renamed from src/output.h) | 1 | ||||
| -rw-r--r-- | src/math/curve.c (renamed from src/curve.c) | 24 | ||||
| -rw-r--r-- | src/math/curve.h (renamed from src/curve.h) | 18 | ||||
| -rw-r--r-- | src/math/equation.c | 58 | ||||
| -rw-r--r-- | src/math/equation.h | 31 | ||||
| -rw-r--r-- | src/math/field.c (renamed from src/field.c) | 4 | ||||
| -rw-r--r-- | src/math/field.h (renamed from src/field.h) | 6 | ||||
| -rw-r--r-- | src/math/point.c (renamed from src/point.c) | 0 | ||||
| -rw-r--r-- | src/math/point.h (renamed from src/point.h) | 0 | ||||
| -rw-r--r-- | src/math/poly.c (renamed from src/poly.c) | 0 | ||||
| -rw-r--r-- | src/math/poly.h (renamed from src/poly.h) | 0 | ||||
| -rw-r--r-- | src/math/random.c (renamed from src/random.c) | 0 | ||||
| -rw-r--r-- | src/math/random.h (renamed from src/random.h) | 0 | ||||
| -rw-r--r-- | src/random/generators.c (renamed from src/generators.c) | 31 | ||||
| -rw-r--r-- | src/random/generators.h (renamed from src/generators.h) | 0 | ||||
| -rw-r--r-- | src/random/seed.c (renamed from src/seed.c) | 24 | ||||
| -rw-r--r-- | src/random/seed.h | 44 | ||||
| -rw-r--r-- | src/seed.h | 21 | ||||
| -rw-r--r-- | src/types.h | 4 |
35 files changed, 281 insertions, 269 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index bb331a3..673aa0b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ add_custom_target(gp2c ALL DEPENDS gp.c gp.h) include_directories(src) include_directories(lib) -file(GLOB SOURCES "src/*.c" "src/*.h") +file(GLOB SOURCES "src/*.c" "src/cm/*.c" "src/invalid/*.c" "src/io/*.c" "src/random/*.c" "src/math/*.c") add_executable(ecgen ${SOURCES}) diff --git a/src/Makefile b/src/Makefile index 2c333ab..d28102c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -14,19 +14,21 @@ LDFLAGS=-L../lib GP_CFLAGS=-O3 -Wall -fomit-frame-pointer -fno-strict-aliasing -fPIC GPFLAGS=-g -i4 -INCLUDES=-I. -I../lib +INCLUDES=-I. -I../lib -Icm -Iinvalid -Iio -Irandom LIBS=-lpari -lparson #### +VPATH = cm:invalid:io:random:math + GP = GPC = $(addsuffix .c, $(GP)) GPO = $(addsuffix .o, $(GP)) GPH = $(addsuffix .h, $(GP)) -SRC = cli input output poly field equation curve point random seed generators -OBJ = $(addsuffix .o, $(SRC)) -HDR = $(addsuffix .h, $(SRC)) +SRC = $(wildcard *.c) $(wildcard */*.c) +OBJ = $(patsubst %.c,%.o, $(SRC)) +HDR = $(wildcard *.h) $(wildcard */*.h) #### @@ -52,7 +54,7 @@ clean-all: clean clean-gp clean: rm -f ecgen - rm -f *.o + find . -type f -name '*.o' -exec rm {} + clean-gp: rm -f $(GPH) @@ -71,7 +73,7 @@ help: @echo " - format : run clang-format on source files" format: - clang-format -i *.c - clang-format -i *.h + clang-format -i $(SRC) + clang-format -i $(HDR) .PHONY: all gp2c clean-all clean clean-gp help format
\ No newline at end of file diff --git a/src/cm/cm.c b/src/cm/cm.c new file mode 100644 index 0000000..a86dcdd --- /dev/null +++ b/src/cm/cm.c @@ -0,0 +1,5 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +#include "cm.h" diff --git a/src/cm/cm.h b/src/cm/cm.h new file mode 100644 index 0000000..ba7c2c4 --- /dev/null +++ b/src/cm/cm.h @@ -0,0 +1,8 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +#ifndef ECGEN_CM_H +#define ECGEN_CM_H + +#endif // ECGEN_CM_H diff --git a/src/ecgen.c b/src/ecgen.c index 5f73c7e..54d7f30 100644 --- a/src/ecgen.c +++ b/src/ecgen.c @@ -24,13 +24,10 @@ */ #include <time.h> -#include "curve.h" -#include "equation.h" -#include "field.h" -#include "generators.h" -#include "input.h" -#include "output.h" -#include "seed.h" +#include "io/input.h" +#include "io/output.h" +#include "math/curve.h" +#include "random/generators.h" const char *argp_program_version = "ecgen 0.2\n" @@ -83,17 +80,21 @@ int main(int argc, char *argv[]) { return quit(1); } - gen_t generators[5]; - gen_init(generators, &cfg); + if (cfg.cm) { + } else if (cfg.invalid) { + } else { + gen_t generators[5]; + gen_init(generators, &cfg); - curve_t *curve = curve_new(); - int state = 0; - while (state != 5) { - int diff = generators[state](curve, &cfg); - state += diff; + curve_t *curve = curve_new(); + int state = 0; + while (state != 5) { + int diff = generators[state](curve, &cfg); + state += diff; + } + output_csv(out, "%Px", ';', curve_params(curve)); + curve_free(&curve); } - output_csv(out, "%Px", ';', curve_params(curve)); - curve_free(&curve); return quit(0); } diff --git a/src/equation.c b/src/equation.c deleted file mode 100644 index d7fa63c..0000000 --- a/src/equation.c +++ /dev/null @@ -1,47 +0,0 @@ -/* - * ecgen, tool for generating Elliptic curve domain parameters - * Copyright (C) 2017 J08nY - */ -#include "equation.h" - -int eq_random(curve_t *curve, config_t *config) { - int r = a_random(curve, config) + b_random(curve, config); - if (r == 2) { - return r; - } - return -1; -} - -int a_random(curve_t *curve, config_t *config) { - curve->a = genrand(curve->field); - return 1; -} - -int a_zero(curve_t *curve, config_t *config) { - curve->a = gen_0; - return 1; -} - -int a_one(curve_t *curve, config_t *config) { - curve->a = gen_1; - return 1; -} - -int a_seed(curve_t *curve, config_t *config) {} - -int b_random(curve_t *curve, config_t *config) { - curve->b = genrand(curve->field); - return 1; -} - -int b_zero(curve_t *curve, config_t *config) { - curve->b = gen_0; - return 1; -} - -int b_one(curve_t *curve, config_t *config) { - curve->b = gen_1; - return 1; -} - -int b_seed(curve_t *curve, config_t *config) {} diff --git a/src/equation.h b/src/equation.h deleted file mode 100644 index 7acb7d0..0000000 --- a/src/equation.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * ecgen, tool for generating Elliptic curve domain parameters - * Copyright (C) 2017 J08nY - */ -#ifndef ECGEN_EQUATION_H -#define ECGEN_EQUATION_H - -#include "cli.h" -#include "types.h" - -int a_random(curve_t *curve, config_t *config); - -int a_zero(curve_t *curve, config_t *config); - -int a_one(curve_t *curve, config_t *config); - -int a_seed(curve_t *curve, config_t *config); - -int b_random(curve_t *curve, config_t *config); - -int b_zero(curve_t *curve, config_t *config); - -int b_one(curve_t *curve, config_t *config); - -int b_seed(curve_t *curve, config_t *config); - -#endif // ECGEN_EQUATION_H diff --git a/src/gp/equation.gp b/src/gp/equation.gp deleted file mode 100644 index c1483e4..0000000 --- a/src/gp/equation.gp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * ecgen, tool for generating Elliptic curve domain parameters - * Copyright (C) 2017 J08nY - */ - -/** - * Constructs an elliptic curve in the form E: - * y^2 = x^3 + ax + b, over a prime field - * @param a - * @param b - * @param p - * @returns elliptic curve - */ -prime_weierstrass(a:int, b:int, field:gen) = { - return(ellinit([a,b], field)); -} - -/** - * Constructs an elliptic curve in the form E: - * y^2 + xy = x^3 + ax + b, over a binary field. - * @param a - * @param b - * @param field - * @returns elliptic curve - */ -binary_weierstrass(a:int, b:int, field:gen) = { - return(ellinit([1,0,0,a,b], field)); -} diff --git a/src/gp/field.gp b/src/gp/field.gp deleted file mode 100644 index c428abd..0000000 --- a/src/gp/field.gp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * ecgen, tool for generating Elliptic curve domain parameters - * Copyright (C) 2017 J08nY - */ - -/** - * Extract a field representation from a field. - * - char(field) == 2: - * returns the vector of powers of middle coefficients of the reduction polynomial. - * - char(field) != 2: - * returns the field characteristic(p). - * - * @return field representation - */ -field_params(field:gen) = { - if(type(field) == "t_INT", - return([field]); - ); - - local(out:vec, j:int, c:int); - out = vector(3); - - j = 1; - for(i=2, length(field.mod) - 2, - c = polcoeff(field.mod, i):int; - if(c != 0, - out[j] = i; - j++; - ); - ); - - return(out); -}
\ No newline at end of file diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c new file mode 100644 index 0000000..a600dcb --- /dev/null +++ b/src/invalid/invalid.c @@ -0,0 +1,5 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +#include "invalid.h" diff --git a/src/invalid/invalid.h b/src/invalid/invalid.h new file mode 100644 index 0000000..180c409 --- /dev/null +++ b/src/invalid/invalid.h @@ -0,0 +1,8 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +#ifndef ECGEN_INVALID_H +#define ECGEN_INVALID_H + +#endif // ECGEN_INVALID_H @@ -16,6 +16,8 @@ enum opt_keys { OPT_RANDOM = 'r', OPT_SEED = 's', OPT_INVALID = 'i', + OPT_ORDER = 'n', + OPT_KOBLITZ = 'k', OPT_OUTPUT = 'o', OPT_INPUT = 'f', OPT_APPEND = 'a', @@ -26,18 +28,20 @@ enum opt_keys { // clang-format off struct argp_option options[] = { // Field specification - {"fp", OPT_FP, 0, 0, "Prime field."}, - {"f2m", OPT_F2M, 0, 0, "Binary field."}, + {"fp", OPT_FP, 0, 0, "Prime field."}, + {"f2m", OPT_F2M, 0, 0, "Binary field."}, // Curve specification - {"random", OPT_RANDOM, 0, 0, "Generate a random curve."}, - {"prime", OPT_PRIME, 0, 0, "Generate a curve with prime order."}, + {"random", OPT_RANDOM, 0, 0, "Generate a random curve."}, + {"prime", OPT_PRIME, 0, 0, "Generate a curve with prime order."}, {"seed", OPT_SEED, "SEED", OPTION_ARG_OPTIONAL, "Generate a curve from SEED (ANSI X9.62 verifiable procedure)."}, - {"invalid", OPT_INVALID, 0, 0, "Generate a set of invalid curves (for a given curve)."}, + {"invalid", OPT_INVALID, 0, 0, "Generate a set of invalid curves (for a given curve)."}, + {"order", OPT_ORDER, "ORDER", 0, "Generate a curve with given order (using Complex Multiplication)."}, + {"koblitz", OPT_KOBLITZ, 0, 0, "Generate a Koblitz curve."}, // Other - {"data-dir", OPT_DATADIR, "DIR", 0, "PARI/GP data directory (containing seadata package)."}, - {"input", OPT_INPUT, "FILE", 0, "Input from file."}, - {"output", OPT_OUTPUT, "FILE", 0, "Output into file. Overwrites any existing file!"}, - {"append", OPT_APPEND, 0, 0, "Append to output file (don't overwrite)."}, + {"data-dir", OPT_DATADIR, "DIR", 0, "PARI/GP data directory (containing seadata package)."}, + {"input", OPT_INPUT, "FILE", 0, "Input from file."}, + {"output", OPT_OUTPUT, "FILE", 0, "Output into file. Overwrites any existing file!"}, + {"append", OPT_APPEND, 0, 0, "Append to output file (don't overwrite)."}, {0}}; // clang-format on @@ -66,6 +70,15 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) { 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; + break; case OPT_SEED: cfg->from_seed = true; if (arg) { @@ -103,11 +116,18 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) { } // Invalid is not prime or seed or random by definition. if (cfg->invalid && (cfg->prime || cfg->from_seed || cfg->random)) { - // not seed, not prime + // not seed, not prime, not random argp_failure(state, 1, 0, "Invalid curve generation can not generate curves " "from seed, random or prime order."); } + if (cfg->cm && (cfg->prime || cfg->from_seed || cfg->invalid)) { + 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."); + } break; case ARGP_KEY_NO_ARGS: argp_usage(state); @@ -22,6 +22,9 @@ typedef struct config_t { bool random; bool prime; bool invalid; + bool cm; + char *order; + bool koblitz; bool from_seed; char *seed; char *datadir; @@ -29,6 +32,7 @@ typedef struct config_t { char *input; bool append; long bits; + } config_t; error_t parse_opt(int key, char *arg, struct argp_state *state); diff --git a/src/input.c b/src/io/input.c index beff9e5..3dcca00 100644 --- a/src/input.c +++ b/src/io/input.c @@ -2,12 +2,12 @@ * ecgen, tool for generating Elliptic curve domain parameters * Copyright (C) 2017 J08nY */ +#include <parson/parson.h> #include "input.h" FILE *in; -GEN fread_i(FILE *stream, const char *prompt, long bits, int delim, - GEN (*rand_func)(long)) { +GEN fread_i(FILE *stream, const char *prompt, long bits, int delim) { if (prompt) { printf("%s ", prompt); } @@ -17,11 +17,7 @@ GEN fread_i(FILE *stream, const char *prompt, long bits, int delim, ssize_t len = getdelim(&line, &n, delim, stream); if (len == 1) { free(line); - if (rand_func) { - return rand_func(bits); - } else { - return gen_0; - } + return gen_m1; } pari_sp ltop = avma; GEN in = strtoi(line); @@ -38,7 +34,7 @@ GEN fread_i(FILE *stream, const char *prompt, long bits, int delim, } GEN fread_prime(FILE *stream, const char *prompt, long bits, int delim) { - GEN read = fread_i(stream, prompt, bits, delim, &random_prime); + GEN read = fread_i(stream, prompt, bits, delim); if (equalii(read, gen_m1)) { return read; } else { @@ -52,11 +48,11 @@ GEN fread_prime(FILE *stream, const char *prompt, long bits, int delim) { } GEN fread_int(FILE *stream, const char *prompt, long bits, int delim) { - return fread_i(stream, prompt, bits, delim, &random_int); + return fread_i(stream, prompt, bits, delim); } GEN fread_short(FILE *stream, const char *prompt, int delim) { - return fread_i(stream, prompt, 16, delim, NULL); + return fread_i(stream, prompt, 16, delim); } GEN fread_string(FILE *stream, const char *prompt, int delim) { @@ -79,7 +75,7 @@ GEN fread_string(FILE *stream, const char *prompt, int delim) { } GEN fread_param(param_t param, FILE *stream, const char *prompt, long bits, - int delim) { + int delim) { switch (param) { case PARAM_PRIME: return fread_prime(stream, prompt, bits, delim); @@ -98,6 +94,7 @@ GEN read_param(param_t param, const char *prompt, long bits, int delim) { } FILE *input_open(const char *input) { + json_set_allocation_functions(pari_malloc, pari_free); if (input) { FILE *in = fopen(input, "r"); if (!in) { diff --git a/src/input.h b/src/io/input.h index 21b8393..f403661 100644 --- a/src/input.h +++ b/src/io/input.h @@ -5,7 +5,7 @@ #ifndef ECGEN_INPUT_H #define ECGEN_INPUT_H -#include "random.h" +#include "math/random.h" typedef enum PARAM { PARAM_PRIME, diff --git a/src/output.c b/src/io/output.c index e6dc120..86de015 100644 --- a/src/output.c +++ b/src/io/output.c @@ -4,6 +4,7 @@ */ #include "output.h" +#include <parson/parson.h> FILE *out; @@ -46,11 +47,15 @@ void output_csv(FILE *out, const char *format, char delim, GEN vector) { free(string); } -char *output_sjson(GEN vector) {} +char *output_sjson(GEN vector) { + parson +} void output_json(FILE *out, GEN vector) {} FILE *output_open(const char *output, bool append) { + json_set_allocation_functions(pari_malloc, pari_free); + if (output) { FILE *out = fopen(output, append ? "a" : "w"); if (!out) { diff --git a/src/output.h b/src/io/output.h index 121a74f..99b92b7 100644 --- a/src/output.h +++ b/src/io/output.h @@ -6,7 +6,6 @@ #define ECGEN_OUTPUT_H #include <pari/pari.h> -#include <parson/parson.h> #include <stdbool.h> /** diff --git a/src/curve.c b/src/math/curve.c index 812f688..422156b 100644 --- a/src/curve.c +++ b/src/math/curve.c @@ -4,7 +4,7 @@ */ #include "curve.h" #include "field.h" -#include "seed.h" +#include "random/seed.h" curve_t *curve_new() { curve_t *curve = pari_malloc(sizeof(curve_t)); @@ -25,7 +25,7 @@ void curve_free(curve_t **curve) { } } -int curve_init(curve_t *curve, config_t *config) { +int curve_init(curve_t *curve, config_t *config, ...) { pari_sp ltop = avma; GEN v = gen_0; switch (typ(curve->field)) { @@ -48,7 +48,7 @@ int curve_init(curve_t *curve, config_t *config) { return 1; } -int curve_nonzero(curve_t *curve, config_t *config) { +int curve_nonzero(curve_t *curve, config_t *config, ...) { pari_sp ltop = avma; curve_init(curve, config); if (gequal0(ell_get_disc(curve->curve))) { @@ -59,7 +59,7 @@ int curve_nonzero(curve_t *curve, config_t *config) { } } -int curve_prime(curve_t *curve, config_t *config) { +int curve_prime(curve_t *curve, config_t *config, ...) { pari_sp ltop = avma; int nonzero = curve_nonzero(curve, config); if (nonzero == 1) { @@ -76,11 +76,11 @@ int curve_prime(curve_t *curve, config_t *config) { } } -int curve_seed_fp(curve_t *curve, config_t *config) {} +int curve_seed_fp(curve_t *curve, config_t *config, ...) {} -int curve_seed_f2m(curve_t *curve, config_t *config) {} +int curve_seed_f2m(curve_t *curve, config_t *config, ...) {} -int curve_seed(curve_t *curve, config_t *config) { +int curve_seed(curve_t *curve, config_t *config, ...) { switch (typ(curve->field)) { case t_INT: return curve_seed_fp(curve, config); @@ -92,16 +92,6 @@ int curve_seed(curve_t *curve, config_t *config) { } } -int curve_g(curve_t *curve, config_t *config) { - if (config->from_seed) { - return curve_seed(curve, config); - } else if (config->prime) { - return curve_prime(curve, config); - } else { - return curve_nonzero(curve, config); - } -} - GEN curve_params(curve_t *curve) { pari_sp ltop = avma; diff --git a/src/curve.h b/src/math/curve.h index e4e973c..d1688ff 100644 --- a/src/curve.h +++ b/src/math/curve.h @@ -6,7 +6,7 @@ #define ECGEN_CURVE_H #include <pari/pari.h> -#include "cli.h" +#include "io/cli.h" #include "types.h" /** @@ -15,7 +15,7 @@ * @param config * @return */ -int curve_init(curve_t *curve, config_t *config); +int curve_init(curve_t *curve, config_t *config, ...); /** * @@ -23,7 +23,7 @@ int curve_init(curve_t *curve, config_t *config); * @param config * @return */ -int curve_nonzero(curve_t *curve, config_t *config); +int curve_nonzero(curve_t *curve, config_t *config, ...); /** * @@ -31,7 +31,7 @@ int curve_nonzero(curve_t *curve, config_t *config); * @param config * @return */ -int curve_prime(curve_t *curve, config_t *config); +int curve_prime(curve_t *curve, config_t *config, ...); /** * @@ -39,15 +39,7 @@ int curve_prime(curve_t *curve, config_t *config); * @param config * @return */ -int curve_seed(curve_t *curve, config_t *config); - -/** - * - * @param curve - * @param config - * @return - */ -int curve_g(curve_t *curve, config_t *config); +int curve_seed(curve_t *curve, config_t *config, ...); /** * @param curve diff --git a/src/math/equation.c b/src/math/equation.c new file mode 100644 index 0000000..571ee71 --- /dev/null +++ b/src/math/equation.c @@ -0,0 +1,58 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +#include <io/input.h> +#include "equation.h" + +int eq_random(curve_t *curve, config_t *config, ...) { + int r = a_random(curve, config) + b_random(curve, config); + if (r == 2) { + return r; + } + return -1; +} + +int a_random(curve_t *curve, config_t *config, ...) { + curve->a = genrand(curve->field); + return 1; +} + +int a_input(curve_t *curve, config_t *config, ...) { + curve->a = fread_int(in, "a: ", config->bits, '\n'); + return 1; +} + +int a_zero(curve_t *curve, config_t *config, ...) { + curve->a = gen_0; + return 1; +} + +int a_one(curve_t *curve, config_t *config, ...) { + curve->a = gen_1; + return 1; +} + +int a_seed(curve_t *curve, config_t *config, ...) {} + +int b_random(curve_t *curve, config_t *config, ...) { + curve->b = genrand(curve->field); + return 1; +} + +int b_input(curve_t *curve, config_t *config, ...) { + curve->b = fread_int(in, "a: ", config->bits, '\n'); + return 1; +} + +int b_zero(curve_t *curve, config_t *config, ...) { + curve->b = gen_0; + return 1; +} + +int b_one(curve_t *curve, config_t *config, ...) { + curve->b = gen_1; + return 1; +} + +int b_seed(curve_t *curve, config_t *config, ...) {} diff --git a/src/math/equation.h b/src/math/equation.h new file mode 100644 index 0000000..72f0634 --- /dev/null +++ b/src/math/equation.h @@ -0,0 +1,31 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +#ifndef ECGEN_EQUATION_H +#define ECGEN_EQUATION_H + +#include "io/cli.h" +#include "types.h" + +int a_random(curve_t *curve, config_t *config, ...); + +int a_input(curve_t *curve, config_t *config, ...); + +int a_zero(curve_t *curve, config_t *config, ...); + +int a_one(curve_t *curve, config_t *config, ...); + +int a_seed(curve_t *curve, config_t *config, ...); + +int b_random(curve_t *curve, config_t *config, ...); + +int b_input(curve_t *curve, config_t *config, ...); + +int b_zero(curve_t *curve, config_t *config, ...); + +int b_one(curve_t *curve, config_t *config, ...); + +int b_seed(curve_t *curve, config_t *config, ...); + +#endif // ECGEN_EQUATION_H diff --git a/src/field.c b/src/math/field.c index f761b8f..ba2972c 100644 --- a/src/field.c +++ b/src/math/field.c @@ -18,7 +18,7 @@ GEN field_binaryr(long bits) { } } -int field_random(curve_t *curve, config_t *config) { +int field_random(curve_t *curve, config_t *config, ...) { switch (config->field) { case FIELD_PRIME: curve->field = field_primer(config->bits); @@ -31,7 +31,7 @@ int field_random(curve_t *curve, config_t *config) { } } -int field_input(curve_t *curve, config_t *config) { +int field_input(curve_t *curve, config_t *config, ...) { return -1; // NOT IMPLEMENTED } diff --git a/src/field.h b/src/math/field.h index f7adca1..9fde4e5 100644 --- a/src/field.h +++ b/src/math/field.h @@ -5,7 +5,7 @@ #ifndef ECGEN_FIELD_H #define ECGEN_FIELD_H -#include "cli.h" +#include "io/cli.h" #include "types.h" /** @@ -14,7 +14,7 @@ * @param config * @return */ -int field_random(curve_t *curve, config_t *config); +int field_random(curve_t *curve, config_t *config, ...); /** * @@ -22,7 +22,7 @@ int field_random(curve_t *curve, config_t *config); * @param config * @return */ -int field_input(curve_t *curve, config_t *config); +int field_input(curve_t *curve, config_t *config, ...); /** * Extract a field representation from a field. diff --git a/src/point.c b/src/math/point.c index a5a1c69..a5a1c69 100644 --- a/src/point.c +++ b/src/math/point.c diff --git a/src/point.h b/src/math/point.h index 3b69144..3b69144 100644 --- a/src/point.h +++ b/src/math/point.h diff --git a/src/poly.c b/src/math/poly.c index f888d97..f888d97 100644 --- a/src/poly.c +++ b/src/math/poly.c diff --git a/src/poly.h b/src/math/poly.h index 990c818..990c818 100644 --- a/src/poly.h +++ b/src/math/poly.h diff --git a/src/random.c b/src/math/random.c index 319ed3c..319ed3c 100644 --- a/src/random.c +++ b/src/math/random.c diff --git a/src/random.h b/src/math/random.h index 5d3d17f..5d3d17f 100644 --- a/src/random.h +++ b/src/math/random.h diff --git a/src/generators.c b/src/random/generators.c index 79320f2..9769166 100644 --- a/src/generators.c +++ b/src/random/generators.c @@ -3,12 +3,12 @@ * Copyright (C) 2017 J08nY */ #include "generators.h" -#include "curve.h" -#include "equation.h" -#include "field.h" -#include "seed.h" +#include "math/curve.h" +#include "math/equation.h" +#include "math/field.h" +#include "random/seed.h" -int gen_skip(curve_t *curve, config_t *config) { return 1; } +int gen_skip(curve_t *curve, config_t *config, ...) { return 1; } void gen_init(gen_t generators[], config_t *config) { if (config->from_seed) { @@ -26,8 +26,19 @@ void gen_init(gen_t generators[], config_t *config) { generators[OFFSET_CURVE] = &curve_seed; } else { generators[OFFSET_SEED] = &gen_skip; - generators[OFFSET_A] = &a_random; - generators[OFFSET_B] = &b_random; + + if (config->random) { + generators[OFFSET_A] = &a_random; + generators[OFFSET_B] = &b_random; + } else { + generators[OFFSET_A] = &a_input; + generators[OFFSET_B] = &b_input; + } + + if (config->koblitz) { + generators[OFFSET_A] = &a_zero; + } + if (config->prime) { generators[OFFSET_CURVE] = &curve_prime; } else { @@ -35,5 +46,9 @@ void gen_init(gen_t generators[], config_t *config) { } } - generators[OFFSET_FIELD] = &field_random; + if (config->random) { + generators[OFFSET_FIELD] = &field_random; + } else { + generators[OFFSET_FIELD] = &field_input; + } }
\ No newline at end of file diff --git a/src/generators.h b/src/random/generators.h index 87e599b..87e599b 100644 --- a/src/generators.h +++ b/src/random/generators.h diff --git a/src/seed.c b/src/random/seed.c index 411195f..a78ff24 100644 --- a/src/seed.c +++ b/src/random/seed.c @@ -3,7 +3,7 @@ * Copyright (C) 2017 J08nY */ #include "seed.h" -#include "input.h" +#include "io/input.h" seed_t *seed_new() { seed_t *seed = pari_malloc(sizeof(seed_t)); @@ -38,19 +38,19 @@ GEN seed_stoi(const char *cstr) { return gerepilecopy(ltop, seed); } -int seed_random(curve_t *curve, config_t *config) { +int seed_random(curve_t *curve, config_t *config, ...) { curve->seed = seed_new(); curve->seed->seed = random_int(160); return 1; } -int seed_argument(curve_t *curve, config_t *config) { +int seed_argument(curve_t *curve, config_t *config, ...) { curve->seed = seed_new(); curve->seed->seed = seed_stoi(config->seed); return 1; } -int seed_input(curve_t *curve, config_t *config) { +int seed_input(curve_t *curve, config_t *config, ...) { pari_sp ltop = avma; GEN str = fread_string(in, "seed:", '\n'); @@ -67,20 +67,4 @@ int seed_input(curve_t *curve, config_t *config) { curve->seed = seed_new(); curve->seed->seed = seed; return 1; -} - -int seed_g(curve_t *curve, config_t *config) { - if (config->from_seed) { - if (config->seed) { - return seed_argument(curve, config); - } else { - if (config->random) { - return seed_random(curve, config); - } else { - return seed_input(curve, config); - } - } - } else { - return 1; // seed none.. skip - } }
\ No newline at end of file diff --git a/src/random/seed.h b/src/random/seed.h new file mode 100644 index 0000000..998bdcd --- /dev/null +++ b/src/random/seed.h @@ -0,0 +1,44 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +#ifndef ECGEN_SEED_H +#define ECGEN_SEED_H + +#include "io/cli.h" +#include "types.h" + +/** + * + * @param seed + */ +void seed_free(seed_t **seed); + +/** + * + * @param curve + * @param config + * @param ... + * @return + */ +int seed_random(curve_t *curve, config_t *config, ...); + +/** + * + * @param curve + * @param config + * @param ... + * @return + */ +int seed_argument(curve_t *curve, config_t *config, ...); + +/** + * + * @param curve + * @param config + * @param ... + * @return + */ +int seed_input(curve_t *curve, config_t *config, ...); + +#endif // ECGEN_SEED_H diff --git a/src/seed.h b/src/seed.h deleted file mode 100644 index bb2b834..0000000 --- a/src/seed.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * ecgen, tool for generating Elliptic curve domain parameters - * Copyright (C) 2017 J08nY - */ -#ifndef ECGEN_SEED_H -#define ECGEN_SEED_H - -#include "cli.h" -#include "types.h" - -void seed_free(seed_t **seed); - -int seed_random(curve_t *curve, config_t *config); - -int seed_argument(curve_t *curve, config_t *config); - -int seed_input(curve_t *curve, config_t *config); - -int seed_g(curve_t *curve, config_t *config); - -#endif // ECGEN_SEED_H diff --git a/src/types.h b/src/types.h index 883dc50..dba2bc7 100644 --- a/src/types.h +++ b/src/types.h @@ -6,7 +6,7 @@ #define ECGEN_TYPES_H #include <pari/pari.h> -#include "cli.h" +#include "io/cli.h" typedef struct seed { GEN seed; } seed_t; @@ -26,6 +26,6 @@ typedef struct curve { size_t npoints; } curve_t; -typedef int (*gen_t)(curve_t *, config_t *); +typedef int (*gen_t)(curve_t *, config_t *, ...); #endif // ECGEN_TYPES_H |
