X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/a2a74efe4b1276b1efec12f25fd65f0df060f38a..0f00dc4c8eb47e67bc0f148c2dd109f73a451e0a:/calc/gfx.cal diff --git a/calc/gfx.cal b/calc/gfx.cal index 8d8fd00..f898c45 100644 --- a/calc/gfx.cal +++ b/calc/gfx.cal @@ -1,13 +1,11 @@ /* -*-apcalc-*- * - * $Id: gfx.cal,v 1.1 2000/10/08 16:01:37 mdw Exp $ - * * Testbed for %$\gf{2}$% poltnomial arithmetic * * (c) 2000 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,26 +13,18 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * Catacomb is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with Catacomb; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: gfx.cal,v $ - * Revision 1.1 2000/10/08 16:01:37 mdw - * Prototypes of various bits of code. - * - */ - /*----- Object types ------------------------------------------------------*/ obj gf { x }; @@ -79,7 +69,9 @@ define gf_mul(x, y) define gfx_div(rx, dx) { local r = gfint(rx), d = gfint(dx), i; - local q = 0, dbits = highbit(d), rbits = highbit(r); + local q = 0, dbits, rbits; + dbits = highbit(d); + rbits = highbit(r); for (i = rbits - dbits; i >= 0; i--) { if (bit(r, i + dbits)) { r = xor(r, d << i); @@ -91,14 +83,48 @@ define gfx_div(rx, dx) define gf_div(x, y) { - local l = gfx_div(x, y); + local l; + l = gfx_div(x, y); return gf(l[[0]]); } define gf_mod(x, y) { - local l = gfx_div(x, y); + local l; + l = gfx_div(x, y); return gf(l[[1]]); } +define gf_gcd(a, b) +{ + local swap = 0; + local g, x = 1, X = 0, y = 0, Y = 1, q, r, t; + if (a.x < b.x) { + t = a; a = b; b = t; + swap = 1; + } + if (b == gf(0)) + g = a; + else { + while (b != gf(0)) { + q = gf_div(a, b); r = gf_mod(a, b); + t = X * q + x; x = X; X = t; + t = Y * q + y; y = Y; Y = t; + a = b; b = r; + } + g = a; + } + if (swap) { + t = x; x = y; y = t; + } + return list(g, x, y); +} + +define gf_inv(a, b) +{ + local l = gf_gcd(b, a); + if (l[[0]] != gf(1)) quit "not coprime in gf_inv"; + return l[[2]]; +} + /*----- That's all, folks -------------------------------------------------*/