Skip to content
Snippets Groups Projects
Commit 8b83e812 authored by Niels Möller's avatar Niels Möller
Browse files

Simplified ecc-modinv-test.

parent eab7abff
Branches
Tags
No related merge requests found
2014-09-22 Niels Möller <nisse@lysator.liu.se> 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 * testsuite/ecc-mod-test.c (test_modulo): New function, testing
one modulo. Replacing... one modulo. Replacing...
(test_curve): ... old function. (test_curve): ... old function.
......
...@@ -37,110 +37,97 @@ ref_modinv (mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *mp, mp_size_t m ...@@ -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 MAX_ECC_SIZE (1 + 521 / GMP_NUMB_BITS)
#define COUNT 500 #define COUNT 500
void static void
test_main (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 a[MAX_ECC_SIZE];
mp_limb_t ai[MAX_ECC_SIZE]; mp_limb_t ai[MAX_ECC_SIZE];
mp_limb_t ref[MAX_ECC_SIZE]; mp_limb_t ref[MAX_ECC_SIZE];
mp_limb_t scratch[ECC_MODINV_ITCH (MAX_ECC_SIZE)]; mp_limb_t scratch[ECC_MODINV_ITCH (MAX_ECC_SIZE)];
unsigned i; unsigned j;
mpz_t r; mpz_t r;
gmp_randinit_default (rands);
mpz_init (r); mpz_init (r);
for (i = 0; ecc_curves[i]; i++)
{
const struct ecc_curve *ecc = ecc_curves[i];
unsigned j;
/* Check behaviour for zero input */ /* Check behaviour for zero input */
mpn_zero (a, ecc->p.size); mpn_zero (a, m->size);
memset (ai, 17, ecc->p.size * sizeof(*ai)); memset (ai, 17, m->size * sizeof(*ai));
ecc->p.invert (&ecc->p, ai, a, scratch); m->invert (m, ai, a, scratch);
if (!mpn_zero_p (ai, ecc->p.size)) if (!mpn_zero_p (ai, m->size))
{ {
fprintf (stderr, "ecc->p.invert failed for zero input (bit size %u):\n", fprintf (stderr, "%s->invert failed for zero input (bit size %u):\n",
ecc->p.bit_size); name, m->bit_size);
gmp_fprintf (stderr, "p = %Nx\n" gmp_fprintf (stderr, "p = %Nx\n"
"t = %Nx (bad)\n", "t = %Nx (bad)\n",
ecc->p.m, ecc->p.size, m->m, m->size,
ai, ecc->p.size); ai, m->size);
abort (); abort ();
} }
/* Check behaviour for a = p */ /* Check behaviour for a = m */
mpn_copyi (a, ecc->p.m, ecc->p.size); mpn_copyi (a, m->m, m->size);
memset (ai, 17, ecc->p.size * sizeof(*ai)); memset (ai, 17, m->size * sizeof(*ai));
ecc->p.invert (&ecc->p, ai, a, scratch); m->invert (m, ai, a, scratch);
if (!mpn_zero_p (ai, ecc->p.size)) if (!mpn_zero_p (ai, m->size))
{ {
fprintf (stderr, "ecc->p.invert failed for a = p input (bit size %u):\n", fprintf (stderr, "%s->invert failed for a = p input (bit size %u):\n",
ecc->p.bit_size); name, m->bit_size);
gmp_fprintf (stderr, "p = %Nx\n" gmp_fprintf (stderr, "p = %Nx\n"
"t = %Nx (bad)\n", "t = %Nx (bad)\n",
ecc->p.m, ecc->p.size, m->m, m->size,
ai, ecc->p.size); ai, m->size);
abort (); abort ();
} }
for (j = 0; j < COUNT; j++) for (j = 0; j < COUNT; j++)
{ {
if (j & 1) if (j & 1)
mpz_rrandomb (r, rands, ecc->p.size * GMP_NUMB_BITS); mpz_rrandomb (r, rands, m->size * GMP_NUMB_BITS);
else else
mpz_urandomb (r, rands, ecc->p.size * GMP_NUMB_BITS); mpz_urandomb (r, rands, m->size * GMP_NUMB_BITS);
mpz_limbs_copy (a, r, ecc->p.size); mpz_limbs_copy (a, r, m->size);
if (!ref_modinv (ref, a, ecc->p.m, ecc->p.size)) if (!ref_modinv (ref, a, m->m, m->size))
{ {
if (verbose) if (verbose)
fprintf (stderr, "Test %u (bit size %u) not invertible.\n", fprintf (stderr, "Test %u (bit size %u) not invertible mod %s.\n",
j, ecc->p.bit_size); j, m->bit_size, name);
continue; continue;
} }
ecc->p.invert (&ecc->p, ai, a, scratch); m->invert (m, ai, a, scratch);
if (mpn_cmp (ref, ai, ecc->p.size)) if (mpn_cmp (ref, ai, m->size))
{ {
fprintf (stderr, "ecc->p.invert failed (test %u, bit size %u):\n", fprintf (stderr, "%s->invert failed (test %u, bit size %u):\n",
j, ecc->p.bit_size); name, j, m->bit_size);
gmp_fprintf (stderr, "a = %Zx\n" gmp_fprintf (stderr, "a = %Zx\n"
"p = %Nx\n" "p = %Nx\n"
"t = %Nx (bad)\n" "t = %Nx (bad)\n"
"r = %Nx\n", "r = %Nx\n",
r, ecc->p.m, ecc->p.size, r, m->m, m->size,
ai, ecc->p.size, ai, m->size,
ref, ecc->p.size); ref, m->size);
abort (); abort ();
} }
mpz_limbs_copy (a, r, ecc->p.size); }
mpz_clear (r);
}
if (!ref_modinv (ref, a, ecc->q.m, ecc->p.size)) void
test_main (void)
{ {
fprintf (stderr, "Test %u (bit size %u) not invertible.\n", gmp_randstate_t rands;
j, ecc->q.bit_size); unsigned i;
continue;
} gmp_randinit_default (rands);
ecc->q.invert (&ecc->q, ai, a, scratch);
if (mpn_cmp (ref, ai, ecc->p.size)) for (i = 0; ecc_curves[i]; i++)
{ {
fprintf (stderr, "ecc->q.invert failed (test %u, bit size %u):\n", test_modulo (rands, "p", &ecc_curves[i]->p);
j, ecc->q.bit_size); test_modulo (rands, "q", &ecc_curves[i]->q);
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 ();
}
}
} }
gmp_randclear (rands); gmp_randclear (rands);
mpz_clear (r);
} }
#endif /* ! NETTLE_USE_MINI_GMP */ #endif /* ! NETTLE_USE_MINI_GMP */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment