Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
183 lines (172 loc) · 3.87 KB
/
Copy pathmain.cpp
File metadata and controls
183 lines (172 loc) · 3.87 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <cstdlib>
#include <iostream>
#include <ctime>
#include "uint1024.h"
uint64 rand64() {
uint64 t = 0ull;
for (int i = 0; i < 10; i ++) {
t = t << 5 ^ rand();
}
return t;
}
uint1024 pow_mod(uint1024 x, const uint1024 & m, const uint1024 & n) {
uint1024 res = 1ull;
int mLen = m.getLength();
x %= n;
for (int i = 0; i < mLen; i ++) {
if (m.getBit(i)) {
res = res * x % n;
}
x = x * x % n;
}
return res;
}
const int MRR = 50;
uint64 xList[MRR];
bool runMillerRabin(uint1024 n) {
if (n < 5) {
return n == 2 || n == 3;
}
uint1024 m = n - 1;
uint k = 0;
while (m.getBit(0) == 0u) {
k ++;
m >>= 1;
}
for (int r = 0; r < MRR; r ++) {
uint1024 x = xList[r];
x = x % (n - 2) + 2;
uint1024 xLast = pow_mod(x, m, n);
if (xLast == 1) {
continue;
}
for (uint i = 1; i <= k; i ++) {
x = xLast * xLast % n;
if (x == 1) {
if (xLast != n - 1) {
return false;
} else {
break;
}
}
xLast = x;
}
if (x != 1) {
return false;
}
}
return true;
}
uint64 mul_mod(uint64 x, uint64 y, uint64 n) {
uint64 res = 0ull;
x %= n;
y %= n;
while (y) {
if (y & 1) {
res += x;
res -= res >= n ? n : 0;
}
x <<= 1;
x -= x >= n ? n : 0;
y >>= 1;
}
return res;
}
uint64 pow_mod(uint64 x, uint64 m, uint64 n) {
uint64 res = 1ull;
x %= n;
while (m) {
if (m & 1) {
res = mul_mod(res, x, n);
}
x = mul_mod(x, x, n);
m >>= 1;
}
return res;
}
bool runMillerRabin2(uint64 n) {
if (n < 5) {
return n == 2 || n == 3;
}
uint64 m = n - 1;
uint k = 0;
while ((m & 1) == 0u) {
k ++;
m >>= 1;
}
for (int r = 0; r < MRR; r ++) {
uint64 x = xList[r];
x = x % (n - 2) + 2;
uint64 xLast = pow_mod(x, m, n);
if (xLast == 1) {
continue;
}
for (uint i = 1; i <= k; i ++) {
x = mul_mod(xLast, xLast, n);
if (x == 1) {
if (xLast != n - 1) {
return false;
} else {
break;
}
}
xLast = x;
}
if (x != 1) {
return false;
}
}
return true;
}
void selfJudge1() {
int len = 62;
uint1024 a, b, c;
while (true) {
a.random(len);
b.random(len);
c.random(len);
c += 1;
std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;
uint1024 res1 = pow_mod(a, b, c);
uint64 res2 = pow_mod(a.toUint64(), b.toUint64(), c.toUint64());
if (res1 != res2) {
std::cout << "res1 = " << res1 << ", res2 = " << res2 << std::endl;
break;
}
}
}
void selfJudge2() {
int len = 62;
uint1024 n;
for (int i = 0; i < MRR; i ++) {
do {
xList[i] = rand64();
} while (xList[i] < 10);
}
while (true) {
n.random(len);
std::cout << "n = " << n << std::endl;
bool res1 = runMillerRabin(n);
bool res2 = runMillerRabin2(n.toUint64());
if (res1 != res2) {
std::cout << "res1 = " << res1 << ", res2 = " << res2 << std::endl;
break;
}
}
}
void findPrime() {
int len = 512;
uint1024 n;
do {
n.random(len);
std::cout << "checking: n = " << n << std::endl;
} while(! runMillerRabin(n));
std::cout << "n = " << n << " is a prime number." << std::endl;
}
int main() {
srand(time(0));
//selfJudge1();
//selfJudge2();
findPrime();
return 0;
}