3 * $Id: mp-gcd.c,v 1.2 1999/11/22 20:49:56 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.2 1999/11/22 20:49:56 mdw
34 * Fix bug which failed to favour `x' when `y' wasn't wanted and the two
35 * arguments needed swapping.
37 * Revision 1.1 1999/11/17 18:02:16 mdw
38 * New multiprecision integer arithmetic suite.
42 /*----- Header files ------------------------------------------------------*/
46 /*----- Main code ---------------------------------------------------------*/
50 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
51 * @mp *a, *b@ = sources (must be nonzero)
55 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
56 * @ax + by = gcd(a, b)@. This is useful for computing modular
57 * inverses. Neither @a@ nor @b@ may be zero. Note that,
58 * unlike @mp_div@ for example, it is not possible to specify
59 * explicit destinations -- new MPs are always allocated.
62 void mp_gcd(mp
**gcd
, mp
**xx
, mp
**yy
, mp
*a
, mp
*b
)
64 mp
*X
= MP_ONE
, *Y
= MP_ZERO
;
65 mp
*x
= MP_ZERO
, *y
= MP_ONE
;
71 /* --- Ensure that @a@ is larger than @b@ --- */
73 if (MP_CMP(a
, <, b
)) {
74 { mp
*t
= a
; a
= b
; b
= t
; }
78 /* --- Take a reference to the arguments --- */
83 /* --- Make sure @a@ and @b@ are not both even --- */
85 if (((a
->v
[0] | b
->v
[0]) & 1) == 0) {
88 /* --- Break off my copies --- */
95 /* --- Start scanning --- */
98 if (!MP_STEP(&asc
) || !MP_STEP(&bsc
))
99 assert(((void)"zero argument passed to mp_gcd", 0));
100 if (MP_BIT(&asc
) || MP_BIT(&bsc
))
105 /* --- Shift @a@ and @b@ down --- */
107 a
= mp_lsr(a
, a
, shift
);
108 b
= mp_lsr(b
, b
, shift
);
111 /* --- Set up @u@ and @v@ --- */
116 /* --- Start the main loop --- */
120 /* --- While @u@ is even --- */
124 size_t n
= 0, nn
= 0;
127 MP_SCAN(&xsc
, X
); MP_SCAN(&ysc
, Y
);
130 MP_STEP(&xsc
); MP_STEP(&ysc
);
133 if (ext
&& (MP_BIT(&xsc
) | MP_BIT(&ysc
))) {
143 MP_STEP(&xsc
); MP_STEP(&ysc
);
149 u
= mp_lsr(u
, u
, nn
);
157 /* --- While @v@ is even --- */
161 size_t n
= 0, nn
= 0;
164 MP_SCAN(&xsc
, x
); MP_SCAN(&ysc
, y
);
167 MP_STEP(&xsc
); MP_STEP(&ysc
);
170 if (ext
&& (MP_BIT(&xsc
) | MP_BIT(&ysc
))) {
180 MP_STEP(&xsc
); MP_STEP(&ysc
);
186 v
= mp_lsr(v
, v
, nn
);
194 /* --- End-of-loop fiddling --- */
196 if (MP_CMP(u
, >=, v
)) {
210 if (MP_CMP(u
, ==, MP_ZERO
))
214 /* --- Write the results out --- */
217 *gcd
= mp_lsl(v
, v
, shift
);
221 /* --- Perform a little normalization --- *
223 * Ensure that the coefficient returned is positive, if there is only one.
224 * If there are two, favour @y@.
229 mp
*t
= x
; x
= y
; y
= t
;
237 } else if (x
->f
& MP_NEG
)
240 if (xx
) *xx
= x
; else MP_DROP(x
);
241 if (yy
) *yy
= y
; else MP_DROP(y
);
245 MP_DROP(X
); MP_DROP(Y
);
246 MP_DROP(a
); MP_DROP(b
);
249 /*----- Test rig ----------------------------------------------------------*/
253 static int gcd(dstr
*v
)
256 mp
*a
= *(mp
**)v
[0].buf
;
257 mp
*b
= *(mp
**)v
[1].buf
;
258 mp
*g
= *(mp
**)v
[2].buf
;
259 mp
*x
= *(mp
**)v
[3].buf
;
260 mp
*y
= *(mp
**)v
[4].buf
;
263 mp_gcd(&gg
, &xx
, &yy
, a
, b
);
264 if (MP_CMP(x
, !=, xx
)) {
265 fputs("\n*** mp_gcd(x) failed", stderr
);
266 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
267 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
268 fputs("\nexpect = ", stderr
); mp_writefile(x
, stderr
, 10);
269 fputs("\nresult = ", stderr
); mp_writefile(xx
, stderr
, 10);
273 if (MP_CMP(y
, !=, yy
)) {
274 fputs("\n*** mp_gcd(y) failed", stderr
);
275 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
276 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
277 fputs("\nexpect = ", stderr
); mp_writefile(y
, stderr
, 10);
278 fputs("\nresult = ", stderr
); mp_writefile(yy
, stderr
, 10);
284 mp
*ax
= mp_mul(MP_NEW
, a
, xx
);
285 mp
*by
= mp_mul(MP_NEW
, b
, yy
);
286 ax
= mp_add(ax
, ax
, by
);
287 if (MP_CMP(ax
, ==, gg
))
288 fputs("\n*** (Alternative result found.)\n", stderr
);
293 if (MP_CMP(g
, !=, gg
)) {
294 fputs("\n*** mp_gcd(gcd) failed", stderr
);
295 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
296 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
297 fputs("\nexpect = ", stderr
); mp_writefile(g
, stderr
, 10);
298 fputs("\nresult = ", stderr
); mp_writefile(gg
, stderr
, 10);
302 MP_DROP(a
); MP_DROP(b
); MP_DROP(g
); MP_DROP(x
); MP_DROP(y
);
303 MP_DROP(gg
); MP_DROP(xx
); MP_DROP(yy
);
307 static test_chunk tests
[] = {
308 { "gcd", gcd
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
312 int main(int argc
, char *argv
[])
315 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
321 /*----- That's all, folks -------------------------------------------------*/