3 * Generate a random multiprecision integer
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #include <mLib/alloc.h>
36 /*----- Main code ---------------------------------------------------------*/
40 * Arguments: @mp *d@ = destination integer
41 * @unsigned b@ = number of bits
42 * @grand *r@ = pointer to random number source
43 * @mpw or@ = mask to OR with low-order bits
45 * Returns: A random integer with the requested number of bits.
47 * Use: Constructs an arbitrarily large pseudorandom integer.
48 * Assuming that the generator @r@ is good, the result is
49 * uniformly distributed in the interval %$[2^{b - 1}, 2^b)$%.
50 * The result is then ORred with the given @or@ value. This
51 * will often be 1, to make the result odd.
54 mp *mprand(mp *d, unsigned b, grand *r, mpw or)
56 size_t sz = (b + 7) >> 3;
57 arena *a = (d && (d->f & MP_BURN)) ? arena_secure : arena_global;
58 octet *v = x_alloc(a, sz);
61 /* --- Fill buffer with random data --- */
63 r->ops->fill(r, v, sz);
65 /* --- Force into the correct range --- *
67 * This is slightly tricky. Oh, well.
72 v[0] = (v[0] & (m - 1)) | m;
74 /* --- Mask, load and return --- */
76 d = mp_loadb(d, v, sz);
83 /* --- @mprand_range@ --- *
85 * Arguments: @mp *d@ = destination integer
86 * @mp *l@ = limit for random number
87 * @grand *r@ = random number source
88 * @mpw or@ = mask for low-order bits
90 * Returns: A pseudorandom integer, unformly distributed over the
91 * interval %$[0, l)$%.
93 * Use: Generates a uniformly-distributed pseudorandom number in the
97 mp *mprand_range(mp *d, mp *l, grand *r, mpw or)
99 size_t b = mp_bits(l);
100 size_t sz = (b + 7) >> 3;
101 arena *a = (d && (d->f & MP_BURN)) ? arena_secure : arena_global;
102 octet *v = x_alloc(a, sz);
105 /* --- The algorithm --- *
107 * Rather simpler than most. Find the number of bits in the number %$l$%
108 * (i.e., the integer %$b$% such that %$2^{b - 1} \le l < 2^b$%), and
109 * generate pseudorandom integers with %$n$% bits (but not, unlike in the
110 * function above, with the top bit forced to 1). If the integer is
111 * greater than or equal to %$l$%, try again.
113 * This is similar to the algorithms used in @lcrand_range@ and friends,
114 * except that I've forced the `raw' range of the random numbers such that
115 * %$l$% itself is the largest multiple of %$l$% in the range (since, by
116 * the inequality above, %$2^b \le 2l$%). This removes the need for costly
117 * division and remainder operations.
119 * As usual, the number of iterations expected is two.
122 b = ((b - 1) & 7) + 1;
125 r->ops->fill(r, v, sz);
127 d = mp_loadb(d, v, sz);
129 } while (MP_CMP(d, >=, l));
138 /*----- That's all, folks -------------------------------------------------*/