diff options
| author | J08nY | 2017-05-23 19:35:46 +0200 |
|---|---|---|
| committer | J08nY | 2017-05-23 19:41:41 +0200 |
| commit | 3f9e42b055c305f05da3f57b2501600488d377a7 (patch) | |
| tree | 6a6c9147f6713f267b26cdf44fd2c0c63005fa8f /src/math | |
| parent | 7abef28a15b4a4d6d3c62453a87910439839bee9 (diff) | |
| download | ecgen-3f9e42b055c305f05da3f57b2501600488d377a7.tar.gz ecgen-3f9e42b055c305f05da3f57b2501600488d377a7.tar.zst ecgen-3f9e42b055c305f05da3f57b2501600488d377a7.zip | |
Add GENERATOR and UNROLL macro, use them
Diffstat (limited to 'src/math')
| -rw-r--r-- | src/math/curve.c | 25 | ||||
| -rw-r--r-- | src/math/curve.h | 8 | ||||
| -rw-r--r-- | src/math/equation.c | 28 | ||||
| -rw-r--r-- | src/math/equation.h | 24 | ||||
| -rw-r--r-- | src/math/field.c | 8 | ||||
| -rw-r--r-- | src/math/field.h | 6 | ||||
| -rw-r--r-- | src/math/gens.c | 6 | ||||
| -rw-r--r-- | src/math/gens.h | 6 | ||||
| -rw-r--r-- | src/math/order.c | 10 | ||||
| -rw-r--r-- | src/math/order.h | 8 | ||||
| -rw-r--r-- | src/math/point.c | 16 | ||||
| -rw-r--r-- | src/math/point.h | 11 | ||||
| -rw-r--r-- | src/math/types.h | 20 |
13 files changed, 94 insertions, 82 deletions
diff --git a/src/math/curve.c b/src/math/curve.c index 90799bb..93b4f52 100644 --- a/src/math/curve.c +++ b/src/math/curve.c @@ -4,7 +4,6 @@ */ #include "curve.h" #include "exhaustive/seed.h" -#include "field.h" #include "point.h" #include "util/memory.h" @@ -88,7 +87,7 @@ void curve_free(curve_t **curve) { } } -int curve_any(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(curve_gen_any) { pari_sp ltop = avma; GEN v = gen_0; switch (typ(curve->field)) { @@ -104,7 +103,7 @@ int curve_any(curve_t *curve, const config_t *cfg, arg_t *args) { gel(v, 4) = curve->b; break; default: - pari_err_TYPE("curve_any", curve->field); + pari_err_TYPE("curve_gen_any", curve->field); } GEN crv = ellinit(v, curve->field, -1); @@ -117,9 +116,9 @@ int curve_any(curve_t *curve, const config_t *cfg, arg_t *args) { } } -int curve_nonzero(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(curve_gen_nonzero) { pari_sp ltop = avma; - int any = curve_any(curve, cfg, args); + int any = curve_gen_any(curve, cfg, args); if (any <= 0) { return any; } @@ -131,30 +130,30 @@ int curve_nonzero(curve_t *curve, const config_t *cfg, arg_t *args) { } } -static int curve_seed_fp(curve_t *curve, const config_t *cfg, arg_t *args) { +static int curve_gen_seed_fp(curve_t *curve, const config_t *cfg, arg_t *args) { // TODO implement return INT_MIN; } -static int curve_seed_f2m(curve_t *curve, const config_t *cfg, arg_t *args) { +static int curve_gen_seed_f2m(curve_t *curve, const config_t *cfg, + arg_t *args) { // TODO implement return INT_MIN; } -int curve_seed(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(curve_gen_seed) { switch (typ(curve->field)) { case t_INT: - return curve_seed_fp(curve, cfg, args); + return curve_gen_seed_fp(curve, cfg, args); case t_FFELT: - return curve_seed_f2m(curve, cfg, args); + return curve_gen_seed_f2m(curve, cfg, args); default: - pari_err_TYPE("curve_seed", curve->field); + pari_err_TYPE("curve_gen_seed", curve->field); return INT_MIN; /* NOT REACHABLE */ } } -int curve_unroll(curve_t *curve, const config_t *cfg, pari_sp from, - pari_sp to) { +UNROLL(curve_unroll) { if (curve->curve) { obj_free(curve->curve); curve->curve = NULL; diff --git a/src/math/curve.h b/src/math/curve.h index c8df723..a283710 100644 --- a/src/math/curve.h +++ b/src/math/curve.h @@ -21,7 +21,7 @@ * @param args unused * @return state diff */ -int curve_any(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(curve_gen_any); /** * GENERATOR(gen_t) @@ -33,7 +33,7 @@ int curve_any(curve_t *curve, const config_t *cfg, arg_t *args); * @param args unused * @return state diff */ -int curve_nonzero(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(curve_gen_nonzero); /** * GENERATOR(gen_t) @@ -46,7 +46,7 @@ int curve_nonzero(curve_t *curve, const config_t *cfg, arg_t *args); * @param args unused * @return state diff */ -int curve_seed(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(curve_gen_seed); /** * @@ -56,7 +56,7 @@ int curve_seed(curve_t *curve, const config_t *cfg, arg_t *args); * @param to * @return */ -int curve_unroll(curve_t *curve, const config_t *cfg, pari_sp from, pari_sp to); +UNROLL(curve_unroll); /** * Allocates and zeros out a new curve_t object. diff --git a/src/math/equation.c b/src/math/equation.c index 4e2e0ec..20313cb 100644 --- a/src/math/equation.c +++ b/src/math/equation.c @@ -6,12 +6,12 @@ #include "io/input.h" #include "math/field.h" -int a_random(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(a_gen_random) { curve->a = genrand(curve->field); return 1; } -int a_input(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(a_gen_input) { pari_sp ltop = avma; GEN inp = input_int("a:", cfg->bits); if (gequalm1(inp)) { @@ -30,13 +30,13 @@ int a_input(curve_t *curve, const config_t *cfg, arg_t *args) { static GEN a = NULL; static curve_t *curve_a = NULL; -int a_once(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(a_gen_once) { if (a && curve_a == curve) { curve->a = gcopy(a); return 1; } - int inp = a_input(curve, cfg, args); + int inp = a_gen_input(curve, cfg, args); if (inp > 0) { a = gclone(curve->a); curve_a = curve; @@ -46,27 +46,27 @@ int a_once(curve_t *curve, const config_t *cfg, arg_t *args) { } } -int a_zero(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(a_gen_zero) { curve->a = gen_0; return 1; } -int a_one(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(a_gen_one) { curve->a = gen_1; return 1; } -int a_seed(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(a_gen_seed) { // TODO implement return INT_MIN; } -int b_random(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(b_gen_random) { curve->b = genrand(curve->field); return 1; } -int b_input(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(b_gen_input) { pari_sp ltop = avma; GEN inp = input_int("b:", cfg->bits); if (gequalm1(inp)) { @@ -85,13 +85,13 @@ int b_input(curve_t *curve, const config_t *cfg, arg_t *args) { static GEN b = NULL; static curve_t *curve_b = NULL; -int b_once(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(b_gen_once) { if (b && curve_b == curve) { curve->b = gcopy(b); return 1; } - int inp = b_input(curve, cfg, args); + int inp = b_gen_input(curve, cfg, args); if (inp > 0) { b = gclone(curve->b); curve_b = curve; @@ -101,17 +101,17 @@ int b_once(curve_t *curve, const config_t *cfg, arg_t *args) { } } -int b_zero(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(b_gen_zero) { curve->b = gen_0; return 1; } -int b_one(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(g_gen_one) { curve->b = gen_1; return 1; } -int b_seed(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(b_gen_seed) { // TODO implement return INT_MIN; } diff --git a/src/math/equation.h b/src/math/equation.h index 858d318..79fad48 100644 --- a/src/math/equation.h +++ b/src/math/equation.h @@ -21,7 +21,7 @@ * @param args * @return state diff */ -int a_random(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(a_gen_random); /** * GENERATOR(gen_t) @@ -32,7 +32,7 @@ int a_random(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return state diff */ -int a_input(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(a_gen_input); /** * GENERATOR(gen_t) @@ -43,7 +43,7 @@ int a_input(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return */ -int a_once(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(a_gen_once); /** * GENERATOR(gen_t) @@ -54,7 +54,7 @@ int a_once(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return state diff */ -int a_zero(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(a_gen_zero); /** * GENERATOR(gen_t) @@ -65,7 +65,7 @@ int a_zero(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return state diff */ -int a_one(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(a_gen_one); /** * @brief @@ -74,7 +74,7 @@ int a_one(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return */ -int a_seed(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(a_gen_seed); /** * GENERATOR(gen_t) @@ -86,7 +86,7 @@ int a_seed(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return state diff */ -int b_random(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(b_gen_random); /** * GENERATOR(gen_t) @@ -97,7 +97,7 @@ int b_random(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return state diff */ -int b_input(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(b_gen_input); /** * GENERATOR(gen_t) @@ -108,7 +108,7 @@ int b_input(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return */ -int b_once(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(b_gen_once); /** * GENERATOR(gen_t) @@ -119,7 +119,7 @@ int b_once(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return state diff */ -int b_zero(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(b_gen_zero); /** * GENERATOR(gen_t) @@ -130,7 +130,7 @@ int b_zero(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return state diff */ -int b_one(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(g_gen_one); /** * @brief @@ -139,7 +139,7 @@ int b_one(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return */ -int b_seed(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(b_gen_seed); /** * diff --git a/src/math/field.c b/src/math/field.c index b140f77..685036a 100644 --- a/src/math/field.c +++ b/src/math/field.c @@ -18,7 +18,7 @@ static GEN field_binaryr(unsigned long bits) { } } -int field_random(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(field_gen_random) { switch (cfg->field) { case FIELD_PRIME: curve->field = field_primer(cfg->bits); @@ -31,7 +31,7 @@ int field_random(curve_t *curve, const config_t *cfg, arg_t *args) { } } -int field_input(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(field_gen_input) { pari_sp ltop = avma; switch (cfg->field) { case FIELD_PRIME: { @@ -97,13 +97,13 @@ int field_input(curve_t *curve, const config_t *cfg, arg_t *args) { static GEN field = NULL; static curve_t *curve_field = NULL; -int field_once(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(field_gen_once) { if (field && curve_field == curve) { curve->field = gcopy(field); return 1; } - int inp = field_input(curve, cfg, args); + int inp = field_gen_input(curve, cfg, args); if (inp > 0) { field = gclone(curve->field); curve_field = curve; diff --git a/src/math/field.h b/src/math/field.h index 0bd79df..f5dc42b 100644 --- a/src/math/field.h +++ b/src/math/field.h @@ -20,7 +20,7 @@ * @param args unused * @return state diff */ -int field_random(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(field_gen_random); /** * GENERATOR(gen_t) @@ -33,7 +33,7 @@ int field_random(curve_t *curve, const config_t *cfg, arg_t *args); * @param args unused * @return state diff */ -int field_input(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(field_gen_input); /** * @@ -42,7 +42,7 @@ int field_input(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return */ -int field_once(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(field_gen_once); /** * Extract a field representation from a field. diff --git a/src/math/gens.c b/src/math/gens.c index 119c22b..f5f9fcb 100644 --- a/src/math/gens.c +++ b/src/math/gens.c @@ -20,13 +20,13 @@ static int gens_put(curve_t *curve, GEN generators, long len) { return 1; } -int gens_any(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(gens_gen_any) { GEN generators = ellff_get_gens(curve->curve); long len = glength(generators); return gens_put(curve, generators, len); } -int gens_one(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(gens_gen_one) { pari_sp ltop = avma; GEN generators = ellff_get_gens(curve->curve); long len = glength(generators); @@ -37,7 +37,7 @@ int gens_one(curve_t *curve, const config_t *cfg, arg_t *args) { return gens_put(curve, generators, len); } -int gens_unroll(curve_t *curve, const config_t *cfg, pari_sp from, pari_sp to) { +UNROLL(gens_unroll) { if (curve->generators) { points_free_deep(&curve->generators, curve->ngens); } diff --git a/src/math/gens.h b/src/math/gens.h index 02bb3a9..7ae9297 100644 --- a/src/math/gens.h +++ b/src/math/gens.h @@ -18,7 +18,7 @@ * @param args * @return */ -int gens_any(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(gens_gen_any); /** * @brief @@ -27,7 +27,7 @@ int gens_any(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return */ -int gens_one(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(gens_gen_one); /** * @@ -37,6 +37,6 @@ int gens_one(curve_t *curve, const config_t *cfg, arg_t *args); * @param to * @return */ -int gens_unroll(curve_t *curve, const config_t *cfg, pari_sp from, pari_sp to); +UNROLL(gens_unroll); #endif // ECGEN_GENS_H diff --git a/src/math/order.c b/src/math/order.c index 037a7f7..4c8c728 100644 --- a/src/math/order.c +++ b/src/math/order.c @@ -4,7 +4,7 @@ */ #include "order.h" -int order_any(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(order_gen_any) { GEN ord = ellff_get_card(curve->curve); if (isclone(ord)) { curve->order = gcopy(ord); @@ -14,7 +14,7 @@ int order_any(curve_t *curve, const config_t *cfg, arg_t *args) { return 1; } -int order_sea(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(order_gen_sea) { pari_sp ltop = avma; GEN order = ellsea(curve->curve, 0); if (gequal0(order)) { @@ -27,9 +27,9 @@ int order_sea(curve_t *curve, const config_t *cfg, arg_t *args) { } } -int order_smallfact(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(order_gen_smallfact) { if (!args) { - fprintf(stderr, "No args to an arged function. order_smallfact\n"); + fprintf(stderr, "No args to an arged function. order_gen_smallfact\n"); return INT_MIN; } @@ -53,7 +53,7 @@ int order_smallfact(curve_t *curve, const config_t *cfg, arg_t *args) { } } -int order_prime(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(order_gen_prime) { pari_sp ltop = avma; GEN order = ellsea(curve->curve, 1); if (gequal0(order) || !(isprime(order))) { diff --git a/src/math/order.h b/src/math/order.h index 0ae5cf8..8a56b94 100644 --- a/src/math/order.h +++ b/src/math/order.h @@ -20,7 +20,7 @@ * @param args * @return state diff */ -int order_any(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(order_gen_any); /** * GENERATOR(gen_t) @@ -31,7 +31,7 @@ int order_any(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return */ -int order_sea(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(order_gen_sea); /** * GENERATOR(gen_t) @@ -41,7 +41,7 @@ int order_sea(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return */ -int order_smallfact(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(order_gen_smallfact); /** * GENERATOR(gen_t) @@ -54,6 +54,6 @@ int order_smallfact(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return state diff */ -int order_prime(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(order_gen_prime); #endif // ECGEN_ORDER_H diff --git a/src/math/point.c b/src/math/point.c index 877cfe6..f590ca2 100644 --- a/src/math/point.c +++ b/src/math/point.c @@ -89,7 +89,7 @@ void points_free_deep(point_t ***points, size_t npoints) { } } -int point_random(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(point_gen_random) { point_t *p = point_new(); p->point = genrand(curve->curve); p->order = ellorder(curve->curve, p->point, NULL); @@ -100,9 +100,9 @@ int point_random(curve_t *curve, const config_t *cfg, arg_t *args) { return 1; } -int points_random(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(points_gen_random) { if (!args) { - fprintf(stderr, "No args to an arged function. points_random\n"); + fprintf(stderr, "No args to an arged function. points_gen_random\n"); return INT_MIN; } @@ -135,10 +135,10 @@ int points_random(curve_t *curve, const config_t *cfg, arg_t *args) { } */ -int points_trial(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(points_gen_trial) { // TODO stack code!!! if (!args) { - fprintf(stderr, "No args to an arged function. points_trial\n"); + fprintf(stderr, "No args to an arged function. points_gen_trial\n"); return INT_MIN; } @@ -173,12 +173,13 @@ int points_trial(curve_t *curve, const config_t *cfg, arg_t *args) { return 1; } -int points_prime(curve_t *curve, const config_t *cfg, arg_t *args) { +GENERATOR(points_gen_prime) { // TODO stack code!!! 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; @@ -210,8 +211,7 @@ int points_prime(curve_t *curve, const config_t *cfg, arg_t *args) { return 1; } -int points_unroll(curve_t *curve, const config_t *cfg, pari_sp from, - pari_sp to) { +UNROLL(points_unroll) { if (curve->points) { points_free_deep(&curve->points, curve->npoints); } diff --git a/src/math/point.h b/src/math/point.h index 15c626b..49c37c6 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -114,7 +114,7 @@ void points_free_deep(point_t ***points, size_t npoints); * @param args unused * @return state diff */ -int point_random(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(point_gen_random); /** * GENERATOR(gen_t) @@ -124,7 +124,7 @@ int point_random(curve_t *curve, const config_t *cfg, arg_t *args); * @param args size_t number of points to generate * @return state diff */ -int points_random(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(points_gen_random); /** * GENERATOR(gen_t) @@ -142,7 +142,7 @@ int points_random(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return state diff */ -int points_trial(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(points_gen_trial); /** * GENERATOR(gen_t) @@ -156,7 +156,7 @@ int points_trial(curve_t *curve, const config_t *cfg, arg_t *args); * @param args * @return state diff */ -int points_prime(curve_t *curve, const config_t *cfg, arg_t *args); +GENERATOR(points_gen_prime); /** * @@ -166,7 +166,6 @@ int points_prime(curve_t *curve, const config_t *cfg, arg_t *args); * @param to * @return */ -int points_unroll(curve_t *curve, const config_t *cfg, pari_sp from, - pari_sp to); +UNROLL(points_unroll); #endif // ECGEN_POINT_H diff --git a/src/math/types.h b/src/math/types.h index 00d78c0..5d9a5b7 100644 --- a/src/math/types.h +++ b/src/math/types.h @@ -84,14 +84,28 @@ typedef struct { } arg_t; /** - * @brief + * @brief A generator function type. + * @param curve + * @param cfg + * @param args + * @return */ -typedef int (*gen_t)(curve_t *, const config_t *, arg_t *); +#define GENERATOR(gen_name) \ + int gen_name(curve_t *curve, const config_t *cfg, arg_t *args) +typedef GENERATOR((*gen_t)); /** * @brief + * @param curve + * @param cfg + * @param from + * @param to + * @return */ -typedef int (*unroll_t)(curve_t *, const config_t *, pari_sp, pari_sp); +#define UNROLL(unroll_name) \ + int unroll_name(curve_t *curve, const config_t *cfg, pari_sp from, \ + pari_sp to) +typedef UNROLL((*unroll_t)); /** * |
