fe79757d58f2cecf579057bb543e95e1d6b09bed
[oweals/gnunet.git] / src / util / perf_crypto_asymmetric.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2015 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @author Bart Polot
23  * @file util/perf_crypto_asymmetric.c
24  * @brief measure performance of public key functions
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include <gauger.h>
29
30 static struct GNUNET_TIME_Absolute start;
31
32 #define l 50
33
34 struct TestSig
35 {
36   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
37   struct GNUNET_HashCode h;
38   struct GNUNET_CRYPTO_EddsaSignature sig;
39 };
40
41
42 static void
43 log_duration (const char *cryptosystem,
44               const char *description)
45 {
46   struct GNUNET_TIME_Relative t;
47   char s[64];
48
49   sprintf (s, "%6s %15s", cryptosystem, description);
50   t = GNUNET_TIME_absolute_get_duration (start);
51   t = GNUNET_TIME_relative_divide (t, l);
52   FPRINTF (stdout,
53            "%s: %10s\n",
54            s,
55            GNUNET_STRINGS_relative_time_to_string (t,
56                                                    GNUNET_NO));
57   GAUGER ("UTIL", s, t.rel_value_us, "us");
58 }
59
60
61 int
62 main (int argc, char *argv[])
63 {
64   int i;
65   struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe[l];
66   struct GNUNET_CRYPTO_EcdhePublicKey dhpub[l];
67   struct GNUNET_CRYPTO_EddsaPrivateKey *eddsa[l];
68   struct GNUNET_CRYPTO_EddsaPublicKey dspub[l];
69   struct TestSig sig[l];
70
71   start = GNUNET_TIME_absolute_get();
72   for (i = 0; i < l; i++)
73   {
74     sig[i].purp.purpose = 0;
75     sig[i].purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)
76                               + sizeof (struct GNUNET_HashCode));
77     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
78                                 &sig[i].h,
79                                 sizeof (sig[i].h));
80   }
81   log_duration ("", "Init");
82
83   start = GNUNET_TIME_absolute_get();
84   for (i = 0; i < l; i++)
85     eddsa[i] = GNUNET_CRYPTO_eddsa_key_create();
86   log_duration ("EdDSA", "create key");
87
88   start = GNUNET_TIME_absolute_get();
89   for (i = 0; i < l; i++)
90     GNUNET_CRYPTO_eddsa_key_get_public (eddsa[i], &dspub[i]);
91   log_duration ("EdDSA", "get pubilc");
92
93   start = GNUNET_TIME_absolute_get();
94   for (i = 0; i < l; i++)
95     GNUNET_CRYPTO_eddsa_sign (eddsa[i], &sig[i].purp, &sig[i].sig);
96   log_duration ("EdDSA", "sign HashCode");
97
98   start = GNUNET_TIME_absolute_get();
99   for (i = 0; i < l; i++)
100     GNUNET_CRYPTO_eddsa_verify (0, &sig[i].purp, &sig[i].sig, &dspub[i]);
101   log_duration ("EdDSA", "verify HashCode");
102
103   start = GNUNET_TIME_absolute_get();
104   for (i = 0; i < l; i++)
105     ecdhe[i] = GNUNET_CRYPTO_ecdhe_key_create();
106   log_duration ("ECDH", "create key");
107
108   start = GNUNET_TIME_absolute_get();
109   for (i = 0; i < l; i++)
110     GNUNET_CRYPTO_ecdhe_key_get_public (ecdhe[i], &dhpub[i]);
111   log_duration ("ECDH", "get public");
112
113   start = GNUNET_TIME_absolute_get();
114   for (i = 0; i < l - 1; i+=2)
115   {
116     GNUNET_CRYPTO_ecc_ecdh (ecdhe[i], &dhpub[i+1], &sig[i].h);
117     GNUNET_CRYPTO_ecc_ecdh (ecdhe[i+1], &dhpub[i], &sig[i+1].h);
118   }
119   log_duration ("ECDH", "do DH");
120
121   return 0;
122 }
123
124 /* end of perf_crypto_asymmetric.c */