diff options
| -rw-r--r-- | .travis.yml | 4 | ||||
| -rw-r--r-- | src/exhaustive/exhaustive.c | 2 | ||||
| -rw-r--r-- | src/invalid/invalid.c | 6 | ||||
| -rw-r--r-- | src/io/output.c | 65 | ||||
| -rw-r--r-- | src/math/curve.c | 45 | ||||
| -rw-r--r-- | src/math/gens.c | 23 | ||||
| -rw-r--r-- | src/math/gens.h | 9 | ||||
| -rw-r--r-- | src/math/order.c | 16 | ||||
| -rw-r--r-- | src/math/order.h | 10 | ||||
| -rw-r--r-- | src/math/point.c | 55 | ||||
| -rw-r--r-- | src/math/point.h | 12 | ||||
| -rw-r--r-- | src/math/types.h | 6 |
12 files changed, 168 insertions, 85 deletions
diff --git a/.travis.yml b/.travis.yml index b81bbaa..0ed1553 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,6 @@ matrix: - ubuntu-toolchain-r-test packages: - gcc-6 - env: COMPILER=gcc-6 - compiler: clang-3.9 addons: apt: @@ -20,7 +19,6 @@ matrix: - llvm-toolchain-precise-3.9 packages: - clang-3.9 - env: COMPILER=clang-3.9 before_install: - wget mirrors.kernel.org/ubuntu/pool/universe/p/pari/libpari-gmp-tls5_2.9.1-1_amd64.deb @@ -32,4 +30,4 @@ script: - make libparson.a - cd ../.. - mv lib/parson/libparson.a lib/libparson.a - - make
\ No newline at end of file + - make diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c index 851e7c6..1f88215 100644 --- a/src/exhaustive/exhaustive.c +++ b/src/exhaustive/exhaustive.c @@ -9,6 +9,7 @@ #include "math/field.h" #include "math/order.h" #include "math/point.h" +#include "math/gens.h" #include "seed.h" void exhaustive_ginit(gen_t *generators, config_t *config) { @@ -48,6 +49,7 @@ void exhaustive_ginit(gen_t *generators, config_t *config) { generators[OFFSET_ORDER] = &order_init; } } + generators[OFFSET_GENERATORS] = &gens_init; if (config->random) { generators[OFFSET_FIELD] = &field_random; diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c index 6d4982a..7663213 100644 --- a/src/invalid/invalid.c +++ b/src/invalid/invalid.c @@ -8,6 +8,7 @@ #include "math/curve.h" #include "math/equation.h" #include "math/field.h" +#include "math/gens.h" #include "math/order.h" #include "math/point.h" @@ -76,6 +77,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_GENERATORS] = &gens_init; invalid_gen[OFFSET_POINTS] = &points_primet; arg_t *invalid_argss[OFFSET_END]; @@ -100,7 +102,7 @@ size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes, pari_sp btop = avma; // generate a curve with random b exhaustive_gen(invalid, cfg, invalid_gen, NULL, OFFSET_B, - OFFSET_POINTS); + OFFSET_GENERATORS); // does some small prime from our array divide the curve order? // if so how many? @@ -139,7 +141,7 @@ size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes, // 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); + OFFSET_GENERATORS, OFFSET_END); size_t count = 0; for (size_t i = nprimes; i-- > 0;) { diff --git a/src/io/output.c b/src/io/output.c index e1e08bd..038ba3d 100644 --- a/src/io/output.c +++ b/src/io/output.c @@ -11,15 +11,6 @@ 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); @@ -59,7 +50,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); - fprintff(out, "%s\n", string); + fprintf(out, "%s\n", string); free(string); } @@ -96,8 +87,7 @@ JSON_Value *output_jjson(curve_t *curve, config_t *config) { pari_free(e3); break; } - default: - fprintf(stderr, "Error, field has unknown amount of elements.\n"); + default: fprintf(stderr, "Error, field has unknown amount of elements.\n"); exit(1); } @@ -110,6 +100,37 @@ JSON_Value *output_jjson(curve_t *curve, config_t *config) { char *order = pari_sprintf("%P#x", curve->order); json_object_set_string(root_object, "order", order); pari_free(order); + if (curve->generators) { + JSON_Value *gens_value = json_value_init_array(); + JSON_Array *gens_array = json_value_get_array(gens_value); + + for (size_t i = 0; i < curve->ngens; ++i) { + 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->generators[i]->point, 1))); + json_object_set_string(point_object, "x", x); + pari_free(x); + char *y = pari_sprintf( + "%P#x", field_elementi(gel(curve->generators[i]->point, 2))); + json_object_set_string(point_object, "y", y); + pari_free(y); + char *p_order = pari_sprintf("%P#x", curve->generators[i]->order); + json_object_set_string(point_object, "order", p_order); + pari_free(p_order); + if (curve->generators[i]->cofactor) { + char *cofactor = pari_sprintf("%P#x", curve->generators[i]->cofactor); + json_object_set_string(point_object, "cofactor", cofactor); + pari_free(p_order); + } + + json_array_append_value(gens_array, point_value); + } + + json_object_set_value(root_object, "generators", gens_value); + } + if (curve->npoints) { JSON_Value *points_value = json_value_init_array(); JSON_Array *points_array = json_value_get_array(points_value); @@ -119,16 +140,22 @@ JSON_Value *output_jjson(curve_t *curve, config_t *config) { JSON_Object *point_object = json_value_get_object(point_value); char *x = pari_sprintf( - "%P#x", field_elementi(gel(curve->points[i]->point, 1))); + "%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))); + "%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); json_object_set_string(point_object, "order", p_order); pari_free(p_order); + if (curve->points[i]->cofactor) { + char *cofactor = pari_sprintf("%P#x", curve->points[i]->cofactor); + json_object_set_string(point_object, "cofactor", cofactor); + pari_free(p_order); + } + json_array_append_value(points_array, point_value); } @@ -148,7 +175,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); - fprintff(out, "%s", s); + fprintf(out, "%s\n", s); json_free_serialized_string(s); } @@ -169,6 +196,7 @@ void output_init(config_t *cfg) { } else { out = stdout; } + setvbuf(out, NULL, _IONBF, 0); if (cfg->debug) { debug = fopen(cfg->debug, "w"); if (!debug) { @@ -178,15 +206,14 @@ void output_init(config_t *cfg) { } else { debug = stdout; } + setvbuf(debug, NULL, _IONBF, 0); switch (cfg->format) { - case FORMAT_JSON: - output_s = &output_sjson; + case FORMAT_JSON: output_s = &output_sjson; output_f = &output_fjson; output_o = &output_json; break; - case FORMAT_CSV: - output_s = &output_scsv; + case FORMAT_CSV: output_s = &output_scsv; output_f = &output_fcsv; output_o = &output_csv; break; diff --git a/src/math/curve.c b/src/math/curve.c index 3892704..71d1744 100644 --- a/src/math/curve.c +++ b/src/math/curve.c @@ -45,19 +45,16 @@ int curve_init(curve_t *curve, config_t *config, arg_t *args) { pari_sp ltop = avma; GEN v = gen_0; switch (typ(curve->field)) { - case t_INT: - v = gtovec0(gen_0, 2); + case t_INT: v = gtovec0(gen_0, 2); gel(v, 1) = curve->a; gel(v, 2) = curve->b; break; - case t_FFELT: - v = gtovec0(gen_0, 5); + case t_FFELT: v = gtovec0(gen_0, 5); gel(v, 1) = gen_1; gel(v, 4) = curve->a; gel(v, 5) = curve->b; break; - default: - pari_err_TYPE("curve_init", curve->field); + default: pari_err_TYPE("curve_init", curve->field); } curve->curve = gerepilecopy(ltop, ellinit(v, curve->field, -1)); @@ -87,12 +84,9 @@ int curve_seed_f2m(curve_t *curve, config_t *config, arg_t *args) { 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, args); - case t_FFELT: - return curve_seed_f2m(curve, config, args); - default: - pari_err_TYPE("curve_seed", curve->field); + case t_INT: return curve_seed_fp(curve, config, args); + case t_FFELT: return curve_seed_f2m(curve, config, args); + default: pari_err_TYPE("curve_seed", curve->field); return INT_MIN; /* NOT REACHABLE */ } } @@ -103,14 +97,35 @@ GEN curve_params(curve_t *curve) { GEN result = field_params(curve->field); if (curve->a) result = gconcat(result, field_elementi(curve->a)); if (curve->b) result = gconcat(result, field_elementi(curve->b)); + if (curve->generators) { + for (size_t i = 0; i < curve->ngens; ++i) { + GEN point = + gconcat(field_elementi(gel(curve->generators[i]->point, 1)), + field_elementi(gel(curve->generators[i]->point, 2))); + GEN x = field_elementi(gel(point, 1)); + GEN y = field_elementi(gel(point, 2)); + result = gconcat(result, x); + result = gconcat(result, y); + result = gconcat(result, curve->generators[i]->order); + if (curve->generators[i]->cofactor) { + result = gconcat(result, curve->generators[i]->cofactor); + } + } + } 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); + gconcat(field_elementi(gel(curve->points[i]->point, 1)), + field_elementi(gel(curve->points[i]->point, 2))); + GEN x = field_elementi(gel(point, 1)); + GEN y = field_elementi(gel(point, 2)); + result = gconcat(result, x); + result = gconcat(result, y); result = gconcat(result, curve->points[i]->order); + if (curve->points[i]->cofactor) { + result = gconcat(result, curve->points[i]->cofactor); + } } } diff --git a/src/math/gens.c b/src/math/gens.c new file mode 100644 index 0000000..619eb25 --- /dev/null +++ b/src/math/gens.c @@ -0,0 +1,23 @@ + +#include "gens.h" +#include "point.h" + + +int gens_init(curve_t *curve, config_t *config, arg_t *args) { + // TODO stack code!!! + GEN generators = ellff_get_gens(curve->curve); + long len = glength(generators); + curve->generators = points_new((size_t)len); + curve->ngens = (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); + p->cofactor = divii(curve->order, p->order); + curve->generators[i - 1] = p; + } + + return 1; +} + diff --git a/src/math/gens.h b/src/math/gens.h new file mode 100644 index 0000000..fc67a23 --- /dev/null +++ b/src/math/gens.h @@ -0,0 +1,9 @@ + +#ifndef ECGEN_GENS_H +#define ECGEN_GENS_H + +#include "types.h" + +int gens_init(curve_t *curve, config_t *config, arg_t *args); + +#endif //ECGEN_GENS_H diff --git a/src/math/order.c b/src/math/order.c index 39222b4..7e7eeda 100644 --- a/src/math/order.c +++ b/src/math/order.c @@ -9,6 +9,22 @@ int order_init(curve_t *curve, config_t *cfg, arg_t *args) { return 1; } +int order_smallfact(curve_t *curve, config_t *cfg, arg_t *args) { + if (!args) { + fprintf(stderr, "No args to an arged function. points_random"); + return INT_MIN; + } + pari_ulong smallfact = *(pari_ulong *)args->args; + pari_sp ltop = avma; + curve->order = ellsea(curve->curve, smallfact); + if (gequal0(curve->order)) { + avma = ltop; + return -4; + } else { + return 1; + } +} + int order_prime(curve_t *curve, config_t *cfg, arg_t *args) { pari_sp ltop = avma; curve->order = ellsea(curve->curve, 1); diff --git a/src/math/order.h b/src/math/order.h index a1ed861..4562fab 100644 --- a/src/math/order.h +++ b/src/math/order.h @@ -24,6 +24,16 @@ int order_init(curve_t *curve, config_t *cfg, arg_t *args); /** * GENERATOR(gen_t) + * + * @param curve + * @param cfg + * @param args + * @return + */ +int order_smallfact(curve_t *curve, config_t *cfg, arg_t *args); + +/** + * GENERATOR(gen_t) * Calculates the curve order, always using the SEA algorithm, * gives up early in case the order is divisible by "something". * Succeeds if the curve has a prime order. diff --git a/src/math/point.c b/src/math/point.c index c2cd02a..514f1b9 100644 --- a/src/math/point.c +++ b/src/math/point.c @@ -91,19 +91,19 @@ int points_random(curve_t *curve, config_t *config, arg_t *args) { } /* - * GEN o = utoi(dprimes[i]); - GEN mul = ellmul(curve->curve, rand, o); + 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(); + 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; - } + 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) { @@ -125,11 +125,14 @@ int points_primet(curve_t *curve, config_t *config, arg_t *args) { for (long i = 0; i < nprimes; ++i) { if (curve->points[i] == NULL && dvdis(ord, primes[i])) { + pari_sp ftop = avma; + GEN p = stoi(primes[i]); GEN mul = divii(ord, p); GEN point = ellmul(curve->curve, rand, mul); curve->points[i] = point_new(); + gerepileall(ftop, 2, &point, &p); curve->points[i]->point = point; curve->points[i]->order = p; npoints++; @@ -156,15 +159,18 @@ int points_prime(curve_t *curve, config_t *config, arg_t *args) { for (long i = 1; i <= nprimes; ++i) { if (curve->points[i - 1] == NULL && dvdii(ord, gel(primes, i))) { + pari_sp ftop = avma; + // primes[i] divides ord // mul = ord/primes[i] - GEN mul = divii(ord, gel(primes, i)); + GEN p = gcopy(gel(primes, i)); + GEN mul = divii(ord, p); GEN point = ellmul(curve->curve, rand, mul); - point_t *p = point_new(); - p->point = point; - p->order = gel(primes, i); - curve->points[i - 1] = p; + curve->points[i - 1] = point_new(); + gerepileall(ftop, 2, &point, &p); + curve->points[i - 1]->point = point; + curve->points[i - 1]->order = p; npoints++; } } @@ -172,20 +178,3 @@ int points_prime(curve_t *curve, config_t *config, arg_t *args) { return 1; } - -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); - 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; - } - - return 1; -} diff --git a/src/math/point.h b/src/math/point.h index fe9aeb4..9a0dd89 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -112,16 +112,4 @@ int points_primet(curve_t *curve, config_t *config, arg_t *args); */ int points_prime(curve_t *curve, config_t *config, arg_t *args); -/** - * GENERATOR(gen_t) - * Calculates the minimal set of generators of the curve.(one or two points). - * Always succeeds. - * - * @param curve - * @param config - * @param args unused - * @return state diff - */ -int points_generators(curve_t *curve, config_t *config, arg_t *args); - #endif // ECGEN_POINT_H diff --git a/src/math/types.h b/src/math/types.h index 575f583..70696ad 100644 --- a/src/math/types.h +++ b/src/math/types.h @@ -16,6 +16,7 @@ typedef struct seed_t { GEN seed; } seed_t; typedef struct point_t { GEN point; GEN order; + GEN cofactor; } point_t; typedef struct curve_t { @@ -25,6 +26,8 @@ typedef struct curve_t { GEN b; GEN curve; GEN order; + point_t **generators; + size_t ngens; point_t **points; size_t npoints; } curve_t; @@ -36,6 +39,7 @@ enum curve_offset { OFFSET_B, OFFSET_CURVE, OFFSET_ORDER, + OFFSET_GENERATORS, OFFSET_POINTS, OFFSET_END }; @@ -45,7 +49,7 @@ typedef struct arg_t { size_t nargs; } arg_t; -typedef int (*gen_t)(curve_t *, config_t *, arg_t *args); +typedef int (*gen_t)(curve_t *, config_t *, arg_t *); int gen_skip(curve_t *curve, config_t *config, arg_t *args); |
