-
Notifications
You must be signed in to change notification settings - Fork 5
/
bench.c
227 lines (192 loc) · 6.2 KB
/
bench.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright (C) 2017 Nagravision S.A.
*/
#include "sign.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#if defined(__amd64__) || defined(__x86_64__)
static unsigned long long cpucycles (void) {
unsigned long long result;
__asm__ __volatile__(".byte 15;.byte 49\n"
"shlq $32,%%rdx\n"
"orq %%rdx,%%rax\n"
: "=a"(result)::"%rdx");
return result;
}
#elif defined(__i386__)
static unsigned long long cpucycles (void) {
unsigned long long result;
__asm__ __volatile__(".byte 15;.byte 49;" : "=A"(result));
return result;
}
#elif defined(_MSC_VER)
#include <intrin.h>
static unsigned long long cpucycles (void) { return __rdtsc (); }
#else
#error "Don't know how to count cycles on this platform!"
#endif
#define BENCH_WARMUP 64
#define BENCH_ROUNDS 32
static int bench_cmp (const void *x, const void *y) {
const int64_t *ix = (const int64_t *)x;
const int64_t *iy = (const int64_t *)y;
return *ix - *iy;
}
extern void expandsk (uint8_t *ek, const uint8_t *sk);
int main () {
int i;
unsigned long long cycles[BENCH_ROUNDS + 1];
unsigned long long smlen;
unsigned long long mlen = N;
uint8_t sk[SKLEN];
uint8_t pk[PKLEN];
uint8_t m[N];
uint8_t *sm = malloc (N + SIGLEN);
uint8_t *ek = malloc (EKLEN);
uint8_t *sk2;
struct timeval tm1, tm2;
unsigned long long usecs;
int ret = -1;
if (!sm) {
fprintf (stderr, "error: sm malloc failed\n");
ret = 1;
if (ek) free (ek);
goto label_exit_0;
}
if (!ek) {
fprintf (stderr, "error: ek malloc failed\n");
ret = 1;
goto label_exit_1;
}
#define MEASURE(s) \
do { \
gettimeofday (&tm2, NULL); \
usecs = 1000000 * (tm2.tv_sec - tm1.tv_sec) + (tm2.tv_usec - tm1.tv_usec); \
for (i = 0; i < BENCH_ROUNDS; ++i) \
cycles[i] = cycles[i + 1] - cycles[i]; \
qsort (cycles, BENCH_ROUNDS, sizeof (uint64_t), bench_cmp); \
printf ("\n# %s\n", s); \
printf ("median cycles count:\t %6llu\n", cycles[BENCH_ROUNDS / 2]); \
printf ("average wall time:\t %.3f usec\n", (double)usecs / BENCH_ROUNDS); \
} while (0)
/***********************/
/* crypto_sign_keypair */
/***********************/
for (i = 0; i < BENCH_WARMUP; ++i) {
if (crypto_sign_keypair (pk, sk)) {
fprintf (stderr, "error: crypto_sign_keypair failed in warmup\n");
ret = 1;
goto label_exit_2;
}
sk[0] = pk[0];
}
gettimeofday (&tm1, NULL);
for (i = 0; i < BENCH_ROUNDS; ++i) {
cycles[i] = cpucycles ();
if (crypto_sign_keypair (pk, sk)) {
fprintf (stderr,
"error: crypto_sign_keypair failed in benchmark\n");
ret = 1;
goto label_exit_2;
}
sk[0] = pk[0];
}
MEASURE ("crypto_sign_keypair");
/***************/
/* crypto_sign */
/***************/
if (crypto_sign_keypair (pk, sk)) {
fprintf (stderr,
"error: crypto_sign_keypair failed before sign warmup\n");
return 1;
}
for (i = 0; i < BENCH_WARMUP; ++i) {
if (crypto_sign (sm, &smlen, m, mlen, sk)) {
fprintf (stderr, "error: crypto_sign failed in warmup\n");
ret = 1;
goto label_exit_2;
}
m[0] = sm[0];
}
gettimeofday (&tm1, NULL);
for (i = 0; i <= BENCH_ROUNDS; ++i) {
cycles[i] = cpucycles ();
if (crypto_sign (sm, &smlen, m, mlen, sk)) {
fprintf (stderr, "error: crypto_sign failed in benchmark\n");
ret = 1;
goto label_exit_2;
}
m[0] = sm[0];
}
MEASURE ("crypto_sign");
/**********************/
/* crypto_sign_cached */
/**********************/
if (crypto_sign_keypair (pk, sk)) {
fprintf (stderr, "error: crypto_sign_keypair failed before sign cached "
"warmup\n");
ret = 1;
goto label_exit_2;
}
expandsk (ek, sk);
sk2 = sk + N;
for (i = 0; i < BENCH_WARMUP; ++i) {
if (crypto_sign_cached (sm, &smlen, m, mlen, sk2, ek)) {
fprintf (stderr, "error: crypto_sign_cached failed in warmup\n");
return 1;
goto label_exit_2;
}
m[0] = sm[0];
}
gettimeofday (&tm1, NULL);
for (i = 0; i <= BENCH_ROUNDS; ++i) {
cycles[i] = cpucycles ();
if (crypto_sign_cached (sm, &smlen, m, mlen, sk2, ek)) {
fprintf (stderr, "error: crypto_sign_cached failed in benchmark\n");
ret = 1;
goto label_exit_2;
}
m[0] = sm[0];
}
MEASURE ("crypto_sign_cached");
/********************/
/* crypto_sign_open */
/********************/
if (crypto_sign_keypair (pk, sk)) {
fprintf (stderr,
"error: crypto_sign_keypair failed before sign_open warmup\n");
ret = 1;
goto label_exit_2;
}
if (crypto_sign (sm, &smlen, m, mlen, sk)) {
fprintf (stderr, "error: crypto_sign failed before sign_open warmup\n");
ret = 1;
goto label_exit_2;
}
for (i = 0; i < BENCH_WARMUP; ++i) {
if (crypto_sign_open (m, &mlen, sm, smlen, pk)) {
fprintf (stderr, "error: crypto_sign_open failed in warmup\n");
ret = 1;
goto label_exit_2;
}
}
gettimeofday (&tm1, NULL);
for (i = 0; i <= BENCH_ROUNDS; ++i) {
cycles[i] = cpucycles ();
if (crypto_sign_open (m, &mlen, sm, smlen, pk)) {
fprintf (stderr, "error: crypto_sign_open failed in benchmark\n");
ret = 1;
goto label_exit_2;
}
}
MEASURE ("crypto_sign_open");
ret = 0;
label_exit_2:
free (ek);
label_exit_1:
free (sm);
label_exit_0:
return ret;
}