3 * $Id: mp-gcd.c,v 1.1 1999/11/17 18:02:16 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 1999/11/17 18:02:16 mdw
34 * New multiprecision integer arithmetic suite.
38 /*----- Header files ------------------------------------------------------*/
42 /*----- Main code ---------------------------------------------------------*/
46 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
47 * @mp *a, *b@ = sources (must be nonzero)
51 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
52 * @ax + by = gcd(a, b)@. This is useful for computing modular
53 * inverses. Neither @a@ nor @b@ may be zero. Note that,
54 * unlike @mp_div@ for example, it is not possible to specify
55 * explicit destinations -- new MPs are always allocated.
58 void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b)
60 mp *X = MP_ONE, *Y = MP_ZERO;
61 mp *x = MP_ZERO, *y = MP_ONE;
67 /* --- Ensure that @a@ is larger than @b@ --- */
69 if (MP_CMP(a, <, b)) {
70 { mp *t = a; a = b; b = t; }
74 /* --- Take a reference to the arguments --- */
79 /* --- Make sure @a@ and @b@ are not both even --- */
81 if (((a->v[0] | b->v[0]) & 1) == 0) {
84 /* --- Break off my copies --- */
91 /* --- Start scanning --- */
94 if (!MP_STEP(&asc) || !MP_STEP(&bsc))
95 assert(((void)"zero argument passed to mp_gcd", 0));
96 if (MP_BIT(&asc) || MP_BIT(&bsc))
101 /* --- Shift @a@ and @b@ down --- */
103 a = mp_lsr(a, a, shift);
104 b = mp_lsr(b, b, shift);
107 /* --- Set up @u@ and @v@ --- */
112 /* --- Start the main loop --- */
116 /* --- While @u@ is even --- */
120 size_t n = 0, nn = 0;
123 MP_SCAN(&xsc, X); MP_SCAN(&ysc, Y);
126 MP_STEP(&xsc); MP_STEP(&ysc);
129 if (ext && (MP_BIT(&xsc) | MP_BIT(&ysc))) {
139 MP_STEP(&xsc); MP_STEP(&ysc);
145 u = mp_lsr(u, u, nn);
153 /* --- While @v@ is even --- */
157 size_t n = 0, nn = 0;
160 MP_SCAN(&xsc, x); MP_SCAN(&ysc, y);
163 MP_STEP(&xsc); MP_STEP(&ysc);
166 if (ext && (MP_BIT(&xsc) | MP_BIT(&ysc))) {
176 MP_STEP(&xsc); MP_STEP(&ysc);
182 v = mp_lsr(v, v, nn);
190 /* --- End-of-loop fiddling --- */
192 if (MP_CMP(u, >=, v)) {
206 if (MP_CMP(u, ==, MP_ZERO))
210 /* --- Write the results out --- */
213 *gcd = mp_lsl(v, v, shift);
217 /* --- Perform a little normalization --- *
219 * Ensure that the coefficient returned is positive, if there is only one.
220 * If there are two, favour @y@.
225 mp *t = x; x = y; y = t;
232 } else if (x->f & MP_NEG)
235 if (xx) *xx = x; else MP_DROP(x);
236 if (yy) *yy = y; else MP_DROP(y);
240 MP_DROP(X); MP_DROP(Y);
241 MP_DROP(a); MP_DROP(b);
244 /*----- Test rig ----------------------------------------------------------*/
248 static int gcd(dstr *v)
251 mp *a = *(mp **)v[0].buf;
252 mp *b = *(mp **)v[1].buf;
253 mp *g = *(mp **)v[2].buf;
254 mp *x = *(mp **)v[3].buf;
255 mp *y = *(mp **)v[4].buf;
258 mp_gcd(&gg, &xx, &yy, a, b);
259 if (MP_CMP(x, !=, xx)) {
260 fputs("\n*** mp_gcd(x) failed", stderr);
261 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
262 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
263 fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 10);
264 fputs("\nresult = ", stderr); mp_writefile(xx, stderr, 10);
268 if (MP_CMP(y, !=, yy)) {
269 fputs("\n*** mp_gcd(y) failed", stderr);
270 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
271 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
272 fputs("\nexpect = ", stderr); mp_writefile(y, stderr, 10);
273 fputs("\nresult = ", stderr); mp_writefile(yy, stderr, 10);
279 mp *ax = mp_mul(MP_NEW, a, xx);
280 mp *by = mp_mul(MP_NEW, b, yy);
281 ax = mp_add(ax, ax, by);
282 if (MP_CMP(ax, ==, gg))
283 fputs("\n*** (Alternative result found.)\n", stderr);
288 if (MP_CMP(g, !=, gg)) {
289 fputs("\n*** mp_gcd(gcd) failed", stderr);
290 fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
291 fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
292 fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 10);
293 fputs("\nresult = ", stderr); mp_writefile(gg, stderr, 10);
297 MP_DROP(a); MP_DROP(b); MP_DROP(g); MP_DROP(x); MP_DROP(y);
298 MP_DROP(gg); MP_DROP(xx); MP_DROP(yy);
302 static test_chunk tests[] = {
303 { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
307 int main(int argc, char *argv[])
310 test_run(argc, argv, tests, SRCDIR "/tests/mp");
316 /*----- That's all, folks -------------------------------------------------*/