1 /* Test mpz_powm_ui, mpz_mul, mpz_mod.
3 Copyright 1991, 1993, 1994, 1996, 1997, 2000, 2001, 2002 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 dump_abort __GMP_PROTO ((mpz_t, mpz_t));
29 void debug_mp __GMP_PROTO ((mpz_t, int));
32 main (int argc, char **argv)
36 mp_size_t base_size, exp_size, mod_size;
37 unsigned long int exp2;
40 gmp_randstate_ptr rands;
42 unsigned long bsi, size_range;
50 reps = atoi (argv[1]);
59 for (i = 0; i < reps; i++)
61 mpz_urandomb (bs, rands, 32);
62 size_range = mpz_get_ui (bs) % 13 + 2;
64 do /* Loop until mathematically well-defined. */
66 mpz_urandomb (bs, rands, size_range);
67 base_size = mpz_get_ui (bs);
68 mpz_rrandomb (base, rands, base_size);
70 mpz_urandomb (bs, rands, 6L);
71 exp_size = mpz_get_ui (bs);
72 mpz_rrandomb (exp, rands, exp_size);
73 exp2 = mpz_getlimbn (exp, (mp_size_t) 0);
75 while (mpz_cmp_ui (base, 0) == 0 && exp2 == 0);
79 mpz_urandomb (bs, rands, size_range);
80 mod_size = mpz_get_ui (bs);
81 mpz_rrandomb (mod, rands, mod_size);
83 while (mpz_cmp_ui (mod, 0) == 0);
85 mpz_urandomb (bs, rands, 2);
86 bsi = mpz_get_ui (bs);
90 /* printf ("%ld %ld\n", SIZ (base), SIZ (mod)); */
98 mpz_powm_ui (r1, base, exp2, mod);
99 MPZ_CHECK_FORMAT (r1);
102 mpz_set (base2, base);
104 mpz_mod (r2, r2, mod); /* needed when exp==0 and mod==1 */
109 mpz_mul (r2, r2, base2);
110 mpz_mod (r2, r2, mod);
112 mpz_mul (base2, base2, base2);
113 mpz_mod (base2, base2, mod);
122 if (mpz_cmp (r1, r2) != 0)
124 fprintf (stderr, "\ntest %d: Incorrect results for operands:\n", i);
125 debug_mp (base, -16);
128 fprintf (stderr, "mpz_powm_ui result:\n");
130 fprintf (stderr, "reference result:\n");
149 dump_abort (mpz_t dividend, mpz_t divisor)
151 fprintf (stderr, "ERROR\n");
152 fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
153 fprintf (stderr, "divisor = "); debug_mp (divisor, -16);
158 debug_mp (mpz_t x, int base)
160 mpz_out_str (stderr, base, x); fputc ('\n', stderr);