acfdba5413403ae5cd2b7af314d24c7549c2e7d9
[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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @author Bart Polot
21  * @file util/perf_crypto_asymmetric.c
22  * @brief measure performance of public key functions
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include <gauger.h>
27
28 static struct GNUNET_TIME_Absolute start;
29
30 #define l 50
31
32 struct TestSig
33 {
34   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
35   struct GNUNET_HashCode h;
36   struct GNUNET_CRYPTO_EddsaSignature sig;
37 };
38
39
40 static void
41 log_duration (const char *cryptosystem,
42               const char *description)
43 {
44   struct GNUNET_TIME_Relative t;
45   char s[64];
46
47   sprintf (s, "%6s %15s", cryptosystem, description);
48   t = GNUNET_TIME_absolute_get_duration (start);
49   t = GNUNET_TIME_relative_divide (t, l);
50   FPRINTF (stdout,
51            "%s: %10s\n",
52            s,
53            GNUNET_STRINGS_relative_time_to_string (t,
54                                                    GNUNET_NO));
55   GAUGER ("UTIL", s, t.rel_value_us, "us");
56 }
57
58
59 int
60 main (int argc, char *argv[])
61 {
62   int i;
63   struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe[l];
64   struct GNUNET_CRYPTO_EcdhePublicKey dhpub[l];
65   struct GNUNET_CRYPTO_EddsaPrivateKey *eddsa[l];
66   struct GNUNET_CRYPTO_EddsaPublicKey dspub[l];
67   struct TestSig sig[l];
68
69   start = GNUNET_TIME_absolute_get();
70   for (i = 0; i < l; i++)
71   {
72     sig[i].purp.purpose = 0;
73     sig[i].purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)
74                               + sizeof (struct GNUNET_HashCode));
75     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
76                                 &sig[i].h,
77                                 sizeof (sig[i].h));
78   }
79   log_duration ("", "Init");
80
81   start = GNUNET_TIME_absolute_get();
82   for (i = 0; i < l; i++)
83     eddsa[i] = GNUNET_CRYPTO_eddsa_key_create();
84   log_duration ("EdDSA", "create key");
85
86   start = GNUNET_TIME_absolute_get();
87   for (i = 0; i < l; i++)
88     GNUNET_CRYPTO_eddsa_key_get_public (eddsa[i], &dspub[i]);
89   log_duration ("EdDSA", "get public");
90
91   start = GNUNET_TIME_absolute_get();
92   for (i = 0; i < l; i++)
93     GNUNET_assert (GNUNET_OK ==
94                    GNUNET_CRYPTO_eddsa_sign (eddsa[i],
95                                              &sig[i].purp,
96                                              &sig[i].sig));
97   log_duration ("EdDSA", "sign HashCode");
98
99   start = GNUNET_TIME_absolute_get();
100   for (i = 0; i < l; i++)
101     GNUNET_assert (GNUNET_OK ==
102                    GNUNET_CRYPTO_eddsa_verify (0,
103                                                &sig[i].purp,
104                                                &sig[i].sig,
105                                                &dspub[i]));
106   log_duration ("EdDSA", "verify HashCode");
107
108   start = GNUNET_TIME_absolute_get();
109   for (i = 0; i < l; i++)
110     ecdhe[i] = GNUNET_CRYPTO_ecdhe_key_create();
111   log_duration ("ECDH", "create key");
112
113   start = GNUNET_TIME_absolute_get();
114   for (i = 0; i < l; i++)
115     GNUNET_CRYPTO_ecdhe_key_get_public (ecdhe[i], &dhpub[i]);
116   log_duration ("ECDH", "get public");
117
118   start = GNUNET_TIME_absolute_get();
119   for (i = 0; i < l - 1; i+=2)
120   {
121     GNUNET_CRYPTO_ecc_ecdh (ecdhe[i], &dhpub[i+1], &sig[i].h);
122     GNUNET_CRYPTO_ecc_ecdh (ecdhe[i+1], &dhpub[i], &sig[i+1].h);
123   }
124   log_duration ("ECDH", "do DH");
125
126   return 0;
127 }
128
129 /* end of perf_crypto_asymmetric.c */