Bitcoin ABC  0.22.12
P2P Digital Currency
bench.c
Go to the documentation of this file.
1 #include "sys/time.h"
2 #include <math.h>
3 #include <stdio.h>
4 
5 #include "ctaes.h"
6 
7 static double gettimedouble(void) {
8  struct timeval tv;
9  gettimeofday(&tv, NULL);
10  return tv.tv_usec * 0.000001 + tv.tv_sec;
11 }
12 
13 static void print_number(double x) {
14  double y = x;
15  int c = 0;
16  if (y < 0.0) {
17  y = -y;
18  }
19  while (y < 100.0) {
20  y *= 10.0;
21  c++;
22  }
23  printf("%.*f", c, x);
24 }
25 
26 static void run_benchmark(char *name, void (*benchmark)(void *),
27  void (*setup)(void *), void (*teardown)(void *),
28  void *data, int count, int iter) {
29  int i;
30  double min = HUGE_VAL;
31  double sum = 0.0;
32  double max = 0.0;
33  for (i = 0; i < count; i++) {
34  double begin, total;
35  if (setup != NULL) {
36  setup(data);
37  }
38  begin = gettimedouble();
39  benchmark(data);
40  total = gettimedouble() - begin;
41  if (teardown != NULL) {
42  teardown(data);
43  }
44  if (total < min) {
45  min = total;
46  }
47  if (total > max) {
48  max = total;
49  }
50  sum += total;
51  }
52  printf("%s: min ", name);
53  print_number(min * 1000000000.0 / iter);
54  printf("ns / avg ");
55  print_number((sum / count) * 1000000000.0 / iter);
56  printf("ns / max ");
57  print_number(max * 1000000000.0 / iter);
58  printf("ns\n");
59 }
60 
61 static void bench_AES128_init(void *data) {
62  AES128_ctx *ctx = (AES128_ctx *)data;
63  int i;
64  for (i = 0; i < 50000; i++) {
65  AES128_init(ctx, (uint8_t *)ctx);
66  }
67 }
68 
69 static void bench_AES128_encrypt_setup(void *data) {
70  AES128_ctx *ctx = (AES128_ctx *)data;
71  static const uint8_t key[16] = {0};
72  AES128_init(ctx, key);
73 }
74 
75 static void bench_AES128_encrypt(void *data) {
76  const AES128_ctx *ctx = (const AES128_ctx *)data;
77  uint8_t scratch[16] = {0};
78  int i;
79  for (i = 0; i < 4000000 / 16; i++) {
80  AES128_encrypt(ctx, 1, scratch, scratch);
81  }
82 }
83 
84 static void bench_AES128_decrypt(void *data) {
85  const AES128_ctx *ctx = (const AES128_ctx *)data;
86  uint8_t scratch[16] = {0};
87  int i;
88  for (i = 0; i < 4000000 / 16; i++) {
89  AES128_decrypt(ctx, 1, scratch, scratch);
90  }
91 }
92 
93 static void bench_AES192_init(void *data) {
94  AES192_ctx *ctx = (AES192_ctx *)data;
95  int i;
96  for (i = 0; i < 50000; i++) {
97  AES192_init(ctx, (uint8_t *)ctx);
98  }
99 }
100 
101 static void bench_AES192_encrypt_setup(void *data) {
102  AES192_ctx *ctx = (AES192_ctx *)data;
103  static const uint8_t key[16] = {0};
104  AES192_init(ctx, key);
105 }
106 
107 static void bench_AES192_encrypt(void *data) {
108  const AES192_ctx *ctx = (const AES192_ctx *)data;
109  uint8_t scratch[16] = {0};
110  int i;
111  for (i = 0; i < 4000000 / 16; i++) {
112  AES192_encrypt(ctx, 1, scratch, scratch);
113  }
114 }
115 
116 static void bench_AES192_decrypt(void *data) {
117  const AES192_ctx *ctx = (const AES192_ctx *)data;
118  uint8_t scratch[16] = {0};
119  int i;
120  for (i = 0; i < 4000000 / 16; i++) {
121  AES192_decrypt(ctx, 1, scratch, scratch);
122  }
123 }
124 
125 static void bench_AES256_init(void *data) {
126  AES256_ctx *ctx = (AES256_ctx *)data;
127  int i;
128  for (i = 0; i < 50000; i++) {
129  AES256_init(ctx, (uint8_t *)ctx);
130  }
131 }
132 
133 static void bench_AES256_encrypt_setup(void *data) {
134  AES256_ctx *ctx = (AES256_ctx *)data;
135  static const uint8_t key[16] = {0};
136  AES256_init(ctx, key);
137 }
138 
139 static void bench_AES256_encrypt(void *data) {
140  const AES256_ctx *ctx = (const AES256_ctx *)data;
141  uint8_t scratch[16] = {0};
142  int i;
143  for (i = 0; i < 4000000 / 16; i++) {
144  AES256_encrypt(ctx, 1, scratch, scratch);
145  }
146 }
147 
148 static void bench_AES256_decrypt(void *data) {
149  const AES256_ctx *ctx = (const AES256_ctx *)data;
150  uint8_t scratch[16] = {0};
151  int i;
152  for (i = 0; i < 4000000 / 16; i++) {
153  AES256_decrypt(ctx, 1, scratch, scratch);
154  }
155 }
156 
157 int main(void) {
158  AES128_ctx ctx128;
159  AES192_ctx ctx192;
160  AES256_ctx ctx256;
161  run_benchmark("aes128_init", bench_AES128_init, NULL, NULL, &ctx128, 20,
162  50000);
163  run_benchmark("aes128_encrypt_byte", bench_AES128_encrypt,
164  bench_AES128_encrypt_setup, NULL, &ctx128, 20, 4000000);
165  run_benchmark("aes128_decrypt_byte", bench_AES128_decrypt,
166  bench_AES128_encrypt_setup, NULL, &ctx128, 20, 4000000);
167  run_benchmark("aes192_init", bench_AES192_init, NULL, NULL, &ctx192, 20,
168  50000);
169  run_benchmark("aes192_encrypt_byte", bench_AES192_encrypt,
170  bench_AES192_encrypt_setup, NULL, &ctx192, 20, 4000000);
171  run_benchmark("aes192_decrypt_byte", bench_AES192_decrypt,
172  bench_AES192_encrypt_setup, NULL, &ctx192, 20, 4000000);
173  run_benchmark("aes256_init", bench_AES256_init, NULL, NULL, &ctx256, 20,
174  50000);
175  run_benchmark("aes256_encrypt_byte", bench_AES256_encrypt,
176  bench_AES256_encrypt_setup, NULL, &ctx256, 20, 4000000);
177  run_benchmark("aes256_decrypt_byte", bench_AES256_decrypt,
178  bench_AES256_encrypt_setup, NULL, &ctx256, 20, 4000000);
179  return 0;
180 }
static double gettimedouble(void)
Definition: bench.c:7
static void bench_AES128_decrypt(void *data)
Definition: bench.c:84
static void bench_AES128_init(void *data)
Definition: bench.c:61
static void bench_AES192_encrypt_setup(void *data)
Definition: bench.c:101
static void bench_AES256_encrypt_setup(void *data)
Definition: bench.c:133
void AES256_encrypt(const AES256_ctx *ctx, size_t blocks, uint8_t *cipher16, const uint8_t *plain16)
Definition: ctaes.c:566
void AES192_encrypt(const AES192_ctx *ctx, size_t blocks, uint8_t *cipher16, const uint8_t *plain16)
Definition: ctaes.c:544
secp256k1_context * ctx
static void bench_AES128_encrypt(void *data)
Definition: bench.c:75
static void bench_AES256_decrypt(void *data)
Definition: bench.c:148
static void bench_AES192_decrypt(void *data)
Definition: bench.c:116
void AES192_decrypt(const AES192_ctx *ctx, size_t blocks, uint8_t *plain16, const uint8_t *cipher16)
Definition: ctaes.c:553
void AES256_decrypt(const AES256_ctx *ctx, size_t blocks, uint8_t *plain16, const uint8_t *cipher16)
Definition: ctaes.c:575
static void print_number(double x)
Definition: bench.c:13
void AES128_encrypt(const AES128_ctx *ctx, size_t blocks, uint8_t *cipher16, const uint8_t *plain16)
Definition: ctaes.c:522
volatile double sum
Definition: examples.cpp:21
static void bench_AES256_init(void *data)
Definition: bench.c:125
void AES192_init(AES192_ctx *ctx, const uint8_t *key24)
Definition: ctaes.c:540
const char * name
Definition: rest.cpp:43
static void run_benchmark(char *name, void(*benchmark)(void *), void(*setup)(void *), void(*teardown)(void *), void *data, int count, int iter)
Definition: bench.c:26
static void bench_AES256_encrypt(void *data)
Definition: bench.c:139
static void bench_AES128_encrypt_setup(void *data)
Definition: bench.c:69
void AES128_init(AES128_ctx *ctx, const uint8_t *key16)
Definition: ctaes.c:518
static void bench_AES192_init(void *data)
Definition: bench.c:93
void AES256_init(AES256_ctx *ctx, const uint8_t *key32)
Definition: ctaes.c:562
static int count
Definition: tests.c:35
static void bench_AES192_encrypt(void *data)
Definition: bench.c:107
void printf(const char *fmt, const Args &... args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1125
void AES128_decrypt(const AES128_ctx *ctx, size_t blocks, uint8_t *plain16, const uint8_t *cipher16)
Definition: ctaes.c:531
int main(void)
Definition: bench.c:157