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