aboutsummaryrefslogtreecommitdiff
path: root/src/math
diff options
context:
space:
mode:
authorJ08nY2017-02-14 03:14:57 +0100
committerJ08nY2017-02-14 03:14:57 +0100
commit0c5ff628d52678bb44b9c595daf1289833d0e532 (patch)
tree913d36f318b30e7984aced1a654d00656d117eb3 /src/math
parent4230a5eb009c92cf7ffe83658e6bf926bccb1400 (diff)
downloadecgen-0c5ff628d52678bb44b9c595daf1289833d0e532.tar.gz
ecgen-0c5ff628d52678bb44b9c595daf1289833d0e532.tar.zst
ecgen-0c5ff628d52678bb44b9c595daf1289833d0e532.zip
Implemented invalid curve generation
Diffstat (limited to 'src/math')
-rw-r--r--src/math/curve.c2
-rw-r--r--src/math/curve.h21
-rw-r--r--src/math/equation.c4
-rw-r--r--src/math/field.c60
-rw-r--r--src/math/order.h2
-rw-r--r--src/math/point.c29
-rw-r--r--src/math/point.h42
-rw-r--r--src/math/random.c2
8 files changed, 134 insertions, 28 deletions
diff --git a/src/math/curve.c b/src/math/curve.c
index e9d1364..57f46e6 100644
--- a/src/math/curve.c
+++ b/src/math/curve.c
@@ -71,7 +71,7 @@ int curve_seed(curve_t *curve, config_t *config, ...) {
return curve_seed_f2m(curve, config);
default:
pari_err_TYPE("curve_seed", curve->field);
- return 0; /* NOT REACHABLE */
+ return INT_MIN; /* NOT REACHABLE */
}
}
diff --git a/src/math/curve.h b/src/math/curve.h
index d1688ff..1a8fb0b 100644
--- a/src/math/curve.h
+++ b/src/math/curve.h
@@ -10,6 +10,8 @@
#include "types.h"
/**
+ * Creates a curve GEN in curve_t curve from field, a and b.
+ * Always succeeds.
*
* @param curve
* @param config
@@ -18,6 +20,8 @@
int curve_init(curve_t *curve, config_t *config, ...);
/**
+ * Creates a curve GEN in curve_t curve from field, a and b.
+ * Succeeds if a curve exists(non-zero discriminant).
*
* @param curve
* @param config
@@ -26,14 +30,9 @@ int curve_init(curve_t *curve, config_t *config, ...);
int curve_nonzero(curve_t *curve, config_t *config, ...);
/**
- *
- * @param curve
- * @param config
- * @return
- */
-int curve_prime(curve_t *curve, config_t *config, ...);
-
-/**
+ * Creates a curve GEN in curve_t curve from field, a and b. Using the ANSI
+ * X9.62 verifiably random algorithm.
+ * Succeeds if a curve exists(non-zero discriminant).
*
* @param curve
* @param config
@@ -43,7 +42,7 @@ int curve_seed(curve_t *curve, config_t *config, ...);
/**
* @param curve
- * @return
+ * @return a t_VEC of curve parameters: field,a,b,order
*/
GEN curve_params(curve_t *curve);
@@ -54,8 +53,8 @@ GEN curve_params(curve_t *curve);
curve_t *curve_new();
/**
- *
- * @param curve
+ * Free a curve_t along with it's seed_t and point_ts.
+ * @param curve to free
*/
void curve_free(curve_t **curve);
diff --git a/src/math/equation.c b/src/math/equation.c
index b699b9a..4e44747 100644
--- a/src/math/equation.c
+++ b/src/math/equation.c
@@ -19,7 +19,7 @@ int a_random(curve_t *curve, config_t *config, ...) {
}
int a_input(curve_t *curve, config_t *config, ...) {
- curve->a = fread_int(in, "a: ", config->bits, '\n');
+ curve->a = fread_int(in, "a:", config->bits);
return 1;
}
@@ -41,7 +41,7 @@ int b_random(curve_t *curve, config_t *config, ...) {
}
int b_input(curve_t *curve, config_t *config, ...) {
- curve->b = fread_int(in, "a: ", config->bits, '\n');
+ curve->b = fread_int(in, "b:", config->bits);
return 1;
}
diff --git a/src/math/field.c b/src/math/field.c
index a4b038e..4d62c73 100644
--- a/src/math/field.c
+++ b/src/math/field.c
@@ -3,6 +3,7 @@
* Copyright (C) 2017 J08nY
*/
#include "field.h"
+#include "io/input.h"
#include "poly.h"
#include "random.h"
@@ -27,21 +28,68 @@ int field_random(curve_t *curve, config_t *config, ...) {
curve->field = field_binaryr(config->bits);
return 1;
default:
- return 0; /* NOT REACHABLE */
+ return INT_MIN; /* NOT REACHABLE */
}
}
int field_input(curve_t *curve, config_t *config, ...) {
- return INT_MIN; // NOT IMPLEMENTED
+ pari_sp ltop = avma;
+ switch (config->field) {
+ case FIELD_PRIME: {
+ GEN p = fread_prime(in, "p:", config->bits);
+ if (equalii(p, gen_m1)) {
+ avma = ltop;
+ return 0;
+ }
+ curve->field = p;
+ return 1;
+ }
+ case FIELD_BINARY: {
+ GEN e1 = fread_short(in, "e1:");
+ if (equalii(e1, gen_m1)) {
+ avma = ltop;
+ return 0;
+ }
+ GEN e2 = fread_short(in, "e2:");
+ if (equalii(e2, gen_m1)) {
+ avma = ltop;
+ return 0;
+ }
+ GEN e3 = fread_short(in, "e3:");
+ if (equalii(e3, gen_m1)) {
+ avma = ltop;
+ return 0;
+ }
+
+ if (isintzero(e1) && isintzero(e2) && isintzero(e3)) {
+ fprintf(stderr, "At least one exponent must be nonzero.\n");
+ avma = ltop;
+ return 0;
+ }
+
+ GEN v = gtovec0(gen_0, config->bits + 1);
+ gel(v, config->bits + 1) = gen_1;
+ if (gsigne(e1) == 1) gel(v, itos(e1) + 1) = gen_1;
+ if (gsigne(e2) == 1) gel(v, itos(e2) + 1) = gen_1;
+ if (gsigne(e3) == 1) gel(v, itos(e3) + 1) = gen_1;
+ gel(v, 1) = gen_1;
+
+ GEN poly = gmul(gtopolyrev(v, -1), gmodulss(1, 2));
+
+ GEN field = gerepilecopy(ltop, ffgen(poly, -1));
+ curve->field = field;
+ return 1;
+ }
+ default:
+ return INT_MIN; /* NOT REACHABLE */
+ }
}
GEN field_params(GEN field) {
pari_sp ltop = avma;
if (typ(field) == t_INT) {
- GEN p3 = cgetg(2, t_VEC);
- gel(p3, 1) = gcopy(field);
- return gerepilecopy(ltop, p3);
+ return gtovec(field);
}
GEN out = gtovec0(gen_0, 3);
@@ -50,7 +98,7 @@ GEN field_params(GEN field) {
long l2 = glength(member_mod(field)) - 2;
{
pari_sp btop = avma;
- for (long i = 0; i <= l2; ++i) {
+ for (long i = l2; i > 0; --i) {
GEN c = polcoeff0(member_mod(field), i, -1);
if (cmpis(c, 0) != 0) {
gel(out, j) = stoi(i);
diff --git a/src/math/order.h b/src/math/order.h
index 4af994a..1f0baa2 100644
--- a/src/math/order.h
+++ b/src/math/order.h
@@ -25,4 +25,4 @@ int order_init(curve_t *curve, config_t *cfg, ...);
*/
int order_prime(curve_t *curve, config_t *cfg, ...);
-#endif //ECGEN_ORDER_H
+#endif // ECGEN_ORDER_H
diff --git a/src/math/point.c b/src/math/point.c
index a5a1c69..ebe6288 100644
--- a/src/math/point.c
+++ b/src/math/point.c
@@ -4,9 +4,28 @@
*/
#include "point.h"
-point_t *gerepile_point(pari_sp ltop, point_t *point) {
- if (point) {
- gerepileall(ltop, 2, &point->point, &point->order);
+point_t *point_new() {}
+
+point_t **points_new(size_t num) {}
+
+void point_free(point_t **point) {}
+
+void points_free(point_t ***point) {}
+
+int point_random(curve_t *curve, config_t *config, ...) {}
+
+int points_random(curve_t *curve, config_t *config, ...) {
+ va_list arg;
+ va_start(arg, config);
+ size_t npoints = va_arg(arg, size_t);
+ va_end(arg);
+
+ curve->points = points_new(npoints);
+ curve->npoints = npoints;
+ for (size_t i = 0; i < npoints; ++i) {
}
- return point;
-} \ No newline at end of file
+}
+
+int points_prime(curve_t *curve, config_t *config, ...) {}
+
+int points_generators(curve_t *curve, config_t *config, ...) {} \ No newline at end of file
diff --git a/src/math/point.h b/src/math/point.h
index 3b69144..064dadc 100644
--- a/src/math/point.h
+++ b/src/math/point.h
@@ -7,7 +7,47 @@
#include <pari/pari.h>
#include "types.h"
+/**
+ *
+ * @return
+ */
+point_t *point_new();
+
+/**
+ *
+ * @param num
+ * @return
+ */
+point_t **points_new(size_t num);
+
+/**
+ *
+ * @param point
+ */
+void point_free(point_t **point);
-point_t *gerepile_point(pari_sp ltop, point_t *point);
+/**
+ *
+ * @param point
+ */
+void points_free(point_t ***point);
+
+/**
+ *
+ * @param curve
+ * @param config
+ * @param ...
+ * @return
+ */
+int point_random(curve_t *curve, config_t *config, ...);
+
+/**
+ *
+ * @param curve
+ * @param config
+ * @param ...
+ * @return
+ */
+int points_random(curve_t *curve, config_t *config, ...);
#endif // ECGEN_POINT_H
diff --git a/src/math/random.c b/src/math/random.c
index 319ed3c..37fd882 100644
--- a/src/math/random.c
+++ b/src/math/random.c
@@ -10,7 +10,7 @@ bool random_init() {
// Try urandom first
FILE *rand = fopen("/dev/urandom", "rb");
if (rand) {
- fread(&seed, sizeof(char), sizeof(pari_ulong), rand);
+ fread(&seed, 1, sizeof(pari_ulong), rand);
fclose(rand);
}
// Try worse methods later