summaryrefslogtreecommitdiff
path: root/src/math
diff options
context:
space:
mode:
authorJ08nY2017-04-09 18:19:25 +0200
committerJ08nY2017-04-09 18:19:25 +0200
commit1834586a6850f3c93107271a0351accde78981fd (patch)
tree30a7b83ef6e289dda3beede9d5d5a8a3aac197c2 /src/math
parent2c9a14b7323749af884279a564dabd710e089dcc (diff)
downloadecgen-1834586a6850f3c93107271a0351accde78981fd.tar.gz
ecgen-1834586a6850f3c93107271a0351accde78981fd.tar.zst
ecgen-1834586a6850f3c93107271a0351accde78981fd.zip
Added multi-threading support to invalid curve generation
Invalid curve generation now supports multi-threading via pthreads, use the --threads= option. - Changed some function params to const, where possible. - Added *_new_copy, *_clone and *_new_clone functions to curve_t*, point_t* and point_t** - Made cfg->bits unsigned long - Made order_any gcopy the generated order(as it can be a clone and screw up stuff later)
Diffstat (limited to 'src/math')
-rw-r--r--src/math/arg.c2
-rw-r--r--src/math/arg.h2
-rw-r--r--src/math/curve.c68
-rw-r--r--src/math/curve.h32
-rw-r--r--src/math/equation.c25
-rw-r--r--src/math/equation.h24
-rw-r--r--src/math/field.c13
-rw-r--r--src/math/field.h6
-rw-r--r--src/math/gens.c4
-rw-r--r--src/math/gens.h4
-rw-r--r--src/math/order.c13
-rw-r--r--src/math/order.h6
-rw-r--r--src/math/point.c57
-rw-r--r--src/math/point.h57
-rw-r--r--src/math/poly.c8
-rw-r--r--src/math/poly.h8
-rw-r--r--src/math/random.c4
-rw-r--r--src/math/random.h4
-rw-r--r--src/math/types.c2
-rw-r--r--src/math/types.h8
20 files changed, 252 insertions, 95 deletions
diff --git a/src/math/arg.c b/src/math/arg.c
index 991eebb..e313adb 100644
--- a/src/math/arg.c
+++ b/src/math/arg.c
@@ -4,7 +4,7 @@
*/
#include "arg.h"
-arg_t *arg_new() {
+arg_t *arg_new(void) {
arg_t *arg = pari_malloc(sizeof(arg_t));
if (!arg) {
perror("Couldn't malloc.");
diff --git a/src/math/arg.h b/src/math/arg.h
index a7859dd..c0f2f6f 100644
--- a/src/math/arg.h
+++ b/src/math/arg.h
@@ -14,7 +14,7 @@
* @brief
* @return
*/
-arg_t *arg_new();
+arg_t *arg_new(void);
/**
* @brief
diff --git a/src/math/curve.c b/src/math/curve.c
index 7b74556..0d4cd12 100644
--- a/src/math/curve.c
+++ b/src/math/curve.c
@@ -18,7 +18,7 @@ curve_t *curve_new(void) {
return curve;
}
-curve_t *curve_copy(curve_t *src, curve_t *dest) {
+curve_t *curve_copy(const curve_t *src, curve_t *dest) {
if (src->seed) dest->seed = seed_copy(src->seed, dest->seed);
if (src->field) dest->field = gcopy(src->field);
if (src->a) dest->a = gcopy(src->a);
@@ -26,25 +26,69 @@ curve_t *curve_copy(curve_t *src, curve_t *dest) {
if (src->curve) dest->curve = gcopy(src->curve);
if (src->order) dest->order = gcopy(src->order);
if (src->generators) {
- dest->generators = points_new(src->ngens);
- dest->generators =
- points_copy(src->generators, dest->generators, src->ngens);
+ dest->generators = points_new_copy(src->generators, src->ngens);
dest->ngens = src->ngens;
}
if (src->points) {
- dest->points = points_new(src->npoints);
- dest->points = points_copy(src->points, dest->points, src->npoints);
+ dest->points = points_new_copy(src->points, src->npoints);
dest->npoints = src->npoints;
}
return dest;
}
+curve_t *curve_new_copy(const curve_t *src) {
+ curve_t *result = curve_new();
+ return curve_copy(src, result);
+}
+
+curve_t *curve_clone(const curve_t *src, curve_t *dest) {
+ if (src->seed) dest->seed = seed_clone(src->seed, dest->seed);
+ if (src->field) dest->field = gclone(src->field);
+ if (src->a) dest->a = gclone(src->a);
+ if (src->b) dest->b = gclone(src->b);
+ if (src->curve) dest->curve = gclone(src->curve);
+ if (src->order) dest->order = gclone(src->order);
+ if (src->generators) {
+ dest->generators = points_new_clone(src->generators, src->ngens);
+ dest->ngens = src->ngens;
+ }
+ if (src->points) {
+ dest->points = points_new_clone(src->points, src->npoints);
+ dest->npoints = src->npoints;
+ }
+ return dest;
+}
+
+curve_t *curve_new_clone(const curve_t *src) {
+ curve_t *result = curve_new();
+ return curve_clone(src, result);
+}
+
void curve_free(curve_t **curve) {
if (*curve) {
seed_free(&(*curve)->seed);
+
if ((*curve)->curve) {
+ // TODO, this is possibly dangerous...
obj_free((*curve)->curve);
+ if (isclone((*curve)->curve)) {
+ gunclone((*curve)->curve);
+ }
+ }
+
+ if ((*curve)->field && isclone((*curve)->field)) {
+ gunclone((*curve)->field);
+ }
+ if ((*curve)->a && isclone((*curve)->a)) {
+ gunclone((*curve)->a);
+ }
+ if ((*curve)->b && isclone((*curve)->b)) {
+ gunclone((*curve)->b);
}
+ if ((*curve)->order && isclone((*curve)->order)) {
+ gunclone((*curve)->order);
+ }
+
points_free_deep(&(*curve)->generators, (*curve)->ngens);
points_free_deep(&(*curve)->points, (*curve)->npoints);
pari_free(*curve);
@@ -52,7 +96,7 @@ void curve_free(curve_t **curve) {
}
}
-int curve_any(curve_t *curve, config_t *cfg, arg_t *args) {
+int curve_any(curve_t *curve, const config_t *cfg, arg_t *args) {
pari_sp ltop = avma;
GEN v = gen_0;
switch (typ(curve->field)) {
@@ -75,7 +119,7 @@ int curve_any(curve_t *curve, config_t *cfg, arg_t *args) {
return 1;
}
-int curve_nonzero(curve_t *curve, config_t *cfg, arg_t *args) {
+int curve_nonzero(curve_t *curve, const config_t *cfg, arg_t *args) {
pari_sp ltop = avma;
curve_any(curve, cfg, args);
if (gequal0(ell_get_disc(curve->curve))) {
@@ -86,17 +130,17 @@ int curve_nonzero(curve_t *curve, config_t *cfg, arg_t *args) {
}
}
-static int curve_seed_fp(curve_t *curve, config_t *cfg, arg_t *args) {
+static int curve_seed_fp(curve_t *curve, const config_t *cfg, arg_t *args) {
// TODO implement
return INT_MIN;
}
-static int curve_seed_f2m(curve_t *curve, config_t *cfg, arg_t *args) {
+static int curve_seed_f2m(curve_t *curve, const config_t *cfg, arg_t *args) {
// TODO implement
return INT_MIN;
}
-int curve_seed(curve_t *curve, config_t *cfg, arg_t *args) {
+int curve_seed(curve_t *curve, const config_t *cfg, arg_t *args) {
switch (typ(curve->field)) {
case t_INT:
return curve_seed_fp(curve, cfg, args);
@@ -108,7 +152,7 @@ int curve_seed(curve_t *curve, config_t *cfg, arg_t *args) {
}
}
-GEN curve_params(curve_t *curve) {
+GEN curve_params(const curve_t *curve) {
pari_sp ltop = avma;
GEN result = field_params(curve->field);
diff --git a/src/math/curve.h b/src/math/curve.h
index 25e4f51..65dc4b7 100644
--- a/src/math/curve.h
+++ b/src/math/curve.h
@@ -22,7 +22,7 @@
* @param args unused
* @return state diff
*/
-int curve_any(curve_t *curve, config_t *cfg, arg_t *args);
+int curve_any(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -34,7 +34,7 @@ int curve_any(curve_t *curve, config_t *cfg, arg_t *args);
* @param args unused
* @return state diff
*/
-int curve_nonzero(curve_t *curve, config_t *cfg, arg_t *args);
+int curve_nonzero(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -47,7 +47,7 @@ int curve_nonzero(curve_t *curve, config_t *cfg, arg_t *args);
* @param args unused
* @return state diff
*/
-int curve_seed(curve_t *curve, config_t *cfg, arg_t *args);
+int curve_seed(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* Serializes curve parameters into a t_VEC:
@@ -59,7 +59,7 @@ int curve_seed(curve_t *curve, config_t *cfg, arg_t *args);
* @param curve to serialize
* @return a t_VEC of curve parameters
*/
-GEN curve_params(curve_t *curve);
+GEN curve_params(const curve_t *curve);
/**
* Allocates and zeros out a new curve_t object.
@@ -75,7 +75,29 @@ curve_t *curve_new(void);
* @param dest destination curve
* @return destination curve
*/
-curve_t *curve_copy(curve_t *src, curve_t *dest);
+curve_t *curve_copy(const curve_t *src, curve_t *dest);
+
+/**
+ *
+ * @param src
+ * @return
+ */
+curve_t *curve_new_copy(const curve_t *src);
+
+/**
+ *
+ * @param src
+ * @param dest
+ * @return
+ */
+curve_t *curve_clone(const curve_t *src, curve_t *dest);
+
+/**
+ *
+ * @param src
+ * @return
+ */
+curve_t *curve_new_clone(const curve_t *src);
/**
* Free a curve_t along with it's seed_t and point_ts.
diff --git a/src/math/equation.c b/src/math/equation.c
index d60a8cd..4e2e0ec 100644
--- a/src/math/equation.c
+++ b/src/math/equation.c
@@ -3,16 +3,15 @@
* 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) {
+int a_random(curve_t *curve, const config_t *cfg, arg_t *args) {
curve->a = genrand(curve->field);
return 1;
}
-int a_input(curve_t *curve, config_t *cfg, arg_t *args) {
+int a_input(curve_t *curve, const config_t *cfg, arg_t *args) {
pari_sp ltop = avma;
GEN inp = input_int("a:", cfg->bits);
if (gequalm1(inp)) {
@@ -31,7 +30,7 @@ int a_input(curve_t *curve, config_t *cfg, arg_t *args) {
static GEN a = NULL;
static curve_t *curve_a = NULL;
-int a_once(curve_t *curve, config_t *cfg, arg_t *args) {
+int a_once(curve_t *curve, const config_t *cfg, arg_t *args) {
if (a && curve_a == curve) {
curve->a = gcopy(a);
return 1;
@@ -47,27 +46,27 @@ int a_once(curve_t *curve, config_t *cfg, arg_t *args) {
}
}
-int a_zero(curve_t *curve, config_t *cfg, arg_t *args) {
+int a_zero(curve_t *curve, const config_t *cfg, arg_t *args) {
curve->a = gen_0;
return 1;
}
-int a_one(curve_t *curve, config_t *cfg, arg_t *args) {
+int a_one(curve_t *curve, const config_t *cfg, arg_t *args) {
curve->a = gen_1;
return 1;
}
-int a_seed(curve_t *curve, config_t *cfg, arg_t *args) {
+int a_seed(curve_t *curve, const config_t *cfg, arg_t *args) {
// TODO implement
return INT_MIN;
}
-int b_random(curve_t *curve, config_t *cfg, arg_t *args) {
+int b_random(curve_t *curve, const config_t *cfg, arg_t *args) {
curve->b = genrand(curve->field);
return 1;
}
-int b_input(curve_t *curve, config_t *cfg, arg_t *args) {
+int b_input(curve_t *curve, const config_t *cfg, arg_t *args) {
pari_sp ltop = avma;
GEN inp = input_int("b:", cfg->bits);
if (gequalm1(inp)) {
@@ -86,7 +85,7 @@ int b_input(curve_t *curve, config_t *cfg, arg_t *args) {
static GEN b = NULL;
static curve_t *curve_b = NULL;
-int b_once(curve_t *curve, config_t *cfg, arg_t *args) {
+int b_once(curve_t *curve, const config_t *cfg, arg_t *args) {
if (b && curve_b == curve) {
curve->b = gcopy(b);
return 1;
@@ -102,17 +101,17 @@ int b_once(curve_t *curve, config_t *cfg, arg_t *args) {
}
}
-int b_zero(curve_t *curve, config_t *cfg, arg_t *args) {
+int b_zero(curve_t *curve, const config_t *cfg, arg_t *args) {
curve->b = gen_0;
return 1;
}
-int b_one(curve_t *curve, config_t *cfg, arg_t *args) {
+int b_one(curve_t *curve, const config_t *cfg, arg_t *args) {
curve->b = gen_1;
return 1;
}
-int b_seed(curve_t *curve, config_t *cfg, arg_t *args) {
+int b_seed(curve_t *curve, const config_t *cfg, arg_t *args) {
// TODO implement
return INT_MIN;
}
diff --git a/src/math/equation.h b/src/math/equation.h
index 03d87af..1f80f21 100644
--- a/src/math/equation.h
+++ b/src/math/equation.h
@@ -22,7 +22,7 @@
* @param args
* @return state diff
*/
-int a_random(curve_t *curve, config_t *cfg, arg_t *args);
+int a_random(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -33,7 +33,7 @@ int a_random(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return state diff
*/
-int a_input(curve_t *curve, config_t *cfg, arg_t *args);
+int a_input(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -44,7 +44,7 @@ int a_input(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return
*/
-int a_once(curve_t *curve, config_t *cfg, arg_t *args);
+int a_once(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -55,7 +55,7 @@ int a_once(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return state diff
*/
-int a_zero(curve_t *curve, config_t *cfg, arg_t *args);
+int a_zero(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -66,7 +66,7 @@ int a_zero(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return state diff
*/
-int a_one(curve_t *curve, config_t *cfg, arg_t *args);
+int a_one(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* @brief
@@ -75,7 +75,7 @@ int a_one(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return
*/
-int a_seed(curve_t *curve, config_t *cfg, arg_t *args);
+int a_seed(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -87,7 +87,7 @@ int a_seed(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return state diff
*/
-int b_random(curve_t *curve, config_t *cfg, arg_t *args);
+int b_random(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -98,7 +98,7 @@ int b_random(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return state diff
*/
-int b_input(curve_t *curve, config_t *cfg, arg_t *args);
+int b_input(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -109,7 +109,7 @@ int b_input(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return
*/
-int b_once(curve_t *curve, config_t *cfg, arg_t *args);
+int b_once(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -120,7 +120,7 @@ int b_once(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return state diff
*/
-int b_zero(curve_t *curve, config_t *cfg, arg_t *args);
+int b_zero(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -131,7 +131,7 @@ int b_zero(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return state diff
*/
-int b_one(curve_t *curve, config_t *cfg, arg_t *args);
+int b_one(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* @brief
@@ -140,7 +140,7 @@ int b_one(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return
*/
-int b_seed(curve_t *curve, config_t *cfg, arg_t *args);
+int b_seed(curve_t *curve, const config_t *cfg, arg_t *args);
/**
*
diff --git a/src/math/field.c b/src/math/field.c
index 5f7bea3..b140f77 100644
--- a/src/math/field.c
+++ b/src/math/field.c
@@ -3,15 +3,12 @@
* Copyright (C) 2017 J08nY
*/
#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); }
+static GEN field_primer(unsigned long bits) { return random_prime(bits); }
-static GEN field_binaryr(long bits) {
+static GEN field_binaryr(unsigned long bits) {
if (poly_exists(bits)) {
return poly_find_gen(bits);
} else {
@@ -21,7 +18,7 @@ static GEN field_binaryr(long bits) {
}
}
-int field_random(curve_t *curve, config_t *cfg, arg_t *args) {
+int field_random(curve_t *curve, const config_t *cfg, arg_t *args) {
switch (cfg->field) {
case FIELD_PRIME:
curve->field = field_primer(cfg->bits);
@@ -34,7 +31,7 @@ int field_random(curve_t *curve, config_t *cfg, arg_t *args) {
}
}
-int field_input(curve_t *curve, config_t *cfg, arg_t *args) {
+int field_input(curve_t *curve, const config_t *cfg, arg_t *args) {
pari_sp ltop = avma;
switch (cfg->field) {
case FIELD_PRIME: {
@@ -100,7 +97,7 @@ int field_input(curve_t *curve, config_t *cfg, arg_t *args) {
static GEN field = NULL;
static curve_t *curve_field = NULL;
-int field_once(curve_t *curve, config_t *cfg, arg_t *args) {
+int field_once(curve_t *curve, const config_t *cfg, arg_t *args) {
if (field && curve_field == curve) {
curve->field = gcopy(field);
return 1;
diff --git a/src/math/field.h b/src/math/field.h
index 66c8920..b1dc4f3 100644
--- a/src/math/field.h
+++ b/src/math/field.h
@@ -21,7 +21,7 @@
* @param args unused
* @return state diff
*/
-int field_random(curve_t *curve, config_t *cfg, arg_t *args);
+int field_random(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -34,7 +34,7 @@ int field_random(curve_t *curve, config_t *cfg, arg_t *args);
* @param args unused
* @return state diff
*/
-int field_input(curve_t *curve, config_t *cfg, arg_t *args);
+int field_input(curve_t *curve, const config_t *cfg, arg_t *args);
/**
*
@@ -43,7 +43,7 @@ int field_input(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return
*/
-int field_once(curve_t *curve, config_t *cfg, arg_t *args);
+int field_once(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* Extract a field representation from a field.
diff --git a/src/math/gens.c b/src/math/gens.c
index 7b0f678..f224386 100644
--- a/src/math/gens.c
+++ b/src/math/gens.c
@@ -22,13 +22,13 @@ static int gens_put(curve_t *curve, GEN generators, long len) {
return 1;
}
-int gens_any(curve_t *curve, config_t *cfg, arg_t *args) {
+int gens_any(curve_t *curve, const config_t *cfg, arg_t *args) {
GEN generators = ellff_get_gens(curve->curve);
long len = glength(generators);
return gens_put(curve, generators, len);
}
-int gens_one(curve_t *curve, config_t *cfg, arg_t *args) {
+int gens_one(curve_t *curve, const config_t *cfg, arg_t *args) {
pari_sp ltop = avma;
GEN generators = ellff_get_gens(curve->curve);
long len = glength(generators);
diff --git a/src/math/gens.h b/src/math/gens.h
index ba22358..0160074 100644
--- a/src/math/gens.h
+++ b/src/math/gens.h
@@ -18,7 +18,7 @@
* @param args
* @return
*/
-int gens_any(curve_t *curve, config_t *cfg, arg_t *args);
+int gens_any(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* @brief
@@ -27,6 +27,6 @@ int gens_any(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return
*/
-int gens_one(curve_t *curve, config_t *cfg, arg_t *args);
+int gens_one(curve_t *curve, const config_t *cfg, arg_t *args);
#endif // ECGEN_GENS_H
diff --git a/src/math/order.c b/src/math/order.c
index 1bcbb3e..17b7bca 100644
--- a/src/math/order.c
+++ b/src/math/order.c
@@ -4,12 +4,17 @@
*/
#include "order.h"
-int order_any(curve_t *curve, config_t *cfg, arg_t *args) {
- curve->order = ellff_get_card(curve->curve);
+int order_any(curve_t *curve, const config_t *cfg, arg_t *args) {
+ GEN ord = ellff_get_card(curve->curve);
+ if (isclone(ord)) {
+ curve->order = gcopy(ord);
+ } else {
+ curve->order = ord;
+ }
return 1;
}
-int order_smallfact(curve_t *curve, config_t *cfg, arg_t *args) {
+int order_smallfact(curve_t *curve, const config_t *cfg, arg_t *args) {
if (!args) {
fprintf(stderr, "No args to an arged function. order_smallfact");
return INT_MIN;
@@ -34,7 +39,7 @@ int order_smallfact(curve_t *curve, config_t *cfg, arg_t *args) {
}
}
-int order_prime(curve_t *curve, config_t *cfg, arg_t *args) {
+int order_prime(curve_t *curve, const config_t *cfg, arg_t *args) {
pari_sp ltop = avma;
GEN order = ellsea(curve->curve, 1);
if (gequal0(order) || !(isprime(order))) {
diff --git a/src/math/order.h b/src/math/order.h
index 14adc79..b883fe6 100644
--- a/src/math/order.h
+++ b/src/math/order.h
@@ -20,7 +20,7 @@
* @param args
* @return state diff
*/
-int order_any(curve_t *curve, config_t *cfg, arg_t *args);
+int order_any(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -30,7 +30,7 @@ int order_any(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return
*/
-int order_smallfact(curve_t *curve, config_t *cfg, arg_t *args);
+int order_smallfact(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -43,6 +43,6 @@ int order_smallfact(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return state diff
*/
-int order_prime(curve_t *curve, config_t *cfg, arg_t *args);
+int order_prime(curve_t *curve, const config_t *cfg, arg_t *args);
#endif // ECGEN_ORDER_H
diff --git a/src/math/point.c b/src/math/point.c
index 409bc48..dc9cd4a 100644
--- a/src/math/point.c
+++ b/src/math/point.c
@@ -3,6 +3,7 @@
* Copyright (C) 2017 J08nY
*/
#include "point.h"
+#include "types.h"
point_t *point_new(void) {
point_t *point = pari_malloc(sizeof(point_t));
@@ -21,8 +22,34 @@ point_t *point_copy(const point_t *src, point_t *dest) {
return dest;
}
+point_t *point_new_copy(const point_t *src) {
+ point_t *result = point_new();
+ return point_copy(src, result);
+}
+
+point_t *point_clone(const point_t *src, point_t *dest) {
+ if (src->point) dest->point = gclone(src->point);
+ if (src->order) dest->order = gclone(src->order);
+ if (src->cofactor) dest->cofactor = gclone(src->cofactor);
+ return dest;
+}
+
+point_t *point_new_clone(const point_t *src) {
+ point_t *result = point_new();
+ return point_clone(src, result);
+}
+
void point_free(point_t **point) {
if (*point) {
+ if ((*point)->point && isclone((*point)->point)) {
+ gunclone((*point)->point);
+ }
+ if ((*point)->order && isclone((*point)->order)) {
+ gunclone((*point)->order);
+ }
+ if ((*point)->cofactor && isclone((*point)->cofactor)) {
+ gunclone((*point)->cofactor);
+ }
pari_free(*point);
*point = NULL;
}
@@ -38,14 +65,30 @@ point_t **points_new(size_t num) {
return points;
}
-point_t **points_copy(point_t **src, point_t **dest, size_t num) {
+point_t **points_copy(point_t **const src, point_t **dest, size_t num) {
for (size_t i = 0; i < num; ++i) {
- dest[i] = point_new();
- dest[i] = point_copy(src[i], dest[i]);
+ dest[i] = point_new_copy(src[i]);
}
return dest;
}
+point_t **points_new_copy(point_t **const src, size_t num) {
+ point_t **result = points_new(num);
+ return points_copy(src, result, num);
+}
+
+point_t **points_clone(point_t **const src, point_t **dest, size_t num) {
+ for (size_t i = 0; i < num; ++i) {
+ dest[i] = point_new_clone(src[i]);
+ }
+ return dest;
+}
+
+point_t **points_new_clone(point_t **const src, size_t num) {
+ point_t **result = points_new(num);
+ return points_clone(src, result, num);
+}
+
void points_free(point_t ***points) {
if (*points) {
pari_free(*points);
@@ -62,7 +105,7 @@ void points_free_deep(point_t ***points, size_t npoints) {
}
}
-int point_random(curve_t *curve, config_t *cfg, arg_t *args) {
+int point_random(curve_t *curve, const config_t *cfg, arg_t *args) {
points_free_deep(&curve->points, curve->npoints);
point_t *p = point_new();
@@ -75,7 +118,7 @@ int point_random(curve_t *curve, config_t *cfg, arg_t *args) {
return 1;
}
-int points_random(curve_t *curve, config_t *cfg, arg_t *args) {
+int points_random(curve_t *curve, const config_t *cfg, arg_t *args) {
if (!args) {
fprintf(stderr, "No args to an arged function. points_random");
return INT_MIN;
@@ -111,7 +154,7 @@ int points_random(curve_t *curve, config_t *cfg, arg_t *args) {
}
*/
-int points_trial(curve_t *curve, config_t *cfg, arg_t *args) {
+int points_trial(curve_t *curve, const config_t *cfg, arg_t *args) {
// TODO stack code!!!
if (!args) {
fprintf(stderr, "No args to an arged function. points_trial");
@@ -150,7 +193,7 @@ int points_trial(curve_t *curve, config_t *cfg, arg_t *args) {
return 1;
}
-int points_prime(curve_t *curve, config_t *cfg, arg_t *args) {
+int points_prime(curve_t *curve, const config_t *cfg, arg_t *args) {
// TODO stack code!!!
points_free_deep(&curve->points, curve->npoints);
diff --git a/src/math/point.h b/src/math/point.h
index 4ebea31..9eef8a4 100644
--- a/src/math/point.h
+++ b/src/math/point.h
@@ -27,6 +27,28 @@ point_t *point_copy(const point_t *src, point_t *dest);
/**
*
+ * @param src
+ * @return
+ */
+point_t *point_new_copy(const point_t *src);
+
+/**
+ *
+ * @param src
+ * @param dest
+ * @return
+ */
+point_t *point_clone(const point_t *src, point_t *dest);
+
+/**
+ *
+ * @param src
+ * @return
+ */
+point_t *point_new_clone(const point_t *src);
+
+/**
+ *
* @param point
*/
void point_free(point_t **point);
@@ -45,7 +67,32 @@ point_t **points_new(size_t num);
* @param num
* @return
*/
-point_t **points_copy(point_t **src, point_t **dest, size_t num);
+point_t **points_copy(point_t **const src, point_t **dest, size_t num);
+
+/**
+ *
+ * @param src
+ * @param num
+ * @return
+ */
+point_t **points_new_copy(point_t **const src, size_t num);
+
+/**
+ *
+ * @param src
+ * @param dest
+ * @param num
+ * @return
+ */
+point_t **points_clone(point_t **const src, point_t **dest, size_t num);
+
+/**
+ *
+ * @param src
+ * @param num
+ * @return
+ */
+point_t **points_new_clone(point_t **const src, size_t num);
/**
*
@@ -68,7 +115,7 @@ void points_free_deep(point_t ***points, size_t npoints);
* @param args unused
* @return state diff
*/
-int point_random(curve_t *curve, config_t *cfg, arg_t *args);
+int point_random(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -78,7 +125,7 @@ int point_random(curve_t *curve, config_t *cfg, arg_t *args);
* @param args size_t number of points to generate
* @return state diff
*/
-int points_random(curve_t *curve, config_t *cfg, arg_t *args);
+int points_random(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -96,7 +143,7 @@ int points_random(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return state diff
*/
-int points_trial(curve_t *curve, config_t *cfg, arg_t *args);
+int points_trial(curve_t *curve, const config_t *cfg, arg_t *args);
/**
* GENERATOR(gen_t)
@@ -110,6 +157,6 @@ int points_trial(curve_t *curve, config_t *cfg, arg_t *args);
* @param args
* @return state diff
*/
-int points_prime(curve_t *curve, config_t *cfg, arg_t *args);
+int points_prime(curve_t *curve, const config_t *cfg, arg_t *args);
#endif // ECGEN_POINT_H
diff --git a/src/math/poly.c b/src/math/poly.c
index 5398d76..50d0da0 100644
--- a/src/math/poly.c
+++ b/src/math/poly.c
@@ -2712,9 +2712,9 @@ static int compare_poly(const void *a, const void *b) {
return (one->m - other->m);
}
-bool poly_exists(long m) { return m >= 2 && m <= 10000; }
+bool poly_exists(unsigned long m) { return m >= 2 && m <= 10000; }
-polynomial_t *poly_find(long m) {
+polynomial_t *poly_find(unsigned long m) {
if (!poly_exists(m)) {
return NULL;
}
@@ -2740,9 +2740,9 @@ polynomial_t *poly_find(long m) {
}
}
-GEN poly_find_gen(long m) { return poly_gen(poly_find(m)); }
+GEN poly_find_gen(unsigned long m) { return poly_gen(poly_find(m)); }
-GEN poly_gen(polynomial_t *polynomial) {
+GEN poly_gen(const polynomial_t *polynomial) {
pari_sp ltop = avma;
GEN coeffs = gtovec0(gen_0, polynomial->m + 1);
diff --git a/src/math/poly.h b/src/math/poly.h
index f9793f4..664ca6b 100644
--- a/src/math/poly.h
+++ b/src/math/poly.h
@@ -23,26 +23,26 @@ typedef struct {
* @param m
* @return
*/
-bool poly_exists(long m);
+bool poly_exists(unsigned long m);
/**
*
* @param m
* @return
*/
-polynomial_t *poly_find(long m);
+polynomial_t *poly_find(unsigned long m);
/**
*
* @param m
* @return
*/
-GEN poly_find_gen(long m);
+GEN poly_find_gen(unsigned long m);
/**
*
* @param polynomial
* @return
*/
-GEN poly_gen(polynomial_t *polynomial);
+GEN poly_gen(const polynomial_t *polynomial);
#endif // ECGEN_POLY_H
diff --git a/src/math/random.c b/src/math/random.c
index 20098e1..da4bc2c 100644
--- a/src/math/random.c
+++ b/src/math/random.c
@@ -35,7 +35,7 @@ bool random_init(void) {
return true;
}
-GEN random_prime(long bits) {
+GEN random_prime(unsigned long bits) {
pari_sp ltop = avma;
GEN range = gtovec0(gen_0, 2);
@@ -54,7 +54,7 @@ GEN random_prime(long bits) {
return gerepilecopy(ltop, p);
}
-GEN random_int(long bits) {
+GEN random_int(unsigned long bits) {
pari_sp ltop = avma;
GEN range = gtovec0(gen_0, 2);
diff --git a/src/math/random.h b/src/math/random.h
index 34c5bd5..de03abb 100644
--- a/src/math/random.h
+++ b/src/math/random.h
@@ -13,8 +13,8 @@
bool random_init(void);
-GEN random_prime(long bits);
+GEN random_prime(unsigned long bits);
-GEN random_int(long bits);
+GEN random_int(unsigned long bits);
#endif // ECGEN_RANDOM_H
diff --git a/src/math/types.c b/src/math/types.c
index 431c9c2..49d8620 100644
--- a/src/math/types.c
+++ b/src/math/types.c
@@ -4,4 +4,4 @@
*/
#include "types.h"
-int gen_skip(curve_t *curve, config_t *cfg, arg_t *args) { return 1; }
+int gen_skip(curve_t *curve, const config_t *cfg, arg_t *args) { return 1; }
diff --git a/src/math/types.h b/src/math/types.h
index a726021..4dee9dd 100644
--- a/src/math/types.h
+++ b/src/math/types.h
@@ -33,7 +33,7 @@ typedef struct {
size_t npoints;
} curve_t;
-enum curve_offset {
+typedef enum {
OFFSET_SEED,
OFFSET_FIELD,
OFFSET_A,
@@ -43,14 +43,14 @@ enum curve_offset {
OFFSET_GENERATORS,
OFFSET_POINTS,
OFFSET_END
-};
+} offset_e;
typedef struct {
void *args;
size_t nargs;
} arg_t;
-typedef int (*gen_t)(curve_t *, config_t *, arg_t *);
+typedef int (*gen_t)(curve_t *, const config_t *, arg_t *);
/**
* @brief
@@ -59,6 +59,6 @@ typedef int (*gen_t)(curve_t *, config_t *, arg_t *);
* @param args
* @return
*/
-int gen_skip(curve_t *curve, config_t *cfg, arg_t *args);
+int gen_skip(curve_t *curve, const config_t *cfg, arg_t *args);
#endif // ECGEN_TYPES_H