--- /dev/null
+/* -*-c-*-
+ *
+ * $Id: bbs.h,v 1.1 1999/12/10 23:14:59 mdw Exp $
+ *
+ * The Blum-Blum-Shub random bit generator
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------*
+ *
+ * $Log: bbs.h,v $
+ * Revision 1.1 1999/12/10 23:14:59 mdw
+ * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
+ *
+ */
+
+/*----- Notes on the BBS generator ----------------------------------------*
+ *
+ * The Blum-Blum-Shub generator takes the least significant bits from the
+ * sequence %$x_i = x_{i - 1}^2 \bmod n$%, where %$n = pq$% is the product of
+ * two primes %$p$% and %$q$%, each of which are congruent to %$3 \bmod 4$%.
+ * For maximum period of the generator, %$(p - 1)/2$% and %$(q - 1)/1$%
+ * should be coprime. It is safe to use the least significant %$\log \log
+ * n$% bits of each step in the sequence -- an adversary must factor the
+ * modulus before being able to work forwards or backwards. The output of
+ * the generator cannot be distinguished from a (uniform, independent) random
+ * sequence of bits using any polynomial-time test. This is by far the
+ * strongest pseudorandom number generator provided in Catacomb, and by far
+ * the slowest too. For normal use, the standard Catacomb @rand@ generator
+ * should be more than adequate.
+ */
+
+#ifndef CATACOMB_BBS_H
+#define CATACOMB_BBS_H
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <mLib/bits.h>
+
+#ifndef CATACOMB_GRAND_H
+# include "grand.h"
+#endif
+
+#ifndef CATACOMB_MP_H
+# include "mp.h"
+#endif
+
+#ifndef CATACOMB_MPBARRETT_H
+# include "mpbarrett.h"
+#endif
+
+/*----- Data structures ---------------------------------------------------*/
+
+/* --- Basic generator state --- */
+
+typedef struct bbs {
+ mpbarrett mb; /* Barrett reduction context */
+ mp *x; /* Current quadratic residue */
+ unsigned k; /* Number of bits from each step */
+ unsigned b; /* Number of bits in reservoir */
+ mpw r; /* Reservoir of output bits */
+} bbs;
+
+/* --- Parameters --- */
+
+typedef struct bbs_params {
+ mp *p, *q; /* Prime factors (3 mod 4) */
+ mp *n; /* Product @pq@ -- a Blum integer */
+} bbs_params;
+
+/*----- Event codes from @bbs_gen@ ----------------------------------------*/
+
+enum {
+ BBSEV_OK,
+
+ BBSEV_FINDP,
+ BBSEV_TRYP,
+ BBSEV_PASSP,
+ BBSEV_FAILP,
+ BBSEV_GOODP,
+
+ BBSEV_FINDQ,
+ BBSEV_TRYQ,
+ BBSEV_PASSQ,
+ BBSEV_FAILQ,
+ BBSEV_GOODQ
+};
+
+/*----- The basic generator -----------------------------------------------*/
+
+/* --- @bbs_create@ --- *
+ *
+ * Arguments: @bbs *b@ = pointer to BBS generator state to initialize
+ * @mp *m@ = modulus (must be a Blum integer)
+ * @mp *x@ = initial seed for generator
+ *
+ * Returns: ---
+ *
+ * Use: Initializes a BBS generator. The generator is stepped once
+ * after initialization, as for @bbs_seed@.
+ */
+
+extern void bbs_create(bbs */*b*/, mp */*m*/, mp */*x*/);
+
+/* --- @bbs_destroy@ --- *
+ *
+ * Arguments: @bbs *b@ = pointer to BBS generator state
+ *
+ * Returns: ---
+ *
+ * Use: Destroys a generator state when it's no longer wanted.
+ */
+
+extern void bbs_destroy(bbs */*b*/);
+
+/* --- @bbs_step@ --- *
+ *
+ * Arguments: @bbs *b@ = pointer to BBS generator state
+ *
+ * Returns: ---
+ *
+ * Use: Steps the generator once. This isn't too useful in client
+ * code.
+ */
+
+extern void bbs_step(bbs */*b*/);
+
+/* --- @bbs_set@ --- *
+ *
+ * Arguments: @bbs *b@ = pointer to BBS generator state
+ * @mp *x@ = new residue to set
+ *
+ * Returns: ---
+ *
+ * Use: Sets a new quadratic residue. The generator is stepped once.
+ */
+
+extern void bbs_set(bbs */*b*/, mp */*x*/);
+
+/* --- @bbs_seed@ --- *
+ *
+ * Arguments: @bbs *b@ = pointer to BBS generator state
+ * @mp *x@ = new seed to set
+ *
+ * Returns ---
+ *
+ * Use: Sets a new seed. The generator is stepped until the residue
+ * has clearly wrapped around.
+ */
+
+extern void bbs_seed(bbs */*b*/, mp */*x*/);
+
+/* --- @bbs_bits@ --- *
+ *
+ * Arguments: @bbs *b@ = pointer to BBS generator state
+ * @unsigned bits@ = number of bits wanted
+ *
+ * Returns: Bits extracted from the BBS generator.
+ *
+ * Use: Extracts a requested number of bits from the BBS generator.
+ */
+
+extern uint32 bbs_bits(bbs */*b*/, unsigned /*bits*/);
+
+/* --- @bbs_wrap@ --- *
+ *
+ * Arguments: @bbs *b@ = pointer to BBS generator state
+ *
+ * Returns: ---
+ *
+ * Use: Steps the generator if any of the reservoir bits are used.
+ * This can be used to `wrap up' after a Blum-Goldwasser
+ * encryption, for example, producing the final value to be sent
+ * along with the ciphertext.
+ *
+ * If a generator is seeded, %$b$% bits are extracted, and then
+ * @bbs_wrap@ is called, the generator will have been stepped
+ * %$\lceil b/k \rceil% times.
+ */
+
+extern void bbs_wrap(bbs */*b*/);
+
+/*----- Large forwards and backwards jumps --------------------------------*/
+
+/* --- @bbs_ff@ --- *
+ *
+ * Arguments: @bbs *b@ = pointer to a BBS generator state
+ * @bbs_params *bp@ = pointer to BBS modulus factors
+ * @unsigned long n@ = number of steps to make
+ *
+ * Returns: ---
+ *
+ * Use: `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
+ * Requires the factorization of the Blum modulus to do this
+ * efficiently.
+ */
+
+extern void bbs_ff(bbs */*b*/, bbs_params */*bp*/, unsigned long /*n*/);
+
+/* --- @bbs_rew@ --- *
+ *
+ * Arguments: @bbs *b@ = pointer to a BBS generator state
+ * @bbs_params *bp@ = pointer to BBS modulus factors
+ * @unsigned long n@ = number of steps to make
+ *
+ * Returns: ---
+ *
+ * Use: `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
+ * Requires the factorization of the Blum modulus to do this
+ * at all.
+ */
+
+extern void bbs_rew(bbs */*b*/, bbs_params */*bp*/, unsigned long /*n*/);
+
+/*----- Parameter generation ----------------------------------------------*/
+
+/* --- @bbs_gen@ --- *
+ *
+ * Arguments: @bbs_params *bp@ = pointer to parameter block
+ * @mp *p, *q@ = initial numbers to search from
+ * @size_t n@ = number of attempts to make
+ * @void (*proc)(int ev, mp *m, void *p)@ = event handler
+ * @void *arg@ = argument for event handler
+ *
+ * Returns: Zero if all went well, otherwise an event code which explains
+ * the problem.
+ *
+ * Use: Finds two prime numbers %$p'$% and %$q'$% such that both are
+ * congruent to %$3 \bmod 4$%, and $(p - 1)/2$% and
+ * %$(q - 1)/2$% have no common factors. The product %$n = pq$%
+ * is eminently suitable for use as a modulus in a Blum-Blum-
+ * Shub pseudorandom bit generator.
+ */
+
+extern int bbs_gen(bbs_params */*bp*/, mp */*p*/, mp */*q*/, size_t /*n*/,
+ int (*/*proc*/)(int /*ev*/, mp */*m*/, void */*p*/),
+ void */*arg*/);
+
+/*----- Generic random number generator interface -------------------------*/
+
+/* --- @bbs_rand@ --- *
+ *
+ * Arguments: @mp *m@ = modulus
+ * @mp *x@ = initial seed
+ *
+ * Returns: Pointer to a generic generator.
+ *
+ * Use: Constructs a generic generator interface over a
+ * Blum-Blum-Shub generator.
+ */
+
+extern grand *bbs_rand(mp */*m*/, mp */*x*/);
+
+/* --- Blum-Blum-Shub-specific misc op codes --- */
+
+enum {
+ BBS_SET = GRAND_SPECIFIC /* @mp *x@ */
+};
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif