summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2017-09-29 17:48:58 +0200
committerJ08nY2017-09-29 17:48:58 +0200
commit49bd1736641a101dde23f2bbe994159ed33ef347 (patch)
tree0c2b9ea76272807c69e8e07c7d3249ab88509e9b
parent63aa880e19b49ab83132b0032ddd9e1f43029e50 (diff)
downloadecgen-49bd1736641a101dde23f2bbe994159ed33ef347.tar.gz
ecgen-49bd1736641a101dde23f2bbe994159ed33ef347.tar.zst
ecgen-49bd1736641a101dde23f2bbe994159ed33ef347.zip
-rw-r--r--src/exhaustive/ansi.c11
-rw-r--r--src/exhaustive/exhaustive.c54
-rw-r--r--src/exhaustive/exhaustive.h23
-rw-r--r--src/gen/seed.c17
-rw-r--r--src/invalid/invalid.c44
-rw-r--r--src/invalid/invalid_thread.c16
-rw-r--r--src/invalid/invalid_thread.h8
-rw-r--r--src/io/cli.c7
-rw-r--r--src/misc/types.c2
-rw-r--r--src/misc/types.h24
10 files changed, 132 insertions, 74 deletions
diff --git a/src/exhaustive/ansi.c b/src/exhaustive/ansi.c
index bbdf24e..51d20e5 100644
--- a/src/exhaustive/ansi.c
+++ b/src/exhaustive/ansi.c
@@ -48,7 +48,7 @@ static void seed_tsh(seed_t *seed, const config_t *cfg) {
pari_sp ltop = avma;
seed->ansi.t = utoi(cfg->bits);
seed->ansi.s =
- floorr(rdivii(subis(seed->ansi.t, 1), stoi(160), DEFAULTPREC));
+ floorr(rdivii(subis(seed->ansi.t, 1), stoi(160), DEFAULTPREC));
seed->ansi.h = subii(seed->ansi.t, mulis(seed->ansi.s, 160));
gerepileall(ltop, 3, &seed->ansi.t, &seed->ansi.s, &seed->ansi.h);
}
@@ -182,9 +182,12 @@ static GENERATOR(ansi_gen_equation_f2m) {
GENERATOR(ansi_gen_equation) {
switch (cfg->field) {
- case FIELD_PRIME: return ansi_gen_equation_fp(curve, cfg, args);
- case FIELD_BINARY: return ansi_gen_equation_f2m(curve, cfg, args);
- default: pari_err_BUG("Field not prime or binary?");
+ case FIELD_PRIME:
+ return ansi_gen_equation_fp(curve, cfg, args);
+ case FIELD_BINARY:
+ return ansi_gen_equation_f2m(curve, cfg, args);
+ default:
+ pari_err_BUG("Field not prime or binary?");
return INT_MIN; /* NOT REACHABLE */
}
}
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c
index e1044f9..afbacff 100644
--- a/src/exhaustive/exhaustive.c
+++ b/src/exhaustive/exhaustive.c
@@ -32,10 +32,14 @@ static void exhaustive_ginit(gen_t *generators, const config_t *cfg) {
generators[OFFSET_A] = &gen_skip;
generators[OFFSET_B] = &ansi_gen_equation;
}
- case SEED_BRAINPOOL:break;
- case SEED_BRAINPOOL_RFC:break;
- case SEED_FIPS:break;
- default:break;
+ case SEED_BRAINPOOL:
+ break;
+ case SEED_BRAINPOOL_RFC:
+ break;
+ case SEED_FIPS:
+ break;
+ default:
+ break;
}
generators[OFFSET_CURVE] = &curve_gen_nonzero;
generators[OFFSET_ORDER] = &order_gen_any;
@@ -119,6 +123,8 @@ static void exhaustive_ginit(gen_t *generators, const config_t *cfg) {
}
}
+static void exhaustive_cinit(check_t *validators, const config_t *cfg) {}
+
static void exhaustive_ainit(arg_t **argss, const config_t *cfg) {
if (cfg->anomalous) {
arg_t *field_arg = arg_new();
@@ -167,15 +173,18 @@ void exhaustive_uinit(unroll_t *unrolls, const config_t *cfg) {
}
int exhaustive_gen_retry(curve_t *curve, const config_t *cfg,
- gen_t generators[], arg_t *argss[], unroll_t unrolls[],
- offset_e start_offset, offset_e end_offset,
- int retry) {
+ const exhaustive_t *setup, offset_e start_offset,
+ offset_e end_offset, int retry) {
if (start_offset == end_offset) {
return 2;
}
if (start_offset > end_offset) {
return 0;
}
+ gen_t *generators = setup->generators;
+ check_t *validators = setup->validators;
+ arg_t **argss = setup->argss;
+ unroll_t *unrolls = setup->unrolls;
pari_sp stack_tops[OFFSET_END] = {0};
int gen_tries[OFFSET_END] = {0};
@@ -235,14 +244,19 @@ int exhaustive_gen_retry(curve_t *curve, const config_t *cfg,
return 1;
}
-int exhaustive_gen(curve_t *curve, const config_t *cfg, gen_t generators[],
- arg_t *argss[], unroll_t unrolls[], offset_e start_offset,
+int exhaustive_gen(curve_t *curve, const config_t *cfg,
+ const exhaustive_t *setup, offset_e start_offset,
offset_e end_offset) {
- return exhaustive_gen_retry(curve, cfg, generators, argss, unrolls,
- start_offset, end_offset, 0);
+ return exhaustive_gen_retry(curve, cfg, setup, start_offset, end_offset, 0);
}
-static void exhaustive_init() { anomalous_init(); }
+static void exhaustive_init(exhaustive_t *setup, const config_t *cfg) {
+ exhaustive_ginit(setup->generators, cfg);
+ exhaustive_cinit(setup->validators, cfg);
+ exhaustive_ainit(setup->argss, cfg);
+ exhaustive_uinit(setup->unrolls, cfg);
+ anomalous_init();
+}
static void exhaustive_quit(arg_t *argss[]) {
equation_quit();
@@ -258,19 +272,21 @@ int exhaustive_do(config_t *cfg) {
debug_log_start("Starting Exhaustive method");
gen_t generators[OFFSET_END] = {NULL};
+ check_t validators[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);
- exhaustive_init();
+
+ exhaustive_t setup = {.generators = generators,
+ .validators = validators,
+ .argss = argss,
+ .unrolls = unrolls};
+ exhaustive_init(&setup, cfg);
output_o_begin(cfg);
for (unsigned long i = 0; i < cfg->count; ++i) {
debug_log_start("Generating new curve");
curve_t *curve = curve_new();
- if (!exhaustive_gen(curve, cfg, generators, argss, unrolls, OFFSET_SEED,
- OFFSET_END)) {
+ if (!exhaustive_gen(curve, cfg, &setup, OFFSET_SEED, OFFSET_END)) {
curve_free(&curve);
return EXIT_FAILURE;
}
@@ -284,7 +300,7 @@ int exhaustive_do(config_t *cfg) {
}
output_o_end(cfg);
- exhaustive_quit(argss);
+ exhaustive_quit(setup.argss);
debug_log_end("Finished Exhaustive method");
return EXIT_SUCCESS;
}
diff --git a/src/exhaustive/exhaustive.h b/src/exhaustive/exhaustive.h
index 446dff2..9419ff4 100644
--- a/src/exhaustive/exhaustive.h
+++ b/src/exhaustive/exhaustive.h
@@ -10,6 +10,13 @@
#include "misc/types.h"
+typedef struct {
+ gen_t *generators;
+ check_t *validators;
+ arg_t **argss;
+ unroll_t *unrolls;
+} exhaustive_t;
+
/**
*
* @param unrolls
@@ -21,31 +28,27 @@ void exhaustive_uinit(unroll_t *unrolls, const config_t *cfg);
*
* @param curve
* @param cfg
- * @param generators
- * @param argss
- * @param unrolls
+ * @param setup
* @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[], unroll_t unrolls[],
- offset_e start_offset, offset_e end_offset, int retry);
+ const exhaustive_t *setup, offset_e start_offset,
+ offset_e end_offset, int retry);
/**
*
* @param curve
* @param config
- * @param generators
- * @param argss
- * @param unrolls
+ * @param setup
* @param start_offset
* @param end_offset
* @return
*/
-int exhaustive_gen(curve_t *curve, const config_t *cfg, gen_t generators[],
- arg_t *argss[], unroll_t unrolls[], offset_e start_offset,
+int exhaustive_gen(curve_t *curve, const config_t *cfg,
+ const exhaustive_t *setup, offset_e start_offset,
offset_e end_offset);
/**
diff --git a/src/gen/seed.c b/src/gen/seed.c
index b1d7aa1..f349982 100644
--- a/src/gen/seed.c
+++ b/src/gen/seed.c
@@ -3,8 +3,8 @@
* Copyright (C) 2017 J08nY
*/
-#include <misc/types.h>
#include "seed.h"
+#include <misc/types.h>
#include "util/bits.h"
#include "util/memory.h"
@@ -43,11 +43,16 @@ void seed_free(seed_t **seed) {
try_free((*seed)->hash20);
}
switch ((*seed)->type) {
- case SEED_ANSI:break;
- case SEED_BRAINPOOL:break;
- case SEED_BRAINPOOL_RFC:break;
- case SEED_FIPS:break;
- case SEED_NONE:break;
+ case SEED_ANSI:
+ break;
+ case SEED_BRAINPOOL:
+ break;
+ case SEED_BRAINPOOL_RFC:
+ break;
+ case SEED_FIPS:
+ break;
+ case SEED_NONE:
+ break;
}
try_free(*seed);
*seed = NULL;
diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c
index cd00b43..f0a1941 100644
--- a/src/invalid/invalid.c
+++ b/src/invalid/invalid.c
@@ -3,6 +3,7 @@
* Copyright (C) 2017 J08nY
*/
#include "invalid.h"
+#include <exhaustive/exhaustive.h>
#include "exhaustive/exhaustive.h"
#include "gen/curve.h"
#include "gen/equation.h"
@@ -79,10 +80,11 @@ static size_t invalid_primes(GEN order, pari_ulong **primes) {
return nprimes;
}
-static size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes,
- size_t nprimes, curve_t **curves,
- gen_t invalid_gen[], unroll_t unrolls[]) {
+static size_t invalid_curves(const curve_t *curve, const config_t *cfg,
+ pari_ulong *primes, size_t nprimes,
+ curve_t **curves, exhaustive_t *setup) {
arg_t *invalid_argss[OFFSET_END];
+ setup->argss = invalid_argss;
// Alloc a curve, and only alloc a new one when this pointer is saved into
// **curves
@@ -95,8 +97,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, unrolls, OFFSET_B,
- OFFSET_GENERATORS);
+ exhaustive_gen(invalid, cfg, setup, OFFSET_B, OFFSET_GENERATORS);
// does some small prime from our array divide the curve order?
// if so how many?
@@ -114,8 +115,8 @@ 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)) {
+ if (!exhaustive_gen_retry(invalid, cfg, setup, OFFSET_GENERATORS,
+ OFFSET_POINTS, 1)) {
curve_unroll(invalid, cfg, avma,
btop); // necessary to free the ellinit
avma = btop;
@@ -143,8 +144,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_POINTS, OFFSET_END);
+ exhaustive_gen(invalid, cfg, setup, OFFSET_POINTS, OFFSET_END);
size_t count = 0;
for (size_t i = nprimes; i-- > 0;) {
@@ -196,10 +196,9 @@ static size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes,
return ncurves;
}
-static size_t invalid_curves_threaded(curve_t *curve, config_t *cfg,
+static size_t invalid_curves_threaded(const curve_t *curve, const config_t *cfg,
pari_ulong *primes, size_t nprimes,
- curve_t **curves, gen_t invalid_gen[],
- unroll_t unrolls[]) {
+ curve_t **curves, exhaustive_t *setup) {
pthread_t pthreads[cfg->threads];
thread_t threads[cfg->threads];
struct pari_thread pari_threads[cfg->threads];
@@ -230,8 +229,7 @@ static size_t invalid_curves_threaded(curve_t *curve, config_t *cfg,
threads[i].mutex_state = &state_mutex;
threads[i].cond_generated = &generated_cond;
threads[i].cfg = cfg;
- threads[i].gens = invalid_gen;
- threads[i].unrolls = unrolls;
+ threads[i].setup = setup;
}
pthread_mutex_lock(&state_mutex);
@@ -284,11 +282,15 @@ int invalid_do(config_t *cfg) {
unroll_t common_unrolls[OFFSET_END];
invalid_original_ginit(original_gens, cfg);
exhaustive_uinit(common_unrolls, cfg);
+ exhaustive_t original_setup = {.generators = original_gens,
+ .validators = NULL,
+ .argss = original_argss,
+ .unrolls = common_unrolls};
debug_log_start("Starting to create curve to invalidate");
curve_t *curve = curve_new();
- if (!exhaustive_gen(curve, cfg, original_gens, original_argss,
- common_unrolls, OFFSET_FIELD, OFFSET_POINTS)) {
+ if (!exhaustive_gen(curve, cfg, &original_setup, OFFSET_FIELD,
+ OFFSET_POINTS)) {
curve_free(&curve);
return EXIT_FAILURE;
}
@@ -310,15 +312,19 @@ int invalid_do(config_t *cfg) {
// init the invalid curve gen_t
gen_t invalid_gens[OFFSET_END];
invalid_invalid_ginit(invalid_gens, cfg);
+ exhaustive_t invalid_setup = {.generators = invalid_gens,
+ .validators = NULL,
+ .argss = NULL,
+ .unrolls = common_unrolls};
debug_log_start("Starting to generate invalid curves");
size_t ncurves;
if (cfg->threads == 1) {
- ncurves = invalid_curves(curve, cfg, primes, nprimes, curves,
- invalid_gens, common_unrolls);
+ ncurves =
+ invalid_curves(curve, cfg, primes, nprimes, curves, &invalid_setup);
} else {
ncurves = invalid_curves_threaded(curve, cfg, primes, nprimes, curves,
- invalid_gens, common_unrolls);
+ &invalid_setup);
}
debug_log_end("Finished generating invalid curves");
output_o_end(cfg);
diff --git a/src/invalid/invalid_thread.c b/src/invalid/invalid_thread.c
index b94e456..be2c4aa 100644
--- a/src/invalid/invalid_thread.c
+++ b/src/invalid/invalid_thread.c
@@ -14,6 +14,10 @@ void *invalid_thread(void *arg) {
pari_thread_start(thread->pari_thread);
random_init();
arg_t *invalid_argss[OFFSET_END];
+ exhaustive_t invalid_setup = {.generators = thread->setup->generators,
+ .validators = thread->setup->validators,
+ .argss = invalid_argss,
+ .unrolls = thread->setup->unrolls};
curve_t *invalid = curve_new();
invalid->field = gcopy(thread->original_curve->field);
@@ -21,8 +25,8 @@ void *invalid_thread(void *arg) {
while (*thread->generated < thread->nprimes) {
pari_sp btop = avma;
- exhaustive_gen(invalid, thread->cfg, thread->gens, NULL,
- thread->unrolls, OFFSET_B, OFFSET_GENERATORS);
+ exhaustive_gen(invalid, thread->cfg, thread->setup, OFFSET_B,
+ OFFSET_GENERATORS);
size_t ndivides = 0;
for (size_t i = thread->nprimes; i-- > 0;) {
if (dvdis(invalid->order, thread->primes[i])) {
@@ -33,8 +37,7 @@ void *invalid_thread(void *arg) {
debug("ndivides = %lu\n", ndivides);
if (ndivides > 0 &&
- exhaustive_gen_retry(invalid, thread->cfg, thread->gens,
- invalid_argss, thread->unrolls,
+ exhaustive_gen_retry(invalid, thread->cfg, &invalid_setup,
OFFSET_GENERATORS, OFFSET_POINTS, 1)) {
pthread_mutex_lock(thread->mutex_state);
size_t nfree = 0;
@@ -55,9 +58,8 @@ void *invalid_thread(void *arg) {
if (nfree > 0) {
arg_t prime_divisors = {primes, nprimes};
invalid_argss[OFFSET_POINTS] = &prime_divisors;
- exhaustive_gen(invalid, thread->cfg, thread->gens,
- invalid_argss, thread->unrolls, OFFSET_POINTS,
- OFFSET_END);
+ exhaustive_gen(invalid, thread->cfg, &invalid_setup,
+ OFFSET_POINTS, OFFSET_END);
pthread_mutex_lock(thread->mutex_state);
size_t count = 0;
diff --git a/src/invalid/invalid_thread.h b/src/invalid/invalid_thread.h
index 0f1b4d0..e93bbda 100644
--- a/src/invalid/invalid_thread.h
+++ b/src/invalid/invalid_thread.h
@@ -8,6 +8,7 @@
#ifndef ECGEN_INVALID_THREAD_H
#define ECGEN_INVALID_THREAD_H
+#include <exhaustive/exhaustive.h>
#include <pthread.h>
#include "misc/types.h"
@@ -15,7 +16,7 @@ typedef enum { STATE_FREE, STATE_GENERATING, STATE_GENERATED } state_e;
typedef struct {
struct pari_thread *pari_thread;
- curve_t *original_curve;
+ const curve_t *original_curve;
size_t nprimes;
pari_ulong *primes;
state_e *states;
@@ -23,9 +24,8 @@ typedef struct {
size_t *generated;
pthread_mutex_t *mutex_state;
pthread_cond_t *cond_generated;
- config_t *cfg;
- gen_t *gens;
- unroll_t *unrolls;
+ const config_t *cfg;
+ const exhaustive_t *setup;
} thread_t;
/**
diff --git a/src/io/cli.c b/src/io/cli.c
index 838c9bf..77f279f 100644
--- a/src/io/cli.c
+++ b/src/io/cli.c
@@ -234,7 +234,8 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) {
"--f2m (but not both).");
}
// Invalid is not prime or seed by definition.
- if (cfg->invalid && (cfg->prime || cfg->seed_algo || cfg->cofactor)) {
+ if (cfg->invalid &&
+ (cfg->prime || cfg->seed_algo || cfg->cofactor)) {
// not seed, not prime
argp_failure(state, 1, 0,
"Invalid curve generation can not generate curves "
@@ -249,8 +250,8 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) {
"prime.");
}
if (cfg->anomalous &&
- (cfg->binary_field || cfg->cofactor || cfg->seed_algo || cfg->cm ||
- cfg->invalid || cfg->koblitz)) {
+ (cfg->binary_field || cfg->cofactor || cfg->seed_algo ||
+ cfg->cm || cfg->invalid || cfg->koblitz)) {
argp_failure(
state, 1, 0,
"Anomalous curve generation can not generate "
diff --git a/src/misc/types.c b/src/misc/types.c
index 63d5401..ab1c229 100644
--- a/src/misc/types.c
+++ b/src/misc/types.c
@@ -6,4 +6,6 @@
GENERATOR(gen_skip) { return 1; }
+CHECK(check_skip) { return 1; }
+
UNROLL(unroll_skip) { return -1; }
diff --git a/src/misc/types.h b/src/misc/types.h
index 5b1fd17..b8d63d7 100644
--- a/src/misc/types.h
+++ b/src/misc/types.h
@@ -10,7 +10,7 @@
#include <limits.h>
#include <pari/pari.h>
-#include "misc/config.h"
+#include "config.h"
/**
* @brief
@@ -138,9 +138,19 @@ typedef GENERATOR((*gen_t));
typedef UNROLL((*unroll_t));
/**
+ * @brief A check function type.
+ * @param curve A curve_t being checked
+ * @param cfg An application config
+ * @param args Current optional check argument
+ * @return state diff
+ */
+#define CHECK(check_name) GENERATOR(check_name)
+
+typedef CHECK((*check_t));
+
+/**
* GENERATOR(gen_t)
*
- *
* @param curve A curve_t being generated
* @param cfg An application config
* @param args unused
@@ -149,6 +159,16 @@ typedef UNROLL((*unroll_t));
GENERATOR(gen_skip);
/**
+ * CHECK(check_t)
+ *
+ * @param curve A curve_t being checked
+ * @param cfg An application config
+ * @param args unused
+ * @return
+ */
+CHECK(check_skip);
+
+/**
* UNROLL(unroll_t)
*
* @param curve