toxcore/testing/crypto_speed_test.c

138 lines
3.2 KiB
C
Raw Normal View History

2013-08-11 01:06:51 +08:00
// Hi-resolution timer
#ifdef WIN32
2013-08-12 02:17:02 +08:00
#ifndef WINVER
//Windows XP
#define WINVER 0x0501
#endif
2013-08-11 01:06:51 +08:00
#include <windows.h>
double get_time()
{
LARGE_INTEGER t, f;
QueryPerformanceCounter(&t);
QueryPerformanceFrequency(&f);
2013-08-17 01:11:09 +08:00
return (double)t.QuadPart / (double)f.QuadPart;
2013-08-11 01:06:51 +08:00
}
#else
#include <sys/time.h>
#include <sys/resource.h>
double get_time()
{
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
2013-08-17 01:11:09 +08:00
return t.tv_sec + t.tv_usec * 1e-6;
2013-08-11 01:06:51 +08:00
}
#endif
#include "../core/net_crypto.h"
#include <stdlib.h>
#include <time.h>
void rand_bytes(uint8_t *b, size_t blen)
{
size_t i;
2013-08-17 01:11:09 +08:00
for (i = 0; i < blen; i++) {
2013-08-11 01:06:51 +08:00
b[i] = rand();
}
}
2013-08-17 01:11:09 +08:00
int main(int argc, char *argv[])
2013-08-11 01:06:51 +08:00
{
const int numtrials = 10000;
2013-08-17 01:11:09 +08:00
2013-08-11 01:06:51 +08:00
unsigned char pk1[crypto_box_PUBLICKEYBYTES];
unsigned char sk1[crypto_box_SECRETKEYBYTES];
unsigned char pk2[crypto_box_PUBLICKEYBYTES];
unsigned char sk2[crypto_box_SECRETKEYBYTES];
unsigned char k1[crypto_box_BEFORENMBYTES];
unsigned char k2[crypto_box_BEFORENMBYTES];
2013-08-17 01:11:09 +08:00
unsigned char n[crypto_box_NONCEBYTES];
2013-08-11 01:06:51 +08:00
unsigned char m[500];
unsigned char c[sizeof(m) + ENCRYPTION_PADDING];
unsigned char k[crypto_box_BEFORENMBYTES];
int trialno;
double starttime;
double endtime;
double slow_time;
double fast_time;
double keygen_time;
double precompute_time;
// Pregenerate
crypto_box_keypair(pk1, sk1);
crypto_box_keypair(pk2, sk2);
encrypt_precompute(pk1, sk2, k1);
encrypt_precompute(pk2, sk1, k2);
rand_bytes(m, sizeof(m));
rand_bytes(n, sizeof(n));
printf("starting slow...\n");
starttime = get_time();
2013-08-17 01:11:09 +08:00
for (trialno = 0; trialno < numtrials; trialno++) {
2013-08-11 01:06:51 +08:00
encrypt_data(pk1, sk2, n, m, sizeof(m), c);
decrypt_data(pk2, sk1, n, c, sizeof(c), m);
}
2013-08-17 01:11:09 +08:00
2013-08-11 01:06:51 +08:00
endtime = get_time();
2013-08-17 01:11:09 +08:00
slow_time = endtime - starttime;
2013-08-11 01:06:51 +08:00
printf("starting fast...\n");
starttime = get_time();
2013-08-17 01:11:09 +08:00
for (trialno = 0; trialno < numtrials; trialno++) {
2013-08-11 01:06:51 +08:00
encrypt_data_fast(k1, n, m, sizeof(m), c);
decrypt_data_fast(k2, n, c, sizeof(c), m);
}
2013-08-17 01:11:09 +08:00
2013-08-11 01:06:51 +08:00
endtime = get_time();
2013-08-17 01:11:09 +08:00
fast_time = endtime - starttime;
2013-08-11 01:06:51 +08:00
printf("starting keygen...\n");
starttime = get_time();
2013-08-17 01:11:09 +08:00
for (trialno = 0; trialno < numtrials; trialno++) {
2013-08-11 01:06:51 +08:00
crypto_box_keypair(pk1, sk1);
crypto_box_keypair(pk2, sk2);
}
2013-08-17 01:11:09 +08:00
2013-08-11 01:06:51 +08:00
endtime = get_time();
2013-08-17 01:11:09 +08:00
keygen_time = endtime - starttime;
2013-08-11 01:06:51 +08:00
printf("starting precompute...\n");
starttime = get_time();
2013-08-17 01:11:09 +08:00
for (trialno = 0; trialno < numtrials; trialno++) {
2013-08-11 01:06:51 +08:00
encrypt_precompute(pk1, sk2, k);
encrypt_precompute(pk2, sk1, k);
}
2013-08-17 01:11:09 +08:00
2013-08-11 01:06:51 +08:00
endtime = get_time();
2013-08-17 01:11:09 +08:00
precompute_time = endtime - starttime;
2013-08-11 01:06:51 +08:00
printf("\n");
2013-08-17 01:11:09 +08:00
printf("trials: %i\n", 2 * numtrials);
2013-08-11 01:06:51 +08:00
printf("\n");
printf("slow time: %f sec\n", slow_time);
printf("fast time: %f sec\n", fast_time);
printf("keygen time: %f sec\n", keygen_time);
printf("precompute time: %f sec\n", precompute_time);
printf("\n");
printf("Speed boost: %.1f%%\n", slow_time * 100 / fast_time);
printf("\n");
2013-08-17 01:11:09 +08:00
printf("slow: %.1f per second\n", 2 * numtrials / slow_time);
printf("fast: %.1f per second\n", 2 * numtrials / fast_time);
2013-08-11 01:06:51 +08:00
return 0;
}