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
|
// bench.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// #include "ntru.h"
#include <stdint.h>
#define NUM_ITER_KEYGEN 50
#define NUM_ITER_ENCDEC 10000
int compare_double(const void *p1, const void *p2) {
double t1 = *(double*)p1;
double t2 = *(double*)p2;
return t1<t2 ? -1 : (t1>t2 ? 1 : 0);
}
double median(double *samples, int num_samples) {
if (num_samples == 0)
return 0;
if (num_samples == 1)
return samples[0];
qsort(samples, num_samples, sizeof(samples[0]), compare_double);
if (num_samples%2 == 0)
return (samples[num_samples/2-1]+samples[num_samples/2]) / 2;
else
return samples[num_samples/2];
}
void print_time(char *label, double *samples, int num_samples) {
double time = median(samples, num_samples);
double per_sec = 1000000.0 / time;
// printf("%s %dus=%d/sec ", label, (uint32_t)time, (uint32_t)per_sec);
printf("%s %ldus=%ld/sec ", label, (long)time, (long)per_sec);
fflush(stdout);
}
int main(int argc, char **argv) {
printf("Please wait...\n");
uint8_t success = 1;
#if 0
NtruEncParams param_arr[] = ALL_PARAM_SETS;
uint8_t param_idx;
for (param_idx=0; param_idx<sizeof(param_arr)/sizeof(param_arr[0]); param_idx++) {
NtruEncParams params = param_arr[param_idx];
NtruEncKeyPair kp;
uint32_t i;
struct timespec t1, t2;
printf("%-10s ", params.name);
fflush(stdout);
double samples_keygen[NUM_ITER_KEYGEN];
NtruRandGen rng = NTRU_RNG_DEFAULT;
NtruRandContext rand_ctx;
success &= ntru_rand_init(&rand_ctx, &rng) == NTRU_SUCCESS;
for (i=0; i<NUM_ITER_KEYGEN; i++) {
clock_gettime(CLOCK_REALTIME, &t1);
success &= ntru_gen_key_pair(¶ms, &kp, &rand_ctx) == NTRU_SUCCESS;
clock_gettime(CLOCK_REALTIME, &t2);
double duration = 1000000000.0*(t2.tv_sec-t1.tv_sec) + t2.tv_nsec-t1.tv_nsec; /* nanoseconds */
samples_keygen[i] = duration / 1000.0; /* microseconds */
}
print_time("keygen", samples_keygen, NUM_ITER_KEYGEN);
double samples_encdec[NUM_ITER_ENCDEC];
uint16_t max_len = ntru_max_msg_len(¶ms); /* max message length for this param set */
uint8_t plain[max_len];
success &= ntru_rand_generate(plain, max_len, &rand_ctx) == NTRU_SUCCESS;
uint16_t enc_len = ntru_enc_len(¶ms);
uint8_t encrypted[enc_len];
uint8_t decrypted[max_len];
for (i=0; i<NUM_ITER_ENCDEC; i++) {
clock_gettime(CLOCK_REALTIME, &t1);
success &= ntru_encrypt((uint8_t*)&plain, max_len, &kp.pub, ¶ms, &rand_ctx, (uint8_t*)&encrypted) == NTRU_SUCCESS;
clock_gettime(CLOCK_REALTIME, &t2);
double duration = 1000000000.0*(t2.tv_sec-t1.tv_sec) + t2.tv_nsec-t1.tv_nsec; /* nanoseconds */
samples_encdec[i] = duration / 1000.0; /* microseconds */
}
print_time("enc", samples_encdec, NUM_ITER_ENCDEC);
success &= ntru_rand_release(&rand_ctx) == NTRU_SUCCESS;
uint16_t dec_len;
for (i=0; i<NUM_ITER_ENCDEC; i++) {
clock_gettime(CLOCK_REALTIME, &t1);
success &= ntru_decrypt((uint8_t*)&encrypted, &kp, ¶ms, (uint8_t*)&decrypted, &dec_len) == NTRU_SUCCESS;
clock_gettime(CLOCK_REALTIME, &t2);
double duration = 1000000000.0*(t2.tv_sec-t1.tv_sec) + t2.tv_nsec-t1.tv_nsec; /* nanoseconds */
samples_encdec[i] = duration / 1000.0; /* microseconds */
}
print_time("dec", samples_encdec, NUM_ITER_ENCDEC);
printf("\n");
}
#endif
if (!success)
printf("Error!\n");
return success ? 0 : 1;
}
| |