-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
568 lines (488 loc) · 15.8 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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#include <stdio.h>
#include <iomanip>
#include <fstream>
#include <boost/regex.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/format.hpp>
#include <boost/program_options.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
namespace fs = boost::filesystem;
namespace po = boost::program_options;
namespace pt = boost::posix_time;
#include "kernel.h"
#include "factors.h"
const unsigned int MAX_STAGE1_BOUND = 5000000;
// Struktura uchovavajici konfiguraci prectenou z parametru nebo ze vstupu
struct progArgs {
string N;
string curveGen;
vector<unsigned int> B1;
unsigned int curB1,genStart;
unsigned short windowSize;
bool verbose;
bool noLCM;
bool exitOnFinish;
bool greedy;
bool onePrimeFile;
bool saveHistogram;
bool saveSummary;
bool meta;
unsigned short whichDevice;
string outputFile;
progArgs()
: verbose(false), noLCM(false), onePrimeFile(false), greedy(false),
exitOnFinish(false), curB1(0), windowSize(0), whichDevice(0), genStart(1)
{ }
};
// Přečte parametry programu
void parseArguments(int argc,char** argv,progArgs& args)
{
po::options_description desc("List of supported options");
desc.add_options()
("help,h", "Print usage information.")
("verbose,v", "More verbose output.")
("histogram,H", "Save curve histogram.")
("no-meta-info,M", "Do not print extended information into prime factors file.")
("save-summary,S", "Save all sessions summary to a file at the end.")
("dont-compute-bound,x", "Coefficient s = B1 when set, otherwise s = lcm(1,2...B1).")
("no-restart,e", "When set, program terminates automatically after finishing.")
("device-id,D", po::value<unsigned short>(&args.whichDevice)->default_value(0),
"ID of a CUDA device used for computation.")
("N-to-factor,N", po::value<string>(),
"Number to factor.")
("curve-generator,c", po::value<string>(),
"Curve generator name or path to a file containing curves used for factoring. Valid generator names: Ed16,Ed12,Tw6,Tw8,Tw4,All")
("generator-init,i", po::value<unsigned int>(&args.genStart)->default_value(1),
"Generator initial point coefficient. Set 0 for random value.")
("stage1-bound,B", po::value<vector<unsigned int>>()->multitoken(),
"Bound for ECM stage 1 or range set by 'start stride end'")
("greedy,g", "If set, wait for input of another N to factor after finishing.")
("window-size,W", po::value<unsigned short>(&args.windowSize)->default_value(4),
"Size of sliding window.")
("single-output,s","If set, only one output file for found prime factors will be created.")
("output-file,o",po::value<string>()->default_value("factors-of"),
"File name where to output all found prime factors.");
po::variables_map vm;
po::store(po::parse_command_line(argc,argv,desc),vm);
po::notify(vm);
args.verbose = vm.count("verbose") != 0;
args.noLCM = vm.count("dont-compute-bound") != 0;
args.exitOnFinish = vm.count("no-restart") != 0 || vm.count("greedy") != 0;;
args.greedy = vm.count("greedy") != 0;
args.onePrimeFile = vm.count("single-output") != 0;
args.saveHistogram = vm.count("histogram") != 0;
args.saveSummary = vm.count("save-summary") != 0;
args.meta = vm.count("no-meta-info") == 0;
// Vygeneruj náhodný start pro křivkový generátor
if (args.genStart == 0)
{
boost::random::mt19937 gen((unsigned int)std::time(0));
boost::random::uniform_int_distribution<> dist(1, 100);
args.genStart = dist(gen);
cout << "NOTE: Random generator initial coefficient: " << args.genStart << endl;
}
if (vm.count("N-to-factor"))
args.N = vm["N-to-factor"].as<string>();
if (vm.count("curve-generator"))
args.curveGen = vm["curve-generator"].as<string>();
if (vm.count("stage1-bound"))
args.B1 = vm["stage1-bound"].as<vector<unsigned int>>();
if (vm.count("output-file"))
args.outputFile = vm["output-file"].as<string>();
if (vm.count("help"))
cout << endl << desc << endl << "-----------------------------------------" << endl << endl;
}
string formatTime(pt::ptime now)
{
static std::locale loc(cout.getloc(),new pt::time_facet("%Y%m%d_%H%M%S"));
stringstream ss;
ss.imbue(loc);
ss << now;
return ss.str();
}
Torsion getGenerator(string& genName)
{
if (genName == "Ed16")
return Z2xZ8;
else if (genName == "Ed12")
return Z12;
else if (genName == "Tw8")
return Z8;
else if (genName == "Tw4")
return Z2xZ4;
else return Z6;
}
// Zkontroluje úplnost parametrů, případně požádá o doplnění
int validateArguments(progArgs& args)
{
bool recheck = false;
const static boost::regex numVal("^[0-9]+$");
if (args.N.empty() || !regex_match(args.N,numVal))
{
// Načíst N
cout << "Enter N:" << endl;
if (!getline(cin,args.N))
return 1;
cout << endl;
recheck = true;
}
if (!args.curveGen.empty())
{
// Zkontrolovat a nahradit krátké zástupce souborů
if (args.curveGen.length() > 4)
{
// Zkontrolovat existenci souborů
fs::path p(args.curveGen);
if (!fs::exists(p) || !fs::is_regular_file(p))
{
// Načíst znovu název souboru s křivkami, pokud je neplatný
cout << "Path to curve file " << args.curveGen << " is invalid, please re-enter valid path or generator name:" << endl;
cin >> args.curveGen;
cout << endl;
recheck = true;
}
}
else
{
// Ověřit jméno generátoru křivek
const static boost::regex genCheck("(Tw[864]{1,1})|(Ed1[62]{1,1})|(All)");
if (!regex_match(args.curveGen,genCheck))
{
cout << "Invalid curve generator name, please re-enter valid path or generator name:" << endl;
cin >> args.curveGen;
cout << endl;
recheck = true;
}
}
}
else
{
// Načíst soubory s křivkami, není-li žádný uveden
cout << "Enter curve generator or path to a curve file:" << endl;
cin >> args.curveGen;
cout << endl;
recheck = true;
}
if (args.B1.size() == 1 || args.B1.size() == 3)
{
// Zkontrolovat velikost první hodnoty
if (args.B1[0] < 2 || args.B1[0] > MAX_STAGE1_BOUND)
{
cout << "Enter stage 1 bound B1:" << endl;
cin >> args.B1[0];
cout << endl;
recheck = true;
}
if (args.B1.size() == 3)
{
// Zkontrolovat velikost poslední hodnoty
if (args.B1[0] >= args.B1[2] || args.B1[2] > MAX_STAGE1_BOUND)
{
cout << "Enter stage 1 bound end:" << endl;
cin >> args.B1[2];
cout << endl;
recheck = true;
} // Zkontrolovat velikost kroku
else if (args.B1[1] > args.B1[2] || args.B1[1] < 128)
{
cout << "Enter stage 1 bound stride:" << endl;
cin >> args.B1[1];
cout << endl;
recheck = true;
}
}
}
else
{
// Načíst hranice první fáze
string bounds;
cout << "Enter stage 1 bound B1 or Start,Stride,End for range:" << endl;
cin >> bounds;
cout << endl;
vector<string> bs;
boost::split(bs,bounds,boost::is_any_of(";,:"));
std::transform(bs.begin(),bs.end(),std::back_inserter(args.B1),
[](const string& s) { return std::stoi(s); });
recheck = true;
}
if (args.windowSize < 2 || args.windowSize > 15)
{
// Velikost okna
cout << "Enter window size:" << endl;
cin >> args.windowSize;
cout << endl;
recheck = true;
}
args.curB1 = args.B1[0];
if (recheck) return validateArguments(args);
return 0;
}
int main(int argc,char** argv)
{
cout << "ScissorHands ECM Factorization" << endl;
cout << "by Lukas Pohanka, FNSPE CTU, 2014" << endl;
cout << endl;
progArgs args;
int exitCode = 0,lastB1 = 0,runNum = 0;
pt::time_duration cudaTimeCounter,cudaTotalTime;
bool useMixedStrategy = false,fullFactorizationFound = false;
char c = 0;
// Jsou-li předány parametry, použij je. Jinak se na ně zeptej.
parseArguments(argc,argv,args);
validateArguments(args);
FoundFactors ffact(args.meta);
// Inicializace N
mpz_t zN;
mpz_init_set_str(zN,args.N.c_str(),10);
ffact.startNewSession(args.N);
// Inicializace proměnných
ExtendedPoint infty; // Neutrální prvek
ComputeConfig ax; // Pomocná struktura
NAF S; // NAF rozvoj
mpz_t zS,zInvW,zX,zY; // Pomocné proměnné
cudaError_t cudaStatus; // Proměnná pro chybové kódy GPU
ExtendedPoint *PP; // Adresa všech bodů
int read_curves,edwards,twisted; // Počty načtených typů křivek
int bitCountN; // Počet bitů N
computeStrategy strategy; // Strategie výpočtu
Generator* gen; // Generátor křivek
pt::ptime totalStart = pt::microsec_clock::local_time();
if (args.curveGen.length() > 4)
gen = new FileGenerator(args.curveGen,192);
else if (args.curveGen == "All")
{
vector<GeneratorSetup> gs;
gs.push_back(GeneratorSetup(16,Z6));
gs.push_back(GeneratorSetup(48,Z8));
gs.push_back(GeneratorSetup(32,Z2xZ4));
gs.push_back(GeneratorSetup(86,Z12));
gs.push_back(GeneratorSetup(10,Z2xZ8));
gen = new MixedGenerator(args.genStart,192,gs);
}
else gen = new EdwardsGenerator(getGenerator(args.curveGen),args.genStart,192);
gen->new_point_set();
restart_bound:
// Pokud je N prvočíslo, není co faktorizovat
if (is_almost_surely_prime(zN) || mpz_cmp_ui(zN,1) == 0 || mpz_cmp_ui(zN,0) == 0)
{
cout << "ERROR: N equals 0,1 or is almost surely a prime." << endl;
exitCode = 1;
goto end;
}
// Kontrola velikosti N
bitCountN = (int)mpz_sizeinbase(zN,2);
if (bitCountN > NB_DIGITS*SIZE_DIGIT)
{
cout << "ERROR: Cannot factor numbers longer than " << NB_DIGITS*SIZE_DIGIT << " bits." << endl;
exitCode = 1;
goto end;
}
cout << "NOTE: N is " << bitCountN << " bits long." << endl;
PP = NULL;
read_curves = edwards = twisted = 0;
strategy = computeStrategy::csNone;
try {
// Pokus se načíst křivky a zvolit vhodnou strategii
strategy = readCurves(gen,zN,&PP,edwards,twisted,read_curves);
}
catch (mpz_t zF)
{
// Ošetři nalezené faktory během předpočítávací fáze
if (ffact.handlePotentialFactor(zF,gen->countCurves()+1))
{
cout << "Factor found during precomputation: " << ffact.getLastFactor() << endl;
if (ffact.tryFinishSession(zN))
{
cout << "Full factorization found during precomputation." << endl;
ffact.savePrimesFromSession(args.outputFile,args.onePrimeFile);
exitCode = 1;
mpz_clear(zF);
goto end;
}
}
mpz_clear(zF);
delete[] PP;
gen->revert();
goto restart_bound;
}
// Pokud není strategie, skonči
if (strategy == csNone)
{
cout << "ERROR: No suitable compute strategy found." << endl;
exitCode = 1;
goto end;
}
// Spočti S = lcm(1,2,3...,B1) a jeho NAF rozvoj, pokud se B1 změnilo
if (lastB1 != args.curB1)
{
mpz_init(zS);
cout << "Recomputing coefficient..." << endl;
if (args.noLCM)
{
cout << "NOTE: Using bound B1 as a coefficient directly." << endl;
mpz_set_ui(zS,args.curB1);
}
else //lcmToN(zS,args.curB1);
mpz_primorial_ui(zS,args.curB1);
lastB1 = args.curB1;
S.initialize(zS,2);
mpz_clear(zS);
}
else cout << "NOTE: B1 hasn't changed." << endl;
cout << endl << "Trying to factor " << mpz_to_string(zN) << " with B1 = "<< args.curB1 << " using " << read_curves << " curves..." << endl << endl;
// Nastavit hodnoty do konfigurace
ax.windowSz = args.windowSize;
ax.nafLen = S.l;
ax.numCurves = read_curves;
ax.minus1 = (strategy == csTwisted);
ax.deviceId = args.whichDevice;
ax.cudaRunTime = 0;
runNum++;
infty.infinity(zN);
ax.initialize(zN);
// Proveď výpočet
if (strategy == csMixed)
{
cout << "NOTE: Using mixed compute strategy." << endl;
cudaStatus = computeMixed(ax,&infty,PP,S);
}
else
{
cout << "NOTE: Using single compute strategy." << endl;
cudaStatus = computeSingle(ax,&infty,PP,S);
}
if (cudaStatus != cudaSuccess)
{
cout << "ERROR: CUDA compute failed!" << endl;
exitCode = 1;
delete[] PP;
goto end;
}
cout << "Computation finished." << endl << endl;
cudaTimeCounter += pt::milliseconds((int64_t)ax.cudaRunTime);
// Inicializace pomocných proměnných
mpz_intz(zInvW,zX,zY);
// Spočti 2^(-W) mod N
mpz_ui_pow_ui(zInvW, 2, MAX_BITS);
mpz_invert(zInvW, zInvW, zN);
// Analyzuj výsledky
for (int i = 0; i < read_curves;++i)
{
try {
if (args.verbose) cout << "Curve #" << i+1 << ":\t";
PP[i].toAffine(zX,zY,zN,zInvW);
if (args.verbose)
{
cout << "No factor found." << endl;
cout << endl << "sP = (" << mpz_to_string(zX) << "," << mpz_to_string(zY) << ")" << endl;
}
}
catch (mpz_t zF)
{
bool ff = ffact.handlePotentialFactor(zF,i);
if (args.verbose)
{
if (ff) cout << "Factor found: " << ffact.getLastFactor() << endl;
else cout << "Error during conversion." << endl;
cout << endl << "------------" << endl;
}
mpz_clear(zF);
}
}
// Vyčisti proměnné
mpz_clrs(zInvW,zX,zY);
delete[] PP;
// Vypiš všechny nově nalezené faktory
ffact.printNewFoundFactors(runNum);
// Poděl číslo N všemi nalezenými prvočíselnými faktory
cout << endl;
fullFactorizationFound = ffact.tryFinishSession(zN);
args.exitOnFinish = args.exitOnFinish || fullFactorizationFound;
// Kontrola, zda máme restartovat bez zeptání a zvýšit B1
if (args.B1.size() > 1 && args.curB1 <= args.B1[2]-args.B1[1] && !fullFactorizationFound)
{
args.curB1 += args.B1[1];
cout << "NOTE: B1 has been automatically incremented to: " << args.curB1 << endl;
gen->revert();
goto restart_bound;
}
// Finální menu
while (!args.exitOnFinish)
{
cout << endl << "Type : " << endl;
cout << "'r' to restart with new configuration." << endl;
cout << "'p' to print all found prime factors so far." << endl;
cout << "'a [number]' to increase B1 by [number] and restart with the same curve file." << endl;
cout << "or 'q' to quit." << endl << endl;
cin >> c;
cout << endl;
if (c == 'q') break;
if (c == 'a')
{
unsigned int B1_inc;
cin >> B1_inc;
args.B1[0] += B1_inc;
validateArguments(args);
cout << "B1 has been incremented to: " << args.curB1 << endl;
gen->revert();
goto restart_bound;
}
else if (c == 'p')
{
cout << endl;
ffact.printPrimesFromSession();
cout << endl;
}
else if (c == 'r')
{
args.B1.clear();
args.curveGen.clear();
validateArguments(args);
gen->revert();
goto restart_bound;
}
}
// Ulož výstup a vypiš celkový čas běhu
ffact.savePrimesFromSession(args.outputFile,args.onePrimeFile);
cout << "Total GPU running time was: " << cudaTimeCounter << endl;
cudaTotalTime += cudaTimeCounter;
// Jsme-li v hladovém módu, chtěj další číslo k faktorizaci
if (args.greedy)
{
args.N = "";
runNum = 0;
cudaTimeCounter = pt::milliseconds(0);
cout << endl;
if (validateArguments(args) != 0) goto end;
mpz_init_set_str(zN,args.N.c_str(),10);
ffact.startNewSession(args.N);
gen->revert();
goto restart_bound;
}
end:
mpz_clear(zN);
string ts = formatTime(pt::second_clock::local_time());
stringstream summary;
summary << "Total prime factors found in all sessions: " << ffact.primesFoundInAllSessions() << endl;
summary << "Total GPU time of all sessions: " << cudaTotalTime << endl;
if (ffact.primesFoundInAllSessions() > 0)
summary << "Average time per factor: " << cudaTotalTime/ffact.primesFoundInAllSessions() << endl;
summary << "Total factorizations/sessions: ";
summary << boost::format("%d/%d (%.2f %%)") % ffact.totalFactorizationsInAllSessions() % ffact.sessionCount() % (100*ffact.totalSuccessRate()) << endl;
pt::ptime totalEnd = pt::microsec_clock::local_time();
summary << "Total time spent: " << totalEnd-totalStart << endl;
cout << endl << summary.str();
string timest = formatTime(pt::second_clock::local_time());
if (args.saveSummary)
{
ofstream f((boost::format("summary-%s.txt") % timest).str(),ofstream::out | ofstream::trunc);
f << summary.str();
f.close();
}
if (args.saveHistogram)
ffact.saveCurveHistogram((boost::format("histogram-%s.txt") % timest).str(),args.genStart);
return exitCode;
}