diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ecgen.c | 4 | ||||
| -rw-r--r-- | src/exhaustive/exhaustive.c | 20 | ||||
| -rw-r--r-- | src/exhaustive/seed.c | 7 | ||||
| -rw-r--r-- | src/exhaustive/seed.h | 14 | ||||
| -rw-r--r-- | src/gp.c | 22 | ||||
| -rw-r--r-- | src/gp.h | 2 | ||||
| -rw-r--r-- | src/gp/gp.gp | 8 | ||||
| -rw-r--r-- | src/invalid/invalid.c | 12 | ||||
| -rw-r--r-- | src/io/cli.c | 36 | ||||
| -rw-r--r-- | src/io/cli.h | 2 | ||||
| -rw-r--r-- | src/io/input.c | 2 | ||||
| -rw-r--r-- | src/io/input.h | 2 | ||||
| -rw-r--r-- | src/io/output.c | 3 | ||||
| -rw-r--r-- | src/io/output.h | 3 | ||||
| -rw-r--r-- | src/math/curve.c | 33 | ||||
| -rw-r--r-- | src/math/curve.h | 10 | ||||
| -rw-r--r-- | src/math/equation.c | 2 | ||||
| -rw-r--r-- | src/math/point.c | 112 | ||||
| -rw-r--r-- | src/math/point.h | 44 | ||||
| -rw-r--r-- | src/math/poly.c | 2 | ||||
| -rw-r--r-- | src/math/random.c | 2 | ||||
| -rw-r--r-- | src/math/random.h | 2 |
22 files changed, 297 insertions, 47 deletions
diff --git a/src/ecgen.c b/src/ecgen.c index 21acea9..724e7e2 100644 --- a/src/ecgen.c +++ b/src/ecgen.c @@ -42,7 +42,7 @@ const char *argp_program_bug_address = "<johny@neuromancer.sk>"; static struct argp argp = {options, parse_opt, args_doc, doc}; static struct config_t cfg; -bool init() { +bool init(void) { // Init PARI, 1GB stack, 1M primes pari_init(1000000000, 1000000); @@ -101,8 +101,8 @@ int quit(int status) { * - Generates field and equation parameters: * - randomly * - using ANSI X9.62 verifiably random method(from seed) - * , until a curve with requested properties appears. * - given input + * , until a curve with requested properties appears. */ int main(int argc, char *argv[]) { // Parse cli args diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c index c5cdebd..00905c5 100644 --- a/src/exhaustive/exhaustive.c +++ b/src/exhaustive/exhaustive.c @@ -8,6 +8,7 @@ #include "math/equation.h" #include "math/field.h" #include "math/order.h" +#include "math/point.h" #include "seed.h" void exhaustive_init(gen_t generators[], config_t *config) { @@ -53,6 +54,8 @@ void exhaustive_init(gen_t generators[], config_t *config) { } else { generators[OFFSET_FIELD] = &field_input; } + + generators[OFFSET_POINTS] = &points_generators; } int exhaustive_gen(curve_t *curve, config_t *config, gen_t generators[], @@ -64,8 +67,21 @@ int exhaustive_gen(curve_t *curve, config_t *config, gen_t generators[], fprintf(stderr, "Error generating a curve. %i\n", state); return 0; } + if (config->verbose) { + if (diff > 0) { + fprintf(out, "+"); + } else if (diff < 0) { + fprintf(out, "-"); + } else { + fprintf(out, "."); + } + fflush(out); + } state += diff; } + + if (config->verbose) fprintf(out, "\n"); + return 1; } @@ -74,11 +90,11 @@ int exhaustive_do(config_t *cfg) { exhaustive_init(generators, cfg); curve_t *curve = curve_new(); - if (!exhaustive_gen(curve, cfg, generators, OFFSET_FIELD, OFFSET_POINTS)) { + if (!exhaustive_gen(curve, cfg, generators, OFFSET_FIELD, OFFSET_END)) { curve_free(&curve); return 1; } - output_csv(out, "%P#x", ';', curve_params(curve)); + output_csv(out, "%P#x", ',', curve_params(curve)); curve_free(&curve); return 0; }
\ No newline at end of file diff --git a/src/exhaustive/seed.c b/src/exhaustive/seed.c index 0656db5..9f6ec46 100644 --- a/src/exhaustive/seed.c +++ b/src/exhaustive/seed.c @@ -5,7 +5,7 @@ #include "seed.h" #include "io/input.h" -seed_t *seed_new() { +seed_t *seed_new(void) { seed_t *seed = pari_malloc(sizeof(seed_t)); if (!seed) { perror("Couldn't malloc."); @@ -15,6 +15,11 @@ seed_t *seed_new() { return seed; } +seed_t *seed_copy(seed_t *src, seed_t *dest) { + dest->seed = gcopy(src->seed); + return dest; +} + void seed_free(seed_t **seed) { if (*seed) { pari_free(*seed); diff --git a/src/exhaustive/seed.h b/src/exhaustive/seed.h index 7c96372..06857fc 100644 --- a/src/exhaustive/seed.h +++ b/src/exhaustive/seed.h @@ -10,6 +10,20 @@ /** * + * @return + */ +seed_t *seed_new(void); + +/** + * + * @param src + * @param dest + * @return + */ +seed_t *seed_copy(seed_t *src, seed_t *dest); + +/** + * * @param seed */ void seed_free(seed_t **seed); @@ -6,3 +6,25 @@ void init_gp(void) /* void */ avma = ltop; return; } + +/* +* ecgen, tool for generating Elliptic curve domain parameters +* Copyright (C) 2017 J08nY +*/ + +/* +\r gp/utils +\r gp/invalid +*/ + +GEN test(void) /* int */ +{ + pari_sp ltop = avma; + GEN e = gen_0; /* ell */ + GEN p = gen_0; + GEN i = gen_0, p1 = gen_0; /* int */ + p = genrand(e); + p1 = i = gcopy(gel(p, 1)); + p1 = gerepilecopy(ltop, p1); + return p1; +} @@ -8,7 +8,9 @@ #include <pari/pari.h> /* GP;install("init_gp","v","init_gp","./gp/gp.gp.so"); +GP;install("test","","test","./gp/gp.gp.so"); */ void init_gp(void); +GEN test(void); /*End of prototype*/ #endif // GP_H diff --git a/src/gp/gp.gp b/src/gp/gp.gp index 9bae867..8c245a9 100644 --- a/src/gp/gp.gp +++ b/src/gp/gp.gp @@ -6,4 +6,10 @@ /* \r gp/utils \r gp/invalid -*/
\ No newline at end of file +*/ + +test() = { + local(e:ell, p, i:int); + p = random(e); + i = p[1]; +}
\ No newline at end of file diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c index 7fe2dd4..54aa7c1 100644 --- a/src/invalid/invalid.c +++ b/src/invalid/invalid.c @@ -9,6 +9,7 @@ #include "math/equation.h" #include "math/field.h" #include "math/order.h" +#include "math/point.h" void invalid_init(gen_t generators[], config_t *cfg) { generators[OFFSET_SEED] = &gen_skip; @@ -74,6 +75,7 @@ size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes, invalid_gen[OFFSET_B] = &b_random; invalid_gen[OFFSET_CURVE] = &curve_nonzero; invalid_gen[OFFSET_ORDER] = &order_init; + invalid_gen[OFFSET_POINTS] = &points_prime; // We will have nprimes curves in the end *curves = pari_malloc(nprimes * sizeof(curve_t *)); @@ -94,7 +96,7 @@ 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_POINTS); + exhaustive_gen(invalid, cfg, invalid_gen, OFFSET_B, OFFSET_END); // does some small prime from our array divide the curve order? size_t count = 0; @@ -104,13 +106,9 @@ size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes, (*curves)[i] = invalid; } else { (*curves)[i] = curve_new(); - (*curves)[i]->field = gcopy(invalid->field); - (*curves)[i]->a = gcopy(invalid->a); - (*curves)[i]->b = gcopy(invalid->b); - (*curves)[i]->curve = gcopy(invalid->curve); - (*curves)[i]->order = gcopy(invalid->order); + (*curves)[i] = curve_copy(invalid, (*curves)[i]); } - output_csv(out, "%P#x", ';', curve_params((*curves)[i])); + output_csv(out, "%P#x", ',', curve_params((*curves)[i])); ncurves++; count++; } diff --git a/src/io/cli.c b/src/io/cli.c index d00a0c5..6aaadc8 100644 --- a/src/io/cli.c +++ b/src/io/cli.c @@ -21,6 +21,7 @@ enum opt_keys { OPT_OUTPUT = 'o', OPT_INPUT = 'f', OPT_APPEND = 'a', + OPT_VERBOSE = 'v', OPT_FP = 1, OPT_F2M = 2, }; @@ -28,20 +29,21 @@ 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."}, - {"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)."}, - {"order", OPT_ORDER, "ORDER", 0, "Generate a curve with given order (using Complex Multiplication)."}, - {"koblitz", OPT_KOBLITZ, 0, 0, "Generate a Koblitz curve."}, + {"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)."}, + {"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)."}, + {"verbose", OPT_VERBOSE, "FILE", OPTION_ARG_OPTIONAL, "Verbose logging to stdout"}, {0}}; // clang-format on @@ -61,6 +63,12 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) { case OPT_APPEND: cfg->append = true; break; + case OPT_VERBOSE: + cfg->verbose++; + if (arg) { + cfg->debug = arg; + } + break; case OPT_RANDOM: cfg->random = true; break; @@ -86,7 +94,7 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) { if (strlen(arg) < 20) { argp_failure( state, 1, 0, - "SEED must be at least 160 bits(20 characters)."); + "SEED must be at least 160 bits (20 characters)."); } cfg->seed = arg; } @@ -112,7 +120,7 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) { if (!cfg->prime_field && !cfg->binary_field) { argp_failure(state, 1, 0, "Specify field type, prime or binary, with --fp / " - "--f2m(but not both)."); + "--f2m (but not both)."); } // Invalid is not prime or seed by definition. if (cfg->invalid && (cfg->prime || cfg->from_seed)) { diff --git a/src/io/cli.h b/src/io/cli.h index 0826419..52b39ec 100644 --- a/src/io/cli.h +++ b/src/io/cli.h @@ -31,6 +31,8 @@ typedef struct config_t { char *output; char *input; bool append; + long verbose; + char *debug; long bits; } config_t; diff --git a/src/io/input.c b/src/io/input.c index 184924f..263364d 100644 --- a/src/io/input.c +++ b/src/io/input.c @@ -111,7 +111,7 @@ void input_init(const char *input) { } } -void input_quit() { +void input_quit(void) { if (in != NULL && in != stdout) { fclose(in); } diff --git a/src/io/input.h b/src/io/input.h index 9cdd008..1a3de5b 100644 --- a/src/io/input.h +++ b/src/io/input.h @@ -50,6 +50,6 @@ extern FILE *in; void input_init(const char *input); -void input_quit(); +void input_quit(void); #endif // ECGEN_INPUT_H diff --git a/src/io/output.c b/src/io/output.c index 31e9f4c..27cdcf4 100644 --- a/src/io/output.c +++ b/src/io/output.c @@ -7,6 +7,7 @@ #include <parson/parson.h> FILE *out; +FILE *debug; char *output_scsv(const char *format, char delim, GEN vector) { long len = lg(vector) - 1; @@ -66,7 +67,7 @@ void output_init(const char *output, bool append) { } } -void output_quit() { +void output_quit(void) { if (out != NULL && out != stdout) { fclose(out); } diff --git a/src/io/output.h b/src/io/output.h index 35d2178..0a2f91d 100644 --- a/src/io/output.h +++ b/src/io/output.h @@ -41,9 +41,10 @@ char *output_sjson(GEN vector); void output_json(FILE *out, GEN vector); extern FILE *out; +extern FILE *debug; void output_init(const char *output, bool append); -void output_quit(); +void output_quit(void); #endif // ECGEN_OUTPUT_H diff --git a/src/math/curve.c b/src/math/curve.c index 57f46e6..2ff3d8f 100644 --- a/src/math/curve.c +++ b/src/math/curve.c @@ -5,8 +5,9 @@ #include "curve.h" #include "exhaustive/seed.h" #include "field.h" +#include "point.h" -curve_t *curve_new() { +curve_t *curve_new(void) { curve_t *curve = pari_malloc(sizeof(curve_t)); if (!curve) { perror("Couldn't malloc."); @@ -16,10 +17,29 @@ curve_t *curve_new() { return curve; } +curve_t *curve_copy(curve_t *src, curve_t *dest) { + if (src->seed) dest->seed = seed_copy(src->seed, dest->seed); + if (src->field) dest->field = gcopy(src->field); + if (src->a) dest->a = gcopy(src->a); + if (src->b) dest->b = gcopy(src->b); + if (src->curve) dest->curve = gcopy(src->curve); + if (src->order) dest->order = gcopy(src->order); + if (src->points) { + dest->points = points_new(src->npoints); + dest->points = points_copy(src->points, dest->points, src->npoints); + dest->npoints = src->npoints; + } +} + void curve_free(curve_t **curve) { if (*curve) { seed_free(&(*curve)->seed); - pari_free((*curve)->points); + if ((*curve)->points) { + for (size_t i = 0; i < (*curve)->npoints; ++i) { + point_free(&(*curve)->points[i]); + } + points_free(&(*curve)->points); + } pari_free(*curve); *curve = NULL; } @@ -82,6 +102,15 @@ GEN curve_params(curve_t *curve) { if (curve->a) result = gconcat(result, field_elementi(curve->a)); if (curve->b) result = gconcat(result, field_elementi(curve->b)); if (curve->order) result = gconcat(result, gtovec(curve->order)); + if (curve->points) { + for (size_t i = 0; i < curve->npoints; ++i) { + GEN point = + gconcat(field_elementi(gel(curve->points[i]->point, 1)), + field_elementi(gel(curve->points[i]->point, 2))); + result = gconcat(result, point); + result = gconcat(result, curve->points[i]->order); + } + } return gerepilecopy(ltop, result); } diff --git a/src/math/curve.h b/src/math/curve.h index 1a8fb0b..20273b7 100644 --- a/src/math/curve.h +++ b/src/math/curve.h @@ -50,7 +50,15 @@ GEN curve_params(curve_t *curve); * * @return */ -curve_t *curve_new(); +curve_t *curve_new(void); + +/** + * + * @param src + * @param dest + * @return + */ +curve_t *curve_copy(curve_t *src, curve_t *dest); /** * Free a curve_t along with it's seed_t and point_ts. diff --git a/src/math/equation.c b/src/math/equation.c index 4e44747..1e29ee4 100644 --- a/src/math/equation.c +++ b/src/math/equation.c @@ -20,6 +20,7 @@ int a_random(curve_t *curve, config_t *config, ...) { int a_input(curve_t *curve, config_t *config, ...) { curve->a = fread_int(in, "a:", config->bits); + // TODO change a to a field element here?. a t_INTMOD or a t_FFELT. return 1; } @@ -42,6 +43,7 @@ int b_random(curve_t *curve, config_t *config, ...) { int b_input(curve_t *curve, config_t *config, ...) { curve->b = fread_int(in, "b:", config->bits); + // TODO change b to a field element here?. a t_INTMOD or a t_FFELT. return 1; } diff --git a/src/math/point.c b/src/math/point.c index ebe6288..2cf1042 100644 --- a/src/math/point.c +++ b/src/math/point.c @@ -4,15 +4,64 @@ */ #include "point.h" -point_t *point_new() {} +point_t *point_new(void) { + point_t *point = pari_malloc(sizeof(point_t)); + if (!point) { + perror("Couldn't malloc."); + exit(1); + } + memset(point, 0, sizeof(point_t)); + return point; +} + +point_t *point_copy(point_t *src, point_t *dest) { + if (src->point) dest->point = gcopy(src->point); + if (dest->point) dest->order = gcopy(src->order); + return dest; +} + +void point_free(point_t **point) { + if (*point) { + pari_free(*point); + *point = NULL; + } +} + +point_t **points_new(size_t num) { + point_t **points = pari_malloc(num * sizeof(point_t *)); + if (!points) { + perror("Couldn't malloc."); + exit(1); + } + memset(points, 0, num * sizeof(point_t *)); + return points; +} -point_t **points_new(size_t num) {} +point_t **points_copy(point_t **src, point_t **dest, size_t num) { + for (size_t i = 0; i < num; ++i) { + dest[i] = point_new(); + dest[i] = point_copy(src[i], dest[i]); + } + return dest; +} -void point_free(point_t **point) {} +void points_free(point_t ***points) { + if (*points) { + pari_free(*points); + *points = NULL; + } +} -void points_free(point_t ***point) {} +int point_random(curve_t *curve, config_t *config, ...) { + point_t *p = point_new(); + p->point = genrand(curve->curve); + p->order = ellorder(curve->curve, p->point, NULL); -int point_random(curve_t *curve, config_t *config, ...) {} + curve->points = points_new(1); + curve->points[0] = p; + curve->npoints = 1; + return 1; +} int points_random(curve_t *curve, config_t *config, ...) { va_list arg; @@ -23,9 +72,58 @@ int points_random(curve_t *curve, config_t *config, ...) { curve->points = points_new(npoints); curve->npoints = npoints; for (size_t i = 0; i < npoints; ++i) { + point_t *p = point_new(); + p->point = genrand(curve->curve); + p->order = ellorder(curve->curve, p->point, NULL); + curve->points[i] = p; + } + return 1; +} + +int points_prime(curve_t *curve, config_t *config, ...) { + GEN factors = Z_factor(curve->order); + GEN primes = gel(factors, 1); + long nprimes = glength(primes); + curve->points = points_new((size_t)nprimes); + curve->npoints = (size_t)nprimes; + + long npoints = 0; + while (npoints < nprimes) { + GEN rand = genrand(curve->curve); + GEN ord = ellorder(curve->curve, rand, NULL); + // ord(rand) = ord + + for (long i = 1; i <= nprimes; ++i) { + if (dvdii(ord, gel(primes, i)) && curve->points[i - 1] == NULL) { + // primes[i] divides ord + // mul = ord/primes[i] + GEN mul = divii(ord, gel(primes, i)); + GEN point = ellmul(curve->curve, rand, mul); + + point_t *p = point_new(); + p->point = point; + p->order = ord; + curve->points[i - 1] = p; + npoints++; + } + } } + + return 1; } -int points_prime(curve_t *curve, config_t *config, ...) {} +int points_generators(curve_t *curve, config_t *config, ...) { + GEN generators = ellff_get_gens(curve->curve); + long len = glength(generators); + curve->points = points_new((size_t)len); + curve->npoints = (size_t)len; + + for (long i = 1; i <= len; ++i) { + point_t *p = point_new(); + p->point = gel(generators, i); + p->order = ellorder(curve->curve, p->point, NULL); + curve->points[i - 1] = p; + } -int points_generators(curve_t *curve, config_t *config, ...) {}
\ No newline at end of file + return 1; +}
\ No newline at end of file diff --git a/src/math/point.h b/src/math/point.h index 064dadc..ed10bdb 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -11,14 +11,15 @@ * * @return */ -point_t *point_new(); +point_t *point_new(void); /** * - * @param num + * @param src + * @param dest * @return */ -point_t **points_new(size_t num); +point_t *point_copy(point_t *src, point_t *dest); /** * @@ -28,6 +29,22 @@ void point_free(point_t **point); /** * + * @param num + * @return + */ +point_t **points_new(size_t num); + +/** + * + * @param src + * @param dest + * @param num + * @return + */ +point_t **points_copy(point_t **src, point_t **dest, size_t num); + +/** + * * @param point */ void points_free(point_t ***point); @@ -50,4 +67,25 @@ int point_random(curve_t *curve, config_t *config, ...); */ int points_random(curve_t *curve, config_t *config, ...); +/** + * Cauchy: + * 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 ... + * @return + */ +int points_prime(curve_t *curve, config_t *config, ...); + +/** + * + * @param curve + * @param config + * @param ... + * @return + */ +int points_generators(curve_t *curve, config_t *config, ...); + #endif // ECGEN_POINT_H diff --git a/src/math/poly.c b/src/math/poly.c index f888d97..6a89061 100644 --- a/src/math/poly.c +++ b/src/math/poly.c @@ -2719,7 +2719,7 @@ polynomial_t *poly_find(long m) { return NULL; } polynomial_t *search_tri = ansi_trinomials; - polynomial_t *search_penta = ansi_trinomials; + polynomial_t *search_penta = ansi_pentanomials; size_t len_tri = sizeof(ansi_trinomials) / sizeof(polynomial_t); size_t len_penta = sizeof(ansi_pentanomials) / sizeof(polynomial_t); if (m < 160 || m > 2000) { diff --git a/src/math/random.c b/src/math/random.c index 37fd882..44bdb27 100644 --- a/src/math/random.c +++ b/src/math/random.c @@ -5,7 +5,7 @@ #include "random.h" #include <time.h> -bool random_init() { +bool random_init(void) { pari_ulong seed = 0; // Try urandom first FILE *rand = fopen("/dev/urandom", "rb"); diff --git a/src/math/random.h b/src/math/random.h index 5d3d17f..d044b90 100644 --- a/src/math/random.h +++ b/src/math/random.h @@ -8,7 +8,7 @@ #include <pari/pari.h> #include <stdbool.h> -bool random_init(); +bool random_init(void); GEN random_prime(long bits); |
