-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / util / perf_crypto_paillier.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2014 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 Christian Grothoff
21  * @file util/perf_crypto_paillier.c
22  * @brief measure performance of Paillier encryption
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include <gauger.h>
27
28
29 int
30 main (int argc, char *argv[])
31 {
32   struct GNUNET_TIME_Absolute start;
33   struct GNUNET_CRYPTO_PaillierPublicKey public_key;
34   struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
35   struct GNUNET_CRYPTO_PaillierCiphertext c1;
36   gcry_mpi_t m1;
37   unsigned int i;
38
39   start = GNUNET_TIME_absolute_get ();
40   for (i=0;i<10;i++)
41     GNUNET_CRYPTO_paillier_create (&public_key,
42                                    &private_key);
43   printf ("10x key generation took %s\n",
44           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
45                                                   GNUNET_YES));
46   GAUGER ("UTIL", "Paillier key generation",
47           64 * 1024 / (1 +
48                        GNUNET_TIME_absolute_get_duration
49                        (start).rel_value_us / 1000LL), "keys/ms");
50
51   m1 = gcry_mpi_new (0);
52   m1 = gcry_mpi_set_ui (m1, 1);
53   /* m1 = m1 * 2 ^ (GCPB - 3) */
54   gcry_mpi_mul_2exp (m1,
55                      m1,
56                      GNUNET_CRYPTO_PAILLIER_BITS - 3);
57   start = GNUNET_TIME_absolute_get ();
58   for (i=0;i<10;i++)
59     GNUNET_CRYPTO_paillier_encrypt (&public_key,
60                                     m1,
61                                     2,
62                                     &c1);
63   printf ("10x encryption took %s\n",
64           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
65                                                   GNUNET_YES));
66   GAUGER ("UTIL", "Paillier encryption",
67           64 * 1024 / (1 +
68                        GNUNET_TIME_absolute_get_duration
69                        (start).rel_value_us / 1000LL), "ops/ms");
70
71   start = GNUNET_TIME_absolute_get ();
72   for (i=0;i<10;i++)
73     GNUNET_CRYPTO_paillier_decrypt (&private_key,
74                                     &public_key,
75                                     &c1,
76                                     m1);
77   printf ("10x decryption took %s\n",
78           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
79                                                   GNUNET_YES));
80   GAUGER ("UTIL", "Paillier decryption",
81           64 * 1024 / (1 +
82                        GNUNET_TIME_absolute_get_duration
83                        (start).rel_value_us / 1000LL), "ops/ms");
84
85
86   return 0;
87 }
88
89 /* end of perf_crypto_paillier.c */