mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Added crypto_speed_test
This commit is contained in:
parent
021852d494
commit
acca5c8e8f
@ -8,6 +8,7 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_test.cmake)
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Lossless_UDP_testclient.cmake)
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Lossless_UDP_testserver.cmake)
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Messenger_test.cmake)
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/crypto_speed_test.cmake)
|
||||
|
||||
if(WIN32)
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox_win32.cmake)
|
||||
|
9
testing/cmake/crypto_speed_test.cmake
Normal file
9
testing/cmake/crypto_speed_test.cmake
Normal file
@ -0,0 +1,9 @@
|
||||
cmake_minimum_required(VERSION 2.6.0)
|
||||
project(crypto_speed_test C)
|
||||
|
||||
set(exe_name crypto_speed_test)
|
||||
|
||||
add_executable(${exe_name}
|
||||
crypto_speed_test.c)
|
||||
|
||||
linkCoreLibraries(${exe_name})
|
130
testing/crypto_speed_test.c
Normal file
130
testing/crypto_speed_test.c
Normal file
@ -0,0 +1,130 @@
|
||||
// Hi-resolution timer
|
||||
#ifdef WIN32
|
||||
|
||||
#include <windows.h>
|
||||
double get_time()
|
||||
{
|
||||
LARGE_INTEGER t, f;
|
||||
QueryPerformanceCounter(&t);
|
||||
QueryPerformanceFrequency(&f);
|
||||
return (double)t.QuadPart/(double)f.QuadPart;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
double get_time()
|
||||
{
|
||||
struct timeval t;
|
||||
struct timezone tzp;
|
||||
gettimeofday(&t, &tzp);
|
||||
return t.tv_sec + t.tv_usec*1e-6;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#include "../core/net_crypto.h"
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
void rand_bytes(uint8_t *b, size_t blen)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < blen; i++)
|
||||
{
|
||||
b[i] = rand();
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
const int numtrials = 10000;
|
||||
|
||||
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];
|
||||
|
||||
unsigned char n[crypto_box_NONCEBYTES];
|
||||
|
||||
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();
|
||||
for (trialno = 0; trialno < numtrials; trialno++)
|
||||
{
|
||||
encrypt_data(pk1, sk2, n, m, sizeof(m), c);
|
||||
decrypt_data(pk2, sk1, n, c, sizeof(c), m);
|
||||
}
|
||||
endtime = get_time();
|
||||
slow_time = endtime-starttime;
|
||||
|
||||
printf("starting fast...\n");
|
||||
starttime = get_time();
|
||||
for (trialno = 0; trialno < numtrials; trialno++)
|
||||
{
|
||||
encrypt_data_fast(k1, n, m, sizeof(m), c);
|
||||
decrypt_data_fast(k2, n, c, sizeof(c), m);
|
||||
}
|
||||
endtime = get_time();
|
||||
fast_time = endtime-starttime;
|
||||
|
||||
printf("starting keygen...\n");
|
||||
starttime = get_time();
|
||||
for (trialno = 0; trialno < numtrials; trialno++)
|
||||
{
|
||||
crypto_box_keypair(pk1, sk1);
|
||||
crypto_box_keypair(pk2, sk2);
|
||||
}
|
||||
endtime = get_time();
|
||||
keygen_time = endtime-starttime;
|
||||
|
||||
printf("starting precompute...\n");
|
||||
starttime = get_time();
|
||||
for (trialno = 0; trialno < numtrials; trialno++)
|
||||
{
|
||||
encrypt_precompute(pk1, sk2, k);
|
||||
encrypt_precompute(pk2, sk1, k);
|
||||
}
|
||||
endtime = get_time();
|
||||
precompute_time = endtime-starttime;
|
||||
|
||||
printf("\n");
|
||||
printf("trials: %i\n", 2*numtrials);
|
||||
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");
|
||||
printf("slow: %.1f per second\n", 2*numtrials/slow_time);
|
||||
printf("fast: %.1f per second\n", 2*numtrials/fast_time);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user