| 1 | /* -*-apcalc-*- |
| 2 | * |
| 3 | * $Id: gfx.cal,v 1.3 2004/04/08 01:36:15 mdw Exp $ |
| 4 | * |
| 5 | * Testbed for %$\gf{2}$% poltnomial arithmetic |
| 6 | * |
| 7 | * (c) 2000 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 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. |
| 18 | * |
| 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. |
| 23 | * |
| 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, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Object types ------------------------------------------------------*/ |
| 31 | |
| 32 | obj gf { x }; |
| 33 | |
| 34 | /*----- Static variables --------------------------------------------------*/ |
| 35 | |
| 36 | static obj gf example_gf_object; |
| 37 | |
| 38 | /*----- Main code ---------------------------------------------------------*/ |
| 39 | |
| 40 | dummy = config("lib_debug", -1); |
| 41 | |
| 42 | define gf(x) |
| 43 | { |
| 44 | local obj gf g; |
| 45 | g.x = x; |
| 46 | return (g); |
| 47 | } |
| 48 | |
| 49 | define gfint(x) |
| 50 | { |
| 51 | if (istype(x, example_gf_object)) |
| 52 | return (x.x); |
| 53 | else |
| 54 | return (x); |
| 55 | } |
| 56 | |
| 57 | define gf_add(x, y) = gf(xor(gfint(x), gfint(y))); |
| 58 | define gf_sub(x, y) = gf(xor(gfint(x), gfint(y))); |
| 59 | define gf_neg(x) = x; |
| 60 | |
| 61 | define gf_mul(x, y) |
| 62 | { |
| 63 | local a = gfint(x), b = gfint(y), z = 0, i, bits = highbit(a); |
| 64 | for (i = 0; i <= bits; i++) { |
| 65 | if (bit(a, i)) |
| 66 | z = xor(z, b << i); |
| 67 | } |
| 68 | return gf(z); |
| 69 | } |
| 70 | |
| 71 | define gfx_div(rx, dx) |
| 72 | { |
| 73 | local r = gfint(rx), d = gfint(dx), i; |
| 74 | local q = 0, dbits, rbits; |
| 75 | dbits = highbit(d); |
| 76 | rbits = highbit(r); |
| 77 | for (i = rbits - dbits; i >= 0; i--) { |
| 78 | if (bit(r, i + dbits)) { |
| 79 | r = xor(r, d << i); |
| 80 | q |= (1 << i); |
| 81 | } |
| 82 | } |
| 83 | return list(q, r); |
| 84 | } |
| 85 | |
| 86 | define gf_div(x, y) |
| 87 | { |
| 88 | local l; |
| 89 | l = gfx_div(x, y); |
| 90 | return gf(l[[0]]); |
| 91 | } |
| 92 | |
| 93 | define gf_mod(x, y) |
| 94 | { |
| 95 | local l; |
| 96 | l = gfx_div(x, y); |
| 97 | return gf(l[[1]]); |
| 98 | } |
| 99 | |
| 100 | define gf_inv(a, b) |
| 101 | { |
| 102 | local g, x, y, X, Y, u, v, t, q, r; |
| 103 | x = gf(1); X = gf(0); |
| 104 | y = gf(0); Y = gf(1); |
| 105 | |
| 106 | if (b == gf(0)) { g = a; } else if (a == gf(0)) { g = b; } |
| 107 | else { |
| 108 | while (b != gf(0)) { |
| 109 | q = gf_div(b, a); r = gf_mod(b, a); |
| 110 | t = X * q + x; x = X; X = t; |
| 111 | t = Y * q + y; y = Y; Y = t; |
| 112 | b = a; a = r; |
| 113 | } |
| 114 | g = a; |
| 115 | } |
| 116 | if (g != gf(1)) quit "not coprime in gf_inv"; |
| 117 | return Y; |
| 118 | } |
| 119 | |
| 120 | /*----- That's all, folks -------------------------------------------------*/ |