| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mp-gcd.c,v 1.3 1999/12/10 23:18:39 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.3 1999/12/10 23:18:39 mdw |
| 34 | * Change interface for suggested destinations. |
| 35 | * |
| 36 | * Revision 1.2 1999/11/22 20:49:56 mdw |
| 37 | * Fix bug which failed to favour `x' when `y' wasn't wanted and the two |
| 38 | * arguments needed swapping. |
| 39 | * |
| 40 | * Revision 1.1 1999/11/17 18:02:16 mdw |
| 41 | * New multiprecision integer arithmetic suite. |
| 42 | * |
| 43 | */ |
| 44 | |
| 45 | /*----- Header files ------------------------------------------------------*/ |
| 46 | |
| 47 | #include "mp.h" |
| 48 | |
| 49 | /*----- Main code ---------------------------------------------------------*/ |
| 50 | |
| 51 | /* --- @mp_gcd@ --- * |
| 52 | * |
| 53 | * Arguments: @mp **gcd, **xx, **yy@ = where to write the results |
| 54 | * @mp *a, *b@ = sources (must be nonzero) |
| 55 | * |
| 56 | * Returns: --- |
| 57 | * |
| 58 | * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that |
| 59 | * @ax + by = gcd(a, b)@. This is useful for computing modular |
| 60 | * inverses. |
| 61 | */ |
| 62 | |
| 63 | void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b) |
| 64 | { |
| 65 | mp *X = MP_ONE, *Y = MP_ZERO; |
| 66 | mp *x = MP_ZERO, *y = MP_ONE; |
| 67 | mp *u, *v; |
| 68 | size_t shift = 0; |
| 69 | unsigned f = 0; |
| 70 | |
| 71 | enum { |
| 72 | f_swap = 1u, |
| 73 | f_aneg = 2u, |
| 74 | f_bneg = 4u, |
| 75 | f_ext = 8u |
| 76 | }; |
| 77 | |
| 78 | /* --- Sort out some initial flags --- */ |
| 79 | |
| 80 | if (xx || yy) |
| 81 | f |= f_ext; |
| 82 | |
| 83 | if (a->f & MP_NEG) |
| 84 | f |= f_aneg; |
| 85 | if (b->f & MP_NEG) |
| 86 | f |= f_bneg; |
| 87 | |
| 88 | /* --- Ensure that @a@ is larger than @b@ --- * |
| 89 | * |
| 90 | * Use absolute values here! |
| 91 | */ |
| 92 | |
| 93 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { |
| 94 | { mp *t = a; a = b; b = t; } |
| 95 | f |= f_swap; |
| 96 | } |
| 97 | |
| 98 | /* --- Check for zeroness --- */ |
| 99 | |
| 100 | if (MP_CMP(b, ==, MP_ZERO)) { |
| 101 | |
| 102 | /* --- Store %$|a|$% as the GCD --- */ |
| 103 | |
| 104 | if (gcd) { |
| 105 | if (*gcd) MP_DROP(*gcd); |
| 106 | a = MP_COPY(a); |
| 107 | if (a->f & MP_NEG) { |
| 108 | MP_SPLIT(a); |
| 109 | a->f &= ~MP_NEG; |
| 110 | f |= f_aneg; |
| 111 | } |
| 112 | *gcd = a; |
| 113 | } |
| 114 | |
| 115 | /* --- Store %$1$% and %$0$% in the appropriate bins --- */ |
| 116 | |
| 117 | if (f & f_ext) { |
| 118 | if (f & f_swap) { |
| 119 | mp **t = xx; xx = yy; yy = t; |
| 120 | } |
| 121 | if (xx) { |
| 122 | if (*xx) MP_DROP(*xx); |
| 123 | if (MP_CMP(a, ==, MP_ZERO)) |
| 124 | *xx = MP_ZERO; |
| 125 | else if (f & f_aneg) |
| 126 | *xx = MP_MONE; |
| 127 | else |
| 128 | *xx = MP_ONE; |
| 129 | } |
| 130 | if (yy) { |
| 131 | if (*yy) MP_DROP(*yy); |
| 132 | *yy = MP_ZERO; |
| 133 | } |
| 134 | } |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | /* --- Take a reference to the arguments --- */ |
| 139 | |
| 140 | a = MP_COPY(a); |
| 141 | b = MP_COPY(b); |
| 142 | |
| 143 | /* --- Make sure @a@ and @b@ are not both even --- */ |
| 144 | |
| 145 | MP_SPLIT(a); a->f &= ~MP_NEG; |
| 146 | MP_SPLIT(b); b->f &= ~MP_NEG; |
| 147 | |
| 148 | if (((a->v[0] | b->v[0]) & 1) == 0) { |
| 149 | mpscan asc, bsc; |
| 150 | |
| 151 | /* --- Break off my copies --- */ |
| 152 | |
| 153 | MP_SCAN(&asc, a); |
| 154 | MP_SCAN(&bsc, b); |
| 155 | |
| 156 | /* --- Start scanning --- */ |
| 157 | |
| 158 | for (;;) { |
| 159 | if (!MP_STEP(&asc) || !MP_STEP(&bsc)) |
| 160 | assert(((void)"zero argument passed to mp_gcd", 0)); |
| 161 | if (MP_BIT(&asc) || MP_BIT(&bsc)) |
| 162 | break; |
| 163 | shift++; |
| 164 | } |
| 165 | |
| 166 | /* --- Shift @a@ and @b@ down --- */ |
| 167 | |
| 168 | a = mp_lsr(a, a, shift); |
| 169 | b = mp_lsr(b, b, shift); |
| 170 | } |
| 171 | |
| 172 | /* --- Set up @u@ and @v@ --- */ |
| 173 | |
| 174 | u = MP_COPY(a); |
| 175 | v = MP_COPY(b); |
| 176 | |
| 177 | /* --- Start the main loop --- */ |
| 178 | |
| 179 | for (;;) { |
| 180 | |
| 181 | /* --- While @u@ is even --- */ |
| 182 | |
| 183 | { |
| 184 | mpscan sc, xsc, ysc; |
| 185 | size_t n = 0, nn = 0; |
| 186 | |
| 187 | MP_SCAN(&sc, u); |
| 188 | MP_SCAN(&xsc, X); MP_SCAN(&ysc, Y); |
| 189 | for (;;) { |
| 190 | MP_STEP(&sc); |
| 191 | MP_STEP(&xsc); MP_STEP(&ysc); |
| 192 | if (MP_BIT(&sc)) |
| 193 | break; |
| 194 | if ((f & f_ext) && (MP_BIT(&xsc) | MP_BIT(&ysc))) { |
| 195 | if (n) { |
| 196 | X = mp_lsr(X, X, n); |
| 197 | Y = mp_lsr(Y, Y, n); |
| 198 | n = 0; |
| 199 | } |
| 200 | X = mp_add(X, X, b); |
| 201 | Y = mp_sub(Y, Y, a); |
| 202 | MP_SCAN(&xsc, X); |
| 203 | MP_SCAN(&ysc, Y); |
| 204 | MP_STEP(&xsc); MP_STEP(&ysc); |
| 205 | } |
| 206 | n++; nn++; |
| 207 | } |
| 208 | |
| 209 | if (nn) { |
| 210 | u = mp_lsr(u, u, nn); |
| 211 | if ((f & f_ext) && n) { |
| 212 | X = mp_lsr(X, X, n); |
| 213 | Y = mp_lsr(Y, Y, n); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* --- While @v@ is even --- */ |
| 219 | |
| 220 | { |
| 221 | mpscan sc, xsc, ysc; |
| 222 | size_t n = 0, nn = 0; |
| 223 | |
| 224 | MP_SCAN(&sc, v); |
| 225 | MP_SCAN(&xsc, x); MP_SCAN(&ysc, y); |
| 226 | for (;;) { |
| 227 | MP_STEP(&sc); |
| 228 | MP_STEP(&xsc); MP_STEP(&ysc); |
| 229 | if (MP_BIT(&sc)) |
| 230 | break; |
| 231 | if ((f & f_ext) && (MP_BIT(&xsc) | MP_BIT(&ysc))) { |
| 232 | if (n) { |
| 233 | x = mp_lsr(x, x, n); |
| 234 | y = mp_lsr(y, y, n); |
| 235 | n = 0; |
| 236 | } |
| 237 | x = mp_add(x, x, b); |
| 238 | y = mp_sub(y, y, a); |
| 239 | MP_SCAN(&xsc, x); |
| 240 | MP_SCAN(&ysc, y); |
| 241 | MP_STEP(&xsc); MP_STEP(&ysc); |
| 242 | } |
| 243 | n++; nn++; |
| 244 | } |
| 245 | |
| 246 | if (nn) { |
| 247 | v = mp_lsr(v, v, nn); |
| 248 | if ((f & f_ext) && n) { |
| 249 | x = mp_lsr(x, x, n); |
| 250 | y = mp_lsr(y, y, n); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* --- End-of-loop fiddling --- */ |
| 256 | |
| 257 | if (MP_CMP(u, >=, v)) { |
| 258 | u = mp_sub(u, u, v); |
| 259 | if (f & f_ext) { |
| 260 | X = mp_sub(X, X, x); |
| 261 | Y = mp_sub(Y, Y, y); |
| 262 | } |
| 263 | } else { |
| 264 | v = mp_sub(v, v, u); |
| 265 | if (f & f_ext) { |
| 266 | x = mp_sub(x, x, X); |
| 267 | y = mp_sub(y, y, Y); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if (MP_CMP(u, ==, MP_ZERO)) |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | /* --- Write the results out --- */ |
| 276 | |
| 277 | if (!gcd) |
| 278 | MP_DROP(v); |
| 279 | else { |
| 280 | if (*gcd) MP_DROP(*gcd); |
| 281 | *gcd = mp_lsl(v, v, shift); |
| 282 | } |
| 283 | |
| 284 | /* --- Perform a little normalization --- * |
| 285 | * |
| 286 | * Ensure that the coefficient returned is positive, if there is only one. |
| 287 | * If there are two, favour @y@. Of course, if the original arguments were |
| 288 | * negative then I'll need to twiddle their signs as well. |
| 289 | */ |
| 290 | |
| 291 | if (f & f_ext) { |
| 292 | |
| 293 | /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */ |
| 294 | |
| 295 | if (f & f_swap) { |
| 296 | mp *t = x; x = y; y = t; |
| 297 | t = a; a = b; b = t; |
| 298 | } |
| 299 | |
| 300 | /* --- Sort out the signs --- * |
| 301 | * |
| 302 | * Note that %$ax + by = a(x - b) + b(y + a)$%. |
| 303 | */ |
| 304 | |
| 305 | if (yy) { |
| 306 | if (y->f & MP_NEG) { |
| 307 | y = mp_add(y, y, a); |
| 308 | x = mp_sub(x, x, b); |
| 309 | } |
| 310 | } else if (x->f & MP_NEG) |
| 311 | x = mp_add(x, x, b); |
| 312 | |
| 313 | /* --- Twiddle the signs --- */ |
| 314 | |
| 315 | if (f & f_aneg) |
| 316 | x->f ^= MP_NEG; |
| 317 | if (f & f_bneg) |
| 318 | y->f ^= MP_NEG; |
| 319 | |
| 320 | /* --- Store the results --- */ |
| 321 | |
| 322 | if (!xx) |
| 323 | MP_DROP(x); |
| 324 | else { |
| 325 | if (*xx) MP_DROP(*xx); |
| 326 | *xx = x; |
| 327 | } |
| 328 | |
| 329 | if (!yy) |
| 330 | MP_DROP(y); |
| 331 | else { |
| 332 | if (*yy) MP_DROP(*yy); |
| 333 | *yy = y; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | MP_DROP(u); |
| 338 | MP_DROP(X); MP_DROP(Y); |
| 339 | MP_DROP(a); MP_DROP(b); |
| 340 | } |
| 341 | |
| 342 | /*----- Test rig ----------------------------------------------------------*/ |
| 343 | |
| 344 | #ifdef TEST_RIG |
| 345 | |
| 346 | static int gcd(dstr *v) |
| 347 | { |
| 348 | int ok = 1; |
| 349 | mp *a = *(mp **)v[0].buf; |
| 350 | mp *b = *(mp **)v[1].buf; |
| 351 | mp *g = *(mp **)v[2].buf; |
| 352 | mp *x = *(mp **)v[3].buf; |
| 353 | mp *y = *(mp **)v[4].buf; |
| 354 | |
| 355 | mp *gg = MP_NEW, *xx = MP_NEW, *yy = MP_NEW; |
| 356 | mp_gcd(&gg, &xx, &yy, a, b); |
| 357 | if (MP_CMP(x, !=, xx)) { |
| 358 | fputs("\n*** mp_gcd(x) failed", stderr); |
| 359 | fputs("\na = ", stderr); mp_writefile(a, stderr, 10); |
| 360 | fputs("\nb = ", stderr); mp_writefile(b, stderr, 10); |
| 361 | fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 10); |
| 362 | fputs("\nresult = ", stderr); mp_writefile(xx, stderr, 10); |
| 363 | fputc('\n', stderr); |
| 364 | ok = 0; |
| 365 | } |
| 366 | if (MP_CMP(y, !=, yy)) { |
| 367 | fputs("\n*** mp_gcd(y) failed", stderr); |
| 368 | fputs("\na = ", stderr); mp_writefile(a, stderr, 10); |
| 369 | fputs("\nb = ", stderr); mp_writefile(b, stderr, 10); |
| 370 | fputs("\nexpect = ", stderr); mp_writefile(y, stderr, 10); |
| 371 | fputs("\nresult = ", stderr); mp_writefile(yy, stderr, 10); |
| 372 | fputc('\n', stderr); |
| 373 | ok = 0; |
| 374 | } |
| 375 | |
| 376 | if (!ok) { |
| 377 | mp *ax = mp_mul(MP_NEW, a, xx); |
| 378 | mp *by = mp_mul(MP_NEW, b, yy); |
| 379 | ax = mp_add(ax, ax, by); |
| 380 | if (MP_CMP(ax, ==, gg)) |
| 381 | fputs("\n*** (Alternative result found.)\n", stderr); |
| 382 | MP_DROP(ax); |
| 383 | MP_DROP(by); |
| 384 | } |
| 385 | |
| 386 | if (MP_CMP(g, !=, gg)) { |
| 387 | fputs("\n*** mp_gcd(gcd) failed", stderr); |
| 388 | fputs("\na = ", stderr); mp_writefile(a, stderr, 10); |
| 389 | fputs("\nb = ", stderr); mp_writefile(b, stderr, 10); |
| 390 | fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 10); |
| 391 | fputs("\nresult = ", stderr); mp_writefile(gg, stderr, 10); |
| 392 | fputc('\n', stderr); |
| 393 | ok = 0; |
| 394 | } |
| 395 | MP_DROP(a); MP_DROP(b); MP_DROP(g); MP_DROP(x); MP_DROP(y); |
| 396 | MP_DROP(gg); MP_DROP(xx); MP_DROP(yy); |
| 397 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 398 | return (ok); |
| 399 | } |
| 400 | |
| 401 | static test_chunk tests[] = { |
| 402 | { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, |
| 403 | { 0, 0, { 0 } } |
| 404 | }; |
| 405 | |
| 406 | int main(int argc, char *argv[]) |
| 407 | { |
| 408 | sub_init(); |
| 409 | test_run(argc, argv, tests, SRCDIR "/tests/mp"); |
| 410 | return (0); |
| 411 | } |
| 412 | |
| 413 | #endif |
| 414 | |
| 415 | /*----- That's all, folks -------------------------------------------------*/ |