3 * $Id: mp-gcd.c,v 1.4 2000/06/17 11:34: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.4 2000/06/17 11:34:46 mdw
34 * More hacking for the signs of the outputs.
36 * Revision 1.3 1999/12/10 23:18:39 mdw
37 * Change interface for suggested destinations.
39 * Revision 1.2 1999/11/22 20:49:56 mdw
40 * Fix bug which failed to favour `x' when `y' wasn't wanted and the two
41 * arguments needed swapping.
43 * Revision 1.1 1999/11/17 18:02:16 mdw
44 * New multiprecision integer arithmetic suite.
48 /*----- Header files ------------------------------------------------------*/
52 /*----- Main code ---------------------------------------------------------*/
56 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
57 * @mp *a, *b@ = sources (must be nonzero)
61 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
62 * @ax + by = gcd(a, b)@. This is useful for computing modular
66 void mp_gcd(mp
**gcd
, mp
**xx
, mp
**yy
, mp
*a
, mp
*b
)
68 mp
*X
= MP_ONE
, *Y
= MP_ZERO
;
69 mp
*x
= MP_ZERO
, *y
= MP_ONE
;
81 /* --- Sort out some initial flags --- */
91 /* --- Ensure that @a@ is larger than @b@ --- *
93 * Use absolute values here!
96 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
97 { mp
*t
= a
; a
= b
; b
= t
; }
101 /* --- Check for zeroness --- */
103 if (MP_CMP(b
, ==, MP_ZERO
)) {
105 /* --- Store %$|a|$% as the GCD --- */
108 if (*gcd
) MP_DROP(*gcd
);
118 /* --- Store %$1$% and %$0$% in the appropriate bins --- */
122 mp
**t
= xx
; xx
= yy
; yy
= t
;
125 if (*xx
) MP_DROP(*xx
);
126 if (MP_CMP(a
, ==, MP_ZERO
))
134 if (*yy
) MP_DROP(*yy
);
141 /* --- Take a reference to the arguments --- */
146 /* --- Make sure @a@ and @b@ are not both even --- */
148 MP_SPLIT(a
); a
->f
&= ~MP_NEG
;
149 MP_SPLIT(b
); b
->f
&= ~MP_NEG
;
151 if (((a
->v
[0] | b
->v
[0]) & 1) == 0) {
154 /* --- Break off my copies --- */
159 /* --- Start scanning --- */
162 if (!MP_STEP(&asc
) || !MP_STEP(&bsc
))
163 assert(((void)"zero argument passed to mp_gcd", 0));
164 if (MP_BIT(&asc
) || MP_BIT(&bsc
))
169 /* --- Shift @a@ and @b@ down --- */
171 a
= mp_lsr(a
, a
, shift
);
172 b
= mp_lsr(b
, b
, shift
);
175 /* --- Set up @u@ and @v@ --- */
180 /* --- Start the main loop --- */
184 /* --- While @u@ is even --- */
188 size_t n
= 0, nn
= 0;
191 MP_SCAN(&xsc
, X
); MP_SCAN(&ysc
, Y
);
194 MP_STEP(&xsc
); MP_STEP(&ysc
);
197 if ((f
& f_ext
) && (MP_BIT(&xsc
) | MP_BIT(&ysc
))) {
207 MP_STEP(&xsc
); MP_STEP(&ysc
);
213 u
= mp_lsr(u
, u
, nn
);
214 if ((f
& f_ext
) && n
) {
221 /* --- While @v@ is even --- */
225 size_t n
= 0, nn
= 0;
228 MP_SCAN(&xsc
, x
); MP_SCAN(&ysc
, y
);
231 MP_STEP(&xsc
); MP_STEP(&ysc
);
234 if ((f
& f_ext
) && (MP_BIT(&xsc
) | MP_BIT(&ysc
))) {
244 MP_STEP(&xsc
); MP_STEP(&ysc
);
250 v
= mp_lsr(v
, v
, nn
);
251 if ((f
& f_ext
) && n
) {
258 /* --- End-of-loop fiddling --- */
260 if (MP_CMP(u
, >=, v
)) {
274 if (MP_CMP(u
, ==, MP_ZERO
))
278 /* --- Write the results out --- */
283 if (*gcd
) MP_DROP(*gcd
);
284 *gcd
= mp_lsl(v
, v
, shift
);
287 /* --- Perform a little normalization --- *
289 * Ensure that the coefficient returned is positive, if there is only one.
290 * If there are two, favour @y@. Of course, if the original arguments were
291 * negative then I'll need to twiddle their signs as well.
296 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
299 mp
*t
= x
; x
= y
; y
= t
;
303 /* --- Sort out the signs --- *
305 * Note that %$ax + by = a(x - b) + b(y + a)$%.
307 * This is currently bodgy. It needs sorting out at some time.
315 } while (y
->f
& MP_NEG
);
317 while (MP_CMP(y
, >=, a
)) {
326 while (x
->f
& MP_NEG
);
328 while (MP_CMP(x
, >=, b
))
333 /* --- Twiddle the signs --- */
340 /* --- Store the results --- */
345 if (*xx
) MP_DROP(*xx
);
352 if (*yy
) MP_DROP(*yy
);
358 MP_DROP(X
); MP_DROP(Y
);
359 MP_DROP(a
); MP_DROP(b
);
362 /*----- Test rig ----------------------------------------------------------*/
366 static int gcd(dstr
*v
)
369 mp
*a
= *(mp
**)v
[0].buf
;
370 mp
*b
= *(mp
**)v
[1].buf
;
371 mp
*g
= *(mp
**)v
[2].buf
;
372 mp
*x
= *(mp
**)v
[3].buf
;
373 mp
*y
= *(mp
**)v
[4].buf
;
375 mp
*gg
= MP_NEW
, *xx
= MP_NEW
, *yy
= MP_NEW
;
376 mp_gcd(&gg
, &xx
, &yy
, a
, b
);
377 if (MP_CMP(x
, !=, xx
)) {
378 fputs("\n*** mp_gcd(x) failed", stderr
);
379 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
380 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
381 fputs("\nexpect = ", stderr
); mp_writefile(x
, stderr
, 10);
382 fputs("\nresult = ", stderr
); mp_writefile(xx
, stderr
, 10);
386 if (MP_CMP(y
, !=, yy
)) {
387 fputs("\n*** mp_gcd(y) failed", stderr
);
388 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
389 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
390 fputs("\nexpect = ", stderr
); mp_writefile(y
, stderr
, 10);
391 fputs("\nresult = ", stderr
); mp_writefile(yy
, stderr
, 10);
397 mp
*ax
= mp_mul(MP_NEW
, a
, xx
);
398 mp
*by
= mp_mul(MP_NEW
, b
, yy
);
399 ax
= mp_add(ax
, ax
, by
);
400 if (MP_CMP(ax
, ==, gg
))
401 fputs("\n*** (Alternative result found.)\n", stderr
);
406 if (MP_CMP(g
, !=, gg
)) {
407 fputs("\n*** mp_gcd(gcd) failed", stderr
);
408 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
409 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
410 fputs("\nexpect = ", stderr
); mp_writefile(g
, stderr
, 10);
411 fputs("\nresult = ", stderr
); mp_writefile(gg
, stderr
, 10);
415 MP_DROP(a
); MP_DROP(b
); MP_DROP(g
); MP_DROP(x
); MP_DROP(y
);
416 MP_DROP(gg
); MP_DROP(xx
); MP_DROP(yy
);
417 assert(mparena_count(MPARENA_GLOBAL
) == 0);
421 static test_chunk tests
[] = {
422 { "gcd", gcd
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
426 int main(int argc
, char *argv
[])
429 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
435 /*----- That's all, folks -------------------------------------------------*/