diff options
author | Marco Bodrato <bodrato@mail.dm.unipi.it> | 2020-01-31 17:56:25 +0100 |
---|---|---|
committer | Marco Bodrato <bodrato@mail.dm.unipi.it> | 2020-01-31 17:56:25 +0100 |
commit | 1f21b44fa19819ce50a2c11ffd203dc0bf61d61c (patch) | |
tree | 12f91f080a2f98299b0d44c6333eb8a8f650ccb2 /tests/mpz | |
parent | 9dbd64b50d02d4c6c0a7221f2de2f6df8806b3a7 (diff) | |
download | gmp-1f21b44fa19819ce50a2c11ffd203dc0bf61d61c.tar.gz |
tests/mpz/t-powm.c: more tests for the base==2 case.
Diffstat (limited to 'tests/mpz')
-rw-r--r-- | tests/mpz/t-powm.c | 59 |
1 files changed, 55 insertions, 4 deletions
diff --git a/tests/mpz/t-powm.c b/tests/mpz/t-powm.c index 24e00eb8b..1a25ed725 100644 --- a/tests/mpz/t-powm.c +++ b/tests/mpz/t-powm.c @@ -42,6 +42,51 @@ allsizes_seen (unsigned int *allsizes) return 1; } +void +small_2pow (unsigned long reps) +{ + mpz_t du, exp, mod; + mpz_t r1; + unsigned long m, e, r; + mp_limb_t b0 = 2; + + mpz_roinit_n (du, &b0, 1); + mpz_init (exp); + mpz_init (mod); + mpz_init (r1); + + for (m = 3; m * m < reps; m += 2) + { + mpz_set_ui (mod, m); + r = 1; + for (e = 0; e < m; e += 1) + { + mpz_set_ui (exp, e); + mpz_powm (r1, du, exp, mod); + MPZ_CHECK_FORMAT (r1); + if (mpz_cmp_ui (r1, r) != 0) + { + fprintf (stderr, "\nIncorrect result for operands:\n"); + debug_mp (du, -16); + debug_mp (exp, -16); + debug_mp (mod, -16); + fprintf (stderr, "mpz_powm result:\n"); + debug_mp (r1, -16); + fprintf (stderr, "Should be 2 ^ 0x%lx = 0x%lx (mod 0x%lx)\n", e, r, m); + abort (); + } + if (r > (m >> 1)) + r = (r << 1) - m; + else + r = r << 1; + } + } + + mpz_clear (exp); + mpz_clear (mod); + mpz_clear (r1); +} + int main (int argc, char **argv) { @@ -58,6 +103,7 @@ main (int argc, char **argv) tests_start (); TESTS_REPS (reps, argv, argc); + small_2pow ((unsigned int) reps); rands = RANDS; mpz_init (bs); @@ -99,7 +145,7 @@ main (int argc, char **argv) mpz_urandomb (bs, rands, size_range); mod_size = mpz_get_ui (bs) + base_size + 2; if ((i & 8) == 0) - mod_size += (GMP_NUMB_BITS - mod_size) % GMP_NUMB_BITS; + mod_size += GMP_NUMB_BITS - mod_size % GMP_NUMB_BITS; mpz_setbit (mod, mod_size); mpz_sub (base, base, mod); @@ -108,9 +154,14 @@ main (int argc, char **argv) { do /* Loop until mathematically well-defined. */ { - mpz_urandomb (bs, rands, size_range); - base_size = mpz_get_ui (bs); - mpz_rrandomb (base, rands, base_size); + if ((i & 7) == 4) + mpz_set_ui (base, 2); + else + { + mpz_urandomb (bs, rands, size_range); + base_size = mpz_get_ui (bs); + mpz_rrandomb (base, rands, base_size); + } mpz_urandomb (bs, rands, 7L); exp_size = mpz_get_ui (bs); |