From 5b67753541e77676685af9c3aa9624fb24ed2b25 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niels=20M=C3=B6ller?= Date: Fri, 3 Oct 2014 16:19:58 +0200 Subject: [PATCH] Make ecc-sqrt-test work with mini-gmp. --- ChangeLog | 11 +++++++++++ testsuite/ecc-sqrt-test.c | 32 ++++++++++++++++++++++++++++++++ testsuite/testutils.c | 19 +++++++++++++++++++ testsuite/testutils.h | 16 +++++++++++++++- 4 files changed, 77 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index bb68d2a9..3087507d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2014-10-03 Niels Möller + + * testsuite/ecc-sqrt-test.c (mpz_ui_kronecker) + [NETTLE_USE_MINI_GMP]: New fallback definition when building with + mini-gmp. + * testsuite/testutils.c (gmp_randinit_default) + [NETTLE_USE_MINI_GMP]: Likewise. + (mpz_urandomb): Likewise. + * testsuite/testutils.h (gmp_randstate_t) [NETTLE_USE_MINI_GMP]: + Fallback typedef, using knuth_lfib_ctx. + 2014-10-02 Niels Möller * testsuite/eddsa-compress-test.c: New testcase. diff --git a/testsuite/ecc-sqrt-test.c b/testsuite/ecc-sqrt-test.c index 08cd8f38..90463faa 100644 --- a/testsuite/ecc-sqrt-test.c +++ b/testsuite/ecc-sqrt-test.c @@ -33,6 +33,38 @@ #define COUNT 5000 +#if NETTLE_USE_MINI_GMP +/* Implements Legendre symbol only, requiring that p is an odd prime */ +static int +mpz_ui_kronecker (mp_limb_t ul, const mpz_t p) +{ + mpz_t t, u; + int r; + + mpz_init_set_ui (u, ul); + mpz_init_set (t, p); + mpz_sub_ui (t, t, 1); + mpz_tdiv_q_2exp (t, t, 1); + mpz_powm (t, u, t, p); + + r = mpz_cmp_ui (t, 1); + if (r < 0) + r = 0; + else if (r == 0) + r = 1; + else + { + mpz_sub (t, p, t); + ASSERT (mpz_cmp_ui (t, 1) == 0); + r = -1; + } + mpz_clear (t); + mpz_clear (u); + + return r; +} +#endif /* NETTLE_USE_MINI_GMP */ + static void test_modulo (gmp_randstate_t rands, const struct ecc_modulo *m) { diff --git a/testsuite/testutils.c b/testsuite/testutils.c index c082e408..b08f726e 100644 --- a/testsuite/testutils.c +++ b/testsuite/testutils.c @@ -699,6 +699,25 @@ mpn_zero_p (mp_srcptr ap, mp_size_t n) } #endif +#if NETTLE_USE_MINI_GMP +void +gmp_randinit_default (struct knuth_lfib_ctx *ctx) +{ + knuth_lfib_init (ctx, 17); +} +void +mpz_urandomb (mpz_t r, struct knuth_lfib_ctx *ctx, mp_bitcnt_t bits) +{ + size_t bytes = (bits+7)/8; + uint8_t *buf = xalloc (bytes); + + knuth_lfib_random (ctx, bytes, buf); + buf[bytes-1] &= 0xff >> (8*bytes - bits); + nettle_mpz_set_str_256_u (r, bytes, buf); + free (buf); +} +#endif /* NETTLE_USE_MINI_GMP */ + mp_limb_t * xalloc_limbs (mp_size_t n) { diff --git a/testsuite/testutils.h b/testsuite/testutils.h index 7f2135b8..b32f76b3 100644 --- a/testsuite/testutils.h +++ b/testsuite/testutils.h @@ -20,11 +20,14 @@ # include "ecc-internal.h" # include "ecdsa.h" # include "gmp-glue.h" +# if NETTLE_USE_MINI_GMP +# include "knuth-lfib.h" +# endif /* Undo dsa-compat name mangling */ #undef dsa_generate_keypair #define dsa_generate_keypair nettle_dsa_generate_keypair -#endif +#endif /* WITH_HOGWEED */ #include "nettle-meta.h" @@ -162,6 +165,17 @@ int mpn_zero_p (mp_srcptr ap, mp_size_t n); #endif +#if NETTLE_USE_MINI_GMP +typedef struct knuth_lfib_ctx gmp_randstate_t[1]; + +void gmp_randinit_default (struct knuth_lfib_ctx *ctx); +#define gmp_randclear(state) +void mpz_urandomb (mpz_t r, struct knuth_lfib_ctx *ctx, mp_bitcnt_t bits); +/* This is cheating */ +#define mpz_rrandomb mpz_urandomb + +#endif /* NETTLE_USE_MINI_GMP */ + mp_limb_t * xalloc_limbs (mp_size_t n); -- 2.47.2