14a1767b4Smrg /* t-rand -- Test random number generators. */
24a1767b4Smrg
34a1767b4Smrg /*
44a1767b4Smrg Copyright 2000, 2001 Free Software Foundation, Inc.
54a1767b4Smrg
6d25e02daSmrg This file is part of the GNU MP Library test suite.
74a1767b4Smrg
8d25e02daSmrg The GNU MP Library test suite is free software; you can redistribute it
9d25e02daSmrg and/or modify it under the terms of the GNU General Public License as
10d25e02daSmrg published by the Free Software Foundation; either version 3 of the License,
11d25e02daSmrg or (at your option) any later version.
124a1767b4Smrg
13d25e02daSmrg The GNU MP Library test suite is distributed in the hope that it will be
14d25e02daSmrg useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15d25e02daSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16d25e02daSmrg Public License for more details.
174a1767b4Smrg
18d25e02daSmrg You should have received a copy of the GNU General Public License along with
19*f81b1c5bSmrg the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
204a1767b4Smrg
214a1767b4Smrg #include <stdlib.h>
224a1767b4Smrg #include <stdio.h>
234a1767b4Smrg #include "gmp.h"
244a1767b4Smrg
254a1767b4Smrg #define SEED 1
264a1767b4Smrg #define BASE 16
274a1767b4Smrg #define ENTS 10 /* Number of entries in array when
284a1767b4Smrg printing. */
294a1767b4Smrg
304a1767b4Smrg /* These were generated by this very program. Do not edit! */
314a1767b4Smrg /* Integers. */
32d25e02daSmrg const char *z1[ENTS] = {"0", "1", "1", "1", "1", "0", "1", "1", "1", "1"};
33d25e02daSmrg const char *z2[ENTS] = {"0", "3", "1", "3", "3", "0", "3", "3", "3", "1"};
34d25e02daSmrg const char *z3[ENTS] = {"4", "3", "1", "7", "3", "0", "3", "3", "3", "1"};
35d25e02daSmrg const char *z4[ENTS] = {"c", "3", "1", "f", "b", "8", "3", "3", "3", "1"};
36d25e02daSmrg const char *z5[ENTS] = {"1c", "13", "11", "1f", "b", "18", "3", "13", "3", "1"};
374a1767b4Smrg
38d25e02daSmrg const char *z10[ENTS] = {"29c", "213", "f1", "17f", "12b", "178", "383", "d3", "3a3", "281"};
394a1767b4Smrg
40d25e02daSmrg const char *z15[ENTS] = {"29c", "1a13", "74f1", "257f", "592b", "4978", "4783", "7cd3", "5ba3", "4681"};
41d25e02daSmrg const char *z16[ENTS] = {"29c", "9a13", "74f1", "a57f", "d92b", "4978", "c783", "fcd3", "5ba3", "c681"};
42d25e02daSmrg const char *z17[ENTS] = {"51e", "f17a", "54ff", "1a335", "cf65", "5d6f", "583f", "618f", "1bc6", "98ff"};
434a1767b4Smrg
44d25e02daSmrg const char *z31[ENTS] = {"3aecd515", "13ae8ec6", "518c8090", "81ca077", "70b7134", "7ee78d71", "323a7636", "2122cb1a", "19811941", "41fd605"};
45d25e02daSmrg const char *z32[ENTS] = {"baecd515", "13ae8ec6", "518c8090", "881ca077", "870b7134", "7ee78d71", "323a7636", "a122cb1a", "99811941", "841fd605"};
46d25e02daSmrg const char *z33[ENTS] = {"1faf4cca", "15d6ef83b", "9095fe72", "1b6a3dff6", "b17cbddd", "16e5209d4", "6f65b12c", "493bbbc6", "abf2a5d5", "6d491a3c"};
474a1767b4Smrg
48d25e02daSmrg const char *z63[ENTS] = {"48a74f367fa7b5c8", "3ba9e9dc1b263076", "1e0ac84e7678e0fb", "11416581728b3e35", "36ab610523f0f1f7", "3e540e8e95c0eb4b", "439ae16057dbc9d3", "734fb260db243950", "7d3a317effc289bf", "1d80301fb3d1a0d1"};
49d25e02daSmrg const char *z64[ENTS] = {"48a74f367fa7b5c8", "bba9e9dc1b263076", "9e0ac84e7678e0fb", "11416581728b3e35", "b6ab610523f0f1f7", "be540e8e95c0eb4b", "439ae16057dbc9d3", "f34fb260db243950", "fd3a317effc289bf", "1d80301fb3d1a0d1"};
50d25e02daSmrg const char *z65[ENTS] = {"1ff77710d846d49f0", "1b1411701d709ee10", "31ffa81a208b6af4", "446638d431d3c681", "df5c569d5baa8b55", "197d99ea9bf28e5a0", "191ade09edd94cfae", "194acefa6dde5e18d", "1afc1167c56272d92", "d092994da72f206f"};
514a1767b4Smrg
52d25e02daSmrg const char *z127[ENTS] = {"2f66ba932aaf58a071fd8f0742a99a0c", "73cfa3c664c9c1753507ca60ec6b8425", "53ea074ca131dec12cd68b8aa8e20278", "3cf5ac8c343532f8a53cc0eb47581f73", "50c11d5869e208aa1b9aa317b8c2d0a9", "b23163c892876472b1ef19642eace09", "489f4c03d41f87509c8d6c90ce674f95", "2ab8748c96aa6762ea1932b44c9d7164", "98cb5591fc05ad31afbbc1d67b90edd", "77848bb991fd0be331adcf1457fbc672"};
53d25e02daSmrg const char *z128[ENTS] = {"af66ba932aaf58a071fd8f0742a99a0c", "73cfa3c664c9c1753507ca60ec6b8425", "53ea074ca131dec12cd68b8aa8e20278", "3cf5ac8c343532f8a53cc0eb47581f73", "50c11d5869e208aa1b9aa317b8c2d0a9", "8b23163c892876472b1ef19642eace09", "489f4c03d41f87509c8d6c90ce674f95", "aab8748c96aa6762ea1932b44c9d7164", "98cb5591fc05ad31afbbc1d67b90edd", "f7848bb991fd0be331adcf1457fbc672"};
544a1767b4Smrg
554a1767b4Smrg /* Floats. */
56d25e02daSmrg const char *f1[ENTS] = {"0.@0", "0.8@0", "0.8@0", "0.8@0", "0.8@0", "0.@0", "0.8@0", "0.8@0", "0.8@0", "0.8@0"};
57d25e02daSmrg const char *f2[ENTS] = {"0.@0", "0.c@0", "0.4@0", "0.c@0", "0.c@0", "0.@0", "0.c@0", "0.c@0", "0.c@0", "0.4@0"};
58d25e02daSmrg const char *f3[ENTS] = {"0.8@0", "0.6@0", "0.2@0", "0.e@0", "0.6@0", "0.@0", "0.6@0", "0.6@0", "0.6@0", "0.2@0"};
59d25e02daSmrg const char *f4[ENTS] = {"0.c@0", "0.3@0", "0.1@0", "0.f@0", "0.b@0", "0.8@0", "0.3@0", "0.3@0", "0.3@0", "0.1@0"};
60d25e02daSmrg const char *f5[ENTS] = {"0.e@0", "0.98@0", "0.88@0", "0.f8@0", "0.58@0", "0.c@0", "0.18@0", "0.98@0", "0.18@0", "0.8@-1"};
614a1767b4Smrg
62d25e02daSmrg const char *f10[ENTS] = {"0.a7@0", "0.84c@0", "0.3c4@0", "0.5fc@0", "0.4ac@0", "0.5e@0", "0.e0c@0", "0.34c@0", "0.e8c@0", "0.a04@0"};
634a1767b4Smrg
64d25e02daSmrg const char *f15[ENTS] = {"0.538@-1", "0.3426@0", "0.e9e2@0", "0.4afe@0", "0.b256@0", "0.92f@0", "0.8f06@0", "0.f9a6@0", "0.b746@0", "0.8d02@0"};
65d25e02daSmrg const char *f16[ENTS] = {"0.29c@-1", "0.9a13@0", "0.74f1@0", "0.a57f@0", "0.d92b@0", "0.4978@0", "0.c783@0", "0.fcd3@0", "0.5ba3@0", "0.c681@0"};
66d25e02daSmrg const char *f17[ENTS] = {"0.28f@-1", "0.78bd@0", "0.2a7f8@0", "0.d19a8@0", "0.67b28@0", "0.2eb78@0", "0.2c1f8@0", "0.30c78@0", "0.de3@-1", "0.4c7f8@0"};
674a1767b4Smrg
68d25e02daSmrg const char *f31[ENTS] = {"0.75d9aa2a@0", "0.275d1d8c@0", "0.a319012@0", "0.103940ee@0", "0.e16e268@-1", "0.fdcf1ae2@0", "0.6474ec6c@0", "0.42459634@0", "0.33023282@0", "0.83fac0a@-1"};
69d25e02daSmrg const char *f32[ENTS] = {"0.baecd515@0", "0.13ae8ec6@0", "0.518c809@0", "0.881ca077@0", "0.870b7134@0", "0.7ee78d71@0", "0.323a7636@0", "0.a122cb1a@0", "0.99811941@0", "0.841fd605@0"};
70d25e02daSmrg const char *f33[ENTS] = {"0.fd7a665@-1", "0.aeb77c1d8@0", "0.484aff39@0", "0.db51effb@0", "0.58be5eee8@0", "0.b72904ea@0", "0.37b2d896@0", "0.249ddde3@0", "0.55f952ea8@0", "0.36a48d1e@0"};
714a1767b4Smrg
72d25e02daSmrg const char *f63[ENTS] = {"0.914e9e6cff4f6b9@0", "0.7753d3b8364c60ec@0", "0.3c15909cecf1c1f6@0", "0.2282cb02e5167c6a@0", "0.6d56c20a47e1e3ee@0", "0.7ca81d1d2b81d696@0", "0.8735c2c0afb793a6@0", "0.e69f64c1b64872a@0", "0.fa7462fdff85137e@0", "0.3b00603f67a341a2@0"};
73d25e02daSmrg const char *f64[ENTS] = {"0.48a74f367fa7b5c8@0", "0.bba9e9dc1b263076@0", "0.9e0ac84e7678e0fb@0", "0.11416581728b3e35@0", "0.b6ab610523f0f1f7@0", "0.be540e8e95c0eb4b@0", "0.439ae16057dbc9d3@0", "0.f34fb260db24395@0", "0.fd3a317effc289bf@0", "0.1d80301fb3d1a0d1@0"};
74d25e02daSmrg const char *f65[ENTS] = {"0.ffbbb886c236a4f8@0", "0.d8a08b80eb84f708@0", "0.18ffd40d1045b57a@0", "0.22331c6a18e9e3408@0", "0.6fae2b4eadd545aa8@0", "0.cbeccf54df9472d@0", "0.c8d6f04f6eca67d7@0", "0.ca5677d36ef2f0c68@0", "0.d7e08b3e2b1396c9@0", "0.68494ca6d39790378@0"};
754a1767b4Smrg
76d25e02daSmrg const char *f127[ENTS] = {"0.5ecd7526555eb140e3fb1e0e85533418@0", "0.e79f478cc99382ea6a0f94c1d8d7084a@0", "0.a7d40e994263bd8259ad171551c404f@0", "0.79eb5918686a65f14a7981d68eb03ee6@0", "0.a1823ab0d3c411543735462f7185a152@0", "0.16462c791250ec8e563de32c85d59c12@0", "0.913e9807a83f0ea1391ad9219cce9f2a@0", "0.5570e9192d54cec5d4326568993ae2c8@0", "0.13196ab23f80b5a635f7783acf721dba@0", "0.ef09177323fa17c6635b9e28aff78ce4@0"};
77d25e02daSmrg const char *f128[ENTS] = {"0.af66ba932aaf58a071fd8f0742a99a0c@0", "0.73cfa3c664c9c1753507ca60ec6b8425@0", "0.53ea074ca131dec12cd68b8aa8e20278@0", "0.3cf5ac8c343532f8a53cc0eb47581f73@0", "0.50c11d5869e208aa1b9aa317b8c2d0a9@0", "0.8b23163c892876472b1ef19642eace09@0", "0.489f4c03d41f87509c8d6c90ce674f95@0", "0.aab8748c96aa6762ea1932b44c9d7164@0", "0.98cb5591fc05ad31afbbc1d67b90edd@-1", "0.f7848bb991fd0be331adcf1457fbc672@0"};
784a1767b4Smrg
794a1767b4Smrg
804a1767b4Smrg struct rt
814a1767b4Smrg {
82d25e02daSmrg const char **s;
834a1767b4Smrg int nbits;
844a1767b4Smrg };
854a1767b4Smrg
864a1767b4Smrg static struct rt zarr[] =
874a1767b4Smrg {
884a1767b4Smrg {z1, 1},
894a1767b4Smrg {z2, 2},
904a1767b4Smrg {z3, 3},
914a1767b4Smrg {z4, 4},
924a1767b4Smrg {z5, 5},
934a1767b4Smrg {z10, 10},
944a1767b4Smrg {z15, 15},
954a1767b4Smrg {z16, 16},
964a1767b4Smrg {z17, 17},
974a1767b4Smrg {z31, 31},
984a1767b4Smrg {z32, 32},
994a1767b4Smrg {z33, 33},
1004a1767b4Smrg {z63, 63},
1014a1767b4Smrg {z64, 64},
1024a1767b4Smrg {z65, 65},
1034a1767b4Smrg {z127, 127},
1044a1767b4Smrg {z128, 128},
1054a1767b4Smrg {NULL, 0}
1064a1767b4Smrg };
1074a1767b4Smrg
1084a1767b4Smrg static struct rt farr[] =
1094a1767b4Smrg {
1104a1767b4Smrg {f1, 1},
1114a1767b4Smrg {f2, 2},
1124a1767b4Smrg {f3, 3},
1134a1767b4Smrg {f4, 4},
1144a1767b4Smrg {f5, 5},
1154a1767b4Smrg {f10, 10},
1164a1767b4Smrg {f15, 15},
1174a1767b4Smrg {f16, 16},
1184a1767b4Smrg {f17, 17},
1194a1767b4Smrg {f31, 31},
1204a1767b4Smrg {f32, 32},
1214a1767b4Smrg {f33, 33},
1224a1767b4Smrg {f63, 63},
1234a1767b4Smrg {f64, 64},
1244a1767b4Smrg {f65, 65},
1254a1767b4Smrg {f127, 127},
1264a1767b4Smrg {f128, 128},
1274a1767b4Smrg {NULL, 0}
1284a1767b4Smrg };
1294a1767b4Smrg
1304a1767b4Smrg
1314a1767b4Smrg int
main(int argc,char * argv[])1324a1767b4Smrg main (int argc, char *argv[])
1334a1767b4Smrg {
1344a1767b4Smrg static char usage[] = "\
1354a1767b4Smrg usage: t-rand [function nbits]\n\
1364a1767b4Smrg function is one of z, f\n\
1374a1767b4Smrg nbits is number of bits\n\
1384a1767b4Smrg ";
1394a1767b4Smrg gmp_randstate_t rstate;
1404a1767b4Smrg mpz_t z, rz;
1414a1767b4Smrg mpf_t f, rf;
1424a1767b4Smrg enum { Z, F } func = Z;
1434a1767b4Smrg int nbits = 1;
1444a1767b4Smrg int verify_mode_flag = 1;
145*f81b1c5bSmrg int i;
1464a1767b4Smrg struct rt *a;
1474a1767b4Smrg
1484a1767b4Smrg
1494a1767b4Smrg if (argc > 1)
1504a1767b4Smrg {
1514a1767b4Smrg if (argc < 3)
1524a1767b4Smrg {
1534a1767b4Smrg fputs (usage, stderr);
1544a1767b4Smrg exit (1);
1554a1767b4Smrg }
1564a1767b4Smrg verify_mode_flag = 0;
1574a1767b4Smrg if (*argv[1] == 'z')
1584a1767b4Smrg func = Z;
1594a1767b4Smrg if (*argv[1] == 'f')
1604a1767b4Smrg func = F;
1614a1767b4Smrg nbits = atoi (argv[2]);
1624a1767b4Smrg }
1634a1767b4Smrg
1644a1767b4Smrg mpz_init (rz);
1654a1767b4Smrg
1664a1767b4Smrg if (verify_mode_flag)
1674a1767b4Smrg {
1684a1767b4Smrg #ifdef VERBOSE
1694a1767b4Smrg printf ("%s: verifying random numbers: ", argv[0]);
1704a1767b4Smrg #endif
1714a1767b4Smrg
1724a1767b4Smrg /* Test z. */
1734a1767b4Smrg mpz_init (z);
1744a1767b4Smrg for (a = zarr; a->s != NULL; a++)
1754a1767b4Smrg {
1764a1767b4Smrg gmp_randinit (rstate, GMP_RAND_ALG_LC, a->nbits);
1774a1767b4Smrg if (gmp_errno != GMP_ERROR_NONE)
1784a1767b4Smrg exit (1);
1794a1767b4Smrg gmp_randseed_ui (rstate, SEED);
1804a1767b4Smrg
1814a1767b4Smrg for (i = 0; i < ENTS; i++)
1824a1767b4Smrg {
1834a1767b4Smrg mpz_urandomb (rz, rstate, a->nbits);
1844a1767b4Smrg mpz_set_str (z, a->s[i], BASE);
1854a1767b4Smrg if (mpz_cmp (z, rz) != 0)
1864a1767b4Smrg {
1874a1767b4Smrg printf ("z%d: ", a->nbits);
1884a1767b4Smrg mpz_out_str (stdout, BASE, rz);
1894a1767b4Smrg printf (" should be ");
1904a1767b4Smrg mpz_out_str (stdout, BASE, z);
1914a1767b4Smrg puts ("");
1924a1767b4Smrg exit (1);
1934a1767b4Smrg }
1944a1767b4Smrg }
1954a1767b4Smrg #ifdef VERBOSE
1964a1767b4Smrg printf ("z%d ", a->nbits);
1974a1767b4Smrg #endif
1984a1767b4Smrg gmp_randclear (rstate);
1994a1767b4Smrg }
2004a1767b4Smrg mpz_clear (z);
2014a1767b4Smrg
2024a1767b4Smrg
2034a1767b4Smrg /* Test f. */
2044a1767b4Smrg for (a = farr; a->s != NULL; a++)
2054a1767b4Smrg {
2064a1767b4Smrg gmp_randinit (rstate, GMP_RAND_ALG_LC, a->nbits);
2074a1767b4Smrg if (gmp_errno != GMP_ERROR_NONE)
2084a1767b4Smrg exit (1);
2094a1767b4Smrg gmp_randseed_ui (rstate, SEED);
2104a1767b4Smrg
2114a1767b4Smrg mpf_init2 (f, a->nbits);
2124a1767b4Smrg mpf_init2 (rf, a->nbits);
2134a1767b4Smrg for (i = 0; i < ENTS; i++)
2144a1767b4Smrg {
2154a1767b4Smrg mpf_urandomb (rf, rstate, a->nbits);
2164a1767b4Smrg mpf_set_str (f, a->s[i], BASE);
2174a1767b4Smrg if (mpf_cmp (f, rf) != 0)
2184a1767b4Smrg {
2194a1767b4Smrg printf ("f%d: ", a->nbits);
2204a1767b4Smrg mpf_out_str (stdout, BASE, a->nbits, rf);
2214a1767b4Smrg printf (" should be ");
2224a1767b4Smrg mpf_out_str (stdout, BASE, a->nbits, f);
2234a1767b4Smrg puts ("");
2244a1767b4Smrg exit (1);
2254a1767b4Smrg }
2264a1767b4Smrg }
2274a1767b4Smrg #ifdef VERBOSE
2284a1767b4Smrg printf ("f%d ", a->nbits);
2294a1767b4Smrg #endif
2304a1767b4Smrg gmp_randclear (rstate);
2314a1767b4Smrg mpf_clear (f);
2324a1767b4Smrg mpf_clear (rf);
2334a1767b4Smrg }
2344a1767b4Smrg
2354a1767b4Smrg #ifdef VERBOSE
2364a1767b4Smrg puts ("");
2374a1767b4Smrg #endif
2384a1767b4Smrg }
2394a1767b4Smrg else /* Print mode. */
2404a1767b4Smrg {
2414a1767b4Smrg gmp_randinit (rstate, GMP_RAND_ALG_LC, nbits);
2424a1767b4Smrg if (gmp_errno != GMP_ERROR_NONE)
2434a1767b4Smrg exit (1);
2444a1767b4Smrg gmp_randseed_ui (rstate, SEED);
2454a1767b4Smrg
2464a1767b4Smrg switch (func)
2474a1767b4Smrg {
2484a1767b4Smrg case Z:
2494a1767b4Smrg printf ("char *z%d[ENTS] = {", nbits);
2504a1767b4Smrg for (i = 0; i < ENTS; i++)
2514a1767b4Smrg {
2524a1767b4Smrg mpz_urandomb (rz, rstate, nbits);
2534a1767b4Smrg printf ("\"");
2544a1767b4Smrg mpz_out_str (stdout, BASE, rz);
2554a1767b4Smrg printf ("\"");
2564a1767b4Smrg if (i != ENTS - 1)
2574a1767b4Smrg printf (", ");
2584a1767b4Smrg }
2594a1767b4Smrg printf ("};\n");
2604a1767b4Smrg printf (" {z%d, %d},\n", nbits, nbits);
2614a1767b4Smrg break;
2624a1767b4Smrg
2634a1767b4Smrg case F:
2644a1767b4Smrg printf ("char *f%d[ENTS] = {", nbits);
2654a1767b4Smrg mpf_init2 (rf, nbits);
2664a1767b4Smrg for (i = 0; i < ENTS; i++)
2674a1767b4Smrg {
2684a1767b4Smrg mpf_urandomb (rf, rstate, nbits);
2694a1767b4Smrg printf ("\"");
2704a1767b4Smrg mpf_out_str (stdout, BASE, nbits, rf);
2714a1767b4Smrg printf ("\"");
2724a1767b4Smrg if (i != ENTS - 1)
2734a1767b4Smrg printf (", ");
2744a1767b4Smrg }
2754a1767b4Smrg printf ("};\n");
2764a1767b4Smrg printf (" {f%d, %d},\n", nbits, nbits);
2774a1767b4Smrg mpf_clear (rf);
2784a1767b4Smrg break;
2794a1767b4Smrg
2804a1767b4Smrg default:
2814a1767b4Smrg exit (1);
2824a1767b4Smrg }
2834a1767b4Smrg
2844a1767b4Smrg gmp_randclear (rstate);
2854a1767b4Smrg }
2864a1767b4Smrg
2874a1767b4Smrg mpz_clear (rz);
2884a1767b4Smrg
2894a1767b4Smrg return 0;
2904a1767b4Smrg }
291