X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/c3caa2face1cda7002eb58245ad75865bf437455..0f00dc4c8eb47e67bc0f148c2dd109f73a451e0a:/calc/gfx.cal diff --git a/calc/gfx.cal b/calc/gfx.cal index 446061e..f898c45 100644 --- a/calc/gfx.cal +++ b/calc/gfx.cal @@ -1,13 +1,11 @@ /* -*-apcalc-*- * - * $Id: gfx.cal,v 1.2 2004/03/21 22:52:06 mdw Exp $ - * * Testbed for %$\gf{2}$% poltnomial arithmetic * * (c) 2000 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,32 +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.2 2004/03/21 22:52:06 mdw - * Merge and close elliptic curve branch. - * - * Revision 1.1.4.1 2004/03/21 22:39:46 mdw - * Elliptic curves on binary fields work. - * - * Revision 1.1 2000/10/08 16:01:37 mdw - * Prototypes of various bits of code. - * - */ - /*----- Object types ------------------------------------------------------*/ obj gf { x }; @@ -111,24 +95,36 @@ define gf_mod(x, y) return gf(l[[1]]); } -define gf_inv(a, b) +define gf_gcd(a, b) { - local g, x, y, X, Y, u, v, t, q, r; - x = gf(1); X = gf(0); - y = gf(0); Y = gf(1); - - if (b == gf(0)) { g = a; } else if (a == gf(0)) { g = 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(b, a); r = gf_mod(b, a); + 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; - b = a; a = r; + a = b; b = r; } g = a; } - if (g != gf(1)) quit "not coprime in gf_inv"; - return Y; + 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 -------------------------------------------------*/