1 /*
2     ecc Version 1.2  by Paul Flaherty (paulf@stanford.edu)
3     Copyright (C) 1993 Free Software Foundation, Inc.
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2, or (at your option)
8     any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 
20 
21 /* gflib.c
22 	Math Library for GF[256]
23 
24 	This file contains a number of mathematical functions for GF[256].
25 	Entry and result are always assumed to be in vector notation, since
26 	said notation allows for the zero element.  Attempting to reciprocate
27 	the zero element results in process exit 42.
28 */
29 
30 #include "gf.h"
31 
32 
33 /* Multiply two field elements */
34 
35 unsigned char
gfmul(mul1,mul2)36 gfmul (mul1, mul2)
37 
38      unsigned char mul1, mul2;
39 {
40   unsigned char mul3;
41   if (mul1 == 0 || mul2 == 0)
42     mul3 = 0;
43   else
44     mul3 = e2v[(v2e[mul1] + v2e[mul2]) % 255];
45   return (mul3);
46 }
47 
48 
49 /* Add two field elements.  Subtraction and addition are equivalent */
50 
51 unsigned char
gfadd(add1,add2)52 gfadd (add1, add2)
53 
54      unsigned char add1, add2;
55 {
56   unsigned char add3;
57   add3 = add1 ^ add2;
58   return (add3);
59 }
60 
61 
62 /* Invert a field element, for division */
63 
64 unsigned char
gfinv(ivt)65 gfinv (ivt)
66 
67      unsigned char ivt;
68 {
69   unsigned char ivtd;
70   if (ivt == 0)
71     exit (42);
72   ivtd = e2v[255 - v2e[ivt]];
73   return (ivtd);
74 }
75 
76 
77 /* Exponentiation.  Convert to exponential notation, mod 255 */
78 
79 unsigned char
gfexp(mant,powr)80 gfexp (mant, powr)
81 
82      unsigned char mant, powr;
83 {
84   unsigned char expt;
85   if (mant == 0)
86     expt = 0;
87   else
88     expt = e2v[(v2e[mant] * powr) % 255];
89   return (expt);
90 }
91