3 * $Id: rho.c,v 1.3 2001/06/16 12:56:38 mdw Exp $
5 * Pollard's rho algorithm for discrete logs
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 2001/06/16 12:56:38 mdw
34 * Fixes for interface change to @mpmont_expr@ and @mpmont_mexpr@.
36 * Revision 1.2 2000/10/08 12:11:22 mdw
37 * Use @MP_EQ@ instead of @MP_CMP@.
39 * Revision 1.1 2000/07/09 21:32:30 mdw
40 * Pollard's rho algorithm for computing discrete logs.
44 /*----- Header files ------------------------------------------------------*/
52 /*----- Main code ---------------------------------------------------------*/
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)
60 * Returns: The discrete logarithm %$\log_g a$%, or null if the algorithm
61 * failed. (This is unlikely, though possible.)
63 * Use: Uses Pollard's rho algorithm to compute discrete logs in the
64 * group %$G$% generated by %$g$%.
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$%).
74 * Locating a cycle gives us a collision
76 * %$g^{\alpha} a^{\beta} = g^{\alpha'} a^{\beta'}$%
78 * Taking logs of both sides (to base %$g$%) gives us that
80 * %$\log a\equiv\frac{\alpha-\alpha'}{\beta'-\beta}\bmod{n}$%
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'
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.
91 * Finally, note that this function will free the input values
92 * when it's finished with them. This probably isn't a great
96 static void step(rho_ctx
*cc
, void *x
, mp
**a
, mp
**b
)
98 switch (cc
->ops
->split(x
)) {
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
);
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
);
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
);
123 mp
*rho(rho_ctx
*cc
, void *x
, void *y
, mp
*a
, mp
*b
)
125 mp
*aa
= MP_COPY(a
), *bb
= MP_COPY(b
);
128 /* --- Grind through the random walk until we find a collision --- */
132 step(cc
, y
, &aa
, &bb
);
133 step(cc
, y
, &aa
, &bb
);
134 } while (!cc
->ops
->eq(x
, y
));
138 /* --- Now sort out the mess --- */
140 aa
= mp_sub(aa
, a
, aa
);
141 bb
= mp_sub(bb
, bb
, b
);
143 mp_gcd(&g
, &bb
, 0, bb
, cc
->n
);
144 if (!MP_EQ(g
, MP_ONE
)) {
148 aa
= mp_mul(aa
, aa
, bb
);
149 mp_div(0, &aa
, aa
, cc
->n
);
161 /* --- @rho_prime@ --- *
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
168 * Returns: The discrete logarithm %$\log_g a$%.
170 * Use: Computes discrete logarithms in a subgroup of a prime field.
173 static void prime_sqr(void *x
, void *c
)
178 a
= mpmont_reduce(c
, a
, a
);
182 static void prime_mul(void *x
, void *y
, void *c
)
186 a
= mpmont_mul(c
, a
, a
, y
);
190 static int prime_eq(void *x
, void *y
)
192 return (MP_EQ(*(mp
**)x
, *(mp
**)y
));
195 static int prime_split(void *x
)
197 /* --- Notes on the splitting function --- *
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).
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.
210 return (((*(mp
**)x
)->v
[0] + 1) % 3);
213 static void prime_drop(void *x
)
218 static rho_ops prime_ops
= {
219 prime_sqr
, prime_mul
, prime_eq
, prime_split
, prime_drop
222 mp
*rho_prime(mp
*g
, mp
*a
, mp
*n
, mp
*p
)
231 /* --- Initialization --- */
233 mpmont_create(&mm
, p
);
237 cc
.g
= mpmont_mul(&mm
, MP_NEW
, g
, mm
.r2
);
238 cc
.a
= mpmont_mul(&mm
, MP_NEW
, a
, mm
.r2
);
243 /* --- The main loop --- */
245 while ((l
= rho(&cc
, &x
, &y
, aa
, bb
)) == 0) {
249 r
= fibrand_create(0);
250 aa
= mprand_range(MP_NEW
, n
, r
, 0);
251 bb
= mprand_range(MP_NEW
, n
, r
, 0);
252 f
[0].base
= cc
.g
; f
[0].exp
= aa
;
253 f
[1].base
= cc
.a
; f
[1].exp
= bb
;
254 x
= mpmont_mexpr(&mm
, MP_NEW
, f
, 2);
258 /* --- Throw everything away now --- */
268 /*----- Test rig ----------------------------------------------------------*/
280 grand
*r
= fibrand_create(0);
285 fputs("rho: ", stdout
);
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
);
293 l
= rho_prime(dp
.g
, y
, dp
.q
, dp
.p
);
295 fputs(". ok\n", stdout
);
298 fputs("\n*** rho (discrete logs) failed\n", stdout
);
307 assert(mparena_count(MPARENA_GLOBAL
) == 0);
309 return (ok ?
0 : EXIT_FAILURE
);
314 /*----- That's all, folks -------------------------------------------------*/