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