-
Notifications
You must be signed in to change notification settings - Fork 181
/
main.cpp
359 lines (308 loc) · 10.1 KB
/
main.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
/*
* This file is part of the BSGS distribution (https://github.com/JeanLucPons/Kangaroo).
* Copyright (c) 2020 Jean Luc PONS.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Kangaroo.h"
#include "Timer.h"
#include "SECPK1/SECP256k1.h"
#include "GPU/GPUEngine.h"
#include <fstream>
#include <string>
#include <string.h>
#include <stdexcept>
using namespace std;
#define CHECKARG(opt,n) if(a>=argc-1) {::printf(opt " missing argument #%d\n",n);exit(0);} else {a++;}
// ------------------------------------------------------------------------------------------
void printUsage() {
printf("Kangaroo [-v] [-t nbThread] [-d dpBit] [gpu] [-check]\n");
printf(" [-gpuId gpuId1[,gpuId2,...]] [-g g1x,g1y[,g2x,g2y,...]]\n");
printf(" inFile\n");
printf(" -v: Print version\n");
printf(" -gpu: Enable gpu calculation\n");
printf(" -gpuId gpuId1,gpuId2,...: List of GPU(s) to use, default is 0\n");
printf(" -g g1x,g1y,g2x,g2y,...: Specify GPU(s) kernel gridsize, default is 2*(MP),2*(Core/MP)\n");
printf(" -d: Specify number of leading zeros for the DP method (default is auto)\n");
printf(" -t nbThread: Secify number of thread\n");
printf(" -w workfile: Specify file to save work into (current processed key only)\n");
printf(" -i workfile: Specify file to load work from (current processed key only)\n");
printf(" -wi workInterval: Periodic interval (in seconds) for saving work\n");
printf(" -ws: Save kangaroos in the work file\n");
printf(" -wss: Save kangaroos via the server\n");
printf(" -wsplit: Split work file of server and reset hashtable\n");
printf(" -wm file1 file2 destfile: Merge work file\n");
printf(" -wmdir dir destfile: Merge directory of work files\n");
printf(" -wt timeout: Save work timeout in millisec (default is 3000ms)\n");
printf(" -winfo file1: Work file info file\n");
printf(" -wpartcreate name: Create empty partitioned work file (name is a directory)\n");
printf(" -wcheck worfile: Check workfile integrity\n");
printf(" -m maxStep: number of operations before give up the search (maxStep*expected operation)\n");
printf(" -s: Start in server mode\n");
printf(" -c server_ip: Start in client mode and connect to server server_ip\n");
printf(" -sp port: Server port, default is 17403\n");
printf(" -nt timeout: Network timeout in millisec (default is 3000ms)\n");
printf(" -o fileName: output result to fileName\n");
printf(" -l: List cuda enabled devices\n");
printf(" -check: Check GPU kernel vs CPU\n");
printf(" inFile: intput configuration file\n");
exit(0);
}
// ------------------------------------------------------------------------------------------
int getInt(string name,char *v) {
int r;
try {
r = std::stoi(string(v));
} catch(std::invalid_argument&) {
printf("Invalid %s argument, number expected\n",name.c_str());
exit(-1);
}
return r;
}
double getDouble(string name,char *v) {
double r;
try {
r = std::stod(string(v));
} catch(std::invalid_argument&) {
printf("Invalid %s argument, number expected\n",name.c_str());
exit(-1);
}
return r;
}
// ------------------------------------------------------------------------------------------
void getInts(string name,vector<int> &tokens,const string &text,char sep) {
size_t start = 0,end = 0;
tokens.clear();
int item;
try {
while((end = text.find(sep,start)) != string::npos) {
item = std::stoi(text.substr(start,end - start));
tokens.push_back(item);
start = end + 1;
}
item = std::stoi(text.substr(start));
tokens.push_back(item);
}
catch(std::invalid_argument &) {
printf("Invalid %s argument, number expected\n",name.c_str());
exit(-1);
}
}
// ------------------------------------------------------------------------------------------
// Default params
static int dp = -1;
static int nbCPUThread;
static string configFile = "";
static bool checkFlag = false;
static bool gpuEnable = false;
static vector<int> gpuId = { 0 };
static vector<int> gridSize;
static string workFile = "";
static string checkWorkFile = "";
static string iWorkFile = "";
static uint32_t savePeriod = 60;
static bool saveKangaroo = false;
static bool saveKangarooByServer = false;
static string merge1 = "";
static string merge2 = "";
static string mergeDest = "";
static string mergeDir = "";
static string infoFile = "";
static double maxStep = 0.0;
static int wtimeout = 3000;
static int ntimeout = 3000;
static int port = 17403;
static bool serverMode = false;
static string serverIP = "";
static string outputFile = "";
static bool splitWorkFile = false;
int main(int argc, char* argv[]) {
#ifdef USE_SYMMETRY
printf("Kangaroo v" RELEASE " (with symmetry)\n");
#else
printf("Kangaroo v" RELEASE "\n");
#endif
// Global Init
Timer::Init();
rseed(Timer::getSeed32());
// Init SecpK1
Secp256K1 *secp = new Secp256K1();
secp->Init();
int a = 1;
nbCPUThread = Timer::getCoreNumber();
while (a < argc) {
if(strcmp(argv[a], "-t") == 0) {
CHECKARG("-t",1);
nbCPUThread = getInt("nbCPUThread",argv[a]);
a++;
} else if(strcmp(argv[a],"-d") == 0) {
CHECKARG("-d",1);
dp = getInt("dpSize",argv[a]);
a++;
} else if (strcmp(argv[a], "-h") == 0) {
printUsage();
} else if(strcmp(argv[a],"-l") == 0) {
#ifdef WITHGPU
GPUEngine::PrintCudaInfo();
#else
printf("GPU code not compiled, use -DWITHGPU when compiling.\n");
#endif
exit(0);
} else if(strcmp(argv[a],"-w") == 0) {
CHECKARG("-w",1);
workFile = string(argv[a]);
a++;
} else if(strcmp(argv[a],"-i") == 0) {
CHECKARG("-i",1);
iWorkFile = string(argv[a]);
a++;
} else if(strcmp(argv[a],"-wm") == 0) {
CHECKARG("-wm",1);
merge1 = string(argv[a]);
CHECKARG("-wm",2);
merge2 = string(argv[a]);
a++;
if(a<argc) {
// classic merge
mergeDest = string(argv[a]);
a++;
}
} else if(strcmp(argv[a],"-wmdir") == 0) {
CHECKARG("-wmdir",1);
mergeDir = string(argv[a]);
CHECKARG("-wmdir",2);
mergeDest = string(argv[a]);
a++;
} else if(strcmp(argv[a],"-wcheck") == 0) {
CHECKARG("-wcheck",1);
checkWorkFile = string(argv[a]);
a++;
} else if(strcmp(argv[a],"-winfo") == 0) {
CHECKARG("-winfo",1);
infoFile = string(argv[a]);
a++;
} else if(strcmp(argv[a],"-o") == 0) {
CHECKARG("-o",1);
outputFile = string(argv[a]);
a++;
} else if(strcmp(argv[a],"-wi") == 0) {
CHECKARG("-wi",1);
savePeriod = getInt("savePeriod",argv[a]);
a++;
} else if(strcmp(argv[a],"-wt") == 0) {
CHECKARG("-wt",1);
wtimeout = getInt("timeout",argv[a]);
a++;
} else if(strcmp(argv[a],"-nt") == 0) {
CHECKARG("-nt",1);
ntimeout = getInt("timeout",argv[a]);
a++;
} else if(strcmp(argv[a],"-m") == 0) {
CHECKARG("-m",1);
maxStep = getDouble("maxStep",argv[a]);
a++;
} else if(strcmp(argv[a],"-ws") == 0) {
a++;
saveKangaroo = true;
} else if(strcmp(argv[a],"-wss") == 0) {
a++;
saveKangarooByServer = true;
} else if(strcmp(argv[a],"-wsplit") == 0) {
a++;
splitWorkFile = true;
} else if(strcmp(argv[a],"-wpartcreate") == 0) {
CHECKARG("-wpartcreate",1);
workFile = string(argv[a]);
Kangaroo::CreateEmptyPartWork(workFile);
exit(0);
} else if(strcmp(argv[a],"-s") == 0) {
a++;
serverMode = true;
} else if(strcmp(argv[a],"-c") == 0) {
CHECKARG("-c",1);
serverIP = string(argv[a]);
a++;
} else if(strcmp(argv[a],"-sp") == 0) {
CHECKARG("-sp",1);
port = getInt("serverPort",argv[a]);
a++;
} else if(strcmp(argv[a],"-gpu") == 0) {
gpuEnable = true;
a++;
} else if(strcmp(argv[a],"-gpuId") == 0) {
CHECKARG("-gpuId",1);
getInts("gpuId",gpuId,string(argv[a]),',');
a++;
} else if(strcmp(argv[a],"-g") == 0) {
CHECKARG("-g",1);
getInts("gridSize",gridSize,string(argv[a]),',');
a++;
} else if(strcmp(argv[a],"-v") == 0) {
::exit(0);
} else if(strcmp(argv[a],"-check") == 0) {
checkFlag = true;
a++;
} else if(a == argc - 1) {
configFile = string(argv[a]);
a++;
} else {
printf("Unexpected %s argument\n",argv[a]);
exit(-1);
}
}
if(gridSize.size() == 0) {
for(int i = 0; i < gpuId.size(); i++) {
gridSize.push_back(0);
gridSize.push_back(0);
}
} else if(gridSize.size() != gpuId.size() * 2) {
printf("Invalid gridSize or gpuId argument, must have coherent size\n");
exit(-1);
}
Kangaroo *v = new Kangaroo(secp,dp,gpuEnable,workFile,iWorkFile,savePeriod,saveKangaroo,saveKangarooByServer,
maxStep,wtimeout,port,ntimeout,serverIP,outputFile,splitWorkFile);
if(checkFlag) {
v->Check(gpuId,gridSize);
exit(0);
} else {
if(checkWorkFile.length() > 0) {
v->CheckWorkFile(nbCPUThread,checkWorkFile);
exit(0);
} if(infoFile.length()>0) {
v->WorkInfo(infoFile);
exit(0);
} else if(mergeDir.length() > 0) {
v->MergeDir(mergeDir,mergeDest);
exit(0);
} else if(merge1.length()>0) {
v->MergeWork(merge1,merge2,mergeDest);
exit(0);
} if(iWorkFile.length()>0) {
if( !v->LoadWork(iWorkFile) )
exit(-1);
} else if(configFile.length()>0) {
if( !v->ParseConfigFile(configFile) )
exit(-1);
} else {
if(serverIP.length()==0) {
::printf("No input file to process\n");
exit(-1);
}
}
if(serverMode)
v->RunServer();
else
v->Run(nbCPUThread,gpuId,gridSize);
}
return 0;
}