1/* gmp_randinit_lc_2exp_size -- initialize a random state with a linear
2   congruential generator of a requested size.
3
4Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
5
6This file is part of the GNU MP Library.
7
8The GNU MP Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MP Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
20
21#include <stdio.h> /* for NULL */
22#include "gmp.h"
23#include "gmp-impl.h"
24
25
26/* Array of LC-schemes, ordered in increasing order of the first
27   member (the 'm2exp' value).  The end of the array is indicated with
28   an entry containing all zeros.  */
29
30/* All multipliers are in the range 0.01*m and 0.99*m, and are
31congruent to 5 (mod 8).
32They all pass the spectral test with Vt >= 2^(30/t) and merit >= 1.
33(Up to and including 196 bits, merit is >= 3.)  */
34
35struct __gmp_rand_lc_scheme_struct
36{
37  unsigned long int m2exp;	/* Modulus is 2 ^ m2exp. */
38  const char *astr;		/* Multiplier in string form. */
39  unsigned long int c;		/* Addend. */
40};
41
42static const struct __gmp_rand_lc_scheme_struct __gmp_rand_lc_scheme[] =
43{
44  {32, "29CF535",	     1},
45  {33, "51F666D",	     1},
46  {34, "A3D73AD",	     1},
47  {35, "147E5B85",	     1},
48  {36, "28F725C5",	     1},
49  {37, "51EE3105",	     1},
50  {38, "A3DD5CDD",	     1},
51  {39, "147AF833D",	     1},
52  {40, "28F5DA175",	     1},
53  {56, "AA7D735234C0DD",  1},
54  {64, "BAECD515DAF0B49D", 1},
55  {100, "292787EBD3329AD7E7575E2FD", 1},
56  {128, "48A74F367FA7B5C8ACBB36901308FA85", 1},
57  {156, "78A7FDDDC43611B527C3F1D760F36E5D7FC7C45", 1},
58  {196, "41BA2E104EE34C66B3520CE706A56498DE6D44721E5E24F5", 1},
59  {200, "4E5A24C38B981EAFE84CD9D0BEC48E83911362C114F30072C5", 1},
60  {256, "AF66BA932AAF58A071FD8F0742A99A0C76982D648509973DB802303128A14CB5", 1},
61  {0, NULL, 0}			/* End of array. */
62};
63
64int
65gmp_randinit_lc_2exp_size (gmp_randstate_t rstate, mp_bitcnt_t size)
66{
67  const struct __gmp_rand_lc_scheme_struct *sp;
68  mpz_t a;
69
70  /* Pick a scheme.  */
71  for (sp = __gmp_rand_lc_scheme; sp->m2exp != 0; sp++)
72    if (sp->m2exp / 2 >= size)
73      goto found;
74  return 0;
75
76 found:
77  /* Install scheme.  */
78  mpz_init_set_str (a, sp->astr, 16);
79  gmp_randinit_lc_2exp (rstate, a, sp->c, sp->m2exp);
80  mpz_clear (a);
81  return 1;
82}
83