1 /* Test mpz_powm, mpz_mul. mpz_mod, mpz_mod_ui, mpz_div_ui.
3 Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Library General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15 License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
38 mpz_t r1, r2, t1, exp2, base2;
39 mp_size_t base_size, exp_size, mod_size;
44 reps = atoi (argv[1]);
55 for (i = 0; i < reps; i++)
57 base_size = urandom () % SIZE - SIZE/2;
58 mpz_random2 (base, base_size);
60 exp_size = urandom () % SIZE;
61 mpz_random2 (exp, exp_size);
63 mod_size = urandom () % SIZE /* - SIZE/2 */;
64 mpz_random2 (mod, mod_size);
65 if (mpz_cmp_ui (mod, 0) == 0)
68 /* This is mathematically undefined. */
69 if (mpz_cmp_ui (base, 0) == 0 && mpz_cmp_ui (exp, 0) == 0)
79 mpz_powm (r1, base, exp, mod);
82 mpz_set (base2, base);
85 mpz_mod (r2, r2, mod); /* needed when exp==0 and mod==1 */
86 while (mpz_cmp_ui (exp2, 0) != 0)
88 mpz_mod_ui (t1, exp2, 2);
89 if (mpz_cmp_ui (t1, 0) != 0)
91 mpz_mul (r2, r2, base2);
92 mpz_mod (r2, r2, mod);
94 mpz_mul (base2, base2, base2);
95 mpz_mod (base2, base2, mod);
96 mpz_div_ui (exp2, exp2, 2);
104 if (mpz_cmp (r1, r2) != 0)
111 dump_abort (dividend, divisor)
112 MP_INT *dividend, *divisor;
114 fprintf (stderr, "ERROR\n");
115 fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
116 fprintf (stderr, "divisor = "); debug_mp (divisor, -16);
124 mpz_out_str (stderr, base, x); fputc ('\n', stderr);