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