cc.h: Reorder the declarations.
[u/mdw/catacomb] / grand.h
1 /* -*-c-*-
2 *
3 * $Id: grand.h,v 1.5 2004/04/08 01:36:15 mdw Exp $
4 *
5 * Generic interface to random number generators
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 #ifndef CATACOMB_GRAND_H
31 #define CATACOMB_GRAND_H
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #include <assert.h>
40 #include <stddef.h>
41
42 #include <mLib/bits.h>
43
44 /*----- Generic random number generator interface -------------------------*/
45
46 typedef struct grand {
47 const struct grand_ops *ops;
48 } grand;
49
50 typedef struct grand_ops {
51
52 /* --- Various important properties --- */
53
54 const char *name; /* Generator's name */
55 unsigned f; /* Various flags */
56 uint32 max; /* Maximum raw output */
57
58 /* --- Maintenance methods --- */
59
60 int (*misc)(grand */*r*/, unsigned /*op*/, ...); /* Miscellaneous ops */
61 void (*destroy)(grand */*r*/); /* Destroy generator context */
62
63 /* --- Output methods --- *
64 *
65 * Only one of these operations need actually be implemented. All the
66 * other operations may be synthesized. Of course, performance is improved
67 * if more are provided.
68 */
69
70 uint32 (*raw)(grand */*r*/); /* Uniform over %$[0, max)$% */
71 octet (*byte)(grand */*r*/); /* Uniform over %$[0, 256)$% */
72 uint32 (*word)(grand */*r*/); /* Uniform over %$[0, 2^{32})$% */
73 uint32 (*range)(grand */*r*/, uint32 /*l*/); /* Uniform over %$[0, l)$% */
74 void (*fill)(grand */*r*/, void */*p*/, size_t /*sz*/); /* Fill buffer */
75 } grand_ops;
76
77 #define GR_DESTROY(r) (r)->ops->destroy((r))
78 #define GR_RAW(r) (r)->ops->raw((r))
79 #define GR_WORD(r) (r)->ops->word((r))
80 #define GR_RANGE(r, l) (r)->ops->range((r), (l))
81 #define GR_FILL(r, p, sz) (r)->ops->fill((r), (p), (sz))
82
83 /* --- Flag types --- */
84
85 #define GRAND_CRYPTO 1u /* Cryptographically strong */
86
87 /* --- Operation types --- */
88
89 enum {
90
91 /* --- Required operations --- */
92
93 GRAND_CHECK, /* @unsigned op2@ */
94
95 /* --- Standard seeding operations --- */
96
97 GRAND_SEEDINT, /* @int i@ */
98 GRAND_SEEDUINT32, /* @uint32 i@ */
99 GRAND_SEEDBLOCK, /* @const void *p, size_t sz@ */
100 GRAND_SEEDMP, /* @mp *m@ */
101 GRAND_SEEDRAND /* @grand *g@ */
102
103 /* --- Generator-specific operations --- */
104
105 #define GRAND_SPECIFIC(ch) ((unsigned)(ch) << 8)
106 };
107
108 #define GRAND_BADOP assert(((void)"bad grand_misc op", 0))
109
110 /*----- Functions provided ------------------------------------------------*/
111
112 /* --- @grand_byte@ --- *
113 *
114 * Arguments: @grand *r@ = pointet to generic generator
115 *
116 * Returns: A uniformly-distributed pseudorandom integer in the interval
117 * %$[0, 256)$%.
118 */
119
120 extern octet grand_byte(grand */*r*/);
121
122 /* --- @grand_word@ --- *
123 *
124 * Arguments: @grand *r@ = pointet to generic generator
125 *
126 * Returns: A uniformly-distributed pseudorandom integer in the interval
127 * %$[0, 2^{32})$%.
128 */
129
130 extern uint32 grand_word(grand */*r*/);
131
132 /* --- @grand_range@ --- *
133 *
134 * Arguments: @grand *r@ = pointet to generic generator
135 * @uint32 l@ = limit for acceptable results
136 *
137 * Returns: A uniformly-distributed pseudorandom integer in the interval
138 * %$[0, l)$%.
139 */
140
141 extern uint32 grand_range(grand */*r*/, uint32 /*l*/);
142
143 /* --- @grand_fill@ --- *
144 *
145 * Arguments: @grand *r@ = pointet to generic generator
146 * @void *p@ = pointer to a buffer
147 * @size_t sz@ = size of the buffer
148 *
149 * Returns: ---
150 *
151 * Use: Fills a buffer with uniformly distributed pseudorandom bytes
152 * (see @grand_byte@).
153 */
154
155 extern void grand_fill(grand */*r*/, void */*p*/, size_t /*sz*/);
156
157 /*----- That's all, folks -------------------------------------------------*/
158
159 #ifdef __cplusplus
160 }
161 #endif
162
163 #endif