chiark / gitweb /
Major memory management overhaul. Added arena support. Use the secure
[catacomb] / mprand.c
1 /* -*-c-*-
2  *
3  * $Id: mprand.c,v 1.3 2000/06/17 11:45:09 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.3  2000/06/17 11:45:09  mdw
34  * Major memory management overhaul.  Added arena support.  Use the secure
35  * arena for secret integers.  Replace and improve the MP management macros
36  * (e.g., replace MP_MODIFY by MP_DEST).
37  *
38  * Revision 1.2  1999/12/22 15:55:33  mdw
39  * Modify `mprand' slightly.  Add `mprand_range'.
40  *
41  * Revision 1.1  1999/12/10 23:23:05  mdw
42  * Support for generating random large integers.
43  *
44  */
45
46 /*----- Header files ------------------------------------------------------*/
47
48 #include <mLib/alloc.h>
49
50 #include "grand.h"
51 #include "mp.h"
52 #include "mprand.h"
53
54 /*----- Main code ---------------------------------------------------------*/
55
56 /* --- @mprand@ --- *
57  *
58  * Arguments:   @mp *d@ = destination integer
59  *              @unsigned b@ = number of bits
60  *              @grand *r@ = pointer to random number source
61  *              @mpw or@ = mask to OR with low-order bits
62  *
63  * Returns:     A random integer with the requested number of bits.
64  *
65  * Use:         Constructs an arbitrarily large pseudorandom integer.
66  *              Assuming that the generator @r@ is good, the result is
67  *              uniformly distributed in the interval %$[2^{b - 1}, 2^b)$%.
68  *              The result is then ORred with the given @or@ value.  This
69  *              will often be 1, to make the result odd.
70  */
71
72 mp *mprand(mp *d, unsigned b, grand *r, mpw or)
73 {
74   size_t sz = (b + 7) >> 3;
75   arena *a = (d && (d->f & MP_BURN)) ? arena_secure : arena_global;
76   octet *v = x_alloc(a, sz);
77   unsigned m;
78
79   /* --- Fill buffer with random data --- */
80
81   r->ops->fill(r, v, sz);
82
83   /* --- Force into the correct range --- *
84    *
85    * This is slightly tricky.  Oh, well.
86    */
87
88   b = (b - 1) & 7;
89   m = (1 << b);
90   v[0] = (v[0] & (m - 1)) | m;
91
92   /* --- Mask, load and return --- */
93
94   d = mp_loadb(d, v, sz);
95   d->v[0] |= or;
96   memset(v, 0, sz);
97   x_free(a, v);
98   return (d);
99 }
100
101 /* --- @mprand_range@ --- *
102  *
103  * Arguments:   @mp *d@ = destination integer
104  *              @mp *l@ = limit for random number
105  *              @grand *r@ = random number source
106  *              @mpw or@ = mask for low-order bits
107  *
108  * Returns:     A pseudorandom integer, unformly distributed over the
109  *              interval %$[0, l)$%.
110  *
111  * Use:         Generates a uniformly-distributed pseudorandom number in the
112  *              appropriate range.
113  */
114
115 mp *mprand_range(mp *d, mp *l, grand *r, mpw or)
116 {
117   size_t b = mp_bits(l);
118   size_t sz = (b + 7) >> 3;
119   arena *a = (d && (d->f & MP_BURN)) ? arena_secure : arena_global;
120   octet *v = x_alloc(a, sz);
121   unsigned m;
122
123   /* --- The algorithm --- *
124    *
125    * Rather simpler than most.  Find the number of bits in the number %$l$%
126    * (i.e., the integer %$b$% such that %$2^{b - 1} \le l < 2^b$%), and
127    * generate pseudorandom integers with %$n$% bits (but not, unlike in the
128    * function above, with the top bit forced to 1).  If the integer is
129    * greater than or equal to %$l$%, try again.
130    *
131    * This is similar to the algorithms used in @lcrand_range@ and friends,
132    * except that I've forced the `raw' range of the random numbers such that
133    * %$l$% itself is the largest multiple of %$l$% in the range (since, by
134    * the inequality above, %$2^b \le 2l$%).  This removes the need for costly
135    * division and remainder operations.
136    *
137    * As usual, the number of iterations expected is two.
138    */
139
140   b = (b - 1) & 7;
141   m = (1 << b) - 1;
142   do {
143     r->ops->fill(r, v, sz);
144     v[0] &= m;
145     d = mp_loadb(d, v, sz);
146     d->v[0] |= or;
147   } while (MP_CMP(d, >=, l));
148
149   /* --- Done --- */
150
151   memset(v, 0, sz);
152   x_free(a, v);
153   return (d);
154 }
155
156 /*----- That's all, folks -------------------------------------------------*/