3 * $Id: mprand.c,v 1.5 2004/04/08 01:36:15 mdw Exp $
5 * Generate a random multiprecision integer
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/alloc.h>
38 /*----- Main code ---------------------------------------------------------*/
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
47 * Returns: A random integer with the requested number of bits.
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.
56 mp
*mprand(mp
*d
, unsigned b
, grand
*r
, mpw
or)
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
);
63 /* --- Fill buffer with random data --- */
65 r
->ops
->fill(r
, v
, sz
);
67 /* --- Force into the correct range --- *
69 * This is slightly tricky. Oh, well.
74 v
[0] = (v
[0] & (m
- 1)) | m
;
76 /* --- Mask, load and return --- */
78 d
= mp_loadb(d
, v
, sz
);
85 /* --- @mprand_range@ --- *
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
92 * Returns: A pseudorandom integer, unformly distributed over the
93 * interval %$[0, l)$%.
95 * Use: Generates a uniformly-distributed pseudorandom number in the
99 mp
*mprand_range(mp
*d
, mp
*l
, grand
*r
, mpw
or)
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
);
107 /* --- The algorithm --- *
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.
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.
121 * As usual, the number of iterations expected is two.
124 b
= ((b
- 1) & 7) + 1;
127 r
->ops
->fill(r
, v
, sz
);
129 d
= mp_loadb(d
, v
, sz
);
131 } while (MP_CMP(d
, >=, l
));
140 /*----- That's all, folks -------------------------------------------------*/