3 * $Id: bbs.h,v 1.1 1999/12/10 23:14:59 mdw Exp $
5 * The Blum-Blum-Shub random bit generator
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 1999/12/10 23:14:59 mdw
34 * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
38 /*----- Notes on the BBS generator ----------------------------------------*
40 * The Blum-Blum-Shub generator takes the least significant bits from the
41 * sequence %$x_i = x_{i - 1}^2 \bmod n$%, where %$n = pq$% is the product of
42 * two primes %$p$% and %$q$%, each of which are congruent to %$3 \bmod 4$%.
43 * For maximum period of the generator, %$(p - 1)/2$% and %$(q - 1)/1$%
44 * should be coprime. It is safe to use the least significant %$\log \log
45 * n$% bits of each step in the sequence -- an adversary must factor the
46 * modulus before being able to work forwards or backwards. The output of
47 * the generator cannot be distinguished from a (uniform, independent) random
48 * sequence of bits using any polynomial-time test. This is by far the
49 * strongest pseudorandom number generator provided in Catacomb, and by far
50 * the slowest too. For normal use, the standard Catacomb @rand@ generator
51 * should be more than adequate.
54 #ifndef CATACOMB_BBS_H
55 #define CATACOMB_BBS_H
61 /*----- Header files ------------------------------------------------------*/
63 #include <mLib/bits.h>
65 #ifndef CATACOMB_GRAND_H
73 #ifndef CATACOMB_MPBARRETT_H
74 # include "mpbarrett.h"
77 /*----- Data structures ---------------------------------------------------*/
79 /* --- Basic generator state --- */
82 mpbarrett mb
; /* Barrett reduction context */
83 mp
*x
; /* Current quadratic residue */
84 unsigned k
; /* Number of bits from each step */
85 unsigned b
; /* Number of bits in reservoir */
86 mpw r
; /* Reservoir of output bits */
89 /* --- Parameters --- */
91 typedef struct bbs_params
{
92 mp
*p
, *q
; /* Prime factors (3 mod 4) */
93 mp
*n
; /* Product @pq@ -- a Blum integer */
96 /*----- Event codes from @bbs_gen@ ----------------------------------------*/
114 /*----- The basic generator -----------------------------------------------*/
116 /* --- @bbs_create@ --- *
118 * Arguments: @bbs *b@ = pointer to BBS generator state to initialize
119 * @mp *m@ = modulus (must be a Blum integer)
120 * @mp *x@ = initial seed for generator
124 * Use: Initializes a BBS generator. The generator is stepped once
125 * after initialization, as for @bbs_seed@.
128 extern void bbs_create(bbs */
*b*/
, mp */
*m*/
, mp */
*x*/
);
130 /* --- @bbs_destroy@ --- *
132 * Arguments: @bbs *b@ = pointer to BBS generator state
136 * Use: Destroys a generator state when it's no longer wanted.
139 extern void bbs_destroy(bbs */
*b*/
);
141 /* --- @bbs_step@ --- *
143 * Arguments: @bbs *b@ = pointer to BBS generator state
147 * Use: Steps the generator once. This isn't too useful in client
151 extern void bbs_step(bbs */
*b*/
);
153 /* --- @bbs_set@ --- *
155 * Arguments: @bbs *b@ = pointer to BBS generator state
156 * @mp *x@ = new residue to set
160 * Use: Sets a new quadratic residue. The generator is stepped once.
163 extern void bbs_set(bbs */
*b*/
, mp */
*x*/
);
165 /* --- @bbs_seed@ --- *
167 * Arguments: @bbs *b@ = pointer to BBS generator state
168 * @mp *x@ = new seed to set
172 * Use: Sets a new seed. The generator is stepped until the residue
173 * has clearly wrapped around.
176 extern void bbs_seed(bbs */
*b*/
, mp */
*x*/
);
178 /* --- @bbs_bits@ --- *
180 * Arguments: @bbs *b@ = pointer to BBS generator state
181 * @unsigned bits@ = number of bits wanted
183 * Returns: Bits extracted from the BBS generator.
185 * Use: Extracts a requested number of bits from the BBS generator.
188 extern uint32
bbs_bits(bbs */
*b*/
, unsigned /*bits*/);
190 /* --- @bbs_wrap@ --- *
192 * Arguments: @bbs *b@ = pointer to BBS generator state
196 * Use: Steps the generator if any of the reservoir bits are used.
197 * This can be used to `wrap up' after a Blum-Goldwasser
198 * encryption, for example, producing the final value to be sent
199 * along with the ciphertext.
201 * If a generator is seeded, %$b$% bits are extracted, and then
202 * @bbs_wrap@ is called, the generator will have been stepped
203 * %$\lceil b/k \rceil% times.
206 extern void bbs_wrap(bbs */
*b*/
);
208 /*----- Large forwards and backwards jumps --------------------------------*/
210 /* --- @bbs_ff@ --- *
212 * Arguments: @bbs *b@ = pointer to a BBS generator state
213 * @bbs_params *bp@ = pointer to BBS modulus factors
214 * @unsigned long n@ = number of steps to make
218 * Use: `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
219 * Requires the factorization of the Blum modulus to do this
223 extern void bbs_ff(bbs */
*b*/
, bbs_params */
*bp*/
, unsigned long /*n*/);
225 /* --- @bbs_rew@ --- *
227 * Arguments: @bbs *b@ = pointer to a BBS generator state
228 * @bbs_params *bp@ = pointer to BBS modulus factors
229 * @unsigned long n@ = number of steps to make
233 * Use: `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
234 * Requires the factorization of the Blum modulus to do this
238 extern void bbs_rew(bbs */
*b*/
, bbs_params */
*bp*/
, unsigned long /*n*/);
240 /*----- Parameter generation ----------------------------------------------*/
242 /* --- @bbs_gen@ --- *
244 * Arguments: @bbs_params *bp@ = pointer to parameter block
245 * @mp *p, *q@ = initial numbers to search from
246 * @size_t n@ = number of attempts to make
247 * @void (*proc)(int ev, mp *m, void *p)@ = event handler
248 * @void *arg@ = argument for event handler
250 * Returns: Zero if all went well, otherwise an event code which explains
253 * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
254 * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
255 * %$(q - 1)/2$% have no common factors. The product %$n = pq$%
256 * is eminently suitable for use as a modulus in a Blum-Blum-
257 * Shub pseudorandom bit generator.
260 extern int bbs_gen(bbs_params */
*bp*/
, mp */
*p*/
, mp */
*q*/
, size_t /*n*/,
261 int (*/
*proc*/
)(int /*ev*/, mp */
*m*/
, void */
*p*/
),
264 /*----- Generic random number generator interface -------------------------*/
266 /* --- @bbs_rand@ --- *
268 * Arguments: @mp *m@ = modulus
269 * @mp *x@ = initial seed
271 * Returns: Pointer to a generic generator.
273 * Use: Constructs a generic generator interface over a
274 * Blum-Blum-Shub generator.
277 extern grand
*bbs_rand(mp */
*m*/
, mp */
*x*/
);
279 /* --- Blum-Blum-Shub-specific misc op codes --- */
282 BBS_SET
= GRAND_SPECIFIC
/* @mp *x@ */
285 /*----- That's all, folks -------------------------------------------------*/