diff --git a/Makefile b/Makefile index 5561632..f68c23c 100644 --- a/Makefile +++ b/Makefile @@ -231,6 +231,9 @@ test_decaf: build/test_decaf bench_decaf: build/bench_decaf build/bench_decaf + +microbench_decaf: build/bench_decaf + build/bench_decaf --micro clean: rm -fr build doc $(BATNAME) diff --git a/include/decaf.hxx b/include/decaf.hxx index c3e0f17..395adfc 100644 --- a/include/decaf.hxx +++ b/include/decaf.hxx @@ -311,7 +311,7 @@ public: */ static inline Point from_hash ( const std::string &s ) NOEXCEPT { std::string t = s; - if (t.size() < DECAF_448_SER_BYTES) return from_hash_nonuniform(s); + if (t.size() <= DECAF_448_SER_BYTES) return from_hash_nonuniform(s); if (t.size() < 2*DECAF_448_SER_BYTES) t.insert(t.size(),2*DECAF_448_SER_BYTES-t.size(),0); Point p; decaf_448_point_from_hash_uniform(p.p,GET_DATA(t)); return p; } diff --git a/test/bench_decaf.cxx b/test/bench_decaf.cxx index ef383c5..d9595eb 100644 --- a/test/bench_decaf.cxx +++ b/test/bench_decaf.cxx @@ -44,8 +44,27 @@ static inline uint64_t rdtsc(void) { } #endif +static void printSI(double x, const char *unit, const char *spacer = " ") { + const char *small[] = {" ","m","µ","n","p"}; + const char *big[] = {" ","k","M","G","T"}; + if (x < 1) { + unsigned di=0; + for (di=0; di= 1000; di++) { + x /= 1000.0; + } + printf("%6.2f%s%s%s", x, spacer, big[di], unit); + } +} + class Benchmark { static const int NTESTS = 1000; + static double totalCy, totalS; public: int i, ntests; double begin; @@ -60,25 +79,35 @@ public: tsc_begin = rdtsc(); } ~Benchmark() { - double tsc = (rdtsc() - tsc_begin) * 1.0 / ntests; - double t = (now() - begin)/ntests; - const char *small[] = {" ","m","µ","n","p"}; - const char *big[] = {" ","k","M","G","T"}; - unsigned di=0; - for (di=0; di= 1000; bi++) { - tsc /= 1000.0; - } - printf("%7.2f %ss", t, small[di]); - if (tsc) printf(" %7.2f %scy", tsc, big[bi]); + double tsc = (rdtsc() - tsc_begin) * 1.0; + double t = (now() - begin); + + totalCy += tsc; + totalS += t; + + t /= ntests; + tsc /= ntests; + + printSI(t,"s"); + printf(" "); + printSI(1/t,"/s"); + if (tsc) { printf(" "); printSI(tsc, "cy"); } printf("\n"); } inline bool iter() { return i++ < ntests; } + static void calib() { + if (totalS && totalCy) { + const char *s = "Cycle calibration"; + printf("%s:", s); + if (strlen(s) < 25) printf("%*s",int(25-strlen(s)),""); + printSI(totalCy / totalS, "Hz"); + printf("\n"); + } + } }; +double Benchmark::totalCy = 0, Benchmark::totalS = 0; + int main(int argc, char **argv) { bool micro = false; if (argc >= 2 && !strcmp(argv[1], "--micro")) @@ -96,23 +125,29 @@ int main(int argc, char **argv) { unsigned char umessage[] = {1,2,3,4,5}; size_t lmessage = sizeof(umessage); + printf("\n"); if (micro) { Precomputed pBase; Point p,q; Scalar s,t; + std::string ep; - printf("Micro:\n"); + printf("Micro-benchmarks:\n"); for (Benchmark b("Scalar add", 1000); b.iter(); ) { s+t; } for (Benchmark b("Scalar times", 100); b.iter(); ) { s*t; } for (Benchmark b("Scalar inv", 10); b.iter(); ) { s.inverse(); } for (Benchmark b("Point add", 100); b.iter(); ) { p + q; } for (Benchmark b("Point double", 100); b.iter(); ) { p.double_in_place(); } for (Benchmark b("Point scalarmul"); b.iter(); ) { p * s; } + for (Benchmark b("Point encode"); b.iter(); ) { ep = std::string(p); } + for (Benchmark b("Point decode"); b.iter(); ) { p = Point(ep); } + for (Benchmark b("Point hash nonuniform"); b.iter(); ) { Point::from_hash(ep); } + for (Benchmark b("Point hash uniform"); b.iter(); ) { Point::from_hash(ep+ep); } for (Benchmark b("Point double scalarmul"); b.iter(); ) { Point::double_scalarmul(p,s,q,t); } for (Benchmark b("Point precmp scalarmul"); b.iter(); ) { pBase * s; } - /* TODO: scalarmul for verif */ - printf("\nMacro:\n"); + /* TODO: scalarmul for verif, etc */ + printf("\nMacro-benchmarks:\n"); } for (Benchmark b("Keygen"); b.iter(); ) { @@ -136,9 +171,12 @@ int main(int argc, char **argv) { for (Benchmark b("Verify"); b.iter(); ) { decaf_bool_t ret = decaf_448_verify(sig1,p1,umessage,lmessage); umessage[0]++; + umessage[1]^=umessage[0]; ignore_result(ret); } + printf("\n"); + Benchmark::calib(); printf("\n"); return 0; diff --git a/test/test_decaf.cxx b/test/test_decaf.cxx index 1daa176..fcdd43c 100644 --- a/test/test_decaf.cxx +++ b/test/test_decaf.cxx @@ -185,6 +185,13 @@ static void test_ec() { point_check(test,p,q,r,x,y,x*p+y*q,Point::double_scalarmul(x,p,y,q),"ds mul"); point_check(test,base,q,r,x,y,x*base+y*q,q.non_secret_combo_with_base(y,x),"ds vt mul"); point_check(test,p,q,r,x,0,Precomputed(p)*x,p*x,"precomp mul"); + point_check(test,p,q,r,0,0,r, + Point::from_hash_nonuniform(buffer) + +Point::from_hash_nonuniform(&buffer[DECAF_448_SCALAR_BYTES]), + "unih = hash+add" + ); + + // TODO: test hash_u(x+x) == hash_nu(x) + hash_nu(x)??? } }