summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJ08nY2017-04-07 02:45:32 +0200
committerJ08nY2017-04-07 02:45:32 +0200
commit7f4596f2735d6011f7413edd7211b8673ecb4a14 (patch)
treed2c745ee2bb615933a9efde7476ef258319609db /src
parent59b338cf8734f89c04042f217fcccf8509c3a197 (diff)
downloadecgen-7f4596f2735d6011f7413edd7211b8673ecb4a14.tar.gz
ecgen-7f4596f2735d6011f7413edd7211b8673ecb4a14.tar.zst
ecgen-7f4596f2735d6011f7413edd7211b8673ecb4a14.zip
Fix binary field curve generation, move config into config.h
Diffstat (limited to 'src')
-rw-r--r--src/cm/cm.c1
-rw-r--r--src/cm/cm.h1
-rw-r--r--src/ecgen.c3
-rw-r--r--src/exhaustive/exhaustive.c1
-rw-r--r--src/exhaustive/seed.c1
-rw-r--r--src/invalid/invalid.c2
-rw-r--r--src/invalid/invalid.h1
-rw-r--r--src/io/cli.c1
-rw-r--r--src/io/cli.h42
-rw-r--r--src/io/config.h55
-rw-r--r--src/io/input.c4
-rw-r--r--src/math/equation.c18
-rw-r--r--src/math/field.c19
-rw-r--r--src/math/field.h8
-rw-r--r--src/math/types.h3
15 files changed, 107 insertions, 53 deletions
diff --git a/src/cm/cm.c b/src/cm/cm.c
index 92d1c66..c1e3393 100644
--- a/src/cm/cm.c
+++ b/src/cm/cm.c
@@ -3,5 +3,6 @@
* Copyright (C) 2017 J08nY
*/
#include "cm.h"
+#include "io/config.h"
int cm_do(config_t *cfg) { return INT_MIN; }
diff --git a/src/cm/cm.h b/src/cm/cm.h
index 14d4fea..29632fc 100644
--- a/src/cm/cm.h
+++ b/src/cm/cm.h
@@ -6,6 +6,7 @@
* @file cm.h
*/
#include "io/cli.h"
+#include "io/config.h"
#ifndef ECGEN_CM_H
#define ECGEN_CM_H
diff --git a/src/ecgen.c b/src/ecgen.c
index 9b978a0..94f731b 100644
--- a/src/ecgen.c
+++ b/src/ecgen.c
@@ -24,10 +24,11 @@
* @copyright GPL v2.0
*/
#include <pari/pari.h>
-#include "io/cli.h"
#include "cm/cm.h"
#include "exhaustive/exhaustive.h"
#include "invalid/invalid.h"
+#include "io/cli.h"
+#include "io/config.h"
#include "io/input.h"
#include "io/output.h"
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c
index 969c2ff..7884ca0 100644
--- a/src/exhaustive/exhaustive.c
+++ b/src/exhaustive/exhaustive.c
@@ -3,6 +3,7 @@
* Copyright (C) 2017 J08nY
*/
#include "exhaustive.h"
+#include "io/config.h"
#include "io/output.h"
#include "math/arg.h"
#include "math/curve.h"
diff --git a/src/exhaustive/seed.c b/src/exhaustive/seed.c
index 85fab98..1a2a731 100644
--- a/src/exhaustive/seed.c
+++ b/src/exhaustive/seed.c
@@ -3,6 +3,7 @@
* Copyright (C) 2017 J08nY
*/
#include "seed.h"
+#include "io/config.h"
#include "io/input.h"
seed_t *seed_new(void) {
diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c
index f029337..b495958 100644
--- a/src/invalid/invalid.c
+++ b/src/invalid/invalid.c
@@ -185,7 +185,7 @@ 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
+ obj_free(invalid->curve); // necessary to free the ellinit
avma = btop;
}
}
diff --git a/src/invalid/invalid.h b/src/invalid/invalid.h
index d8090c3..e98f6e4 100644
--- a/src/invalid/invalid.h
+++ b/src/invalid/invalid.h
@@ -6,6 +6,7 @@
* @file invalid.h
*/
#include "io/cli.h"
+#include "io/config.h"
#ifndef ECGEN_INVALID_H
#define ECGEN_INVALID_H
diff --git a/src/io/cli.c b/src/io/cli.c
index 4e4a7f9..3c74c1e 100644
--- a/src/io/cli.c
+++ b/src/io/cli.c
@@ -4,6 +4,7 @@
*/
#include "cli.h"
#include <string.h>
+#include "io/config.h"
char doc[] =
"ecgen, tool for generating Elliptic curve domain parameters.\v(C) 2017 "
diff --git a/src/io/cli.h b/src/io/cli.h
index 9960aca..82dcf4b 100644
--- a/src/io/cli.h
+++ b/src/io/cli.h
@@ -9,54 +9,12 @@
#define ECGEN_CLI_H
#include <argp.h>
-#include <stdbool.h>
#include <stdlib.h>
extern char doc[];
extern char args_doc[];
extern struct argp_option options[];
-enum field_e { FIELD_PRIME, FIELD_BINARY };
-enum format_e { FORMAT_JSON, FORMAT_CSV };
-enum points_e { POINTS_PRIME, POINTS_RANDOM, POINTS_NONE };
-struct points_s {
- enum points_e type;
- size_t amount;
-};
-
-typedef struct{
- enum field_e field;
- bool binary_field;
- bool prime_field;
-
- unsigned long count;
- bool random;
- bool prime;
- bool invalid;
- bool cm;
- char *order;
- bool koblitz;
- bool cofactor;
- long cofactor_bound;
- bool from_seed;
- char *seed;
- bool unique;
- struct points_s points;
-
- char *datadir;
- unsigned long memory;
- enum format_e format;
- char *output;
- char *input;
- bool append;
-
- long verbose;
- char *debug;
-
- long bits;
-
-} config_t;
-
error_t cli_parse(int key, char *arg, struct argp_state *state);
char *cli_filter(int key, const char *text, void *input);
diff --git a/src/io/config.h b/src/io/config.h
new file mode 100644
index 0000000..8e9dc02
--- /dev/null
+++ b/src/io/config.h
@@ -0,0 +1,55 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017 J08nY
+ */
+/**
+ * @file config.h
+ */
+#ifndef ECGEN_CONFIG_H
+#define ECGEN_CONFIG_H
+
+#include <stdbool.h>
+#include <stddef.h>
+
+enum field_e { FIELD_PRIME, FIELD_BINARY };
+enum format_e { FORMAT_JSON, FORMAT_CSV };
+enum points_e { POINTS_PRIME, POINTS_RANDOM, POINTS_NONE };
+struct points_s {
+ enum points_e type;
+ size_t amount;
+};
+
+typedef struct {
+ enum field_e field;
+ bool binary_field;
+ bool prime_field;
+
+ unsigned long count;
+ bool random;
+ bool prime;
+ bool invalid;
+ bool cm;
+ char *order;
+ bool koblitz;
+ bool cofactor;
+ long cofactor_bound;
+ bool from_seed;
+ char *seed;
+ bool unique;
+ struct points_s points;
+
+ char *datadir;
+ unsigned long memory;
+ enum format_e format;
+ char *output;
+ char *input;
+ bool append;
+
+ long verbose;
+ char *debug;
+
+ long bits;
+
+} config_t;
+
+#endif // ECGEN_CONFIG_H
diff --git a/src/io/input.c b/src/io/input.c
index 462ad1b..2d5a1e9 100644
--- a/src/io/input.c
+++ b/src/io/input.c
@@ -117,10 +117,8 @@ void input_init(config_t *cfg) {
in = fopen(cfg->input, "r");
delim = ',';
if (!in) {
- // fallback to stdin or quit?
- in = stdin;
- delim = '\n';
perror("Failed to open input file.");
+ exit(1);
}
} else {
in = stdin;
diff --git a/src/math/equation.c b/src/math/equation.c
index d676350..d60a8cd 100644
--- a/src/math/equation.c
+++ b/src/math/equation.c
@@ -3,7 +3,9 @@
* Copyright (C) 2017 J08nY
*/
#include "equation.h"
+#include "io/cli.h"
#include "io/input.h"
+#include "math/field.h"
int a_random(curve_t *curve, config_t *cfg, arg_t *args) {
curve->a = genrand(curve->field);
@@ -17,8 +19,12 @@ int a_input(curve_t *curve, config_t *cfg, arg_t *args) {
avma = ltop;
return 0;
}
- curve->a = inp;
- // TODO change a to a field element here?. a t_INTMOD or a t_FFELT.
+ GEN elem = field_ielement(curve->field, inp);
+ if (!elem) {
+ avma = ltop;
+ return 0;
+ }
+ curve->a = elem;
return 1;
}
@@ -68,8 +74,12 @@ int b_input(curve_t *curve, config_t *cfg, arg_t *args) {
avma = ltop;
return 0;
}
- curve->b = inp;
- // TODO change b to a field element here?. a t_INTMOD or a t_FFELT.
+ GEN elem = field_ielement(curve->field, inp);
+ if (!elem) {
+ avma = ltop;
+ return 0;
+ }
+ curve->b = elem;
return 1;
}
diff --git a/src/math/field.c b/src/math/field.c
index f717bf8..5f7bea3 100644
--- a/src/math/field.c
+++ b/src/math/field.c
@@ -2,11 +2,12 @@
* ecgen, tool for generating Elliptic curve domain parameters
* Copyright (C) 2017 J08nY
*/
-#include <io/cli.h>
#include "field.h"
+#include <io/cli.h>
#include "io/input.h"
#include "poly.h"
#include "random.h"
+#include "types.h"
static GEN field_primer(long bits) { return random_prime(bits); }
@@ -158,3 +159,19 @@ GEN field_elementi(GEN element) {
return NULL; /* NOT REACHABLE */
}
}
+
+GEN field_ielement(GEN field, GEN in) {
+ switch (typ(field)) {
+ case t_INT:
+ return Fp_to_mod(in, field);
+ case t_FFELT: {
+ pari_sp ltop = avma;
+ GEN coeffs = digits(in, gen_2);
+ GEN poly = gtopoly(coeffs, -1);
+ return gerepilecopy(ltop, Fq_to_FF(poly, field));
+ }
+ default:
+ pari_err_TYPE("field_ielement, field is unknown type. ", field);
+ return gen_m1; /* NOT REACHABLE */
+ }
+}
diff --git a/src/math/field.h b/src/math/field.h
index 3ed300c..66c8920 100644
--- a/src/math/field.h
+++ b/src/math/field.h
@@ -67,4 +67,12 @@ GEN field_params(GEN field);
*/
GEN field_elementi(GEN element);
+/**
+ *
+ * @param field
+ * @param in
+ * @return
+ */
+GEN field_ielement(GEN field, GEN in);
+
#endif // ECGEN_FIELD_H
diff --git a/src/math/types.h b/src/math/types.h
index 4eb2bd4..a726021 100644
--- a/src/math/types.h
+++ b/src/math/types.h
@@ -10,6 +10,7 @@
#include <pari/pari.h>
#include "io/cli.h"
+#include "io/config.h"
typedef struct { GEN seed; } seed_t;
@@ -19,7 +20,7 @@ typedef struct {
GEN cofactor;
} point_t;
-typedef struct {
+typedef struct {
seed_t *seed;
GEN field;
GEN a;