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