5 * Extended GCD calculation
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
34 /*----- Main code ---------------------------------------------------------*/
38 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
39 * @mp *a, *b@ = sources (must be nonzero)
43 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
44 * @ax + by = gcd(a, b)@. This is useful for computing modular
48 void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b)
50 mp *x = MP_ONE, *X = MP_ZERO;
51 mp *y = MP_ZERO, *Y = MP_ONE;
61 /* --- Sort out some initial flags --- */
71 /* --- Ensure that @a@ is larger than @b@ --- *
73 * Use absolute values here!
76 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
77 { mp *t = a; a = b; b = t; }
81 /* --- Check for zeroness --- */
83 if (MP_EQ(b, MP_ZERO)) {
85 /* --- Store %$|a|$% as the GCD --- */
88 if (*gcd) MP_DROP(*gcd);
98 /* --- Store %$1$% and %$0$% in the appropriate bins --- */
102 mp **t = xx; xx = yy; yy = t;
105 if (*xx) MP_DROP(*xx);
106 if (MP_EQ(a, MP_ZERO))
114 if (*yy) MP_DROP(*yy);
121 /* --- Force the signs on the arguments and take copies --- */
126 MP_SPLIT(a); a->f &= ~MP_NEG;
127 MP_SPLIT(b); b->f &= ~MP_NEG;
132 /* --- Main extended Euclidean algorithm --- */
134 while (!MP_ZEROP(v)) {
136 mp_div(&q, &u, u, v);
138 t = mp_mul(MP_NEW, X, q);
140 MP_DROP(x); x = X; X = t;
141 t = mp_mul(MP_NEW, Y, q);
143 MP_DROP(y); y = Y; Y = t;
152 if (*gcd) MP_DROP(*gcd);
157 /* --- Perform a little normalization --- *
159 * Ensure that the coefficient returned is positive, if there is only one.
160 * If there are two, favour @y@. Of course, if the original arguments were
161 * negative then I'll need to twiddle their signs as well.
166 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
169 mp *t = x; x = y; y = t;
173 /* --- Sort out the signs --- *
175 * Note that %$ax + by = a(x - b) + b(y + a)$%.
177 * This is currently bodgy. It needs sorting out at some time.
185 } while (MP_NEGP(y));
187 while (MP_CMP(y, >=, a)) {
198 while (MP_CMP(x, >=, b))
203 /* --- Twiddle the signs --- */
210 /* --- Store the results --- */
215 if (*xx) MP_DROP(*xx);
222 if (*yy) MP_DROP(*yy);
228 MP_DROP(X); MP_DROP(Y);
229 MP_DROP(a); MP_DROP(b);
232 /* -- @mp_modinv@ --- *
234 * Arguments: @mp *d@ = destination
238 * Returns: The inverse %$x^{-1} \bmod p$%.
240 * Use: Computes a modular inverse. An assertion fails if %$p$%
244 mp *mp_modinv(mp *d, mp *x, mp *p)
247 mp_gcd(&g, 0, &d, p, x);
248 assert(MP_EQ(g, MP_ONE));
253 /*----- Test rig ----------------------------------------------------------*/
257 static int modinv(dstr *v)
260 mp *x = *(mp **)v[0].buf;
261 mp *m = *(mp **)v[1].buf;
262 mp *r = *(mp **)v[2].buf;
264 mp *y = mp_modinv(MP_NEW, x, m);
266 fputs("\n*** mp_modinv failed", stderr);
267 fputs("\nx = ", stderr); mp_writefile(x, stderr, 10);
268 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
269 fputs("\nexpect = ", stderr); mp_writefile(r, stderr, 10);
270 fputs("\nresult = ", stderr); mp_writefile(y, stderr, 10);
273 MP_DROP(x); MP_DROP(m); MP_DROP(r); MP_DROP(y);
274 assert(mparena_count(MPARENA_GLOBAL) == 0);
278 static int gcd(dstr *v)
281 mp *a = *(mp **)v[0].buf;
282 mp *b = *(mp **)v[1].buf;
283 mp *g = *(mp **)v[2].buf;
284 mp *x = *(mp **)v[3].buf;
285 mp *y = *(mp **)v[4].buf;
287 mp *gg = MP_NEW, *xx = MP_NEW, *yy = MP_NEW;
288 mp_gcd(&gg, &xx, &yy, a, b);
290 fputs("\n*** mp_gcd(x) failed", stderr);
291 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
292 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
293 fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 10);
294 fputs("\nresult = ", stderr); mp_writefile(xx, stderr, 10);
299 fputs("\n*** mp_gcd(y) failed", stderr);
300 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
301 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
302 fputs("\nexpect = ", stderr); mp_writefile(y, stderr, 10);
303 fputs("\nresult = ", stderr); mp_writefile(yy, stderr, 10);
309 mp *ax = mp_mul(MP_NEW, a, xx);
310 mp *by = mp_mul(MP_NEW, b, yy);
311 ax = mp_add(ax, ax, by);
313 fputs("\n*** (Alternative result found.)\n", stderr);
319 fputs("\n*** mp_gcd(gcd) failed", stderr);
320 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
321 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
322 fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 10);
323 fputs("\nresult = ", stderr); mp_writefile(gg, stderr, 10);
327 MP_DROP(a); MP_DROP(b); MP_DROP(g); MP_DROP(x); MP_DROP(y);
328 MP_DROP(gg); MP_DROP(xx); MP_DROP(yy);
329 assert(mparena_count(MPARENA_GLOBAL) == 0);
333 static test_chunk tests[] = {
334 { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
335 { "modinv", modinv, { &type_mp, &type_mp, &type_mp, 0 } },
339 int main(int argc, char *argv[])
342 test_run(argc, argv, tests, SRCDIR "/tests/mp");
348 /*----- That's all, folks -------------------------------------------------*/