Elliptic curves on binary fields work.
[u/mdw/catacomb] / calc / gfx.cal
CommitLineData
a2a74efe 1/* -*-apcalc-*-
2 *
ceb3f0c0 3 * $Id: gfx.cal,v 1.1.4.1 2004/03/21 22:39:46 mdw Exp $
a2a74efe 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/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: gfx.cal,v $
ceb3f0c0 33 * Revision 1.1.4.1 2004/03/21 22:39:46 mdw
34 * Elliptic curves on binary fields work.
35 *
a2a74efe 36 * Revision 1.1 2000/10/08 16:01:37 mdw
37 * Prototypes of various bits of code.
38 *
39 */
40
41/*----- Object types ------------------------------------------------------*/
42
43obj gf { x };
44
45/*----- Static variables --------------------------------------------------*/
46
47static obj gf example_gf_object;
48
49/*----- Main code ---------------------------------------------------------*/
50
51dummy = config("lib_debug", -1);
52
53define gf(x)
54{
55 local obj gf g;
56 g.x = x;
57 return (g);
58}
59
60define gfint(x)
61{
62 if (istype(x, example_gf_object))
63 return (x.x);
64 else
65 return (x);
66}
67
68define gf_add(x, y) = gf(xor(gfint(x), gfint(y)));
69define gf_sub(x, y) = gf(xor(gfint(x), gfint(y)));
70define gf_neg(x) = x;
71
72define gf_mul(x, y)
73{
74 local a = gfint(x), b = gfint(y), z = 0, i, bits = highbit(a);
75 for (i = 0; i <= bits; i++) {
76 if (bit(a, i))
77 z = xor(z, b << i);
78 }
79 return gf(z);
80}
81
82define gfx_div(rx, dx)
83{
84 local r = gfint(rx), d = gfint(dx), i;
ceb3f0c0 85 local q = 0, dbits, rbits;
86 dbits = highbit(d);
87 rbits = highbit(r);
a2a74efe 88 for (i = rbits - dbits; i >= 0; i--) {
89 if (bit(r, i + dbits)) {
90 r = xor(r, d << i);
91 q |= (1 << i);
92 }
93 }
94 return list(q, r);
95}
96
97define gf_div(x, y)
98{
ceb3f0c0 99 local l;
100 l = gfx_div(x, y);
a2a74efe 101 return gf(l[[0]]);
102}
103
104define gf_mod(x, y)
105{
ceb3f0c0 106 local l;
107 l = gfx_div(x, y);
a2a74efe 108 return gf(l[[1]]);
109}
110
ceb3f0c0 111define gf_inv(a, b)
112{
113 local g, x, y, X, Y, u, v, t, q, r;
114 x = gf(1); X = gf(0);
115 y = gf(0); Y = gf(1);
116
117 if (b == gf(0)) { g = a; } else if (a == gf(0)) { g = b; }
118 else {
119 while (b != gf(0)) {
120 q = gf_div(b, a); r = gf_mod(b, a);
121 t = X * q + x; x = X; X = t;
122 t = Y * q + y; y = Y; Y = t;
123 b = a; a = r;
124 }
125 g = a;
126 }
127 if (g != gf(1)) quit "not coprime in gf_inv";
128 return Y;
129}
130
a2a74efe 131/*----- That's all, folks -------------------------------------------------*/