-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquicksand.cpp
238 lines (223 loc) · 6.3 KB
/
quicksand.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
#include "quicksand.h"
using namespace quicksand;
static inline std::string generateHeader(std::vector<unsigned int>& in){
std::string result;
for (auto& i: in) {
result+=std::to_string(i) + "-";
}
result.pop_back();
unsigned char hash_buff[32]; //32
crypto_hash_sha256(hash_buff, (unsigned char*)result.c_str(),result.length());
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (int i = 0; i < 32; ++i)
{
ss << std::setw(2) << static_cast<unsigned>(hash_buff[i]);
}
return ss.str();
};
std::string quicksand::generateStamp(unsigned int iterations, unsigned int size,
unsigned int edgePercentage, unsigned int shift, std::string header)
{
std::vector<std::vector<unsigned int>> result;
std::stringstream ss;
QuickSandSolver qs(size,edgePercentage);
int curIter = 0;
while (curIter<iterations) {
QuickSandHeader qh(header.c_str(),shift);
std::vector<unsigned int> solved = qs.solve(&qh);
if (solved.size() == size) {
header = generateHeader(solved);
result.push_back(solved);
}
else
curIter--;
curIter++;
}
for (int i = 0 ; i<result.size();i++){
for (int j = 0 ; j<result[i].size();j++){
ss<<result[i][j];
if (j!=result[i].size()-1)
ss<<",";
}
if (i!=result.size()-1) {
ss<<"|";
}
}
return ss.str();
}
QuickSandHeader::QuickSandHeader(const char* header, unsigned int shift){
if (!(shift&32))
size=1LL<<shift;
halfSize=size>>1;
field1=halfSize-1;
unsigned char hash_buff[32];
crypto_hash_sha256(hash_buff, (unsigned char*)header,strlen(header));
unsigned long long res = u8ToU64(hash_buff);
unsigned long long res2 = u8ToU64(hash_buff+8);
data[0]=res^0x736f6d6570736575ULL;
data[1]=res2^0x646f72616e646f6dULL;
data[2]=res^0x6c7967656e657261ULL;
data[3]=res2^0x7465646279746573ULL;
}
unsigned long long QuickSandHeader::u8ToU64(const unsigned char* data){
unsigned long long res; //before same as down
memcpy(&res,data,8);
return res;
}
void QuickSandHeader::sipRound(unsigned long long& v0 , unsigned long long& v1, unsigned long long& v2, unsigned long long& v3){
v0 += v1;
v2 += v3;
v1 = rotateLeft(v1,13);
v3 = rotateLeft(v3,16);
v1 ^= v0;
v3 ^= v2;
v0 = rotateLeft(v0,32);
v2 += v1;
v0 += v3;
v1 = rotateLeft(v1,17);
v3 = rotateLeft(v3,21);
v1 ^= v2;
v3 ^= v0;
v2 = rotateLeft(v2,32);
}
unsigned long long QuickSandHeader::sipHash24(unsigned long long msg){ //+
auto v0=data[0];
auto v1=data[1];
auto v2=data[2];
auto v3=data[3];
v3^=msg;
sipRound(v0,v1,v2,v3);
sipRound(v0,v1,v2,v3);
v2^=0xff;
v0^=msg;
sipRound(v0,v1,v2,v3);
sipRound(v0,v1,v2,v3);
sipRound(v0,v1,v2,v3);
sipRound(v0,v1,v2,v3);
return v0^v1^v2^v3;
}
unsigned long long QuickSandHeader::sipNode(unsigned int seed, unsigned int parity){
return (unsigned int)(sipHash24(parity+2*seed))&field1;
}
QuickSandHeader::sipNoderes QuickSandHeader::sipEdge(unsigned int msg){
sipNoderes res{0,0};
res.node1 = sipNode(msg,0);
res.node2 = sipNode(msg,1);
return res;
}
QuickSandSolver::QuickSandSolver(unsigned int size, int edgePercentage){
stopFlag=0;
this->size=size;
this->edgePercentage=edgePercentage;
}
std::vector<unsigned int> QuickSandSolver::solve(QuickSandHeader* qhInstance){
auto numCycles = qhInstance->getSize()*edgePercentage/100;
std::vector<unsigned int> v0;
std::vector<unsigned int> result;
auto size = qhInstance->getSize()+1;
if (size!=0) {
if (size>0x3fffffff)
throw std::bad_alloc();
v0.assign(size,0);
}
std::vector<unsigned int> v1(8192,0);
std::vector<unsigned int> v2(8192,0);
if (numCycles ==0)
return result;
for (unsigned int cycle = 0; cycle < numCycles && !stopFlag;cycle++){
auto edge = qhInstance->sipEdge(cycle);
auto node1 = edge.node1;
auto node2 = edge.node2;
auto node1m = node1+1;
auto node2m = node2+qhInstance->getHalfSize()+1;
if (v0[node2m]==node1m || v0[node1m]==node2m)
continue;
v1[0]=node1m;
v2[0]=node2m;
auto path1 = path(v0[node1m],v1,v0);
auto path2 = path(v0[node2m],v2,v0);
if (v1[path1]==v2[path2]){
if (path2>path1){
path2-=path1;
path1=0;
}
else {
path1-=path2;
path2=0;
}
while(v1[path1]!=v2[path2]){
path1++;
path2++;
}
if (this->size==path1+path2+1){
recoverSolution(result, path1,path2,v1,v2,qhInstance,this->size,numCycles);
return result;
}
}
else if (path1<path2) {
while (path1 != 0) {
v0[v1[path1]]=v1[path1-1];
path1--;
}
v0[node1m]=node2m;
}
else {
while (path2 != 0) {
v0[v2[path2]]=v2[path2-1];
path2--;
}
v0[node2m]=node1m;
}
}
return result;
}
unsigned int QuickSandSolver::path(unsigned int value, std::vector<unsigned int>& v1, std::vector<unsigned int>& v2){
if (!value)
return 0;
unsigned int i;
for (i =1;i<8192;i++){
v1[i]=value;
value=v2[value];
if (!value)
return i;
}
for (i=8191;i>0;i--)
if (v1[i]==value)
throw std::runtime_error("[-] Illegal cycle has occured");
throw std::runtime_error("[-] Maximum path length was exceeded");
}
void QuickSandSolver::recoverSolution(std::vector<unsigned int>& res, unsigned int index1, unsigned int index2, std::vector<unsigned int>& v1, std::vector<unsigned int>& v2, QuickSandHeader* qhInstance, unsigned int size, unsigned long long numCycles){
auto var1 = numCycles;
res.resize(size);
std::set<std::pair<unsigned int, unsigned int>> set;
set.insert(std::make_pair(v1[0],v2[0]));
while (index1){
set.insert(std::make_pair(v1[index1&0xfffffffe],v1[(index1-1)|1]));
index1--;
}
while(index2){
set.insert(std::make_pair(v2[(index2-1)|1], v2[index2&0xfffffffe]));//0xFFFFFFFE
index2--;
}
if (!numCycles)
return;
unsigned int cycle=0;
unsigned int i = 0;
do {
auto hs = qhInstance->getHalfSize();
auto res1 = qhInstance->sipNode(cycle,1);
res1+=hs+1;
auto res2 = qhInstance->sipNode(cycle,0);
res2++;
auto node = set.find(std::make_pair(res2,res1));
if (node == set.end()){
cycle++;
continue;
}
res[i++] = cycle;
set.erase(node);
cycle++;
} while(cycle<numCycles);
return;
}