diff options
-rw-r--r-- | testing/CMakeLists.txt | 1 | ||||
-rw-r--r-- | testing/cmake/crypto_speed_test.cmake | 9 | ||||
-rw-r--r-- | testing/crypto_speed_test.c | 130 |
3 files changed, 140 insertions, 0 deletions
diff --git a/testing/CMakeLists.txt b/testing/CMakeLists.txt index f2a2e95e..7322509a 100644 --- a/testing/CMakeLists.txt +++ b/testing/CMakeLists.txt | |||
@@ -8,6 +8,7 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_test.cmake) | |||
8 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Lossless_UDP_testclient.cmake) | 8 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Lossless_UDP_testclient.cmake) |
9 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Lossless_UDP_testserver.cmake) | 9 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Lossless_UDP_testserver.cmake) |
10 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Messenger_test.cmake) | 10 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Messenger_test.cmake) |
11 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/crypto_speed_test.cmake) | ||
11 | 12 | ||
12 | if(WIN32) | 13 | if(WIN32) |
13 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox_win32.cmake) | 14 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox_win32.cmake) |
diff --git a/testing/cmake/crypto_speed_test.cmake b/testing/cmake/crypto_speed_test.cmake new file mode 100644 index 00000000..c269af5b --- /dev/null +++ b/testing/cmake/crypto_speed_test.cmake | |||
@@ -0,0 +1,9 @@ | |||
1 | cmake_minimum_required(VERSION 2.6.0) | ||
2 | project(crypto_speed_test C) | ||
3 | |||
4 | set(exe_name crypto_speed_test) | ||
5 | |||
6 | add_executable(${exe_name} | ||
7 | crypto_speed_test.c) | ||
8 | |||
9 | linkCoreLibraries(${exe_name}) | ||
diff --git a/testing/crypto_speed_test.c b/testing/crypto_speed_test.c new file mode 100644 index 00000000..81be92c6 --- /dev/null +++ b/testing/crypto_speed_test.c | |||
@@ -0,0 +1,130 @@ | |||
1 | // Hi-resolution timer | ||
2 | #ifdef WIN32 | ||
3 | |||
4 | #include <windows.h> | ||
5 | double get_time() | ||
6 | { | ||
7 | LARGE_INTEGER t, f; | ||
8 | QueryPerformanceCounter(&t); | ||
9 | QueryPerformanceFrequency(&f); | ||
10 | return (double)t.QuadPart/(double)f.QuadPart; | ||
11 | } | ||
12 | |||
13 | #else | ||
14 | |||
15 | #include <sys/time.h> | ||
16 | #include <sys/resource.h> | ||
17 | |||
18 | double get_time() | ||
19 | { | ||
20 | struct timeval t; | ||
21 | struct timezone tzp; | ||
22 | gettimeofday(&t, &tzp); | ||
23 | return t.tv_sec + t.tv_usec*1e-6; | ||
24 | } | ||
25 | |||
26 | #endif | ||
27 | |||
28 | #include "../core/net_crypto.h" | ||
29 | #include <stdlib.h> | ||
30 | #include <time.h> | ||
31 | |||
32 | void rand_bytes(uint8_t *b, size_t blen) | ||
33 | { | ||
34 | size_t i; | ||
35 | for (i = 0; i < blen; i++) | ||
36 | { | ||
37 | b[i] = rand(); | ||
38 | } | ||
39 | } | ||
40 | |||
41 | int main(int argc, char* argv[]) | ||
42 | { | ||
43 | const int numtrials = 10000; | ||
44 | |||
45 | unsigned char pk1[crypto_box_PUBLICKEYBYTES]; | ||
46 | unsigned char sk1[crypto_box_SECRETKEYBYTES]; | ||
47 | unsigned char pk2[crypto_box_PUBLICKEYBYTES]; | ||
48 | unsigned char sk2[crypto_box_SECRETKEYBYTES]; | ||
49 | unsigned char k1[crypto_box_BEFORENMBYTES]; | ||
50 | unsigned char k2[crypto_box_BEFORENMBYTES]; | ||
51 | |||
52 | unsigned char n[crypto_box_NONCEBYTES]; | ||
53 | |||
54 | unsigned char m[500]; | ||
55 | unsigned char c[sizeof(m) + ENCRYPTION_PADDING]; | ||
56 | |||
57 | unsigned char k[crypto_box_BEFORENMBYTES]; | ||
58 | |||
59 | int trialno; | ||
60 | |||
61 | double starttime; | ||
62 | double endtime; | ||
63 | double slow_time; | ||
64 | double fast_time; | ||
65 | double keygen_time; | ||
66 | double precompute_time; | ||
67 | |||
68 | // Pregenerate | ||
69 | crypto_box_keypair(pk1, sk1); | ||
70 | crypto_box_keypair(pk2, sk2); | ||
71 | encrypt_precompute(pk1, sk2, k1); | ||
72 | encrypt_precompute(pk2, sk1, k2); | ||
73 | rand_bytes(m, sizeof(m)); | ||
74 | rand_bytes(n, sizeof(n)); | ||
75 | |||
76 | printf("starting slow...\n"); | ||
77 | starttime = get_time(); | ||
78 | for (trialno = 0; trialno < numtrials; trialno++) | ||
79 | { | ||
80 | encrypt_data(pk1, sk2, n, m, sizeof(m), c); | ||
81 | decrypt_data(pk2, sk1, n, c, sizeof(c), m); | ||
82 | } | ||
83 | endtime = get_time(); | ||
84 | slow_time = endtime-starttime; | ||
85 | |||
86 | printf("starting fast...\n"); | ||
87 | starttime = get_time(); | ||
88 | for (trialno = 0; trialno < numtrials; trialno++) | ||
89 | { | ||
90 | encrypt_data_fast(k1, n, m, sizeof(m), c); | ||
91 | decrypt_data_fast(k2, n, c, sizeof(c), m); | ||
92 | } | ||
93 | endtime = get_time(); | ||
94 | fast_time = endtime-starttime; | ||
95 | |||
96 | printf("starting keygen...\n"); | ||
97 | starttime = get_time(); | ||
98 | for (trialno = 0; trialno < numtrials; trialno++) | ||
99 | { | ||
100 | crypto_box_keypair(pk1, sk1); | ||
101 | crypto_box_keypair(pk2, sk2); | ||
102 | } | ||
103 | endtime = get_time(); | ||
104 | keygen_time = endtime-starttime; | ||
105 | |||
106 | printf("starting precompute...\n"); | ||
107 | starttime = get_time(); | ||
108 | for (trialno = 0; trialno < numtrials; trialno++) | ||
109 | { | ||
110 | encrypt_precompute(pk1, sk2, k); | ||
111 | encrypt_precompute(pk2, sk1, k); | ||
112 | } | ||
113 | endtime = get_time(); | ||
114 | precompute_time = endtime-starttime; | ||
115 | |||
116 | printf("\n"); | ||
117 | printf("trials: %i\n", 2*numtrials); | ||
118 | printf("\n"); | ||
119 | printf("slow time: %f sec\n", slow_time); | ||
120 | printf("fast time: %f sec\n", fast_time); | ||
121 | printf("keygen time: %f sec\n", keygen_time); | ||
122 | printf("precompute time: %f sec\n", precompute_time); | ||
123 | printf("\n"); | ||
124 | printf("Speed boost: %.1f%%\n", slow_time * 100 / fast_time); | ||
125 | printf("\n"); | ||
126 | printf("slow: %.1f per second\n", 2*numtrials/slow_time); | ||
127 | printf("fast: %.1f per second\n", 2*numtrials/fast_time); | ||
128 | |||
129 | return 0; | ||
130 | } | ||