-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgf2.cpp
146 lines (130 loc) · 3.88 KB
/
gf2.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
This file is part of simple Galois field library.
This is a free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
It 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file gf2.h
* @brief Simple Galois field library for GF(2^8)
* @version 1.1
* @author Piotr Wilkon <[email protected]>
* @copyright Copyright 2021 Piotr Wilkon, licensed under GNU GPLv3
**/
#include "gf2.h"
uint8_t GF2::add(uint8_t x, uint8_t y)
{
return x ^ y; //addition is done by XOR in GF(2^8)
}
uint8_t GF2::sub(uint8_t x, uint8_t y)
{
return x ^ y; //subtraction is done by XOR in GF(2^8) - surprisingly, addition is the same as subtraction in GF(2^n)
}
/**
* @brief Fast multiplication in Galois field
* @param x Multiplicand
* @param y Multiplier
* @return Multiplication result
*/
uint8_t GF2::mul(uint8_t x, uint8_t y)
{
if((x == 0) || (y == 0)) //trivial multiplication by 0
return 0;
//fast multiplication using lookup tables
//we know that log(x)+log(y)=log(x*y), and b^log(a)=a, when b is the logarithm base
//so b^(log(x)+log(y))=b^log(x*y)=x*y, where b is the logarithm base
return exp[log[x] + log[y]];
}
/**
* @brief Fast division in Galois field
* @param dividend Dividend
* @param divisor Divisor
* @return Division result. 0 is returned when dividing by 0.
*/
uint8_t GF2::div(uint8_t dividend, uint8_t divisor)
{
if(divisor == 0) return 0; //illegal division by 0, but for now just return 0
if(dividend == 0) return 0; //trivial division of 0
//similarily to multiplication, x/y=b^(log(x)-log(y)), where b is the logarithm base
return exp[(log[dividend] + 255 - log[divisor]) % 255];
}
/**
* @brief Fast power in Galois field
* @param x Base
* @param exponent Exponent
* @return Result
*/
uint8_t GF2::pow(uint8_t x, uint8_t exponent)
{
//since a*log(x)=log(x^a) and b^log(x)=x, b^(a*log(x))=b^(log(x^a))=x^a, where b is the logarithm base
return exp[(exponent * log[x]) % 255];
}
/**
* @brief Fast inverse in Galois field
* @param x Number of which inverse is calculated
* @return 1/x
*/
uint8_t GF2::inv(uint8_t x)
{
return exp[255 - log[x]];
}
/**
* @brief Slow (no lookup table) multiplication algorithm in Galois field
* @param x Multiplicand
* @param y Multiplier
* @return Multiplication result
* Uses Russian Peasant Multiplication algorithm
*/
uint8_t GF2::slowMul(uint8_t x, uint8_t y)
{
uint8_t ret = 0;
uint16_t x_ = (uint16_t)x;
while(y)
{
if(y & 1) //if current multiplier is odd
ret ^= x_; //add multiplicand to the result
y >>= 1; //divide y by 2
x_ <<= 1; //multiply x_ by 2
if(x_ & 256) x_ ^= GF2_POLY; //if there is a carry bit, apply modular reduction
}
return ret;
}
/**
* @brief Check if object is initialized
* @return 0 if initialized
*/
uint8_t isInitialized(void)
{
return 0; //it must be initialized if the constructor was called
}
GF2::GF2()
{
exp = new uint8_t[512];
log = new uint8_t[256];
uint8_t x = 1;
//fill logarithm and exponential f. lookup tables for all possible values
for(uint16_t i = 0; i < 256; i++)
{
exp[i] = x;
log[x] = i;
x = slowMul(x, 2);
}
for(uint16_t i = 256; i < 512; i++)
{
exp[i] = exp[i - 255]; //this is not necessary, but it will make things easier
}
}
GF2::~GF2()
{
if(exp != nullptr)
delete [] exp;
if(log != nullptr)
delete [] log;
}