Commit d6c8f92a authored by Niels Möller's avatar Niels Möller
Browse files

Enable ecc-modinv-test, also with mini-gmp.

parent 5d39ab92
2014-10-03 Niels Möller <nisse@lysator.liu.se> 2014-10-03 Niels Möller <nisse@lysator.liu.se>
* testsuite/ecc-mod-test.c [NETTLE_USE_MINI_GMP]: Enable test. * testsuite/ecc-modinv-test.c [NETTLE_USE_MINI_GMP]: Enable test.
(ref_mod): Use mpz_mod and mpz_limbs_copy, instead of mpn_tdiv_qr. (ref_modinv): Use mpz_gcdext, instead of mpn_gcdext.
(test_modulo): Replace gmp_fprintf calls by plain fprintf and (test_modulo): Replace gmp_fprintf calls by plain fprintf and
mpn_out_str. 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 * testsuite/testutils.c (mpn_out_str): New function, needed to
replace uses of gmp_fprintf. replace uses of gmp_fprintf.
......
#include "testutils.h" #include "testutils.h"
#if NETTLE_USE_MINI_GMP
void
test_main (void)
{
SKIP();
}
#else /* ! NETTLE_USE_MINI_GMP */
static int static int
ref_modinv (mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *mp, mp_size_t mn) 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)]; mpz_t g, s, a, m;
mp_limb_t *up = tp; int res;
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;
mpn_copyi (up, ap, mn); mpz_init (g);
mpn_copyi (vp, mp, mn); mpz_init (s);
gn = mpn_gcdext (gp, sp, &sn, up, mn, vp, mn); mpz_roinit_n (a, ap, mn);
if (gn != 1 || gp[0] != 1) mpz_roinit_n (m, mp, mn);
return 0;
if (sn < 0) mpz_gcdext (g, s, NULL, a, m);
mpn_sub (sp, mp, mn, sp, -sn); if (mpz_cmp_ui (g, 1) == 0)
else if (sn < mn) {
/* Zero-pad. */ if (mpz_sgn (s) < 0)
mpn_zero (sp + sn, mn - sn); {
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); mpz_clear (g);
return 1; mpz_clear (s);
return res;
} }
static int static int
...@@ -70,10 +66,11 @@ test_modulo (gmp_randstate_t rands, const char *name, ...@@ -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", fprintf (stderr, "%s->invert failed for zero input (bit size %u):\n",
name, m->bit_size); name, m->bit_size);
gmp_fprintf (stderr, "p = %Nx\n" fprintf (stderr, "p = ");
"t = %Nx (bad)\n", mpn_out_str (stderr, 16, m->m, m->size);
m->m, m->size, fprintf (stderr, "\nt = ");
ai, m->size); mpn_out_str (stderr, 16, ai, m->size);
fprintf (stderr, " (bad)\n");
abort (); abort ();
} }
...@@ -84,10 +81,12 @@ test_modulo (gmp_randstate_t rands, const char *name, ...@@ -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", fprintf (stderr, "%s->invert failed for a = p input (bit size %u):\n",
name, m->bit_size); name, m->bit_size);
gmp_fprintf (stderr, "p = %Nx\n"
"t = %Nx (bad)\n", fprintf (stderr, "p = ");
m->m, m->size, mpn_out_str (stderr, 16, m->m, m->size);
ai, m->size); fprintf (stderr, "\nt = ");
mpn_out_str (stderr, 16, ai, m->size);
fprintf (stderr, " (bad)\n");
abort (); abort ();
} }
...@@ -112,13 +111,15 @@ test_modulo (gmp_randstate_t rands, const char *name, ...@@ -112,13 +111,15 @@ test_modulo (gmp_randstate_t rands, const char *name,
{ {
fprintf (stderr, "%s->invert failed (test %u, bit size %u):\n", fprintf (stderr, "%s->invert failed (test %u, bit size %u):\n",
name, j, m->bit_size); name, j, m->bit_size);
gmp_fprintf (stderr, "a = %Zx\n" fprintf (stderr, "a = ");
"p = %Nx\n" mpz_out_str (stderr, 16, r);
"t = %Nx (bad)\n" fprintf (stderr, "\np = ");
"r = %Nx\n", mpn_out_str (stderr, 16, m->m, m->size);
r, m->m, m->size, fprintf (stderr, "\nt = ");
ai, m->size, mpn_out_str (stderr, 16, ai, m->size);
ref, m->size); fprintf (stderr, " (bad)\nr = ");
mpn_out_str (stderr, 16, ref, m->size);
abort (); abort ();
} }
...@@ -145,4 +146,3 @@ test_main (void) ...@@ -145,4 +146,3 @@ test_main (void)
} }
gmp_randclear (rands); gmp_randclear (rands);
} }
#endif /* ! NETTLE_USE_MINI_GMP */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment