aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJ08nY2017-12-13 16:35:58 +0100
committerJ08nY2017-12-13 16:35:58 +0100
commite0a255ac68932521c24e39f73f5fbf166383f275 (patch)
tree10440ad2f286ce53ef094abb5f399e494685dedd /src
parent629ba8254039945dbf1fe7126611ac8b72010429 (diff)
downloadecgen-e0a255ac68932521c24e39f73f5fbf166383f275.tar.gz
ecgen-e0a255ac68932521c24e39f73f5fbf166383f275.tar.zst
ecgen-e0a255ac68932521c24e39f73f5fbf166383f275.zip
Diffstat (limited to 'src')
-rw-r--r--src/exhaustive/brainpool.c60
-rw-r--r--src/exhaustive/brainpool.h9
-rw-r--r--src/exhaustive/brainpool_rfc.c22
-rw-r--r--src/exhaustive/brainpool_rfc.h47
-rw-r--r--src/exhaustive/exhaustive.c23
-rw-r--r--src/misc/types.h2
6 files changed, 143 insertions, 20 deletions
diff --git a/src/exhaustive/brainpool.c b/src/exhaustive/brainpool.c
index 5557bc8..afe5b23 100644
--- a/src/exhaustive/brainpool.c
+++ b/src/exhaustive/brainpool.c
@@ -3,17 +3,16 @@
* Copyright (C) 2017 J08nY
*/
+#include <misc/types.h>
#include "brainpool.h"
#include "gen/seed.h"
#include "io/output.h"
#include "util/bits.h"
#include "util/str.h"
-#include "util/memory.h"
static seed_t *brainpool_new() {
seed_t *result = seed_new();
result->type = SEED_BRAINPOOL;
- result->brainpool.first = true;
return result;
}
@@ -22,7 +21,7 @@ static void seed_wv(seed_t *seed) {
GEN L = utoi(cfg->bits);
seed->brainpool.v = itou(gfloor(gdivgs(subis(L, 1), 160)));
seed->brainpool.w =
- itou(subis(subis(L, 160 * seed->brainpool.v), 1));
+ itou(subis(subis(L, 160 * seed->brainpool.v), 1));
avma = ltop;
}
@@ -41,7 +40,7 @@ static bits_t *brainpool_hash(const bits_t *s, long w, long v) {
unsigned char h0[20];
bits_sha1(s, h0);
unsigned char hashout[w + 20 * v];
- memcpy(hashout, h0, (size_t)w);
+ memcpy(hashout, h0, (size_t) w);
GEN z = bits_to_i(s);
GEN m = int2n(160);
@@ -50,7 +49,7 @@ static bits_t *brainpool_hash(const bits_t *s, long w, long v) {
bits_sha1(si, hashout + w + 20 * i);
bits_free(&si);
}
- bits_t *result = bits_from_raw(hashout, (size_t)(w + (20 * v)));
+ bits_t *result = bits_from_raw(hashout, (size_t) (w + (20 * v)));
avma = ltop;
return result;
}
@@ -94,19 +93,57 @@ GENERATOR(brainpool_gen_seed_input) {
return 1;
}
+GENERATOR(brainpool_gen_field) {
+ pari_sp btop = avma;
+ seed_t *seed = curve->seed;
+ do {
+ if (seed->brainpool.update_seed) {
+ brainpool_update_seed(seed->seed);
+ seed->brainpool.update_seed = false;
+ }
+ bits_t *p_bits = brainpool_hash(seed->seed, seed->brainpool.w + 1, seed->brainpool.v);
+ GEN c = bits_to_i(p_bits);
+ bits_free(&p_bits);
+ GEN p = c;
+ do {
+ p = nextprime(p);
+ } while (mod4(p) != 3);
+
+ long p_len = glength(binary_zv(p));
+ if (p_len >= cfg->bits || p_len <= cfg->bits - 1) {
+ brainpool_update_seed(seed->seed);
+ avma = btop;
+ continue;
+ }
+
+ if (!isprime(p)) {
+ brainpool_update_seed(seed->seed);
+ avma = btop;
+ continue;
+ }
+
+ curve->field = p;
+ gerepileall(btop, 1, &curve->field);
+ break;
+ } while (true);
+
+ seed->brainpool.update_seed = true;
+ return 1;
+}
+
GENERATOR(brainpool_gen_equation) {
// field is definitely prime
pari_sp btop = avma;
seed_t *seed = curve->seed;
do {
- if (seed->brainpool.first) {
+ if (seed->brainpool.update_seed) {
brainpool_update_seed(seed->seed);
- seed->brainpool.first = false;
+ seed->brainpool.update_seed = false;
}
GEN z;
bits_t *a_bits =
- brainpool_hash(seed->seed, seed->brainpool.w, seed->brainpool.v);
+ brainpool_hash(seed->seed, seed->brainpool.w, seed->brainpool.v);
GEN a = bits_to_i(a_bits);
bits_free(&a_bits);
z = Fp_sqrtn(a, stoi(4), curve->field, NULL);
@@ -120,7 +157,7 @@ GENERATOR(brainpool_gen_equation) {
brainpool_update_seed(seed->seed);
bits_t *b_bits =
- brainpool_hash(seed->seed, seed->brainpool.w, seed->brainpool.v);
+ brainpool_hash(seed->seed, seed->brainpool.w, seed->brainpool.v);
GEN b = bits_to_i(b_bits);
bits_free(&b_bits);
if (!Fp_issquare(b, curve->field)) {
@@ -135,7 +172,7 @@ GENERATOR(brainpool_gen_equation) {
GEN mod_b = gmodulo(b, curve->field);
if (gequal0(gmulsg(-16, gadd(gmulsg(4, gpowgs(mod_a, 3)),
- gmulsg(27, gsqr(mod_b)))))) {
+ gmulsg(27, gsqr(mod_b)))))) {
brainpool_update_seed(seed->seed);
bits_free(&seed->brainpool.seed_a);
bits_free(&seed->brainpool.seed_b);
@@ -147,7 +184,7 @@ GENERATOR(brainpool_gen_equation) {
seed->brainpool.seed_bp = bits_copy(seed->seed);
bits_t *mult_bits =
- brainpool_hash(seed->seed, seed->brainpool.w, seed->brainpool.v);
+ brainpool_hash(seed->seed, seed->brainpool.w, seed->brainpool.v);
seed->brainpool.mult = bits_to_i(mult_bits);
curve->a = mod_a;
@@ -156,5 +193,6 @@ GENERATOR(brainpool_gen_equation) {
break;
} while (true);
+ seed->brainpool.update_seed = true;
return 1;
} \ No newline at end of file
diff --git a/src/exhaustive/brainpool.h b/src/exhaustive/brainpool.h
index cb79e89..188780b 100644
--- a/src/exhaustive/brainpool.h
+++ b/src/exhaustive/brainpool.h
@@ -43,6 +43,15 @@ GENERATOR(brainpool_gen_seed_argument);
GENERATOR(brainpool_gen_seed_input);
/**
+ *
+ * @param curve
+ * @param args
+ * @param state
+ * @return
+ */
+GENERATOR(brainpool_gen_field);
+
+/**
* @brief
* @param curve
* @param args
diff --git a/src/exhaustive/brainpool_rfc.c b/src/exhaustive/brainpool_rfc.c
new file mode 100644
index 0000000..7af6b29
--- /dev/null
+++ b/src/exhaustive/brainpool_rfc.c
@@ -0,0 +1,22 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017 J08nY
+ */
+
+#include "brainpool_rfc.h"
+
+GENERATOR(brainpool_rfc_gen_seed_random) {
+
+}
+
+GENERATOR(brainpool_rfc_gen_seed_argument) {
+
+}
+
+GENERATOR(brainpool_rfc_gen_seed_input) {
+
+}
+
+GENERATOR(brainpool_rfc_gen_equation) {
+
+} \ No newline at end of file
diff --git a/src/exhaustive/brainpool_rfc.h b/src/exhaustive/brainpool_rfc.h
new file mode 100644
index 0000000..ade7068
--- /dev/null
+++ b/src/exhaustive/brainpool_rfc.h
@@ -0,0 +1,47 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017 J08nY
+ */
+
+#ifndef ECGEN_BRAINPOOL_RFC_H
+#define ECGEN_BRAINPOOL_RFC_H
+
+#include "misc/types.h"
+
+/**
+ * @brief
+ * @param curve
+ * @param args
+ * @param state
+ * @return
+ */
+GENERATOR(brainpool_rfc_gen_seed_random);
+
+/**
+ * @brief
+ * @param curve
+ * @param args
+ * @param state
+ * @return
+ */
+GENERATOR(brainpool_rfc_gen_seed_argument);
+
+/**
+ * @brief
+ * @param curve
+ * @param args
+ * @param state
+ * @return
+ */
+GENERATOR(brainpool_rfc_gen_seed_input);
+
+/**
+ * @brief
+ * @param curve
+ * @param args
+ * @param state
+ * @return
+ */
+GENERATOR(brainpool_rfc_gen_equation);
+
+#endif //ECGEN_BRAINPOOL_RFC_H
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c
index 78c952e..9c2dfdd 100644
--- a/src/exhaustive/exhaustive.c
+++ b/src/exhaustive/exhaustive.c
@@ -53,6 +53,11 @@ static void exhaustive_ginit(gen_f *generators) {
}
generators[OFFSET_A] = &gen_skip;
generators[OFFSET_B] = &ansi_gen_equation;
+ if (cfg->random) {
+ generators[OFFSET_FIELD] = &field_gen_random;
+ } else {
+ generators[OFFSET_FIELD] = &field_gen_input;
+ }
} break;
case SEED_BRAINPOOL: {
if (cfg->seed) {
@@ -64,7 +69,8 @@ static void exhaustive_ginit(gen_f *generators) {
generators[OFFSET_SEED] = &brainpool_gen_seed_input;
}
}
- generators[OFFSET_A] = &gen_skip;
+ generators[OFFSET_FIELD] = &brainpool_gen_field;
+ generators[OFFSET_A] = &gen_skip;
generators[OFFSET_B] = &brainpool_gen_equation;
} break;
case SEED_BRAINPOOL_RFC:
@@ -120,6 +126,14 @@ static void exhaustive_ginit(gen_f *generators) {
} else {
generators[OFFSET_ORDER] = &order_gen_any;
}
+
+ if (cfg->method == METHOD_ANOMALOUS) {
+ generators[OFFSET_FIELD] = &anomalous_gen_field;
+ } else if (cfg->random) {
+ generators[OFFSET_FIELD] = &field_gen_random;
+ } else {
+ generators[OFFSET_FIELD] = &field_gen_input;
+ }
}
// setup common generators
generators[OFFSET_CURVE] = &curve_gen_any;
@@ -130,13 +144,6 @@ static void exhaustive_ginit(gen_f *generators) {
generators[OFFSET_GENERATORS] = &gens_gen_any;
}
- if (cfg->method == METHOD_ANOMALOUS) {
- generators[OFFSET_FIELD] = &anomalous_gen_field;
- } else if (cfg->random) {
- generators[OFFSET_FIELD] = &field_gen_random;
- } else {
- generators[OFFSET_FIELD] = &field_gen_input;
- }
switch (cfg->points.type) {
case POINTS_RANDOM:
diff --git a/src/misc/types.h b/src/misc/types.h
index 9c1cff6..960745c 100644
--- a/src/misc/types.h
+++ b/src/misc/types.h
@@ -44,7 +44,7 @@ typedef struct {
GEN r;
} ansi;
struct {
- bool first;
+ bool update_seed;
long w;
long v;
bits_t *seed_a;