diff options
author | Niels Möller <nisse@lysator.liu.se> | 2020-10-30 15:01:39 +0100 |
---|---|---|
committer | Niels Möller <nisse@lysator.liu.se> | 2020-10-30 20:22:40 +0100 |
commit | f4f5625edb7d899972431b838ac19ced9288f68a (patch) | |
tree | bc18e9bd95b9f940bc6d4229e012c063562e8c9e /testsuite | |
parent | cbf217b15f66a73041e6fe1a108453efd0297d27 (diff) | |
download | nettle-f4f5625edb7d899972431b838ac19ced9288f68a.tar.gz |
Add separate result argument to all mod functions.
* ecc-internal.h (typedef ecc_mod_func): Add separate result
argument. Updated all C implementations and callers.
Diffstat (limited to 'testsuite')
-rw-r--r-- | testsuite/ecc-mod-test.c | 60 | ||||
-rw-r--r-- | testsuite/ecc-redc-test.c | 6 |
2 files changed, 51 insertions, 15 deletions
diff --git a/testsuite/ecc-mod-test.c b/testsuite/ecc-mod-test.c index 41933b6f..8a074c7d 100644 --- a/testsuite/ecc-mod-test.c +++ b/testsuite/ecc-mod-test.c @@ -19,6 +19,15 @@ ref_mod (mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *mp, mp_size_t mn) #define MAX_SIZE (2*MAX_ECC_SIZE) #define COUNT 50000 +/* 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; +} + static void test_one(const char *name, const struct ecc_modulo *m, @@ -33,13 +42,10 @@ test_one(const char *name, ref_mod (ref, a, m->m, m->size); mpn_copyi (t, a, 2*m->size); - m->mod (m, t); - if (mpn_cmp (t, m->m, m->size) >= 0) - mpn_sub_n (t, t, m->m, m->size); - - if (mpn_cmp (t, ref, m->size)) + m->mod (m, t, t); + if (!mod_equal (m, ref, t)) { - fprintf (stderr, "m->mod %s failed: bit_size = %u\n", + fprintf (stderr, "m->mod %s failed: bit_size = %u, rp == xp\n", name, m->bit_size); fprintf (stderr, "a = "); @@ -52,16 +58,30 @@ test_one(const char *name, abort (); } + mpn_copyi (t, a, 2*m->size); + m->mod (m, t + m->size, t); + if (!mod_equal (m, ref, t + m->size)) + { + fprintf (stderr, "m->mod %s failed: bit_size = %u, rp == xp + size\n", + name, m->bit_size); + + fprintf (stderr, "a = "); + mpn_out_str (stderr, 16, a, 2*m->size); + fprintf (stderr, "\nt = "); + mpn_out_str (stderr, 16, t + m->size, m->size); + fprintf (stderr, " (bad)\nref = "); + mpn_out_str (stderr, 16, ref, m->size); + fprintf (stderr, "\n"); + abort (); + } + if (m->B_size < m->size) { mpn_copyi (t, a, 2*m->size); - ecc_mod (m, t); - if (mpn_cmp (t, m->m, m->size) >= 0) - mpn_sub_n (t, t, m->m, m->size); - - if (mpn_cmp (t, ref, m->size)) + ecc_mod (m, t, t); + if (!mod_equal (m, ref, t)) { - fprintf (stderr, "ecc_mod %s failed: bit_size = %u\n", + fprintf (stderr, "ecc_mod %s failed: bit_size = %u, rp == xp\n", name, m->bit_size); fprintf (stderr, "a = "); mpn_out_str (stderr, 16, a, 2*m->size); @@ -72,6 +92,22 @@ test_one(const char *name, fprintf (stderr, "\n"); abort (); } + + mpn_copyi (t, a, 2*m->size); + ecc_mod (m, t + m->size, t); + if (!mod_equal (m, ref, t + m->size)) + { + fprintf (stderr, "ecc_mod %s failed: bit_size = %u, rp == xp + size\n", + name, m->bit_size); + fprintf (stderr, "a = "); + mpn_out_str (stderr, 16, a, 2*m->size); + fprintf (stderr, "\nt = "); + mpn_out_str (stderr, 16, t + m->size, m->size); + fprintf (stderr, " (bad)\nref = "); + mpn_out_str (stderr, 16, ref, m->size); + fprintf (stderr, "\n"); + abort (); + } } } diff --git a/testsuite/ecc-redc-test.c b/testsuite/ecc-redc-test.c index 2d165f43..2c18ea25 100644 --- a/testsuite/ecc-redc-test.c +++ b/testsuite/ecc-redc-test.c @@ -64,7 +64,7 @@ test_main (void) if (ecc->p.reduce != ecc->p.mod) { mpn_copyi (m, a, 2*ecc->p.size); - ecc->p.reduce (&ecc->p, m); + 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); @@ -86,9 +86,9 @@ test_main (void) { mpn_copyi (m, a, 2*ecc->p.size); if (ecc->p.m[0] == 1) - ecc_pm1_redc (&ecc->p, m); + ecc_pm1_redc (&ecc->p, m, m); else - ecc_pp1_redc (&ecc->p, m); + 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); |