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