xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpz/t-powm.c (revision b45fa494daa2ba02187711d31a4144faf0993066)
1 /* Test mpz_powm, mpz_mul, mpz_mod, mpz_mod_ui, mpz_div_ui.
2 
3 Copyright 1991, 1993, 1994, 1996, 1999, 2000, 2001, 2009 Free Software
4 Foundation, 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 3 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.  If not, see http://www.gnu.org/licenses/.  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include "gmp.h"
25 #include "gmp-impl.h"
26 #include "tests.h"
27 
28 void debug_mp __GMP_PROTO ((mpz_t, int));
29 
30 int
31 main (int argc, char **argv)
32 {
33   mpz_t base, exp, mod;
34   mpz_t r1, r2, t1, exp2, base2;
35   mp_size_t base_size, exp_size, mod_size;
36   int i;
37   int reps = 1000;
38   gmp_randstate_ptr rands;
39   mpz_t bs;
40   unsigned long bsi, size_range;
41 
42   tests_start ();
43   TESTS_REPS (reps, argv, argc);
44 
45   rands = RANDS;
46 
47   mpz_init (bs);
48 
49   mpz_init (base);
50   mpz_init (exp);
51   mpz_init (mod);
52   mpz_init (r1);
53   mpz_init (r2);
54   mpz_init (t1);
55   mpz_init (exp2);
56   mpz_init (base2);
57 
58   for (i = 0; i < reps; i++)
59     {
60       mpz_urandomb (bs, rands, 32);
61       size_range = mpz_get_ui (bs) % 13 + 2;
62 
63       do  /* Loop until mathematically well-defined.  */
64 	{
65 	  mpz_urandomb (bs, rands, size_range);
66 	  base_size = mpz_get_ui (bs);
67 	  mpz_rrandomb (base, rands, base_size);
68 
69 	  mpz_urandomb (bs, rands, 7L);
70 	  exp_size = mpz_get_ui (bs);
71 	  mpz_rrandomb (exp, rands, exp_size);
72 	}
73       while (mpz_cmp_ui (base, 0) == 0 && mpz_cmp_ui (exp, 0) == 0);
74 
75       do
76         {
77 	  mpz_urandomb (bs, rands, size_range);
78 	  mod_size = mpz_get_ui (bs);
79 	  mpz_rrandomb (mod, rands, mod_size);
80 	}
81       while (mpz_cmp_ui (mod, 0) == 0);
82 
83       mpz_urandomb (bs, rands, 2);
84       bsi = mpz_get_ui (bs);
85       if ((bsi & 1) != 0)
86 	mpz_neg (base, base);
87 
88       /* printf ("%ld %ld %ld\n", SIZ (base), SIZ (exp), SIZ (mod)); */
89 
90       mpz_set_ui (r2, 1);
91       mpz_mod (base2, base, mod);
92       mpz_set (exp2, exp);
93       mpz_mod (r2, r2, mod);
94 
95       for (;;)
96 	{
97 	  if (mpz_tstbit (exp2, 0))
98 	    {
99 	      mpz_mul (r2, r2, base2);
100 	      mpz_mod (r2, r2, mod);
101 	    }
102 	  if  (mpz_cmp_ui (exp2, 1) <= 0)
103 	    break;
104 	  mpz_mul (base2, base2, base2);
105 	  mpz_mod (base2, base2, mod);
106 	  mpz_tdiv_q_2exp (exp2, exp2, 1);
107 	}
108 
109       mpz_powm (r1, base, exp, mod);
110       MPZ_CHECK_FORMAT (r1);
111 
112       if (mpz_cmp (r1, r2) != 0)
113 	{
114 	  fprintf (stderr, "\nIncorrect results in test %d for operands:\n", i);
115 	  debug_mp (base, -16);
116 	  debug_mp (exp, -16);
117 	  debug_mp (mod, -16);
118 	  fprintf (stderr, "mpz_powm result:\n");
119 	  debug_mp (r1, -16);
120 	  fprintf (stderr, "reference result:\n");
121 	  debug_mp (r2, -16);
122 	  abort ();
123 	}
124 
125       if (mpz_tdiv_ui (mod, 2) == 0)
126 	continue;
127 
128       mpz_powm_sec (r1, base, exp, mod);
129       MPZ_CHECK_FORMAT (r1);
130 
131       if (mpz_cmp (r1, r2) != 0)
132 	{
133 	  fprintf (stderr, "\nIncorrect results in test %d for operands:\n", i);
134 	  debug_mp (base, -16);
135 	  debug_mp (exp, -16);
136 	  debug_mp (mod, -16);
137 	  fprintf (stderr, "mpz_powm_sec result:\n");
138 	  debug_mp (r1, -16);
139 	  fprintf (stderr, "reference result:\n");
140 	  debug_mp (r2, -16);
141 	  abort ();
142 	}
143     }
144 
145   mpz_clear (bs);
146   mpz_clear (base);
147   mpz_clear (exp);
148   mpz_clear (mod);
149   mpz_clear (r1);
150   mpz_clear (r2);
151   mpz_clear (t1);
152   mpz_clear (exp2);
153   mpz_clear (base2);
154 
155   tests_end ();
156   exit (0);
157 }
158 
159 void
160 debug_mp (mpz_t x, int base)
161 {
162   mpz_out_str (stderr, base, x); fputc ('\n', stderr);
163 }
164