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