3 * $Id: mp-gcd.c,v 1.6 2004/03/21 22:52:06 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.6 2004/03/21 22:52:06 mdw
34 * Merge and close elliptic curve branch.
36 * Revision 1.5.4.1 2004/03/21 22:39:46 mdw
37 * Elliptic curves on binary fields work.
39 * Revision 1.5 2000/10/08 12:02:41 mdw
40 * Use Euclid's algorithm rather than the binary one.
42 * Revision 1.4 2000/06/17 11:34:46 mdw
43 * More hacking for the signs of the outputs.
45 * Revision 1.3 1999/12/10 23:18:39 mdw
46 * Change interface for suggested destinations.
48 * Revision 1.2 1999/11/22 20:49:56 mdw
49 * Fix bug which failed to favour `x' when `y' wasn't wanted and the two
50 * arguments needed swapping.
52 * Revision 1.1 1999/11/17 18:02:16 mdw
53 * New multiprecision integer arithmetic suite.
57 /*----- Header files ------------------------------------------------------*/
61 /*----- Main code ---------------------------------------------------------*/
65 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
66 * @mp *a, *b@ = sources (must be nonzero)
70 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
71 * @ax + by = gcd(a, b)@. This is useful for computing modular
75 void mp_gcd(mp
**gcd
, mp
**xx
, mp
**yy
, mp
*a
, mp
*b
)
77 mp
*x
= MP_ONE
, *X
= MP_ZERO
;
78 mp
*y
= MP_ZERO
, *Y
= MP_ONE
;
88 /* --- Sort out some initial flags --- */
98 /* --- Ensure that @a@ is larger than @b@ --- *
100 * Use absolute values here!
103 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
104 { mp
*t
= a
; a
= b
; b
= t
; }
108 /* --- Check for zeroness --- */
110 if (MP_EQ(b
, MP_ZERO
)) {
112 /* --- Store %$|a|$% as the GCD --- */
115 if (*gcd
) MP_DROP(*gcd
);
125 /* --- Store %$1$% and %$0$% in the appropriate bins --- */
129 mp
**t
= xx
; xx
= yy
; yy
= t
;
132 if (*xx
) MP_DROP(*xx
);
133 if (MP_EQ(a
, MP_ZERO
))
141 if (*yy
) MP_DROP(*yy
);
148 /* --- Take a reference to the arguments --- */
153 /* --- Make sure @a@ and @b@ are not both even --- */
155 MP_SPLIT(a
); a
->f
&= ~MP_NEG
;
156 MP_SPLIT(b
); b
->f
&= ~MP_NEG
;
163 mp_div(&q
, &u
, u
, v
);
165 t
= mp_mul(MP_NEW
, X
, q
);
167 MP_DROP(x
); x
= X
; X
= t
;
168 t
= mp_mul(MP_NEW
, Y
, q
);
170 MP_DROP(y
); y
= Y
; Y
= t
;
179 if (*gcd
) MP_DROP(*gcd
);
184 /* --- Perform a little normalization --- *
186 * Ensure that the coefficient returned is positive, if there is only one.
187 * If there are two, favour @y@. Of course, if the original arguments were
188 * negative then I'll need to twiddle their signs as well.
193 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
196 mp
*t
= x
; x
= y
; y
= t
;
200 /* --- Sort out the signs --- *
202 * Note that %$ax + by = a(x - b) + b(y + a)$%.
204 * This is currently bodgy. It needs sorting out at some time.
212 } while (y
->f
& MP_NEG
);
214 while (MP_CMP(y
, >=, a
)) {
223 while (x
->f
& MP_NEG
);
225 while (MP_CMP(x
, >=, b
))
230 /* --- Twiddle the signs --- */
237 /* --- Store the results --- */
242 if (*xx
) MP_DROP(*xx
);
249 if (*yy
) MP_DROP(*yy
);
255 MP_DROP(X
); MP_DROP(Y
);
256 MP_DROP(a
); MP_DROP(b
);
259 /*----- Test rig ----------------------------------------------------------*/
263 static int gcd(dstr
*v
)
266 mp
*a
= *(mp
**)v
[0].buf
;
267 mp
*b
= *(mp
**)v
[1].buf
;
268 mp
*g
= *(mp
**)v
[2].buf
;
269 mp
*x
= *(mp
**)v
[3].buf
;
270 mp
*y
= *(mp
**)v
[4].buf
;
272 mp
*gg
= MP_NEW
, *xx
= MP_NEW
, *yy
= MP_NEW
;
273 mp_gcd(&gg
, &xx
, &yy
, a
, b
);
275 fputs("\n*** mp_gcd(x) failed", stderr
);
276 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
277 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
278 fputs("\nexpect = ", stderr
); mp_writefile(x
, stderr
, 10);
279 fputs("\nresult = ", stderr
); mp_writefile(xx
, stderr
, 10);
284 fputs("\n*** mp_gcd(y) failed", stderr
);
285 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
286 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
287 fputs("\nexpect = ", stderr
); mp_writefile(y
, stderr
, 10);
288 fputs("\nresult = ", stderr
); mp_writefile(yy
, stderr
, 10);
294 mp
*ax
= mp_mul(MP_NEW
, a
, xx
);
295 mp
*by
= mp_mul(MP_NEW
, b
, yy
);
296 ax
= mp_add(ax
, ax
, by
);
298 fputs("\n*** (Alternative result found.)\n", stderr
);
304 fputs("\n*** mp_gcd(gcd) failed", stderr
);
305 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
306 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
307 fputs("\nexpect = ", stderr
); mp_writefile(g
, stderr
, 10);
308 fputs("\nresult = ", stderr
); mp_writefile(gg
, stderr
, 10);
312 MP_DROP(a
); MP_DROP(b
); MP_DROP(g
); MP_DROP(x
); MP_DROP(y
);
313 MP_DROP(gg
); MP_DROP(xx
); MP_DROP(yy
);
314 assert(mparena_count(MPARENA_GLOBAL
) == 0);
318 static test_chunk tests
[] = {
319 { "gcd", gcd
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
323 int main(int argc
, char *argv
[])
326 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
332 /*----- That's all, folks -------------------------------------------------*/