math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / rand / grand.h
1 /* -*-c-*-
2 *
3 * Generic interface to random number generators
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 #ifndef CATACOMB_GRAND_H
29 #define CATACOMB_GRAND_H
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <assert.h>
38 #include <stddef.h>
39
40 #include <mLib/bits.h>
41
42 /*----- Generic random number generator interface -------------------------*/
43
44 typedef struct grand {
45 const struct grand_ops *ops;
46 } grand;
47
48 typedef struct grand_ops {
49
50 /* --- Various important properties --- */
51
52 const char *name; /* Generator's name */
53 unsigned f; /* Various flags */
54 uint32 max; /* Maximum raw output */
55
56 /* --- Maintenance methods --- */
57
58 int (*misc)(grand */*r*/, unsigned /*op*/, ...); /* Miscellaneous ops */
59 void (*destroy)(grand */*r*/); /* Destroy generator context */
60
61 /* --- Output methods --- *
62 *
63 * Only one of these operations need actually be implemented. All the
64 * other operations may be synthesized. Of course, performance is improved
65 * if more are provided.
66 */
67
68 uint32 (*raw)(grand */*r*/); /* Uniform over %$[0, max)$% */
69 octet (*byte)(grand */*r*/); /* Uniform over %$[0, 256)$% */
70 uint32 (*word)(grand */*r*/); /* Uniform over %$[0, 2^{32})$% */
71 uint32 (*range)(grand */*r*/, uint32 /*l*/); /* Uniform over %$[0, l)$% */
72 void (*fill)(grand */*r*/, void */*p*/, size_t /*sz*/); /* Fill buffer */
73 } grand_ops;
74
75 #define GR_DESTROY(r) (r)->ops->destroy((r))
76 #define GR_RAW(r) (r)->ops->raw((r))
77 #define GR_WORD(r) (r)->ops->word((r))
78 #define GR_RANGE(r, l) (r)->ops->range((r), (l))
79 #define GR_FILL(r, p, sz) (r)->ops->fill((r), (p), (sz))
80
81 /* --- Flag types --- */
82
83 #define GRAND_CRYPTO 1u /* Cryptographically strong */
84
85 /* --- Operation types --- */
86
87 enum {
88
89 /* --- Required operations --- */
90
91 GRAND_CHECK, /* @unsigned op2@ */
92
93 /* --- Standard seeding operations --- */
94
95 GRAND_SEEDINT, /* @int i@ */
96 GRAND_SEEDUINT32, /* @uint32 i@ */
97 GRAND_SEEDBLOCK, /* @const void *p, size_t sz@ */
98 GRAND_SEEDMP, /* @mp *m@ */
99 GRAND_SEEDRAND /* @grand *g@ */
100
101 /* --- Generator-specific operations --- */
102
103 #define GRAND_SPECIFIC(ch) ((unsigned)(ch) << 8)
104 };
105
106 #define GRAND_BADOP assert(((void)"bad grand_misc op", 0))
107
108 /*----- Functions provided ------------------------------------------------*/
109
110 /* --- @grand_byte@ --- *
111 *
112 * Arguments: @grand *r@ = pointet to generic generator
113 *
114 * Returns: A uniformly-distributed pseudorandom integer in the interval
115 * %$[0, 256)$%.
116 */
117
118 extern octet grand_byte(grand */*r*/);
119
120 /* --- @grand_word@ --- *
121 *
122 * Arguments: @grand *r@ = pointet to generic generator
123 *
124 * Returns: A uniformly-distributed pseudorandom integer in the interval
125 * %$[0, 2^{32})$%.
126 */
127
128 extern uint32 grand_word(grand */*r*/);
129
130 /* --- @grand_range@ --- *
131 *
132 * Arguments: @grand *r@ = pointet to generic generator
133 * @uint32 l@ = limit for acceptable results
134 *
135 * Returns: A uniformly-distributed pseudorandom integer in the interval
136 * %$[0, l)$%.
137 */
138
139 extern uint32 grand_range(grand */*r*/, uint32 /*l*/);
140
141 /* --- @grand_fill@ --- *
142 *
143 * Arguments: @grand *r@ = pointet to generic generator
144 * @void *p@ = pointer to a buffer
145 * @size_t sz@ = size of the buffer
146 *
147 * Returns: ---
148 *
149 * Use: Fills a buffer with uniformly distributed pseudorandom bytes
150 * (see @grand_byte@).
151 */
152
153 extern void grand_fill(grand */*r*/, void */*p*/, size_t /*sz*/);
154
155 /*----- That's all, folks -------------------------------------------------*/
156
157 #ifdef __cplusplus
158 }
159 #endif
160
161 #endif