1
/* Test mpz_powm, mpz_mul. mpz_mod, mpz_mod_ui, mpz_div_ui.
3
Copyright 1991, 1993, 1994, 1996, 1999, 2000, 2001 Free Software Foundation,
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 2.1 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; see the file COPYING.LIB. If not, write to
20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21
MA 02111-1307, USA. */
30
void debug_mp _PROTO ((mpz_t, int));
33
main (int argc, char **argv)
36
mpz_t r1, r2, t1, exp2, base2;
37
mp_size_t base_size, exp_size, mod_size;
40
gmp_randstate_ptr rands;
42
unsigned long bsi, size_range;
50
reps = atoi (argv[1]);
61
for (i = 0; i < reps; i++)
63
mpz_urandomb (bs, rands, 32);
64
size_range = mpz_get_ui (bs) % 13 + 2;
66
do /* Loop until mathematically well-defined. */
68
mpz_urandomb (bs, rands, size_range);
69
base_size = mpz_get_ui (bs);
70
mpz_rrandomb (base, rands, base_size);
72
mpz_urandomb (bs, rands, 7L);
73
exp_size = mpz_get_ui (bs);
74
mpz_rrandomb (exp, rands, exp_size);
76
while (mpz_cmp_ui (base, 0) == 0 && mpz_cmp_ui (exp, 0) == 0);
80
mpz_urandomb (bs, rands, size_range);
81
mod_size = mpz_get_ui (bs);
82
mpz_rrandomb (mod, rands, mod_size);
84
while (mpz_cmp_ui (mod, 0) == 0);
86
mpz_urandomb (bs, rands, 2);
87
bsi = mpz_get_ui (bs);
91
/* printf ("%ld %ld %ld\n", SIZ (base), SIZ (exp), SIZ (mod)); */
93
mpz_powm (r1, base, exp, mod);
96
mpz_set (base2, base);
99
mpz_mod (r2, r2, mod); /* needed when exp==0 and mod==1 */
100
while (mpz_cmp_ui (exp2, 0) != 0)
102
mpz_mod_ui (t1, exp2, 2);
103
if (mpz_cmp_ui (t1, 0) != 0)
105
mpz_mul (r2, r2, base2);
106
mpz_mod (r2, r2, mod);
108
mpz_mul (base2, base2, base2);
109
mpz_mod (base2, base2, mod);
110
mpz_div_ui (exp2, exp2, 2);
113
if (mpz_cmp (r1, r2) != 0)
115
fprintf (stderr, "\nIncorrect results for operands:\n");
116
debug_mp (base, -16);
119
fprintf (stderr, "mpz_powm result:\n");
121
fprintf (stderr, "reference result:\n");
142
debug_mp (mpz_t x, int base)
144
mpz_out_str (stderr, base, x); fputc ('\n', stderr);