Browse Source

more tests and benchmarks

master
Mike Hamburg 9 years ago
parent
commit
18e7c31691
4 changed files with 66 additions and 18 deletions
  1. +3
    -0
      Makefile
  2. +1
    -1
      include/decaf.hxx
  3. +55
    -17
      test/bench_decaf.cxx
  4. +7
    -0
      test/test_decaf.cxx

+ 3
- 0
Makefile View File

@@ -231,6 +231,9 @@ test_decaf: build/test_decaf
bench_decaf: build/bench_decaf bench_decaf: build/bench_decaf
build/bench_decaf build/bench_decaf
microbench_decaf: build/bench_decaf
build/bench_decaf --micro


clean: clean:
rm -fr build doc $(BATNAME) rm -fr build doc $(BATNAME)

+ 1
- 1
include/decaf.hxx View File

@@ -311,7 +311,7 @@ public:
*/ */
static inline Point from_hash ( const std::string &s ) NOEXCEPT { static inline Point from_hash ( const std::string &s ) NOEXCEPT {
std::string t = s; 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); 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; Point p; decaf_448_point_from_hash_uniform(p.p,GET_DATA(t)); return p;
} }


+ 55
- 17
test/bench_decaf.cxx View File

@@ -44,8 +44,27 @@ static inline uint64_t rdtsc(void) {
} }
#endif #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<sizeof(small)/sizeof(*small)-1 && x && x < 1; di++) {
x *= 1000.0;
}
printf("%6.2f%s%s%s", x, spacer, small[di], unit);
} else {
unsigned di=0;
for (di=0; di<sizeof(big)/sizeof(*big)-1 && x && x >= 1000; di++) {
x /= 1000.0;
}
printf("%6.2f%s%s%s", x, spacer, big[di], unit);
}
}

class Benchmark { class Benchmark {
static const int NTESTS = 1000; static const int NTESTS = 1000;
static double totalCy, totalS;
public: public:
int i, ntests; int i, ntests;
double begin; double begin;
@@ -60,25 +79,35 @@ public:
tsc_begin = rdtsc(); tsc_begin = rdtsc();
} }
~Benchmark() { ~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<sizeof(small)/sizeof(*small)-1 && t && t < 1; di++) {
t *= 1000.0;
}
unsigned bi=0;
for (bi=0; bi<sizeof(big)/sizeof(*big)-1 && tsc && tsc >= 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"); printf("\n");
} }
inline bool iter() { return i++ < ntests; } 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) { int main(int argc, char **argv) {
bool micro = false; bool micro = false;
if (argc >= 2 && !strcmp(argv[1], "--micro")) 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}; unsigned char umessage[] = {1,2,3,4,5};
size_t lmessage = sizeof(umessage); size_t lmessage = sizeof(umessage);
printf("\n");


if (micro) { if (micro) {
Precomputed pBase; Precomputed pBase;
Point p,q; Point p,q;
Scalar s,t; 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 add", 1000); b.iter(); ) { s+t; }
for (Benchmark b("Scalar times", 100); 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("Scalar inv", 10); b.iter(); ) { s.inverse(); }
for (Benchmark b("Point add", 100); b.iter(); ) { p + q; } 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 double", 100); b.iter(); ) { p.double_in_place(); }
for (Benchmark b("Point scalarmul"); b.iter(); ) { p * s; } 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 double scalarmul"); b.iter(); ) { Point::double_scalarmul(p,s,q,t); }
for (Benchmark b("Point precmp scalarmul"); b.iter(); ) { pBase * s; } 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(); ) { for (Benchmark b("Keygen"); b.iter(); ) {
@@ -136,9 +171,12 @@ int main(int argc, char **argv) {
for (Benchmark b("Verify"); b.iter(); ) { for (Benchmark b("Verify"); b.iter(); ) {
decaf_bool_t ret = decaf_448_verify(sig1,p1,umessage,lmessage); decaf_bool_t ret = decaf_448_verify(sig1,p1,umessage,lmessage);
umessage[0]++; umessage[0]++;
umessage[1]^=umessage[0];
ignore_result(ret); ignore_result(ret);
} }
printf("\n");
Benchmark::calib();
printf("\n"); printf("\n");
return 0; return 0;


+ 7
- 0
test/test_decaf.cxx View File

@@ -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,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,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,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)???
} }
} }




Loading…
Cancel
Save