3 * Euclidian algorithm on binary polynomials
5 * (c) 2004 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)
42 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
43 * @ax + by = gcd(a, b)@. This is useful for computing modular
47 void gf_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b)
49 mp *x = MP_ONE, *X = MP_ZERO;
50 mp *y = MP_ZERO, *Y = MP_ONE;
52 mp *q = MP_NEW, *t, *spare = MP_NEW;
58 /* --- Sort out some initial flags --- */
63 /* --- Ensure that @a@ is larger than @b@ --- *
65 * If they're the same length we don't care which order they're in, so this
66 * unsigned comparison is fine.
69 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
74 /* --- Check for zeroness --- */
76 if (MP_EQ(b, MP_ZERO)) {
78 /* --- Store %$|a|$% as the GCD --- */
81 if (*gcd) MP_DROP(*gcd);
86 /* --- Store %$1$% and %$0$% in the appropriate bins --- */
90 mp **t = xx; xx = yy; yy = t;
93 if (*xx) MP_DROP(*xx);
94 if (MP_EQ(a, MP_ZERO))
100 if (*yy) MP_DROP(*yy);
107 /* --- Main extended Euclidean algorithm --- */
112 while (!MP_ZEROP(v)) {
113 gf_div(&q, &u, u, v);
115 t = gf_mul(spare, X, q);
117 spare = x; x = X; X = t;
118 t = gf_mul(spare, Y, q);
120 spare = y; y = Y; Y = t;
125 MP_DROP(q); if (spare) MP_DROP(spare);
129 if (*gcd) MP_DROP(*gcd);
134 /* --- Perform a little normalization --- */
138 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
145 /* --- Store the results --- */
150 if (*xx) MP_DROP(*xx);
157 if (*yy) MP_DROP(*yy);
163 MP_DROP(X); MP_DROP(Y);
166 /* -- @gf_modinv@ --- *
168 * Arguments: @mp *d@ = destination
172 * Returns: The inverse %$x^{-1} \bmod p$%.
174 * Use: Computes a modular inverse, the catch being that the
175 * arguments and results are binary polynomials. An assertion
176 * fails if %$p$% has no inverse.
179 mp *gf_modinv(mp *d, mp *x, mp *p)
182 gf_gcd(&g, 0, &d, p, x);
183 assert(MP_EQ(g, MP_ONE));
188 /*----- Test rig ----------------------------------------------------------*/
192 static int gcd(dstr *v)
195 mp *a = *(mp **)v[0].buf;
196 mp *b = *(mp **)v[1].buf;
197 mp *g = *(mp **)v[2].buf;
198 mp *x = *(mp **)v[3].buf;
199 mp *y = *(mp **)v[4].buf;
201 mp *gg = MP_NEW, *xx = MP_NEW, *yy = MP_NEW;
202 gf_gcd(&gg, &xx, &yy, a, b);
204 fputs("\n*** gf_gcd(x) failed", stderr);
205 fputs("\na = ", stderr); mp_writefile(a, stderr, 16);
206 fputs("\nb = ", stderr); mp_writefile(b, stderr, 16);
207 fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 16);
208 fputs("\nresult = ", stderr); mp_writefile(xx, stderr, 16);
213 fputs("\n*** gf_gcd(y) failed", stderr);
214 fputs("\na = ", stderr); mp_writefile(a, stderr, 16);
215 fputs("\nb = ", stderr); mp_writefile(b, stderr, 16);
216 fputs("\nexpect = ", stderr); mp_writefile(y, stderr, 16);
217 fputs("\nresult = ", stderr); mp_writefile(yy, stderr, 16);
223 mp *ax = gf_mul(MP_NEW, a, xx);
224 mp *by = gf_mul(MP_NEW, b, yy);
225 ax = gf_add(ax, ax, by);
227 fputs("\n*** (Alternative result found.)\n", stderr);
233 fputs("\n*** gf_gcd(gcd) failed", stderr);
234 fputs("\na = ", stderr); mp_writefile(a, stderr, 16);
235 fputs("\nb = ", stderr); mp_writefile(b, stderr, 16);
236 fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 16);
237 fputs("\nresult = ", stderr); mp_writefile(gg, stderr, 16);
241 MP_DROP(a); MP_DROP(b); MP_DROP(g); MP_DROP(x); MP_DROP(y);
242 MP_DROP(gg); MP_DROP(xx); MP_DROP(yy);
243 assert(mparena_count(MPARENA_GLOBAL) == 0);
247 static test_chunk tests[] = {
248 { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
252 int main(int argc, char *argv[])
255 test_run(argc, argv, tests, SRCDIR "/t/gf");
261 /*----- That's all, folks -------------------------------------------------*/