Table for driving key data extraction.
[u/mdw/catacomb] / mprand.c
1 /* -*-c-*-
2 *
3 * $Id: mprand.c,v 1.2 1999/12/22 15:55:33 mdw Exp $
4 *
5 * Generate a random multiprecision integer
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: mprand.c,v $
33 * Revision 1.2 1999/12/22 15:55:33 mdw
34 * Modify `mprand' slightly. Add `mprand_range'.
35 *
36 * Revision 1.1 1999/12/10 23:23:05 mdw
37 * Support for generating random large integers.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include <mLib/alloc.h>
44
45 #include "grand.h"
46 #include "mp.h"
47 #include "mprand.h"
48
49 /*----- Main code ---------------------------------------------------------*/
50
51 /* --- @mprand@ --- *
52 *
53 * Arguments: @mp *d@ = destination integer
54 * @unsigned b@ = number of bits
55 * @grand *r@ = pointer to random number source
56 * @mpw or@ = mask to OR with low-order bits
57 *
58 * Returns: A random integer with the requested number of bits.
59 *
60 * Use: Constructs an arbitrarily large pseudorandom integer.
61 * Assuming that the generator @r@ is good, the result is
62 * uniformly distributed in the interval %$[2^{b - 1}, 2^b)$%.
63 * The result is then ORred with the given @or@ value. This
64 * will often be 1, to make the result odd.
65 */
66
67 mp *mprand(mp *d, unsigned b, grand *r, mpw or)
68 {
69 size_t sz = (b + 7) >> 3;
70 octet *v = xmalloc(sz);
71 unsigned m;
72
73 /* --- Fill buffer with random data --- */
74
75 r->ops->fill(r, v, sz);
76
77 /* --- Force into the correct range --- *
78 *
79 * This is slightly tricky. Oh, well.
80 */
81
82 b = (b - 1) & 7;
83 m = (1 << b);
84 v[0] = (v[0] & (m - 1)) | m;
85
86 /* --- Mask, load and return --- */
87
88 d = mp_loadb(d, v, sz);
89 d->v[0] |= or;
90 free(v);
91 return (d);
92 }
93
94 /* --- @mprand_range@ --- *
95 *
96 * Arguments: @mp *d@ = destination integer
97 * @mp *l@ = limit for random number
98 * @grand *r@ = random number source
99 * @mpw or@ = mask for low-order bits
100 *
101 * Returns: A pseudorandom integer, unformly distributed over the
102 * interval %$[0, l)$%.
103 *
104 * Use: Generates a uniformly-distributed pseudorandom number in the
105 * appropriate range.
106 */
107
108 mp *mprand_range(mp *d, mp *l, grand *r, mpw or)
109 {
110 size_t b = mp_bits(l);
111 size_t sz = (b + 7) >> 3;
112 octet *v = xmalloc(sz);
113 unsigned m;
114
115 /* --- The algorithm --- *
116 *
117 * Rather simpler than most. Find the number of bits in the number %$l$%
118 * (i.e., the integer %$b$% such that %$2^{b - 1} \le l < 2^b$%), and
119 * generate pseudorandom integers with %$n$% bits (but not, unlike in the
120 * function above, with the top bit forced to 1). If the integer is
121 * greater than or equal to %$l$%, try again.
122 *
123 * This is similar to the algorithms used in @lcrand_range@ and friends,
124 * except that I've forced the `raw' range of the random numbers such that
125 * %$l$% itself is the largest multiple of %$l$% in the range (since, by
126 * the inequality above, %$2^b \le 2l$%). This removes the need for costly
127 * division and remainder operations.
128 *
129 * As usual, the number of iterations expected is two.
130 */
131
132 b = (b - 1) & 7;
133 m = (1 << b) - 1;
134 do {
135 r->ops->fill(r, v, sz);
136 v[0] &= m;
137 d = mp_loadb(d, v, sz);
138 d->v[0] |= or;
139 } while (MP_CMP(d, >=, l));
140
141 /* --- Done --- */
142
143 free(v);
144 return (d);
145 }
146
147 /*----- That's all, folks -------------------------------------------------*/