ethash.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. This file is part of ethash.
  3. ethash is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. ethash is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with ethash. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file ethash.h
  15. * @date 2015
  16. */
  17. #pragma once
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #include <stddef.h>
  22. #include "compiler.h"
  23. #define ETHASH_REVISION 23
  24. #define ETHASH_DATASET_BYTES_INIT 1073741824U // 2**30
  25. #define ETHASH_DATASET_BYTES_GROWTH 8388608U // 2**23
  26. #define ETHASH_CACHE_BYTES_INIT 1073741824U // 2**24
  27. #define ETHASH_CACHE_BYTES_GROWTH 131072U // 2**17
  28. #define ETHASH_EPOCH_LENGTH 30000U
  29. #define ETHASH_MIX_BYTES 128
  30. #define ETHASH_HASH_BYTES 64
  31. #define ETHASH_DATASET_PARENTS 256
  32. #define ETHASH_CACHE_ROUNDS 3
  33. #define ETHASH_ACCESSES 64
  34. #define ETHASH_DAG_MAGIC_NUM_SIZE 8
  35. #define ETHASH_DAG_MAGIC_NUM 0xFEE1DEADBADDCAFE
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /// Type of a seedhash/blockhash e.t.c.
  40. typedef struct ethash_h256 { uint8_t b[32]; } ethash_h256_t;
  41. // convenience macro to statically initialize an h256_t
  42. // usage:
  43. // ethash_h256_t a = ethash_h256_static_init(1, 2, 3, ... )
  44. // have to provide all 32 values. If you don't provide all the rest
  45. // will simply be unitialized (not guranteed to be 0)
  46. #define ethash_h256_static_init(...) \
  47. { {__VA_ARGS__} }
  48. struct ethash_light;
  49. typedef struct ethash_light* ethash_light_t;
  50. struct ethash_full;
  51. typedef struct ethash_full* ethash_full_t;
  52. typedef int(*ethash_callback_t)(unsigned);
  53. typedef struct ethash_return_value {
  54. ethash_h256_t result;
  55. ethash_h256_t mix_hash;
  56. bool success;
  57. } ethash_return_value_t;
  58. /**
  59. * Allocate and initialize a new ethash_light handler
  60. *
  61. * @param block_number The block number for which to create the handler
  62. * @return Newly allocated ethash_light handler or NULL in case of
  63. * ERRNOMEM or invalid parameters used for @ref ethash_compute_cache_nodes()
  64. */
  65. ethash_light_t ethash_light_new(uint64_t block_number);
  66. /**
  67. * Frees a previously allocated ethash_light handler
  68. * @param light The light handler to free
  69. */
  70. void ethash_light_delete(ethash_light_t light);
  71. /**
  72. * Calculate the light client data
  73. *
  74. * @param light The light client handler
  75. * @param header_hash The header hash to pack into the mix
  76. * @param nonce The nonce to pack into the mix
  77. * @return an object of ethash_return_value_t holding the return values
  78. */
  79. ethash_return_value_t ethash_light_compute(
  80. ethash_light_t light,
  81. ethash_h256_t const header_hash,
  82. uint64_t nonce
  83. );
  84. /**
  85. * Allocate and initialize a new ethash_full handler
  86. *
  87. * @param light The light handler containing the cache.
  88. * @param callback A callback function with signature of @ref ethash_callback_t
  89. * It accepts an unsigned with which a progress of DAG calculation
  90. * can be displayed. If all goes well the callback should return 0.
  91. * If a non-zero value is returned then DAG generation will stop.
  92. * Be advised. A progress value of 100 means that DAG creation is
  93. * almost complete and that this function will soon return succesfully.
  94. * It does not mean that the function has already had a succesfull return.
  95. * @return Newly allocated ethash_full handler or NULL in case of
  96. * ERRNOMEM or invalid parameters used for @ref ethash_compute_full_data()
  97. */
  98. ethash_full_t ethash_full_new(ethash_light_t light, ethash_callback_t callback);
  99. /**
  100. * Frees a previously allocated ethash_full handler
  101. * @param full The light handler to free
  102. */
  103. void ethash_full_delete(ethash_full_t full);
  104. /**
  105. * Calculate the full client data
  106. *
  107. * @param full The full client handler
  108. * @param header_hash The header hash to pack into the mix
  109. * @param nonce The nonce to pack into the mix
  110. * @return An object of ethash_return_value to hold the return value
  111. */
  112. ethash_return_value_t ethash_full_compute(
  113. ethash_full_t full,
  114. ethash_h256_t const header_hash,
  115. uint64_t nonce
  116. );
  117. /**
  118. * Get a pointer to the full DAG data
  119. */
  120. void const* ethash_full_dag(ethash_full_t full);
  121. /**
  122. * Get the size of the DAG data
  123. */
  124. uint64_t ethash_full_dag_size(ethash_full_t full);
  125. /**
  126. * Calculate the seedhash for a given block number
  127. */
  128. ethash_h256_t ethash_get_seedhash(uint64_t block_number);
  129. #ifdef __cplusplus
  130. }
  131. #endif