6c3db48fc3e5d397bb6c08e9a0101336c2c92174
[oweals/gnunet.git] / src / util / perf_crypto_asymmetric.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2015 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14 */
15
16 /**
17  * @author Bart Polot
18  * @file util/perf_crypto_asymmetric.c
19  * @brief measure performance of public key functions
20  */
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23 #include <gauger.h>
24
25 static struct GNUNET_TIME_Absolute start;
26
27 #define l 50
28
29 struct TestSig
30 {
31   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
32   struct GNUNET_HashCode h;
33   struct GNUNET_CRYPTO_EddsaSignature sig;
34 };
35
36
37 static void
38 log_duration (const char *cryptosystem,
39               const char *description)
40 {
41   struct GNUNET_TIME_Relative t;
42   char s[64];
43
44   sprintf (s, "%6s %15s", cryptosystem, description);
45   t = GNUNET_TIME_absolute_get_duration (start);
46   t = GNUNET_TIME_relative_divide (t, l);
47   FPRINTF (stdout,
48            "%s: %10s\n",
49            s,
50            GNUNET_STRINGS_relative_time_to_string (t,
51                                                    GNUNET_NO));
52   GAUGER ("UTIL", s, t.rel_value_us, "us");
53 }
54
55
56 int
57 main (int argc, char *argv[])
58 {
59   int i;
60   struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe[l];
61   struct GNUNET_CRYPTO_EcdhePublicKey dhpub[l];
62   struct GNUNET_CRYPTO_EddsaPrivateKey *eddsa[l];
63   struct GNUNET_CRYPTO_EddsaPublicKey dspub[l];
64   struct TestSig sig[l];
65
66   start = GNUNET_TIME_absolute_get();
67   for (i = 0; i < l; i++)
68   {
69     sig[i].purp.purpose = 0;
70     sig[i].purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)
71                               + sizeof (struct GNUNET_HashCode));
72     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
73                                 &sig[i].h,
74                                 sizeof (sig[i].h));
75   }
76   log_duration ("", "Init");
77
78   start = GNUNET_TIME_absolute_get();
79   for (i = 0; i < l; i++)
80     eddsa[i] = GNUNET_CRYPTO_eddsa_key_create();
81   log_duration ("EdDSA", "create key");
82
83   start = GNUNET_TIME_absolute_get();
84   for (i = 0; i < l; i++)
85     GNUNET_CRYPTO_eddsa_key_get_public (eddsa[i], &dspub[i]);
86   log_duration ("EdDSA", "get public");
87
88   start = GNUNET_TIME_absolute_get();
89   for (i = 0; i < l; i++)
90     GNUNET_assert (GNUNET_OK ==
91                    GNUNET_CRYPTO_eddsa_sign (eddsa[i],
92                                              &sig[i].purp,
93                                              &sig[i].sig));
94   log_duration ("EdDSA", "sign HashCode");
95
96   start = GNUNET_TIME_absolute_get();
97   for (i = 0; i < l; i++)
98     GNUNET_assert (GNUNET_OK ==
99                    GNUNET_CRYPTO_eddsa_verify (0,
100                                                &sig[i].purp,
101                                                &sig[i].sig,
102                                                &dspub[i]));
103   log_duration ("EdDSA", "verify HashCode");
104
105   start = GNUNET_TIME_absolute_get();
106   for (i = 0; i < l; i++)
107     ecdhe[i] = GNUNET_CRYPTO_ecdhe_key_create();
108   log_duration ("ECDH", "create key");
109
110   start = GNUNET_TIME_absolute_get();
111   for (i = 0; i < l; i++)
112     GNUNET_CRYPTO_ecdhe_key_get_public (ecdhe[i], &dhpub[i]);
113   log_duration ("ECDH", "get public");
114
115   start = GNUNET_TIME_absolute_get();
116   for (i = 0; i < l - 1; i+=2)
117   {
118     GNUNET_CRYPTO_ecc_ecdh (ecdhe[i], &dhpub[i+1], &sig[i].h);
119     GNUNET_CRYPTO_ecc_ecdh (ecdhe[i+1], &dhpub[i], &sig[i+1].h);
120   }
121   log_duration ("ECDH", "do DH");
122
123   return 0;
124 }
125
126 /* end of perf_crypto_asymmetric.c */