-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECC_p_q_add.cpp
54 lines (51 loc) · 1.37 KB
/
ECC_p_q_add.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
#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
int modInverse(int a, int p) {
a = a % p;
for(int i = 1; i < p; i++) {
if ((a*i) % p == 1) {
return i;
}
}
return -1;
}
void addEllipticCurve(int p, int a, int x1, int y1, int x2, int y2) {
if(x1 == x2 && y1 == y2) {
int lambda_num = (3 * x1 * x1 + a) % p;
int lambda_d = modInverse(2 * y1, p);
int lambda = (lambda_num * lambda_d) % p;
int x3 = (lambda * lambda - 2 * x1) %p;
if(x3 < 0) {
x3 += p;
}
int y3 = (lambda * (x1 - x3) - y1) % p;
if(y3 < 0) {
y3 += p;
}
cout << x3 << " " << y3 << endl;
} else {
int lambda_num = (y2 - y1) % p;
if(lambda_num < 0) {
lambda_num += p;
}
int lambda_d = modInverse(x2 - x1, p);
int lambda = (lambda_num * lambda_d) % p;
int x3 = (lambda * lambda - x1 - x2) % p;
if(x3 < 0) {
x3 += p;
}
int y3 = (lambda * (x1 - x3) - y1) % p;
if(y3 < 0) {
y3 += p;
}
cout << x3 << " " << y3 << endl;
}
}
int main() {
int p, a, b, x1, y1, x2, y2;
cin >> p >> a >> b >> x1 >> y1 >> x2 >> y2;
addEllipticCurve(p, a, x1, y1, x2, y2);
return 0;
}