aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cm/cm.c9
-rw-r--r--src/cm/p1363.c11
-rw-r--r--src/exhaustive/anomalous.c18
-rw-r--r--src/exhaustive/exhaustive.c12
-rw-r--r--src/exhaustive/seed.c19
-rw-r--r--src/invalid/invalid.c72
-rw-r--r--src/io/input.c10
-rw-r--r--src/io/output.c22
-rw-r--r--src/io/output.h29
-rw-r--r--src/math/arg.c17
-rw-r--r--src/math/curve.c11
-rw-r--r--src/math/point.c21
-rw-r--r--src/math/types.h2
-rw-r--r--src/util/memory.c28
-rw-r--r--src/util/memory.h32
15 files changed, 158 insertions, 155 deletions
diff --git a/src/cm/cm.c b/src/cm/cm.c
index 43ae515..f91cddf 100644
--- a/src/cm/cm.c
+++ b/src/cm/cm.c
@@ -2,14 +2,16 @@
* ecgen, tool for generating Elliptic curve domain parameters
* Copyright (C) 2017 J08nY
*/
-/**
- * @file cm.c
- */
#include "cm.h"
#include "exhaustive/anomalous.h"
+#include "io/output.h"
#include "p1363.h"
int cm_do(config_t *cfg) {
+ debug_log_start("Starting Complex Multiplication method");
+
+ fprintf(stderr, "This is *NOT IMPLEMENTED* currently.\n");
+
GEN D = stoi(71);
form_t **forms;
size_t nforms = p1363_forms(D, &forms);
@@ -18,5 +20,6 @@ int cm_do(config_t *cfg) {
}
p1363_free(&forms, nforms);
+ debug_log_start("Finished Complex Multiplication method");
return 0;
}
diff --git a/src/cm/p1363.c b/src/cm/p1363.c
index 24d28cb..d9fe943 100644
--- a/src/cm/p1363.c
+++ b/src/cm/p1363.c
@@ -2,11 +2,8 @@
* ecgen, tool for generating Elliptic curve domain parameters
* Copyright (C) 2017 J08nY
*/
-/**
- * @file p1363.c
- */
#include "p1363.h"
-#include <pari/paripriv.h>
+#include "util/memory.h"
GEN p1363_group(GEN D) {
pari_sp ltop = avma;
@@ -57,12 +54,10 @@ long p1363_num(GEN group) { return glength(group); }
size_t p1363_forms(GEN D, form_t ***forms) {
GEN group = p1363_group(D);
size_t nforms = (size_t)p1363_num(group);
- *forms = pari_malloc(nforms * sizeof(form_t *));
- memset(*forms, 0, nforms * sizeof(form_t *));
+ *forms = try_calloc(nforms * sizeof(form_t *));
for (size_t i = 0; i < nforms; ++i) {
- (*forms)[i] = pari_malloc(sizeof(form_t));
- memset((*forms)[i], 0, sizeof(form_t));
+ (*forms)[i] = try_calloc(sizeof(form_t));
(*forms)[i]->A = gel(gel(group, i + 1), 1);
(*forms)[i]->B = gel(gel(group, i + 1), 2);
(*forms)[i]->C = gel(gel(group, i + 1), 3);
diff --git a/src/exhaustive/anomalous.c b/src/exhaustive/anomalous.c
index 93adf7d..44f8780 100644
--- a/src/exhaustive/anomalous.c
+++ b/src/exhaustive/anomalous.c
@@ -2,27 +2,15 @@
* ecgen, tool for generating Elliptic curve domain parameters
* Copyright (C) 2017 J08nY
*/
-/**
- * @file anomalous.c
- */
#include "anomalous.h"
-#include <io/config.h>
-#include <math/types.h>
+#include "util/memory.h"
static disc_t **disc_table;
void anomalous_init() {
- disc_table = pari_malloc(sizeof(disc_t *) * 5);
- if (!disc_table) {
- perror("Couldn't malloc.");
- exit(1);
- }
+ disc_table = try_calloc(sizeof(disc_t *) * 5);
for (int i = 0; i < 5; ++i) {
- disc_table[i] = pari_malloc(sizeof(disc_t));
- if (!disc_table[i]) {
- perror("Couldn't malloc.");
- exit(1);
- }
+ disc_table[i] = try_calloc(sizeof(disc_t));
}
/*
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c
index 55d2212..f361a13 100644
--- a/src/exhaustive/exhaustive.c
+++ b/src/exhaustive/exhaustive.c
@@ -3,7 +3,6 @@
* Copyright (C) 2017 J08nY
*/
#include "exhaustive.h"
-#include <math/types.h>
#include "anomalous.h"
#include "io/output.h"
#include "math/curve.h"
@@ -13,6 +12,7 @@
#include "math/order.h"
#include "math/point.h"
#include "seed.h"
+#include "util/memory.h"
static void exhaustive_ginit(gen_t *generators, const config_t *cfg) {
if (cfg->from_seed) {
@@ -93,13 +93,13 @@ static void exhaustive_ainit(arg_t **argss, const config_t *cfg) {
if (cfg->anomalous) {
arg_t *field_arg = arg_new();
arg_t *eq_arg = arg_new();
- size_t *i = pari_malloc(sizeof(size_t));
+ size_t *i = try_calloc(sizeof(size_t));
*i = 3;
field_arg->args = i;
field_arg->nargs = 1;
eq_arg->args = i;
eq_arg->nargs = 1;
- eq_arg->mallocd = i;
+ eq_arg->allocd = i;
argss[OFFSET_FIELD] = field_arg;
argss[OFFSET_B] = eq_arg;
}
@@ -222,7 +222,7 @@ static void exhaustive_quit(arg_t *argss[]) {
}
int exhaustive_do(config_t *cfg) {
- debug("# Starting Exhaustive method\n");
+ debug_log_start("Starting Exhaustive method");
gen_t generators[OFFSET_END] = {NULL};
arg_t *argss[OFFSET_END] = {NULL};
@@ -234,12 +234,15 @@ int exhaustive_do(config_t *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)) {
curve_free(&curve);
return 1;
}
+ debug_log_end("Generated new curve");
+
output_o(curve, cfg);
if (i != cfg->count - 1) {
output_o_separator(cfg);
@@ -249,5 +252,6 @@ int exhaustive_do(config_t *cfg) {
output_o_end(cfg);
exhaustive_quit(argss);
+ debug_log_end("Finished Exhaustive method");
return 0;
}
diff --git a/src/exhaustive/seed.c b/src/exhaustive/seed.c
index d35fc05..44663c8 100644
--- a/src/exhaustive/seed.c
+++ b/src/exhaustive/seed.c
@@ -2,18 +2,11 @@
* ecgen, tool for generating Elliptic curve domain parameters
* Copyright (C) 2017 J08nY
*/
+
#include "seed.h"
-#include "io/input.h"
+#include "util/memory.h"
-seed_t *seed_new(void) {
- seed_t *seed = pari_malloc(sizeof(seed_t));
- if (!seed) {
- perror("Couldn't malloc.");
- exit(1);
- }
- memset(seed, 0, sizeof(seed_t));
- return seed;
-}
+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);
@@ -67,11 +60,7 @@ char *seed_itos(GEN seed) {
long len = glength(bits);
long bytes = (len / 8) + (len % 8 == 0 ? 0 : 1);
- char *result = pari_malloc((size_t)bytes);
- if (!result) {
- perror("Couldn't malloc.");
- exit(1);
- }
+ char *result = try_malloc((size_t)bytes);
for (long i = 0; i < len; ++i) {
// TODO
diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c
index 122bf4f..3859de7 100644
--- a/src/invalid/invalid.c
+++ b/src/invalid/invalid.c
@@ -12,6 +12,7 @@
#include "math/gens.h"
#include "math/order.h"
#include "math/point.h"
+#include "util/memory.h"
static void invalid_original_ginit(gen_t *generators, const config_t *cfg) {
generators[OFFSET_SEED] = &gen_skip;
@@ -53,7 +54,8 @@ static size_t invalid_primes(GEN order, pari_ulong **primes) {
size_t nprimes = 0;
size_t size = 10;
- *primes = pari_malloc(size * sizeof(pari_ulong));
+ *primes = try_calloc(size * sizeof(pari_ulong));
+
while (cmpii(bound, product) >= 0) {
product = mulis(product, last);
(*primes)[nprimes] = unextprime(last + 1);
@@ -61,14 +63,9 @@ static size_t invalid_primes(GEN order, pari_ulong **primes) {
nprimes++;
if (nprimes == size) {
pari_ulong *new_primes =
- pari_realloc(*primes, size * 2 * sizeof(pari_ulong));
- if (new_primes) {
- *primes = new_primes;
- size *= 2;
- } else {
- perror("Couldn't malloc.");
- return 0;
- }
+ try_realloc(*primes, size * 2 * sizeof(pari_ulong));
+ *primes = new_primes;
+ size *= 2;
}
}
// realloc to smaller size, to tightly fit all generated primes
@@ -76,11 +73,7 @@ static size_t invalid_primes(GEN order, pari_ulong **primes) {
pari_realloc(*primes, nprimes * sizeof(pari_ulong));
if (new_primes) {
*primes = new_primes;
- } else {
- perror("Couldn't malloc.");
- return 0;
}
-
avma = ltop;
return nprimes;
@@ -174,7 +167,7 @@ static size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes,
// copy the curve params that stay into a new curve, since this
// curve pointer got assigned
// this is for performance reasons. As it only copies the curve
- // and mallocs memory if this curve is saved.
+ // and allocs memory if this curve is saved.
invalid = curve_new();
invalid->field = gcopy(curve->field);
invalid->a = gcopy(curve->a);
@@ -283,56 +276,50 @@ static size_t invalid_curves_threaded(curve_t *curve, config_t *cfg,
}
int invalid_do(config_t *cfg) {
- debug("# Starting Invalid curve method\n");
+ debug_log_start("Starting Invalid curve method");
- gen_t gen[OFFSET_END];
- arg_t *argss[OFFSET_END];
- unroll_t unrolls[OFFSET_END];
- invalid_original_ginit(gen, cfg);
- exhaustive_uinit(unrolls, cfg);
+ gen_t original_gens[OFFSET_END];
+ arg_t *original_argss[OFFSET_END];
+ unroll_t common_unrolls[OFFSET_END];
+ invalid_original_ginit(original_gens, cfg);
+ exhaustive_uinit(common_unrolls, cfg);
- // create the curve to invalidate
- // Either from input or random with -
+ debug_log_start("Starting to create curve to invalidate");
curve_t *curve = curve_new();
- // actually generate the curve
- if (!exhaustive_gen(curve, cfg, gen, argss, unrolls, OFFSET_FIELD,
- OFFSET_POINTS)) {
+ if (!exhaustive_gen(curve, cfg, original_gens, original_argss,
+ common_unrolls, OFFSET_FIELD, OFFSET_POINTS)) {
curve_free(&curve);
return 1;
}
+ debug_log_end("Finished creating curve to invalidate");
+
output_o_begin(cfg);
output_o(curve, cfg);
output_o_separator(cfg);
- // now, generate primes upto order^2
+ debug_log_start("Starting to generate primes to product over order^2");
pari_ulong *primes;
size_t nprimes = invalid_primes(curve->order, &primes);
- if (cfg->verbose) {
- fprintf(verbose, "primes upto: p_max = %lu, n = %lu\n",
- primes[nprimes - 1], nprimes);
- }
+ verbose_log("primes upto: p_max = %lu, n = %lu\n", primes[nprimes - 1],
+ nprimes);
+ debug_log_end("Generated primes");
- // Alloc enough curves
- curve_t **curves = pari_malloc(nprimes * sizeof(curve_t *));
- if (!(curves)) {
- perror("Couldn't malloc.");
- return 0;
- }
- memset(curves, 0, nprimes * sizeof(curve_t *));
+ curve_t **curves = try_calloc(nprimes * sizeof(curve_t *));
// init the invalid curve gen_t
- gen_t invalid_gen[OFFSET_END];
- invalid_invalid_ginit(invalid_gen, cfg);
+ gen_t invalid_gens[OFFSET_END];
+ invalid_invalid_ginit(invalid_gens, cfg);
- // now, generate the invalid curves for all primes
+ debug_log_start("Starting to generate invalid curves");
size_t ncurves;
if (cfg->threads == 1) {
ncurves = invalid_curves(curve, cfg, primes, nprimes, curves,
- invalid_gen, unrolls);
+ invalid_gens, common_unrolls);
} else {
ncurves = invalid_curves_threaded(curve, cfg, primes, nprimes, curves,
- invalid_gen, unrolls);
+ invalid_gens, common_unrolls);
}
+ debug_log_end("Finished generating invalid curves");
output_o_end(cfg);
for (size_t i = 0; i < ncurves; ++i) {
@@ -342,5 +329,6 @@ int invalid_do(config_t *cfg) {
pari_free(primes);
curve_free(&curve);
+ debug_log_end("Finished Invalid curve method");
return 0;
}
diff --git a/src/io/input.c b/src/io/input.c
index ead0bf2..2b18a53 100644
--- a/src/io/input.c
+++ b/src/io/input.c
@@ -4,8 +4,8 @@
*/
#define _POSIX_C_SOURCE 200809L
#include "input.h"
-#include <parson/parson.h>
#include "output.h"
+#include "util/memory.h"
FILE *in;
int delim;
@@ -29,11 +29,7 @@ static GEN input_i(const char *prompt, unsigned long bits) {
;
if (len <= 3 || !(line[0] == '0' && (line[1] == 'x' || line[1] == 'X'))) {
- char *new_line = realloc(line, (size_t)(len + 2));
- if (!new_line) {
- perror("Couldn't alloc.");
- exit(1);
- }
+ char *new_line = try_realloc(line, (size_t)(len + 2));
memmove(new_line + 2, new_line, (size_t)len);
new_line[0] = '0';
new_line[1] = 'x';
@@ -113,8 +109,6 @@ GEN input_param(param_t param, const char *prompt, unsigned long bits) {
}
void input_init(const config_t *cfg) {
- json_set_allocation_functions(pari_malloc, pari_free);
-
if (cfg->input) {
in = fopen(cfg->input, "r");
delim = ',';
diff --git a/src/io/output.c b/src/io/output.c
index 749d008..dafbf14 100644
--- a/src/io/output.c
+++ b/src/io/output.c
@@ -6,16 +6,13 @@
#include "output.h"
#include <parson/parson.h>
#include "math/field.h"
+#include "util/memory.h"
FILE *out;
FILE *verbose;
char *output_malloc(const char *what) {
- char *s = pari_malloc(sizeof(char) * (strlen(what) + 1));
- if (!s) {
- perror("Couldn't malloc.");
- exit(1);
- }
+ char *s = try_calloc(sizeof(char) * (strlen(what) + 1));
strcpy(s, what);
return s;
}
@@ -62,8 +59,7 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
len += strlen(gens[i]);
}
size_t lenn = sizeof(char) * (len + curve->ngens);
- params[OFFSET_GENERATORS] = pari_malloc(lenn);
- params[OFFSET_GENERATORS][0] = '\0';
+ params[OFFSET_GENERATORS] = try_calloc(lenn);
for (size_t i = 0; i < curve->ngens; ++i) {
if (i > 0) strncat(params[OFFSET_GENERATORS], ",", lenn - 1);
strncat(params[OFFSET_GENERATORS], gens[i], lenn - 1);
@@ -87,8 +83,7 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
len += strlen(points[i]);
}
size_t lenn = sizeof(char) * (len + curve->npoints);
- params[OFFSET_POINTS] = pari_malloc(lenn);
- params[OFFSET_POINTS][0] = '\0';
+ params[OFFSET_POINTS] = try_calloc(lenn);
for (size_t i = 0; i < curve->npoints; ++i) {
if (i > 0) strncat(params[OFFSET_POINTS], ",", lenn - 1);
strncat(params[OFFSET_POINTS], points[i], lenn - 1);
@@ -105,8 +100,7 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
}
}
size_t lenn = sizeof(char) * (len + count);
- char *result = pari_malloc(lenn);
- result[0] = '\0';
+ char *result = try_calloc(lenn);
for (int i = OFFSET_FIELD; i < OFFSET_END; ++i) {
if (params[i]) {
@@ -299,7 +293,7 @@ void output_f_end(FILE *out, const config_t *cfg) {
void output_o_end(const config_t *cfg) { output_f_end(out, cfg); }
void output_init(const config_t *cfg) {
- json_set_allocation_functions(pari_malloc, pari_free);
+ json_set_allocation_functions(try_malloc, pari_free);
if (cfg->output) {
out = fopen(cfg->output, cfg->append ? "a" : "w");
@@ -315,11 +309,11 @@ void output_init(const config_t *cfg) {
if (cfg->verbose_log) {
verbose = fopen(cfg->verbose_log, "w");
if (!verbose) {
- verbose = stdout;
+ verbose = stderr;
perror("Failed to open verbose output file.");
}
} else {
- verbose = stdout;
+ verbose = stderr;
}
setvbuf(verbose, NULL, _IONBF, 0);
diff --git a/src/io/output.h b/src/io/output.h
index ef36f99..5713b37 100644
--- a/src/io/output.h
+++ b/src/io/output.h
@@ -12,6 +12,29 @@
#include <stdbool.h>
#include "math/types.h"
+#ifdef DEBUG
+
+#include "time.h"
+
+#define debug(...) fprintf(verbose, __VA_ARGS__)
+#define debug_log(...) \
+ fprintf(verbose, " - %lu %s\n", time(NULL), __VA_ARGS__);
+#define debug_log_start(...) \
+ fprintf(verbose, "[ ] %lu %s\n", time(NULL), __VA_ARGS__)
+#define debug_log_end(...) \
+ fprintf(verbose, "[*] %lu %s\n", time(NULL), __VA_ARGS__)
+#else
+#define debug(...)
+#define debug_log(...)
+#define debug_log_start(...)
+#define debug_log_end(...)
+#endif // DEBUG
+
+#define verbose_log(...) \
+ if (cfg->verbose) fprintf(verbose, __VA_ARGS__)
+
+#define output_log(...) fprintf(out, __VA_ARGS__)
+
/**
* @brief Output curve to a pari_malloc'ed string in CSV format.
* @param curve
@@ -178,10 +201,4 @@ void output_init(const config_t *cfg);
*/
void output_quit(void);
-#ifdef DEBUG
-#define debug(...) fprintf(out, __VA_ARGS__)
-#else
-#define debug(...)
-#endif
-
#endif // ECGEN_OUTPUT_H
diff --git a/src/math/arg.c b/src/math/arg.c
index d2eba9b..6b11777 100644
--- a/src/math/arg.c
+++ b/src/math/arg.c
@@ -3,22 +3,15 @@
* Copyright (C) 2017 J08nY
*/
#include "arg.h"
+#include "util/memory.h"
-arg_t *arg_new(void) {
- arg_t *arg = pari_malloc(sizeof(arg_t));
- if (!arg) {
- perror("Couldn't malloc.");
- exit(1);
- }
- memset(arg, 0, sizeof(arg_t));
- return arg;
-}
+arg_t *arg_new(void) { return try_calloc(sizeof(arg_t)); }
void arg_free(arg_t **arg) {
if (*arg) {
- if ((*arg)->mallocd) {
- pari_free((*arg)->mallocd);
- (*arg)->mallocd = NULL;
+ if ((*arg)->allocd) {
+ pari_free((*arg)->allocd);
+ (*arg)->allocd = NULL;
}
pari_free(*arg);
*arg = NULL;
diff --git a/src/math/curve.c b/src/math/curve.c
index 969e628..90799bb 100644
--- a/src/math/curve.c
+++ b/src/math/curve.c
@@ -6,16 +6,9 @@
#include "exhaustive/seed.h"
#include "field.h"
#include "point.h"
+#include "util/memory.h"
-curve_t *curve_new(void) {
- curve_t *curve = pari_malloc(sizeof(curve_t));
- if (!curve) {
- perror("Couldn't malloc.");
- exit(1);
- }
- memset(curve, 0, sizeof(curve_t));
- return curve;
-}
+curve_t *curve_new(void) { return try_calloc(sizeof(curve_t)); }
curve_t *curve_copy(const curve_t *src, curve_t *dest) {
if (src->seed) dest->seed = seed_copy(src->seed, dest->seed);
diff --git a/src/math/point.c b/src/math/point.c
index d8a9d8d..877cfe6 100644
--- a/src/math/point.c
+++ b/src/math/point.c
@@ -3,16 +3,9 @@
* Copyright (C) 2017 J08nY
*/
#include "point.h"
+#include "util/memory.h"
-point_t *point_new(void) {
- point_t *point = pari_malloc(sizeof(point_t));
- if (!point) {
- perror("Couldn't malloc.");
- exit(1);
- }
- memset(point, 0, sizeof(point_t));
- return point;
-}
+point_t *point_new(void) { return try_calloc(sizeof(point_t)); }
point_t *point_copy(const point_t *src, point_t *dest) {
if (src->point) dest->point = gcopy(src->point);
@@ -54,15 +47,7 @@ void point_free(point_t **point) {
}
}
-point_t **points_new(size_t num) {
- point_t **points = pari_malloc(num * sizeof(point_t *));
- if (!points) {
- perror("Couldn't malloc.");
- exit(1);
- }
- memset(points, 0, num * sizeof(point_t *));
- return points;
-}
+point_t **points_new(size_t num) { return try_calloc(num * sizeof(point_t *)); }
point_t **points_copy(point_t **const src, point_t **dest, size_t num) {
for (size_t i = 0; i < num; ++i) {
diff --git a/src/math/types.h b/src/math/types.h
index ab4b224..00d78c0 100644
--- a/src/math/types.h
+++ b/src/math/types.h
@@ -80,7 +80,7 @@ typedef enum {
typedef struct {
const void *args;
size_t nargs;
- void *mallocd;
+ void *allocd;
} arg_t;
/**
diff --git a/src/util/memory.c b/src/util/memory.c
new file mode 100644
index 0000000..9724c81
--- /dev/null
+++ b/src/util/memory.c
@@ -0,0 +1,28 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017 J08nY
+ */
+#include "memory.h"
+#include <pari/pari.h>
+
+void *alloc(void *(*fun)(size_t), size_t size) {
+ void *result = fun(size);
+ if (!result) {
+ perror("Couldn't alloc.");
+ exit(1);
+ }
+ return result;
+}
+
+void *try_malloc(size_t size) { return alloc(pari_malloc, size); }
+
+void *try_calloc(size_t size) { return alloc(pari_calloc, size); }
+
+void *try_realloc(void *ptr, size_t size) {
+ void *result = pari_realloc(ptr, size);
+ if (!result) {
+ perror("Couldn't alloc.");
+ exit(1);
+ }
+ return result;
+} \ No newline at end of file
diff --git a/src/util/memory.h b/src/util/memory.h
new file mode 100644
index 0000000..fef237e
--- /dev/null
+++ b/src/util/memory.h
@@ -0,0 +1,32 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017 J08nY
+ */
+#include <stddef.h>
+
+#ifndef ECGEN_MEMORY_H
+#define ECGEN_MEMORY_H
+
+/**
+ * @brief
+ * @param size
+ * @return
+ */
+void *try_malloc(size_t size);
+
+/**
+ * @brief
+ * @param size
+ * @return
+ */
+void *try_calloc(size_t size);
+
+/**
+ * @brief
+ * @param ptr
+ * @param size
+ * @return
+ */
+void *try_realloc(void *ptr, size_t size);
+
+#endif // ECGEN_MEMORY_H