math/gfreduce.[ch]: Fix out-of-bounds memory access.
[u/mdw/catacomb] / math / mprand.c
1 /* -*-c-*-
2 *
3 * Generate a random multiprecision integer
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <mLib/alloc.h>
31
32 #include "grand.h"
33 #include "mp.h"
34 #include "mprand.h"
35
36 /*----- Main code ---------------------------------------------------------*/
37
38 /* --- @mprand@ --- *
39 *
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
44 *
45 * Returns: A random integer with the requested number of bits.
46 *
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.
52 */
53
54 mp *mprand(mp *d, unsigned b, grand *r, mpw or)
55 {
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);
59 unsigned m;
60
61 /* --- Fill buffer with random data --- */
62
63 r->ops->fill(r, v, sz);
64
65 /* --- Force into the correct range --- *
66 *
67 * This is slightly tricky. Oh, well.
68 */
69
70 b = (b - 1) & 7;
71 m = (1 << b);
72 v[0] = (v[0] & (m - 1)) | m;
73
74 /* --- Mask, load and return --- */
75
76 d = mp_loadb(d, v, sz);
77 d->v[0] |= or;
78 memset(v, 0, sz);
79 x_free(a, v);
80 return (d);
81 }
82
83 /* --- @mprand_range@ --- *
84 *
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
89 *
90 * Returns: A pseudorandom integer, unformly distributed over the
91 * interval %$[0, l)$%.
92 *
93 * Use: Generates a uniformly-distributed pseudorandom number in the
94 * appropriate range.
95 */
96
97 mp *mprand_range(mp *d, mp *l, grand *r, mpw or)
98 {
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);
103 unsigned m;
104
105 /* --- The algorithm --- *
106 *
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.
112 *
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.
118 *
119 * As usual, the number of iterations expected is two.
120 */
121
122 b = ((b - 1) & 7) + 1;
123 m = (1 << b) - 1;
124 do {
125 r->ops->fill(r, v, sz);
126 v[0] &= m;
127 d = mp_loadb(d, v, sz);
128 d->v[0] |= or;
129 } while (MP_CMP(d, >=, l));
130
131 /* --- Done --- */
132
133 memset(v, 0, sz);
134 x_free(a, v);
135 return (d);
136 }
137
138 /*----- That's all, folks -------------------------------------------------*/