perf_kdf.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2002, 2003, 2004, 2006, 2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @author Christian Grothoff
  19. * @file nse/perf_kdf.c
  20. * @brief measure performance of KDF hash function
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include <gcrypt.h>
  25. #include <gauger.h>
  26. /**
  27. * Calculate the 'proof-of-work' hash (an expensive hash).
  28. *
  29. * @param buf data to hash
  30. * @param buf_len number of bytes in 'buf'
  31. * @param result where to write the resulting hash
  32. */
  33. static void
  34. pow_hash (const void *buf,
  35. size_t buf_len,
  36. struct GNUNET_HashCode *result)
  37. {
  38. GNUNET_break (0 ==
  39. gcry_kdf_derive (buf, buf_len,
  40. GCRY_KDF_SCRYPT,
  41. 1 /* subalgo */,
  42. "gnunet-proof-of-work", strlen ("gnunet-proof-of-work"),
  43. 2 /* iterations; keep cost of individual op small */,
  44. sizeof (struct GNUNET_HashCode), result));
  45. }
  46. static void
  47. perfHash ()
  48. {
  49. struct GNUNET_HashCode hc;
  50. unsigned int i;
  51. char buf[64];
  52. memset (buf, 1, sizeof (buf));
  53. for (i = 0; i < 1024; i++)
  54. pow_hash (buf, sizeof (buf), &hc);
  55. }
  56. int
  57. main (int argc, char *argv[])
  58. {
  59. struct GNUNET_TIME_Absolute start;
  60. start = GNUNET_TIME_absolute_get ();
  61. perfHash ();
  62. printf ("Hash perf took %s\n",
  63. GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
  64. GNUNET_YES));
  65. GAUGER ("NSE", "Proof-of-work hashing",
  66. 1024 / (1 +
  67. GNUNET_TIME_absolute_get_duration
  68. (start).rel_value_us / 1000LL), "hashes/ms");
  69. return 0;
  70. }
  71. /* end of perf_kdf.c */