forked from gadial/ECC
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zp_int.cpp
151 lines (129 loc) · 3.32 KB
/
zp_int.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
147
148
149
150
151
#include "zp_int.h"
using std::cout;
using std::endl;
zp_int& zp_int::operator+=(const zp_int& rhs){
val += rhs.val;
return normalize();
}
zp_int& zp_int::operator-=(const zp_int& rhs){
// cout << "val = " << val << " rhs.val = " << rhs.val << endl;
val -= rhs.val;
return normalize();
}
zp_int& zp_int::operator*=(const zp_int& rhs){
val *= rhs.val;
return normalize();
}
zp_int& zp_int::operator/=(const zp_int& rhs){
if (p == rhs.p)
val *= rhs.inverse().val;
else
val *= zp_int(rhs.val,p).inverse().val;
return normalize();
}
zp_int& zp_int::invert(){
if (val == 0)
throw "Division by 0";
mpz_invert(val.get_mpz_t(), val.get_mpz_t(),p.get_mpz_t());
return normalize();
}
zp_int zp_int::inverse() const{
zp_int temp = *this;
return temp.invert();
}
zp_int& zp_int::normalize(){
if (p == 0)
return *this;
val %= p;
if (val < 0)
val += p;
return *this;
}
bool zp_int::is_equal(const zp_int& rhs) const{
return (val == rhs.val);
}
bool zp_int::is_smaller(const zp_int& rhs) const{
return (val < rhs.val);
}
zp_int zp_int::operator-() const{
return zp_int(-val,p).normalize();
}
zp_int& zp_int::operator=(const zp_int& rhs){
val = rhs.val;
p = rhs.p;
}
zp_int& zp_int::operator^=(const mpz_class& exp){
// cout << "val = " << val << ", exp = " << exp << ", p = " << p << endl;
mpz_powm(val.get_mpz_t(),val.get_mpz_t(),exp.get_mpz_t(),p.get_mpz_t());
return *this;
}
ostream& zp_int::print(ostream& o) const{
o << val;
return o;
}
ostream& zp_int::full_print(ostream& o) const{
o << "("<<val<<", "<<p<<")";
return o;
}
string zp_int::to_s(int base) const{
return val.get_str(base);
}
zp_int operator+(const zp_int& lhs, const zp_int& rhs){
zp_int temp = lhs;
return (temp += rhs);
}
zp_int operator-(const zp_int& lhs, const zp_int& rhs){
zp_int temp = lhs;
return (temp -= rhs);
}
zp_int operator*(const zp_int& lhs, const zp_int& rhs){
zp_int temp = lhs;
return (temp *= rhs);
}
zp_int operator/(const zp_int& lhs, const zp_int& rhs){
zp_int temp = lhs;
return (temp /= rhs);
}
zp_int operator^(const zp_int& lhs, const mpz_class& rhs){
zp_int temp = lhs;
return (temp ^= rhs);
}
bool operator==(const zp_int& lhs, const zp_int& rhs){
return (lhs.is_equal(rhs));
}
bool operator!=(const zp_int& lhs, const zp_int& rhs){
return (!lhs.is_equal(rhs));
}
bool operator<(const zp_int& lhs, const zp_int& rhs){
return (lhs.is_smaller(rhs));
}
ostream& operator<<(ostream& o, const zp_int& rhs){
return rhs.print(o);
}
ZpCoordinate::ZpCoordinate(const ZpJacobian& jac):X(0),Y(0),p(jac.p) {
if (jac.isInfinite()){
*this = ZpCoordinate::infinity();
}
else{
int const_2 = 2, const_3 = 3;
X = (jac.X) / (jac.Z^const_2);
Y = (jac.Y) / (jac.Z^const_3);
}
}
string ZpCoordinate::toCompressedForm(){
string result;
switch (Y % 2){
case 0: result += '-'; break;
case 1: result += '+'; break;
}
result += X.to_s();
return result;
}
ostream& operator<<(ostream& out, const ZpCoordinate& rhs){
out << "(" << rhs.X << ", "<<rhs.Y<<")";
return out;
}
ostream& operator<<(ostream& out, const ZpJacobian& rhs){
out << "(" << rhs.X << ", "<<rhs.Y<<", " <<rhs.Z<<")";
return out;
}