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