3 * $Id: mp-gcd.c,v 1.5.4.1 2004/03/21 22:39:46 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.4.1 2004/03/21 22:39:46 mdw
34 * Elliptic curves on binary fields work.
36 * Revision 1.5 2000/10/08 12:02:41 mdw
37 * Use Euclid's algorithm rather than the binary one.
39 * Revision 1.4 2000/06/17 11:34:46 mdw
40 * More hacking for the signs of the outputs.
42 * Revision 1.3 1999/12/10 23:18:39 mdw
43 * Change interface for suggested destinations.
45 * Revision 1.2 1999/11/22 20:49:56 mdw
46 * Fix bug which failed to favour `x' when `y' wasn't wanted and the two
47 * arguments needed swapping.
49 * Revision 1.1 1999/11/17 18:02:16 mdw
50 * New multiprecision integer arithmetic suite.
54 /*----- Header files ------------------------------------------------------*/
58 /*----- Main code ---------------------------------------------------------*/
62 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
63 * @mp *a, *b@ = sources (must be nonzero)
67 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
68 * @ax + by = gcd(a, b)@. This is useful for computing modular
72 void mp_gcd(mp
**gcd
, mp
**xx
, mp
**yy
, mp
*a
, mp
*b
)
74 mp
*x
= MP_ONE
, *X
= MP_ZERO
;
75 mp
*y
= MP_ZERO
, *Y
= MP_ONE
;
85 /* --- Sort out some initial flags --- */
95 /* --- Ensure that @a@ is larger than @b@ --- *
97 * Use absolute values here!
100 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
101 { mp
*t
= a
; a
= b
; b
= t
; }
105 /* --- Check for zeroness --- */
107 if (MP_EQ(b
, MP_ZERO
)) {
109 /* --- Store %$|a|$% as the GCD --- */
112 if (*gcd
) MP_DROP(*gcd
);
122 /* --- Store %$1$% and %$0$% in the appropriate bins --- */
126 mp
**t
= xx
; xx
= yy
; yy
= t
;
129 if (*xx
) MP_DROP(*xx
);
130 if (MP_EQ(a
, MP_ZERO
))
138 if (*yy
) MP_DROP(*yy
);
145 /* --- Take a reference to the arguments --- */
150 /* --- Make sure @a@ and @b@ are not both even --- */
152 MP_SPLIT(a
); a
->f
&= ~MP_NEG
;
153 MP_SPLIT(b
); b
->f
&= ~MP_NEG
;
160 mp_div(&q
, &u
, u
, v
);
162 t
= mp_mul(MP_NEW
, X
, q
);
164 MP_DROP(x
); x
= X
; X
= t
;
165 t
= mp_mul(MP_NEW
, Y
, q
);
167 MP_DROP(y
); y
= Y
; Y
= t
;
176 if (*gcd
) MP_DROP(*gcd
);
181 /* --- Perform a little normalization --- *
183 * Ensure that the coefficient returned is positive, if there is only one.
184 * If there are two, favour @y@. Of course, if the original arguments were
185 * negative then I'll need to twiddle their signs as well.
190 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
193 mp
*t
= x
; x
= y
; y
= t
;
197 /* --- Sort out the signs --- *
199 * Note that %$ax + by = a(x - b) + b(y + a)$%.
201 * This is currently bodgy. It needs sorting out at some time.
209 } while (y
->f
& MP_NEG
);
211 while (MP_CMP(y
, >=, a
)) {
220 while (x
->f
& MP_NEG
);
222 while (MP_CMP(x
, >=, b
))
227 /* --- Twiddle the signs --- */
234 /* --- Store the results --- */
239 if (*xx
) MP_DROP(*xx
);
246 if (*yy
) MP_DROP(*yy
);
252 MP_DROP(X
); MP_DROP(Y
);
253 MP_DROP(a
); MP_DROP(b
);
256 /*----- Test rig ----------------------------------------------------------*/
260 static int gcd(dstr
*v
)
263 mp
*a
= *(mp
**)v
[0].buf
;
264 mp
*b
= *(mp
**)v
[1].buf
;
265 mp
*g
= *(mp
**)v
[2].buf
;
266 mp
*x
= *(mp
**)v
[3].buf
;
267 mp
*y
= *(mp
**)v
[4].buf
;
269 mp
*gg
= MP_NEW
, *xx
= MP_NEW
, *yy
= MP_NEW
;
270 mp_gcd(&gg
, &xx
, &yy
, a
, b
);
272 fputs("\n*** mp_gcd(x) failed", stderr
);
273 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
274 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
275 fputs("\nexpect = ", stderr
); mp_writefile(x
, stderr
, 10);
276 fputs("\nresult = ", stderr
); mp_writefile(xx
, stderr
, 10);
281 fputs("\n*** mp_gcd(y) failed", stderr
);
282 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
283 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
284 fputs("\nexpect = ", stderr
); mp_writefile(y
, stderr
, 10);
285 fputs("\nresult = ", stderr
); mp_writefile(yy
, stderr
, 10);
291 mp
*ax
= mp_mul(MP_NEW
, a
, xx
);
292 mp
*by
= mp_mul(MP_NEW
, b
, yy
);
293 ax
= mp_add(ax
, ax
, by
);
295 fputs("\n*** (Alternative result found.)\n", stderr
);
301 fputs("\n*** mp_gcd(gcd) failed", stderr
);
302 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
303 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
304 fputs("\nexpect = ", stderr
); mp_writefile(g
, stderr
, 10);
305 fputs("\nresult = ", stderr
); mp_writefile(gg
, stderr
, 10);
309 MP_DROP(a
); MP_DROP(b
); MP_DROP(g
); MP_DROP(x
); MP_DROP(y
);
310 MP_DROP(gg
); MP_DROP(xx
); MP_DROP(yy
);
311 assert(mparena_count(MPARENA_GLOBAL
) == 0);
315 static test_chunk tests
[] = {
316 { "gcd", gcd
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
320 int main(int argc
, char *argv
[])
323 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
329 /*----- That's all, folks -------------------------------------------------*/