From ba8c1f2bc424205cbb167b3c65ce184912c6173a Mon Sep 17 00:00:00 2001 From: J08nY Date: Mon, 29 May 2017 15:11:38 +0200 Subject: Add some more comments and docs, move exhaustive/seed.[ch] into math/ --- src/ecgen.c | 13 +++--- src/econvert.c | 4 +- src/exhaustive/anomalous.c | 1 - src/exhaustive/anomalous.h | 31 +++++++++++++ src/exhaustive/exhaustive.c | 6 +-- src/exhaustive/seed.c | 104 -------------------------------------------- src/exhaustive/seed.h | 83 ----------------------------------- src/invalid/invalid.c | 4 +- src/io/cli.h | 14 ++++++ src/io/input.c | 19 ++------ src/io/input.h | 23 ++-------- src/io/output.c | 3 +- src/io/output.h | 3 +- src/math/curve.c | 2 +- src/math/curve.h | 12 ++--- src/math/equation.h | 86 ++++++++++++++++++------------------ src/math/field.h | 25 ++++++----- src/math/gens.h | 23 +++++----- src/math/order.h | 35 +++++++-------- src/math/point.h | 32 ++++++-------- src/math/poly.c | 4 +- src/math/poly.h | 30 ++++++++----- src/math/random.h | 4 +- src/math/seed.c | 104 ++++++++++++++++++++++++++++++++++++++++++++ src/math/seed.h | 83 +++++++++++++++++++++++++++++++++++ src/math/types.h | 21 +++++---- src/util/macro.h | 14 ------ src/util/memory.c | 4 +- src/util/memory.h | 6 +-- 29 files changed, 408 insertions(+), 385 deletions(-) delete mode 100644 src/exhaustive/seed.c delete mode 100644 src/exhaustive/seed.h create mode 100644 src/math/seed.c create mode 100644 src/math/seed.h delete mode 100644 src/util/macro.h diff --git a/src/ecgen.c b/src/ecgen.c index ea8f8ff..3d3b1af 100644 --- a/src/ecgen.c +++ b/src/ecgen.c @@ -27,7 +27,6 @@ #include "cm/cm.h" #include "exhaustive/exhaustive.h" #include "invalid/invalid.h" -#include "io/cli.h" #include "io/input.h" #include "io/output.h" @@ -58,15 +57,19 @@ bool init(void) { // init the modular polynomial db from seadata pari_sp ltop = avma; - pari_CATCH(e_FILE) {} + pari_CATCH(e_FILE) { + fprintf( + stderr, + "seadata not found, this will probably take quite some time.\n"); + } pari_TRY { ellmodulareqn(2, -1, -1); } pari_ENDCATCH avma = ltop; // open outfile - output_init(&cfg); + if (!output_init(&cfg)) return false; // open infile - input_init(&cfg); + if (!input_init(&cfg)) return false; return true; } @@ -134,7 +137,7 @@ int main(int argc, char *argv[]) { argp_parse(&argp, argc, argv, 0, 0, &cfg); if (!init()) { - return quit(1); + return quit(EXIT_FAILURE); } int status; diff --git a/src/econvert.c b/src/econvert.c index 0f9b2ad..bc35f36 100644 --- a/src/econvert.c +++ b/src/econvert.c @@ -26,6 +26,4 @@ * @copyright GPL v2.0 */ -int main(void) { - fprintf(stderr, "This is not implemented *yet*.\n"); -} +int main(void) { fprintf(stderr, "This is not implemented *yet*.\n"); } diff --git a/src/exhaustive/anomalous.c b/src/exhaustive/anomalous.c index 44f8780..89cc89c 100644 --- a/src/exhaustive/anomalous.c +++ b/src/exhaustive/anomalous.c @@ -113,7 +113,6 @@ int anomalous_equation(curve_t *curve, const config_t *cfg, arg_t *args) { } size_t i = *(size_t *)args->args; - // 1 on success, -2 on failure pari_sp ltop = avma; GEN c = anomalous_c(i, curve->field); diff --git a/src/exhaustive/anomalous.h b/src/exhaustive/anomalous.h index ac86018..849cb55 100644 --- a/src/exhaustive/anomalous.h +++ b/src/exhaustive/anomalous.h @@ -19,14 +19,45 @@ typedef struct disc_t { GEN alpha; } disc_t; +/** + * GENERATOR(gen_t) + * + * @param curve A curve_t being generated + * @param cfg An application config + * @param args the index of the discriminant to use, in the disc_table + * @return state diff + */ int anomalous_field(curve_t *curve, const config_t *cfg, arg_t *args); +/** + * GENERATOR(gen_t) + * + * @param curve A curve_t being generated + * @param cfg An application config + * @param args the index of the discriminant to use, in the disc_table + * @return state diff + */ int anomalous_equation(curve_t *curve, const config_t *cfg, arg_t *args); +/** + * GENERATOR(gen_t) + * + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused + * @return state diff + */ int anomalous_order(curve_t *curve, const config_t *cfg, arg_t *args); +/** + * @brief Initialize anomalous generation, allocate and set the disc_table. + */ void anomalous_init(); +/** + * @brief Deinitialize anomalous generation, free the discriminants from the + * disc_table. + */ void anomalous_quit(); #endif // ECGEN_ANOMALOUS_H diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c index eb2c69e..d070b60 100644 --- a/src/exhaustive/exhaustive.c +++ b/src/exhaustive/exhaustive.c @@ -11,7 +11,7 @@ #include "math/gens.h" #include "math/order.h" #include "math/point.h" -#include "seed.h" +#include "math/seed.h" #include "util/memory.h" static void exhaustive_ginit(gen_t *generators, const config_t *cfg) { @@ -246,7 +246,7 @@ int exhaustive_do(config_t *cfg) { if (!exhaustive_gen(curve, cfg, generators, argss, unrolls, OFFSET_SEED, OFFSET_END)) { curve_free(&curve); - return 1; + return EXIT_FAILURE; } debug_log_end("Generated new curve"); @@ -260,5 +260,5 @@ int exhaustive_do(config_t *cfg) { exhaustive_quit(argss); debug_log_end("Finished Exhaustive method"); - return 0; + return EXIT_SUCCESS; } diff --git a/src/exhaustive/seed.c b/src/exhaustive/seed.c deleted file mode 100644 index 44663c8..0000000 --- a/src/exhaustive/seed.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * ecgen, tool for generating Elliptic curve domain parameters - * Copyright (C) 2017 J08nY - */ - -#include "seed.h" -#include "util/memory.h" - -seed_t *seed_new(void) { return try_calloc(sizeof(seed_t)); } - -seed_t *seed_copy(const seed_t *src, seed_t *dest) { - if (src->seed) dest->seed = gcopy(src->seed); - return dest; -} - -seed_t *seed_new_copy(const seed_t *src) { - seed_t *result = seed_new(); - return seed_copy(src, result); -} - -seed_t *seed_clone(const seed_t *src, seed_t *dest) { - if (src->seed) dest->seed = gclone(src->seed); - return dest; -} - -seed_t *seed_new_clone(const seed_t *src) { - seed_t *result = seed_new(); - return seed_clone(src, result); -} - -void seed_free(seed_t **seed) { - if (*seed) { - if ((*seed)->seed && isclone((*seed)->seed)) { - gunclone((*seed)->seed); - } - pari_free(*seed); - *seed = NULL; - } -} - -static GEN seed_stoi(const char *cstr) { - pari_sp ltop = avma; - GEN seed = gen_0; - - size_t len = strlen(cstr); - for (size_t i = 0; i < len; ++i) { - pari_sp btop = avma; - GEN s = stoi(cstr[i]); - s = shifti(s, (len - i - 1) * 8); - seed = addii(seed, s); - gerepileall(btop, 1, &seed); - } - - return gerepilecopy(ltop, seed); -} - -char *seed_itos(GEN seed) { - pari_sp ltop = avma; - GEN bits = binary_zv(seed); - - long len = glength(bits); - long bytes = (len / 8) + (len % 8 == 0 ? 0 : 1); - char *result = try_malloc((size_t)bytes); - - for (long i = 0; i < len; ++i) { - // TODO - } - avma = ltop; - return result; -} - -int seed_random(curve_t *curve, const config_t *cfg, arg_t *args) { - curve->seed = seed_new(); - curve->seed->seed = random_int(160); - curve->seed->raw = seed_itos(curve->seed->seed); - curve->seed->raw_len = strlen(curve->seed->raw); - return 1; -} - -int seed_argument(curve_t *curve, const config_t *cfg, arg_t *args) { - curve->seed = seed_new(); - curve->seed->seed = seed_stoi(cfg->seed); - curve->seed->raw = cfg->seed; - curve->seed->raw_len = strlen(cfg->seed); - return 1; -} - -int seed_input(curve_t *curve, const config_t *cfg, arg_t *args) { - pari_sp ltop = avma; - - 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"); - avma = ltop; - return 0; - } - - GEN seed = seed_stoi(cstr); - - curve->seed = seed_new(); - curve->seed->seed = gerepilecopy(ltop, seed); - return 1; -} diff --git a/src/exhaustive/seed.h b/src/exhaustive/seed.h deleted file mode 100644 index 89c7e07..0000000 --- a/src/exhaustive/seed.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - * ecgen, tool for generating Elliptic curve domain parameters - * Copyright (C) 2017 J08nY - */ -/** - * @file seed.h - */ -#ifndef ECGEN_SEED_H -#define ECGEN_SEED_H - -#include "io/input.h" -#include "math/types.h" - -/** - * - * @return - */ -seed_t *seed_new(void); - -/** - * - * @param src - * @param dest - * @return - */ -seed_t *seed_copy(const seed_t *src, seed_t *dest); - -/** - * - * @param src - * @return - */ -seed_t *seed_new_copy(const seed_t *src); - -/** - * - * @param src - * @param dest - * @return - */ -seed_t *seed_clone(const seed_t *src, seed_t *dest); - -/** - * - * @param src - * @return - */ -seed_t *seed_new_clone(const seed_t *src); - -/** - * - * @param seed - */ -void seed_free(seed_t **seed); - -/** - * - * @param curve - * @param config - * @param args - * @return - */ -int seed_random(curve_t *curve, const config_t *cfg, arg_t *args); - -/** - * - * @param curve - * @param config - * @param args - * @return - */ -int seed_argument(curve_t *curve, const config_t *cfg, arg_t *args); - -/** - * - * @param curve - * @param config - * @param args - * @return - */ -int seed_input(curve_t *curve, const config_t *cfg, arg_t *args); - -#endif // ECGEN_SEED_H diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c index d817007..dee5c0a 100644 --- a/src/invalid/invalid.c +++ b/src/invalid/invalid.c @@ -290,7 +290,7 @@ int invalid_do(config_t *cfg) { if (!exhaustive_gen(curve, cfg, original_gens, original_argss, common_unrolls, OFFSET_FIELD, OFFSET_POINTS)) { curve_free(&curve); - return 1; + return EXIT_FAILURE; } debug_log_end("Finished creating curve to invalidate"); @@ -331,5 +331,5 @@ int invalid_do(config_t *cfg) { curve_free(&curve); debug_log_end("Finished Invalid curve method"); - return 0; + return EXIT_SUCCESS; } diff --git a/src/io/cli.h b/src/io/cli.h index 3ae314e..6e0ab99 100644 --- a/src/io/cli.h +++ b/src/io/cli.h @@ -16,8 +16,22 @@ extern char cli_doc[]; extern char cli_args_doc[]; extern struct argp_option cli_options[]; +/** + * @brief + * @param key + * @param arg + * @param state + * @return + */ error_t cli_parse(int key, char *arg, struct argp_state *state); +/** + * @brief + * @param key + * @param text + * @param input + * @return + */ char *cli_filter(int key, const char *text, void *input); #endif // ECGEN_CLI_H diff --git a/src/io/input.c b/src/io/input.c index 2b18a53..726b710 100644 --- a/src/io/input.c +++ b/src/io/input.c @@ -94,32 +94,19 @@ GEN input_string(const char *prompt) { return result; } -GEN input_param(param_t param, const char *prompt, unsigned long bits) { - switch (param) { - case PARAM_PRIME: - return input_prime(prompt, bits); - case PARAM_INT: - return input_int(prompt, bits); - case PARAM_SHORT: - return input_short(prompt); - case PARAM_STRING: - return input_string(prompt); - } - return gen_m1; -} - -void input_init(const config_t *cfg) { +bool input_init(const config_t *cfg) { if (cfg->input) { in = fopen(cfg->input, "r"); delim = ','; if (!in) { perror("Failed to open input file."); - exit(1); + return false; } } else { in = stdin; delim = '\n'; } + return true; } void input_quit(void) { diff --git a/src/io/input.h b/src/io/input.h index 579af3c..fa69503 100644 --- a/src/io/input.h +++ b/src/io/input.h @@ -11,13 +11,6 @@ #include "math/random.h" #include "math/types.h" -typedef enum PARAM { - PARAM_PRIME, - PARAM_INT, - PARAM_SHORT, - PARAM_STRING -} param_t; - /** * * @param prompt @@ -48,28 +41,20 @@ GEN input_short(const char *prompt); */ GEN input_string(const char *prompt); -/** - * - * @param param - * @param prompt - * @param bits - * @return - */ -GEN input_param(param_t param, const char *prompt, unsigned long bits); - /** * */ extern FILE *in; /** - * + * @brief Initialize input based on cfg. * @param cfg + * @return whether the initialization was successful */ -void input_init(const config_t *cfg); +bool input_init(const config_t *cfg); /** - * + * @brief Deinitialize input. */ void input_quit(void); diff --git a/src/io/output.c b/src/io/output.c index dafbf14..1904a22 100644 --- a/src/io/output.c +++ b/src/io/output.c @@ -292,7 +292,7 @@ void output_f_end(FILE *out, const config_t *cfg) { void output_o_end(const config_t *cfg) { output_f_end(out, cfg); } -void output_init(const config_t *cfg) { +bool output_init(const config_t *cfg) { json_set_allocation_functions(try_malloc, pari_free); if (cfg->output) { @@ -333,6 +333,7 @@ void output_init(const config_t *cfg) { break; } } + return true; } void output_quit(void) { diff --git a/src/io/output.h b/src/io/output.h index cafffc1..f04e665 100644 --- a/src/io/output.h +++ b/src/io/output.h @@ -208,8 +208,9 @@ extern FILE *verbose; /** * @brief Initialize output based on cfg. * @param cfg + * @return whether the initialization was successful */ -void output_init(const config_t *cfg); +bool output_init(const config_t *cfg); /** * @brief Deinitialize output. diff --git a/src/math/curve.c b/src/math/curve.c index 93b4f52..bc382cd 100644 --- a/src/math/curve.c +++ b/src/math/curve.c @@ -3,8 +3,8 @@ * Copyright (C) 2017 J08nY */ #include "curve.h" -#include "exhaustive/seed.h" #include "point.h" +#include "seed.h" #include "util/memory.h" curve_t *curve_new(void) { return try_calloc(sizeof(curve_t)); } diff --git a/src/math/curve.h b/src/math/curve.h index a283710..2e7651f 100644 --- a/src/math/curve.h +++ b/src/math/curve.h @@ -16,8 +16,8 @@ * Creates a curve GEN in curve_t curve from field, a and b. * Always succeeds. * - * @param curve - * @param config + * @param curve A curve_t being generated + * @param cfg An application config * @param args unused * @return state diff */ @@ -28,8 +28,8 @@ GENERATOR(curve_gen_any); * Creates a curve GEN in curve_t curve from field, a and b. * Succeeds if a curve exists(non-zero discriminant). * - * @param curve - * @param config + * @param curve A curve_t being generated + * @param cfg An application config * @param args unused * @return state diff */ @@ -41,8 +41,8 @@ GENERATOR(curve_gen_nonzero); * X9.62 verifiably random algorithm. * Succeeds if a curve exists(non-zero discriminant). * - * @param curve - * @param config + * @param curve A curve_t being generated + * @param cfg An application config * @param args unused * @return state diff */ diff --git a/src/math/equation.h b/src/math/equation.h index 79fad48..741517c 100644 --- a/src/math/equation.h +++ b/src/math/equation.h @@ -16,9 +16,9 @@ * element from the curve field. * Always succeeds. * - * @param curve - * @param config - * @param args + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused * @return state diff */ GENERATOR(a_gen_random); @@ -27,9 +27,9 @@ GENERATOR(a_gen_random); * GENERATOR(gen_t) * Creates a parameter by reading from input. * - * @param curve - * @param config - * @param args + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused * @return state diff */ GENERATOR(a_gen_input); @@ -38,10 +38,10 @@ GENERATOR(a_gen_input); * GENERATOR(gen_t) * Creates a parameter by reading once from input. * - * @param curve - * @param config - * @param args - * @return + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused + * @return state diff */ GENERATOR(a_gen_once); @@ -49,9 +49,9 @@ GENERATOR(a_gen_once); * GENERATOR(gen_t) * Creates a parameter set to zero. * - * @param curve - * @param config - * @param args + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused * @return state diff */ GENERATOR(a_gen_zero); @@ -60,19 +60,20 @@ GENERATOR(a_gen_zero); * GENERATOR(gen_t) * Creates a parameter set to one. * - * @param curve - * @param config - * @param args + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused * @return state diff */ GENERATOR(a_gen_one); /** - * @brief - * @param curve - * @param config - * @param args - * @return + * GENERATOR(gen_t) + * + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused + * @return state diff */ GENERATOR(a_gen_seed); @@ -81,9 +82,9 @@ GENERATOR(a_gen_seed); * Creates a random b parameter by selecting a random field * element from the curve field. * - * @param curve - * @param config - * @param args + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused * @return state diff */ GENERATOR(b_gen_random); @@ -92,9 +93,9 @@ GENERATOR(b_gen_random); * GENERATOR(gen_t) * Creates b parameter by reading from input. * - * @param curve - * @param config - * @param args + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused * @return state diff */ GENERATOR(b_gen_input); @@ -103,10 +104,10 @@ GENERATOR(b_gen_input); * GENERATOR(gen_t) * Creates b parameter by reading once from input. * - * @param curve - * @param config - * @param args - * @return + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused + * @return state diff */ GENERATOR(b_gen_once); @@ -114,9 +115,9 @@ GENERATOR(b_gen_once); * GENERATOR(gen_t) * Creates b parameter set to zero. * - * @param curve - * @param config - * @param args + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused * @return state diff */ GENERATOR(b_gen_zero); @@ -125,19 +126,20 @@ GENERATOR(b_gen_zero); * GENERATOR(gen_t) * Creates b parameter set to one. * - * @param curve - * @param config - * @param args + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused * @return state diff */ GENERATOR(g_gen_one); /** - * @brief - * @param curve - * @param config - * @param args - * @return + * GENERATOR(gen_t) + * + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused + * @return state diff */ GENERATOR(b_gen_seed); diff --git a/src/math/field.h b/src/math/field.h index f5dc42b..04af2c6 100644 --- a/src/math/field.h +++ b/src/math/field.h @@ -15,8 +15,8 @@ * Creates a random field. * Always succeeds. * - * @param curve - * @param config + * @param curve A curve_t being generated + * @param cfg An application config * @param args unused * @return state diff */ @@ -28,19 +28,21 @@ GENERATOR(field_gen_random); * - a prime number in the prime field case * - three short exponents of the reduction polynomial in the binary case * - * @param curve - * @param config + * @param curve A curve_t being generated + * @param cfg An application config * @param args unused * @return state diff */ GENERATOR(field_gen_input); /** + * GENERATOR(gen_t) + * Creates the field by reading it once. * - * @param curve - * @param cfg - * @param args - * @return + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused + * @return state diff */ GENERATOR(field_gen_once); @@ -67,10 +69,11 @@ GEN field_params(GEN field); GEN field_elementi(GEN element); /** + * Transforms an integer into a field element. * - * @param field - * @param in - * @return + * @param field the field to work in + * @param in the integer to transform + * @return a field element, t_INTMOD or t_FFELT */ GEN field_ielement(GEN field, GEN in); diff --git a/src/math/gens.h b/src/math/gens.h index 7ae9297..f46efbf 100644 --- a/src/math/gens.h +++ b/src/math/gens.h @@ -12,24 +12,27 @@ #include "types.h" /** - * @brief - * @param curve - * @param config - * @param args - * @return + * GENERATOR(gen_t) + * + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused + * @return state diff */ GENERATOR(gens_gen_any); /** - * @brief - * @param curve - * @param config - * @param args - * @return + * GENERATOR(gen_t) + * + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused + * @return state diff */ GENERATOR(gens_gen_one); /** + * UNROLL(unroll_t) * * @param curve * @param cfg diff --git a/src/math/order.h b/src/math/order.h index 6d1ed1f..bdb6ec0 100644 --- a/src/math/order.h +++ b/src/math/order.h @@ -31,9 +31,10 @@ GEN order_groups(curve_t *curve, const config_t *cfg, GEN factors); * GENERATOR(gen_t) * Reads the curve order from input, does not verify it. * - * @param curve - * @param cfg - * @param args + * @param curve A curve_t being generated + * @param cfg An application config + * @param args Current optional generator argument + * @return state diff * @return state diff */ GENERATOR(order_gen_input); @@ -43,9 +44,9 @@ GENERATOR(order_gen_input); * Calculates the curve order, using a general algorithm. * Always succeeds. * - * @param curve - * @param cfg - * @param args + * @param curve A curve_t being generated + * @param cfg An application config + * @param args Current optional generator argument * @return state diff */ GENERATOR(order_gen_any); @@ -54,20 +55,20 @@ GENERATOR(order_gen_any); * GENERATOR(gen_t) * Calculates the curve order, using the SEA algorithm. * - * @param curve - * @param cfg - * @param args - * @return + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused + * @return state diff */ GENERATOR(order_gen_sea); /** * GENERATOR(gen_t) * - * @param curve - * @param cfg - * @param args - * @return + * @param curve A curve_t being generated + * @param cfg An application config + * @param args pari_ulong passed to ellsea(curve, smallfact) + * @return state diff */ GENERATOR(order_gen_smallfact); @@ -77,9 +78,9 @@ GENERATOR(order_gen_smallfact); * gives up early in case the order is divisible by "something". * Succeeds if the curve has a prime order. * - * @param curve - * @param cfg - * @param args + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused * @return state diff */ GENERATOR(order_gen_prime); diff --git a/src/math/point.h b/src/math/point.h index a25ec2c..1a0b348 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -109,8 +109,8 @@ void points_free_deep(point_t ***points, size_t npoints); /** * GENERATOR(gen_t) * - * @param curve - * @param config + * @param curve A curve_t being generated + * @param cfg An application config * @param args unused * @return state diff */ @@ -119,8 +119,8 @@ GENERATOR(point_gen_random); /** * GENERATOR(gen_t) * - * @param curve - * @param config + * @param curve A curve_t being generated + * @param cfg An application config * @param args size_t number of points to generate * @return state diff */ @@ -129,17 +129,13 @@ GENERATOR(points_gen_random); /** * 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 args + * @param curve A curve_t being generated + * @param cfg An application config + * @param args pari_ulong array of primes length nargs * @return state diff */ GENERATOR(points_gen_trial); @@ -151,9 +147,9 @@ GENERATOR(points_gen_trial); * Let G be a finite group and p be a prime. If p divides the order of G, then * G has an element of order p. * - * @param curve - * @param config - * @param args + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused * @return state diff */ GENERATOR(points_gen_prime); @@ -163,10 +159,10 @@ GENERATOR(points_gen_prime); * * Generates points on all subgroups of the curve. Prime and non-prime order. * - * @param curve - * @param cfg - * @param args - * @return + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused + * @return state diff */ GENERATOR(points_gen_allgroups); diff --git a/src/math/poly.c b/src/math/poly.c index 2b655da..460770f 100644 --- a/src/math/poly.c +++ b/src/math/poly.c @@ -2740,8 +2740,6 @@ polynomial_t *poly_find(unsigned long m) { } } -GEN poly_find_gen(unsigned long m) { return poly_gen(poly_find(m)); } - GEN poly_gen(const polynomial_t *polynomial) { pari_sp ltop = avma; @@ -2755,3 +2753,5 @@ GEN poly_gen(const polynomial_t *polynomial) { GEN poly = gmul(gtopolyrev(coeffs, -1), gmodulss(1, 2)); return gerepilecopy(ltop, ffgen(poly, -1)); } + +GEN poly_find_gen(unsigned long m) { return poly_gen(poly_find(m)); } diff --git a/src/math/poly.h b/src/math/poly.h index 83b909a..89061ce 100644 --- a/src/math/poly.h +++ b/src/math/poly.h @@ -11,6 +11,9 @@ #include #include +/** + * @brief + */ typedef struct { unsigned int m; unsigned int e1; @@ -19,30 +22,37 @@ typedef struct { } polynomial_t; /** + * @brief Whether a polynomial exists in the polynomial data with degree + * m. * - * @param m - * @return + * @param m the degree of the polynomial searched + * @return whether it exists */ bool poly_exists(unsigned long m); + /** + * @brief Find a polynomial of degree m in the polynomial dataset. * - * @param m - * @return + * @param m the degree of the polynomial searched + * @return the polynomial_t * inside the polynomial dataset */ polynomial_t *poly_find(unsigned long m); /** + * @brief Turn a polynomial_t into a GEN. * - * @param m - * @return + * @param polynomial the polynomial_t to convert + * @return a t_POL equal to the polynomial */ -GEN poly_find_gen(unsigned long m); +GEN poly_gen(const polynomial_t *polynomial); /** + * @brief Find a polynomial of degree m and turn it into a GEN. * - * @param polynomial - * @return + * @see poly_gen + * @param m the degree of the polynomial searched + * @return a t_POL equal to the polynomial */ -GEN poly_gen(const polynomial_t *polynomial); +GEN poly_find_gen(unsigned long m); #endif // ECGEN_POLY_H diff --git a/src/math/random.h b/src/math/random.h index 1152bb5..96eb210 100644 --- a/src/math/random.h +++ b/src/math/random.h @@ -16,9 +16,9 @@ * * Initializes the PARI-GP random generator, tries to do so from * cryptographically strong sources(/dev/urandom) at first but falls back on - * clock_gettime and {@link time(NULL)}. + * clock_gettime and time(NULL). * - * @return Whether the initialization was successful. + * @return whether the initialization was successful */ bool random_init(void); diff --git a/src/math/seed.c b/src/math/seed.c new file mode 100644 index 0000000..44663c8 --- /dev/null +++ b/src/math/seed.c @@ -0,0 +1,104 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ + +#include "seed.h" +#include "util/memory.h" + +seed_t *seed_new(void) { return try_calloc(sizeof(seed_t)); } + +seed_t *seed_copy(const seed_t *src, seed_t *dest) { + if (src->seed) dest->seed = gcopy(src->seed); + return dest; +} + +seed_t *seed_new_copy(const seed_t *src) { + seed_t *result = seed_new(); + return seed_copy(src, result); +} + +seed_t *seed_clone(const seed_t *src, seed_t *dest) { + if (src->seed) dest->seed = gclone(src->seed); + return dest; +} + +seed_t *seed_new_clone(const seed_t *src) { + seed_t *result = seed_new(); + return seed_clone(src, result); +} + +void seed_free(seed_t **seed) { + if (*seed) { + if ((*seed)->seed && isclone((*seed)->seed)) { + gunclone((*seed)->seed); + } + pari_free(*seed); + *seed = NULL; + } +} + +static GEN seed_stoi(const char *cstr) { + pari_sp ltop = avma; + GEN seed = gen_0; + + size_t len = strlen(cstr); + for (size_t i = 0; i < len; ++i) { + pari_sp btop = avma; + GEN s = stoi(cstr[i]); + s = shifti(s, (len - i - 1) * 8); + seed = addii(seed, s); + gerepileall(btop, 1, &seed); + } + + return gerepilecopy(ltop, seed); +} + +char *seed_itos(GEN seed) { + pari_sp ltop = avma; + GEN bits = binary_zv(seed); + + long len = glength(bits); + long bytes = (len / 8) + (len % 8 == 0 ? 0 : 1); + char *result = try_malloc((size_t)bytes); + + for (long i = 0; i < len; ++i) { + // TODO + } + avma = ltop; + return result; +} + +int seed_random(curve_t *curve, const config_t *cfg, arg_t *args) { + curve->seed = seed_new(); + curve->seed->seed = random_int(160); + curve->seed->raw = seed_itos(curve->seed->seed); + curve->seed->raw_len = strlen(curve->seed->raw); + return 1; +} + +int seed_argument(curve_t *curve, const config_t *cfg, arg_t *args) { + curve->seed = seed_new(); + curve->seed->seed = seed_stoi(cfg->seed); + curve->seed->raw = cfg->seed; + curve->seed->raw_len = strlen(cfg->seed); + return 1; +} + +int seed_input(curve_t *curve, const config_t *cfg, arg_t *args) { + pari_sp ltop = avma; + + 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"); + avma = ltop; + return 0; + } + + GEN seed = seed_stoi(cstr); + + curve->seed = seed_new(); + curve->seed->seed = gerepilecopy(ltop, seed); + return 1; +} diff --git a/src/math/seed.h b/src/math/seed.h new file mode 100644 index 0000000..89c7e07 --- /dev/null +++ b/src/math/seed.h @@ -0,0 +1,83 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +/** + * @file seed.h + */ +#ifndef ECGEN_SEED_H +#define ECGEN_SEED_H + +#include "io/input.h" +#include "math/types.h" + +/** + * + * @return + */ +seed_t *seed_new(void); + +/** + * + * @param src + * @param dest + * @return + */ +seed_t *seed_copy(const seed_t *src, seed_t *dest); + +/** + * + * @param src + * @return + */ +seed_t *seed_new_copy(const seed_t *src); + +/** + * + * @param src + * @param dest + * @return + */ +seed_t *seed_clone(const seed_t *src, seed_t *dest); + +/** + * + * @param src + * @return + */ +seed_t *seed_new_clone(const seed_t *src); + +/** + * + * @param seed + */ +void seed_free(seed_t **seed); + +/** + * + * @param curve + * @param config + * @param args + * @return + */ +int seed_random(curve_t *curve, const config_t *cfg, arg_t *args); + +/** + * + * @param curve + * @param config + * @param args + * @return + */ +int seed_argument(curve_t *curve, const config_t *cfg, arg_t *args); + +/** + * + * @param curve + * @param config + * @param args + * @return + */ +int seed_input(curve_t *curve, const config_t *cfg, arg_t *args); + +#endif // ECGEN_SEED_H diff --git a/src/math/types.h b/src/math/types.h index 5d9a5b7..a6494cf 100644 --- a/src/math/types.h +++ b/src/math/types.h @@ -85,17 +85,17 @@ typedef struct { /** * @brief A generator function type. - * @param curve - * @param cfg - * @param args - * @return + * @param curve A curve_t being generated + * @param cfg An application config + * @param args Current optional generator argument + * @return state diff */ #define GENERATOR(gen_name) \ int gen_name(curve_t *curve, const config_t *cfg, arg_t *args) typedef GENERATOR((*gen_t)); /** - * @brief + * @brief An unroll function type * @param curve * @param cfg * @param from @@ -108,15 +108,18 @@ typedef GENERATOR((*gen_t)); typedef UNROLL((*unroll_t)); /** + * GENERATOR(gen_t) * - * @param curve - * @param config - * @param args - * @return + * + * @param curve A curve_t being generated + * @param cfg An application config + * @param args unused + * @return state diff */ int gen_skip(curve_t *curve, const config_t *cfg, arg_t *args); /** + * UNROLL(unroll_t) * * @param curve * @param cfg diff --git a/src/util/macro.h b/src/util/macro.h deleted file mode 100644 index abe2c5f..0000000 --- a/src/util/macro.h +++ /dev/null @@ -1,14 +0,0 @@ -/* - * ecgen, tool for generating Elliptic curve domain parameters - * Copyright (C) 2017 J08nY - */ -/** - * @file macro.h - */ -#ifndef ECGEN_MACRO_H -#define ECGEN_MACRO_H - -#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N -#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) - -#endif // ECGEN_MACRO_H diff --git a/src/util/memory.c b/src/util/memory.c index 9724c81..2c2281c 100644 --- a/src/util/memory.c +++ b/src/util/memory.c @@ -9,7 +9,7 @@ void *alloc(void *(*fun)(size_t), size_t size) { void *result = fun(size); if (!result) { perror("Couldn't alloc."); - exit(1); + exit(EXIT_FAILURE); } return result; } @@ -22,7 +22,7 @@ void *try_realloc(void *ptr, size_t size) { void *result = pari_realloc(ptr, size); if (!result) { perror("Couldn't alloc."); - exit(1); + exit(EXIT_FAILURE); } return result; } \ No newline at end of file diff --git a/src/util/memory.h b/src/util/memory.h index 8b85a3d..55e8eac 100644 --- a/src/util/memory.h +++ b/src/util/memory.h @@ -11,21 +11,21 @@ #include /** - * @brief + * @brief Try mallocing some memory, exit(EXIT_FAILURE) if it fails. * @param size * @return */ void *try_malloc(size_t size); /** - * @brief + * @brief Try callocing some memory, exit(EXIT_FAILURE) if it fails. * @param size * @return */ void *try_calloc(size_t size); /** - * @brief + * @brief Try reallocing some memory, exit(EXIT_FAILURE) if it fails. * @param ptr * @param size * @return -- cgit v1.3.1