cb4827509494deaab94da239e4e7e524f90e5543
[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 struct GNUNET_TIME_Absolute start;
31 const int l = 50;
32
33 struct TestSig {
34   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
35   struct GNUNET_HashCode h;
36   struct GNUNET_CRYPTO_EddsaSignature sig;
37 };
38
39 static void
40 log_duration (const char *system, const char *description)
41 {
42   struct GNUNET_TIME_Relative t;
43   char s[64];
44
45   sprintf (s, "%6s %15s", system, description);
46   t = GNUNET_TIME_absolute_get_duration (start);
47   t = GNUNET_TIME_relative_divide (t, l);
48   FPRINTF (stdout, "%s: %10s\n", s,
49            GNUNET_STRINGS_relative_time_to_string(t, GNUNET_NO));
50
51   GAUGER ("UTIL", s, t.rel_value_us, "us");
52 }
53
54 int
55 main (int argc, char *argv[])
56 {
57   int i;
58   struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe[l];
59   struct GNUNET_CRYPTO_EcdhePublicKey dhpub[l];
60   struct GNUNET_CRYPTO_EddsaPrivateKey *eddsa[l];
61   struct GNUNET_CRYPTO_EddsaPublicKey dspub[l];
62   struct TestSig sig[l];
63
64   start = GNUNET_TIME_absolute_get();
65   for (i = 0; i < l; i++)
66   {
67     sig[i].purp.purpose = 0;
68     sig[i].purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)
69                               + sizeof (struct GNUNET_HashCode));
70     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
71                                 &sig[i].h, sizeof (&sig[0].h));
72   }
73   log_duration ("", "Init");
74
75   start = GNUNET_TIME_absolute_get();
76   for (i = 0; i < l; i++)
77     eddsa[i] = GNUNET_CRYPTO_eddsa_key_create();
78   log_duration ("EdDSA", "create key");
79
80   start = GNUNET_TIME_absolute_get();
81   for (i = 0; i < l; i++)
82     GNUNET_CRYPTO_eddsa_key_get_public (eddsa[i], &dspub[i]);
83   log_duration ("EdDSA", "get pubilc");
84
85   start = GNUNET_TIME_absolute_get();
86   for (i = 0; i < l; i++)
87     GNUNET_CRYPTO_eddsa_sign (eddsa[i], &sig[i].purp, &sig[i].sig);
88   log_duration ("EdDSA", "sign HashCode");
89
90   start = GNUNET_TIME_absolute_get();
91   for (i = 0; i < l; i++)
92     GNUNET_CRYPTO_eddsa_verify (0, &sig[i].purp, &sig[i].sig, &dspub[i]);
93   log_duration ("EdDSA", "verify HashCode");
94
95   start = GNUNET_TIME_absolute_get();
96   for (i = 0; i < l; i++)
97     ecdhe[i] = GNUNET_CRYPTO_ecdhe_key_create();
98   log_duration ("ECDH", "create key");
99
100   start = GNUNET_TIME_absolute_get();
101   for (i = 0; i < l; i++)
102     GNUNET_CRYPTO_ecdhe_key_get_public (ecdhe[i], &dhpub[i]);
103   log_duration ("ECDH", "get public");
104
105   start = GNUNET_TIME_absolute_get();
106   for (i = 0; i < l; i+=2)
107   {
108     GNUNET_CRYPTO_ecc_ecdh (ecdhe[i], &dhpub[i+1], &sig[i].h);
109     GNUNET_CRYPTO_ecc_ecdh (ecdhe[i+1], &dhpub[i], &sig[i+i].h);
110   }
111   log_duration ("ECDH", "do DH");
112
113   return 0;
114 }
115
116 /* end of perf_crypto_asymmetric.c */