measure.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <stdlib.h>
  2. #include "randombytes.h"
  3. #include "cpucycles.h"
  4. #include "crypto_sign.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_sign_IMPLEMENTATION;
  14. const char *implementationversion = crypto_sign_VERSION;
  15. const char *sizenames[] = { "outputbytes", "publickeybytes", "secretkeybytes", 0 };
  16. const long long sizes[] = { crypto_sign_BYTES, crypto_sign_PUBLICKEYBYTES, crypto_sign_SECRETKEYBYTES };
  17. #define MAXTEST_BYTES 100000
  18. static unsigned char *pk;
  19. static unsigned char *sk;
  20. static unsigned char *m; unsigned long long mlen;
  21. static unsigned char *sm; unsigned long long smlen;
  22. static unsigned char *t; unsigned long long tlen;
  23. void preallocate(void)
  24. {
  25. #ifdef RAND_R_PRNG_NOT_SEEDED
  26. RAND_status();
  27. #endif
  28. }
  29. void allocate(void)
  30. {
  31. pk = alignedcalloc(crypto_sign_PUBLICKEYBYTES);
  32. sk = alignedcalloc(crypto_sign_SECRETKEYBYTES);
  33. m = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
  34. sm = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
  35. t = alignedcalloc(MAXTEST_BYTES + crypto_sign_BYTES);
  36. }
  37. #define TIMINGS 31
  38. static long long cycles[TIMINGS + 1];
  39. static long long bytes[TIMINGS + 1];
  40. void measure(void)
  41. {
  42. int i;
  43. int loop;
  44. for (loop = 0;loop < LOOPS;++loop) {
  45. for (i = 0;i <= TIMINGS;++i) {
  46. cycles[i] = cpucycles();
  47. crypto_sign_keypair(pk,sk);
  48. }
  49. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  50. printentry(-1,"keypair_cycles",cycles,TIMINGS);
  51. for (mlen = 0;mlen <= MAXTEST_BYTES;mlen += 1 + mlen / 4) {
  52. randombytes(m,mlen);
  53. for (i = 0;i <= TIMINGS;++i) {
  54. cycles[i] = cpucycles();
  55. bytes[i] = crypto_sign(sm,&smlen,m,mlen,sk);
  56. if (bytes[i] == 0) bytes[i] = smlen;
  57. }
  58. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  59. printentry(mlen,"cycles",cycles,TIMINGS);
  60. printentry(mlen,"bytes",bytes,TIMINGS);
  61. for (i = 0;i <= TIMINGS;++i) {
  62. cycles[i] = cpucycles();
  63. bytes[i] = crypto_sign_open(t,&tlen,sm,smlen,pk);
  64. if (bytes[i] == 0) bytes[i] = tlen;
  65. }
  66. for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
  67. printentry(mlen,"open_cycles",cycles,TIMINGS);
  68. printentry(mlen,"open_bytes",bytes,TIMINGS);
  69. }
  70. }
  71. }