aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/math/koblitz.c73
-rw-r--r--src/math/koblitz.h23
-rw-r--r--src/math/poly.c2
-rw-r--r--src/math/poly.h2
-rw-r--r--test/src/util/test_timeout.c12
5 files changed, 103 insertions, 9 deletions
diff --git a/src/math/koblitz.c b/src/math/koblitz.c
new file mode 100644
index 0000000..48eec2a
--- /dev/null
+++ b/src/math/koblitz.c
@@ -0,0 +1,73 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017-2018 J08nY
+ */
+#include "koblitz.h"
+#include "gen/field.h"
+
+/**
+ * Data from:
+ * Guide to Elliptic Curve Cryptography,
+ * Darrel Hankerson, Alfred Menezes, Scott Vanstone
+ * Springer, 2004
+ */
+static koblitz_t koblitz_curves[] = {
+ // clang-format off
+ {101, 1, "0x2000000000000a8c8e37728cd6"},
+ {103, 0, "0x8000000000000a6a298f2129f4"},
+ {107, 0, "0x7ffffffffffffb57c25324737c4"},
+ {107, 1, "0x80000000000004a83dacdb8c83e"},
+ {109, 1, "0x1fffffffffffff4c436058707d36"},
+ {113, 1, "0x1fffffffffffffffb7f235edbd4e6"},
+ {131, 0, "0x80000000000000001353f755c0e8fc9a4"},
+ {163, 1, "0x800000000000000000004021145c1981b33f14bde"},
+ {233, 0, "0x200000000000000000000000000001a756ee456f351bbec6b57c5ceaf7c"},
+ {239, 0, "0x80000000000000000000000000000169e7fb19f2dba47c7076a00391e294"},
+ {277, 0, "0x1ffffffffffffffffffffffffffffffffffb42a2d15e3f4d2f69828d921e5bb03c3eec"},
+ {283, 0, "0x7ffffffffffffffffffffffffffffffffffa6b8bb41d5dc9977fdfe511478187858f184"},
+ {283, 1, "0x80000000000000000000000000000000000594744be2a2366880201aeeb87e787a70e7e"},
+ {311, 1, "0x7ffffffffffffffffffffffffffffffffffffff136b216132e5082acf66a36f8f226eb6b04ddce"},
+ {331, 1, "0x8000000000000000000000000000000000000000014cf64d52882df2e59be7970195b73750ef4048ebe"},
+ {347, 1, "0x7ffffffffffffffffffffffffffffffffffffffffffe7eb05ecfb792870172fe61a4926fc197f7b6801597e"},
+ {349, 0, "0x200000000000000000000000000000000000000000004f4b4f74ea048634aaca843ad4252e1d28422edd038c"},
+ {359, 1, "0x7fffffffffffffffffffffffffffffffffffffffffffedec280d2e16075c01f6a24e2d482a3941d593a361c20e"},
+ {409, 0, "0x1fffffffffffffffffffffffffffffffffffffffffffffffffff97e0ecb53a881003b1155f57b4f8f9f296d2d720ee380797f3c"},
+ {571, 0, "0x800000000000000000000000000000000000000000000000000000000000000000000004c614387c6698f92ce46a36e45fd04e2d8c3612f9758e4e07a477ad173f9de3d8df04004"}
+ // clang-format on
+};
+
+bool koblitz_is_curve(const curve_t *curve) {
+ pari_sp ltop = avma;
+ GEN a = field_elementi(curve->a);
+ GEN b = field_elementi(curve->b);
+ bool result = (gequal(a, gen_0) || gequal(a, gen_1)) && gequal(b, gen_1);
+ avma = ltop;
+ return result;
+}
+
+static int compare_koblitz(const void *a, const void *b) {
+ const koblitz_t *one = a;
+ const koblitz_t *other = b;
+ int m_diff = one->m - other->m;
+ if (m_diff == 0) {
+ return one->a - other->a;
+ } else {
+ return m_diff;
+ }
+}
+
+const koblitz_t *koblitz_find(unsigned int m, unsigned int a) {
+ koblitz_t searched = {m, a, NULL};
+ return (koblitz_t *)bsearch(&searched, koblitz_curves,
+ sizeof(koblitz_curves) / sizeof(koblitz_t),
+ sizeof(koblitz_t), &compare_koblitz);
+}
+
+GEN koblitz_get_order(unsigned long m, unsigned int a) {
+ const koblitz_t *found = koblitz_find(m, a);
+ if (found) {
+ return strtoi(found->hex_order);
+ } else {
+ return NULL;
+ }
+} \ No newline at end of file
diff --git a/src/math/koblitz.h b/src/math/koblitz.h
new file mode 100644
index 0000000..8284c2e
--- /dev/null
+++ b/src/math/koblitz.h
@@ -0,0 +1,23 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017-2018 J08nY
+ */
+#ifndef ECGEN_KOBLITZ_H
+#define ECGEN_KOBLITZ_H
+
+#include <stdbool.h>
+#include "misc/types.h"
+
+typedef struct {
+ unsigned int m;
+ unsigned int a;
+ const char *hex_order;
+} koblitz_t;
+
+bool koblitz_is_curve(const curve_t *curve);
+
+const koblitz_t *koblitz_find(unsigned int m, unsigned int a);
+
+GEN koblitz_get_order(unsigned long m, unsigned int a);
+
+#endif // ECGEN_KOBLITZ_H
diff --git a/src/math/poly.c b/src/math/poly.c
index d97c4af..5ffd243 100644
--- a/src/math/poly.c
+++ b/src/math/poly.c
@@ -2714,7 +2714,7 @@ static int compare_poly(const void *a, const void *b) {
bool poly_exists(unsigned long m) { return m >= 2 && m <= 10000; }
-polynomial_t *poly_find(unsigned long m) {
+const polynomial_t *poly_find(unsigned long m) {
if (!poly_exists(m)) {
return NULL;
}
diff --git a/src/math/poly.h b/src/math/poly.h
index 6552fb2..6c298d8 100644
--- a/src/math/poly.h
+++ b/src/math/poly.h
@@ -36,7 +36,7 @@ bool poly_exists(unsigned long m);
* @param m the degree of the polynomial searched
* @return the polynomial_t * inside the polynomial dataset
*/
-polynomial_t *poly_find(unsigned long m);
+const polynomial_t *poly_find(unsigned long m);
/**
* @brief Turn a polynomial_t into a GEN.
diff --git a/test/src/util/test_timeout.c b/test/src/util/test_timeout.c
index 307c3a8..21a13b0 100644
--- a/test/src/util/test_timeout.c
+++ b/test/src/util/test_timeout.c
@@ -4,8 +4,8 @@
*/
#include <criterion/criterion.h>
-#include "util/timeout.h"
#include "test/default.h"
+#include "util/timeout.h"
void timeout_setup(void) {
default_setup();
@@ -16,9 +16,8 @@ TestSuite(timeout, .init = timeout_setup, .fini = default_teardown);
Test(timeout, test_timeout_stop) {
bool done = false;
- timeout_start(5) {
- cr_assert_fail();
- } else {
+ timeout_start(5) { cr_assert_fail(); }
+ else {
done = true;
}
timeout_stop();
@@ -27,9 +26,8 @@ Test(timeout, test_timeout_stop) {
Test(timeout, test_timeout_handle) {
bool done = false;
- timeout_start(1) {
- done = true;
- } else {
+ timeout_start(1) { done = true; }
+ else {
sleep(2);
}
cr_assert(done, );