[BACK]Return to t-powm.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpz / tests

Annotation of OpenXM_contrib/gmp/mpz/tests/t-powm.c, Revision 1.1.1.2

1.1       maekawa     1: /* Test mpz_powm, mpz_mul. mpz_mod, mpz_mod_ui, mpz_div_ui.
                      2:
1.1.1.2 ! maekawa     3: Copyright (C) 1991, 1993, 1994, 1996, 1999, 2000 Free Software Foundation,
        !             4: Inc.
1.1       maekawa     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
1.1.1.2 ! maekawa     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
1.1       maekawa    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
1.1.1.2 ! maekawa    15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    16: License for more details.
                     17:
1.1.1.2 ! maekawa    18: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    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 "gmp.h"
                     25: #include "gmp-impl.h"
                     26: #include "urandom.h"
                     27:
                     28: void debug_mp ();
                     29:
                     30: #ifndef SIZE
1.1.1.2 ! maekawa    31: #define SIZE 50
        !            32: #endif
        !            33:
        !            34: #ifndef EXP_SIZE
        !            35: #define EXP_SIZE 2
1.1       maekawa    36: #endif
                     37:
                     38: main (argc, argv)
                     39:      int argc;
                     40:      char **argv;
                     41: {
                     42:   mpz_t base, exp, mod;
                     43:   mpz_t r1, r2, t1, exp2, base2;
                     44:   mp_size_t base_size, exp_size, mod_size;
                     45:   int i;
1.1.1.2 ! maekawa    46:   int reps = 2500;
1.1       maekawa    47:
                     48:   if (argc == 2)
                     49:      reps = atoi (argv[1]);
                     50:
                     51:   mpz_init (base);
                     52:   mpz_init (exp);
                     53:   mpz_init (mod);
                     54:   mpz_init (r1);
                     55:   mpz_init (r2);
                     56:   mpz_init (t1);
                     57:   mpz_init (exp2);
                     58:   mpz_init (base2);
                     59:
                     60:   for (i = 0; i < reps; i++)
                     61:     {
1.1.1.2 ! maekawa    62:       do
        !            63:        {
        !            64:          base_size = urandom () % 2 * SIZE - SIZE;
        !            65:          mpz_random2 (base, base_size);
        !            66:
        !            67:          exp_size = urandom () % (EXP_SIZE + 1);
        !            68:          mpz_random2 (exp, exp_size);
        !            69:
        !            70:       /* Loop until mathematically well-defined.  */
        !            71:        }
        !            72:       while (mpz_cmp_ui (base, 0) == 0 && mpz_cmp_ui (exp, 0) == 0);
        !            73:
        !            74:       do
        !            75:        {
        !            76:          mod_size = urandom () % SIZE /* - SIZE/2 */;
        !            77:          mpz_random2 (mod, mod_size);
        !            78:        }
        !            79:       while (mpz_cmp_ui (mod, 0) == 0);
1.1       maekawa    80:
                     81: #if 0
                     82:       putc ('\n', stderr);
                     83:       debug_mp (base, -16);
                     84:       debug_mp (exp, -16);
                     85:       debug_mp (mod, -16);
                     86: #endif
                     87:
                     88:       mpz_powm (r1, base, exp, mod);
                     89:
                     90:       mpz_set_ui (r2, 1);
                     91:       mpz_set (base2, base);
                     92:       mpz_set (exp2, exp);
                     93:
                     94:       mpz_mod (r2, r2, mod);   /* needed when exp==0 and mod==1 */
                     95:       while (mpz_cmp_ui (exp2, 0) != 0)
                     96:        {
                     97:          mpz_mod_ui (t1, exp2, 2);
                     98:          if (mpz_cmp_ui (t1, 0) != 0)
                     99:            {
                    100:              mpz_mul (r2, r2, base2);
                    101:              mpz_mod (r2, r2, mod);
                    102:            }
                    103:          mpz_mul (base2, base2, base2);
                    104:          mpz_mod (base2, base2, mod);
                    105:          mpz_div_ui (exp2, exp2, 2);
                    106:        }
                    107:
                    108: #if 0
                    109:       debug_mp (r1, -16);
                    110:       debug_mp (r2, -16);
                    111: #endif
                    112:
                    113:       if (mpz_cmp (r1, r2) != 0)
                    114:        abort ();
                    115:     }
                    116:
                    117:   exit (0);
                    118: }
                    119:
                    120: dump_abort (dividend, divisor)
                    121:      MP_INT *dividend, *divisor;
                    122: {
                    123:   fprintf (stderr, "ERROR\n");
                    124:   fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
                    125:   fprintf (stderr, "divisor  = "); debug_mp (divisor, -16);
                    126:   abort();
                    127: }
                    128:
                    129: void
                    130: debug_mp (x, base)
                    131:      MP_INT *x;
                    132: {
                    133:   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
                    134: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>