measure.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <stdlib.h>
  2. #include "randombytes.h"
  3. #include "cpucycles.h"
  4. #include "crypto_box.h"
  5. extern void printentry(long long,const char *,long long *,long long);
  6. extern unsigned char *alignedcalloc(unsigned long long);
  7. extern const char *primitiveimplementation;
  8. extern const char *implementationversion;
  9. extern const char *sizenames[];
  10. extern const long long sizes[];
  11. extern void allocate(void);
  12. extern void measure(void);
  13. const char *primitiveimplementation = crypto_box_IMPLEMENTATION;
  14. const char *implementationversion = crypto_box_VERSION;
  15. const char *sizenames[] = { "publickeybytes", "secretkeybytes", "beforenmbytes", "noncebytes", "zerobytes", "boxzerobytes", 0 };
  16. const long long sizes[] = { crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES, crypto_box_BEFORENMBYTES, crypto_box_NONCEBYTES, crypto_box_ZEROBYTES, crypto_box_BOXZEROBYTES };
  17. #define MAXTEST_BYTES 4096
  18. static unsigned char *ska;
  19. static unsigned char *pka;
  20. static unsigned char *skb;
  21. static unsigned char *pkb;
  22. static unsigned char *n;
  23. static unsigned char *m;
  24. static unsigned char *c;
  25. static unsigned char *sa;
  26. static unsigned char *sb;
  27. void preallocate(void)
  28. {
  29. }
  30. void allocate(void)
  31. {
  32. ska = alignedcalloc(crypto_box_SECRETKEYBYTES);
  33. pka = alignedcalloc(crypto_box_PUBLICKEYBYTES);
  34. skb = alignedcalloc(crypto_box_SECRETKEYBYTES);
  35. pkb = alignedcalloc(crypto_box_PUBLICKEYBYTES);
  36. n = alignedcalloc(crypto_box_NONCEBYTES);
  37. m = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
  38. c = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
  39. sa = alignedcalloc(crypto_box_BEFORENMBYTES);
  40. sb = alignedcalloc(crypto_box_BEFORENMBYTES);
  41. }
  42. #define TIMINGS 15
  43. static long long cycles[TIMINGS + 1];
  44. void measure(void)
  45. {
  46. int i;
  47. int loop;
  48. int mlen;
  49. for (loop = 0;loop < LOOPS;++loop) {
  50. for (i = 0;i <= TIMINGS;++i) {
  51. cycles[i] = cpucycles();
  52. crypto_box_keypair(pka,ska);
  53. }
  54. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  55. printentry(-1,"keypair_cycles",cycles,TIMINGS);
  56. for (i = 0;i <= TIMINGS;++i) {
  57. cycles[i] = cpucycles();
  58. crypto_box_keypair(pkb,skb);
  59. }
  60. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  61. printentry(-1,"keypair_cycles",cycles,TIMINGS);
  62. for (i = 0;i <= TIMINGS;++i) {
  63. cycles[i] = cpucycles();
  64. crypto_box_beforenm(sa,pkb,ska);
  65. }
  66. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  67. printentry(-1,"beforenm_cycles",cycles,TIMINGS);
  68. for (i = 0;i <= TIMINGS;++i) {
  69. cycles[i] = cpucycles();
  70. crypto_box_beforenm(sb,pka,skb);
  71. }
  72. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  73. printentry(-1,"beforenm_cycles",cycles,TIMINGS);
  74. for (mlen = 0;mlen <= MAXTEST_BYTES;mlen += 1 + mlen / 8) {
  75. randombytes(n,crypto_box_NONCEBYTES);
  76. randombytes(m + crypto_box_ZEROBYTES,mlen);
  77. randombytes(c,mlen + crypto_box_ZEROBYTES);
  78. for (i = 0;i <= TIMINGS;++i) {
  79. cycles[i] = cpucycles();
  80. crypto_box(c,m,mlen + crypto_box_ZEROBYTES,n,pka,skb);
  81. }
  82. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  83. printentry(mlen,"cycles",cycles,TIMINGS);
  84. for (i = 0;i <= TIMINGS;++i) {
  85. cycles[i] = cpucycles();
  86. crypto_box_open(m,c,mlen + crypto_box_ZEROBYTES,n,pkb,ska);
  87. }
  88. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  89. printentry(mlen,"open_cycles",cycles,TIMINGS);
  90. ++c[crypto_box_ZEROBYTES];
  91. for (i = 0;i <= TIMINGS;++i) {
  92. cycles[i] = cpucycles();
  93. crypto_box_open(m,c,mlen + crypto_box_ZEROBYTES,n,pkb,ska);
  94. }
  95. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  96. printentry(mlen,"forgery_open_cycles",cycles,TIMINGS);
  97. for (i = 0;i <= TIMINGS;++i) {
  98. cycles[i] = cpucycles();
  99. crypto_box_afternm(c,m,mlen + crypto_box_ZEROBYTES,n,sb);
  100. }
  101. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  102. printentry(mlen,"afternm_cycles",cycles,TIMINGS);
  103. for (i = 0;i <= TIMINGS;++i) {
  104. cycles[i] = cpucycles();
  105. crypto_box_open_afternm(m,c,mlen + crypto_box_ZEROBYTES,n,sa);
  106. }
  107. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  108. printentry(mlen,"open_afternm_cycles",cycles,TIMINGS);
  109. ++c[crypto_box_ZEROBYTES];
  110. for (i = 0;i <= TIMINGS;++i) {
  111. cycles[i] = cpucycles();
  112. crypto_box_open_afternm(m,c,mlen + crypto_box_ZEROBYTES,n,sa);
  113. }
  114. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  115. printentry(mlen,"forgery_open_afternm_cycles",cycles,TIMINGS);
  116. }
  117. }
  118. }