-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
GaloisField.hpp
226 lines (183 loc) · 7.01 KB
/
GaloisField.hpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <vector>
#include <type_traits>
struct GaloisFieldInitByZero {};
struct GaloisFieldInitByOne {};
struct GaloisFieldInitByElement {};
struct GaloisFieldInitByDump {};
template<typename __FieldTraits>
class GaloisField {
public:
static constexpr size_t BitSizeValue = __FieldTraits::BitSizeValue;
static constexpr size_t DumpSizeValue = __FieldTraits::DumpSizeValue;
private:
static_assert(std::is_pod<typename __FieldTraits::ElementType>::value == true);
typename __FieldTraits::ElementType _Val;
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 26495) // disable uninitialized warning
#endif
struct NoInitialization {};
GaloisField(NoInitialization) noexcept {};
#ifdef _MSC_VER
#pragma warning(pop)
#endif
public:
GaloisField() noexcept {
__FieldTraits::SetZero(_Val);
}
GaloisField(GaloisFieldInitByZero) noexcept {
__FieldTraits::SetZero(_Val);
}
GaloisField(GaloisFieldInitByOne) noexcept {
__FieldTraits::SetOne(_Val);
}
GaloisField(GaloisFieldInitByElement, const typename __FieldTraits::ElementType& Element) {
__FieldTraits::Verify(Element);
_Val = Element;
}
GaloisField(GaloisFieldInitByDump, const void* pbBuffer, size_t cbBuffer) {
__FieldTraits::Load(_Val, pbBuffer, cbBuffer);
}
template<typename __Dummy = std::enable_if<BitSizeValue <= sizeof(uintptr_t) * 8>::type>
GaloisField(GaloisFieldInitByDump, uintptr_t SerializedValue) {
__FieldTraits::Load(_Val, SerializedValue);
}
GaloisField<__FieldTraits>& operator=(const typename __FieldTraits::ElementType& Element) {
__FieldTraits::Verify(Element);
_Val = Element;
return *this;
}
template<typename __Dummy = std::enable_if<BitSizeValue <= sizeof(uintptr_t) * 8>::type>
GaloisField<__FieldTraits>& operator=(uintptr_t SerializedValue) {
__FieldTraits::Load(_Val, SerializedValue);
return *this;
}
bool IsZero() const noexcept {
return __FieldTraits::IsZero(_Val);
}
bool IsOne() const noexcept {
return __FieldTraits::IsOne(_Val);
}
bool operator==(const GaloisField<__FieldTraits>& Other) const noexcept {
return __FieldTraits::IsEqual(_Val, Other._Val);
}
bool operator!=(const GaloisField<__FieldTraits>& Other) const noexcept {
return __FieldTraits::IsEqual(_Val, Other._Val) == false;
}
GaloisField<__FieldTraits> operator+(const GaloisField<__FieldTraits>& Other) const noexcept {
GaloisField<__FieldTraits> Result(NoInitialization{});
__FieldTraits::Add(Result._Val, _Val, Other._Val);
return Result;
}
GaloisField<__FieldTraits>& operator+=(const GaloisField<__FieldTraits>& Other) noexcept {
__FieldTraits::AddAssign(_Val, Other._Val);
return *this;
}
GaloisField<__FieldTraits> operator-(const GaloisField<__FieldTraits>& Other) const noexcept {
GaloisField<__FieldTraits> Result(NoInitialization{});
__FieldTraits::Substract(Result._Val, _Val, Other._Val);
return Result;
}
GaloisField<__FieldTraits>& operator-=(const GaloisField<__FieldTraits>& Other) noexcept {
__FieldTraits::SubstractAssign(_Val, Other._Val);
return *this;
}
GaloisField<__FieldTraits> operator*(const GaloisField<__FieldTraits>& Other) const noexcept {
GaloisField<__FieldTraits> Result(NoInitialization{});
__FieldTraits::Multiply(Result._Val, _Val, Other._Val);
return Result;
}
GaloisField<__FieldTraits>& operator*=(const GaloisField<__FieldTraits>& Other) noexcept {
__FieldTraits::MultiplyAssign(_Val, Other._Val);
return *this;
}
GaloisField<__FieldTraits> operator/(const GaloisField<__FieldTraits>& Other) const {
GaloisField<__FieldTraits> Result(NoInitialization{});
__FieldTraits::Divide(Result._Val, _Val, Other._Val);
return Result;
}
GaloisField<__FieldTraits>& operator/=(const GaloisField<__FieldTraits>& Other) {
__FieldTraits::DivideAssign(_Val, Other._Val);
return *this;
}
GaloisField<__FieldTraits>& operator++() noexcept { // prefix ++
__FieldTraits::AddOneAssign(_Val);
return *this;
}
GaloisField<__FieldTraits> operator++(int) noexcept { // postfix ++
GaloisField<__FieldTraits> Prev(*this);
__FieldTraits::AddOneAssign(_Val);
return Prev;
}
GaloisField<__FieldTraits>& operator--() noexcept { // prefix --
__FieldTraits::SubstractOneAssign(_Val);
return *this;
}
GaloisField<__FieldTraits> operator--(int) noexcept { // postfix --
GaloisField<__FieldTraits> Prev(*this);
__FieldTraits::SubstractOneAssign(_Val);
return Prev;
}
GaloisField<__FieldTraits>& Inverse() {
__FieldTraits::InverseAssign(_Val);
return *this;
}
GaloisField<__FieldTraits> InverseValue() const {
GaloisField<__FieldTraits> Result(NoInitialization{});
__FieldTraits::Inverse(Result, _Val);
return Result;
}
GaloisField<__FieldTraits>& AddOne() noexcept {
__FieldTraits::AddOneAssign(_Val);
return *this;
}
GaloisField<__FieldTraits> AddOneValue() const noexcept {
GaloisField<__FieldTraits> Result(NoInitialization{});
__FieldTraits::AddOne(Result._Val, _Val);
return Result;
}
GaloisField<__FieldTraits>& SubstractOne() noexcept {
__FieldTraits::SubstractOneAssign(_Val);
return *this;
}
GaloisField<__FieldTraits> SubstractOneValue() const noexcept {
GaloisField<__FieldTraits> Result(NoInitialization{});
__FieldTraits::SubstractOne(Result._Val, _Val);
return Result;
}
GaloisField<__FieldTraits>& Square() noexcept {
__FieldTraits::SquareAssign(_Val);
return *this;
}
GaloisField<__FieldTraits> SquareValue() const noexcept {
GaloisField<__FieldTraits> Result(NoInitialization{});
__FieldTraits::Square(Result._Val, _Val);
return Result;
}
template<typename __Dummy = std::enable_if<BitSizeValue <= sizeof(uintptr_t) * 8>::type>
uintptr_t Dump() const noexcept {
return __FieldTraits::Dump(_Val);
}
size_t Dump(void* lpBuffer, size_t cbBuffer) const {
return __FieldTraits::Dump(_Val, lpBuffer, cbBuffer);
}
std::vector<uint8_t> Dump() const noexcept {
return __FieldTraits::Dump(_Val);
}
template<typename __Dummy = std::enable_if<BitSizeValue <= sizeof(uintptr_t) * 8>::type>
GaloisField<__FieldTraits>& Load(uintptr_t SerializedValue) {
__FieldTraits::Load(_Val, SerializedValue);
return *this;
}
GaloisField<__FieldTraits>& Load(const void* lpBuffer, size_t cbBuffer) {
__FieldTraits::Load(_Val, lpBuffer, cbBuffer);
return *this;
}
GaloisField<__FieldTraits>& Load(const std::vector<uint8_t>& Buffer) {
__FieldTraits::Load(_Val, Buffer);
return *this;
}
};