-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
48 lines (41 loc) · 6.4 KB
/
Copy pathmain.cpp
File metadata and controls
48 lines (41 loc) · 6.4 KB
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
#include <iostream>
#include <chrono>
#include <vector>
// #define BI_BLEN (4096 * 10)
#define BI_BLEN (4096)
#include "bigint.h"
struct BenchResult {
const char* name;
double ms;
bool correct;
};
template<typename F>
static BenchResult bench(const char* name, F f,
const bui& x, const bui& e, const bui& m, const bui& expected) {
auto start = std::chrono::high_resolution_clock::now();
bui r = f(x, e, m);
auto end = std::chrono::high_resolution_clock::now();
double ms = std::chrono::duration<double, std::nano>(end - start).count() / 1E6;
bool ok = cmp(expected, r) == 0;
printf("%-25s %8.2f ms %s\n", name, ms, ok ? "OK" : "FAIL");
return { name, ms, ok };
}
int main() {
bui a = bui_from_dec("294659437513360555428281919645599968995190371519631966686213755511358816684616070985469088551461374152030885828765017129016962685800740906840522615076203253884649934197506467078388422736998803537879188082679421665973723723959084563094048284484266108811003733184102200738443069911309302835267342148199395076052333200220524194931021449485067438780873633632151045586062075740774960516156595313590497273810821141109062692766101984352621246488910095571343522441162716344963595533174543873913403044058158913840553493440552129662864284224164268429343139208723659647761679280677812855663749229408742010504807036763445673741777099942142811120623904412073388473227703482985294850599869827885641101563498019805688804610384450871508563041035387370669882088160956854828224604564503733080710683027038167330218942345194372849468360417203967253811096837931486123847397711478107615319989042646155699198716713537033967140160335242140711972514564695990390939940379124814243587555425524838694044940009872756002699659690760152701368709140261799108033664373127246411256502226320284141841474773281107790424419584826555066536966897753081203881265245567079706125297845698237328128364119879163376768582175941712219304621861958167949859923196500995637267456673");
bui b = bui_from_dec("601721046772866117824719219375142775027677048423567159547949782234920264316415898330724565503384653633646172244553780243741194339206460035940807707235365457167388392383622113514799342933251460300679158853262919849650156669551386807788176748428530124279292765213363916991741343803050891903715101676384699923936991367141088323528982388440504830321952858952892212309505633748018780105059413407315555094222756392741844603319040425486415349515964580417230846648800923004088910513146500145380180768714688309572905221473510632820510496865180140627382183270744028771466767701824976735901655784461514168938655122638059355489425741184315096503278472585781406922901072662886846016368015896338856800253608785754084335685641135538720339721500100121242429827146411482348363548470141814080156412272458194464078069627423679225791412231091332192946545591837387391901595653299327455223052676777765652686826330991337163369860471846046534921512890760186124876625214504674883347838870546857311158816350948185053813999740436300132562623034890681824936066105560215552370736526202585961625531858563076293421277820808010388319878151743519437818182162712478551006690178945449637109178842321866477772598879923953236918230081351141654315603667742626406896007453");
bui m = bui_from_dec("544116353078657303842241146677112117272548419037795752877900434420206365262643291145396097955996278823130969759452826674933337456697425752367820321686120324394553789662425555698386323841900736815623312263543093090663047768332101182426782500412013884327565583284444926934993238399236969554011999063813973564955685672648479918498505114683072998221690054289324120339026270616318549719716707908631375187806161853111643974234092750444679919842148654824450320959864815649160513921090604798829866204651826114410206283417486282768731039452005946819511317689533952873444981922101788229217893133574425262048841768734326227491807589678078392673073130487664891047235432852660728882572095134711406034326596242974729274167113327158284852531288394073840341809927815621462859993829627666831840656966878668797035576305475761518418837122963020020774092750810636337857870629676782438326641055906844541892521172206024665163055571471975518676750555119900346075352191595346123371237613161816447264710796188490151153215880866397244453602850731208953838416330939837426478178512476916384398496648351611964799439556565292158535209266052953734798951000249917880731970940363319204908824776614614062436827628546035641478505695769076861921478482166802971575011969");
bui y = bui_from_dec("213685042107832718671989886535904233444560358642797571787487044544132747231952091962996428301468977648035945573848518943555965970995708734619387508587875678568682376562859551662440505326826794541763132198070400450460232093111940983507425837312175137030884939786271066340633051877196994826739170460404265510514787279922361835450704395019455606927263547184020169175978925158091407516863908987474342927306300458278559996027150427459416235571871366380055625084561367337783386368227401977817724730315463854811121136276346809426214199303113897981668742900573053356567687618506168186672132864015776990106634852202342885075773368764385206248058017461313045191375024448687894090858074176679394951491679505559079326344837169538432247324313781936855915334734181644086818840991316631195981066792033105274932766907154353348464777660540013068811282727703591505436593780734013282500187616218883643719069361669003070907155619844585176353336150377552517404005672937372429465899763581436669471426534506786221720582198147833285207831253383374178191831837274672190282596050340764625998613274576472580118425666051384292505700021503647822802594241111191500366238857345912210262613727563547132968146863626762031330811012825270113187884266903646711437770103");
printf("expected : %s\n", bui_to_dec(y).c_str());
printf("mont_cios3 : %s\n", bui_to_dec(pow_mod_mont_cios3(a, b, m)).c_str());
printf("mont_cios2 : %s\n", bui_to_dec(pow_mod_mont_cios2(a, b, m)).c_str());
printf("mr_pow_mod : %s\n", bui_to_dec(mr_pow_mod(a, b, m)).c_str());
printf("mont_window: %s\n", bui_to_dec(pow_mod_mont_window(a, b, m)).c_str());
printf("window : %s\n", bui_to_dec(pow_mod_window(a, b, m)).c_str());
printf("powmod : %s\n", bui_to_dec(pow_mod(a, b, m)).c_str());
printf("%-25s %10s %s\n", "Method", "Time", "Status");
printf("------------------------------------------------\n");
bench("mont_cios3 (binary)", pow_mod_mont_cios3, a, b, m, y);
bench("mont_window", pow_mod_mont_window, a, b, m, y);
bench("window (schoolbook)", pow_mod_window, a, b, m, y);
return 0;
}