Generic interface.
[u/mdw/catacomb] / bbs-jump.c
1 /* -*-c-*-
2 *
3 * $Id: bbs-jump.c,v 1.1 1999/12/10 23:14:59 mdw Exp $
4 *
5 * Jumping around a BBS sequence
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-jump.c,v $
33 * Revision 1.1 1999/12/10 23:14:59 mdw
34 * Blum-Blum-Shub generator, and Blum-Goldwasser encryption.
35 *
36 */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include "bbs.h"
41 #include "mp.h"
42 #include "mpbarrett.h"
43 #include "mpcrt.h"
44 #include "mpint.h"
45
46 /*----- Main code ---------------------------------------------------------*/
47
48 /* --- @jump@ --- *
49 *
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
55 *
56 * Returns: ---
57 *
58 * Use: Jumps a BBS context a certain number of places (assuming the
59 * arguments are right).
60 *
61 * Let the BBS modulus be %$n = pq$% and the current residue be
62 * %$x$%. Then the computations performed are:
63 *
64 * * Calculate %$x_p = x \bmod p$% and %$x_q = x \bmod q$%.
65 *
66 * * Determine %$e_p = px^n \bmod (p - 1)$% and similarly
67 * %$e_q = qx^n \bmod (p - 1)$%.
68 *
69 * * Calculate %$x_p' = x_p^{e_p} \bmod p$% and
70 * %$x_q' = x_q^{e_q} \bmod q$%.
71 *
72 * * Combine %$x_p'$% and %$x_q'$% using the Chinese Remainder
73 * Theorem.
74 *
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
79 *
80 * %$(x^2) ^ {(p + 1)/4}$%
81 * %${} = x^{(p + 1)/2}$%
82 * %${} = x \cdot x^{(p - 1)/2}$%
83 * %${} = x$%
84 *
85 * Simple, no? (Note that the division works because
86 * %$p \equiv 3 \pmod 4$%.)
87 */
88
89 static void jump(bbs *b, bbs_params *bp, unsigned long n,
90 mp *px, mp *qx)
91 {
92 mp *ep, *eq;
93 mp *v[2] = { MP_NEW, MP_NEW };
94
95 /* --- First work out the exponents --- */
96
97 {
98 mpbarrett mb;
99 mp *m;
100 mp *e;
101
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);
107 if (qx == px)
108 eq = MP_COPY(ep);
109 else {
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);
114 }
115
116 mp_drop(m);
117 mp_drop(e);
118 }
119
120 /* --- Now calculate the residues of @x@ --- */
121
122 mp_div(0, &v[0], b->x, bp->p);
123 mp_div(0, &v[1], b->x, bp->q);
124
125 /* --- Exponentiate --- */
126
127 {
128 mpbarrett mb;
129
130 mpbarrett_create(&mb, bp->p);
131 v[0] = mpbarrett_exp(&mb, v[0], v[0], ep);
132 mpbarrett_destroy(&mb);
133
134 mpbarrett_create(&mb, bp->q);
135 v[1] = mpbarrett_exp(&mb, v[1], v[1], eq);
136 mpbarrett_destroy(&mb);
137
138 mp_drop(ep);
139 mp_drop(eq);
140 }
141
142 /* --- Sort out the result using the Chinese Remainder Theorem --- */
143
144 {
145 mpcrt_mod mv[2];
146 mpcrt c;
147 int i;
148
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);
155 mpcrt_destroy(&c);
156 }
157
158 /* --- Tidy away --- */
159
160 mp_drop(v[0]);
161 mp_drop(v[1]);
162 b->r = b->x->v[0];
163 b->b = b->k;
164 }
165
166 /* --- @bbs_ff@ --- *
167 *
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
171 *
172 * Returns: ---
173 *
174 * Use: `Fast-forwards' a Blum-Blum-Shub generator by @n@ steps.
175 * Requires the factorization of the Blum modulus to do this
176 * efficiently.
177 */
178
179 void bbs_ff(bbs *b, bbs_params *bp, unsigned long n)
180 {
181 jump(b, bp, n, MP_TWO, MP_TWO);
182 }
183
184 /* --- @bbs_rew@ --- *
185 *
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
189 *
190 * Returns: ---
191 *
192 * Use: `Rewinds' a Blum-Blum-Shub generator by @n@ steps.
193 * Requires the factorization of the Blum modulus to do this
194 * at all.
195 */
196
197 void bbs_rew(bbs *b, bbs_params *bp, unsigned long n)
198 {
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);
204 mp_drop(px);
205 mp_drop(qx);
206 }
207
208 /*----- Test rig ----------------------------------------------------------*/
209
210 #ifdef TEST_RIG
211
212 static int verify(dstr *v)
213 {
214 bbs_params bp;
215 bbs b;
216 mp *x;
217 unsigned long n;
218 int ok = 1;
219 uint32 p, q, r;
220 int i;
221
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;
227
228 bbs_create(&b, bp.n, x);
229 p = bbs_bits(&b, 32);
230
231 bbs_seed(&b, x);
232 for (i = 0; i < n; i++)
233 bbs_step(&b);
234 q = bbs_bits(&b, 32);
235 bbs_wrap(&b);
236
237 bbs_rew(&b, &bp, n + (32 + b.k - 1) / b.k);
238 r = bbs_bits(&b, 32);
239
240 if (r != p) {
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);
249 ok = 0;
250 }
251
252 bbs_seed(&b, x);
253 bbs_ff(&b, &bp, n);
254 r = bbs_bits(&b, 32);
255
256 if (q != r) {
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);
265 ok = 0;
266 }
267
268 bbs_destroy(&b);
269 mp_drop(bp.p);
270 mp_drop(bp.q);
271 mp_drop(bp.n);
272 mp_drop(x);
273
274 assert(mparena_count(MPARENA_GLOBAL) == 0);
275 return (ok);
276 }
277
278 static test_chunk tests[] = {
279 { "bbs-jump", verify, { &type_mp, &type_mp, &type_mp, &type_ulong, 0 } },
280 { 0, 0, { 0 } }
281 };
282
283 int main(int argc, char *argv[])
284 {
285 sub_init();
286 test_run(argc, argv, tests, SRCDIR "/tests/bbs");
287 return (0);
288 }
289
290 #endif
291
292 /*----- That's all, folks -------------------------------------------------*/