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