-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypervulnerable.cpp
448 lines (394 loc) · 10.4 KB
/
hypervulnerable.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include <bits/stdc++.h>
//# define int long long
// note that 'E' is reserved for epsilon transition and hence its use should be avoided in the language
// numbering of states start from 0 (keeping in mind the existence of q0)
const char eps = 'E'; // null/empty transition
using namespace std;
struct nfa
{
// NFA A = (Q,sigma, delta, q0,F)
vector<int> q; // vector of states
set<char> lan; // set of language characters
set<tuple<int, char, int>> trans; // transition states
int q0; // starting state
set<int> f; // set of final states
};
void PrintNFA(nfa a)
{
cout << "States in the attack automaton - "
<< "\n";
for (auto &x : a.q)
{
cout << x << " ";
}
cout << "\n";
cout << "Start state of attack automaton - " << a.q0 << "\n";
cout << "\n";
cout << "Final states of attack automaton - "
<< "\n";
for (auto &x : a.f)
{
cout << x << " ";
}
cout << "\n";
cout << "Transitions in the attack automaton - "
<< "\n";
for (auto &x : a.trans)
{
cout << get<0>(x) << " " << get<1>(x) << " " << get<2>(x) << "\n";
}
cout << "\n";
}
nfa comp(nfa a)
{
nfa a_;
for (auto x : a.q)
{
bool pres = false;
for (auto y : a.f)
{
if (x == y)
{
pres = true;
break;
}
}
if (!pres)
a_.f.insert(x);
}
a_.lan = a.lan;
a_.q = a.q;
a_.q0 = a.q0;
a_.trans = a.trans;
return a_;
}
nfa un(nfa a, nfa b)
{
if (a.q.empty())
return b;
if (b.q.empty())
return a;
nfa ans;
sort(a.q.begin(), a.q.end());
sort(b.q.begin(), b.q.end());
int sizeA = a.q.size();
int sizeB = b.q.size();
// states mapping for A U B
for (int i = 0; i < sizeA * sizeB; i++)
ans.q.push_back(i);
// start state for A U B
int startA = lower_bound(a.q.begin(), a.q.end(), a.q0) - a.q.begin();
int startB = lower_bound(b.q.begin(), b.q.end(), b.q0) - b.q.begin();
ans.q0 = (startA)*sizeB + startB;
// language remains the same
ans.lan = a.lan;
// final states of A U B
for (auto &fA : a.f)
{
int i = lower_bound(a.q.begin(), a.q.end(), fA) - a.q.begin();
for (int &sB : b.q)
{
int j = lower_bound(b.q.begin(), b.q.end(), sB) - b.q.begin();
ans.f.insert(i * sizeB + j);
}
}
for (auto &fB : b.f)
{
int j = lower_bound(b.q.begin(), b.q.end(), fB) - b.q.begin();
for (int &sA : a.q)
{
int i = lower_bound(a.q.begin(), a.q.end(), sA) - a.q.begin();
ans.f.insert(i * sizeB + j);
}
}
// building the transition function for A U B
// a.lan.insert('E');
for (auto ch : a.lan)
{
map<int, set<int>> relA, relB;
for (auto x : a.trans)
{
if (get<1>(x) == ch)
{
relA[get<0>(x)].insert(get<2>(x));
// if (ch==eps) relA[get<0>(x)].insert(get<0>(x));
}
}
for (auto x : b.trans)
{
if (get<1>(x) == ch)
{
relB[get<0>(x)].insert(get<2>(x));
// if (ch==eps) relB[get<0>(x)].insert(get<0>(x));
}
}
for (auto pair1 : relA)
{
for (auto pair2 : relB)
{
int i = lower_bound(a.q.begin(), a.q.end(), pair1.first) - a.q.begin();
int j = lower_bound(b.q.begin(), b.q.end(), pair2.first) - b.q.begin();
for (auto stateA : pair1.second)
{
for (auto stateB : pair2.second)
{
int i1 = lower_bound(a.q.begin(), a.q.end(), stateA) - a.q.begin();
int j1 = lower_bound(b.q.begin(), b.q.end(), stateB) - b.q.begin();
ans.trans.insert(make_tuple(i * (sizeB) + j, ch, i1 * (sizeB) + j1));
}
}
}
}
}
return ans;
}
nfa inter(nfa a, nfa b)
{
// calculated using DeMorgans Law
nfa a_ = comp(a);
nfa b_ = comp(b);
nfa ans = comp(un(a_, b_));
return ans;
}
nfa concat(nfa a, nfa b)
{
// a o b
nfa ans;
sort(a.q.begin(), a.q.end());
sort(b.q.begin(), b.q.end());
int sizeA = a.q.size();
int sizeB = b.q.size();
map<int, int> mapA;
map<int, int> mapB;
// normalizing nfa a
for (int i = 0; i < sizeA; i++)
{
mapA[a.q[i]] = i;
a.q[i] = i;
}
a.q0 = mapA[a.q0];
set<int> finA;
for (auto &x : a.f)
finA.insert(mapA[x]);
a.f = finA;
set<tuple<int, char, int>> trA;
for (auto &x : a.trans)
trA.insert(make_tuple(mapA[get<0>(x)], get<1>(x), mapA[get<2>(x)]));
a.trans = trA;
// PrintNFA(a);
// normalizing nfa b
for (int i = 0; i < sizeB; i++)
{
mapB[b.q[i]] = i + sizeA;
b.q[i] = i + sizeA;
}
b.q0 = mapB[b.q0];
set<int> finB;
for (auto &x : b.f)
finB.insert(mapB[x]);
b.f = finB;
set<tuple<int, char, int>> trB;
for (auto &x : b.trans)
trB.insert(make_tuple(mapB[get<0>(x)], get<1>(x), mapB[get<2>(x)]));
b.trans = trB;
// PrintNFA(b);
// cout<<"\n"<<"\n"<<"\n";
// forming a o b
for (int i = 0; i < sizeA + sizeB; i++)
ans.q.push_back(i);
ans.q0 = a.q0;
ans.f = b.f;
ans.trans = a.trans;
for (auto &x : b.trans)
ans.trans.insert(x);
ans.lan = a.lan;
for (auto &x : a.f)
{
for (auto &y : b.trans)
{
if (get<0>(y) == b.q0)
ans.trans.insert(make_tuple(x, get<1>(y), get<2>(y)));
}
}
return ans;
}
nfa LoopBack(nfa a, int pivot, char ch, int state)
{
// q* is treated as the minimum excludent of a.q
sort(a.q.begin(), a.q.end());
int qStar = a.q.size();
for (int i = 0; i < a.q.size(); i++)
{
if (a.q[i] != i)
{
qStar = i;
break;
}
}
a.q.push_back(qStar);
a.trans.insert(make_tuple(qStar, ch, state));
a.q0 = qStar;
a.f.clear();
a.f.insert(pivot);
return a;
}
nfa AttackForPivot(nfa a, int state)
{
nfa ans;
map<char, vector<int>> m;
for (auto &transition : a.trans)
{
if (get<0>(transition) == state)
{
m[get<1>(transition)].push_back(get<2>(transition));
}
}
for (auto &x : m)
{
auto ch = x.first;
auto vec = m[ch];
for (int i = 0; i < vec.size(); i++)
{
for (int j = i + 1; j < vec.size(); j++)
{
// cout<<"call";
nfa a1 = LoopBack(a, state, ch, vec[i]);
nfa a2 = LoopBack(a, state, ch, vec[j]);
nfa ap = a;
ap.f.clear();
ap.f.insert(state);
// PrintNFA(ap);
nfa as = a;
as.q0 = state;
// PrintNFA(comp(as));
// PrintNFA(concat((concat(ap,inter(a1,a2))),comp(as)));
ans = un(ans, concat((concat(ap, inter(a1, a2))), comp(as)));
// PrintNFA(inter(a1,a2));
}
}
}
return ans;
}
// AttackAutomaton function
nfa AttackAutomaton(nfa a)
{
nfa ans;
for (auto &state : a.q)
{
auto aI = AttackForPivot(a, state);
ans = un(ans, aI);
}
return ans;
}
nfa StateReduction(nfa a){
if (a.q.empty()) return a;
int n = a.q.size();
// adjacency list of all nodes
vector < set <int> > adj(n+1);
int visit[n+1]={0};
for (auto &x : a.trans){
adj[get<0>(x)].insert(get<2>(x));
}
set<int> toBeVis;
toBeVis.insert(a.q0);
map <int, int> newStates;
int ctr = 0;
// visiting the connected component of the nfa starting with starting node
while(!toBeVis.empty()){
int state = *toBeVis.begin();
newStates[state] = ctr++;
visit[state] = 1;
for (auto &x : adj[state]){
if (!visit[x])
toBeVis.insert(x);
}
toBeVis.erase(toBeVis.find(state));
}
nfa ans;
// keeping the lang same
ans.lan = a.lan;
// building the transition function
for (auto &x : a.trans){
if (visit[get<0>(x)]){
ans.trans.insert(make_tuple(newStates[get<0>(x)], get<1>(x), newStates[get<2>(x)]));
}
}
// storing the reduced states
for (int i=0; i < ctr; i++) ans.q.push_back(i);
// assigning the start state
ans.q0 = newStates[a.q0];
// assigning the final states
for (auto &x : a.f){
if (visit[x])
ans.f.insert(newStates[x]);
}
return ans;
}
int main()
{
nfa inp;
cout << "Avoid NFAs with epsilon transitions"
<< "\n"
<< "\n";
cout << "Enter the number of characters in NFA language"
<< "\n";
int l;
cin >> l;
cout << "Now in the next 'l' lines, enter the characters -"
<< "\n";
char ch;
while (l--)
{
cin >> ch;
inp.lan.insert(ch);
}
cout << "Enter the number of states in the input NFA (n)- "
<< "\n";
int n;
cin >> n;
cout << "Now in the next 'n' lines, enter the nodes (not in any particular order) -"
<< "\n";
int x;
while (n--)
{
cin >> x;
inp.q.push_back(x);
}
cout << "Enter the start state -"
<< "\n";
int st;
cin >> st;
inp.q0 = st;
cout << "Enter the number of final states in the input NFA (m)- "
<< "\n";
int m;
cin >> m;
cout << "Now in the next 'm' lines, enter the final nodes (accepting nodes) (not in any particular order) -"
<< "\n";
int y;
while (m--)
{
cin >> y;
inp.f.insert(y);
}
cout << "Enter the number of transitions in the input NFA (t)- "
<< "\n";
int t;
cin >> t;
cout << "Now in the next 't' lines, enter the transitions in the form of (starting node (int), character (char), end node (int)) -"
<< "\n";
int a, b;
char c;
while (t--)
{
cin >> a >> c >> b;
inp.trans.insert(make_tuple(a, c, b));
}
// for (auto &x : inp.trans){
// cout<<get<0>(x)<<" "<<get<1>(x)<<" "<<get<2>(x)<<"\n";
// }
nfa out = AttackAutomaton(inp);
nfa ans = StateReduction(out);
PrintNFA(ans);
return 0;
}