]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Make ecc-sqrt-test work with mini-gmp.
authorNiels Möller <nisse@lysator.liu.se>
Fri, 3 Oct 2014 14:19:58 +0000 (16:19 +0200)
committerNiels Möller <nisse@lysator.liu.se>
Fri, 3 Oct 2014 14:19:58 +0000 (16:19 +0200)
ChangeLog
testsuite/ecc-sqrt-test.c
testsuite/testutils.c
testsuite/testutils.h

index bb68d2a9a8ef6e1330359cd241b3ed1c441fdf12..3087507dc971bc8f4c7e943e76fc5cae1aa12f9c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2014-10-03  Niels Möller  <nisse@lysator.liu.se>
+
+       * 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  <nisse@lysator.liu.se>
 
        * testsuite/eddsa-compress-test.c: New testcase.
index 08cd8f38ac505bb41e31710e2708c40dce835bd7..90463faadff0564ca8b8d07ace26bb95f22bd320 100644 (file)
 
 #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)
 {
index c082e408a4bea1a553f93f831a86df2a92e921b6..b08f726eea468c2b1e45fcee79ad5fac1e6798a1 100644 (file)
@@ -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)
 {
index 7f2135b88b487e6668e1d6e8c090ee731ada53e7..b32f76b3dcfd4e3f4fa50a9afbe70efae54a56fa 100644 (file)
 # 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);