aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJ08nY2017-08-30 23:57:19 +0200
committerJ08nY2017-08-30 23:57:19 +0200
commitd0f496ae3f0a014ad74f83ad5208b1b5bd9a42f1 (patch)
tree9fafcd840478e5fe03cb84ffb81ed7500c143929 /src
parentb3e37b24c247379712d03076bd583e8155e7e27c (diff)
downloadecgen-d0f496ae3f0a014ad74f83ad5208b1b5bd9a42f1.tar.gz
ecgen-d0f496ae3f0a014ad74f83ad5208b1b5bd9a42f1.tar.zst
ecgen-d0f496ae3f0a014ad74f83ad5208b1b5bd9a42f1.zip
Diffstat (limited to 'src')
-rw-r--r--src/exhaustive/exhaustive.c3
-rw-r--r--src/gen/field.c3
-rw-r--r--src/gen/seed.c51
-rw-r--r--src/io/input.c16
-rw-r--r--src/io/output.c41
-rw-r--r--src/util/memory.c13
-rw-r--r--src/util/memory.h4
7 files changed, 64 insertions, 67 deletions
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c
index 6b77f3a..3553efb 100644
--- a/src/exhaustive/exhaustive.c
+++ b/src/exhaustive/exhaustive.c
@@ -176,8 +176,7 @@ int exhaustive_gen_retry(curve_t *curve, const config_t *cfg,
if (diff <= 0) {
if (diff == INT_MIN || state + diff < 0) {
- fprintf(err, "Error generating a curve. state = %i\n",
- state);
+ fprintf(err, "Error generating a curve. state = %i\n", state);
return 0;
}
// record try
diff --git a/src/gen/field.c b/src/gen/field.c
index 482bcc5..9a908cc 100644
--- a/src/gen/field.c
+++ b/src/gen/field.c
@@ -13,7 +13,8 @@ static GEN field_binaryr(unsigned long bits) {
if (poly_exists(bits)) {
return poly_find_gen(bits);
} else {
- fprintf(err, "Unable to find a suitable binary field. Use an explicit one.");
+ fprintf(err,
+ "Unable to find a suitable binary field. Use an explicit one.");
exit(1);
}
}
diff --git a/src/gen/seed.c b/src/gen/seed.c
index 505493a..49a33ff 100644
--- a/src/gen/seed.c
+++ b/src/gen/seed.c
@@ -4,6 +4,7 @@
*/
#include "seed.h"
+#include <io/config.h>
#include "io/output.h"
#include "util/memory.h"
@@ -41,33 +42,34 @@ void seed_free(seed_t **seed) {
static GEN seed_stoi(const char *cstr) {
pari_sp ltop = avma;
- GEN seed = gen_0;
size_t len = strlen(cstr);
- for (size_t i = 0; i < len; ++i) {
- pari_sp btop = avma;
- GEN s = stoi(cstr[i]);
- s = shifti(s, (len - i - 1) * 8);
- seed = addii(seed, s);
- gerepileall(btop, 1, &seed);
+ char *seed_str;
+ if (len <= 3 || !(cstr[0] == '0' && (cstr[1] == 'x' || cstr[1] == 'X'))) {
+ seed_str = try_malloc((size_t)(len + 3));
+ strncpy(seed_str + 2, cstr, len);
+ seed_str[0] = '0';
+ seed_str[1] = 'x';
+ seed_str[len + 2] = 0;
+ } else {
+ seed_str = try_malloc(len + 1);
+ strncpy(seed_str, cstr, len);
}
+ GEN i = strtoi(seed_str);
- return gerepilecopy(ltop, seed);
+ return gerepilecopy(ltop, i);
}
static char *seed_itos(GEN seed) {
pari_sp ltop = avma;
- GEN bits = binary_zv(seed);
+ char *result = pari_sprintf("%Px", seed);
- long len = glength(bits);
- long bytes = (len / 8) + (len % 8 == 0 ? 0 : 1);
- char *result = try_malloc((size_t)bytes);
+ size_t seed_len = strlen(result);
+ char *seed_str = try_malloc(seed_len + 1);
+ strcpy(seed_str, result);
- for (long i = 0; i < len; ++i) {
- // TODO
- }
avma = ltop;
- return result;
+ return seed_str;
}
GENERATOR(seed_gen_random) {
@@ -81,8 +83,13 @@ GENERATOR(seed_gen_random) {
GENERATOR(seed_gen_argument) {
curve->seed = seed_new();
curve->seed->seed = seed_stoi(cfg->seed);
- curve->seed->raw = cfg->seed;
- curve->seed->raw_len = strlen(cfg->seed);
+
+ size_t seed_len = strlen(cfg->seed);
+ char *seed = try_malloc(seed_len + 1);
+ strcpy(seed, cfg->seed);
+
+ curve->seed->raw = seed;
+ curve->seed->raw_len = seed_len;
return 1;
}
@@ -96,10 +103,16 @@ GENERATOR(seed_gen_input) {
avma = ltop;
return 0;
}
-
GEN seed = seed_stoi(cstr);
curve->seed = seed_new();
curve->seed->seed = gerepilecopy(ltop, seed);
+
+ size_t seed_len = strlen(cstr);
+ char *seed_str = try_malloc(seed_len + 1);
+ strcpy(seed_str, cfg->seed);
+
+ curve->seed->raw = seed_str;
+ curve->seed->raw_len = seed_len;
return 1;
}
diff --git a/src/io/input.c b/src/io/input.c
index 2c0398a..f86afe4 100644
--- a/src/io/input.c
+++ b/src/io/input.c
@@ -26,20 +26,8 @@ static GEN input_i(const char *prompt, unsigned long bits) {
free(line);
return gen_m1;
}
- for (size_t i = 0, j = 0; (line[j] = line[i]); j += !isspace(line[i++]));
-
-/* if (len <= 3 || !(line[0] == '0' && (line[1] == 'x' || line[1] == 'X'))) {
- 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';
- if (!feof(in)) {
- new_line[len + 1] = 0;
- } else {
- new_line[len + 2] = 0;
- }
- line = new_line;
- }*/
+ for (size_t i = 0, j = 0; (line[j] = line[i]); j += !isspace(line[i++]))
+ ;
pari_sp ltop = avma;
GEN in = strtoi(line);
diff --git a/src/io/output.c b/src/io/output.c
index 2d05bec..4cabafd 100644
--- a/src/io/output.c
+++ b/src/io/output.c
@@ -29,23 +29,23 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
switch (cfg->field) {
case FIELD_PRIME:
params[OFFSET_FIELD] =
- pari_sprintf("%P0#*x", cfg->hex_digits, curve->field);
+ pari_sprintf("%P0#*x", cfg->hex_digits, curve->field);
break;
case FIELD_BINARY: {
GEN field = field_params(curve->field);
params[OFFSET_FIELD] =
- pari_sprintf("%P#x,%P#x,%P#x,%P#x", gel(field, 1),
- gel(field, 2), gel(field, 3), gel(field, 4));
+ pari_sprintf("%P#x,%P#x,%P#x,%P#x", gel(field, 1),
+ gel(field, 2), gel(field, 3), gel(field, 4));
break;
}
}
if (curve->a)
params[OFFSET_A] =
- pari_sprintf("%P0#*x", cfg->hex_digits, field_elementi(curve->a));
+ pari_sprintf("%P0#*x", cfg->hex_digits, field_elementi(curve->a));
if (curve->b)
params[OFFSET_B] =
- pari_sprintf("%P0#*x", cfg->hex_digits, field_elementi(curve->b));
+ pari_sprintf("%P0#*x", cfg->hex_digits, field_elementi(curve->b));
if (curve->generators) {
char *gens[curve->ngens];
@@ -55,8 +55,8 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
GEN x = field_elementi(gel(generator->point, 1));
GEN y = field_elementi(gel(generator->point, 2));
gens[i] = pari_sprintf("%P0#*x,%P0#*x,%P#x,%P#x", cfg->hex_digits,
- x, cfg->hex_digits, y, generator->order,
- generator->cofactor);
+ x, cfg->hex_digits, y, generator->order,
+ generator->cofactor);
len += strlen(gens[i]);
}
size_t lenn = sizeof(char) * (len + curve->ngens);
@@ -70,7 +70,7 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
if (curve->order)
params[OFFSET_ORDER] =
- pari_sprintf("%P0#*x", cfg->hex_digits, curve->order);
+ pari_sprintf("%P0#*x", cfg->hex_digits, curve->order);
if (curve->points) {
char *points[curve->npoints];
@@ -80,7 +80,7 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
GEN x = field_elementi(gel(point->point, 1));
GEN y = field_elementi(gel(point->point, 2));
points[i] = pari_sprintf("%P0#*x,%P0#*x,%P#x", cfg->hex_digits, x,
- cfg->hex_digits, y, point->order);
+ cfg->hex_digits, y, point->order);
len += strlen(points[i]);
}
size_t lenn = sizeof(char) * (len + curve->npoints);
@@ -150,7 +150,8 @@ static JSON_Value *output_jjson(curve_t *curve, const config_t *cfg) {
pari_free(e3);
break;
}
- default: fprintf(err, "Error, field has unknown amount of elements.\n");
+ default:
+ fprintf(err, "Error, field has unknown amount of elements.\n");
exit(1);
}
@@ -172,13 +173,13 @@ static JSON_Value *output_jjson(curve_t *curve, const config_t *cfg) {
JSON_Object *point_object = json_value_get_object(point_value);
char *x = pari_sprintf(
- "%P0#*x", cfg->hex_digits,
- field_elementi(gel(curve->generators[i]->point, 1)));
+ "%P0#*x", cfg->hex_digits,
+ field_elementi(gel(curve->generators[i]->point, 1)));
json_object_set_string(point_object, "x", x);
pari_free(x);
char *y = pari_sprintf(
- "%P0#*x", cfg->hex_digits,
- field_elementi(gel(curve->generators[i]->point, 2)));
+ "%P0#*x", cfg->hex_digits,
+ field_elementi(gel(curve->generators[i]->point, 2)));
json_object_set_string(point_object, "y", y);
pari_free(y);
char *p_order = pari_sprintf("%P#x", curve->generators[i]->order);
@@ -186,7 +187,7 @@ static JSON_Value *output_jjson(curve_t *curve, const config_t *cfg) {
pari_free(p_order);
if (curve->generators[i]->cofactor) {
char *cofactor =
- pari_sprintf("%P#x", curve->generators[i]->cofactor);
+ pari_sprintf("%P#x", curve->generators[i]->cofactor);
json_object_set_string(point_object, "cofactor", cofactor);
pari_free(cofactor);
}
@@ -206,13 +207,13 @@ static JSON_Value *output_jjson(curve_t *curve, const config_t *cfg) {
JSON_Object *point_object = json_value_get_object(point_value);
char *x =
- pari_sprintf("%P0#*x", cfg->hex_digits,
- field_elementi(gel(curve->points[i]->point, 1)));
+ pari_sprintf("%P0#*x", cfg->hex_digits,
+ field_elementi(gel(curve->points[i]->point, 1)));
json_object_set_string(point_object, "x", x);
pari_free(x);
char *y =
- pari_sprintf("%P0#*x", cfg->hex_digits,
- field_elementi(gel(curve->points[i]->point, 2)));
+ pari_sprintf("%P0#*x", cfg->hex_digits,
+ field_elementi(gel(curve->points[i]->point, 2)));
json_object_set_string(point_object, "y", y);
pari_free(y);
char *p_order = pari_sprintf("%P#x", curve->points[i]->order);
@@ -220,7 +221,7 @@ static JSON_Value *output_jjson(curve_t *curve, const config_t *cfg) {
pari_free(p_order);
if (curve->points[i]->cofactor) {
char *cofactor =
- pari_sprintf("%P#x", curve->points[i]->cofactor);
+ pari_sprintf("%P#x", curve->points[i]->cofactor);
json_object_set_string(point_object, "cofactor", cofactor);
pari_free(cofactor);
}
diff --git a/src/util/memory.c b/src/util/memory.c
index 53f68c9..b506011 100644
--- a/src/util/memory.c
+++ b/src/util/memory.c
@@ -5,7 +5,6 @@
#include "memory.h"
#include <pari/pari.h>
-
static void *(*malloc_func)(size_t) = pari_malloc;
static void *(*calloc_func)(size_t) = pari_calloc;
@@ -25,9 +24,7 @@ void *alloc(void *(*fun)(size_t), size_t size) {
void *try_malloc(size_t size) { return alloc(malloc_func, size); }
-void *try_calloc(size_t size) {
- return alloc(calloc_func, size);
-}
+void *try_calloc(size_t size) { return alloc(calloc_func, size); }
void *try_realloc(void *ptr, size_t size) {
void *result = realloc_func(ptr, size);
@@ -38,13 +35,11 @@ void *try_realloc(void *ptr, size_t size) {
return result;
}
-void try_free(void *ptr) {
- free_func(ptr);
-}
+void try_free(void *ptr) { free_func(ptr); }
void set_mem_funcs(void *(*malloc_fun)(size_t), void *(*calloc_fun)(size_t),
- void *(*realloc_fun)(void *, size_t),
- void(*free_fun)(void *)) {
+ void *(*realloc_fun)(void *, size_t),
+ void (*free_fun)(void *)) {
malloc_func = malloc_fun;
calloc_func = calloc_fun;
realloc_func = realloc_fun;
diff --git a/src/util/memory.h b/src/util/memory.h
index 7070430..408052f 100644
--- a/src/util/memory.h
+++ b/src/util/memory.h
@@ -46,7 +46,7 @@ void try_free(void *ptr);
* @param free_fun
*/
void set_mem_funcs(void *(*malloc_fun)(size_t), void *(*calloc_fun)(size_t),
- void *(*realloc_fun)(void *, size_t),
- void(*free_fun)(void *));
+ void *(*realloc_fun)(void *, size_t),
+ void (*free_fun)(void *));
#endif // ECGEN_MEMORY_H