perf_crypto_paillier.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2014 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  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. int
  25. main (int argc, char *argv[])
  26. {
  27. struct GNUNET_TIME_Absolute start;
  28. struct GNUNET_CRYPTO_PaillierPublicKey public_key;
  29. struct GNUNET_CRYPTO_PaillierPrivateKey private_key;
  30. struct GNUNET_CRYPTO_PaillierCiphertext c1;
  31. gcry_mpi_t m1;
  32. unsigned int i;
  33. start = GNUNET_TIME_absolute_get ();
  34. for (i = 0; i < 10; i++)
  35. GNUNET_CRYPTO_paillier_create (&public_key,
  36. &private_key);
  37. printf ("10x key generation took %s\n",
  38. GNUNET_STRINGS_relative_time_to_string (
  39. GNUNET_TIME_absolute_get_duration (start),
  40. GNUNET_YES));
  41. GAUGER ("UTIL", "Paillier key generation",
  42. 64 * 1024 / (1
  43. + GNUNET_TIME_absolute_get_duration
  44. (start).rel_value_us / 1000LL), "keys/ms");
  45. m1 = gcry_mpi_new (0);
  46. m1 = gcry_mpi_set_ui (m1, 1);
  47. /* m1 = m1 * 2 ^ (GCPB - 3) */
  48. gcry_mpi_mul_2exp (m1,
  49. m1,
  50. GNUNET_CRYPTO_PAILLIER_BITS - 3);
  51. start = GNUNET_TIME_absolute_get ();
  52. for (i = 0; i < 10; i++)
  53. GNUNET_CRYPTO_paillier_encrypt (&public_key,
  54. m1,
  55. 2,
  56. &c1);
  57. printf ("10x encryption took %s\n",
  58. GNUNET_STRINGS_relative_time_to_string (
  59. GNUNET_TIME_absolute_get_duration (start),
  60. GNUNET_YES));
  61. GAUGER ("UTIL", "Paillier encryption",
  62. 64 * 1024 / (1
  63. + GNUNET_TIME_absolute_get_duration
  64. (start).rel_value_us / 1000LL), "ops/ms");
  65. start = GNUNET_TIME_absolute_get ();
  66. for (i = 0; i < 10; i++)
  67. GNUNET_CRYPTO_paillier_decrypt (&private_key,
  68. &public_key,
  69. &c1,
  70. m1);
  71. printf ("10x decryption took %s\n",
  72. GNUNET_STRINGS_relative_time_to_string (
  73. GNUNET_TIME_absolute_get_duration (start),
  74. GNUNET_YES));
  75. GAUGER ("UTIL", "Paillier decryption",
  76. 64 * 1024 / (1
  77. + GNUNET_TIME_absolute_get_duration
  78. (start).rel_value_us / 1000LL), "ops/ms");
  79. return 0;
  80. }
  81. /* end of perf_crypto_paillier.c */