1 /* Test mpz_powm, mpz_mul, mpz_mod, mpz_mod_ui, mpz_div_ui.
3 Copyright 1991, 1993, 1994, 1996, 1999, 2000, 2001, 2009 Free Software
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
28 void debug_mp __GMP_PROTO ((mpz_t, int));
31 main (int argc, char **argv)
34 mpz_t r1, r2, t1, exp2, base2;
35 mp_size_t base_size, exp_size, mod_size;
38 gmp_randstate_ptr rands;
40 unsigned long bsi, size_range;
43 TESTS_REPS (reps, argv, argc);
58 for (i = 0; i < reps; i++)
60 mpz_urandomb (bs, rands, 32);
61 size_range = mpz_get_ui (bs) % 13 + 2;
63 do /* Loop until mathematically well-defined. */
65 mpz_urandomb (bs, rands, size_range);
66 base_size = mpz_get_ui (bs);
67 mpz_rrandomb (base, rands, base_size);
69 mpz_urandomb (bs, rands, 7L);
70 exp_size = mpz_get_ui (bs);
71 mpz_rrandomb (exp, rands, exp_size);
73 while (mpz_cmp_ui (base, 0) == 0 && mpz_cmp_ui (exp, 0) == 0);
77 mpz_urandomb (bs, rands, size_range);
78 mod_size = mpz_get_ui (bs);
79 mpz_rrandomb (mod, rands, mod_size);
81 while (mpz_cmp_ui (mod, 0) == 0);
83 mpz_urandomb (bs, rands, 2);
84 bsi = mpz_get_ui (bs);
88 /* printf ("%ld %ld %ld\n", SIZ (base), SIZ (exp), SIZ (mod)); */
91 mpz_mod (base2, base, mod);
93 mpz_mod (r2, r2, mod);
97 if (mpz_tstbit (exp2, 0))
99 mpz_mul (r2, r2, base2);
100 mpz_mod (r2, r2, mod);
102 if (mpz_cmp_ui (exp2, 1) <= 0)
104 mpz_mul (base2, base2, base2);
105 mpz_mod (base2, base2, mod);
106 mpz_tdiv_q_2exp (exp2, exp2, 1);
109 mpz_powm (r1, base, exp, mod);
110 MPZ_CHECK_FORMAT (r1);
112 if (mpz_cmp (r1, r2) != 0)
114 fprintf (stderr, "\nIncorrect results in test %d for operands:\n", i);
115 debug_mp (base, -16);
118 fprintf (stderr, "mpz_powm result:\n");
120 fprintf (stderr, "reference result:\n");
125 if (mpz_tdiv_ui (mod, 2) == 0)
128 mpz_powm_sec (r1, base, exp, mod);
129 MPZ_CHECK_FORMAT (r1);
131 if (mpz_cmp (r1, r2) != 0)
133 fprintf (stderr, "\nIncorrect results in test %d for operands:\n", i);
134 debug_mp (base, -16);
137 fprintf (stderr, "mpz_powm_sec result:\n");
139 fprintf (stderr, "reference result:\n");
160 debug_mp (mpz_t x, int base)
162 mpz_out_str (stderr, base, x); fputc ('\n', stderr);