3 * $Id: fibrand.c,v 1.4 2002/10/09 00:35:18 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 2002/10/09 00:35:18 mdw
34 * Fix bogus type name.
36 * Revision 1.3 2000/12/06 20:31:06 mdw
37 * Simplify uniform range transformation.
39 * Revision 1.2 2000/06/17 10:55:24 mdw
40 * Typesetting fixes. Add flags word to generatic random generator.
42 * Revision 1.1 1999/12/10 23:15:27 mdw
43 * Noncryptographic random number generator.
47 /*----- Header files ------------------------------------------------------*/
54 #include <mLib/bits.h>
61 /*----- Main code ---------------------------------------------------------*/
63 /* --- @fibrand_step@ --- *
65 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
67 * Returns: Next output from generator.
69 * Use: Steps the generator. Returns
70 * %$x_{i - 24} + x_{i - 55} \bmod 2^{32}$%.
73 uint32 fibrand_step(fibrand *f)
76 unsigned j = i + (FIB_SZ - FIB_TAP);
80 x = f->x[i] = U32(f->x[i] + f->x[j]);
88 /* --- @fibrand_seed@ --- *
90 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
91 * @grand *r@ = random number generator to extract words from
95 * Use: Initializes a Fibonacci generator using word outputs from the
96 * given random number source @r@.
99 void fibrand_seed(fibrand *f, grand *r)
104 for (i = 0; i < FIB_SZ; i++)
105 p |= f->x[i] = r->ops->word(r);
107 i = r->ops->range(r, FIB_SZ);
113 /* --- @fibrand_lcseed@ --- *
115 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
116 * @uint32 seed@ = seed value
120 * Use: Initializes a Fibonacci generator using outputs from the
121 * @lcrand@ generator seeded from @seed@. This is faster than
122 * using a generic @lcrand@-based generator and @fibrand_rseed@
123 * because it uses raw outputs rather than uniformly distributed
127 void fibrand_lcseed(fibrand *f, uint32 seed)
132 for (i = 0; i < FIB_SZ; i++)
133 p |= f->x[i] = seed = lcrand(seed);
135 i = lcrand_range(&seed, FIB_SZ);
141 /* --- @fibrand_range@ --- *
143 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
146 * Returns: A uniformly distributed pseudorandom integer in the interval
150 uint32 fibrand_range(fibrand *f, uint32 m)
152 uint32 r = 0xffffffff - (0xffffffff % m);
155 /* --- Now generate numbers until a good one comes along --- */
157 do x = fibrand_step(f); while (x >= r);
161 /*----- Generic interface -------------------------------------------------*/
163 typedef struct gctx {
168 static void gdestroy(grand *r)
174 static int gmisc(grand *r, unsigned op, ...)
183 switch (va_arg(ap, unsigned)) {
186 case GRAND_SEEDUINT32:
196 fibrand_lcseed(&g->f, va_arg(ap, unsigned));
198 case GRAND_SEEDUINT32:
199 fibrand_lcseed(&g->f, va_arg(ap, uint32));
202 fibrand_seed(&g->f, va_arg(ap, grand *));
213 static octet gbyte(grand *r)
216 return (U8(fibrand_step(&g->f)));
219 static uint32 gword(grand *r)
222 return (fibrand_step(&g->f));
225 static uint32 grange(grand *r, uint32 l)
228 return (fibrand_range(&g->f, l));
231 static void gfill(grand *r, void *p, size_t sz)
236 *q++ = U8(fibrand_step(&g->f));
241 static const grand_ops gops = {
245 gword, gbyte, gword, grange, gfill
248 /* --- @fibrand_create@ --- *
250 * Arguments: @uint32 seed@ = initial seed
252 * Returns: Pointer to a generic generator.
254 * Use: Constructs a generic generator interface over a Fibonacci
255 * generator. The generator is seeded using @fibrand_lcseed@.
258 grand *fibrand_create(uint32 seed)
260 gctx *g = CREATE(gctx);
262 fibrand_lcseed(&g->f, seed);
266 /*----- That's all, folks -------------------------------------------------*/