perf_kdf.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2002, 2003, 2004, 2006, 2013 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 nse/perf_kdf.c
  19. * @brief measure performance of KDF hash function
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include <gcrypt.h>
  24. #include <gauger.h>
  25. /**
  26. * Calculate the 'proof-of-work' hash (an expensive hash).
  27. *
  28. * @param buf data to hash
  29. * @param buf_len number of bytes in 'buf'
  30. * @param result where to write the resulting hash
  31. */
  32. static void
  33. pow_hash (const void *buf,
  34. size_t buf_len,
  35. struct GNUNET_HashCode *result)
  36. {
  37. GNUNET_break (0 ==
  38. gcry_kdf_derive (buf, buf_len,
  39. GCRY_KDF_SCRYPT,
  40. 1 /* subalgo */,
  41. "gnunet-proof-of-work", strlen ("gnunet-proof-of-work"),
  42. 2 /* iterations; keep cost of individual op small */,
  43. sizeof (struct GNUNET_HashCode), result));
  44. }
  45. static void
  46. perfHash ()
  47. {
  48. struct GNUNET_HashCode hc;
  49. unsigned int i;
  50. char buf[64];
  51. memset (buf, 1, sizeof (buf));
  52. for (i = 0; i < 1024; i++)
  53. pow_hash (buf, sizeof (buf), &hc);
  54. }
  55. int
  56. main (int argc, char *argv[])
  57. {
  58. struct GNUNET_TIME_Absolute start;
  59. start = GNUNET_TIME_absolute_get ();
  60. perfHash ();
  61. printf ("Hash perf took %s\n",
  62. GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
  63. GNUNET_YES));
  64. GAUGER ("NSE", "Proof-of-work hashing",
  65. 1024.0 / (1.0 +
  66. GNUNET_TIME_absolute_get_duration
  67. (start).rel_value_us / 1000.0), "hashes/ms");
  68. return 0;
  69. }
  70. /* end of perf_kdf.c */