-
Notifications
You must be signed in to change notification settings - Fork 59
/
eradicate2.cpp
381 lines (325 loc) · 12 KB
/
eradicate2.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
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <random>
#include <map>
#include <set>
#if defined(__APPLE__) || defined(__MACOSX)
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include "hexadecimal.hpp"
#include "Dispatcher.hpp"
#include "ArgParser.hpp"
#include "ModeFactory.hpp"
#include "types.hpp"
#include "help.hpp"
#include "sha3.hpp"
std::string readFile(const char * const szFilename)
{
std::ifstream in(szFilename, std::ios::in | std::ios::binary);
std::ostringstream contents;
contents << in.rdbuf();
return contents.str();
}
std::vector<cl_device_id> getAllDevices(cl_device_type deviceType = CL_DEVICE_TYPE_GPU)
{
std::vector<cl_device_id> vDevices;
cl_uint platformIdCount = 0;
clGetPlatformIDs (0, NULL, &platformIdCount);
std::vector<cl_platform_id> platformIds (platformIdCount);
clGetPlatformIDs (platformIdCount, platformIds.data (), NULL);
for( auto it = platformIds.cbegin(); it != platformIds.cend(); ++it ) {
cl_uint countDevice;
clGetDeviceIDs(*it, deviceType, 0, NULL, &countDevice);
std::vector<cl_device_id> deviceIds(countDevice);
clGetDeviceIDs(*it, deviceType, countDevice, deviceIds.data(), &countDevice);
std::copy( deviceIds.begin(), deviceIds.end(), std::back_inserter(vDevices) );
}
return vDevices;
}
template <typename T, typename U, typename V, typename W>
T clGetWrapper(U function, V param, W param2) {
T t;
function(param, param2, sizeof(t), &t, NULL);
return t;
}
template <typename U, typename V, typename W>
std::string clGetWrapperString(U function, V param, W param2) {
size_t len;
function(param, param2, 0, NULL, &len);
char * const szString = new char[len];
function(param, param2, len, szString, NULL);
std::string r(szString);
delete[] szString;
return r;
}
template <typename T, typename U, typename V, typename W>
std::vector<T> clGetWrapperVector(U function, V param, W param2) {
size_t len;
function(param, param2, 0, NULL, &len);
len /= sizeof(T);
std::vector<T> v;
if (len > 0) {
T * pArray = new T[len];
function(param, param2, len * sizeof(T), pArray, NULL);
for (size_t i = 0; i < len; ++i) {
v.push_back(pArray[i]);
}
delete[] pArray;
}
return v;
}
std::vector<std::string> getBinaries(cl_program & clProgram) {
std::vector<std::string> vReturn;
auto vSizes = clGetWrapperVector<size_t>(clGetProgramInfo, clProgram, CL_PROGRAM_BINARY_SIZES);
if (!vSizes.empty()) {
unsigned char * * pBuffers = new unsigned char *[vSizes.size()];
for (size_t i = 0; i < vSizes.size(); ++i) {
pBuffers[i] = new unsigned char[vSizes[i]];
}
clGetProgramInfo(clProgram, CL_PROGRAM_BINARIES, vSizes.size() * sizeof(unsigned char *), pBuffers, NULL);
for (size_t i = 0; i < vSizes.size(); ++i) {
std::string strData(reinterpret_cast<char *>(pBuffers[i]), vSizes[i]);
vReturn.push_back(strData);
delete[] pBuffers[i];
}
delete[] pBuffers;
}
return vReturn;
}
template <typename T> bool printResult(const T & t, const cl_int & err) {
std::cout << ((t == NULL) ? lexical_cast::write(err) : "OK") << std::endl;
return t == NULL;
}
bool printResult(const cl_int err) {
std::cout << ((err != CL_SUCCESS) ? lexical_cast::write(err) : "OK") << std::endl;
return err != CL_SUCCESS;
}
std::string keccakDigest(const std::string data) {
char digest[32];
sha3(data.c_str(), data.size(), digest, 32);
return std::string(digest, 32);
}
void trim(std::string & s) {
const auto iLeft = s.find_first_not_of(" \t\r\n");
if (iLeft != std::string::npos) {
s.erase(0, iLeft);
}
const auto iRight = s.find_last_not_of(" \t\r\n");
if (iRight != std::string::npos) {
const auto count = s.length() - iRight - 1;
s.erase(iRight + 1, count);
}
}
std::string makePreprocessorInitHashExpression(const std::string & strAddressBinary, const std::string & strInitCodeDigest) {
std::random_device rd;
std::mt19937_64 eng(rd());
std::uniform_int_distribution<unsigned int> distr; // C++ requires integer type: "C2338 note : char, signed char, unsigned char, int8_t, and uint8_t are not allowed"
ethhash h = { 0 };
h.b[0] = 0xff;
for (int i = 0; i < 20; ++i) {
h.b[i + 1] = strAddressBinary[i];
}
for (int i = 0; i < 32; ++i) {
h.b[i + 21] = distr(eng);
}
for (int i = 0; i < 32; ++i) {
h.b[i + 53] = strInitCodeDigest[i];
}
h.b[85] ^= 0x01;
std::ostringstream oss;
oss << std::hex;
for (int i = 0; i < 25; ++i) {
oss << "0x" << h.q[i];
if (i + 1 != 25) {
oss << ",";
}
}
return oss.str();
}
int main(int argc, char * * argv) {
try {
ArgParser argp(argc, argv);
bool bHelp = false;
bool bModeBenchmark = false;
bool bModeZeroBytes = false;
bool bModeZeros = false;
bool bModeLetters = false;
bool bModeNumbers = false;
std::string strModeLeading;
std::string strModeMatching;
bool bModeLeadingRange = false;
bool bModeRange = false;
bool bModeMirror = false;
bool bModeDoubles = false;
int rangeMin = 0;
int rangeMax = 0;
std::vector<size_t> vDeviceSkipIndex;
size_t worksizeLocal = 128;
size_t worksizeMax = 0; // Will be automatically determined later if not overriden by user
size_t size = 16777216;
std::string strAddress;
std::string strInitCode;
std::string strInitCodeFile;
argp.addSwitch('h', "help", bHelp);
argp.addSwitch('0', "benchmark", bModeBenchmark);
argp.addSwitch('z', "zero-bytes", bModeZeroBytes);
argp.addSwitch('1', "zeros", bModeZeros);
argp.addSwitch('2', "letters", bModeLetters);
argp.addSwitch('3', "numbers", bModeNumbers);
argp.addSwitch('4', "leading", strModeLeading);
argp.addSwitch('5', "matching", strModeMatching);
argp.addSwitch('6', "leading-range", bModeLeadingRange);
argp.addSwitch('7', "range", bModeRange);
argp.addSwitch('8', "mirror", bModeMirror);
argp.addSwitch('9', "leading-doubles", bModeDoubles);
argp.addSwitch('m', "min", rangeMin);
argp.addSwitch('M', "max", rangeMax);
argp.addMultiSwitch('s', "skip", vDeviceSkipIndex);
argp.addSwitch('w', "work", worksizeLocal);
argp.addSwitch('W', "work-max", worksizeMax);
argp.addSwitch('S', "size", size);
argp.addSwitch('A', "address", strAddress);
argp.addSwitch('I', "init-code", strInitCode);
argp.addSwitch('i', "init-code-file", strInitCodeFile);
if (!argp.parse()) {
std::cout << "error: bad arguments, try again :<" << std::endl;
return 1;
}
if (bHelp) {
std::cout << g_strHelp << std::endl;
return 0;
}
// Parse hexadecimal values and/or read init code from file
if (strInitCodeFile != "") {
std::ifstream ifs(strInitCodeFile);
if (!ifs.is_open()) {
std::cout << "error: failed to open input file for init code" << std::endl;
return 1;
}
strInitCode.assign(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>());
}
trim(strInitCode);
const std::string strAddressBinary = parseHexadecimalBytes(strAddress);
const std::string strInitCodeBinary = parseHexadecimalBytes(strInitCode);
const std::string strInitCodeDigest = keccakDigest(strInitCodeBinary);
const std::string strPreprocessorInitStructure = makePreprocessorInitHashExpression(strAddressBinary, strInitCodeDigest);
mode mode = ModeFactory::benchmark();
if (bModeBenchmark) {
mode = ModeFactory::benchmark();
} else if (bModeZeroBytes) {
mode = ModeFactory::zerobytes();
} else if (bModeZeros) {
mode = ModeFactory::zeros();
} else if (bModeLetters) {
mode = ModeFactory::letters();
} else if (bModeNumbers) {
mode = ModeFactory::numbers();
} else if (!strModeLeading.empty()) {
mode = ModeFactory::leading(strModeLeading.front());
} else if (!strModeMatching.empty()) {
mode = ModeFactory::matching(strModeMatching);
} else if (bModeLeadingRange) {
mode = ModeFactory::leadingRange(rangeMin, rangeMax);
} else if (bModeRange) {
mode = ModeFactory::range(rangeMin, rangeMax);
} else if(bModeMirror) {
mode = ModeFactory::mirror();
} else if (bModeDoubles) {
mode = ModeFactory::doubles();
} else {
std::cout << g_strHelp << std::endl;
return 0;
}
std::vector<cl_device_id> vFoundDevices = getAllDevices();
std::vector<cl_device_id> vDevices;
std::map<cl_device_id, size_t> mDeviceIndex;
std::vector<std::string> vDeviceBinary;
std::vector<size_t> vDeviceBinarySize;
cl_int errorCode;
std::cout << "Devices:" << std::endl;
for (size_t i = 0; i < vFoundDevices.size(); ++i) {
// Ignore devices in skip index
if (std::find(vDeviceSkipIndex.begin(), vDeviceSkipIndex.end(), i) != vDeviceSkipIndex.end()) {
continue;
}
cl_device_id & deviceId = vFoundDevices[i];
const auto strName = clGetWrapperString(clGetDeviceInfo, deviceId, CL_DEVICE_NAME);
const auto computeUnits = clGetWrapper<cl_uint>(clGetDeviceInfo, deviceId, CL_DEVICE_MAX_COMPUTE_UNITS);
const auto globalMemSize = clGetWrapper<cl_ulong>(clGetDeviceInfo, deviceId, CL_DEVICE_GLOBAL_MEM_SIZE);
std::cout << " GPU" << i << ": " << strName << ", " << globalMemSize << " bytes available, " << computeUnits << " compute units" << std::endl;
vDevices.push_back(vFoundDevices[i]);
mDeviceIndex[vFoundDevices[i]] = i;
}
if (vDevices.empty()) {
return 1;
}
std::cout << std::endl;
std::cout << "Initializing OpenCL..." << std::endl;
std::cout << " Creating context..." << std::flush;
auto clContext = clCreateContext( NULL, vDevices.size(), vDevices.data(), NULL, NULL, &errorCode);
if (printResult(clContext, errorCode)) {
return 1;
}
cl_program clProgram;
if (vDeviceBinary.size() == vDevices.size()) {
// Create program from binaries
std::cout << " Loading kernel from binary..." << std::flush;
const unsigned char * * pKernels = new const unsigned char *[vDevices.size()];
for (size_t i = 0; i < vDeviceBinary.size(); ++i) {
pKernels[i] = reinterpret_cast<const unsigned char *>(vDeviceBinary[i].data());
}
cl_int * pStatus = new cl_int[vDevices.size()];
clProgram = clCreateProgramWithBinary(clContext, vDevices.size(), vDevices.data(), vDeviceBinarySize.data(), pKernels, pStatus, &errorCode);
if(printResult(clProgram, errorCode)) {
return 1;
}
} else {
// Create a program from the kernel source
std::cout << " Compiling kernel..." << std::flush;
const std::string strKeccak = readFile("keccak.cl");
const std::string strVanity = readFile("eradicate2.cl");
const char * szKernels[] = { strKeccak.c_str(), strVanity.c_str() };
clProgram = clCreateProgramWithSource(clContext, sizeof(szKernels) / sizeof(char *), szKernels, NULL, &errorCode);
if (printResult(clProgram, errorCode)) {
return 1;
}
}
// Build the program
std::cout << " Building program..." << std::flush;
const std::string strBuildOptions = "-D ERADICATE2_MAX_SCORE=" + lexical_cast::write(ERADICATE2_MAX_SCORE) + " -D ERADICATE2_INITHASH=" + strPreprocessorInitStructure;
if (printResult(clBuildProgram(clProgram, vDevices.size(), vDevices.data(), strBuildOptions.c_str(), NULL, NULL))) {
#ifdef ERADICATE2_DEBUG
std::cout << std::endl;
std::cout << "build log:" << std::endl;
size_t sizeLog;
clGetProgramBuildInfo(clProgram, vDevices[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &sizeLog);
char * const szLog = new char[sizeLog];
clGetProgramBuildInfo(clProgram, vDevices[0], CL_PROGRAM_BUILD_LOG, sizeLog, szLog, NULL);
std::cout << szLog << std::endl;
delete[] szLog;
#endif
return 1;
}
std::cout << std::endl;
Dispatcher d(clContext, clProgram, worksizeMax == 0 ? size : worksizeMax, size);
for (auto & i : vDevices) {
d.addDevice(i, worksizeLocal, mDeviceIndex[i]);
}
d.run(mode);
clReleaseContext(clContext);
return 0;
} catch (std::runtime_error & e) {
std::cout << "std::runtime_error - " << e.what() << std::endl;
} catch (...) {
std::cout << "unknown exception occured" << std::endl;
}
return 1;
}