-
Notifications
You must be signed in to change notification settings - Fork 0
/
miller_rabin.cpp
244 lines (196 loc) · 6.8 KB
/
miller_rabin.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// --------------------------------------
// - Miler rabin
// - This code is in Long long (64bits)
// dataset:
// list of small prime: https://t5k.org/lists/small/
// generate prime: https://bigprimes.org/
// test is prime online: https://www.dcode.fr/primality-test
// --------------------------------------
#include <iostream>
#include <iomanip>
#include <time.h>
#include <chrono>
#include <fstream>
using namespace std;
using namespace std::chrono;
// ======================
// >>>>>> Power Mod <<<<<
// Input: base (b), exponent (exp), modulus (m)
// Output: b^e mod m
long power_mod(long long b, long exp, long m)
{
long result = 1;
b %= m;
while (exp > 0)
{
// Check odds
if (exp & 1)
result = result * b % m;
b = b * b % m;
exp >>= 1;
}
return result;
}
// ============================
// >>>>>>>>>> Parse n <<<<<<<<<
// >> Parse n to 2^k * m + 1 <<
//
// Input: n, k, m
// Output: 2^s * d + 1
void parse_number(long long n, int &s, long long &d)
{
n = n - 1;
// while n even
while (n % 2 == 0)
{
s++;
n = n / 2;
}
d = n;
}
// ======================
// >>>> Miller Rabin <<<<
// Input:
// + checking number (n)
// + number of round of testing to perform (k)
// Output:
// + True: probably prime
// + False: composite
bool miller_rabin(long long n, int k)
{
// check if n even or = 2
if (n == 2)
return true;
else if (n < 2 || n % 2 == 0)
return false;
// repeat k times
long long y = 0;
int testing_round = 0;
while (testing_round < k)
{
// Get s and d
int s = 0;
long long d = 0;
parse_number(n, s, d);
// calc a and x
long long a = rand() % (n - 1) + 1;
// cout << "round: " << testing_round << "- a: " << a << endl;
long long x = power_mod(a, d, n);
// repeat s times
for (int i = 0; i < s; i++)
{
y = power_mod(x, 2, n);
// nontrivial square root of 1 modulo n
if ((y == 1) && (x != 1) && (x != n - 1))
return false;
x = y;
}
if (y != 1)
return false;
testing_round++;
}
return true;
}
template <typename T>
long double average(T arr[], long long size)
{
long double sum = 0.0;
// Calculate sum of all elements
for (long long i = 0; i < size; ++i)
sum += arr[i];
// Calculate average
return sum / size;
}
// ======================
// >>>>>> Benchmark <<<<<
void benchmark(int number_of_benchmark_round)
{
int number_of_testing_round = 3;
double total_benchmark_runtime = 0;
double total_runtime[number_of_benchmark_round];
long long total_test[number_of_benchmark_round];
long long total_correct_output[number_of_benchmark_round];
cout << "================[ BENCHMARK ]=================" << endl;
cout << " ***" << endl;
// run round benchmark
int round = 0;
while (round < number_of_benchmark_round)
{
cout << "[+] Benchmarking round <" << round + 1 << ">..." << flush;
total_runtime[round] = 0;
total_test[round] = 0;
total_correct_output[round] = 0;
long long checking_number = 0;
long long max_check_number = 1000000000;
string datasets_file = "./dataset/billion-primes.txt";
// open datasets file
ifstream file(datasets_file);
if (!file.is_open())
{
cout << "[!] Datasets file does not exist." << endl;
file.close();
return;
}
string line;
getline(file, line);
long long prime = stoll(line);
// benchmark
auto start_benchmark_clock = high_resolution_clock::now();
while (checking_number < max_check_number)
{
checking_number++;
total_test[round]++;
auto start_testing_clock = high_resolution_clock::now();
bool is_checking_number_prime = miller_rabin(checking_number, number_of_testing_round);
auto end_testing_clock = high_resolution_clock::now();
// Calculate miller-rabin runtime (ms)
duration<double, milli> testing_runtime = end_testing_clock - start_testing_clock;
// check if miller-rabin return correct output
if ((is_checking_number_prime && checking_number == prime) || (!is_checking_number_prime && checking_number != prime))
total_correct_output[round]++;
// update new prime in file
if ((checking_number >= prime) && getline(file, line))
prime = stoll(line);
total_runtime[round] += testing_runtime.count();
}
auto end_benchmark_clock = high_resolution_clock::now();
// Calculate benchmark runtime (ms) of round round
duration<double, milli> benchmark_runtime = end_benchmark_clock - start_benchmark_clock;
total_benchmark_runtime += benchmark_runtime.count();
cout << fixed << setprecision(6) << "Done! - Run in " << benchmark_runtime.count() << " (ms)" << endl;
file.close();
round++;
}
long double avg_total_test = average(total_test, number_of_benchmark_round);
long double avg_total_correct_output = average(total_correct_output, number_of_benchmark_round);
long double avg_total_runtime = average(total_runtime, number_of_benchmark_round);
cout << "----------------------------------------------" << endl;
cout << "Total runtime for " << number_of_benchmark_round << " benchmark rounds: " << total_benchmark_runtime << " (ms)" << endl;
cout << "\n================[ STATISTICS ]================" << endl;
cout << " *** Average result ***" << endl;
cout << "ROUND" << endl;
cout << fixed << setprecision(0) << "[+] Total test : " << avg_total_test << endl;
cout << fixed << setprecision(0) << "[+] Total correct : " << avg_total_correct_output << endl;
cout << fixed << setprecision(6) << "[+] Total runtime : " << avg_total_runtime << " (ms)" << endl;
cout << "----------------------------------------------" << endl;
cout << "ALGORITHM" << endl;
cout << fixed << setprecision(6) << "[+] Average runtime: " << float(avg_total_runtime) / avg_total_test << " (ms)" << endl;
cout << fixed << setprecision(6) << "[+] Average correct: " << float(avg_total_correct_output) / avg_total_test * 100 << "%" << endl;
}
// ======================
// >>>>>>> Main <<<<<<
int main()
{
cout << " /=================================\\" << endl;
cout << " | |" << endl;
cout << " | MILLER RABIN |" << endl;
cout << " | ____________________ |" << endl;
cout << " | Probabilistic primality test |" << endl;
cout << " | |" << endl;
cout << " \\=================================/" << endl;
cout << " \\_______QuanBlue_______/" << endl
<< endl;
srand(time(NULL));
benchmark(20);
return 0;
}