aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/exhaustive/exhaustive.c44
-rw-r--r--src/exhaustive/exhaustive.h14
-rw-r--r--src/invalid/invalid.c10
-rw-r--r--src/invalid/invalid_thread.c11
-rw-r--r--src/invalid/invalid_thread.h1
-rw-r--r--src/math/curve.c11
-rw-r--r--src/math/curve.h10
-rw-r--r--src/math/gens.c8
-rw-r--r--src/math/gens.h10
-rw-r--r--src/math/order.c4
-rw-r--r--src/math/point.c15
-rw-r--r--src/math/point.h11
-rw-r--r--src/math/types.c4
-rw-r--r--src/math/types.h16
14 files changed, 134 insertions, 35 deletions
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c
index be3ab1c..2f7dda9 100644
--- a/src/exhaustive/exhaustive.c
+++ b/src/exhaustive/exhaustive.c
@@ -13,7 +13,7 @@
#include "math/point.h"
#include "seed.h"
-static void exhaustive_ginit(gen_t *generators, config_t *cfg) {
+static void exhaustive_ginit(gen_t *generators, const config_t *cfg) {
if (cfg->from_seed) {
if (cfg->seed) {
generators[OFFSET_SEED] = &seed_argument;
@@ -81,7 +81,7 @@ static void exhaustive_ginit(gen_t *generators, config_t *cfg) {
}
}
-static void exhaustive_ainit(arg_t **argss, config_t *cfg) {
+static void exhaustive_ainit(arg_t **argss, const config_t *cfg) {
for (size_t i = 0; i < OFFSET_END; ++i) {
argss[i] = NULL;
}
@@ -99,8 +99,18 @@ static void exhaustive_ainit(arg_t **argss, config_t *cfg) {
}
}
+void exhaustive_uinit(unroll_t *unrolls, const config_t *cfg) {
+ unrolls[OFFSET_FIELD] = &unroll_skip;
+ unrolls[OFFSET_A] = &unroll_skip;
+ unrolls[OFFSET_B] = &unroll_skip;
+ unrolls[OFFSET_CURVE] = &curve_unroll;
+ unrolls[OFFSET_ORDER] = &unroll_skip;
+ unrolls[OFFSET_GENERATORS] = &gens_unroll;
+ unrolls[OFFSET_POINTS] = &points_unroll;
+}
+
int exhaustive_gen_retry(curve_t *curve, const config_t *cfg,
- gen_t generators[], arg_t *argss[],
+ gen_t generators[], arg_t *argss[], unroll_t unrolls[],
offset_e start_offset, offset_e end_offset,
int retry) {
if (start_offset == end_offset) {
@@ -129,12 +139,17 @@ int exhaustive_gen_retry(curve_t *curve, const config_t *cfg,
// what now?
// TODO
} else if (diff <= 0) {
- // rewind pari stack
- int new_state = state + diff - start_offset;
- if (new_state <= OFFSET_CURVE) {
- // obj_free(curve->curve);
+ // unroll pari stack
+ int new_state = state + diff;
+ for (int i = state; i > new_state;) {
+ if (unrolls && unrolls[i]) {
+ debug("Unroll from state %i to state %i\n", i, i - 1);
+ i += unrolls[i](curve, cfg, tops[i], tops[i - 1]);
+ } else {
+ --i;
+ }
}
- avma = tops[new_state];
+ avma = tops[new_state - start_offset];
}
if (diff == 0) {
@@ -166,9 +181,10 @@ int exhaustive_gen_retry(curve_t *curve, const config_t *cfg,
}
int exhaustive_gen(curve_t *curve, const config_t *cfg, gen_t generators[],
- arg_t *argss[], offset_e start_offset, offset_e end_offset) {
- return exhaustive_gen_retry(curve, cfg, generators, argss, start_offset,
- end_offset, 0);
+ arg_t *argss[], unroll_t unrolls[], offset_e start_offset,
+ offset_e end_offset) {
+ return exhaustive_gen_retry(curve, cfg, generators, argss, unrolls,
+ start_offset, end_offset, 0);
}
static void exhaustive_quit(arg_t *argss[]) {
@@ -185,13 +201,15 @@ int exhaustive_do(config_t *cfg) {
gen_t generators[OFFSET_END];
arg_t *argss[OFFSET_END];
+ unroll_t unrolls[OFFSET_END];
exhaustive_ginit(generators, cfg);
exhaustive_ainit(argss, cfg);
+ exhaustive_uinit(unrolls, cfg);
for (unsigned long i = 0; i < cfg->count; ++i) {
curve_t *curve = curve_new();
- if (!exhaustive_gen_retry(curve, cfg, generators, argss, OFFSET_SEED,
- OFFSET_END, 10)) {
+ if (!exhaustive_gen_retry(curve, cfg, generators, argss, unrolls,
+ OFFSET_SEED, OFFSET_END, 10)) {
curve_free(&curve);
return 1;
}
diff --git a/src/exhaustive/exhaustive.h b/src/exhaustive/exhaustive.h
index 934133f..afcdc5b 100644
--- a/src/exhaustive/exhaustive.h
+++ b/src/exhaustive/exhaustive.h
@@ -12,17 +12,25 @@
/**
*
+ * @param unrolls
+ * @param cfg
+ */
+void exhaustive_uinit(unroll_t *unrolls, const config_t *cfg);
+
+/**
+ *
* @param curve
* @param cfg
* @param generators
* @param argss
+ * @param unrolls
* @param start_offset
* @param end_offset
* @param retry
* @return
*/
int exhaustive_gen_retry(curve_t *curve, const config_t *cfg,
- gen_t generators[], arg_t *argss[],
+ gen_t generators[], arg_t *argss[], unroll_t unrolls[],
offset_e start_offset, offset_e end_offset, int retry);
/**
@@ -31,12 +39,14 @@ int exhaustive_gen_retry(curve_t *curve, const config_t *cfg,
* @param config
* @param generators
* @param argss
+ * @param unrolls
* @param start_offset
* @param end_offset
* @return
*/
int exhaustive_gen(curve_t *curve, const config_t *cfg, gen_t generators[],
- arg_t *argss[], offset_e start_offset, offset_e end_offset);
+ arg_t *argss[], unroll_t unrolls[], offset_e start_offset,
+ offset_e end_offset);
/**
*
diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c
index e01a49c..e6715b7 100644
--- a/src/invalid/invalid.c
+++ b/src/invalid/invalid.c
@@ -101,7 +101,7 @@ static 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, NULL, OFFSET_B,
+ exhaustive_gen(invalid, cfg, invalid_gen, NULL, NULL, OFFSET_B,
OFFSET_GENERATORS);
// does some small prime from our array divide the curve order?
@@ -140,7 +140,7 @@ static 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,
+ exhaustive_gen(invalid, cfg, invalid_gen, invalid_argss, NULL,
OFFSET_GENERATORS, OFFSET_END);
size_t count = 0;
@@ -269,14 +269,16 @@ int invalid_do(config_t *cfg) {
gen_t gen[OFFSET_END];
arg_t *argss[OFFSET_END];
+ unroll_t unrolls[OFFSET_END];
invalid_original_ginit(gen, cfg);
+ exhaustive_uinit(unrolls, cfg);
// create the curve to invalidate
// Either from input or random with -
curve_t *curve = curve_new();
// actually generate the curve
- if (!exhaustive_gen_retry(curve, cfg, gen, argss, OFFSET_FIELD,
- OFFSET_POINTS, 1)) {
+ if (!exhaustive_gen(curve, cfg, gen, argss, NULL, OFFSET_FIELD,
+ OFFSET_POINTS)) {
curve_free(&curve);
return 1;
}
diff --git a/src/invalid/invalid_thread.c b/src/invalid/invalid_thread.c
index 2f61ffa..a06bafa 100644
--- a/src/invalid/invalid_thread.c
+++ b/src/invalid/invalid_thread.c
@@ -22,8 +22,8 @@ void *invalid_thread(void *arg) {
while (*thread->generated < thread->nprimes) {
pari_sp btop = avma;
- exhaustive_gen(invalid, thread->cfg, thread->gens, NULL, OFFSET_B,
- OFFSET_GENERATORS);
+ exhaustive_gen(invalid, thread->cfg, thread->gens, NULL,
+ thread->unrolls, OFFSET_B, OFFSET_GENERATORS);
size_t ndivides = 0;
for (size_t i = thread->nprimes; i-- > 0;) {
if (dvdis(invalid->order, thread->primes[i])) {
@@ -53,7 +53,8 @@ void *invalid_thread(void *arg) {
arg_t prime_divisors = {primes, nprimes};
invalid_argss[OFFSET_POINTS] = &prime_divisors;
exhaustive_gen(invalid, thread->cfg, thread->gens,
- invalid_argss, OFFSET_GENERATORS, OFFSET_END);
+ invalid_argss, thread->unrolls,
+ OFFSET_GENERATORS, OFFSET_END);
pthread_mutex_lock(thread->mutex_state);
size_t count = 0;
@@ -77,11 +78,11 @@ void *invalid_thread(void *arg) {
invalid->field = gcopy(thread->original_curve->field);
invalid->a = gcopy(thread->original_curve->a);
} else {
- obj_free(invalid->curve); // necessary to free the ellinit
+ curve_unroll(invalid, thread->cfg, avma, btop);
avma = btop;
}
} else {
- obj_free(invalid->curve); // necessary to free the ellinit
+ curve_unroll(invalid, thread->cfg, avma, btop);
avma = btop;
}
}
diff --git a/src/invalid/invalid_thread.h b/src/invalid/invalid_thread.h
index bc30d04..4486961 100644
--- a/src/invalid/invalid_thread.h
+++ b/src/invalid/invalid_thread.h
@@ -25,6 +25,7 @@ typedef struct {
pthread_cond_t *cond_generated;
config_t *cfg;
gen_t *gens;
+ unroll_t *unrolls;
} thread_t;
/**
diff --git a/src/math/curve.c b/src/math/curve.c
index ba21280..8e8e832 100644
--- a/src/math/curve.c
+++ b/src/math/curve.c
@@ -5,8 +5,8 @@
#include "curve.h"
#include "exhaustive/seed.h"
#include "field.h"
+#include "io/output.h"
#include "point.h"
-#include "types.h"
curve_t *curve_new(void) {
curve_t *curve = pari_malloc(sizeof(curve_t));
@@ -161,6 +161,15 @@ int curve_seed(curve_t *curve, const config_t *cfg, arg_t *args) {
}
}
+int curve_unroll(curve_t *curve, const config_t *cfg, pari_sp from,
+ pari_sp to) {
+ if (curve->curve) {
+ obj_free(curve->curve);
+ curve->curve = NULL;
+ }
+ return -1;
+}
+
GEN curve_params(const curve_t *curve) {
pari_sp ltop = avma;
diff --git a/src/math/curve.h b/src/math/curve.h
index 65dc4b7..d1aaf27 100644
--- a/src/math/curve.h
+++ b/src/math/curve.h
@@ -50,6 +50,16 @@ int curve_nonzero(curve_t *curve, const config_t *cfg, arg_t *args);
int curve_seed(curve_t *curve, const config_t *cfg, arg_t *args);
/**
+ *
+ * @param curve
+ * @param cfg
+ * @param from
+ * @param to
+ * @return
+ */
+int curve_unroll(curve_t *curve, const config_t *cfg, pari_sp from, pari_sp to);
+
+/**
* Serializes curve parameters into a t_VEC:
* - prime field:
* p,a,b,order,(point.x, point.y, point.order)*
diff --git a/src/math/gens.c b/src/math/gens.c
index f224386..ef47525 100644
--- a/src/math/gens.c
+++ b/src/math/gens.c
@@ -3,6 +3,7 @@
* Copyright (C) 2017 J08nY
*/
#include "gens.h"
+#include "io/output.h"
#include "point.h"
static int gens_put(curve_t *curve, GEN generators, long len) {
@@ -38,3 +39,10 @@ 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) {
+ if (curve->generators) {
+ points_free_deep(&curve->generators, curve->ngens);
+ }
+ return -1;
+}
diff --git a/src/math/gens.h b/src/math/gens.h
index 0160074..02bb3a9 100644
--- a/src/math/gens.h
+++ b/src/math/gens.h
@@ -29,4 +29,14 @@ int gens_any(curve_t *curve, const config_t *cfg, arg_t *args);
*/
int gens_one(curve_t *curve, const config_t *cfg, arg_t *args);
+/**
+ *
+ * @param curve
+ * @param cfg
+ * @param from
+ * @param to
+ * @return
+ */
+int gens_unroll(curve_t *curve, const config_t *cfg, pari_sp from, pari_sp to);
+
#endif // ECGEN_GENS_H
diff --git a/src/math/order.c b/src/math/order.c
index 17b7bca..7f90849 100644
--- a/src/math/order.c
+++ b/src/math/order.c
@@ -34,7 +34,7 @@ int order_smallfact(curve_t *curve, const config_t *cfg, arg_t *args) {
return -4;
} else {
curve->order = order;
- obj_insert_shallow(curve->curve, 1, curve->order);
+ obj_insert(curve->curve, 1, curve->order);
return 1;
}
}
@@ -47,7 +47,7 @@ int order_prime(curve_t *curve, const config_t *cfg, arg_t *args) {
return -4;
} else {
curve->order = order;
- obj_insert_shallow(curve->curve, 1, curve->order);
+ obj_insert(curve->curve, 1, curve->order);
return 1;
}
}
diff --git a/src/math/point.c b/src/math/point.c
index dc9cd4a..8b3dcef 100644
--- a/src/math/point.c
+++ b/src/math/point.c
@@ -3,7 +3,7 @@
* Copyright (C) 2017 J08nY
*/
#include "point.h"
-#include "types.h"
+#include "io/output.h"
point_t *point_new(void) {
point_t *point = pari_malloc(sizeof(point_t));
@@ -106,8 +106,6 @@ void points_free_deep(point_t ***points, size_t npoints) {
}
int point_random(curve_t *curve, const config_t *cfg, arg_t *args) {
- points_free_deep(&curve->points, curve->npoints);
-
point_t *p = point_new();
p->point = genrand(curve->curve);
p->order = ellorder(curve->curve, p->point, NULL);
@@ -123,7 +121,6 @@ int points_random(curve_t *curve, const config_t *cfg, arg_t *args) {
fprintf(stderr, "No args to an arged function. points_random");
return INT_MIN;
}
- points_free_deep(&curve->points, curve->npoints);
size_t npoints = *(size_t *)args->args;
@@ -160,7 +157,6 @@ int points_trial(curve_t *curve, const config_t *cfg, arg_t *args) {
fprintf(stderr, "No args to an arged function. points_trial");
return INT_MIN;
}
- points_free_deep(&curve->points, curve->npoints);
pari_ulong *primes = (pari_ulong *)args->args;
size_t nprimes = args->nargs;
@@ -195,7 +191,6 @@ int points_trial(curve_t *curve, const config_t *cfg, arg_t *args) {
int points_prime(curve_t *curve, const config_t *cfg, arg_t *args) {
// TODO stack code!!!
- points_free_deep(&curve->points, curve->npoints);
GEN factors = Z_factor(curve->order);
GEN primes = gel(factors, 1);
@@ -230,3 +225,11 @@ 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) {
+ if (curve->points) {
+ points_free_deep(&curve->points, curve->npoints);
+ }
+ return -1;
+}
diff --git a/src/math/point.h b/src/math/point.h
index 9eef8a4..ef6facf 100644
--- a/src/math/point.h
+++ b/src/math/point.h
@@ -159,4 +159,15 @@ int points_trial(curve_t *curve, const config_t *cfg, arg_t *args);
*/
int points_prime(curve_t *curve, const config_t *cfg, arg_t *args);
+/**
+ *
+ * @param curve
+ * @param cfg
+ * @param from
+ * @param to
+ * @return
+ */
+int points_unroll(curve_t *curve, const config_t *cfg, pari_sp from,
+ pari_sp to);
+
#endif // ECGEN_POINT_H
diff --git a/src/math/types.c b/src/math/types.c
index 49d8620..afd6542 100644
--- a/src/math/types.c
+++ b/src/math/types.c
@@ -5,3 +5,7 @@
#include "types.h"
int gen_skip(curve_t *curve, const config_t *cfg, arg_t *args) { return 1; }
+
+int unroll_skip(curve_t *curve, const config_t *cfg, pari_sp from, pari_sp to) {
+ return -1;
+}
diff --git a/src/math/types.h b/src/math/types.h
index 4dee9dd..3d2476a 100644
--- a/src/math/types.h
+++ b/src/math/types.h
@@ -46,14 +46,16 @@ typedef enum {
} offset_e;
typedef struct {
- void *args;
+ const void *args;
size_t nargs;
} arg_t;
typedef int (*gen_t)(curve_t *, const config_t *, arg_t *);
+typedef int (*unroll_t)(curve_t *, const config_t *, pari_sp, pari_sp);
+
/**
- * @brief
+ *
* @param curve
* @param config
* @param args
@@ -61,4 +63,14 @@ typedef int (*gen_t)(curve_t *, const config_t *, arg_t *);
*/
int gen_skip(curve_t *curve, const config_t *cfg, arg_t *args);
+/**
+ *
+ * @param curve
+ * @param cfg
+ * @param from
+ * @param to
+ * @return
+ */
+int unroll_skip(curve_t *curve, const config_t *cfg, pari_sp from, pari_sp to);
+
#endif // ECGEN_TYPES_H