diff options
Diffstat (limited to 'src/math')
| -rw-r--r-- | src/math/curve.c | 2 | ||||
| -rw-r--r-- | src/math/curve.h | 12 | ||||
| -rw-r--r-- | src/math/equation.h | 86 | ||||
| -rw-r--r-- | src/math/field.h | 25 | ||||
| -rw-r--r-- | src/math/gens.h | 23 | ||||
| -rw-r--r-- | src/math/order.h | 35 | ||||
| -rw-r--r-- | src/math/point.h | 32 | ||||
| -rw-r--r-- | src/math/poly.c | 4 | ||||
| -rw-r--r-- | src/math/poly.h | 30 | ||||
| -rw-r--r-- | src/math/random.h | 4 | ||||
| -rw-r--r-- | src/math/seed.c | 104 | ||||
| -rw-r--r-- | src/math/seed.h | 83 | ||||
| -rw-r--r-- | src/math/types.h | 21 |
13 files changed, 333 insertions, 128 deletions
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 <pari/pari.h> #include <stdbool.h> +/** + * @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 + * <code>m</code>. * - * @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 <code>m</code> 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 <code>m</code> 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 |
