measure.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <stdlib.h>
  2. #include "randombytes.h"
  3. #include "cpucycles.h"
  4. #include "crypto_stream.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_stream_IMPLEMENTATION;
  14. const char *implementationversion = crypto_stream_VERSION;
  15. const char *sizenames[] = { "keybytes", "noncebytes", 0 };
  16. const long long sizes[] = { crypto_stream_KEYBYTES, crypto_stream_NONCEBYTES };
  17. #define MAXTEST_BYTES 4096
  18. #ifdef SUPERCOP
  19. #define MGAP 8192
  20. #else
  21. #define MGAP 8
  22. #endif
  23. static unsigned char *k;
  24. static unsigned char *n;
  25. static unsigned char *m;
  26. static unsigned char *c;
  27. void preallocate(void)
  28. {
  29. }
  30. void allocate(void)
  31. {
  32. k = alignedcalloc(crypto_stream_KEYBYTES);
  33. n = alignedcalloc(crypto_stream_NONCEBYTES);
  34. m = alignedcalloc(MAXTEST_BYTES);
  35. c = alignedcalloc(MAXTEST_BYTES);
  36. }
  37. #define TIMINGS 15
  38. static long long cycles[TIMINGS + 1];
  39. void measure(void)
  40. {
  41. int i;
  42. int loop;
  43. int mlen;
  44. for (loop = 0;loop < LOOPS;++loop) {
  45. for (mlen = 0;mlen <= MAXTEST_BYTES;mlen += 1 + mlen / MGAP) {
  46. randombytes(k,crypto_stream_KEYBYTES);
  47. randombytes(n,crypto_stream_NONCEBYTES);
  48. randombytes(m,mlen);
  49. randombytes(c,mlen);
  50. for (i = 0;i <= TIMINGS;++i) {
  51. cycles[i] = cpucycles();
  52. crypto_stream(c,mlen,n,k);
  53. }
  54. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  55. printentry(mlen,"cycles",cycles,TIMINGS);
  56. for (i = 0;i <= TIMINGS;++i) {
  57. cycles[i] = cpucycles();
  58. crypto_stream_xor(c,m,mlen,n,k);
  59. }
  60. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  61. printentry(mlen,"xor_cycles",cycles,TIMINGS);
  62. }
  63. }
  64. }