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 /* --- Main extended Euclidean algorithm --- */
114 while (!MP_ZEROP(v)) {
116 gf_div(&q, &u, u, v);
118 t = gf_mul(MP_NEW, X, q);
120 MP_DROP(x); x = X; X = t;
121 t = gf_mul(MP_NEW, Y, q);
123 MP_DROP(y); y = Y; Y = t;
132 if (*gcd) MP_DROP(*gcd);
137 /* --- Perform a little normalization --- */
141 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
144 mp *t = x; x = y; y = t;
148 /* --- Store the results --- */
153 if (*xx) MP_DROP(*xx);
160 if (*yy) MP_DROP(*yy);
166 MP_DROP(X); MP_DROP(Y);
169 /* -- @gf_modinv@ --- *
171 * Arguments: @mp *d@ = destination
175 * Returns: The inverse %$x^{-1} \bmod p$%.
177 * Use: Computes a modular inverse, the catch being that the
178 * arguments and results are binary polynomials. An assertion
179 * fails if %$p$% has no inverse.
182 mp *gf_modinv(mp *d, mp *x, mp *p)
185 gf_gcd(&g, 0, &d, p, x);
186 assert(MP_EQ(g, MP_ONE));
191 /*----- Test rig ----------------------------------------------------------*/
195 static int gcd(dstr *v)
198 mp *a = *(mp **)v[0].buf;
199 mp *b = *(mp **)v[1].buf;
200 mp *g = *(mp **)v[2].buf;
201 mp *x = *(mp **)v[3].buf;
202 mp *y = *(mp **)v[4].buf;
204 mp *gg = MP_NEW, *xx = MP_NEW, *yy = MP_NEW;
205 gf_gcd(&gg, &xx, &yy, a, b);
207 fputs("\n*** gf_gcd(x) failed", stderr);
208 fputs("\na = ", stderr); mp_writefile(a, stderr, 16);
209 fputs("\nb = ", stderr); mp_writefile(b, stderr, 16);
210 fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 16);
211 fputs("\nresult = ", stderr); mp_writefile(xx, stderr, 16);
216 fputs("\n*** gf_gcd(y) 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(y, stderr, 16);
220 fputs("\nresult = ", stderr); mp_writefile(yy, stderr, 16);
226 mp *ax = gf_mul(MP_NEW, a, xx);
227 mp *by = gf_mul(MP_NEW, b, yy);
228 ax = gf_add(ax, ax, by);
230 fputs("\n*** (Alternative result found.)\n", stderr);
236 fputs("\n*** gf_gcd(gcd) failed", stderr);
237 fputs("\na = ", stderr); mp_writefile(a, stderr, 16);
238 fputs("\nb = ", stderr); mp_writefile(b, stderr, 16);
239 fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 16);
240 fputs("\nresult = ", stderr); mp_writefile(gg, stderr, 16);
244 MP_DROP(a); MP_DROP(b); MP_DROP(g); MP_DROP(x); MP_DROP(y);
245 MP_DROP(gg); MP_DROP(xx); MP_DROP(yy);
246 assert(mparena_count(MPARENA_GLOBAL) == 0);
250 static test_chunk tests[] = {
251 { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
255 int main(int argc, char *argv[])
258 test_run(argc, argv, tests, SRCDIR "/tests/gf");
264 /*----- That's all, folks -------------------------------------------------*/