-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithms.cpp
253 lines (214 loc) · 5.4 KB
/
Algorithms.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
245
246
247
248
249
250
251
252
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
class Algorithms {
public:
static int* EEA(int, int);
static int MI(int, int);
static int GDC(int, int);
static int FEA(int, int, int);
static std::vector<int> Z(int);
static std::vector<int> primitiveInts(int);
};
// Extended Euclidean Algorithm.
// Find rhe multipolicative inverse of x
// such that : ax = by + 1. Another way
// to think of it is x mod m = 1.
// Returns a and b.
// Example:
// EEA(200,313) is: 36 because
// 36*200 = (23)*313 + 1
int* EEA(int x, int y) {
int* returnVal = new int[2];
int tmp_x = x;
int tmp_y = y;
int tmp_a = 1;
int tmp_b = 0;
int tmp_c = 0;
int tmp_d = 1;
int tmp_e = 0;
int remainder = x % y;
int quotient = x / y ;
do {
remainder = tmp_x % tmp_y;
quotient = tmp_x / tmp_y ;
tmp_x = tmp_y;
tmp_y = remainder;
tmp_e = tmp_c;
tmp_c = tmp_a - quotient * tmp_c;
tmp_a = tmp_e;
tmp_e = tmp_d;
tmp_d = tmp_b - quotient * tmp_d;
tmp_b = tmp_e;
} while (remainder > 0);
returnVal[0] = tmp_a;
returnVal[1] = tmp_b;
return returnVal;
}
// Multipilicative Inverse (brute force).
// Returns the mulitplicitive inverse of x
// such that: ax = by + 1. Returns a.
// Example:
// MI(200, 313) is: 36
// (36)(200) = (somenumber)(313) + 1
int MI(int base, int mod) {
int remainder = 0;
for(int b = 0; b < 16000 ; b++) {
for (int a = 0; a < 16000; a++) {
remainder = (base * a) - (mod * b);
if (remainder == 1)
return a;
}
}
return -1;
}
// Greatest common divisor.
// x = the bigger number
// y = the smaller number
// Example:
// GCD(899, 1914) is: 29
int GCD(int x, int y) {
while (y != 0) {
int remainder = x % y;
x = y;
y = remainder;
}
return x;
}
// Calculates the group Z sub n
// Example:
// Z_n(14) is: { 1, 3, 5, 9, 11, 13 }
std::vector<int> Z_n(int n) {
vector< int > Z;
for ( int i=0 ; i < n ; i++ ) {
int gcd = GCD(i,n);
if (gcd == 1)
Z.push_back(i);
}
return Z;
}
// Fast Exponentiation algorithm.
// Returns x^e mod m
// Example:
// FEA(8, 2, 13) is: 12
// (8)^2 % 13 = 12
int FEA(int x, int e, int m) {
int Y = 1;
while (e != 0) {
if (e % 2 == 0) {
x = ( (x * x) % m);
e = e / 2;
} else {
e = e - 1;
Y = ( (x * Y) % m);
}
}
return Y;
}
// Calculates the primitive roots of a number n.
// Example:
// primitiveRoots(26) is: { 7 11 15 19 }
std::vector<int> primitiveRoots(int n) {
vector< int > primitive_roots;
std::vector<int> Z = Z_n(n);
int phi = Z.size();
int z;
int val;
for ( int i = 0 ; i < phi ; i++ ) {
vector<int> Z_temp = Z;
z = Z[i];
cout << z << " | ";
for ( int ii = 1 ; ii <= phi ; ii++) {
val = FEA(z, ii, n);
Z_temp.erase(std::remove(Z_temp.begin(), Z_temp.end(), val), Z_temp.end());
cout << val << " ";
}
cout << endl;
if (Z_temp.size() == 0)
primitive_roots.push_back(z);
}
return primitive_roots;
}
// Baby-step Giant-step Algorithm.
// Calulates log_b(x) mod p.
// Example:
// BSGS(2,3,101) is: 69
// Log_2(3) mod 101 = 69
int BSGS(int logbase, int x, int mod) {
int m = floor( sqrt(mod - 1) );
vector<int> vec(m);
// Calculate c.
int mult_inv = MI(logbase, mod);
int c = FEA(mult_inv, m, mod);
cout << "c = " << c << endl;
for ( int i = 0 ; i < m ; i++ ) {
vec[i] = FEA(logbase,i,mod);
cout << i << ", " << vec[i] << endl;
}
// vec should be sorted based on the second element
// of the pair. Skippimng that step for now.
// sort(vec)
int temp_x = x;
int j;
int i = 0;
vector<int>::iterator it;
while (i < m) {
it = std::find(vec.begin(), vec.end(), temp_x);
cout << "temp_x = " << temp_x << ", it = " << (*it) << ", other = " << std::distance(vec.begin(), it) << endl;
if (*it != 0) {
j = it - vec.begin();
break;
}
temp_x = (temp_x * c) % mod;
i++;
}
cout << "log_" << logbase << "(" << x << ")" << " % " << mod << " = " <<
m << " * " << i << " + " << j << " = ";
if (j==0)
return 0;
else
return (m*i + j);
}
int main(int argc, char *argv[]) {
cout << "Running in C++" << endl;
// Test Extended Euclidean Algorithm.
int base = 200;
int mod = 313;
int* vals = EEA(200, 313);
cout << "(" << vals[0] << ") * " << base << " + (" << vals[1] << ") * " << mod << " = 1" << endl;
// Test Multiplicative Inverse.
base = 200;
mod = 313;
cout << "Multiplicative Inverse for " << base << " mod " << mod << " = " << MI(base, mod) << endl;
// Test Greatest Common Denominator code.
int x = 899;
int y = 1914;
int gcd = GCD(x, y);
cout << "GCD for " << x << " and " << y << " is: " << gcd << endl;
// Test Fast Exponentiation Algorithm code.
base = 11;
int e = 9;
int m = 29;
int val = FEA(base, e, m);
cout << base << "^" << e << " % " << m << " = " << val << endl;
// Test Z_n code.
int n = 26;
vector<int> Z = Z_n(n);
cout << "Z_" << n << " = { ";
for ( int i = 0 ; i < Z.size() ; i++ )
cout << Z.at(i) << " ";
cout << "}" << endl;
// Test Primitive Roots algorithms.
vector<int> primitive_roots = primitiveRoots(26);
cout << "primitive_roots = { ";
for ( int i = 0 ; i < primitive_roots.size() ; i++ )
cout << primitive_roots.at(i) << " ";
cout << "}" << endl;
// Test Baby-step Giant-step algorithm.
int b = 2;
x = 3;
int p = 101;
val = BSGS(b, x, p);
cout << val << endl;
}