5 * Euclidian algorithm on binary polynomials
7 * (c) 2004 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 /*----- Header files ------------------------------------------------------*/
34 /*----- Main code ---------------------------------------------------------*/
38 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
39 * @mp *a, *b@ = sources (must be nonzero)
44 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
45 * @ax + by = gcd(a, b)@. This is useful for computing modular
49 void gf_gcd(mp
**gcd
, mp
**xx
, mp
**yy
, mp
*a
, mp
*b
)
51 mp
*x
= MP_ONE
, *X
= MP_ZERO
;
52 mp
*y
= MP_ZERO
, *Y
= MP_ONE
;
54 mp
*q
= MP_NEW
, *t
, *spare
= MP_NEW
;
60 /* --- Sort out some initial flags --- */
65 /* --- Ensure that @a@ is larger than @b@ --- *
67 * If they're the same length we don't care which order they're in, so this
68 * unsigned comparison is fine.
71 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
76 /* --- Check for zeroness --- */
78 if (MP_EQ(b
, MP_ZERO
)) {
80 /* --- Store %$|a|$% as the GCD --- */
83 if (*gcd
) MP_DROP(*gcd
);
88 /* --- Store %$1$% and %$0$% in the appropriate bins --- */
92 mp
**t
= xx
; xx
= yy
; yy
= t
;
95 if (*xx
) MP_DROP(*xx
);
96 if (MP_EQ(a
, MP_ZERO
))
102 if (*yy
) MP_DROP(*yy
);
109 /* --- Main extended Euclidean algorithm --- */
114 while (!MP_ZEROP(v
)) {
115 gf_div(&q
, &u
, u
, v
);
117 t
= gf_mul(spare
, X
, q
);
119 spare
= x
; x
= X
; X
= t
;
120 t
= gf_mul(spare
, Y
, q
);
122 spare
= y
; y
= Y
; Y
= t
;
127 MP_DROP(q
); if (spare
) MP_DROP(spare
);
131 if (*gcd
) MP_DROP(*gcd
);
136 /* --- Perform a little normalization --- */
140 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
147 /* --- Store the results --- */
152 if (*xx
) MP_DROP(*xx
);
159 if (*yy
) MP_DROP(*yy
);
165 MP_DROP(X
); MP_DROP(Y
);
168 /* -- @gf_modinv@ --- *
170 * Arguments: @mp *d@ = destination
174 * Returns: The inverse %$x^{-1} \bmod p$%.
176 * Use: Computes a modular inverse, the catch being that the
177 * arguments and results are binary polynomials. An assertion
178 * fails if %$p$% has no inverse.
181 mp
*gf_modinv(mp
*d
, mp
*x
, mp
*p
)
184 gf_gcd(&g
, 0, &d
, p
, x
);
185 assert(MP_EQ(g
, MP_ONE
));
190 /*----- Test rig ----------------------------------------------------------*/
194 static int gcd(dstr
*v
)
197 mp
*a
= *(mp
**)v
[0].buf
;
198 mp
*b
= *(mp
**)v
[1].buf
;
199 mp
*g
= *(mp
**)v
[2].buf
;
200 mp
*x
= *(mp
**)v
[3].buf
;
201 mp
*y
= *(mp
**)v
[4].buf
;
203 mp
*gg
= MP_NEW
, *xx
= MP_NEW
, *yy
= MP_NEW
;
204 gf_gcd(&gg
, &xx
, &yy
, a
, b
);
206 fputs("\n*** gf_gcd(x) failed", stderr
);
207 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 16);
208 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 16);
209 fputs("\nexpect = ", stderr
); mp_writefile(x
, stderr
, 16);
210 fputs("\nresult = ", stderr
); mp_writefile(xx
, stderr
, 16);
215 fputs("\n*** gf_gcd(y) failed", stderr
);
216 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 16);
217 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 16);
218 fputs("\nexpect = ", stderr
); mp_writefile(y
, stderr
, 16);
219 fputs("\nresult = ", stderr
); mp_writefile(yy
, stderr
, 16);
225 mp
*ax
= gf_mul(MP_NEW
, a
, xx
);
226 mp
*by
= gf_mul(MP_NEW
, b
, yy
);
227 ax
= gf_add(ax
, ax
, by
);
229 fputs("\n*** (Alternative result found.)\n", stderr
);
235 fputs("\n*** gf_gcd(gcd) failed", stderr
);
236 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 16);
237 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 16);
238 fputs("\nexpect = ", stderr
); mp_writefile(g
, stderr
, 16);
239 fputs("\nresult = ", stderr
); mp_writefile(gg
, stderr
, 16);
243 MP_DROP(a
); MP_DROP(b
); MP_DROP(g
); MP_DROP(x
); MP_DROP(y
);
244 MP_DROP(gg
); MP_DROP(xx
); MP_DROP(yy
);
245 assert(mparena_count(MPARENA_GLOBAL
) == 0);
249 static test_chunk tests
[] = {
250 { "gcd", gcd
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
254 int main(int argc
, char *argv
[])
257 test_run(argc
, argv
, tests
, SRCDIR
"/tests/gf");
263 /*----- That's all, folks -------------------------------------------------*/