measure.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <stdlib.h>
  2. #include "randombytes.h"
  3. #include "cpucycles.h"
  4. #include "crypto_scalarmult.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_scalarmult_IMPLEMENTATION;
  14. const char *implementationversion = crypto_scalarmult_VERSION;
  15. const char *sizenames[] = { "outputbytes", "scalarbytes", 0 };
  16. const long long sizes[] = { crypto_scalarmult_BYTES, crypto_scalarmult_SCALARBYTES };
  17. static unsigned char *m;
  18. static unsigned char *n;
  19. static unsigned char *p;
  20. static unsigned char *q;
  21. void preallocate(void)
  22. {
  23. }
  24. void allocate(void)
  25. {
  26. m = alignedcalloc(crypto_scalarmult_SCALARBYTES);
  27. n = alignedcalloc(crypto_scalarmult_SCALARBYTES);
  28. p = alignedcalloc(crypto_scalarmult_BYTES);
  29. q = alignedcalloc(crypto_scalarmult_BYTES);
  30. }
  31. #define TIMINGS 63
  32. static long long cycles[TIMINGS + 1];
  33. void measure(void)
  34. {
  35. int i;
  36. int loop;
  37. for (loop = 0;loop < LOOPS;++loop) {
  38. randombytes(m,crypto_scalarmult_SCALARBYTES);
  39. randombytes(n,crypto_scalarmult_SCALARBYTES);
  40. for (i = 0;i <= TIMINGS;++i) {
  41. cycles[i] = cpucycles();
  42. crypto_scalarmult_base(p,m);
  43. }
  44. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  45. printentry(-1,"base_cycles",cycles,TIMINGS);
  46. for (i = 0;i <= TIMINGS;++i) {
  47. cycles[i] = cpucycles();
  48. crypto_scalarmult(q,n,p);
  49. }
  50. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  51. printentry(-1,"cycles",cycles,TIMINGS);
  52. }
  53. }