aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/exhaustive/exhaustive.c100
-rw-r--r--src/invalid/invalid.c15
-rw-r--r--src/invalid/invalid_thread.c21
-rw-r--r--src/math/curve.c4
-rw-r--r--src/math/gens.c2
-rw-r--r--src/math/types.h2
6 files changed, 77 insertions, 67 deletions
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c
index 3e0209f..55d2212 100644
--- a/src/exhaustive/exhaustive.c
+++ b/src/exhaustive/exhaustive.c
@@ -3,11 +3,9 @@
* Copyright (C) 2017 J08nY
*/
#include "exhaustive.h"
-#include <io/config.h>
#include <math/types.h>
#include "anomalous.h"
#include "io/output.h"
-#include "math/arg.h"
#include "math/curve.h"
#include "math/equation.h"
#include "math/field.h"
@@ -92,9 +90,6 @@ static void exhaustive_ginit(gen_t *generators, const 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;
- }
if (cfg->anomalous) {
arg_t *field_arg = arg_new();
arg_t *eq_arg = arg_new();
@@ -116,13 +111,18 @@ static void exhaustive_ainit(arg_t **argss, const config_t *cfg) {
}
if (cfg->cofactor) {
arg_t *order_arg = arg_new();
+ arg_t *gens_arg = arg_new();
order_arg->args = &cfg->cofactor_bound;
order_arg->nargs = 1;
+ gens_arg->args = &cfg->cofactor_bound;
+ gens_arg->nargs = 1;
argss[OFFSET_ORDER] = order_arg;
+ argss[OFFSET_GENERATORS] = gens_arg;
}
}
void exhaustive_uinit(unroll_t *unrolls, const config_t *cfg) {
+ unrolls[OFFSET_SEED] = &unroll_skip;
unrolls[OFFSET_FIELD] = &unroll_skip;
unrolls[OFFSET_A] = &unroll_skip;
unrolls[OFFSET_B] = &unroll_skip;
@@ -137,64 +137,64 @@ int exhaustive_gen_retry(curve_t *curve, const config_t *cfg,
offset_e start_offset, offset_e end_offset,
int retry) {
if (start_offset == end_offset) {
- return 1;
+ return 2;
}
if (start_offset > end_offset) {
- return -1;
+ return 0;
}
- int num_gens = end_offset - start_offset;
- pari_sp tops[num_gens];
- int tries[num_gens];
- memset(tries, 0, sizeof(int) * num_gens);
+
+ pari_sp stack_tops[OFFSET_END] = {0};
+ int gen_tries[OFFSET_END] = {0};
int state = start_offset;
while (state < end_offset) {
- // remember pari stack
- tops[state - start_offset] = avma;
+ stack_tops[state] = avma;
int diff = generators[state](curve, cfg, argss ? argss[state] : NULL);
- if (diff == INT_MIN) {
- fprintf(stderr, "Error generating a curve. state = %i\n", state);
- return 0;
- }
+ int new_state = state + diff;
+ if (new_state < start_offset) new_state = start_offset;
+
+ if (diff <= 0) {
+ if (diff == INT_MIN || state + diff < 0) {
+ fprintf(stderr, "Error generating a curve. state = %i\n",
+ state);
+ return 0;
+ }
+ // record try
+ int tried = ++gen_tries[state];
+ if (retry && tried >= retry) {
+ if (cfg->verbose) {
+ fprintf(verbose, "Reached retry limit: %i, state = %i\n",
+ retry, state);
+ }
+ return 0;
+ }
- if (state + diff < start_offset) {
- // what now?
- // TODO
- } else if (diff <= 0) {
- // unroll pari stack
- int new_state = state + diff;
+ // unroll
for (int i = state; i > new_state;) {
if (unrolls && unrolls[i]) {
- i += unrolls[i](curve, cfg, tops[i], tops[i - 1]);
+ i += unrolls[i](curve, cfg, stack_tops[i],
+ stack_tops[i - 1]);
} else {
--i;
}
}
- avma = tops[new_state - start_offset];
- }
-
- if (diff <= 0) {
- int tried = ++tries[state - start_offset];
- if (retry && tried >= retry) {
- fprintf(stderr, "Reached retry limit: %i, state = %i\n", retry,
- state);
- return 0;
- }
- } else if (diff > 0) {
- tries[state - start_offset] = 0;
- }
- state += diff;
- if (cfg->verbose) {
- if (diff > 0) {
- fprintf(verbose, "+");
- } else if (diff < 0) {
- fprintf(verbose, "-");
- } else {
- fprintf(verbose, ".");
+ if (cfg->verbose) {
+ if (diff < 0) {
+ fprintf(verbose, "-");
+ } else {
+ fprintf(verbose, ".");
+ }
}
+ // unroll stack
+ avma = stack_tops[new_state];
+ } else {
+ // we are going forward, reset tries
+ gen_tries[state] = 0;
+ if (cfg->verbose) fprintf(verbose, "+");
}
+ state = new_state;
}
if (cfg->verbose) fprintf(verbose, "\n");
@@ -224,9 +224,9 @@ static void exhaustive_quit(arg_t *argss[]) {
int exhaustive_do(config_t *cfg) {
debug("# Starting Exhaustive method\n");
- gen_t generators[OFFSET_END];
- arg_t *argss[OFFSET_END];
- unroll_t unrolls[OFFSET_END];
+ gen_t generators[OFFSET_END] = {NULL};
+ arg_t *argss[OFFSET_END] = {NULL};
+ unroll_t unrolls[OFFSET_END] = {NULL};
exhaustive_ginit(generators, cfg);
exhaustive_ainit(argss, cfg);
exhaustive_uinit(unrolls, cfg);
@@ -235,8 +235,8 @@ int exhaustive_do(config_t *cfg) {
output_o_begin(cfg);
for (unsigned long i = 0; i < cfg->count; ++i) {
curve_t *curve = curve_new();
- if (!exhaustive_gen_retry(curve, cfg, generators, argss, unrolls,
- OFFSET_SEED, OFFSET_END, 10)) {
+ if (!exhaustive_gen(curve, cfg, generators, argss, unrolls, OFFSET_SEED,
+ OFFSET_END)) {
curve_free(&curve);
return 1;
}
diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c
index 18224e8..122bf4f 100644
--- a/src/invalid/invalid.c
+++ b/src/invalid/invalid.c
@@ -5,7 +5,6 @@
#include "invalid.h"
#include "exhaustive/exhaustive.h"
#include "invalid_thread.h"
-#include "io/config.h"
#include "io/output.h"
#include "math/curve.h"
#include "math/equation.h"
@@ -31,6 +30,7 @@ static void invalid_original_ginit(gen_t *generators, const config_t *cfg) {
}
static void invalid_invalid_ginit(gen_t *generators, const config_t *cfg) {
+ generators[OFFSET_SEED] = &gen_skip;
generators[OFFSET_FIELD] = &gen_skip;
generators[OFFSET_A] = &gen_skip;
generators[OFFSET_B] = &b_random;
@@ -121,6 +121,14 @@ static size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes,
}
if (total > 0) {
+ if (!exhaustive_gen_retry(invalid, cfg, invalid_gen, NULL, unrolls,
+ OFFSET_GENERATORS, OFFSET_POINTS, 1)) {
+ curve_unroll(invalid, cfg, avma,
+ btop); // necessary to free the ellinit
+ avma = btop;
+ continue;
+ }
+
// only pass small primes that divide the curve order and those
// where we dont have a curve yet.
// this is passed to points_trial which uses trial division to find
@@ -142,7 +150,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, unrolls,
- OFFSET_GENERATORS, OFFSET_END);
+ OFFSET_POINTS, OFFSET_END);
size_t count = 0;
for (size_t i = nprimes; i-- > 0;) {
@@ -184,7 +192,8 @@ static size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes,
// primes from range divided order. Thus remove it
// like it never existed.
- obj_free(invalid->curve); // necessary to free the ellinit
+ curve_unroll(invalid, cfg, avma,
+ btop); // necessary to free the ellinit
avma = btop;
}
}
diff --git a/src/invalid/invalid_thread.c b/src/invalid/invalid_thread.c
index 1539714..66aca66 100644
--- a/src/invalid/invalid_thread.c
+++ b/src/invalid/invalid_thread.c
@@ -30,8 +30,12 @@ void *invalid_thread(void *arg) {
ndivides++;
}
}
+
debug("ndivides = %lu\n", ndivides);
- if (ndivides > 0) {
+ if (ndivides > 0 &&
+ exhaustive_gen_retry(invalid, thread->cfg, thread->gens,
+ invalid_argss, thread->unrolls,
+ OFFSET_GENERATORS, OFFSET_POINTS, 1)) {
pthread_mutex_lock(thread->mutex_state);
size_t nfree = 0;
// can be up to ndivides, but also lower...
@@ -52,8 +56,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, thread->unrolls,
- OFFSET_GENERATORS, OFFSET_END);
+ invalid_argss, thread->unrolls, OFFSET_POINTS,
+ OFFSET_END);
pthread_mutex_lock(thread->mutex_state);
size_t count = 0;
@@ -76,14 +80,13 @@ void *invalid_thread(void *arg) {
invalid = curve_new();
invalid->field = gcopy(thread->original_curve->field);
invalid->a = gcopy(thread->original_curve->a);
- } else {
- curve_unroll(invalid, thread->cfg, avma, btop);
- avma = btop;
+ continue;
}
- } else {
- curve_unroll(invalid, thread->cfg, avma, btop);
- avma = btop;
}
+
+ // We were unsuccessful for some reasol, unroll
+ curve_unroll(invalid, thread->cfg, avma, btop);
+ avma = btop;
}
curve_free(&invalid);
diff --git a/src/math/curve.c b/src/math/curve.c
index 633cf27..969e628 100644
--- a/src/math/curve.c
+++ b/src/math/curve.c
@@ -66,6 +66,8 @@ curve_t *curve_new_clone(const curve_t *src) {
void curve_free(curve_t **curve) {
if (*curve) {
seed_free(&(*curve)->seed);
+ points_free_deep(&(*curve)->generators, (*curve)->ngens);
+ points_free_deep(&(*curve)->points, (*curve)->npoints);
if ((*curve)->curve) {
// TODO, this is possibly dangerous...
@@ -88,8 +90,6 @@ void curve_free(curve_t **curve) {
gunclone((*curve)->order);
}
- points_free_deep(&(*curve)->generators, (*curve)->ngens);
- points_free_deep(&(*curve)->points, (*curve)->npoints);
pari_free(*curve);
*curve = NULL;
}
diff --git a/src/math/gens.c b/src/math/gens.c
index 5965f9a..119c22b 100644
--- a/src/math/gens.c
+++ b/src/math/gens.c
@@ -6,8 +6,6 @@
#include "point.h"
static int gens_put(curve_t *curve, GEN generators, long len) {
- points_free_deep(&curve->generators, curve->ngens);
-
curve->generators = points_new((size_t)len);
curve->ngens = (size_t)len;
diff --git a/src/math/types.h b/src/math/types.h
index 64b9c99..ab4b224 100644
--- a/src/math/types.h
+++ b/src/math/types.h
@@ -63,7 +63,7 @@ typedef struct {
* @brief
*/
typedef enum {
- OFFSET_SEED,
+ OFFSET_SEED = 0,
OFFSET_FIELD,
OFFSET_A,
OFFSET_B,