]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Simplified ecc-modinv-test.
authorNiels Möller <nisse@lysator.liu.se>
Mon, 22 Sep 2014 19:19:24 +0000 (21:19 +0200)
committerNiels Möller <nisse@lysator.liu.se>
Mon, 22 Sep 2014 19:19:24 +0000 (21:19 +0200)
ChangeLog
testsuite/ecc-modinv-test.c

index b3273de7317f957a25a01d70b9819ce860f6792b..8b2e04ac9c40835495a4a3eaaa1ab3f7da71681a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2014-09-22  Niels Möller  <nisse@lysator.liu.se>
 
+       * testsuite/ecc-modinv-test.c (test_modulo): New function, same
+       organization as in ecc-mod-test.c below.
+
        * testsuite/ecc-mod-test.c (test_modulo): New function, testing
        one modulo. Replacing...
        (test_curve): ... old function.
index 8c78c700459210c2a6b02870e4c84bb24a6772a4..ea49c2b6ac7a0401576797706b71d73c39350042 100644 (file)
@@ -37,110 +37,97 @@ ref_modinv (mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *mp, mp_size_t m
 #define MAX_ECC_SIZE (1 + 521 / GMP_NUMB_BITS)
 #define COUNT 500
 
-void
-test_main (void)
+static void
+test_modulo (gmp_randstate_t rands, const char *name,
+            const struct ecc_modulo *m)
 {
-  gmp_randstate_t rands;
   mp_limb_t a[MAX_ECC_SIZE];
   mp_limb_t ai[MAX_ECC_SIZE];
   mp_limb_t ref[MAX_ECC_SIZE];
   mp_limb_t scratch[ECC_MODINV_ITCH (MAX_ECC_SIZE)];
-  unsigned i;
+  unsigned j;
   mpz_t r;
 
-  gmp_randinit_default (rands);
   mpz_init (r);
   
-  for (i = 0; ecc_curves[i]; i++)
+  /* Check behaviour for zero input */
+  mpn_zero (a, m->size);
+  memset (ai, 17, m->size * sizeof(*ai));
+  m->invert (m, ai, a, scratch);
+  if (!mpn_zero_p (ai, m->size))
+    {
+      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);
+      abort ();
+    }
+         
+  /* Check behaviour for a = m */
+  mpn_copyi (a, m->m, m->size);
+  memset (ai, 17, m->size * sizeof(*ai));
+  m->invert (m, ai, a, scratch);
+  if (!mpn_zero_p (ai, m->size))
     {
-      const struct ecc_curve *ecc = ecc_curves[i];
-      unsigned j;
-      /* Check behaviour for zero input */
-      mpn_zero (a, ecc->p.size);
-      memset (ai, 17, ecc->p.size * sizeof(*ai));
-      ecc->p.invert (&ecc->p, ai, a, scratch);
-      if (!mpn_zero_p (ai, ecc->p.size))
+      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);
+      abort ();
+    }
+       
+  for (j = 0; j < COUNT; j++)
+    {
+      if (j & 1)
+       mpz_rrandomb (r, rands, m->size * GMP_NUMB_BITS);
+      else
+       mpz_urandomb (r, rands, m->size * GMP_NUMB_BITS);
+
+      mpz_limbs_copy (a, r, m->size);
+
+      if (!ref_modinv (ref, a, m->m, m->size))
        {
-         fprintf (stderr, "ecc->p.invert failed for zero input (bit size %u):\n",
-                      ecc->p.bit_size);
-         gmp_fprintf (stderr, "p = %Nx\n"
-                      "t = %Nx (bad)\n",
-                      ecc->p.m, ecc->p.size,
-                      ai, ecc->p.size);
-         abort ();
+         if (verbose)
+           fprintf (stderr, "Test %u (bit size %u) not invertible mod %s.\n",
+                    j, m->bit_size, name);
+         continue;
        }
-         
-      /* Check behaviour for a = p */
-      mpn_copyi (a, ecc->p.m, ecc->p.size);
-      memset (ai, 17, ecc->p.size * sizeof(*ai));
-      ecc->p.invert (&ecc->p, ai, a, scratch);
-      if (!mpn_zero_p (ai, ecc->p.size))
+      m->invert (m, ai, a, scratch);
+      if (mpn_cmp (ref, ai, m->size))
        {
-         fprintf (stderr, "ecc->p.invert failed for a = p input (bit size %u):\n",
-                      ecc->p.bit_size);
-         gmp_fprintf (stderr, "p = %Nx\n"
-                      "t = %Nx (bad)\n",
-                      ecc->p.m, ecc->p.size,
-                      ai, ecc->p.size);
+         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);
          abort ();
        }
-       
-      for (j = 0; j < COUNT; j++)
-       {
-         if (j & 1)
-           mpz_rrandomb (r, rands, ecc->p.size * GMP_NUMB_BITS);
-         else
-           mpz_urandomb (r, rands, ecc->p.size * GMP_NUMB_BITS);
-
-         mpz_limbs_copy (a, r, ecc->p.size);
+         
+    }
+  mpz_clear (r);
+}
 
-         if (!ref_modinv (ref, a, ecc->p.m, ecc->p.size))
-           {
-             if (verbose)
-               fprintf (stderr, "Test %u (bit size %u) not invertible.\n",
-                        j, ecc->p.bit_size);
-             continue;
-           }
-         ecc->p.invert (&ecc->p, ai, a, scratch);
-         if (mpn_cmp (ref, ai, ecc->p.size))
-           {
-             fprintf (stderr, "ecc->p.invert failed (test %u, bit size %u):\n",
-                      j, ecc->p.bit_size);
-             gmp_fprintf (stderr, "a = %Zx\n"
-                          "p = %Nx\n"
-                          "t = %Nx (bad)\n"
-                          "r = %Nx\n",
-                          r, ecc->p.m, ecc->p.size,
-                          ai, ecc->p.size,
-                          ref, ecc->p.size);
-             abort ();
-           }
+void
+test_main (void)
+{
+  gmp_randstate_t rands;
+  unsigned i;
 
-         mpz_limbs_copy (a, r, ecc->p.size);
+  gmp_randinit_default (rands);
 
-         if (!ref_modinv (ref, a, ecc->q.m, ecc->p.size))
-           {
-             fprintf (stderr, "Test %u (bit size %u) not invertible.\n",
-                      j, ecc->q.bit_size);
-             continue;
-           }
-         ecc->q.invert (&ecc->q, ai, a, scratch);
-         if (mpn_cmp (ref, ai, ecc->p.size))
-           {
-             fprintf (stderr, "ecc->q.invert failed (test %u, bit size %u):\n",
-                      j, ecc->q.bit_size);
-             gmp_fprintf (stderr, "a = %Zx\n"
-                          "p = %Nx\n"
-                          "t = %Nx (bad)\n"
-                          "r = %Nx\n",
-                          r, ecc->p.m, ecc->p.size,
-                          ai, ecc->p.size,
-                          ref, ecc->p.size);
-             abort ();
-           }
-       }
+  for (i = 0; ecc_curves[i]; i++)
+    {
+      test_modulo (rands, "p", &ecc_curves[i]->p);
+      test_modulo (rands, "q", &ecc_curves[i]->q);
     }
   gmp_randclear (rands);
-  mpz_clear (r);
 }
 #endif /* ! NETTLE_USE_MINI_GMP */