2 Copyright 2000 Free Software Foundation, Inc.
4 This file is part of the GNU MP Library.
6 The GNU MP Library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or (at your
9 option) any later version.
11 The GNU MP Library is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
28 static /**/const char *const rcsid[] = { (char *)rcsid, "\100(#)" msg }
39 printf ("sh_status: signal %d caught. dumping status.\n", sig);
42 mpz_out_str (stdout, 10, a);
46 if (SIGSEGV == sig) /* remove SEGV handler */
47 signal (SIGSEGV, SIG_DFL);
50 /* Input is a modulus (m). We shall find multiplier (a) and adder (c)
51 conforming to the rules found in the first comment block in file
54 Then run a spectral test on the generator and discard any
55 multipliers not passing. */
59 . find a better algorithm than a+=8; bigger jumps perhaps?
64 mpz_true_random (mpz_t s, unsigned long int nbits)
72 for (i = 0; i < nbits; i += 8)
77 fs = fopen ("/dev/random", "r");
78 nread = fread (c, 1, 1, fs);
84 mpz_mul_2exp (s, s, 8);
85 mpz_add_ui (s, s, ((unsigned long int) c[0]) & 0xff);
86 printf ("%d random bits\n", i + 8);
89 mpz_mod_2exp (s, s, nbits);
94 main (int argc, char *argv[])
96 const char usage[] = "usage: findlc [-dv] m2exp [low_merit [high_merit]]\n";
98 int v_lose, m_lose, v_best, m_best;
103 unsigned long int m2exp;
104 #define DIMS 6 /* dimensions run in spectral test */
105 mpf_t v[DIMS-1]; /* spectral test result (there's no v
107 mpf_t f_merit, low_merit, high_merit;
115 for (f = 0; f < DIMS-1; f++)
118 mpf_init_set_d (low_merit, .1);
119 mpf_init_set_d (high_merit, .1);
121 while ((c = getopt (argc, argv, "a:di:hv")) != -1)
124 case 'd': /* debug */
128 case 'v': /* print version */
135 fputs (usage, stderr);
144 fputs (usage, stderr);
148 /* Install signal handler. */
149 if (SIG_ERR == signal (SIGSEGV, sh_status))
151 perror ("signal (SIGSEGV)");
154 if (SIG_ERR == signal (SIGHUP, sh_status))
156 perror ("signal (SIGHUP)");
160 printf ("findlc: version: %s\n", rcsid[1]);
161 m2exp = atol (argv[0]);
162 mpz_init_set_ui (m, 1);
163 mpz_mul_2exp (m, m, m2exp);
165 mpz_out_str (stdout, 16, m);
168 if (argc > 1) /* have low_merit */
169 mpf_set_str (low_merit, argv[1], 0);
170 if (argc > 2) /* have high_merit */
171 mpf_set_str (high_merit, argv[2], 0);
175 fprintf (stderr, "low_merit = ");
176 mpf_out_str (stderr, 10, 2, low_merit);
177 fprintf (stderr, "; high_merit = ");
178 mpf_out_str (stderr, 10, 2, high_merit);
179 fputs ("\n", stderr);
183 mpz_set_si (minus8, -8L);
184 mpz_init_set_ui (acc, 0);
186 mpz_init_set_d (min, 0.01 * pow (2.0, (double) m2exp));
187 mpz_init_set_d (max, 0.99 * pow (2.0, (double) m2exp));
189 mpz_true_random (s, m2exp); /* Start. */
190 mpz_setbit (s, 0); /* Make it odd. */
192 v_best = m_best = 2*(DIMS-1);
195 mpz_add (acc, acc, s);
196 mpz_mod_2exp (acc, acc, m2exp);
198 mpz_and_si (a, acc, -8L);
200 mpz_and (a, acc, minus8);
202 mpz_add_ui (a, a, 5);
203 if (mpz_cmp (a, min) <= 0 || mpz_cmp (a, max) >= 0)
206 spectral_test (v, DIMS, a, m);
207 for (f = 0, v_lose = m_lose = 0, cnt_high_merit = DIMS-1;
210 merit (f_merit, f + 2, v[f], m);
212 if (mpf_cmp_ui (v[f], 1 << (30 / (f + 2) + (f == 2))) < 0)
215 if (mpf_cmp (f_merit, low_merit) < 0)
218 if (mpf_cmp (f_merit, high_merit) >= 0)
222 if (0 == v_lose && 0 == m_lose)
224 mpz_out_str (stdout, 10, a); puts (""); fflush (stdout);
225 if (0 == cnt_high_merit)
226 break; /* leave loop */
231 printf ("best (v_lose=%d; m_lose=%d): ", v_lose, m_lose);
232 mpz_out_str (stdout, 10, a); puts (""); fflush (stdout);
237 printf ("best (v_lose=%d; m_lose=%d): ", v_lose, m_lose);
238 mpz_out_str (stdout, 10, a); puts (""); fflush (stdout);
244 for (f = 0; f < DIMS-1; f++)
247 mpf_clear (low_merit);
248 mpf_clear (high_merit);