forked from bitcookies/winrar-keygen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EllipticCurveGF2m.hpp
314 lines (261 loc) · 9.64 KB
/
EllipticCurveGF2m.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdexcept>
#include <algorithm>
#include "BigInteger.hpp"
template<typename __FieldType>
class EllipticCurveGF2m {
private:
// y^2 + xy = x^3 + Ax^2 + B
__FieldType _A;
__FieldType _B;
void _VerifyParameters() const {
if (_B.IsZero()) {
throw std::invalid_argument("B cannot be zero.");
}
}
public:
class Point {
friend EllipticCurveGF2m<__FieldType>;
private:
const EllipticCurveGF2m<__FieldType>& _Curve;
__FieldType _X;
__FieldType _Y;
void _VerifyParameters() const {
auto Left = _Y.SquareValue() + _X * _Y;
auto Right = (_X + _Curve._A) * _X.SquareValue() + _Curve._B;
if (Left != Right) {
throw std::invalid_argument("New point is not on the curve specified.");
}
}
public:
Point(const EllipticCurveGF2m<__FieldType>& Curve) noexcept :
_Curve(Curve) {}
Point(const EllipticCurveGF2m<__FieldType>& Curve, const void* pbX, size_t cbX, const void* pbY, size_t cbY) :
_Curve(Curve),
_X(pbX, cbX),
_Y(pbY, cbY)
{
_VerifyParameters();
}
Point(const EllipticCurveGF2m<__FieldType>& Curve, const __FieldType& X, const __FieldType& Y) :
_Curve(Curve), _X(X), _Y(Y)
{
_VerifyParameters();
}
Point operator-() const noexcept {
return Point(_X, _X + _Y);
}
Point& operator=(const Point& Other) {
if (&_Curve == &Other._Curve || _Curve == Other._Curve) {
_X = Other._X;
_Y = Other._Y;
return *this;
} else {
throw std::invalid_argument("Not on the same curve.");
}
}
bool operator==(const Point& Other) const noexcept {
if (&_Curve == &Other._Curve || _Curve == Other._Curve) {
return _X == Other._X && _Y == Other._Y;
} else {
return false;
}
}
bool operator!=(const Point& Other) const noexcept {
if (&_Curve == &Other._Curve || _Curve == Other._Curve) {
return _X != Other._X || _Y != Other._Y;
} else {
return true;
}
}
const __FieldType& GetX() const noexcept {
return _X;
}
const __FieldType& GetY() const noexcept {
return _Y;
}
bool IsAtInfinity() const noexcept {
return _X.IsZero() && _Y.IsZero();
}
Point& Double() noexcept {
if (IsAtInfinity() == false) {
auto m = _Y / _X + _X;
// NewX = m ^ 2 + m + a
__FieldType NewX = m.SquareValue();
NewX += m;
NewX += _Curve._A;
// NewY = X ^ 2 + (m + 1) * NewX
_Y = m.AddOne();
_Y *= NewX;
_Y += _X.Square();
_X = NewX;
}
return *this;
}
Point ValueOfDouble() const noexcept {
Point Result(_Curve);
if (IsAtInfinity() == false) {
// m = X + Y / X
auto m = _Y / _X + _X;
// NewX = m ^ 2 + m + a
Result._X = m.SquareValue();
Result._X += m;
Result._X += _Curve._A;
// NewY = X ^ 2 + (m + 1) * NewX
Result._Y = m.AddOne();
Result._Y *= Result._X;
Result._Y += _X.SquareValue();
}
return Result;
}
Point operator+(const Point& Other) const {
if (&_Curve == &Other._Curve || _Curve == Other._Curve) {
if (IsAtInfinity()) {
return Other;
} else {
if (this == &Other || _X == Other._X) {
return ValueOfDouble();
} else {
Point Result(_Curve);
// m = (Y0 + Y1) / (X0 + X1)
auto m = (_Y + Other._Y) / (_X + Other._X);
// NewX = m ^ 2 + m + X0 + X1 + a
Result._X = m.SquareValue();
Result._X += m;
Result._X += _X;
Result._X += Other._X;
Result._X += _Curve._A;
// NewY = m * (X0 + NewX) + NewX + Y0
Result._Y = _X + Result._X;
Result._Y *= m;
Result._Y += Result._X;
Result._Y += _Y;
return Result;
}
}
} else {
throw std::invalid_argument("Not on the same curve.");
}
}
Point& operator+=(const Point& Other) {
if (&_Curve == &Other._Curve || _Curve == Other._Curve) {
if (IsAtInfinity()) {
_X = Other._X;
_Y = Other._Y;
} else {
if (this == &Other || _X == Other._X) {
Double();
} else {
Point Result(_Curve);
// m = (Y0 + Y1) / (X0 + X1)
auto m = (_Y + Other._Y) / (_X + Other._X);
// NewX = m ^ 2 + m + X0 + X1 + a
__FieldType NewX = m.SquareValue();
NewX += m;
NewX += _X;
NewX += Other._X;
NewX += _Curve._A;
// NewY = m * (X0 + NewX) + NewX + Y0
_X += NewX;
_X *= m;
_X += NewX;
_Y += _X;
_X = NewX;
}
}
return *this;
} else {
throw std::invalid_argument("Not on the same curve.");
}
}
Point operator-(const Point& Other) const {
Point Result = -Other;
Result += *this;
return Result;
}
Point& operator-=(const Point& Other) {
return *this += -Other;
}
Point operator*(const BigInteger N) const noexcept {
Point Result(_Curve);
Point temp(*this);
size_t bit_length = N.BitLength();
for (size_t i = 0; i < bit_length; ++i) {
if (N.TestBit(i) == true)
Result += temp;
temp.Double();
}
return Result;
}
Point operator*=(const BigInteger N) noexcept {
Point Result(_Curve);
size_t bit_length = N.BitLength();
for (size_t i = 0; i < bit_length; ++i) {
if (N.TestBit(i) == true)
Result += *this;
Double();
}
*this = Result;
}
// SEC 1: Elliptic Curve Cryptography
// 2.3.3 Elliptic-Curve-Point-to-Octet-String Conversion
std::vector<uint8_t> Dump() const noexcept {
if (IsAtInfinity()) {
std::vector<uint8_t> bytes = { 0x00 };
return bytes;
} else {
std::vector<uint8_t> bytes = { 0x04 };
std::vector<uint8_t> xbytes = _X.Dump();
std::vector<uint8_t> ybytes = _Y.Dump();
std::reverse(xbytes.begin(), xbytes.end()); // to big endian
std::reverse(ybytes.begin(), ybytes.end()); // to big endian
bytes.insert(bytes.end(), xbytes.begin(), xbytes.end());
bytes.insert(bytes.end(), ybytes.begin(), ybytes.end());
return bytes;
}
}
// SEC 1: Elliptic Curve Cryptography
// 2.3.3 Elliptic-Curve-Point-to-Octet-String Conversion
std::vector<uint8_t> DumpCompressed() const noexcept {
if (IsAtInfinity()) {
std::vector<uint8_t> bytes = { 0x00 };
return bytes;
} else {
std::vector<uint8_t> bytes(1);
std::vector<uint8_t> xbytes = _X.Dump();
std::vector<uint8_t> zbytes = (_Y / _X).Dump();
if (zbytes[0] & 1) {
bytes[0] = 0x03;
} else {
bytes[0] = 0x02;
}
std::reverse(xbytes.begin(), xbytes.end()); // to big endian
bytes.insert(bytes.end(), xbytes.begin(), xbytes.end());
return bytes;
}
}
};
EllipticCurveGF2m(const __FieldType& A, const __FieldType& B) : _A(A), _B(B) {
_VerifyParameters();
}
EllipticCurveGF2m(const void* pbA, size_t cbA, const void* pbB, size_t cbB) : _A(pbA, cbA), _B(pbB, cbB) {
_VerifyParameters();
}
bool operator==(const EllipticCurveGF2m<__FieldType>& Other) const noexcept {
return _A == Other._A && _B == Other._B;
}
bool operator!=(const EllipticCurveGF2m<__FieldType>& Other) const noexcept {
return _A != Other._A || _B != Other._B;
}
Point GetInfinityPoint() const noexcept {
return Point(*this);
}
Point GetPoint(const __FieldType& X, const __FieldType& Y) const {
return Point(*this, X, Y);
}
Point GetPoint(const void* pbX, size_t cbX, const void* pbY, size_t cbY) const {
return Point(*this, pbX, cbX, pbY, cbY);
}
};