Miscellaneous constification.
[u/mdw/catacomb] / rho.c
1 /* -*-c-*-
2 *
3 * $Id: rho.c,v 1.4 2004/04/02 01:03:49 mdw Exp $
4 *
5 * Pollard's rho algorithm for discrete logs
6 *
7 * (c) 2000 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: rho.c,v $
33 * Revision 1.4 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
35 *
36 * Revision 1.3 2001/06/16 12:56:38 mdw
37 * Fixes for interface change to @mpmont_expr@ and @mpmont_mexpr@.
38 *
39 * Revision 1.2 2000/10/08 12:11:22 mdw
40 * Use @MP_EQ@ instead of @MP_CMP@.
41 *
42 * Revision 1.1 2000/07/09 21:32:30 mdw
43 * Pollard's rho algorithm for computing discrete logs.
44 *
45 */
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include "fibrand.h"
50 #include "mp.h"
51 #include "mpmont.h"
52 #include "mprand.h"
53 #include "rho.h"
54
55 /*----- Main code ---------------------------------------------------------*/
56
57 /* --- @rho@ --- *
58 *
59 * Arguments: @rho_ctx *cc@ = pointer to the context structure
60 * @void *x, *y@ = two (equal) base values (try 1)
61 * @mp *a, *b@ = logs of %$x$% (see below)
62 *
63 * Returns: The discrete logarithm %$\log_g a$%, or null if the algorithm
64 * failed. (This is unlikely, though possible.)
65 *
66 * Use: Uses Pollard's rho algorithm to compute discrete logs in the
67 * group %$G$% generated by %$g$%.
68 *
69 * The algorithm works by finding a cycle in a pseudo-random
70 * walk. The function @ops->split@ should return an element
71 * from %$\{\,0, 1, 2\,\}$% according to its argument, in order
72 * to determine the walk. At each step in the walk, we know a
73 * group element %$x \in G$% together with its representation as
74 * a product of powers of %$g$% and $%a$% (i.e., we know that
75 * %$x = g^\alpha a^\beta$% for some %$\alpha$%, %$\beta$%).
76 *
77 * Locating a cycle gives us a collision
78 *
79 * %$g^{\alpha} a^{\beta} = g^{\alpha'} a^{\beta'}$%
80 *
81 * Taking logs of both sides (to base %$g$%) gives us that
82 *
83 * %$\log a\equiv\frac{\alpha-\alpha'}{\beta'-\beta}\bmod{n}$%
84 *
85 * Good initial values are %$x = y = 1$% (the multiplicative
86 * identity of %$G$%) and %$\alpha\equiv\beta\equiv0\bmod{n}$%.
87 * If that doesn't work then start choosing more `interesting'
88 * values.
89 *
90 * Note that the algorithm requires minimal space but
91 * %$O(\sqrt{n})$% time. Don't do this on large groups,
92 * particularly if you can find a decent factor base.
93 *
94 * Finally, note that this function will free the input values
95 * when it's finished with them. This probably isn't a great
96 * problem.
97 */
98
99 static void step(rho_ctx *cc, void *x, mp **a, mp **b)
100 {
101 switch (cc->ops->split(x)) {
102 case 0:
103 cc->ops->mul(x, cc->g, cc->c);
104 *a = mp_add(*a, *a, MP_ONE);
105 if (MP_CMP(*a, >=, cc->n))
106 *a = mp_sub(*a, *a, cc->n);
107 break;
108 case 1:
109 cc->ops->sqr(x, cc->c);
110 *a = mp_lsl(*a, *a, 1);
111 if (MP_CMP(*a, >=, cc->n))
112 *a = mp_sub(*a, *a, cc->n);
113 *b = mp_lsl(*b, *b, 1);
114 if (MP_CMP(*b, >=, cc->n))
115 *b = mp_sub(*b, *b, cc->n);
116 break;
117 case 2:
118 cc->ops->mul(x, cc->a, cc->c);
119 *b = mp_add(*b, *b, MP_ONE);
120 if (MP_CMP(*b, >=, cc->n))
121 *b = mp_sub(*b, *b, cc->n);
122 break;
123 }
124 }
125
126 mp *rho(rho_ctx *cc, void *x, void *y, mp *a, mp *b)
127 {
128 mp *aa = MP_COPY(a), *bb = MP_COPY(b);
129 mp *g;
130
131 /* --- Grind through the random walk until we find a collision --- */
132
133 do {
134 step(cc, x, &a, &b);
135 step(cc, y, &aa, &bb);
136 step(cc, y, &aa, &bb);
137 } while (!cc->ops->eq(x, y));
138 cc->ops->drop(x);
139 cc->ops->drop(y);
140
141 /* --- Now sort out the mess --- */
142
143 aa = mp_sub(aa, a, aa);
144 bb = mp_sub(bb, bb, b);
145 g = MP_NEW;
146 mp_gcd(&g, &bb, 0, bb, cc->n);
147 if (!MP_EQ(g, MP_ONE)) {
148 mp_drop(aa);
149 aa = 0;
150 } else {
151 aa = mp_mul(aa, aa, bb);
152 mp_div(0, &aa, aa, cc->n);
153 }
154
155 /* --- Done --- */
156
157 mp_drop(bb);
158 mp_drop(g);
159 mp_drop(a);
160 mp_drop(b);
161 return (aa);
162 }
163
164 /* --- @rho_prime@ --- *
165 *
166 * Arguments: @mp *g@ = generator for the group
167 * @mp *a@ = value to find the logarithm of
168 * @mp *n@ = order of the group
169 * @mp *p@ = prime size of the underlying prime field
170 *
171 * Returns: The discrete logarithm %$\log_g a$%.
172 *
173 * Use: Computes discrete logarithms in a subgroup of a prime field.
174 */
175
176 static void prime_sqr(void *x, void *c)
177 {
178 mp **p = x;
179 mp *a = *p;
180 a = mp_sqr(a, a);
181 a = mpmont_reduce(c, a, a);
182 *p = a;
183 }
184
185 static void prime_mul(void *x, void *y, void *c)
186 {
187 mp **p = x;
188 mp *a = *p;
189 a = mpmont_mul(c, a, a, y);
190 *p = a;
191 }
192
193 static int prime_eq(void *x, void *y)
194 {
195 return (MP_EQ(*(mp **)x, *(mp **)y));
196 }
197
198 static int prime_split(void *x)
199 {
200 /* --- Notes on the splitting function --- *
201 *
202 * The objective is to produce a simple pseudorandom mapping from the
203 * underlying field \gf{p} to \{\,0, 1, 2\,\}$%. This is further
204 * constrained by the fact that we must not have %$1 \mapsto 1$% (since
205 * otherwise the stepping function above will loop).
206 *
207 * The function we choose is very simple: we take the least significant
208 * word from the integer, add one (to prevent the %$1 \mapsto 1$% property
209 * described above) and reduce modulo 3. This is slightly biased against
210 * the result 2, but this doesn't appear to be relevant.
211 */
212
213 return (((*(mp **)x)->v[0] + 1) % 3);
214 }
215
216 static void prime_drop(void *x)
217 {
218 MP_DROP(*(mp **)x);
219 }
220
221 static const rho_ops prime_ops = {
222 prime_sqr, prime_mul, prime_eq, prime_split, prime_drop
223 };
224
225 mp *rho_prime(mp *g, mp *a, mp *n, mp *p)
226 {
227 rho_ctx cc;
228 grand *r = 0;
229 mpmont mm;
230 mp *x, *y;
231 mp *aa, *bb;
232 mp *l;
233
234 /* --- Initialization --- */
235
236 mpmont_create(&mm, p);
237 cc.ops = &prime_ops;
238 cc.c = &mm;
239 cc.n = n;
240 cc.g = mpmont_mul(&mm, MP_NEW, g, mm.r2);
241 cc.a = mpmont_mul(&mm, MP_NEW, a, mm.r2);
242 x = MP_COPY(mm.r);
243 y = MP_COPY(x);
244 aa = bb = MP_ZERO;
245
246 /* --- The main loop --- */
247
248 while ((l = rho(&cc, &x, &y, aa, bb)) == 0) {
249 mp_expfactor f[2];
250
251 if (!r)
252 r = fibrand_create(0);
253 aa = mprand_range(MP_NEW, n, r, 0);
254 bb = mprand_range(MP_NEW, n, r, 0);
255 f[0].base = cc.g; f[0].exp = aa;
256 f[1].base = cc.a; f[1].exp = bb;
257 x = mpmont_mexpr(&mm, MP_NEW, f, 2);
258 y = MP_COPY(x);
259 }
260
261 /* --- Throw everything away now --- */
262
263 if (r)
264 r->ops->destroy(r);
265 mp_drop(cc.g);
266 mp_drop(cc.a);
267 mpmont_destroy(&mm);
268 return (l);
269 }
270
271 /*----- Test rig ----------------------------------------------------------*/
272
273 #ifdef TEST_RIG
274
275 #include <stdio.h>
276
277 #include "dh.h"
278
279 int main(void)
280 {
281 dh_param dp;
282 mp *x, *y;
283 grand *r = fibrand_create(0);
284 mpmont mm;
285 mp *l;
286 int ok;
287
288 fputs("rho: ", stdout);
289 fflush(stdout);
290
291 dh_gen(&dp, 32, 256, 0, r, pgen_evspin, 0);
292 x = mprand_range(MP_NEW, dp.q, r, 0);
293 mpmont_create(&mm, dp.p);
294 y = mpmont_exp(&mm, MP_NEW, dp.g, x);
295 mpmont_destroy(&mm);
296 l = rho_prime(dp.g, y, dp.q, dp.p);
297 if (MP_EQ(x, l)) {
298 fputs(". ok\n", stdout);
299 ok = 1;
300 } else {
301 fputs("\n*** rho (discrete logs) failed\n", stdout);
302 ok = 0;
303 }
304
305 mp_drop(l);
306 mp_drop(x);
307 mp_drop(y);
308 r->ops->destroy(r);
309 dh_paramfree(&dp);
310 assert(mparena_count(MPARENA_GLOBAL) == 0);
311
312 return (ok ? 0 : EXIT_FAILURE);
313 }
314
315 #endif
316
317 /*----- That's all, folks -------------------------------------------------*/