Annotation of OpenXM_contrib/gmp/tests/mpz/t-powm.c, Revision 1.1
1.1 ! ohara 1: /* Test mpz_powm, mpz_mul. mpz_mod, mpz_mod_ui, mpz_div_ui.
! 2:
! 3: Copyright 1991, 1993, 1994, 1996, 1999, 2000, 2001 Free Software Foundation,
! 4: Inc.
! 5:
! 6: This file is part of the GNU MP Library.
! 7:
! 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.
! 12:
! 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.
! 17:
! 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. */
! 22:
! 23: #include <stdio.h>
! 24: #include <stdlib.h>
! 25:
! 26: #include "gmp.h"
! 27: #include "gmp-impl.h"
! 28: #include "tests.h"
! 29:
! 30: void debug_mp _PROTO ((mpz_t, int));
! 31:
! 32: int
! 33: main (int argc, char **argv)
! 34: {
! 35: mpz_t base, exp, mod;
! 36: mpz_t r1, r2, t1, exp2, base2;
! 37: mp_size_t base_size, exp_size, mod_size;
! 38: int i;
! 39: int reps = 200;
! 40: gmp_randstate_ptr rands;
! 41: mpz_t bs;
! 42: unsigned long bsi, size_range;
! 43:
! 44: tests_start ();
! 45: rands = RANDS;
! 46:
! 47: mpz_init (bs);
! 48:
! 49: if (argc == 2)
! 50: reps = atoi (argv[1]);
! 51:
! 52: mpz_init (base);
! 53: mpz_init (exp);
! 54: mpz_init (mod);
! 55: mpz_init (r1);
! 56: mpz_init (r2);
! 57: mpz_init (t1);
! 58: mpz_init (exp2);
! 59: mpz_init (base2);
! 60:
! 61: for (i = 0; i < reps; i++)
! 62: {
! 63: mpz_urandomb (bs, rands, 32);
! 64: size_range = mpz_get_ui (bs) % 13 + 2;
! 65:
! 66: do /* Loop until mathematically well-defined. */
! 67: {
! 68: mpz_urandomb (bs, rands, size_range);
! 69: base_size = mpz_get_ui (bs);
! 70: mpz_rrandomb (base, rands, base_size);
! 71:
! 72: mpz_urandomb (bs, rands, 7L);
! 73: exp_size = mpz_get_ui (bs);
! 74: mpz_rrandomb (exp, rands, exp_size);
! 75: }
! 76: while (mpz_cmp_ui (base, 0) == 0 && mpz_cmp_ui (exp, 0) == 0);
! 77:
! 78: do
! 79: {
! 80: mpz_urandomb (bs, rands, size_range);
! 81: mod_size = mpz_get_ui (bs);
! 82: mpz_rrandomb (mod, rands, mod_size);
! 83: }
! 84: while (mpz_cmp_ui (mod, 0) == 0);
! 85:
! 86: mpz_urandomb (bs, rands, 2);
! 87: bsi = mpz_get_ui (bs);
! 88: if ((bsi & 1) != 0)
! 89: mpz_neg (base, base);
! 90:
! 91: /* printf ("%ld %ld %ld\n", SIZ (base), SIZ (exp), SIZ (mod)); */
! 92:
! 93: mpz_powm (r1, base, exp, mod);
! 94:
! 95: mpz_set_ui (r2, 1);
! 96: mpz_set (base2, base);
! 97: mpz_set (exp2, exp);
! 98:
! 99: mpz_mod (r2, r2, mod); /* needed when exp==0 and mod==1 */
! 100: while (mpz_cmp_ui (exp2, 0) != 0)
! 101: {
! 102: mpz_mod_ui (t1, exp2, 2);
! 103: if (mpz_cmp_ui (t1, 0) != 0)
! 104: {
! 105: mpz_mul (r2, r2, base2);
! 106: mpz_mod (r2, r2, mod);
! 107: }
! 108: mpz_mul (base2, base2, base2);
! 109: mpz_mod (base2, base2, mod);
! 110: mpz_div_ui (exp2, exp2, 2);
! 111: }
! 112:
! 113: if (mpz_cmp (r1, r2) != 0)
! 114: {
! 115: fprintf (stderr, "\nIncorrect results for operands:\n");
! 116: debug_mp (base, -16);
! 117: debug_mp (exp, -16);
! 118: debug_mp (mod, -16);
! 119: fprintf (stderr, "mpz_powm result:\n");
! 120: debug_mp (r1, -16);
! 121: fprintf (stderr, "reference result:\n");
! 122: debug_mp (r2, -16);
! 123: abort ();
! 124: }
! 125: }
! 126:
! 127: mpz_clear (bs);
! 128: mpz_clear (base);
! 129: mpz_clear (exp);
! 130: mpz_clear (mod);
! 131: mpz_clear (r1);
! 132: mpz_clear (r2);
! 133: mpz_clear (t1);
! 134: mpz_clear (exp2);
! 135: mpz_clear (base2);
! 136:
! 137: tests_end ();
! 138: exit (0);
! 139: }
! 140:
! 141: void
! 142: debug_mp (mpz_t x, int base)
! 143: {
! 144: mpz_out_str (stderr, base, x); fputc ('\n', stderr);
! 145: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>