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