-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathec_ops.cpp
More file actions
78 lines (62 loc) · 1.77 KB
/
ec_ops.cpp
File metadata and controls
78 lines (62 loc) · 1.77 KB
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
#include "ec_ops.h"
#include <cstdio>
#include <cstdlib>
//==================== Methods for Zp ====================
Zp Zp::operator+(const Zp &a) const {
mpz_class result = (this->value + a.value) % PRIME;
Zp c;
c.setValue(result);
return c;
}
Zp Zp::operator*(const Zp &a) const {
mpz_class result = (this->value * a.value) % PRIME;
Zp c;
c.setValue(result);
return c;
}
Zp Zp::operator-(const Zp &a) const {
mpz_class result = (this->value - a.value) % PRIME;
Zp c;
c.setValue(result);
return c;
}
bool Zp::operator==(const Zp &a) const {
if (this->value == a.value)
return true;
// since % in gmpxx returns something between -PRIME+1 and PRIME-1,
// we need the following two checks as well
if (this->value == a.value - PRIME)
return true;
if (this->value == a.value + PRIME)
return true;
return false;
}
ostream& operator<<(ostream& output, const Zp &a){
output << a.value;
return output;
}
//================== Methods for ECpoint ==================
bool ECpoint::operator == (const ECpoint &a) const {
if(this->x == a.x && this->y == a.y)
return true;
return false;
}
ECpoint ECpoint::operator * (const mpz_class &a) const {
return repeatSum(*this, a);
}
ostream& operator << (ostream& output, const ECpoint& a){
if(a.infinityPoint == true)
output << "(INF_POINT)";
else
output << "(" << a.x << "," << a.y << ")";
return output;
}
//================= Methods for ECsystem =================
pair <ECpoint, mpz_class> ECsystem::generateKeys(){
//Generate the private key and public key for the user to whom message is sent
//Returns only the "P" value of public key and "a" value of private key,
//as other parameters are globally defined
privateKey = XA;
publicKey = G*privateKey;
return pair <ECpoint, mpz_class> (publicKey, privateKey);
}