]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Enable ecc-modinv-test, also with mini-gmp.
authorNiels Möller <nisse@lysator.liu.se>
Fri, 3 Oct 2014 14:39:56 +0000 (16:39 +0200)
committerNiels Möller <nisse@lysator.liu.se>
Fri, 3 Oct 2014 14:39:56 +0000 (16:39 +0200)
ChangeLog
testsuite/ecc-modinv-test.c

index 44cf8557acc6d4b5f19706edb7d47d93b25d7bbc..8ef735499f6adf659e44eb8c5ef0040fbc6bd343 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,10 +1,14 @@
 2014-10-03  Niels Möller  <nisse@lysator.liu.se>
 
-       * testsuite/ecc-mod-test.c [NETTLE_USE_MINI_GMP]: Enable test.
-       (ref_mod): Use mpz_mod and mpz_limbs_copy, instead of mpn_tdiv_qr.
+       * testsuite/ecc-modinv-test.c [NETTLE_USE_MINI_GMP]: Enable test.
+       (ref_modinv): Use mpz_gcdext, instead of mpn_gcdext.
        (test_modulo): Replace gmp_fprintf calls by plain fprintf and
        mpn_out_str.
 
+       * testsuite/ecc-mod-test.c [NETTLE_USE_MINI_GMP]: Enable test.
+       (ref_mod): Use mpz_mod and mpz_limbs_copy, instead of mpn_tdiv_qr.
+       (test_modulo): Replace gmp_fprintf calls.
+
        * testsuite/testutils.c (mpn_out_str): New function, needed to
        replace uses of gmp_fprintf.
 
index a685f59c4a650f69399dbdb81a34031588963aca..c46c69f5060813ba0bc8f404715c98bf5c0b1cc7 100644 (file)
@@ -1,37 +1,33 @@
 #include "testutils.h"
 
-#if NETTLE_USE_MINI_GMP
-void
-test_main (void)
-{
-  SKIP();
-}
-#else /* ! NETTLE_USE_MINI_GMP */
-
 static int
 ref_modinv (mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *mp, mp_size_t mn)
 {
-  mp_limb_t tp[4*(mn+1)];
-  mp_limb_t *up = tp;
-  mp_limb_t *vp = tp + mn+1;
-  mp_limb_t *gp = tp + 2*(mn+1);
-  mp_limb_t *sp = tp + 3*(mn+1);
-  mp_size_t gn, sn;
+  mpz_t g, s, a, m;
+  int res;
 
-  mpn_copyi (up, ap, mn);
-  mpn_copyi (vp, mp, mn);
-  gn = mpn_gcdext (gp, sp, &sn, up, mn, vp, mn);
-  if (gn != 1 || gp[0] != 1)
-    return 0;
+  mpz_init (g);
+  mpz_init (s);
+  mpz_roinit_n (a, ap, mn);
+  mpz_roinit_n (m, mp, mn);
   
-  if (sn < 0)
-    mpn_sub (sp, mp, mn, sp, -sn);
-  else if (sn < mn)
-    /* Zero-pad. */
-    mpn_zero (sp + sn, mn - sn);
+  mpz_gcdext (g, s, NULL, a, m);
+  if (mpz_cmp_ui (g, 1) == 0)
+    {
+      if (mpz_sgn (s) < 0)
+       {
+         mpz_add (s, s, m);
+         ASSERT (mpz_sgn (s) > 0);
+       }
+      mpz_limbs_copy (rp, s, mn);
+      res = 1;
+    }
+  else
+    res = 0;
 
-  mpn_copyi (rp, sp, mn);
-  return 1;
+  mpz_clear (g);
+  mpz_clear (s);
+  return res;
 }
 
 static int
@@ -70,10 +66,11 @@ test_modulo (gmp_randstate_t rands, const char *name,
     {
       fprintf (stderr, "%s->invert failed for zero input (bit size %u):\n",
               name, m->bit_size);
-      gmp_fprintf (stderr, "p = %Nx\n"
-                  "t = %Nx (bad)\n",
-                  m->m, m->size,
-                  ai, m->size);
+      fprintf (stderr, "p = ");
+      mpn_out_str (stderr, 16, m->m, m->size);
+      fprintf (stderr, "\nt = ");
+      mpn_out_str (stderr, 16, ai, m->size);
+      fprintf (stderr, " (bad)\n");
       abort ();
     }
          
@@ -84,10 +81,12 @@ test_modulo (gmp_randstate_t rands, const char *name,
     {
       fprintf (stderr, "%s->invert failed for a = p input (bit size %u):\n",
               name, m->bit_size);
-      gmp_fprintf (stderr, "p = %Nx\n"
-                  "t = %Nx (bad)\n",
-                  m->m, m->size,
-                  ai, m->size);
+      
+      fprintf (stderr, "p = ");
+      mpn_out_str (stderr, 16, m->m, m->size);
+      fprintf (stderr, "\nt = ");
+      mpn_out_str (stderr, 16, ai, m->size);
+      fprintf (stderr, " (bad)\n");
       abort ();
     }
        
@@ -112,13 +111,15 @@ test_modulo (gmp_randstate_t rands, const char *name,
        {
          fprintf (stderr, "%s->invert failed (test %u, bit size %u):\n",
                   name, j, m->bit_size);
-         gmp_fprintf (stderr, "a = %Zx\n"
-                      "p = %Nx\n"
-                      "t = %Nx (bad)\n"
-                      "r = %Nx\n",
-                      r, m->m, m->size,
-                      ai, m->size,
-                      ref, m->size);
+         fprintf (stderr, "a = ");
+         mpz_out_str (stderr, 16, r);
+         fprintf (stderr, "\np = ");
+         mpn_out_str (stderr, 16, m->m, m->size);
+         fprintf (stderr, "\nt = ");
+         mpn_out_str (stderr, 16, ai, m->size);
+         fprintf (stderr, " (bad)\nr = ");
+         mpn_out_str (stderr, 16, ref, m->size);
+
          abort ();
        }
          
@@ -145,4 +146,3 @@ test_main (void)
     }
   gmp_randclear (rands);
 }
-#endif /* ! NETTLE_USE_MINI_GMP */