diff options
| author | J08nY | 2018-01-18 17:00:41 +0100 |
|---|---|---|
| committer | J08nY | 2018-01-18 17:00:41 +0100 |
| commit | f940f80a2cb69ffcd1f0d608cbb1b0f06e0854d6 (patch) | |
| tree | a088defb41617f21d1cc729c3c71fd378f1b7f10 | |
| parent | 8033902b02a82ce2521d4d029b6927e190866c95 (diff) | |
| download | ecgen-f940f80a2cb69ffcd1f0d608cbb1b0f06e0854d6.tar.gz ecgen-f940f80a2cb69ffcd1f0d608cbb1b0f06e0854d6.tar.zst ecgen-f940f80a2cb69ffcd1f0d608cbb1b0f06e0854d6.zip | |
| -rw-r--r-- | src/math/twists.c | 18 | ||||
| -rw-r--r-- | src/math/twists.h | 17 | ||||
| -rw-r--r-- | src/util/random.c | 12 | ||||
| -rw-r--r-- | src/util/random.h | 7 | ||||
| -rw-r--r-- | test/src/math/test_twists.c | 48 |
5 files changed, 102 insertions, 0 deletions
diff --git a/src/math/twists.c b/src/math/twists.c new file mode 100644 index 0000000..78481f4 --- /dev/null +++ b/src/math/twists.c @@ -0,0 +1,18 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017-2018 J08nY + */ +#include "twists.h" + +void curve_twist_rand(curve_t *to, const curve_t *of) { + to->field = gcopy(of->field); + GEN v = elltwist(of->curve, NULL); + to->curve = ellinit(v, to->field, -1); + if (typ(of->field) == t_INT) { + to->a = ell_get_a4(to->curve); + to->b = ell_get_a6(to->curve); + } else if (typ(of->field) == t_FFELT) { + to->a = ell_get_a2(to->curve); + to->b = ell_get_a6(to->curve); + } +}
\ No newline at end of file diff --git a/src/math/twists.h b/src/math/twists.h new file mode 100644 index 0000000..3249047 --- /dev/null +++ b/src/math/twists.h @@ -0,0 +1,17 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017-2018 J08nY + */ +#ifndef ECGEN_TWIST_H +#define ECGEN_TWIST_H + +#include "misc/types.h" + +/** + * @brief Twists the <code>of</code> curve randomly. + * @param to The result of the twist. + * @param of The curve to be twisted. + */ +void curve_twist_rand(curve_t *to, const curve_t *of); + +#endif // ECGEN_TWIST_H diff --git a/src/util/random.c b/src/util/random.c index c754677..860cff0 100644 --- a/src/util/random.c +++ b/src/util/random.c @@ -61,3 +61,15 @@ GEN random_int(unsigned long bits) { GEN range = mkvec2(int2n(bits - 1), int2n(bits)); return gerepilecopy(ltop, genrand(range)); } + +GEN random_field_element(GEN field) { + switch (typ(field)) { + case t_INT: + return mkintmod(genrand(field), field); + case t_FFELT: + return genrand(field); + default: + pari_err_TYPE("", field); + return NULL; + } +} diff --git a/src/util/random.h b/src/util/random.h index f78f991..56e2efd 100644 --- a/src/util/random.h +++ b/src/util/random.h @@ -36,4 +36,11 @@ GEN random_prime(unsigned long bits); */ GEN random_int(unsigned long bits); +/** + * @brief + * @param field + * @return + */ +GEN random_field_element(GEN field); + #endif // ECGEN_RANDOM_H diff --git a/test/src/math/test_twists.c b/test/src/math/test_twists.c new file mode 100644 index 0000000..30bc958 --- /dev/null +++ b/test/src/math/test_twists.c @@ -0,0 +1,48 @@ +/* + * ecgen, tool for generating Elliptic curve domain parameters + * Copyright (C) 2017 J08nY + */ +#include <criterion/criterion.h> +#include "gen/point.h" +#include "gen/field.h" +#include "math/poly.h" +#include "math/twists.h" +#include "test/default.h" +#include "util/random.h" + +TestSuite(twists, .init = default_setup, .fini = default_teardown); + +Test(twists, test_twist_rand_fp) { + random_init(); + GEN a = mkintmodu(3, 23); + GEN b = mkintmodu(4, 23); + curve_t curve = {.a = a, + .b = b, + .field = stoi(23), + .curve = ellinit(mkvec2(a, b), stoi(23), -1)}; + + curve_t to = {0}; + curve_twist_rand(&to, &curve); + cr_assert_not_null(to.a, ); + cr_assert_not_null(to.b, ); + cr_assert_not_null(to.field, ); + cr_assert_not_null(to.curve, ); +} + +Test(twists, test_twist_rand_f2m) { + random_init(); + GEN field = poly_find_gen(13); + GEN a = field_ielement(field, stoi(2)); + GEN b = field_ielement(field, stoi(3)); + curve_t curve = {.a = a, + .b = b, + .field = field, + .curve = ellinit(mkvecn(5, gen_1, a, gen_0, gen_0, b), NULL, -1)}; + + curve_t to = {0}; + curve_twist_rand(&to, &curve); + cr_assert_not_null(to.a, ); + cr_assert_not_null(to.b, ); + cr_assert_not_null(to.field, ); + cr_assert_not_null(to.curve, ); +}
\ No newline at end of file |
