chiark / gitweb /
Rename `bbs_params' to `bbs_param' for consistency.
[catacomb] / bbs.h
1 /* -*-c-*-
2  *
3  * $Id: bbs.h,v 1.2 1999/12/22 15:52:08 mdw Exp $
4  *
5  * The Blum-Blum-Shub random bit generator
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: bbs.h,v $
33  * Revision 1.2  1999/12/22 15:52:08  mdw
34  * Rename `bbs_params' to `bbs_param' for consistency.
35  *
36  * Revision 1.1  1999/12/10 23:14:59  mdw
37  * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
38  *
39  */
40
41 /*----- Notes on the BBS generator ----------------------------------------*
42  *
43  * The Blum-Blum-Shub generator takes the least significant bits from the
44  * sequence %$x_i = x_{i - 1}^2 \bmod n$%, where %$n = pq$% is the product of
45  * two primes %$p$% and %$q$%, each of which are congruent to %$3 \bmod 4$%.
46  * For maximum period of the generator, %$(p - 1)/2$% and %$(q - 1)/1$%
47  * should be coprime.  It is safe to use the least significant %$\log \log
48  * n$% bits of each step in the sequence -- an adversary must factor the
49  * modulus before being able to work forwards or backwards.  The output of
50  * the generator cannot be distinguished from a (uniform, independent) random
51  * sequence of bits using any polynomial-time test.  This is by far the
52  * strongest pseudorandom number generator provided in Catacomb, and by far
53  * the slowest too.  For normal use, the standard Catacomb @rand@ generator
54  * should be more than adequate.
55  */
56
57 #ifndef CATACOMB_BBS_H
58 #define CATACOMB_BBS_H
59
60 #ifdef __cplusplus
61   extern "C" {
62 #endif
63
64 /*----- Header files ------------------------------------------------------*/
65
66 #include <mLib/bits.h>
67
68 #ifndef CATACOMB_GRAND_H
69 #  include "grand.h"
70 #endif
71
72 #ifndef CATACOMB_MP_H
73 #  include "mp.h"
74 #endif
75
76 #ifndef CATACOMB_MPBARRETT_H
77 #  include "mpbarrett.h"
78 #endif
79
80 #ifndef CATACOMB_PGEN_H
81 #  include "pgen.h"
82 #endif
83
84 /*----- Data structures ---------------------------------------------------*/
85
86 /* --- Basic generator state --- */
87
88 typedef struct bbs {
89   mpbarrett mb;                         /* Barrett reduction context */
90   mp *x;                                /* Current quadratic residue */
91   unsigned k;                           /* Number of bits from each step */
92   unsigned b;                           /* Number of bits in reservoir */
93   mpw r;                                /* Reservoir of output bits */
94 } bbs;
95
96 /* --- Parameters --- */
97
98 typedef struct bbs_param {
99   mp *p, *q;                            /* Prime factors (3 mod 4) */
100   mp *n;                                /* Product @pq@ -- a Blum integer */
101 } bbs_param;
102
103 /*----- The basic generator -----------------------------------------------*/
104
105 /* --- @bbs_create@ --- *
106  *
107  * Arguments:   @bbs *b@ = pointer to BBS generator state to initialize
108  *              @mp *m@ = modulus (must be a Blum integer)
109  *              @mp *x@ = initial seed for generator
110  *
111  * Returns:     ---
112  *
113  * Use:         Initializes a BBS generator.  The generator is stepped once
114  *              after initialization, as for @bbs_seed@.
115  */
116
117 extern void bbs_create(bbs */*b*/, mp */*m*/, mp */*x*/);
118
119 /* --- @bbs_destroy@ --- *
120  *
121  * Arguments:   @bbs *b@ = pointer to BBS generator state
122  *
123  * Returns:     ---
124  *
125  * Use:         Destroys a generator state when it's no longer wanted.
126  */
127
128 extern void bbs_destroy(bbs */*b*/);
129
130 /* --- @bbs_step@ --- *
131  *
132  * Arguments:   @bbs *b@ = pointer to BBS generator state
133  *
134  * Returns:     ---
135  *
136  * Use:         Steps the generator once.  This isn't too useful in client
137  *              code.
138  */
139
140 extern void bbs_step(bbs */*b*/);
141
142 /* --- @bbs_set@ --- *
143  *
144  * Arguments:   @bbs *b@ = pointer to BBS generator state
145  *              @mp *x@ = new residue to set
146  *
147  * Returns:     ---
148  *
149  * Use:         Sets a new quadratic residue.  The generator is stepped once.
150  */
151
152 extern void bbs_set(bbs */*b*/, mp */*x*/);
153
154 /* --- @bbs_seed@ --- *
155  *
156  * Arguments:   @bbs *b@ = pointer to BBS generator state
157  *              @mp *x@ = new seed to set
158  *
159  * Returns      ---
160  *
161  * Use:         Sets a new seed.  The generator is stepped until the residue
162  *              has clearly wrapped around.
163  */
164
165 extern void bbs_seed(bbs */*b*/, mp */*x*/);
166
167 /* --- @bbs_bits@ --- *
168  *
169  * Arguments:   @bbs *b@ = pointer to BBS generator state
170  *              @unsigned bits@ = number of bits wanted
171  *
172  * Returns:     Bits extracted from the BBS generator.
173  *
174  * Use:         Extracts a requested number of bits from the BBS generator.
175  */
176
177 extern uint32 bbs_bits(bbs */*b*/, unsigned /*bits*/);
178
179 /* --- @bbs_wrap@ --- *
180  *
181  * Arguments:   @bbs *b@ = pointer to BBS generator state
182  *
183  * Returns:     ---
184  *
185  * Use:         Steps the generator if any of the reservoir bits are used.
186  *              This can be used to `wrap up' after a Blum-Goldwasser
187  *              encryption, for example, producing the final value to be sent
188  *              along with the ciphertext.
189  *
190  *              If a generator is seeded, %$b$% bits are extracted, and then
191  *              @bbs_wrap@ is called, the generator will have been stepped
192  *              %$\lceil b/k \rceil% times.
193  */
194
195 extern void bbs_wrap(bbs */*b*/);
196
197 /*----- Large forwards and backwards jumps --------------------------------*/
198
199 /* --- @bbs_ff@ --- *
200  *
201  * Arguments:   @bbs *b@ = pointer to a BBS generator state
202  *              @bbs_param *bp@ = pointer to BBS modulus factors
203  *              @unsigned long n@ = number of steps to make
204  *
205  * Returns:     ---
206  *
207  * Use:         `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
208  *              Requires the factorization of the Blum modulus to do this
209  *              efficiently.
210  */
211
212 extern void bbs_ff(bbs */*b*/, bbs_param */*bp*/, unsigned long /*n*/);
213
214 /* --- @bbs_rew@ --- *
215  *
216  * Arguments:   @bbs *b@ = pointer to a BBS generator state
217  *              @bbs_param *bp@ = pointer to BBS modulus factors
218  *              @unsigned long n@ = number of steps to make
219  *
220  * Returns:     ---
221  *
222  * Use:         `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
223  *              Requires the factorization of the Blum modulus to do this
224  *              at all.
225  */
226
227 extern void bbs_rew(bbs */*b*/, bbs_param */*bp*/, unsigned long /*n*/);
228
229 /*----- Parameter generation ----------------------------------------------*/
230
231 /* --- @bbs_gen@ --- *
232  *
233  * Arguments:   @bbs_param *bp@ = pointer to parameter block
234  *              @mp *p, *q@ = initial numbers to search from
235  *              @size_t n@ = number of attempts to make
236  *              @pgen_proc *event@ = event handler function
237  *              @void *ectx@ = argument for event handler
238  *
239  * Returns:     If it worked OK, @PGEN_DONE@, otherwise @PGEN_ABORT@.
240  *
241  * Use:         Finds two prime numbers %$p'$% and %$q'$% such that both are
242  *              congruent to %$3 \bmod 4$%, and  $(p - 1)/2$% and
243  *              %$(q - 1)/2$% have no common factors.  The product %$n = pq$%
244  *              is eminently suitable for use as a modulus in a Blum-Blum-
245  *              Shub pseudorandom bit generator.
246  */
247
248 extern int bbs_gen(bbs_param */*bp*/, mp */*p*/, mp */*q*/, size_t /*n*/,
249                    pgen_proc */*event*/, void */*ectx*/);
250
251 /*----- Generic random number generator interface -------------------------*/
252
253 /* --- @bbs_rand@ --- *
254  *
255  * Arguments:   @mp *m@ = modulus
256  *              @mp *x@ = initial seed
257  *
258  * Returns:     Pointer to a generic generator.
259  *
260  * Use:         Constructs a generic generator interface over a
261  *              Blum-Blum-Shub generator.
262  */
263
264 extern grand *bbs_rand(mp */*m*/, mp */*x*/);
265
266 /* --- Blum-Blum-Shub-specific misc op codes --- */
267
268 enum {
269   BBS_SET = GRAND_SPECIFIC              /* @mp *x@ */
270 };
271
272 /*----- That's all, folks -------------------------------------------------*/
273
274 #ifdef __cplusplus
275   }
276 #endif
277
278 #endif