]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Extend ecc-redc-test.
authorNiels Möller <nisse@lysator.liu.se>
Sat, 31 Oct 2020 09:15:46 +0000 (10:15 +0100)
committerNiels Möller <nisse@lysator.liu.se>
Sat, 31 Oct 2020 09:15:46 +0000 (10:15 +0100)
ChangeLog
testsuite/ecc-redc-test.c

index cb2deaa55fb50fb32252550005afbef3e8fa7f86..a11c69ce02833a47ec7424f15ad3d67a6622d7a6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2020-10-31  Niels Möller  <nisse@lysator.liu.se>
+
+       * testsuite/ecc-mod-test.c (test_one): Extend tests, to also test
+       with different destination area.
+       * testsuite/ecc-redc-test.c (test_main): Likewise.
+
 2020-10-30  Niels Möller  <nisse@lysator.liu.se>
 
        * ecc-internal.h (typedef ecc_mod_func): Add separate result
index 2c18ea255b3fda01b51036b51481e1d252f1e918..3e02979262eff629c12ab2b892091aa25ca65b3e 100644 (file)
@@ -27,6 +27,15 @@ ref_redc (mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *mp, mp_size_t mn)
   mpz_clear (t);
 }
 
+/* Destructively normalize tp, then compare */
+static int
+mod_equal(const struct ecc_modulo *m, const mp_limb_t *ref, mp_limb_t *tp)
+{
+  if (mpn_cmp (tp, m->m, m->size) >= 0)
+    mpn_sub_n (tp, tp, m->m, m->size);
+  return mpn_cmp (ref, tp, m->size) == 0;
+}
+
 #define MAX_ECC_SIZE (1 + 521 / GMP_NUMB_BITS)
 #define MAX_SIZE (2*MAX_ECC_SIZE)
 #define COUNT 50000
@@ -65,12 +74,9 @@ test_main (void)
            {
              mpn_copyi (m, a, 2*ecc->p.size);
              ecc->p.reduce (&ecc->p, m, m);
-             if (mpn_cmp (m, ecc->p.m, ecc->p.size) >= 0)
-               mpn_sub_n (m, m, ecc->p.m, ecc->p.size);
-
-             if (mpn_cmp (m, ref, ecc->p.size))
+             if (!mod_equal (&ecc->p, ref, m))
                {
-                 fprintf (stderr, "ecc->p.reduce failed: bit_size = %u\n",
+                 fprintf (stderr, "ecc->p.reduce failed: bit_size = %u, rp == xp\n",
                           ecc->p.bit_size);
                  fprintf (stderr, "a   = ");
                  mpn_out_str (stderr, 16, a, 2*ecc->p.size);
@@ -81,6 +87,21 @@ test_main (void)
                  fprintf (stderr, "\n");
                  abort ();
                }
+             mpn_copyi (m, a, 2*ecc->p.size);
+             ecc->p.reduce (&ecc->p, m + ecc->p.size, m);
+             if (!mod_equal (&ecc->p, ref, m + ecc->p.size))
+               {
+                 fprintf (stderr, "ecc->p.reduce failed: bit_size = %u, rp == xp + size\n",
+                          ecc->p.bit_size);
+                 fprintf (stderr, "a   = ");
+                 mpn_out_str (stderr, 16, a, 2*ecc->p.size);
+                 fprintf (stderr, "\nm   = ");
+                 mpn_out_str (stderr, 16, m + ecc->p.size, ecc->p.size);
+                 fprintf (stderr, " (bad)\nref   = ");
+                 mpn_out_str (stderr, 16, ref, ecc->p.size);
+                 fprintf (stderr, "\n");
+                 abort ();
+               }
            }
          if (ecc->p.redc_size != 0)
            {     
@@ -90,10 +111,7 @@ test_main (void)
              else
                ecc_pp1_redc (&ecc->p, m, m);
 
-             if (mpn_cmp (m, ecc->p.m, ecc->p.size) >= 0)
-               mpn_sub_n (m, m, ecc->p.m, ecc->p.size);
-
-             if (mpn_cmp (m, ref, ecc->p.size))
+             if (!mod_equal (&ecc->p, ref, m))
                {
                  fprintf (stderr, "ecc_p%c1_redc failed: bit_size = %u\n",
                           (ecc->p.m[0] == 1) ? 'm' : 'p', ecc->p.bit_size);
@@ -106,6 +124,26 @@ test_main (void)
                  fprintf (stderr, "\n");
                  abort ();
                }
+
+             mpn_copyi (m, a, 2*ecc->p.size);
+             if (ecc->p.m[0] == 1)
+               ecc_pm1_redc (&ecc->p, m + ecc->p.size, m);
+             else
+               ecc_pp1_redc (&ecc->p, m + ecc->p.size, m);
+
+             if (!mod_equal (&ecc->p, ref, m + ecc->p.size))
+               {
+                 fprintf (stderr, "ecc_p%c1_redc failed: bit_size = %u\n",
+                          (ecc->p.m[0] == 1) ? 'm' : 'p', ecc->p.bit_size);
+                 fprintf (stderr, "a   = ");
+                 mpn_out_str (stderr, 16, a, 2*ecc->p.size);
+                 fprintf (stderr, "\nm   = ");
+                 mpn_out_str (stderr, 16, m + ecc->p.size, ecc->p.size);
+                 fprintf (stderr, " (bad)\nref = ");
+                 mpn_out_str (stderr, 16, ref, ecc->p.size);
+                 fprintf (stderr, "\n");
+                 abort ();
+               }
            }
        }
     }