Cache.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Copyright (c) 2018 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #include <cstring>
  16. #include "Cache.hpp"
  17. #include "argon2.h"
  18. #include "argon2_core.h"
  19. namespace RandomX {
  20. static_assert(RANDOMX_ARGON_MEMORY % (RANDOMX_ARGON_LANES * ARGON2_SYNC_POINTS) == 0, "RANDOMX_ARGON_MEMORY - invalid value");
  21. static_assert(RANDOMX_ARGON_GROWTH % (RANDOMX_ARGON_LANES * ARGON2_SYNC_POINTS) == 0, "RANDOMX_ARGON_GROWTH - invalid value");
  22. void argonFill(Cache& cache, const void* seed, size_t seedSize) {
  23. uint32_t memory_blocks, segment_length;
  24. argon2_instance_t instance;
  25. argon2_context context;
  26. context.out = nullptr;
  27. context.outlen = 0;
  28. context.pwd = CONST_CAST(uint8_t *)seed;
  29. context.pwdlen = (uint32_t)seedSize;
  30. context.salt = CONST_CAST(uint8_t *)RANDOMX_ARGON_SALT;
  31. context.saltlen = (uint32_t)ArgonSaltSize;
  32. context.secret = NULL;
  33. context.secretlen = 0;
  34. context.ad = NULL;
  35. context.adlen = 0;
  36. context.t_cost = RANDOMX_ARGON_ITERATIONS;
  37. context.m_cost = cache.size / ArgonBlockSize;
  38. context.lanes = RANDOMX_ARGON_LANES;
  39. context.threads = 1;
  40. context.allocate_cbk = NULL;
  41. context.free_cbk = NULL;
  42. context.flags = ARGON2_DEFAULT_FLAGS;
  43. context.version = ARGON2_VERSION_NUMBER;
  44. /* 2. Align memory size */
  45. /* Minimum memory_blocks = 8L blocks, where L is the number of lanes */
  46. memory_blocks = context.m_cost;
  47. segment_length = memory_blocks / (context.lanes * ARGON2_SYNC_POINTS);
  48. instance.version = context.version;
  49. instance.memory = NULL;
  50. instance.passes = context.t_cost;
  51. instance.memory_blocks = memory_blocks;
  52. instance.segment_length = segment_length;
  53. instance.lane_length = segment_length * ARGON2_SYNC_POINTS;
  54. instance.lanes = context.lanes;
  55. instance.threads = context.threads;
  56. instance.type = Argon2_d;
  57. instance.memory = (block*)cache.memory;
  58. if (instance.threads > instance.lanes) {
  59. instance.threads = instance.lanes;
  60. }
  61. /* 3. Initialization: Hashing inputs, allocating memory, filling first
  62. * blocks
  63. */
  64. argon_initialize(&instance, &context);
  65. fill_memory_blocks(&instance);
  66. }
  67. }