| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mp-gcd.c,v 1.5 2000/10/08 12:02:41 mdw Exp $ |
| 4 | * |
| 5 | * Extended GCD calculation |
| 6 | * |
| 7 | * (c) 1999 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: mp-gcd.c,v $ |
| 33 | * Revision 1.5 2000/10/08 12:02:41 mdw |
| 34 | * Use Euclid's algorithm rather than the binary one. |
| 35 | * |
| 36 | * Revision 1.4 2000/06/17 11:34:46 mdw |
| 37 | * More hacking for the signs of the outputs. |
| 38 | * |
| 39 | * Revision 1.3 1999/12/10 23:18:39 mdw |
| 40 | * Change interface for suggested destinations. |
| 41 | * |
| 42 | * Revision 1.2 1999/11/22 20:49:56 mdw |
| 43 | * Fix bug which failed to favour `x' when `y' wasn't wanted and the two |
| 44 | * arguments needed swapping. |
| 45 | * |
| 46 | * Revision 1.1 1999/11/17 18:02:16 mdw |
| 47 | * New multiprecision integer arithmetic suite. |
| 48 | * |
| 49 | */ |
| 50 | |
| 51 | /*----- Header files ------------------------------------------------------*/ |
| 52 | |
| 53 | #include "mp.h" |
| 54 | |
| 55 | /*----- Main code ---------------------------------------------------------*/ |
| 56 | |
| 57 | /* --- @mp_gcd@ --- * |
| 58 | * |
| 59 | * Arguments: @mp **gcd, **xx, **yy@ = where to write the results |
| 60 | * @mp *a, *b@ = sources (must be nonzero) |
| 61 | * |
| 62 | * Returns: --- |
| 63 | * |
| 64 | * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that |
| 65 | * @ax + by = gcd(a, b)@. This is useful for computing modular |
| 66 | * inverses. |
| 67 | */ |
| 68 | |
| 69 | void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b) |
| 70 | { |
| 71 | mp *x = MP_ONE, *X = MP_ZERO; |
| 72 | mp *y = MP_ZERO, *Y = MP_ONE; |
| 73 | mp *u, *v; |
| 74 | mp *q = MP_NEW; |
| 75 | unsigned f = 0; |
| 76 | |
| 77 | enum { |
| 78 | f_swap = 1u, |
| 79 | f_aneg = 2u, |
| 80 | f_bneg = 4u, |
| 81 | f_ext = 8u |
| 82 | }; |
| 83 | |
| 84 | /* --- Sort out some initial flags --- */ |
| 85 | |
| 86 | if (xx || yy) |
| 87 | f |= f_ext; |
| 88 | |
| 89 | if (a->f & MP_NEG) |
| 90 | f |= f_aneg; |
| 91 | if (b->f & MP_NEG) |
| 92 | f |= f_bneg; |
| 93 | |
| 94 | /* --- Ensure that @a@ is larger than @b@ --- * |
| 95 | * |
| 96 | * Use absolute values here! |
| 97 | */ |
| 98 | |
| 99 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { |
| 100 | { mp *t = a; a = b; b = t; } |
| 101 | f |= f_swap; |
| 102 | } |
| 103 | |
| 104 | /* --- Check for zeroness --- */ |
| 105 | |
| 106 | if (MP_EQ(b, MP_ZERO)) { |
| 107 | |
| 108 | /* --- Store %$|a|$% as the GCD --- */ |
| 109 | |
| 110 | if (gcd) { |
| 111 | if (*gcd) MP_DROP(*gcd); |
| 112 | a = MP_COPY(a); |
| 113 | if (a->f & MP_NEG) { |
| 114 | MP_SPLIT(a); |
| 115 | a->f &= ~MP_NEG; |
| 116 | f |= f_aneg; |
| 117 | } |
| 118 | *gcd = a; |
| 119 | } |
| 120 | |
| 121 | /* --- Store %$1$% and %$0$% in the appropriate bins --- */ |
| 122 | |
| 123 | if (f & f_ext) { |
| 124 | if (f & f_swap) { |
| 125 | mp **t = xx; xx = yy; yy = t; |
| 126 | } |
| 127 | if (xx) { |
| 128 | if (*xx) MP_DROP(*xx); |
| 129 | if (MP_EQ(a, MP_ZERO)) |
| 130 | *xx = MP_ZERO; |
| 131 | else if (f & f_aneg) |
| 132 | *xx = MP_MONE; |
| 133 | else |
| 134 | *xx = MP_ONE; |
| 135 | } |
| 136 | if (yy) { |
| 137 | if (*yy) MP_DROP(*yy); |
| 138 | *yy = MP_ZERO; |
| 139 | } |
| 140 | } |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | /* --- Take a reference to the arguments --- */ |
| 145 | |
| 146 | a = MP_COPY(a); |
| 147 | b = MP_COPY(b); |
| 148 | |
| 149 | /* --- Make sure @a@ and @b@ are not both even --- */ |
| 150 | |
| 151 | MP_SPLIT(a); a->f &= ~MP_NEG; |
| 152 | MP_SPLIT(b); b->f &= ~MP_NEG; |
| 153 | |
| 154 | u = MP_COPY(a); |
| 155 | v = MP_COPY(b); |
| 156 | |
| 157 | while (MP_LEN(v)) { |
| 158 | mp *t; |
| 159 | mp_div(&q, &u, u, v); |
| 160 | if (f & f_ext) { |
| 161 | t = mp_mul(MP_NEW, X, q); |
| 162 | t = mp_sub(t, x, t); |
| 163 | MP_DROP(x); x = X; X = t; |
| 164 | t = mp_mul(MP_NEW, Y, q); |
| 165 | t = mp_sub(t, y, t); |
| 166 | MP_DROP(y); y = Y; Y = t; |
| 167 | } |
| 168 | t = u; u = v; v = t; |
| 169 | } |
| 170 | |
| 171 | MP_DROP(q); |
| 172 | if (!gcd) |
| 173 | MP_DROP(u); |
| 174 | else { |
| 175 | if (*gcd) MP_DROP(*gcd); |
| 176 | u->f &= ~MP_NEG; |
| 177 | *gcd = u; |
| 178 | } |
| 179 | |
| 180 | /* --- Perform a little normalization --- * |
| 181 | * |
| 182 | * Ensure that the coefficient returned is positive, if there is only one. |
| 183 | * If there are two, favour @y@. Of course, if the original arguments were |
| 184 | * negative then I'll need to twiddle their signs as well. |
| 185 | */ |
| 186 | |
| 187 | if (f & f_ext) { |
| 188 | |
| 189 | /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */ |
| 190 | |
| 191 | if (f & f_swap) { |
| 192 | mp *t = x; x = y; y = t; |
| 193 | t = a; a = b; b = t; |
| 194 | } |
| 195 | |
| 196 | /* --- Sort out the signs --- * |
| 197 | * |
| 198 | * Note that %$ax + by = a(x - b) + b(y + a)$%. |
| 199 | * |
| 200 | * This is currently bodgy. It needs sorting out at some time. |
| 201 | */ |
| 202 | |
| 203 | if (yy) { |
| 204 | if (y->f & MP_NEG) { |
| 205 | do { |
| 206 | y = mp_add(y, y, a); |
| 207 | x = mp_sub(x, x, b); |
| 208 | } while (y->f & MP_NEG); |
| 209 | } else { |
| 210 | while (MP_CMP(y, >=, a)) { |
| 211 | y = mp_sub(y, y, a); |
| 212 | x = mp_add(x, x, b); |
| 213 | } |
| 214 | } |
| 215 | } else { |
| 216 | if (x->f & MP_NEG) { |
| 217 | do |
| 218 | x = mp_add(x, x, b); |
| 219 | while (x->f & MP_NEG); |
| 220 | } else { |
| 221 | while (MP_CMP(x, >=, b)) |
| 222 | x = mp_sub(x, x, b); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* --- Twiddle the signs --- */ |
| 227 | |
| 228 | if (f & f_aneg) |
| 229 | x->f ^= MP_NEG; |
| 230 | if (f & f_bneg) |
| 231 | y->f ^= MP_NEG; |
| 232 | |
| 233 | /* --- Store the results --- */ |
| 234 | |
| 235 | if (!xx) |
| 236 | MP_DROP(x); |
| 237 | else { |
| 238 | if (*xx) MP_DROP(*xx); |
| 239 | *xx = x; |
| 240 | } |
| 241 | |
| 242 | if (!yy) |
| 243 | MP_DROP(y); |
| 244 | else { |
| 245 | if (*yy) MP_DROP(*yy); |
| 246 | *yy = y; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | MP_DROP(v); |
| 251 | MP_DROP(X); MP_DROP(Y); |
| 252 | MP_DROP(a); MP_DROP(b); |
| 253 | } |
| 254 | |
| 255 | /*----- Test rig ----------------------------------------------------------*/ |
| 256 | |
| 257 | #ifdef TEST_RIG |
| 258 | |
| 259 | static int gcd(dstr *v) |
| 260 | { |
| 261 | int ok = 1; |
| 262 | mp *a = *(mp **)v[0].buf; |
| 263 | mp *b = *(mp **)v[1].buf; |
| 264 | mp *g = *(mp **)v[2].buf; |
| 265 | mp *x = *(mp **)v[3].buf; |
| 266 | mp *y = *(mp **)v[4].buf; |
| 267 | |
| 268 | mp *gg = MP_NEW, *xx = MP_NEW, *yy = MP_NEW; |
| 269 | mp_gcd(&gg, &xx, &yy, a, b); |
| 270 | if (!MP_EQ(x, xx)) { |
| 271 | fputs("\n*** mp_gcd(x) failed", stderr); |
| 272 | fputs("\na = ", stderr); mp_writefile(a, stderr, 10); |
| 273 | fputs("\nb = ", stderr); mp_writefile(b, stderr, 10); |
| 274 | fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 10); |
| 275 | fputs("\nresult = ", stderr); mp_writefile(xx, stderr, 10); |
| 276 | fputc('\n', stderr); |
| 277 | ok = 0; |
| 278 | } |
| 279 | if (!MP_EQ(y, yy)) { |
| 280 | fputs("\n*** mp_gcd(y) failed", stderr); |
| 281 | fputs("\na = ", stderr); mp_writefile(a, stderr, 10); |
| 282 | fputs("\nb = ", stderr); mp_writefile(b, stderr, 10); |
| 283 | fputs("\nexpect = ", stderr); mp_writefile(y, stderr, 10); |
| 284 | fputs("\nresult = ", stderr); mp_writefile(yy, stderr, 10); |
| 285 | fputc('\n', stderr); |
| 286 | ok = 0; |
| 287 | } |
| 288 | |
| 289 | if (!ok) { |
| 290 | mp *ax = mp_mul(MP_NEW, a, xx); |
| 291 | mp *by = mp_mul(MP_NEW, b, yy); |
| 292 | ax = mp_add(ax, ax, by); |
| 293 | if (MP_EQ(ax, gg)) |
| 294 | fputs("\n*** (Alternative result found.)\n", stderr); |
| 295 | MP_DROP(ax); |
| 296 | MP_DROP(by); |
| 297 | } |
| 298 | |
| 299 | if (!MP_EQ(g, gg)) { |
| 300 | fputs("\n*** mp_gcd(gcd) failed", stderr); |
| 301 | fputs("\na = ", stderr); mp_writefile(a, stderr, 10); |
| 302 | fputs("\nb = ", stderr); mp_writefile(b, stderr, 10); |
| 303 | fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 10); |
| 304 | fputs("\nresult = ", stderr); mp_writefile(gg, stderr, 10); |
| 305 | fputc('\n', stderr); |
| 306 | ok = 0; |
| 307 | } |
| 308 | MP_DROP(a); MP_DROP(b); MP_DROP(g); MP_DROP(x); MP_DROP(y); |
| 309 | MP_DROP(gg); MP_DROP(xx); MP_DROP(yy); |
| 310 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 311 | return (ok); |
| 312 | } |
| 313 | |
| 314 | static test_chunk tests[] = { |
| 315 | { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, |
| 316 | { 0, 0, { 0 } } |
| 317 | }; |
| 318 | |
| 319 | int main(int argc, char *argv[]) |
| 320 | { |
| 321 | sub_init(); |
| 322 | test_run(argc, argv, tests, SRCDIR "/tests/mp"); |
| 323 | return (0); |
| 324 | } |
| 325 | |
| 326 | #endif |
| 327 | |
| 328 | /*----- That's all, folks -------------------------------------------------*/ |