aboutsummaryrefslogtreecommitdiff
path: root/vendor/gmp-6.3.0/tests/rand/t-lc2exp.c
blob: c66691de0cf177c298406766295e7637111fce8c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Exercise the lc2exp random functions.

Copyright 2002, 2011 Free Software Foundation, Inc.

This file is part of the GNU MP Library test suite.

The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.

The GNU MP Library test suite is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */

#include <stdio.h>
#include <stdlib.h>
#include "gmp-impl.h"
#include "tests.h"


/* a=0 and c=0 produces zero results always. */
void
check_zero (unsigned long m2exp)
{
  gmp_randstate_t  r;
  mpz_t            a;
  unsigned long    c;
  int              i;

  mpz_init_set_ui (a, 0L);
  c = 0L;

  gmp_randinit_lc_2exp (r, a, c, m2exp);
  gmp_randseed_ui (r, 0L);

  for (i = 0; i < 5; i++)
    {
      mpz_urandomb (a, r, 123L);
      if (mpz_sgn (a) != 0)
        {
          printf ("check_zero m2exp=%lu: didn't get zero\n", m2exp);
          gmp_printf ("  rand=%#Zx\n", a);
          abort ();
        }
    }

  mpz_clear (a);
  gmp_randclear (r);
}

/* negative a */
void
check_nega (void)
{
  gmp_randstate_t  r;
  mpz_t            a;
  unsigned long    c, m2exp;
  int              i;

  mpz_init (a);
  mpz_setbit (a, 1000L);
  mpz_neg (a, a);
  c = 0L;
  m2exp = 45L;

  gmp_randinit_lc_2exp (r, a, c, m2exp);
  gmp_randseed_ui (r, 0L);

  for (i = 0; i < 5; i++)
    {
      mpz_urandomb (a, r, 123L);
      if (mpz_sgn (a) != 0)
        printf ("check_nega m2exp=%lu: didn't get zero\n", m2exp);
    }

  mpz_clear (a);
  gmp_randclear (r);
}

void
check_bigc (void)
{
  gmp_randstate_t  r;
  mpz_t            a;
  unsigned long    c, m2exp, bits;
  int              i;

  mpz_init_set_ui (a, 0L);
  c = ULONG_MAX;
  m2exp = 8;

  gmp_randinit_lc_2exp (r, a, c, m2exp);
  gmp_randseed_ui (r, 0L);

  for (i = 0; i < 20; i++)
    {
      bits = 123L;
      mpz_urandomb (a, r, bits);
      if (mpz_sgn (a) < 0 || mpz_sizeinbase (a, 2) > bits)
        {
          printf     ("check_bigc: mpz_urandomb out of range\n");
          printf     ("   m2exp=%lu\n", m2exp);
          gmp_printf ("   rand=%#ZX\n", a);
          gmp_printf ("   sizeinbase2=%u\n", mpz_sizeinbase (a, 2));
	  abort ();
        }
    }

  mpz_clear (a);
  gmp_randclear (r);
}

void
check_bigc1 (void)
{
  gmp_randstate_t  r;
  mpz_t            a;
  unsigned long    c, m2exp;
  int              i;

  mpz_init_set_ui (a, 0L);
  c = ULONG_MAX;
  m2exp = 2;

  gmp_randinit_lc_2exp (r, a, c, m2exp);
  gmp_randseed_ui (r, 0L);

  for (i = 0; i < 20; i++)
    {
      mpz_urandomb (a, r, 1L);
      if (mpz_cmp_ui (a, 1L) != 0)
        {
          printf     ("check_bigc1: mpz_urandomb didn't give 1\n");
          printf     ("   m2exp=%lu\n", m2exp);
          gmp_printf ("   got rand=%#ZX\n", a);
          abort ();
        }
    }

  mpz_clear (a);
  gmp_randclear (r);
}

/* Checks parameters which triggered an assertion failure in the past.
   Happened when limbs(a)+limbs(c) < bits_to_limbs(m2exp).  */
void
check_bigm (void)
{
  gmp_randstate_t rstate;
  mpz_t a;

  mpz_init_set_ui (a, 5L);
  gmp_randinit_lc_2exp (rstate, a, 1L, 384L);

  mpz_urandomb (a, rstate, 20L);

  gmp_randclear (rstate);
  mpz_clear (a);
}

/* Checks for seeds bigger than the modulus.  */
void
check_bigs (void)
{
  gmp_randstate_t rstate;
  mpz_t sd, a;
  int i;

  mpz_init (sd);
  mpz_setbit (sd, 300L);
  mpz_sub_ui (sd, sd, 1L);
  mpz_clrbit (sd, 13L);
  mpz_init_set_ui (a, 123456789L);

  gmp_randinit_lc_2exp (rstate, a, 5L, 64L);

  for (i = 0; i < 20; i++)
    {
      mpz_neg (sd, sd);
      gmp_randseed (rstate, sd);
      mpz_mul_ui (sd, sd, 7L);

      mpz_urandomb (a, rstate, 80L);
    }

  gmp_randclear (rstate);
  mpz_clear (a);
  mpz_clear (sd);
}

int
main (void)
{
  tests_start ();

  check_zero (2L);
  check_zero (7L);
  check_zero (32L);
  check_zero (64L);
  check_zero (1000L);

  check_nega ();
  check_bigc ();
  check_bigc1 ();

  check_bigm ();
  check_bigs ();

  tests_end ();
  exit (0);
}