math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / rand / grand.h
CommitLineData
aa1082f2 1/* -*-c-*-
2 *
aa1082f2 3 * Generic interface to random number generators
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
aa1082f2 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.
45c0fd36 16 *
aa1082f2 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.
45c0fd36 21 *
aa1082f2 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
aa1082f2 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
44typedef struct grand {
45 const struct grand_ops *ops;
46} grand;
47
48typedef struct grand_ops {
49
50 /* --- Various important properties --- */
51
52 const char *name; /* Generator's name */
159bb2d9 53 unsigned f; /* Various flags */
aa1082f2 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)$% */
159bb2d9 69 octet (*byte)(grand */*r*/); /* Uniform over %$[0, 256)$% */
aa1082f2 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
b817bfc6 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
159bb2d9 81/* --- Flag types --- */
82
76ba9c8f 83#define GRAND_CRYPTO 1u /* Cryptographically strong */
159bb2d9 84
aa1082f2 85/* --- Operation types --- */
86
87enum {
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@ */
1709c9a1 99 GRAND_SEEDRAND /* @grand *g@ */
aa1082f2 100
101 /* --- Generator-specific operations --- */
102
45c0fd36 103#define GRAND_SPECIFIC(ch) ((unsigned)(ch) << 8)
aa1082f2 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
118extern 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
128extern 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
139extern 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
153extern 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