3 * Extended GCD calculation
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
32 /*----- Main code ---------------------------------------------------------*/
36 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
37 * @mp *a, *b@ = sources (must be nonzero)
41 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
42 * @ax + by = gcd(a, b)@. This is useful for computing modular
46 void mp_gcd(mp
**gcd
, mp
**xx
, mp
**yy
, mp
*a
, mp
*b
)
48 mp
*x
= MP_ONE
, *X
= MP_ZERO
;
49 mp
*y
= MP_ZERO
, *Y
= MP_ONE
;
51 mp
*q
= MP_NEW
, *t
, *spare
= MP_NEW
;
59 /* --- Sort out some initial flags --- */
69 /* --- Ensure that @a@ is larger than @b@ --- *
71 * Use absolute values here!
74 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
79 /* --- Check for zeroness --- */
83 /* --- Store %$|a|$% as the GCD --- */
86 if (*gcd
) MP_DROP(*gcd
);
96 /* --- Store %$1$% and %$0$% in the appropriate bins --- */
100 mp
**tt
= xx
; xx
= yy
; yy
= tt
;
103 if (*xx
) MP_DROP(*xx
);
104 if (MP_EQ(a
, MP_ZERO
))
112 if (*yy
) MP_DROP(*yy
);
119 /* --- Force the signs on the arguments and take copies --- */
124 MP_SPLIT(a
); a
->f
&= ~MP_NEG
;
125 MP_SPLIT(b
); b
->f
&= ~MP_NEG
;
130 /* --- Main extended Euclidean algorithm --- */
132 while (!MP_ZEROP(v
)) {
133 mp_div(&q
, &u
, u
, v
);
135 t
= mp_mul(spare
, X
, q
);
137 spare
= x
; x
= X
; X
= t
;
138 t
= mp_mul(spare
, Y
, q
);
140 spare
= y
; y
= Y
; Y
= t
;
145 MP_DROP(q
); if (spare
) MP_DROP(spare
);
149 if (*gcd
) MP_DROP(*gcd
);
154 /* --- Perform a little normalization --- *
156 * Ensure that the coefficient returned is positive, if there is only one.
157 * If there are two, favour @y@. Of course, if the original arguments were
158 * negative then I'll need to twiddle their signs as well.
163 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
170 /* --- Sort out the signs --- *
172 * Note that %$ax + by = a(x - b) + b(y + a)$%.
174 * This is currently bodgy. It needs sorting out at some time.
182 } while (MP_NEGP(y
));
184 while (MP_CMP(y
, >=, a
)) {
195 while (MP_CMP(x
, >=, b
))
200 /* --- Twiddle the signs --- */
207 /* --- Store the results --- */
212 if (*xx
) MP_DROP(*xx
);
219 if (*yy
) MP_DROP(*yy
);
225 MP_DROP(X
); MP_DROP(Y
);
226 MP_DROP(a
); MP_DROP(b
);
229 /* -- @mp_modinv@ --- *
231 * Arguments: @mp *d@ = destination
235 * Returns: The inverse %$x^{-1} \bmod p$%.
237 * Use: Computes a modular inverse. An assertion fails if %$p$%
241 mp
*mp_modinv(mp
*d
, mp
*x
, mp
*p
)
244 mp_gcd(&g
, 0, &d
, p
, x
);
245 assert(MP_EQ(g
, MP_ONE
));
250 /*----- Test rig ----------------------------------------------------------*/
254 static int modinv(dstr
*v
)
257 mp
*x
= *(mp
**)v
[0].buf
;
258 mp
*m
= *(mp
**)v
[1].buf
;
259 mp
*r
= *(mp
**)v
[2].buf
;
261 mp
*y
= mp_modinv(MP_NEW
, x
, m
);
263 fputs("\n*** mp_modinv failed", stderr
);
264 fputs("\nx = ", stderr
); mp_writefile(x
, stderr
, 10);
265 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
266 fputs("\nexpect = ", stderr
); mp_writefile(r
, stderr
, 10);
267 fputs("\nresult = ", stderr
); mp_writefile(y
, stderr
, 10);
270 MP_DROP(x
); MP_DROP(m
); MP_DROP(r
); MP_DROP(y
);
271 assert(mparena_count(MPARENA_GLOBAL
) == 0);
275 static int gcd(dstr
*v
)
278 mp
*a
= *(mp
**)v
[0].buf
;
279 mp
*b
= *(mp
**)v
[1].buf
;
280 mp
*g
= *(mp
**)v
[2].buf
;
281 mp
*x
= *(mp
**)v
[3].buf
;
282 mp
*y
= *(mp
**)v
[4].buf
;
284 mp
*gg
= MP_NEW
, *xx
= MP_NEW
, *yy
= MP_NEW
;
285 mp_gcd(&gg
, &xx
, &yy
, a
, b
);
287 fputs("\n*** mp_gcd(x) failed", stderr
);
288 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
289 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
290 fputs("\nexpect = ", stderr
); mp_writefile(x
, stderr
, 10);
291 fputs("\nresult = ", stderr
); mp_writefile(xx
, stderr
, 10);
296 fputs("\n*** mp_gcd(y) failed", stderr
);
297 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
298 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
299 fputs("\nexpect = ", stderr
); mp_writefile(y
, stderr
, 10);
300 fputs("\nresult = ", stderr
); mp_writefile(yy
, stderr
, 10);
306 mp
*ax
= mp_mul(MP_NEW
, a
, xx
);
307 mp
*by
= mp_mul(MP_NEW
, b
, yy
);
308 ax
= mp_add(ax
, ax
, by
);
310 fputs("\n*** (Alternative result found.)\n", stderr
);
316 fputs("\n*** mp_gcd(gcd) failed", stderr
);
317 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 10);
318 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 10);
319 fputs("\nexpect = ", stderr
); mp_writefile(g
, stderr
, 10);
320 fputs("\nresult = ", stderr
); mp_writefile(gg
, stderr
, 10);
324 MP_DROP(a
); MP_DROP(b
); MP_DROP(g
); MP_DROP(x
); MP_DROP(y
);
325 MP_DROP(gg
); MP_DROP(xx
); MP_DROP(yy
);
326 assert(mparena_count(MPARENA_GLOBAL
) == 0);
330 static test_chunk tests
[] = {
331 { "gcd", gcd
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
332 { "modinv", modinv
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
336 int main(int argc
, char *argv
[])
339 test_run(argc
, argv
, tests
, SRCDIR
"/t/mp");
345 /*----- That's all, folks -------------------------------------------------*/