diff options
| -rw-r--r-- | CMakeLists.txt | 8 | ||||
| -rw-r--r-- | src/Makefile | 9 | ||||
| -rw-r--r-- | src/cm/cm.c | 4 | ||||
| -rw-r--r-- | src/ecgen.c | 3 | ||||
| -rw-r--r-- | src/econvert.c | 27 | ||||
| -rw-r--r-- | src/exhaustive/exhaustive.c | 22 | ||||
| -rw-r--r-- | src/exhaustive/exhaustive.h | 3 | ||||
| -rw-r--r-- | src/exhaustive/seed.c | 13 | ||||
| -rw-r--r-- | src/exhaustive/seed.h | 12 | ||||
| -rw-r--r-- | src/gp.h | 2 | ||||
| -rw-r--r-- | src/invalid/invalid.c | 103 | ||||
| -rw-r--r-- | src/io/cli.c | 43 | ||||
| -rw-r--r-- | src/io/cli.h | 3 | ||||
| -rw-r--r-- | src/io/input.c | 47 | ||||
| -rw-r--r-- | src/io/input.h | 37 | ||||
| -rw-r--r-- | src/io/output.c | 32 | ||||
| -rw-r--r-- | src/math/arg.c | 6 | ||||
| -rw-r--r-- | src/math/arg.h | 8 | ||||
| -rw-r--r-- | src/math/curve.c | 27 | ||||
| -rw-r--r-- | src/math/curve.h | 12 | ||||
| -rw-r--r-- | src/math/equation.c | 84 | ||||
| -rw-r--r-- | src/math/equation.h | 58 | ||||
| -rw-r--r-- | src/math/field.c | 22 | ||||
| -rw-r--r-- | src/math/field.h | 8 | ||||
| -rw-r--r-- | src/math/order.c | 6 | ||||
| -rw-r--r-- | src/math/order.h | 8 | ||||
| -rw-r--r-- | src/math/point.c | 82 | ||||
| -rw-r--r-- | src/math/point.h | 41 | ||||
| -rw-r--r-- | src/math/random.c | 3 | ||||
| -rw-r--r-- | src/math/types.c | 2 | ||||
| -rw-r--r-- | src/math/types.h | 9 |
31 files changed, 523 insertions, 221 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 30b6466..b912458 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,8 @@ project(ecgen) set(CMAKE_LIBRARY_PATH ${CMAKE_SOURCE_DIR}/lib) -SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -Wall -Werror") -SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -Wall") +SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -std=c11 -g -Wall -Werror -pedantic") +SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -std=c11 -O3 -Wall") add_custom_command( OUTPUT gp.c gp.h @@ -19,9 +19,9 @@ add_custom_target(gp2c ALL DEPENDS gp.c gp.h) include_directories(src) include_directories(lib) -file(GLOB SOURCES "src/*.c" "src/math/*.c" "src/cm/*.c" "src/invalid/*.c" "src/io/*.c" "src/exhaustive/*.c" ) +file(GLOB ECGEN_SRC "src/ecgen.c" "src/math/*.c" "src/cm/*.c" "src/invalid/*.c" "src/io/*.c" "src/exhaustive/*.c" ) -add_executable(ecgen ${SOURCES}) +add_executable(ecgen ${ECGEN_SRC}) find_library(parson parson/libparson.a) diff --git a/src/Makefile b/src/Makefile index 397ed22..db5936e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,10 +8,10 @@ CC=gcc GP2C=gp2c -CFLAGS=-O3 -Wall +CFLAGS=-O3 -Wall -std=c11 LDFLAGS=-L../lib -GP_CFLAGS=-O3 -Wall -fomit-frame-pointer -fno-strict-aliasing -fPIC +GP_CFLAGS=-O3 -Wall -std=c11 -fomit-frame-pointer -fno-strict-aliasing -fPIC GPFLAGS=-g -i4 INCLUDES=-I. -I../lib -Icm -Iinvalid -Iio -Irandom -Iexhaustive @@ -26,7 +26,7 @@ GPC = $(addsuffix .c, $(GP)) GPO = $(addsuffix .o, $(GP)) GPH = $(addsuffix .h, $(GP)) -SRC = $(wildcard *.c) $(wildcard */*.c) +SRC = ecgen.c $(wildcard */*.c) OBJ = $(patsubst %.c,%.o, $(SRC)) HDR = $(wildcard *.h) $(wildcard */*.h) @@ -36,6 +36,7 @@ all: ecgen ecgen: ecgen.o $(GPO) $(OBJ) $(CC) $(CFLAGS) $(INCLUDES) -o $@ $^ $(LDFLAGS) $(LIBS) + mv ecgen .. gp2c: $(GPC) $(GPH) @@ -76,4 +77,4 @@ format: clang-format -i $(SRC) clang-format -i $(HDR) -.PHONY: all gp2c clean-all clean clean-gp help format
\ No newline at end of file +.PHONY: all gp2c clean-all clean clean-gp help format diff --git a/src/cm/cm.c b/src/cm/cm.c index 6595226..8b61c65 100644 --- a/src/cm/cm.c +++ b/src/cm/cm.c @@ -5,6 +5,6 @@ #include "cm.h" int cm_do(config_t *cfg) { - //TODO implement + // TODO implement return INT_MIN; -}
\ No newline at end of file +} diff --git a/src/ecgen.c b/src/ecgen.c index e90ac27..a24bc2b 100644 --- a/src/ecgen.c +++ b/src/ecgen.c @@ -104,7 +104,8 @@ int quit(int status) { * - using ANSI X9.62 verifiably random method(from seed) * - given input * , until a curve with requested properties appears. - * - Can generate curves repeatedly until one satisfies requested properties: + * - Can generate curves repeatedly until one satisfies requested + * properties: * - -p / --prime generates curves until a prime order curve is found. * - -k / --koblitz generates a curve with fixed A = 0 parameter. * diff --git a/src/econvert.c b/src/econvert.c new file mode 100644 index 0000000..21f9304 --- /dev/null +++ b/src/econvert.c @@ -0,0 +1,27 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + */ +/** + * @file econvert.c + * @author J08nY <johny@neuromancer.sk> + * @version 0.2 + * @copyright GPL v2.0 + */ + +int main(void) {} diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c index 4d6e7a9..851e7c6 100644 --- a/src/exhaustive/exhaustive.c +++ b/src/exhaustive/exhaustive.c @@ -11,7 +11,7 @@ #include "math/point.h" #include "seed.h" -void exhaustive_init(gen_t generators[], config_t *config) { +void exhaustive_ginit(gen_t *generators, config_t *config) { if (config->from_seed) { if (config->seed) { generators[OFFSET_SEED] = &seed_argument; @@ -55,14 +55,19 @@ void exhaustive_init(gen_t generators[], config_t *config) { generators[OFFSET_FIELD] = &field_input; } - generators[OFFSET_POINTS] = &points_generators; + generators[OFFSET_POINTS] = &points_prime; +} + +void exhaustive_vinit(arg_t *argss[], config_t *config) { + // TODO implement when for example points_random is used... } int exhaustive_gen(curve_t *curve, config_t *config, gen_t generators[], - int start_offset, int end_offset) { + arg_t *argss[], int start_offset, int end_offset) { int state = start_offset; while (state != end_offset) { - int diff = generators[state](curve, config); + int diff = + generators[state](curve, config, argss ? argss[state] : NULL); if (diff == INT_MIN) { fprintf(stderr, "Error generating a curve. %i\n", state); return 0; @@ -87,14 +92,17 @@ int exhaustive_gen(curve_t *curve, config_t *config, gen_t generators[], int exhaustive_do(config_t *cfg) { gen_t generators[OFFSET_END]; - exhaustive_init(generators, cfg); + arg_t *argss[OFFSET_END]; + exhaustive_ginit(generators, cfg); + exhaustive_vinit(argss, cfg); curve_t *curve = curve_new(); - if (!exhaustive_gen(curve, cfg, generators, OFFSET_SEED, OFFSET_END)) { + if (!exhaustive_gen(curve, cfg, generators, argss, OFFSET_SEED, + OFFSET_END)) { curve_free(&curve); return 1; } output_o(curve, cfg); curve_free(&curve); return 0; -}
\ No newline at end of file +} diff --git a/src/exhaustive/exhaustive.h b/src/exhaustive/exhaustive.h index b4c963c..22bfe71 100644 --- a/src/exhaustive/exhaustive.h +++ b/src/exhaustive/exhaustive.h @@ -15,12 +15,13 @@ * @param curve * @param config * @param generators + * @param argss * @param start_offset * @param end_offset * @return */ int exhaustive_gen(curve_t *curve, config_t *config, gen_t generators[], - int start_offset, int end_offset); + arg_t *argss[], int start_offset, int end_offset); /** * diff --git a/src/exhaustive/seed.c b/src/exhaustive/seed.c index 9f6ec46..8e379bb 100644 --- a/src/exhaustive/seed.c +++ b/src/exhaustive/seed.c @@ -43,22 +43,22 @@ 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, arg_t *args) { 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, arg_t *args) { 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, arg_t *args) { pari_sp ltop = avma; - GEN str = fread_string(in, "seed:"); + GEN str = input_string("seed:"); const char *cstr = GSTR(str); if (strlen(cstr) < 20) { fprintf(stderr, "SEED must be at least 160 bits(20 characters).\n"); @@ -67,9 +67,8 @@ int seed_input(curve_t *curve, config_t *config, ...) { } GEN seed = seed_stoi(cstr); - gerepileall(ltop, 1, &seed); curve->seed = seed_new(); - curve->seed->seed = seed; + curve->seed->seed = gerepilecopy(ltop, seed); return 1; -}
\ No newline at end of file +} diff --git a/src/exhaustive/seed.h b/src/exhaustive/seed.h index 2e07dc1..9107a71 100644 --- a/src/exhaustive/seed.h +++ b/src/exhaustive/seed.h @@ -35,27 +35,27 @@ void seed_free(seed_t **seed); * * @param curve * @param config - * @param ... + * @param args * @return */ -int seed_random(curve_t *curve, config_t *config, ...); +int seed_random(curve_t *curve, config_t *config, arg_t *args); /** * * @param curve * @param config - * @param ... + * @param args * @return */ -int seed_argument(curve_t *curve, config_t *config, ...); +int seed_argument(curve_t *curve, config_t *config, arg_t *args); /** * * @param curve * @param config - * @param ... + * @param args * @return */ -int seed_input(curve_t *curve, config_t *config, ...); +int seed_input(curve_t *curve, config_t *config, arg_t *args); #endif // ECGEN_SEED_H @@ -11,4 +11,4 @@ GP;install("init_gp","v","init_gp","./gp/gp.gp.so"); */ void init_gp(void); /*End of prototype*/ -#endif //GP_H +#endif // GP_H diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c index 72a9877..6d4982a 100644 --- a/src/invalid/invalid.c +++ b/src/invalid/invalid.c @@ -11,7 +11,7 @@ #include "math/order.h" #include "math/point.h" -void invalid_init(gen_t generators[], config_t *cfg) { +void invalid_ginit(gen_t *generators, config_t *cfg) { generators[OFFSET_SEED] = &gen_skip; if (cfg->random) { generators[OFFSET_FIELD] = &field_random; @@ -53,6 +53,7 @@ size_t invalid_primes(GEN order, pari_ulong **primes) { } } } + // realloc to smaller size, to tightly fit all generated primes pari_ulong *new_primes = pari_realloc(*primes, nprimes * sizeof(pari_ulong)); if (new_primes) { @@ -61,6 +62,7 @@ size_t invalid_primes(GEN order, pari_ulong **primes) { perror("Couldn't malloc."); return 0; } + avma = ltop; return nprimes; @@ -68,14 +70,15 @@ size_t invalid_primes(GEN order, pari_ulong **primes) { size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes, size_t nprimes, curve_t ***curves) { - // Have primes, now generate random b gen_t invalid_gen[OFFSET_END]; invalid_gen[OFFSET_FIELD] = &gen_skip; invalid_gen[OFFSET_A] = &gen_skip; invalid_gen[OFFSET_B] = &b_random; invalid_gen[OFFSET_CURVE] = &curve_nonzero; invalid_gen[OFFSET_ORDER] = &order_init; - invalid_gen[OFFSET_POINTS] = &points_prime; + invalid_gen[OFFSET_POINTS] = &points_primet; + + arg_t *invalid_argss[OFFSET_END]; // We will have nprimes curves in the end *curves = pari_malloc(nprimes * sizeof(curve_t *)); @@ -96,28 +99,86 @@ size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes, while (ncurves < nprimes) { pari_sp btop = avma; // generate a curve with random b - exhaustive_gen(invalid, cfg, invalid_gen, OFFSET_B, OFFSET_END); + exhaustive_gen(invalid, cfg, invalid_gen, NULL, OFFSET_B, + OFFSET_POINTS); // does some small prime from our array divide the curve order? - size_t count = 0; + // if so how many? + size_t total = 0; for (size_t i = nprimes; i-- > 0;) { - if (dvdis(invalid->order, primes[i]) && (*curves)[i] == NULL) { - if (count == 0) { - (*curves)[i] = invalid; - } else { - (*curves)[i] = curve_new(); - (*curves)[i] = curve_copy(invalid, (*curves)[i]); + if ((*curves)[i] == NULL && dvdis(invalid->order, primes[i])) { + // whoo we have a new invalid curve + if (!total && cfg->verbose) { + fprintf( + debug, + "we have a new one, calculating prime order points.\n"); } - output_o((*curves)[i], cfg); - ncurves++; - count++; + total++; } } - if (count > 0) { + + 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 + // a point with given prime order. + size_t j = 0; + pari_ulong dprimes[total]; + for (size_t i = 0; i < nprimes; ++i) { + if ((*curves)[i] == NULL && dvdis(invalid->order, primes[i])) { + if (cfg->verbose) { + fprintf(debug, "prime %lu divides curve order.\n", + primes[i]); + } + dprimes[j++] = primes[i]; + } + } + arg_t prime_divisors = {dprimes, total}; + invalid_argss[OFFSET_POINTS] = &prime_divisors; + + // generate prime order points, this is expensive (order needs to be + // factorised, so only do it if we want the curve) + exhaustive_gen(invalid, cfg, invalid_gen, invalid_argss, + OFFSET_POINTS, OFFSET_END); + + size_t count = 0; + for (size_t i = nprimes; i-- > 0;) { + if ((*curves)[i] == NULL && dvdis(invalid->order, primes[i])) { + if (count == 0) { + // save a copy on first prime divisor from range + (*curves)[i] = invalid; + } else { + // copy if pointer already assigned + (*curves)[i] = curve_new(); + (*curves)[i] = curve_copy(invalid, (*curves)[i]); + } + output_o((*curves)[i], cfg); + ncurves++; + count++; + } + } + + // copy the curve params that stay into a new curve, since this + // curve pointer got assigned + // this is for performance reasons. As it only copies the curve + // and mallocs memory if this curve is saved. invalid = curve_new(); invalid->field = gcopy(curve->field); invalid->a = gcopy(curve->a); + + if (cfg->verbose) { + fprintf(debug, + "curve saved: %lu primes from range divide order.\n", + total); + fprintf(debug, "curves done: %lu out of %lu needed. %.0f%% \n", + ncurves, nprimes, ((float)(ncurves) / nprimes) * 100); + } } else { + // here, we generated the curve but didn't use it, because no + // primes from range divided order. Thus remove it + // like it never existed. + + points_free_deep(&invalid->points, invalid->npoints); avma = btop; } } @@ -129,10 +190,11 @@ int invalid_do(config_t *cfg) { // Either from input or random with -r curve_t *curve = curve_new(); gen_t gen[OFFSET_END]; - invalid_init(gen, cfg); + arg_t *argss[OFFSET_END]; + invalid_ginit(gen, cfg); // actually generate the curve - if (!exhaustive_gen(curve, cfg, gen, OFFSET_FIELD, OFFSET_POINTS)) { + if (!exhaustive_gen(curve, cfg, gen, argss, OFFSET_FIELD, OFFSET_POINTS)) { curve_free(&curve); return 1; } @@ -142,6 +204,11 @@ int invalid_do(config_t *cfg) { pari_ulong *primes; size_t nprimes = invalid_primes(curve->order, &primes); + if (cfg->verbose) { + fprintf(debug, "primes upto: p_max = %lu, n = %lu\n", + primes[nprimes - 1], nprimes); + } + curve_t **curves; size_t ncurves = invalid_curves(curve, cfg, primes, nprimes, &curves); @@ -153,4 +220,4 @@ int invalid_do(config_t *cfg) { curve_free(&curve); return 0; -}
\ No newline at end of file +} diff --git a/src/io/cli.c b/src/io/cli.c index f9097d9..22fd457 100644 --- a/src/io/cli.c +++ b/src/io/cli.c @@ -6,12 +6,13 @@ #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_RANDOM = 'r', OPT_SEED = 's', @@ -39,6 +40,7 @@ struct argp_option options[] = { {"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."}, + //{"count", OPT_COUNT, "COUNT", 0, "Generate multiple curves."}, // Other {"data-dir", OPT_DATADIR, "DIR", 0, "PARI/GP data directory (containing seadata package)."}, {"format", OPT_FORMAT, "FORMAT", 0, "Format to output in. One of [csv,json], default is json."}, @@ -56,6 +58,11 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) { case OPT_DATADIR: cfg->datadir = arg; break; + case OPT_COUNT: + if (arg) { + cfg->count = strtol(arg, NULL, 10); + } + break; case OPT_FORMAT: if (arg) { if (!strcmp(arg, "csv")) { @@ -64,11 +71,13 @@ error_t parse_opt(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."); + argp_failure( + state, 1, 0, + "You have to specify a format with the format option."); } break; case OPT_INPUT: @@ -110,8 +119,8 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) { // 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; } @@ -136,22 +145,26 @@ error_t parse_opt(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)) { // 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)) { 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: diff --git a/src/io/cli.h b/src/io/cli.h index 96871ce..76e17db 100644 --- a/src/io/cli.h +++ b/src/io/cli.h @@ -17,12 +17,13 @@ extern char args_doc[]; extern struct argp_option options[]; enum field_e { FIELD_PRIME, FIELD_BINARY }; -enum format_e { FORMAT_JSON, FORMAT_CSV }; +enum format_e { FORMAT_JSON, FORMAT_CSV }; typedef struct config_t { enum field_e field; bool binary_field; bool prime_field; + long count; bool random; bool prime; bool invalid; diff --git a/src/io/input.c b/src/io/input.c index 780e4d3..34aeb5a 100644 --- a/src/io/input.c +++ b/src/io/input.c @@ -2,24 +2,29 @@ * ecgen, tool for generating Elliptic curve domain parameters * Copyright (C) 2017 J08nY */ +#define _POSIX_C_SOURCE 200809L #include "input.h" #include <parson/parson.h> +#include "output.h" FILE *in; int delim; -GEN fread_i(FILE *stream, const char *prompt, long bits) { - if (prompt) { - printf("%s ", prompt); +GEN input_i(const char *prompt, long bits) { + if (prompt && out == stdout) { + fprintf(out, "%s ", prompt); } char *line = NULL; size_t n = 0; - ssize_t len = getdelim(&line, &n, delim, stream); + ssize_t len = getdelim(&line, &n, delim, in); if (len == 1) { free(line); return gen_m1; } + for (size_t i = 0, j = 0; (line[j] = line[i]); j += !isspace(line[i++])) + ; + pari_sp ltop = avma; GEN in = strtoi(line); free(line); @@ -34,8 +39,8 @@ GEN fread_i(FILE *stream, const char *prompt, long bits) { } } -GEN fread_prime(FILE *stream, const char *prompt, long bits) { - GEN read = fread_i(stream, prompt, bits); +GEN input_prime(const char *prompt, long bits) { + GEN read = input_i(prompt, bits); if (equalii(read, gen_m1)) { return read; } else { @@ -48,22 +53,18 @@ GEN fread_prime(FILE *stream, const char *prompt, long bits) { } } -GEN fread_int(FILE *stream, const char *prompt, long bits) { - return fread_i(stream, prompt, bits); -} +GEN input_int(const char *prompt, long bits) { return input_i(prompt, bits); } -GEN fread_short(FILE *stream, const char *prompt) { - return fread_i(stream, prompt, 16); -} +GEN input_short(const char *prompt) { return input_i(prompt, 16); } -GEN fread_string(FILE *stream, const char *prompt) { - if (prompt) { - printf("%s ", prompt); +GEN input_string(const char *prompt) { + if (prompt && out == stdout) { + fprintf(out, "%s ", prompt); } char *line = NULL; size_t n = 0; - ssize_t len = getdelim(&line, &n, delim, stream); + ssize_t len = getdelim(&line, &n, delim, in); if (len == 1) { free(line); return strtoGENstr(""); @@ -75,24 +76,20 @@ GEN fread_string(FILE *stream, const char *prompt) { return result; } -GEN fread_param(param_t param, FILE *stream, const char *prompt, long bits) { +GEN input_param(param_t param, const char *prompt, long bits) { switch (param) { case PARAM_PRIME: - return fread_prime(stream, prompt, bits); + return input_prime(prompt, bits); case PARAM_INT: - return fread_int(stream, prompt, bits); + return input_int(prompt, bits); case PARAM_SHORT: - return fread_short(stream, prompt); + return input_short(prompt); case PARAM_STRING: - return fread_string(stream, prompt); + return input_string(prompt); } return gen_m1; } -GEN read_param(param_t param, const char *prompt, long bits) { - return fread_param(param, stdin, prompt, bits); -} - void input_init(config_t *cfg) { json_set_allocation_functions(pari_malloc, pari_free); diff --git a/src/io/input.h b/src/io/input.h index b76159d..3be68fc 100644 --- a/src/io/input.h +++ b/src/io/input.h @@ -20,40 +20,57 @@ typedef enum PARAM { /** * - * @param stream * @param prompt * @param bits * @return */ -GEN fread_prime(FILE *stream, const char *prompt, long bits); +GEN input_prime(const char *prompt, long bits); /** * - * @param stream * @param prompt * @param bits * @return */ -GEN fread_int(FILE *stream, const char *prompt, long bits); +GEN input_int(const char *prompt, long bits); /** * - * @param stream * @param prompt * @return */ -GEN fread_short(FILE *stream, const char *prompt); +GEN input_short(const char *prompt); -GEN fread_string(FILE *stream, const char *prompt); - -GEN fread_param(param_t param, FILE *stream, const char *prompt, long bits); +/** + * + * @param prompt + * @return + */ +GEN input_string(const char *prompt); -GEN read_param(param_t param, const char *prompt, long bits); +/** + * + * @param param + * @param prompt + * @param bits + * @return + */ +GEN input_param(param_t param, const char *prompt, long bits); +/** + * + */ extern FILE *in; +/** + * + * @param cfg + */ void input_init(config_t *cfg); +/** + * + */ void input_quit(void); #endif // ECGEN_INPUT_H diff --git a/src/io/output.c b/src/io/output.c index 7b3e8a1..e1e08bd 100644 --- a/src/io/output.c +++ b/src/io/output.c @@ -5,13 +5,21 @@ #include "output.h" #include <parson/parson.h> -#include "math/field.h" #include "math/curve.h" - +#include "math/field.h" FILE *out; FILE *debug; +int fprintff(FILE *stream, const char *fmt, ...) { + va_list arg; + va_start(arg, fmt); + int result = vfprintf(stream, fmt, arg); + fflush(stream); + va_end(arg); + return result; +} + char *output_scsv(curve_t *curve, config_t *config) { pari_sp ltop = avma; GEN vector = curve_params(curve); @@ -26,7 +34,7 @@ char *output_scsv(curve_t *curve, config_t *config) { total += lengths[i]; } - char *result = (char *) malloc(total + len); + char *result = (char *)malloc(total + len); if (!result) { perror("Couldn't malloc."); exit(1); @@ -51,7 +59,7 @@ char *output_scsv(curve_t *curve, config_t *config) { void output_fcsv(FILE *out, curve_t *curve, config_t *config) { char *string = output_scsv(curve, config); - fprintf(out, "%s\n", string); + fprintff(out, "%s\n", string); free(string); } @@ -74,10 +82,10 @@ JSON_Value *output_jjson(curve_t *curve, config_t *config) { } case FIELD_BINARY: { GEN field = field_params(curve->field); - char *e1 = pari_sprintf("%P#x", gel(field, 1)); - char *e2 = pari_sprintf("%P#x", gel(field, 2)); - char *e3 = pari_sprintf("%P#x", gel(field, 3)); - char *m = pari_sprintf("%#lx", config->bits); // maybe not? + char *m = pari_sprintf("%P#x", gel(field, 1)); + char *e1 = pari_sprintf("%P#x", gel(field, 2)); + char *e2 = pari_sprintf("%P#x", gel(field, 3)); + char *e3 = pari_sprintf("%P#x", gel(field, 4)); json_object_dotset_string(root_object, "field.m", m); json_object_dotset_string(root_object, "field.e1", e1); json_object_dotset_string(root_object, "field.e2", e2); @@ -110,10 +118,12 @@ JSON_Value *output_jjson(curve_t *curve, config_t *config) { JSON_Value *point_value = json_value_init_object(); JSON_Object *point_object = json_value_get_object(point_value); - char *x = pari_sprintf("%P#x", field_elementi(gel(curve->points[i]->point, 1))); + char *x = pari_sprintf( + "%P#x", field_elementi(gel(curve->points[i]->point, 1))); json_object_set_string(point_object, "x", x); pari_free(x); - char *y = pari_sprintf("%P#x", field_elementi(gel(curve->points[i]->point, 2))); + char *y = pari_sprintf( + "%P#x", field_elementi(gel(curve->points[i]->point, 2))); json_object_set_string(point_object, "y", y); pari_free(y); char *p_order = pari_sprintf("%P#x", curve->points[i]->order); @@ -138,7 +148,7 @@ char *output_sjson(curve_t *curve, config_t *config) { void output_fjson(FILE *out, curve_t *curve, config_t *config) { char *s = output_sjson(curve, config); - fprintf(out, "%s", s); + fprintff(out, "%s", s); json_free_serialized_string(s); } diff --git a/src/math/arg.c b/src/math/arg.c new file mode 100644 index 0000000..1a0d1ca --- /dev/null +++ b/src/math/arg.c @@ -0,0 +1,6 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +#include "arg.h" +#include "types.h" diff --git a/src/math/arg.h b/src/math/arg.h new file mode 100644 index 0000000..65ea311 --- /dev/null +++ b/src/math/arg.h @@ -0,0 +1,8 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +#ifndef ECGEN_ARG_H +#define ECGEN_ARG_H + +#endif // ECGEN_ARG_H diff --git a/src/math/curve.c b/src/math/curve.c index 42844ff..3892704 100644 --- a/src/math/curve.c +++ b/src/math/curve.c @@ -35,18 +35,13 @@ curve_t *curve_copy(curve_t *src, curve_t *dest) { void curve_free(curve_t **curve) { if (*curve) { seed_free(&(*curve)->seed); - if ((*curve)->points) { - for (size_t i = 0; i < (*curve)->npoints; ++i) { - point_free(&(*curve)->points[i]); - } - points_free(&(*curve)->points); - } + points_free_deep(&(*curve)->points, (*curve)->npoints); pari_free(*curve); *curve = NULL; } } -int curve_init(curve_t *curve, config_t *config, ...) { +int curve_init(curve_t *curve, config_t *config, arg_t *args) { pari_sp ltop = avma; GEN v = gen_0; switch (typ(curve->field)) { @@ -69,9 +64,9 @@ 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, arg_t *args) { pari_sp ltop = avma; - curve_init(curve, config); + curve_init(curve, config, args); if (gequal0(ell_get_disc(curve->curve))) { avma = ltop; return -3; @@ -80,22 +75,22 @@ int curve_nonzero(curve_t *curve, config_t *config, ...) { } } -int curve_seed_fp(curve_t *curve, config_t *config, ...) { - //TODO implement +int curve_seed_fp(curve_t *curve, config_t *config, arg_t *args) { + // TODO implement return INT_MIN; } -int curve_seed_f2m(curve_t *curve, config_t *config, ...) { - //TODO implement +int curve_seed_f2m(curve_t *curve, config_t *config, arg_t *args) { + // TODO implement return INT_MIN; } -int curve_seed(curve_t *curve, config_t *config, ...) { +int curve_seed(curve_t *curve, config_t *config, arg_t *args) { switch (typ(curve->field)) { case t_INT: - return curve_seed_fp(curve, config); + return curve_seed_fp(curve, config, args); case t_FFELT: - return curve_seed_f2m(curve, config); + return curve_seed_f2m(curve, config, args); default: pari_err_TYPE("curve_seed", curve->field); return INT_MIN; /* NOT REACHABLE */ diff --git a/src/math/curve.h b/src/math/curve.h index 425d9a6..cc241b4 100644 --- a/src/math/curve.h +++ b/src/math/curve.h @@ -19,10 +19,10 @@ * * @param curve * @param config - * @param ... unused + * @param args unused * @return state diff */ -int curve_init(curve_t *curve, config_t *config, ...); +int curve_init(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) @@ -31,10 +31,10 @@ int curve_init(curve_t *curve, config_t *config, ...); * * @param curve * @param config - * @param ... unused + * @param args unused * @return state diff */ -int curve_nonzero(curve_t *curve, config_t *config, ...); +int curve_nonzero(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) @@ -44,10 +44,10 @@ int curve_nonzero(curve_t *curve, config_t *config, ...); * * @param curve * @param config - * @param ... unused + * @param args unused * @return state diff */ -int curve_seed(curve_t *curve, config_t *config, ...); +int curve_seed(curve_t *curve, config_t *config, arg_t *args); /** * Serializes curve parameters into a t_VEC: diff --git a/src/math/equation.c b/src/math/equation.c index 47060ad..023b823 100644 --- a/src/math/equation.c +++ b/src/math/equation.c @@ -5,64 +5,100 @@ #include "equation.h" #include "io/input.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, ...) { +int a_random(curve_t *curve, config_t *config, arg_t *args) { curve->a = genrand(curve->field); return 1; } -int a_input(curve_t *curve, config_t *config, ...) { - curve->a = fread_int(in, "a:", config->bits); - //TODO check if a is valid int here, if not repeat +int a_input(curve_t *curve, config_t *config, arg_t *args) { + pari_sp ltop = avma; + GEN inp = input_int("a:", config->bits); + if (gequalm1(inp)) { + avma = ltop; + return 0; + } + curve->a = gerepilecopy(ltop, inp); // TODO change a to a field element here?. a t_INTMOD or a t_FFELT. return 1; } -int a_zero(curve_t *curve, config_t *config, ...) { +static GEN a = NULL; + +int a_once(curve_t *curve, config_t *config, arg_t *args) { + if (a) { + curve->a = gcopy(a); + return 1; + } + + int inp = a_input(curve, config, args); + if (inp) { + a = gclone(curve->a); + } else { + return 0; + } + return 1; +} + +int a_zero(curve_t *curve, config_t *config, arg_t *args) { curve->a = gen_0; return 1; } -int a_one(curve_t *curve, config_t *config, ...) { +int a_one(curve_t *curve, config_t *config, arg_t *args) { curve->a = gen_1; return 1; } -int a_seed(curve_t *curve, config_t *config, ...) { - //TODO implement +int a_seed(curve_t *curve, config_t *config, arg_t *args) { + // TODO implement return INT_MIN; } -int b_random(curve_t *curve, config_t *config, ...) { +int b_random(curve_t *curve, config_t *config, arg_t *args) { curve->b = genrand(curve->field); return 1; } -int b_input(curve_t *curve, config_t *config, ...) { - curve->b = fread_int(in, "b:", config->bits); - //TODO check if a is valid int here, if not repeat +int b_input(curve_t *curve, config_t *config, arg_t *args) { + pari_sp ltop = avma; + GEN inp = input_int("b:", config->bits); + if (gequalm1(inp)) { + avma = ltop; + return 0; + } + curve->b = gerepilecopy(ltop, inp); // TODO change b to a field element here?. a t_INTMOD or a t_FFELT. return 1; } -int b_zero(curve_t *curve, config_t *config, ...) { +static GEN b = NULL; + +int b_once(curve_t *curve, config_t *config, arg_t *args) { + if (b) { + curve->b = gcopy(b); + return 1; + } + + int inp = b_input(curve, config, args); + if (inp) { + b = gclone(curve->b); + } else { + return 0; + } + return 1; +} + +int b_zero(curve_t *curve, config_t *config, arg_t *args) { curve->b = gen_0; return 1; } -int b_one(curve_t *curve, config_t *config, ...) { +int b_one(curve_t *curve, config_t *config, arg_t *args) { curve->b = gen_1; return 1; } -int b_seed(curve_t *curve, config_t *config, ...) { - //TODO implement +int b_seed(curve_t *curve, config_t *config, arg_t *args) { + // TODO implement return INT_MIN; } diff --git a/src/math/equation.h b/src/math/equation.h index 76b5e06..4e0202e 100644 --- a/src/math/equation.h +++ b/src/math/equation.h @@ -19,10 +19,10 @@ * * @param curve * @param config - * @param ... + * @param args * @return state diff */ -int a_random(curve_t *curve, config_t *config, ...); +int a_random(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) @@ -30,10 +30,21 @@ int a_random(curve_t *curve, config_t *config, ...); * * @param curve * @param config - * @param ... + * @param args * @return state diff */ -int a_input(curve_t *curve, config_t *config, ...); +int a_input(curve_t *curve, config_t *config, arg_t *args); + +/** + * GENERATOR(gen_t) + * Creates a parameter by reading once from input. + * + * @param curve + * @param config + * @param args + * @return + */ +int a_once(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) @@ -41,10 +52,10 @@ int a_input(curve_t *curve, config_t *config, ...); * * @param curve * @param config - * @param ... + * @param args * @return state diff */ -int a_zero(curve_t *curve, config_t *config, ...); +int a_zero(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) @@ -52,12 +63,12 @@ int a_zero(curve_t *curve, config_t *config, ...); * * @param curve * @param config - * @param ... + * @param args * @return state diff */ -int a_one(curve_t *curve, config_t *config, ...); +int a_one(curve_t *curve, config_t *config, arg_t *args); -int a_seed(curve_t *curve, config_t *config, ...); +int a_seed(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) @@ -66,10 +77,10 @@ int a_seed(curve_t *curve, config_t *config, ...); * * @param curve * @param config - * @param ... + * @param args * @return state diff */ -int b_random(curve_t *curve, config_t *config, ...); +int b_random(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) @@ -77,10 +88,21 @@ int b_random(curve_t *curve, config_t *config, ...); * * @param curve * @param config - * @param ... + * @param args * @return state diff */ -int b_input(curve_t *curve, config_t *config, ...); +int b_input(curve_t *curve, config_t *config, arg_t *args); + +/** + * GENERATOR(gen_t) + * Creates b parameter by reading once from input. + * + * @param curve + * @param config + * @param args + * @return + */ +int b_once(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) @@ -88,10 +110,10 @@ int b_input(curve_t *curve, config_t *config, ...); * * @param curve * @param config - * @param ... + * @param args * @return state diff */ -int b_zero(curve_t *curve, config_t *config, ...); +int b_zero(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) @@ -99,11 +121,11 @@ int b_zero(curve_t *curve, config_t *config, ...); * * @param curve * @param config - * @param ... + * @param args * @return state diff */ -int b_one(curve_t *curve, config_t *config, ...); +int b_one(curve_t *curve, config_t *config, arg_t *args); -int b_seed(curve_t *curve, config_t *config, ...); +int b_seed(curve_t *curve, config_t *config, arg_t *args); #endif // ECGEN_EQUATION_H diff --git a/src/math/field.c b/src/math/field.c index cd35990..09b9a51 100644 --- a/src/math/field.c +++ b/src/math/field.c @@ -19,7 +19,7 @@ GEN field_binaryr(long bits) { } } -int field_random(curve_t *curve, config_t *config, ...) { +int field_random(curve_t *curve, config_t *config, arg_t *args) { switch (config->field) { case FIELD_PRIME: curve->field = field_primer(config->bits); @@ -32,11 +32,11 @@ 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, arg_t *args) { pari_sp ltop = avma; switch (config->field) { case FIELD_PRIME: { - GEN p = fread_prime(in, "p:", config->bits); + GEN p = input_prime("p:", config->bits); if (equalii(p, gen_m1)) { avma = ltop; return 0; @@ -45,17 +45,17 @@ int field_input(curve_t *curve, config_t *config, ...) { return 1; } case FIELD_BINARY: { - GEN e1 = fread_short(in, "e1:"); + GEN e1 = input_short("e1:"); if (equalii(e1, gen_m1)) { avma = ltop; return 0; } - GEN e2 = fread_short(in, "e2:"); + GEN e2 = input_short("e2:"); if (equalii(e2, gen_m1)) { avma = ltop; return 0; } - GEN e3 = fread_short(in, "e3:"); + GEN e3 = input_short("e3:"); if (equalii(e3, gen_m1)) { avma = ltop; return 0; @@ -75,7 +75,7 @@ int field_input(curve_t *curve, config_t *config, ...) { gel(v, 1) = gen_1; GEN poly = gmul(gtopolyrev(v, -1), gmodulss(1, 2)); - //TODO check irreducibility here + // TODO check irreducibility here GEN field = gerepilecopy(ltop, ffgen(poly, -1)); curve->field = field; @@ -93,10 +93,10 @@ GEN field_params(GEN field) { return gtovec(field); } - GEN out = gtovec0(gen_0, 3); + GEN out = gtovec0(gen_0, 4); long j = 1; - long l2 = glength(member_mod(field)) - 2; + long l2 = glength(member_mod(field)) - 1; { pari_sp btop = avma; for (long i = l2; i > 0; --i) { @@ -105,7 +105,7 @@ GEN field_params(GEN field) { gel(out, j) = stoi(i); j++; } - if (gc_needed(btop, 1)) gerepileall(btop, 3, &out, &c); + if (gc_needed(btop, 1)) gerepileall(btop, 2, &out, &c); } } return gerepilecopy(ltop, out); @@ -128,4 +128,4 @@ GEN field_elementi(GEN element) { pari_err_TYPE("field_elementi", element); return NULL; /* NOT REACHABLE */ } -}
\ No newline at end of file +} diff --git a/src/math/field.h b/src/math/field.h index 13cb283..2d6d92a 100644 --- a/src/math/field.h +++ b/src/math/field.h @@ -18,10 +18,10 @@ * * @param curve * @param config - * @param ... unused + * @param args unused * @return state diff */ -int field_random(curve_t *curve, config_t *config, ...); +int field_random(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) @@ -31,10 +31,10 @@ int field_random(curve_t *curve, config_t *config, ...); * * @param curve * @param config - * @param ... unused + * @param args unused * @return state diff */ -int field_input(curve_t *curve, config_t *config, ...); +int field_input(curve_t *curve, config_t *config, arg_t *args); /** * Extract a field representation from a field. diff --git a/src/math/order.c b/src/math/order.c index 5e15279..39222b4 100644 --- a/src/math/order.c +++ b/src/math/order.c @@ -4,12 +4,12 @@ */ #include "order.h" -int order_init(curve_t *curve, config_t *cfg, ...) { +int order_init(curve_t *curve, config_t *cfg, arg_t *args) { curve->order = ellff_get_card(curve->curve); return 1; } -int order_prime(curve_t *curve, config_t *cfg, ...) { +int order_prime(curve_t *curve, config_t *cfg, arg_t *args) { pari_sp ltop = avma; curve->order = ellsea(curve->curve, 1); if (gequal0(curve->order) || !(isprime(curve->order))) { @@ -18,4 +18,4 @@ int order_prime(curve_t *curve, config_t *cfg, ...) { } else { return 1; } -}
\ No newline at end of file +} diff --git a/src/math/order.h b/src/math/order.h index 91b0ada..a1ed861 100644 --- a/src/math/order.h +++ b/src/math/order.h @@ -17,10 +17,10 @@ * * @param curve * @param cfg - * @param ... + * @param args * @return state diff */ -int order_init(curve_t *curve, config_t *cfg, ...); +int order_init(curve_t *curve, config_t *cfg, arg_t *args); /** * GENERATOR(gen_t) @@ -30,9 +30,9 @@ int order_init(curve_t *curve, config_t *cfg, ...); * * @param curve * @param cfg - * @param ... + * @param args * @return state diff */ -int order_prime(curve_t *curve, config_t *cfg, ...); +int order_prime(curve_t *curve, config_t *cfg, arg_t *args); #endif // ECGEN_ORDER_H diff --git a/src/math/point.c b/src/math/point.c index 0bedc11..c2cd02a 100644 --- a/src/math/point.c +++ b/src/math/point.c @@ -52,7 +52,16 @@ void points_free(point_t ***points) { } } -int point_random(curve_t *curve, config_t *config, ...) { +void points_free_deep(point_t ***points, size_t npoints) { + if (*points) { + for (size_t i = 0; i < npoints; ++i) { + point_free(&(*points)[i]); + } + points_free(points); + } +} + +int point_random(curve_t *curve, config_t *config, arg_t *args) { point_t *p = point_new(); p->point = genrand(curve->curve); p->order = ellorder(curve->curve, p->point, NULL); @@ -63,11 +72,12 @@ int point_random(curve_t *curve, config_t *config, ...) { return 1; } -int points_random(curve_t *curve, config_t *config, ...) { - va_list arg; - va_start(arg, config); - size_t npoints = va_arg(arg, size_t); - va_end(arg); +int points_random(curve_t *curve, config_t *config, arg_t *args) { + if (!args) { + fprintf(stderr, "No args to an arged function. points_random"); + return INT_MIN; + } + size_t npoints = *(size_t *)args->args; curve->points = points_new(npoints); curve->npoints = npoints; @@ -80,7 +90,58 @@ int points_random(curve_t *curve, config_t *config, ...) { return 1; } -int points_prime(curve_t *curve, config_t *config, ...) { +/* + * GEN o = utoi(dprimes[i]); + GEN mul = ellmul(curve->curve, rand, o); + + if (gequal0(mul)) { + printf("Success! %lu\n", npoints); + curve->points[i] = point_new(); + + gerepileall(btop, 2, &rand, &o); + curve->points[i]->point = rand; + curve->points[i]->order = o; + npoints++; + break; + } + */ + +int points_primet(curve_t *curve, config_t *config, arg_t *args) { + // TODO stack code!!! + if (!args) { + fprintf(stderr, "No args to an arged function. points_primet"); + return INT_MIN; + } + pari_ulong *primes = (pari_ulong *)args->args; + size_t nprimes = args->nargs; + + curve->points = points_new(nprimes); + curve->npoints = nprimes; + + size_t npoints = 0; + while (npoints < nprimes) { + GEN rand = genrand(curve->curve); + GEN ord = ellorder(curve->curve, rand, NULL); + + for (long i = 0; i < nprimes; ++i) { + if (curve->points[i] == NULL && dvdis(ord, primes[i])) { + GEN p = stoi(primes[i]); + GEN mul = divii(ord, p); + GEN point = ellmul(curve->curve, rand, mul); + + curve->points[i] = point_new(); + curve->points[i]->point = point; + curve->points[i]->order = p; + npoints++; + } + } + } + + return 1; +} + +int points_prime(curve_t *curve, config_t *config, arg_t *args) { + // TODO stack code!!! GEN factors = Z_factor(curve->order); GEN primes = gel(factors, 1); long nprimes = glength(primes); @@ -94,7 +155,7 @@ int points_prime(curve_t *curve, config_t *config, ...) { // ord(rand) = ord for (long i = 1; i <= nprimes; ++i) { - if (dvdii(ord, gel(primes, i)) && curve->points[i - 1] == NULL) { + if (curve->points[i - 1] == NULL && dvdii(ord, gel(primes, i))) { // primes[i] divides ord // mul = ord/primes[i] GEN mul = divii(ord, gel(primes, i)); @@ -112,7 +173,8 @@ int points_prime(curve_t *curve, config_t *config, ...) { return 1; } -int points_generators(curve_t *curve, config_t *config, ...) { +int points_generators(curve_t *curve, config_t *config, arg_t *args) { + // TODO stack code!!! GEN generators = ellff_get_gens(curve->curve); long len = glength(generators); curve->points = points_new((size_t)len); @@ -126,4 +188,4 @@ int points_generators(curve_t *curve, config_t *config, ...) { } return 1; -}
\ No newline at end of file +} diff --git a/src/math/point.h b/src/math/point.h index bd7d2dd..fe9aeb4 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -54,24 +54,49 @@ point_t **points_copy(point_t **src, point_t **dest, size_t num); void points_free(point_t ***point); /** + * + * @param points + * @param npoints + */ +void points_free_deep(point_t ***points, size_t npoints); + +/** + * GENERATOR(gen_t) + * + * @param curve + * @param config + * @param args unused + * @return state diff + */ +int point_random(curve_t *curve, config_t *config, arg_t *args); + +/** * GENERATOR(gen_t) * * @param curve * @param config - * @param ... unused + * @param args size_t number of points to generate * @return state diff */ -int point_random(curve_t *curve, config_t *config, ...); +int points_random(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) + * Generates prime order points using trial division. + * The supplied arg is of format: + * + * pari_ulong *args->args primes + * size_t args->nargs length of primes + * + * Assumes the primes divide curve order, thus that points with all + * prime orders specified exist. * * @param curve * @param config - * @param ... size_t number of points to generate + * @param args * @return state diff */ -int points_random(curve_t *curve, config_t *config, ...); +int points_primet(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) @@ -82,10 +107,10 @@ int points_random(curve_t *curve, config_t *config, ...); * * @param curve * @param config - * @param ... + * @param args * @return state diff */ -int points_prime(curve_t *curve, config_t *config, ...); +int points_prime(curve_t *curve, config_t *config, arg_t *args); /** * GENERATOR(gen_t) @@ -94,9 +119,9 @@ int points_prime(curve_t *curve, config_t *config, ...); * * @param curve * @param config - * @param ... unused + * @param args unused * @return state diff */ -int points_generators(curve_t *curve, config_t *config, ...); +int points_generators(curve_t *curve, config_t *config, arg_t *args); #endif // ECGEN_POINT_H diff --git a/src/math/random.c b/src/math/random.c index 44bdb27..94197fe 100644 --- a/src/math/random.c +++ b/src/math/random.c @@ -2,6 +2,7 @@ * ecgen, tool for generating Elliptic curve domain parameters * Copyright (C) 2017 J08nY */ +#define _POSIX_C_SOURCE 200809L #include "random.h" #include <time.h> @@ -57,4 +58,4 @@ GEN random_int(long bits) { gel(range, 2) = powis(gen_2, bits); return gerepilecopy(ltop, genrand(range)); -}
\ No newline at end of file +} diff --git a/src/math/types.c b/src/math/types.c index 958de7f..bf1bb5d 100644 --- a/src/math/types.c +++ b/src/math/types.c @@ -4,4 +4,4 @@ */ #include "types.h" -int gen_skip(curve_t *curve, config_t *config, ...) { return 1; } +int gen_skip(curve_t *curve, config_t *config, arg_t *args) { return 1; } diff --git a/src/math/types.h b/src/math/types.h index 7124573..575f583 100644 --- a/src/math/types.h +++ b/src/math/types.h @@ -40,8 +40,13 @@ enum curve_offset { OFFSET_END }; -typedef int (*gen_t)(curve_t *, config_t *, ...); +typedef struct arg_t { + void *args; + size_t nargs; +} arg_t; -int gen_skip(curve_t *curve, config_t *config, ...); +typedef int (*gen_t)(curve_t *, config_t *, arg_t *args); + +int gen_skip(curve_t *curve, config_t *config, arg_t *args); #endif // ECGEN_TYPES_H |
