~ubuntu-branches/ubuntu/vivid/gcl/vivid

« back to all changes in this revision

Viewing changes to gmp/mpz/tests/t-powm_ui.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2002-03-04 14:29:59 UTC
  • Revision ID: james.westby@ubuntu.com-20020304142959-dey14w08kr7lldu3
Tags: upstream-2.5.0.cvs20020219
ImportĀ upstreamĀ versionĀ 2.5.0.cvs20020219

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test mpz_powm_ui, mpz_mul. mpz_mod, mpz_mod_ui, mpz_div_ui.
 
2
 
 
3
Copyright (C) 1991, 1993, 1994, 1996, 1997 Free Software Foundation, Inc.
 
4
 
 
5
This file is part of the GNU MP Library.
 
6
 
 
7
The GNU MP Library is free software; you can redistribute it and/or modify
 
8
it under the terms of the GNU Lesser General Public License as published by
 
9
the Free Software Foundation; either version 2.1 of the License, or (at your
 
10
option) any later version.
 
11
 
 
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 Lesser General Public
 
15
License for more details.
 
16
 
 
17
You should have received a copy of the GNU Lesser 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. */
 
21
 
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include "gmp.h"
 
26
#include "gmp-impl.h"
 
27
#include "urandom.h"
 
28
 
 
29
size_t
 
30
#if defined(__STDC__)
 
31
  mpz_out_str (FILE *stream, int base, mpz_srcptr x);
 
32
#else
 
33
  mpz_out_str (stream, base, x);
 
34
#endif
 
35
 
 
36
void debug_mp ();
 
37
 
 
38
#ifndef SIZE
 
39
#define SIZE 8
 
40
#endif
 
41
 
 
42
main (argc, argv)
 
43
     int argc;
 
44
     char **argv;
 
45
{
 
46
  mpz_t base, mod;
 
47
  mpz_t r1, r2, base2;
 
48
  mp_size_t base_size, mod_size;
 
49
  mp_limb_t exp;
 
50
  unsigned long int exp2;
 
51
  int i;
 
52
  int reps = 10000;
 
53
 
 
54
  if (argc == 2)
 
55
     reps = atoi (argv[1]);
 
56
 
 
57
  mpz_init (base);
 
58
  mpz_init (mod);
 
59
  mpz_init (r1);
 
60
  mpz_init (r2);
 
61
  mpz_init (base2);
 
62
 
 
63
  for (i = 0; i < reps; i++)
 
64
    {
 
65
      base_size = urandom () % SIZE /* - SIZE/2 */;
 
66
      mpz_random2 (base, base_size);
 
67
 
 
68
      mpn_random2 (&exp, 1);
 
69
 
 
70
      mod_size = urandom () % SIZE /* - SIZE/2 */;
 
71
      mpz_random2 (mod, mod_size);
 
72
      if (mpz_cmp_ui (mod, 0) == 0)
 
73
        continue;
 
74
 
 
75
      /* This is mathematically undefined.  */
 
76
      if (mpz_cmp_ui (base, 0) == 0 && exp == 0)
 
77
        continue;
 
78
 
 
79
#if 0
 
80
      putc ('\n', stderr);
 
81
      debug_mp (base, -16);
 
82
      debug_mp (mod, -16);
 
83
#endif
 
84
 
 
85
      mpz_powm_ui (r1, base, (unsigned long int) exp, mod);
 
86
 
 
87
      mpz_set_ui (r2, 1);
 
88
      mpz_set (base2, base);
 
89
      exp2 = exp;
 
90
 
 
91
      mpz_mod (r2, r2, mod);    /* needed when exp==0 and mod==1 */
 
92
      while (exp2 != 0)
 
93
        {
 
94
          if (exp2 % 2 != 0)
 
95
            {
 
96
              mpz_mul (r2, r2, base2);
 
97
              mpz_mod (r2, r2, mod);
 
98
            }
 
99
          mpz_mul (base2, base2, base2);
 
100
          mpz_mod (base2, base2, mod);
 
101
          exp2 = exp2 / 2;
 
102
        }
 
103
 
 
104
#if 0
 
105
      debug_mp (r1, -16);
 
106
      debug_mp (r2, -16);
 
107
#endif
 
108
 
 
109
      if (mpz_cmp (r1, r2) != 0)
 
110
        abort ();
 
111
    }
 
112
 
 
113
  exit (0);
 
114
}
 
115
 
 
116
dump_abort (dividend, divisor)
 
117
     MP_INT *dividend, *divisor;
 
118
{
 
119
  fprintf (stderr, "ERROR\n");
 
120
  fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
 
121
  fprintf (stderr, "divisor  = "); debug_mp (divisor, -16);
 
122
  abort();
 
123
}
 
124
 
 
125
void
 
126
debug_mp (x, base)
 
127
     MP_INT *x;
 
128
{
 
129
  mpz_out_str (stderr, base, x); fputc ('\n', stderr);
 
130
}