-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGFChar.cpp
64 lines (51 loc) · 1.75 KB
/
GFChar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// GFChar.cpp
// GFChar
//
// Created by Cory Pruce on 7/10/13.
// Copyright (c) 2013 NYIT REU. All rights reserved.
// Code modified from "Arithmetic Operations in a Power-of-Two Galois Field" by AIM Inc.
#include <iostream>
using namespace std;
#define GF 256 // define the Size & Prime Polynomial of this Galois field
#define PP 285
unsigned char Log[GF], ALog[GF]; // establish global Log and Antilog arrays
// fill the Log[] and ALog[] arrays with appropriate integer values
void FillLogArrays () {
int i;
Log[0] = GF - 1; //initialization
ALog[0] = 1;
for (i=1; i<GF; i++) {
if (ALog[i-1] >= 128) {
ALog[i] = (ALog[i-1] * 2) ^ PP; // (2^i) mod PP, where (2^i) > 255
}
else {
ALog[i] = ALog[i-1] * 2; // (2^i) mod PP
}
Log[ALog[i]] = i; // corresponding exponent
}
}
unsigned char Product (unsigned char A, unsigned char B) {
if ((A == 0) || (B == 0)) return (0);
else return (ALog[(Log[A] + Log[B]) % (GF-1)]);
}
unsigned char Quotient (unsigned char A, unsigned char B) {
if (B == 0) return (1-GF);
else if (A == 0) return (0);
else return (ALog[(Log[A] - Log[B] + (GF-1)) % (GF-1)]);
}
unsigned char inverse(unsigned char y)
{
if (y == 0) return -1;
return Quotient(1, y);
}
unsigned char power (unsigned char a, unsigned char b){
if ((a == 1) || (b == 0)) return (1);
return ALog[Product(b, Log[a])];
}
/*int main(){
FillLogArrays();
//cout << static_cast<int>(Product(Product(8, 8), 8)) << endl;
cout << static_cast<int>(Product(Product(32, 32), 32)) << endl;
cout << static_cast<int>(power(32, 3)) << endl;
}*/