summaryrefslogtreecommitdiff
path: root/rand.c
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordberg.se>2000-04-01 15:52:49 +0200
committerLinus Nordberg <linus@nordberg.se>2000-04-01 15:52:49 +0200
commit291b7e8a4c022f4034b4e8375f7d903b5e8cec8a (patch)
treeb1c7cc71dc4a3b40d232f276acded8f386ad8d5a /rand.c
parent94c252940976314d2b315473cf8f839c59c9b6cf (diff)
downloadgmp-291b7e8a4c022f4034b4e8375f7d903b5e8cec8a.tar.gz
(__gmp_rand_scheme): Use better multipliers. Remove test schemes. Replace `m' with `m2exp'.
(gmp_rand_init): Change parameters and return type. Use `m2exp' instead of `m'. Set `gmp_errno' on error. Disable BBS algorithm.
Diffstat (limited to 'rand.c')
-rw-r--r--rand.c139
1 files changed, 57 insertions, 82 deletions
diff --git a/rand.c b/rand.c
index c1baa3274..45527378f 100644
--- a/rand.c
+++ b/rand.c
@@ -1,6 +1,6 @@
-/* gmp_rand_init (state, alg, size, seed) -- Initialize a random state.
+/* gmp_rand_init (state, size, alg) -- Initialize a random state.
-Copyright (C) 1999 Free Software Foundation, Inc.
+Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
@@ -19,115 +19,92 @@ along with the GNU MP Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
-#include <stdlib.h> /* FIXME: For malloc(). */
#include "gmp.h"
#include "gmp-impl.h"
-/* Array of CL-schemes, ordered in increasing order for the first
- member (the 'bits' value). The 'm' entry is converted by
- mpz_set_str() with BASE=0. End of array is indicated with an entry
- containing all zeros. */
+/* Array of CL-schemes, ordered in increasing order of the first
+ member (the 'm2exp' value). The end of the array is indicated with
+ an entry containing all zeros. */
static __gmp_rand_lc_scheme_struct __gmp_rand_scheme[] =
{
+#if 0
/* FIXME: Remove. */
- {8, "7", 0, "256"}, /* Test. */
+ {8, "7", 0}, /* Test. */
{31, /* fbsd rand(3) */
"1103515245", /* a (multiplier) */
- 12345, /* c (adder) */
- "0x80000000"}, /* m (modulo) = 2^31 */
+ 12345}, /* c (adder) */
+#endif
- /* {31, "22298549", 1, "0x80000000"},*/
+ /* {31, "22298549", 1}, */
/* merit >= 1; no merit > 3 up to 32845469 */
/* The following multipliers are all between 0.01m and 0.99m, and
are congruent to 5 (mod 8). They all pass the spectral test with
- Vt >= 2^(30/t) and merit >= 0.1.
-
- Up to and including 40 bits, merit >= 3.
- Up to and including 56 bits, merit >= 2.
- Up to and including 64 bits, merit >= 1. */
-
- {32, "43840821", 1, "0x100000000"},
- {33, "85943917", 1, "0x200000000"},
- {34, "171799469", 1, "0x400000000"},
- {35, "343825285", 1, "0x800000000"},
- {36, "687285701", 1, "0x1000000000"},
- {37, "1374564613", 1, "0x2000000000"},
- {38, "2749193437", 1, "0x4000000000"},
- {39, "5497652029", 1, "0x8000000000"},
- {40, "10995212661", 1, "0x10000000000"},
- {56, "720575942521269", 1, "0x100000000000000"},
- /* no merit >3 up to 720575942889605 */
- {64, "184467440744758277", 1, "0x10000000000000000"},
- /* no merit >2 up to 184467440750242261 */
-
- {100, "12676506002282294015055173877", 1, "0x10000000000000000000000000"},
- /* low merit2; (12676506002282294015146266925) */
- {128, "3402823669209384634633746074363516093",
- 1, "0x100000000000000000000000000000000"},
- /* low merit2+3; (3402823669209384634633746074396970501) */
- {156, "913438523331814323877303020447676887284958173",
- 1, "0x1000000000000000000000000000000000000000"},
- /* low merit* */
- {196, "1004336277661868922213726307713226626576376871114287459829",
- 1, "0x10000000000000000000000000000000000000000000000000"},
- /* low merit*; (1004336277661868922213726307713226626576376871114345439621) */
- {200, "16069380442589902755419620923411626025222029937827928353349",
- 1, "0x100000000000000000000000000000000000000000000000000"},
- /* low merit*; */
- {256, "1157920892373161954235709850086879078532699846656405640394575840079131296733",
- 1, "0x10000000000000000000000000000000000000000000000000000000000000000"},
- /* low merit*; */
-
- {0, NULL, 0, NULL} /* End of array. */
+ Vt >= 2^(30/t) and merit >= 1. (Up to and including 196 bits,
+ merit >= 3.) */
+
+ {32, "43840821", 1},
+ {33, "85943917", 1},
+ {34, "171799469", 1},
+ {35, "343825285", 1},
+ {36, "687285701", 1},
+ {37, "1374564613", 1},
+ {38, "2749193437", 1},
+ {39, "5497652029", 1},
+ {40, "10995212661", 1},
+ {56, "47988680294711517", 1},
+ {64, "13469374875402548381", 1},
+ {100, "203786806069096950756900463357", 1},
+ {128, "96573135900076068624591706046897650309", 1},
+ {156, "43051576988660538262511726153887323360449035333", 1},
+ {196, "1611627857640767981443524165616850972435303571524033586421", 1},
+ {200, "491824250216153841876046962368396460896019632211283945747141", 1},
+ {256, "79336254595106925775099152154558630917988041692672147726148065355845551082677", 1},
+ {0, NULL, 0} /* End of array. */
};
-/* gmp_rand_init() -- Initialize a gmp_rand_state. Return 0 on
- success and 1 on failure. */
-
-int
+void
#if __STDC__
gmp_rand_init (gmp_rand_state s,
- gmp_rand_algorithm alg,
unsigned long int size,
- mpz_t seed)
+ gmp_rand_algorithm alg)
#else
-gmp_rand_init (s, alg, size, seed)
+gmp_rand_init (s, size, alg)
gmp_rand_state s;
- gmp_rand_algorithm alg;
unsigned long int size;
- mpz_t seed;
+ gmp_rand_algorithm alg;
#endif
{
switch (alg)
{
- case GMP_RAND_ALG_LC: /* Linear congruental. */
+ case GMP_RAND_ALG_LC: /* Linear congruental. */
{
__gmp_rand_lc_scheme_struct *sp;
- mpz_t a, m;
+ mpz_t a;
- /* Pick a scheme. */
- for (sp = __gmp_rand_scheme; sp->bits != 0; sp++)
- if (sp->bits >= size)
+ /* Pick a scheme. */
+ for (sp = __gmp_rand_scheme; sp->m2exp != 0; sp++)
+ if (sp->m2exp / 2 >= size)
break;
- if (!sp->bits) /* Nothing big enough found. */
- sp--; /* Use largest available. */
- /* Install scheme. */
+ if (sp->m2exp == 0) /* Nothing big enough found. */
+ {
+ gmp_errno |= GMP_ERROR_INVALID_ARGUMENT;
+ return;
+ }
+
+ /* Install scheme. */
mpz_init_set_str (a, sp->astr, 0);
- mpz_init_set_str (m, sp->mstr, 0);
- gmp_rand_init_lc (s, sp->bits, seed, a, sp->c, m);
+ gmp_rand_init_lc_2exp (s, a, sp->c, sp->m2exp);
- mpz_clear (a);
- mpz_clear (m);
-
break;
}
- case GMP_RAND_ALG_BBS: /* Blum, Blum, and Shub */
+#if 0
+ case GMP_RAND_ALG_BBS: /* Blum, Blum, and Shub. */
{
- mpz_t p, q; /* FIXME: Ok for all compilers? */
+ mpz_t p, q;
mpz_t ztmp;
/* FIXME: Generate p and q. They must be ``large'' primes,
@@ -139,9 +116,8 @@ gmp_rand_init (s, alg, size, seed)
mpz_init_set_str (q, "315270837425234199477225845240496832591", 10);
/* Allocate algorithm specific data. */
- /* FIXME: Use user supplied allocation func instead of malloc? */
- s->data.bbs =
- (__gmp_rand_data_bbs *) malloc (sizeof (__gmp_rand_data_bbs));
+ s->data.bbs = (__gmp_rand_data_bbs *)
+ (*_mp_allocate_func) (sizeof (__gmp_rand_data_bbs));
mpz_init (s->data.bbs->bi); /* The Blum integer. */
mpz_mul (s->data.bbs->bi, p, q);
@@ -157,18 +133,17 @@ gmp_rand_init (s, alg, size, seed)
}
s->alg = alg;
- s->size = size;
- mpz_init_set (s->seed, seed);
+ s->size = size; /* FIXME: Remove. */
+ mpz_set (s->seed, seed);
mpz_clear (p);
mpz_clear (q);
mpz_clear (ztmp);
break;
}
+#endif /* 0 */
default: /* Bad choice. */
- return 1;
+ gmp_errno |= GMP_ERROR_UNSUPPORTED_ARGUMENT;
}
-
- return 0;
}