This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElliptic_Curve_Factorization.cpp
More file actions
62 lines (62 loc) · 1.9 KB
/
Elliptic_Curve_Factorization.cpp
File metadata and controls
62 lines (62 loc) · 1.9 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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <numeric>
struct Point {
long long x, y;
};
struct EllipticCurve {
long long a, b;
long long p;
};
Point addPoints(Point P, Point Q, EllipticCurve curve) {
Point result;
if (P.x == Q.x && P.y == Q.y) {
long long slope = (3 * P.x * P.x + curve.a) * static_cast<long long>(std::pow(2 * P.y, curve.p - 2)) % curve.p;
result.x = (slope * slope - 2 * P.x) % curve.p;
result.y = (slope * (P.x - result.x) - P.y) % curve.p;
} else {
long long slope = (Q.y - P.y) * static_cast<long long>(std::pow(Q.x - P.x, curve.p - 2)) % curve.p;
result.x = (slope * slope - P.x - Q.x) % curve.p;
result.y = (slope * (P.x - result.x) - P.y) % curve.p;
}
result.x = (result.x + curve.p) % curve.p;
result.y = (result.y + curve.p) % curve.p;
return result;
}
long long ellipticCurveFactorization(long long n, long long iterations) {
EllipticCurve curve;
curve.a = 2;
curve.b = 2;
curve.p = n;
Point P;
P.x = 0;
P.y = 1;
for (long long i = 0; i < iterations; ++i) {
long long x = rand() % n;
Point Q = P;
for (long long j = 0; j < x; ++j) {
Q = addPoints(Q, P, curve);
}
long long factor = std::gcd(n, Q.x);
if (factor > 1 && factor < n) {
return factor;
}
}
return -1;
}
int main() {
srand(time(0));
long long number;
std::cout << "Enter a number to factorize: ";
std::cin >> number;
long long factor = ellipticCurveFactorization(number, 1000);
if (factor != -1) {
std::cout << "Found factor: " << factor << std::endl;
std::cout << "Other factor: " << number / factor << std::endl;
} else {
std::cout << "Failed to perform factorization!\nTry increasing the number of iterations." << std::endl;
}
return 0;
}