| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: rho.h,v 1.3 2004/04/08 01:36:15 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 | #ifndef CATACOMB_RHO_H |
| 31 | #define CATACOMB_RHO_H |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | /*----- Header files ------------------------------------------------------*/ |
| 38 | |
| 39 | #ifndef CATACOMB_MP_H |
| 40 | # include "mp.h" |
| 41 | #endif |
| 42 | |
| 43 | /*----- Data structures ---------------------------------------------------*/ |
| 44 | |
| 45 | /* --- The group operations table --- */ |
| 46 | |
| 47 | typedef struct rho_ops { |
| 48 | void (*sqr)(void *x, void *c); |
| 49 | void (*mul)(void *x, void *y, void *c); |
| 50 | int (*eq)(void *x, void *y); |
| 51 | int (*split)(void *x); |
| 52 | void (*drop)(void *x); |
| 53 | } rho_ops; |
| 54 | |
| 55 | /* --- The Pollard's rho context structure --- */ |
| 56 | |
| 57 | typedef struct rho_ctx { |
| 58 | const rho_ops *ops; /* Group operations table */ |
| 59 | void *c; /* Context for group operations */ |
| 60 | void *g, *a; /* Generator and argument for log */ |
| 61 | mp *n; /* Cyclic group order */ |
| 62 | } rho_ctx; |
| 63 | |
| 64 | /*----- Functions provided ------------------------------------------------*/ |
| 65 | |
| 66 | /* --- @rho@ --- * |
| 67 | * |
| 68 | * Arguments: @rho_ctx *cc@ = pointer to the context structure |
| 69 | * @void *x, *y@ = two (equal) base values (try 1) |
| 70 | * @mp *a, *b@ = logs of %$x$% (see below) |
| 71 | * |
| 72 | * Returns: The discrete logarithm %$\log_g a$%, or null if the algorithm |
| 73 | * failed. (This is unlikely, though possible.) |
| 74 | * |
| 75 | * Use: Uses Pollard's rho algorithm to compute discrete logs in the |
| 76 | * group %$G$% generated by %$g$%. |
| 77 | * |
| 78 | * The algorithm works by finding a cycle in a pseudo-random |
| 79 | * walk. The function @ops->split@ should return an element |
| 80 | * from %$\{\,0, 1, 2\,\}$% according to its argument, in order |
| 81 | * to determine the walk. At each step in the walk, we know a |
| 82 | * group element %$x \in G$% together with its representation as |
| 83 | * a product of powers of %$g$% and $%a$% (i.e., we know that |
| 84 | * %$x = g^\alpha a^\beta$% for some %$\alpha$%, %$\beta$%). |
| 85 | * |
| 86 | * Locating a cycle gives us a collision |
| 87 | * |
| 88 | * %$g^{\alpha} a^{\beta} = g^{\alpha'} a^{\beta'}$% |
| 89 | * |
| 90 | * Taking logs of both sides (to base %$g$%) gives us that |
| 91 | * |
| 92 | * %$\log a\equiv\frac{\alpha-\alpha'}{\beta'-\beta}\bmod{n}$% |
| 93 | * |
| 94 | * Good initial values are %$x = y = 1$% (the multiplicative |
| 95 | * identity of %$G$%) and %$\alpha\equiv\beta\equiv0\bmod{n}$%. |
| 96 | * If that doesn't work then start choosing more `interesting' |
| 97 | * values. |
| 98 | * |
| 99 | * Note that the algorithm requires minimal space but |
| 100 | * %$O(\sqrt{n})$% time. Don't do this on large groups, |
| 101 | * particularly if you can find a decent factor base. |
| 102 | * |
| 103 | * Finally, note that this function will free the input values |
| 104 | * when it's finished with them. This probably isn't a great |
| 105 | * problem. |
| 106 | */ |
| 107 | |
| 108 | extern mp *rho(rho_ctx */*cc*/, void */*x*/, void */*y*/, |
| 109 | mp */*a*/, mp */*b*/); |
| 110 | |
| 111 | /* --- @rho_prime@ --- * |
| 112 | * |
| 113 | * Arguments: @mp *g@ = generator for the group |
| 114 | * @mp *a@ = value to find the logarithm of |
| 115 | * @mp *n@ = order of the group |
| 116 | * @mp *p@ = prime size of the underlying prime field |
| 117 | * |
| 118 | * Returns: The discrete logarithm %$\log_g a$%. |
| 119 | * |
| 120 | * Use: Computes discrete logarithms in a subgroup of a prime field. |
| 121 | */ |
| 122 | |
| 123 | extern mp *rho_prime(mp */*g*/, mp */*a*/, mp */*n*/, mp */*p*/); |
| 124 | |
| 125 | /*----- That's all, folks -------------------------------------------------*/ |
| 126 | |
| 127 | #ifdef __cplusplus |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | #endif |