measure.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <stdlib.h>
  2. #include "randombytes.h"
  3. #include "cpucycles.h"
  4. #include "crypto_hash.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_hash_IMPLEMENTATION;
  14. const char *implementationversion = crypto_hash_VERSION;
  15. const char *sizenames[] = { "outputbytes", 0 };
  16. const long long sizes[] = { crypto_hash_BYTES };
  17. #define MAXTEST_BYTES 4096
  18. #ifdef SUPERCOP
  19. #define MGAP 8192
  20. #else
  21. #define MGAP 8
  22. #endif
  23. static unsigned char *h;
  24. static unsigned char *m;
  25. void preallocate(void)
  26. {
  27. }
  28. void allocate(void)
  29. {
  30. h = alignedcalloc(crypto_hash_BYTES);
  31. m = alignedcalloc(MAXTEST_BYTES);
  32. }
  33. #define TIMINGS 15
  34. static long long cycles[TIMINGS + 1];
  35. static void printcycles(long long mlen)
  36. {
  37. int i;
  38. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  39. printentry(mlen,"cycles",cycles,TIMINGS);
  40. }
  41. void measure(void)
  42. {
  43. int i;
  44. int loop;
  45. int mlen;
  46. for (loop = 0;loop < LOOPS;++loop) {
  47. for (mlen = 0;mlen <= MAXTEST_BYTES;mlen += 1 + mlen / MGAP) {
  48. randombytes(m,mlen);
  49. for (i = 0;i <= TIMINGS;++i) {
  50. cycles[i] = cpucycles();
  51. crypto_hash(h,m,mlen);
  52. }
  53. printcycles(mlen);
  54. }
  55. }
  56. }