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