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
;
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
)) {
72 { mp
*t
= a
; a
= b
; b
= t
; }
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 /* --- Take a reference to the arguments --- */
114 /* --- Make sure @a@ and @b@ are not both even --- */
116 MP_SPLIT(a
); a
->f
&= ~MP_NEG
;
117 MP_SPLIT(b
); b
->f
&= ~MP_NEG
;
122 while (!MP_ZEROP(v
)) {
124 gf_div(&q
, &u
, u
, v
);
126 t
= gf_mul(MP_NEW
, X
, q
);
128 MP_DROP(x
); x
= X
; X
= t
;
129 t
= gf_mul(MP_NEW
, Y
, q
);
131 MP_DROP(y
); y
= Y
; Y
= t
;
140 if (*gcd
) MP_DROP(*gcd
);
145 /* --- Perform a little normalization --- */
149 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
152 mp
*t
= x
; x
= y
; y
= t
;
156 /* --- Store the results --- */
161 if (*xx
) MP_DROP(*xx
);
168 if (*yy
) MP_DROP(*yy
);
174 MP_DROP(X
); MP_DROP(Y
);
175 MP_DROP(a
); MP_DROP(b
);
178 /* -- @gf_modinv@ --- *
180 * Arguments: @mp *d@ = destination
184 * Returns: The inverse %$x^{-1} \bmod p$%.
186 * Use: Computes a modular inverse, the catch being that the
187 * arguments and results are binary polynomials. An assertion
188 * fails if %$p$% has no inverse.
191 mp
*gf_modinv(mp
*d
, mp
*x
, mp
*p
)
194 gf_gcd(&g
, 0, &d
, p
, x
);
195 assert(MP_EQ(g
, MP_ONE
));
200 /*----- Test rig ----------------------------------------------------------*/
204 static int gcd(dstr
*v
)
207 mp
*a
= *(mp
**)v
[0].buf
;
208 mp
*b
= *(mp
**)v
[1].buf
;
209 mp
*g
= *(mp
**)v
[2].buf
;
210 mp
*x
= *(mp
**)v
[3].buf
;
211 mp
*y
= *(mp
**)v
[4].buf
;
213 mp
*gg
= MP_NEW
, *xx
= MP_NEW
, *yy
= MP_NEW
;
214 gf_gcd(&gg
, &xx
, &yy
, a
, b
);
216 fputs("\n*** gf_gcd(x) failed", stderr
);
217 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 16);
218 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 16);
219 fputs("\nexpect = ", stderr
); mp_writefile(x
, stderr
, 16);
220 fputs("\nresult = ", stderr
); mp_writefile(xx
, stderr
, 16);
225 fputs("\n*** gf_gcd(y) failed", stderr
);
226 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 16);
227 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 16);
228 fputs("\nexpect = ", stderr
); mp_writefile(y
, stderr
, 16);
229 fputs("\nresult = ", stderr
); mp_writefile(yy
, stderr
, 16);
235 mp
*ax
= gf_mul(MP_NEW
, a
, xx
);
236 mp
*by
= gf_mul(MP_NEW
, b
, yy
);
237 ax
= gf_add(ax
, ax
, by
);
239 fputs("\n*** (Alternative result found.)\n", stderr
);
245 fputs("\n*** gf_gcd(gcd) failed", stderr
);
246 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 16);
247 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 16);
248 fputs("\nexpect = ", stderr
); mp_writefile(g
, stderr
, 16);
249 fputs("\nresult = ", stderr
); mp_writefile(gg
, stderr
, 16);
253 MP_DROP(a
); MP_DROP(b
); MP_DROP(g
); MP_DROP(x
); MP_DROP(y
);
254 MP_DROP(gg
); MP_DROP(xx
); MP_DROP(yy
);
255 assert(mparena_count(MPARENA_GLOBAL
) == 0);
259 static test_chunk tests
[] = {
260 { "gcd", gcd
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
264 int main(int argc
, char *argv
[])
267 test_run(argc
, argv
, tests
, SRCDIR
"/tests/gf");
273 /*----- That's all, folks -------------------------------------------------*/