-
Notifications
You must be signed in to change notification settings - Fork 0
/
elliptic_curve.hpp
121 lines (95 loc) · 2.2 KB
/
elliptic_curve.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
/**
* Ðåàë³çàö³ÿ êëàñ³â òî÷êè, êîîðäèíàòè ÿêî¿ íàëåæàòü äåÿêîìó ê³ëüöþ ëèøê³â,
* òà åë³ïòè÷íî¿ êðèâî¿ íàä ïîëåì ëèøê³â
* ðîçä³ë 2.4 êóðñîâî¿
*/
#include <cstdint>
#include <string>
#include "modular_arithmetic.hpp"
#ifndef ELLIPTIC_CURVE_H
#define ELLIPTIC_CURVE_H
/**
* Òî÷êà, êîîðäèíàòè ÿêî¿ x òà y íàëåæàòü äåÿêîìó ê³ëüöþ ëèøê³â.
* ßêùî áóëåâà çì³ííà is_infinity = true, òî öå íåñê³í÷åííî â³ääàëåíà òî÷êà
*/
class Point
{
private:
Galois x, y;
bool is_infinity;
public:
Point() {}
Point(Galois x, Galois y);
/**
* Êîíñòðóêòîð äëÿ ñòâîðåííÿ íåñê³í÷åííî â³ääàëåíî¿ òî÷êè.
* is_infinity ìຠäîð³âíþâàòè true. Ïðè false êîðåêòíà ðîáîòà íå ãàðàíòóºòüñÿ.
*/
Point(bool is_infinity);
bool operator== (const Point& other);
bool operator!= (const Point& other);
Point operator- ();
/**
* Ïîâåðòຠtrue ÿêùî òî÷êà º íåñê³í÷åííî â³ääàëåíîþ, false - â ³íøîìó âèïàäêó
*/
bool isinfty();
/**
* Ïîâåðòຠêîîðäèíàòó x
*/
Galois get_x();
/**
* Ïîâåðòຠêîîðäèíàòó y
*/
Galois get_y();
/**
* Ïîâåðòຠñòðîêîâå ïðåäñòàâëåííÿ
* ßêùî òî÷êà ñê³í÷åííà, òî âîíî ìàòèìå âèãëÿä (x, y). ßêùî íåñê³í÷åííî â³ääàëåíà - (infty).
*/
std::string get_str();
};
/**
* Åë³ïòè÷íà êðèâà, ÿêà ìຠâèãëÿä y^2 = x^3 + a*x + b.
* Äëÿ êîðåêòíî¿ ðîáîòè ìåòîä³â double_point, add_points, sub_points ïàðàìåòð q,
* ÿêèé âèçíà÷ຠÿêîìó ê³ëüöþ ëèøê³â ìàþòü íàëåæàòè êîîðäèíàòè òî÷îê êðèâî¿, ìຠáóòè ïðîñòèì
*/
class EllipticCurve
{
private:
Galois a, b;
int_fast64_t q;
public:
EllipticCurve(int_fast64_t a, int_fast64_t b, int_fast64_t q);
EllipticCurve();
/**
* Ïîâåðòຠtrue ÿêùî òî÷êà P íàëåæèòü êðèâ³é, false - â ³íøîìó âèïàäêó
*/
bool is_point_on_curve(Point& P);
/**
* Ïîäâîºííÿ òî÷êè P íà åë³ïòè÷í³é êðèâ³é
*/
Point double_point(Point& P);
/**
* Äîäàâàííÿ òî÷îê P òà Q íà åë³ïòè÷í³é êðèâ³é
*/
Point add_points(Point& P, Point& Q);
/**
* ³äí³ìàííÿ òî÷îê P òà Q íà åë³ïòè÷í³é êðèâ³é
*/
Point sub_points(Point& P, Point& Q);
/**
* Ïîâåðòຠïîðÿäîê òî÷êè r, äå r * P = (infty)
*/
int_fast64_t order(Point& P);
/**
* Ïîâåðòຠçíà÷åííÿ ïàðàìåòðà q
*/
int_fast64_t get_mod();
/**
* Ïîâåðòຠçíà÷åííÿ êîåô³ö³ºíòà a
*/
Galois get_a();
/**
* Ïîâåðòຠçíà÷åííÿ êîåô³ö³ºíòà b
*/
Galois get_b();
};
#endif