Skip to content

Commit bd0cf73

Browse files
authored
Create performance_test.c
1 parent 9762f1d commit bd0cf73

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

test/performance_test.c

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include "HiAE.h"
6+
#include <time.h>
7+
8+
#define REPEAT 262144
9+
10+
const int len_test_case = 9;
11+
size_t test_case[9] = {16, 64, 256, 512, 1024, 2048, 4096, 8192, 16384};
12+
13+
void print_data(const uint8_t *data, size_t len) {
14+
for (size_t i = 0; i < len; ++i) {
15+
printf("%02x", data[i]);
16+
}
17+
printf("\n");
18+
}
19+
20+
double speed_test_ad_work(size_t len) {
21+
uint8_t key[32];
22+
memset(key, 1, 32);
23+
uint8_t iv[16];
24+
memset(iv, 1, 16);
25+
size_t ad_len = len;
26+
uint8_t *ad = (uint8_t *) malloc(ad_len);
27+
memset(ad, 1, ad_len);
28+
clock_t start, end;
29+
uint8_t tag[16];
30+
start = clock();
31+
for (size_t iter = REPEAT; iter > 0; iter --) {
32+
HiAE_verification(key, iv, ad, ad_len, tag);
33+
}
34+
end = clock();
35+
36+
double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
37+
double speed = ((double) REPEAT * len) / (cpu_time_used*(125000000));
38+
39+
return speed;
40+
}
41+
42+
double speed_test_encode_work(size_t len, int AEAD) {
43+
uint8_t key[32];
44+
memset(key, 1, 32);
45+
uint8_t iv[16];
46+
memset(iv, 1, 16);
47+
size_t ad_len = 48;
48+
uint8_t *ad = (uint8_t *) malloc(ad_len);
49+
memset(ad, 1, ad_len);
50+
size_t plain_len = len;
51+
uint8_t *plain = (uint8_t *) malloc(plain_len);
52+
uint8_t *cipher = (uint8_t *) malloc(plain_len);
53+
memset(plain, 0x1, plain_len);
54+
clock_t start, end;
55+
56+
if(AEAD == 1) {
57+
uint8_t tag[16];
58+
start = clock();
59+
for (size_t iter = REPEAT; iter > 0; iter --) {
60+
HiAE_AEAD_encrypt(key, iv, plain, cipher, plain_len, ad, ad_len, tag);
61+
}
62+
end = clock();
63+
}
64+
else {
65+
start = clock();
66+
for (size_t iter = REPEAT; iter > 0; iter --) {
67+
HiAE_encrypt(key, iv, plain, cipher, plain_len);
68+
}
69+
end = clock();
70+
}
71+
72+
double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
73+
double speed = ((double) REPEAT * plain_len) / (cpu_time_used*(125000000));
74+
75+
return speed;
76+
}
77+
78+
double speed_test_decode_work(size_t len, int AEAD) {
79+
uint8_t key[32];
80+
memset(key, 1, 32);
81+
uint8_t iv[16];
82+
memset(iv, 1, 16);
83+
size_t ad_len = 48;
84+
uint8_t *ad = (uint8_t *) malloc(ad_len);
85+
memset(ad, 1, ad_len);
86+
size_t plain_len = len;
87+
uint8_t *plain = (uint8_t *) malloc(plain_len);
88+
uint8_t *cipher = (uint8_t *) malloc(plain_len);
89+
memset(plain, 0x1, plain_len);
90+
clock_t start, end;
91+
92+
if(AEAD == 1) {
93+
uint8_t tag[16];
94+
start = clock();
95+
for (size_t iter = REPEAT; iter > 0; iter --) {
96+
HiAE_AEAD_decrypt(key, iv, plain, cipher, plain_len, ad, ad_len, tag);
97+
}
98+
end = clock();
99+
}
100+
else {
101+
start = clock();
102+
for (size_t iter = REPEAT; iter > 0; iter --) {
103+
HiAE_decrypt(key, iv, plain, cipher, plain_len);
104+
}
105+
end = clock();
106+
}
107+
double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
108+
double speed = ((double) REPEAT * plain_len) / (cpu_time_used*(125000000.0));
109+
110+
return speed;
111+
}
112+
113+
void speed_test_encryption() {
114+
double encrypto_speed[len_test_case];
115+
double decrypto_speed[len_test_case];
116+
printf("--------speed test Encryption Only(Gbps)----------\n");
117+
for (int i = 0; i < len_test_case; i++)
118+
{
119+
encrypto_speed[i] = speed_test_encode_work(test_case[i], 0);
120+
decrypto_speed[i] = speed_test_decode_work(test_case[i], 0);
121+
printf("length: %ld, encrypt: %.2f, decrypt: %.2f\n", test_case[i], encrypto_speed[i], decrypto_speed[i]);
122+
}
123+
}
124+
125+
void speed_test_ad_only() {
126+
printf("--------speed test AD Only(Gbps)----------\n");
127+
for (int i = 0; i < len_test_case; i++)
128+
{
129+
double ad = speed_test_ad_work(test_case[i]);
130+
printf("length: %ld, AD: %.2f\n", test_case[i], ad);
131+
}
132+
}
133+
134+
void speed_test_aead() {
135+
double encrypto_speed[len_test_case];
136+
double decrypto_speed[len_test_case];
137+
printf("--------speed test AEAD(Gbps)----------\n");
138+
for (int i = 0; i < len_test_case; i++)
139+
{
140+
encrypto_speed[i] = speed_test_encode_work(test_case[i], 1);
141+
decrypto_speed[i] = speed_test_decode_work(test_case[i], 1);
142+
printf("length: %ld, encrypt: %.2f, decrypt: %.2f\n", test_case[i], encrypto_speed[i], decrypto_speed[i]);
143+
}
144+
}
145+
146+
int main() {
147+
printf("========HiAE Performance test========\n");
148+
speed_test_encryption();
149+
speed_test_ad_only();
150+
speed_test_aead();
151+
}

0 commit comments

Comments
 (0)