3 * $Id: bbs-jump.c,v 1.1 1999/12/10 23:14:59 mdw Exp $
5 * Jumping around a BBS sequence
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 --------------------------------------------------*
32 * $Log: bbs-jump.c,v $
33 * Revision 1.1 1999/12/10 23:14:59 mdw
34 * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
38 /*----- Header files ------------------------------------------------------*/
42 #include "mpbarrett.h"
46 /*----- Main code ---------------------------------------------------------*/
50 * Arguments: @bbs *b@ = pointer to BBS generator context
51 * @bbs_params *bp@ = pointer to BBS modulus factors
52 * @unsigned long n@ = number of steps to move
53 * @mp *px@ = exponent mod @p@ for a one-step jump
54 * @mp *qx@ = exponent mod @q@ for a one-step jump
58 * Use: Jumps a BBS context a certain number of places (assuming the
59 * arguments are right).
61 * Let the BBS modulus be %$n = pq$% and the current residue be
62 * %$x$%. Then the computations performed are:
64 * * Calculate %$x_p = x \bmod p$% and %$x_q = x \bmod q$%.
66 * * Determine %$e_p = px^n \bmod (p - 1)$% and similarly
67 * %$e_q = qx^n \bmod (p - 1)$%.
69 * * Calculate %$x_p' = x_p^{e_p} \bmod p$% and
70 * %$x_q' = x_q^{e_q} \bmod q$%.
72 * * Combine %$x_p'$% and %$x_q'$% using the Chinese Remainder
75 * If you want to step the generator forwards, simply set
76 * %$px = qx = 2$%. If you want to step backwards, make
77 * %$px = (p + 1)/4$% and %$qx = (q + 1)/4%$. Note that, if
78 * %$x$% is a quadratic residue mod $%p$%, then
80 * %$(x^2) ^ {(p + 1)/4}$%
81 * %${} = x^{(p + 1)/2}$%
82 * %${} = x \cdot x^{(p - 1)/2}$%
85 * Simple, no? (Note that the division works because
86 * %$p \equiv 3 \pmod 4$%.)
89 static void jump(bbs
*b
, bbs_params
*bp
, unsigned long n
,
93 mp
*v
[2] = { MP_NEW
, MP_NEW
};
95 /* --- First work out the exponents --- */
102 e
= mp_fromulong(MP_NEW
, n
);
103 m
= mp_sub(MP_NEW
, bp
->p
, MP_ONE
);
104 mpbarrett_create(&mb
, m
);
105 ep
= mpbarrett_exp(&mb
, MP_NEW
, px
, e
);
106 mpbarrett_destroy(&mb
);
110 m
= mp_sub(m
, bp
->q
, MP_ONE
);
111 mpbarrett_create(&mb
, m
);
112 eq
= mpbarrett_exp(&mb
, MP_NEW
, qx
, e
);
113 mpbarrett_destroy(&mb
);
120 /* --- Now calculate the residues of @x@ --- */
122 mp_div(0, &v
[0], b
->x
, bp
->p
);
123 mp_div(0, &v
[1], b
->x
, bp
->q
);
125 /* --- Exponentiate --- */
130 mpbarrett_create(&mb
, bp
->p
);
131 v
[0] = mpbarrett_exp(&mb
, v
[0], v
[0], ep
);
132 mpbarrett_destroy(&mb
);
134 mpbarrett_create(&mb
, bp
->q
);
135 v
[1] = mpbarrett_exp(&mb
, v
[1], v
[1], eq
);
136 mpbarrett_destroy(&mb
);
142 /* --- Sort out the result using the Chinese Remainder Theorem --- */
149 mv
[0].m
= MP_COPY(bp
->p
);
150 mv
[1].m
= MP_COPY(bp
->q
);
151 for (i
= 0; i
< 2; i
++)
152 mv
[i
].n
= mv
[i
].ni
= mv
[i
].nni
= MP_NEW
;
153 mpcrt_create(&c
, mv
, 2, b
->mb
.m
);
154 b
->x
= mpcrt_solve(&c
, b
->x
, v
);
158 /* --- Tidy away --- */
166 /* --- @bbs_ff@ --- *
168 * Arguments: @bbs *b@ = pointer to a BBS generator state
169 * @bbs_params *bp@ = pointer to BBS modulus factors
170 * @unsigned long n@ = number of steps to make
174 * Use: `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
175 * Requires the factorization of the Blum modulus to do this
179 void bbs_ff(bbs
*b
, bbs_params
*bp
, unsigned long n
)
181 jump(b
, bp
, n
, MP_TWO
, MP_TWO
);
184 /* --- @bbs_rew@ --- *
186 * Arguments: @bbs *b@ = pointer to a BBS generator state
187 * @bbs_params *bp@ = pointer to BBS modulus factors
188 * @unsigned long n@ = number of steps to make
192 * Use: `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
193 * Requires the factorization of the Blum modulus to do this
197 void bbs_rew(bbs
*b
, bbs_params
*bp
, unsigned long n
)
199 mp
*px
= mp_lsr(MP_NEW
, bp
->p
, 2);
200 mp
*qx
= mp_lsr(MP_NEW
, bp
->q
, 2);
201 px
= mp_add(px
, px
, MP_ONE
);
202 qx
= mp_add(qx
, qx
, MP_ONE
);
203 jump(b
, bp
, n
, px
, qx
);
208 /*----- Test rig ----------------------------------------------------------*/
212 static int verify(dstr
*v
)
222 bp
.p
= *(mp
**)v
[0].buf
;
223 bp
.q
= *(mp
**)v
[1].buf
;
224 bp
.n
= mp_mul(MP_NEW
, bp
.p
, bp
.q
);
225 x
= *(mp
**)v
[2].buf
;
226 n
= *(unsigned long *)v
[3].buf
;
228 bbs_create(&b
, bp
.n
, x
);
229 p
= bbs_bits(&b
, 32);
232 for (i
= 0; i
< n
; i
++)
234 q
= bbs_bits(&b
, 32);
237 bbs_rew(&b
, &bp
, n
+ (32 + b
.k
- 1) / b
.k
);
238 r
= bbs_bits(&b
, 32);
241 fputs("\n*** bbs rewind failure\n", stderr
);
242 fputs("p = ", stderr
); mp_writefile(bp
.p
, stderr
, 10); fputc('\n', stderr
);
243 fputs("q = ", stderr
); mp_writefile(bp
.q
, stderr
, 10); fputc('\n', stderr
);
244 fputs("n = ", stderr
); mp_writefile(bp
.n
, stderr
, 10); fputc('\n', stderr
);
245 fputs("x = ", stderr
); mp_writefile(x
, stderr
, 10); fputc('\n', stderr
);
246 fprintf(stderr
, "stepped %lu back\n", n
+ (32 + b
.k
- 1) / b
.k
);
247 fprintf(stderr
, "expected output = %08lx, found %08lx\n",
248 (unsigned long)p
, (unsigned long)r
);
254 r
= bbs_bits(&b
, 32);
257 fputs("\n*** bbs fastforward failure\n", stderr
);
258 fputs("p = ", stderr
); mp_writefile(bp
.p
, stderr
, 10); fputc('\n', stderr
);
259 fputs("q = ", stderr
); mp_writefile(bp
.q
, stderr
, 10); fputc('\n', stderr
);
260 fputs("n = ", stderr
); mp_writefile(bp
.n
, stderr
, 10); fputc('\n', stderr
);
261 fputs("x = ", stderr
); mp_writefile(x
, stderr
, 10); fputc('\n', stderr
);
262 fprintf(stderr
, "stepped %lu back\n", n
+ (32 + b
.k
- 1) / b
.k
);
263 fprintf(stderr
, "expected output = %08lx, found %08lx\n",
264 (unsigned long)q
, (unsigned long)r
);
274 assert(mparena_count(MPARENA_GLOBAL
) == 0);
278 static test_chunk tests
[] = {
279 { "bbs-jump", verify
, { &type_mp
, &type_mp
, &type_mp
, &type_ulong
, 0 } },
283 int main(int argc
, char *argv
[])
286 test_run(argc
, argv
, tests
, SRCDIR
"/tests/bbs");
292 /*----- That's all, folks -------------------------------------------------*/