3 * Pollard's rho algorithm for discrete logs
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
36 /*----- Main code ---------------------------------------------------------*/
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)
44 * Returns: The discrete logarithm %$\log_g a$%, or null if the algorithm
45 * failed. (This is unlikely, though possible.)
47 * Use: Uses Pollard's rho algorithm to compute discrete logs in the
48 * group %$G$% generated by %$g$%.
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$%).
58 * Locating a cycle gives us a collision
60 * %$g^{\alpha} a^{\beta} = g^{\alpha'} a^{\beta'}$%
62 * Taking logs of both sides (to base %$g$%) gives us that
64 * %$\log a\equiv\frac{\alpha-\alpha'}{\beta'-\beta}\bmod{n}$%
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'
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.
75 * Finally, note that this function will free the input values
76 * when it's finished with them. This probably isn't a great
80 static void step(rho_ctx *cc, void *x, mp **a, mp **b)
82 switch (cc->ops->split(x)) {
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);
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);
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);
107 mp *rho(rho_ctx *cc, void *x, void *y, mp *a, mp *b)
109 mp *aa = MP_COPY(a), *bb = MP_COPY(b);
112 /* --- Grind through the random walk until we find a collision --- */
116 step(cc, y, &aa, &bb);
117 step(cc, y, &aa, &bb);
118 } while (!cc->ops->eq(x, y));
122 /* --- Now sort out the mess --- */
124 aa = mp_sub(aa, a, aa);
125 bb = mp_sub(bb, bb, b);
127 mp_gcd(&g, &bb, 0, bb, cc->n);
128 if (!MP_EQ(g, MP_ONE)) {
132 aa = mp_mul(aa, aa, bb);
133 mp_div(0, &aa, aa, cc->n);
145 /* --- @rho_prime@ --- *
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
152 * Returns: The discrete logarithm %$\log_g a$%.
154 * Use: Computes discrete logarithms in a subgroup of a prime field.
157 static void prime_sqr(void *x, void *c)
162 a = mpmont_reduce(c, a, a);
166 static void prime_mul(void *x, void *y, void *c)
170 a = mpmont_mul(c, a, a, y);
174 static int prime_eq(void *x, void *y)
176 return (MP_EQ(*(mp **)x, *(mp **)y));
179 static int prime_split(void *x)
181 /* --- Notes on the splitting function --- *
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).
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.
194 return (((*(mp **)x)->v[0] + 1) % 3);
197 static void prime_drop(void *x)
202 static const rho_ops prime_ops = {
203 prime_sqr, prime_mul, prime_eq, prime_split, prime_drop
206 mp *rho_prime(mp *g, mp *a, mp *n, mp *p)
215 /* --- Initialization --- */
217 mpmont_create(&mm, p);
221 cc.g = mpmont_mul(&mm, MP_NEW, g, mm.r2);
222 cc.a = mpmont_mul(&mm, MP_NEW, a, mm.r2);
227 /* --- The main loop --- */
229 while ((l = rho(&cc, &x, &y, aa, bb)) == 0) {
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);
242 /* --- Throw everything away now --- */
252 /*----- Test rig ----------------------------------------------------------*/
264 grand *r = fibrand_create(0);
269 fputs("rho: ", stdout);
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);
277 l = rho_prime(dp.g, y, dp.q, dp.p);
279 fputs(". ok\n", stdout);
282 fputs("\n*** rho (discrete logs) failed\n", stdout);
291 assert(mparena_count(MPARENA_GLOBAL) == 0);
293 return (ok ? 0 : EXIT_FAILURE);
298 /*----- That's all, folks -------------------------------------------------*/