3 * $Id: mp-gcd.c,v 1.5 2000/10/08 12:02:41 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.5 2000/10/08 12:02:41 mdw
34 * Use Euclid's algorithm rather than the binary one.
36 * Revision 1.4 2000/06/17 11:34:46 mdw
37 * More hacking for the signs of the outputs.
39 * Revision 1.3 1999/12/10 23:18:39 mdw
40 * Change interface for suggested destinations.
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.
46 * Revision 1.1 1999/11/17 18:02:16 mdw
47 * New multiprecision integer arithmetic suite.
51 /*----- Header files ------------------------------------------------------*/
55 /*----- Main code ---------------------------------------------------------*/
59 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
60 * @mp *a, *b@ = sources (must be nonzero)
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
69 void mp_gcd(mp
**gcd
, mp
**xx
, mp
**yy
, mp
*a
, mp
*b
)
71 mp
*x
= MP_ONE
, *X
= MP_ZERO
;
72 mp
*y
= MP_ZERO
, *Y
= MP_ONE
;
84 /* --- Sort out some initial flags --- */
94 /* --- Ensure that @a@ is larger than @b@ --- *
96 * Use absolute values here!
99 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
100 { mp
*t
= a
; a
= b
; b
= t
; }
104 /* --- Check for zeroness --- */
106 if (MP_EQ(b
, MP_ZERO
)) {
108 /* --- Store %$|a|$% as the GCD --- */
111 if (*gcd
) MP_DROP(*gcd
);
121 /* --- Store %$1$% and %$0$% in the appropriate bins --- */
125 mp
**t
= xx
; xx
= yy
; yy
= t
;
128 if (*xx
) MP_DROP(*xx
);
129 if (MP_EQ(a
, MP_ZERO
))
137 if (*yy
) MP_DROP(*yy
);
144 /* --- Take a reference to the arguments --- */
149 /* --- Make sure @a@ and @b@ are not both even --- */
151 MP_SPLIT(a
); a
->f
&= ~MP_NEG
;
152 MP_SPLIT(b
); b
->f
&= ~MP_NEG
;
159 mp_div(&q
, &u
, u
, v
);
161 t
= mp_mul(MP_NEW
, X
, q
);
163 MP_DROP(x
); x
= X
; X
= t
;
164 t
= mp_mul(MP_NEW
, Y
, q
);
166 MP_DROP(y
); y
= Y
; Y
= t
;
175 if (*gcd
) MP_DROP(*gcd
);
180 /* --- Perform a little normalization --- *
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.
189 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
192 mp
*t
= x
; x
= y
; y
= t
;
196 /* --- Sort out the signs --- *
198 * Note that %$ax + by = a(x - b) + b(y + a)$%.
200 * This is currently bodgy. It needs sorting out at some time.
208 } while (y
->f
& MP_NEG
);
210 while (MP_CMP(y
, >=, a
)) {
219 while (x
->f
& MP_NEG
);
221 while (MP_CMP(x
, >=, b
))
226 /* --- Twiddle the signs --- */
233 /* --- Store the results --- */
238 if (*xx
) MP_DROP(*xx
);
245 if (*yy
) MP_DROP(*yy
);
251 MP_DROP(X
); MP_DROP(Y
);
252 MP_DROP(a
); MP_DROP(b
);
255 /*----- Test rig ----------------------------------------------------------*/
259 static int gcd(dstr
*v
)
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
;
268 mp
*gg
= MP_NEW
, *xx
= MP_NEW
, *yy
= MP_NEW
;
269 mp_gcd(&gg
, &xx
, &yy
, a
, b
);
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);
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);
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
);
294 fputs("\n*** (Alternative result found.)\n", stderr
);
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);
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);
314 static test_chunk tests
[] = {
315 { "gcd", gcd
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
319 int main(int argc
, char *argv
[])
322 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
328 /*----- That's all, folks -------------------------------------------------*/