You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

146 lines
4.1 KiB

  1. /**
  2. * @file test_decaf.cxx
  3. * @author Mike Hamburg
  4. *
  5. * @copyright
  6. * Copyright (c) 2015 Cryptography Research, Inc. \n
  7. * Released under the MIT License. See LICENSE.txt for license information.
  8. *
  9. * @brief C++ benchmarks, because that's easier.
  10. */
  11. #include "decaf.hxx"
  12. #include "shake.h"
  13. #include "decaf_crypto.h"
  14. #include <stdio.h>
  15. #include <sys/time.h>
  16. #include <assert.h>
  17. #include <stdint.h>
  18. typedef decaf::decaf<448>::Scalar Scalar;
  19. typedef decaf::decaf<448>::Point Point;
  20. typedef decaf::decaf<448>::Precomputed Precomputed;
  21. static __inline__ void __attribute__((unused)) ignore_result ( int result ) { (void)result; }
  22. static double now(void) {
  23. struct timeval tv;
  24. gettimeofday(&tv, NULL);
  25. return tv.tv_sec + tv.tv_usec/1000000.0;
  26. }
  27. // RDTSC from the chacha code
  28. #ifndef __has_builtin
  29. #define __has_builtin(X) 0
  30. #endif
  31. #if defined(__clang__) && __has_builtin(__builtin_readcyclecounter)
  32. #define rdtsc __builtin_readcyclecounter
  33. #else
  34. static inline uint64_t rdtsc(void) {
  35. u_int64_t out = 0;
  36. # if (defined(__i386__) || defined(__x86_64__))
  37. __asm__ __volatile__ ("rdtsc" : "=A"(out));
  38. # endif
  39. return out;
  40. }
  41. #endif
  42. class Benchmark {
  43. static const int NTESTS = 1000;
  44. public:
  45. int i, ntests;
  46. double begin;
  47. uint64_t tsc_begin;
  48. Benchmark(const char *s, double factor = 1) {
  49. printf("%s:", s);
  50. if (strlen(s) < 25) printf("%*s",int(25-strlen(s)),"");
  51. fflush(stdout);
  52. i = 0;
  53. ntests = NTESTS * factor;
  54. begin = now();
  55. tsc_begin = rdtsc();
  56. }
  57. ~Benchmark() {
  58. double tsc = (rdtsc() - tsc_begin) * 1.0 / ntests;
  59. double t = (now() - begin)/ntests;
  60. const char *small[] = {" ","m","ยต","n","p"};
  61. const char *big[] = {" ","k","M","G","T"};
  62. unsigned di=0;
  63. for (di=0; di<sizeof(small)/sizeof(*small)-1 && t && t < 1; di++) {
  64. t *= 1000.0;
  65. }
  66. unsigned bi=0;
  67. for (bi=0; bi<sizeof(big)/sizeof(*big)-1 && tsc && tsc >= 1000; bi++) {
  68. tsc /= 1000.0;
  69. }
  70. printf("%7.2f %ss", t, small[di]);
  71. if (tsc) printf(" %7.2f %scy", tsc, big[bi]);
  72. printf("\n");
  73. }
  74. inline bool iter() { return i++ < ntests; }
  75. };
  76. int main(int argc, char **argv) {
  77. bool micro = false;
  78. if (argc >= 2 && !strcmp(argv[1], "--micro"))
  79. micro = true;
  80. decaf_448_public_key_t p1,p2;
  81. decaf_448_private_key_t s1,s2;
  82. decaf_448_symmetric_key_t r1,r2;
  83. decaf_448_signature_t sig1;
  84. unsigned char ss[32];
  85. memset(r1,1,sizeof(r1));
  86. memset(r2,2,sizeof(r2));
  87. unsigned char umessage[] = {1,2,3,4,5};
  88. size_t lmessage = sizeof(umessage);
  89. if (micro) {
  90. Precomputed pBase;
  91. Point p,q;
  92. Scalar s,t;
  93. printf("Micro:\n");
  94. for (Benchmark b("Scalar add", 1000); b.iter(); ) { s+t; }
  95. for (Benchmark b("Scalar times", 100); b.iter(); ) { s*t; }
  96. for (Benchmark b("Scalar inv", 10); b.iter(); ) { s.inverse(); }
  97. for (Benchmark b("Point add", 100); b.iter(); ) { p + q; }
  98. for (Benchmark b("Point double", 100); b.iter(); ) { p.double_in_place(); }
  99. for (Benchmark b("Point scalarmul"); b.iter(); ) { p * s; }
  100. for (Benchmark b("Point double scalarmul"); b.iter(); ) { Point::double_scalarmul(p,s,q,t); }
  101. for (Benchmark b("Point precmp scalarmul"); b.iter(); ) { pBase * s; }
  102. /* TODO: scalarmul for verif */
  103. printf("\nMacro:\n");
  104. }
  105. for (Benchmark b("Keygen"); b.iter(); ) {
  106. decaf_448_derive_private_key(s1,r1);
  107. }
  108. decaf_448_private_to_public(p1,s1);
  109. decaf_448_derive_private_key(s2,r2);
  110. decaf_448_private_to_public(p2,s2);
  111. for (Benchmark b("Shared secret"); b.iter(); ) {
  112. decaf_bool_t ret = decaf_448_shared_secret(ss,sizeof(ss),s1,p2);
  113. ignore_result(ret);
  114. assert(ret);
  115. }
  116. for (Benchmark b("Sign"); b.iter(); ) {
  117. decaf_448_sign(sig1,s1,umessage,lmessage);
  118. }
  119. for (Benchmark b("Verify"); b.iter(); ) {
  120. decaf_bool_t ret = decaf_448_verify(sig1,p1,umessage,lmessage);
  121. umessage[0]++;
  122. ignore_result(ret);
  123. }
  124. printf("\n");
  125. return 0;
  126. }