rng-tests.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. cd ~
  3. wget http://simul.iro.umontreal.ca/testu01/TestU01.zip
  4. unzip TestU01.zip
  5. mkdir TestU01
  6. cd TestU01-1.2.3
  7. ./configure --prefix=`pwd`/../TestU01
  8. make -j8
  9. make install
  10. cd ~/RandomX
  11. g++ -O3 src/tests/rng-tests.cpp -lm -I ~/TestU01/include -L ~/TestU01/lib -L bin/ -l:libtestu01.a -l:libmylib.a -l:libprobdist.a -lrandomx -o bin/rng-tests -DRANDOMX_GEN=4R -DRANDOMX_TESTU01=Crush
  12. bin/rng-tests 0
  13. */
  14. extern "C" {
  15. #include "unif01.h"
  16. #include "bbattery.h"
  17. }
  18. #include "../aes_hash.hpp"
  19. #include "../blake2/blake2.h"
  20. #include "utility.hpp"
  21. #include <cstdint>
  22. #ifndef RANDOMX_GEN
  23. #error Please define RANDOMX_GEN with a value of 1R or 4R
  24. #endif
  25. #ifndef RANDOMX_TESTU01
  26. #error Please define RANDOMX_TESTU01 with a value of SmallCrush, Crush or BigCrush
  27. #endif
  28. #define STR(x) #x
  29. #define CONCAT(a,b,c) a ## b ## c
  30. #define GEN_NAME(x) "AesGenerator" STR(x)
  31. #define GEN_FUNC(x) CONCAT(fillAes, x, x4)
  32. #define TEST_SUITE(x) CONCAT(bbattery_, x,)
  33. constexpr int GeneratorStateSize = 64;
  34. constexpr int GeneratorCapacity = GeneratorStateSize / sizeof(uint32_t);
  35. static unsigned long aesGenBits(void *param, void *state) {
  36. uint32_t* statePtr = (uint32_t*)state;
  37. int* indexPtr = (int*)param;
  38. int stateIndex = *indexPtr;
  39. if(stateIndex >= GeneratorCapacity) {
  40. GEN_FUNC(RANDOMX_GEN)<false>(statePtr, GeneratorStateSize, statePtr);
  41. stateIndex = 0;
  42. }
  43. uint32_t next = statePtr[stateIndex];
  44. *indexPtr = stateIndex + 1;
  45. return next;
  46. }
  47. static double aesGenDouble(void *param, void *state) {
  48. return aesGenBits (param, state) / unif01_NORM32;
  49. }
  50. static void aesWriteState(void* state) {
  51. char* statePtr = (char*)state;
  52. for(int i = 0; i < 4; ++i) {
  53. std::cout << "state" << i << " = ";
  54. outputHex(std::cout, statePtr + (i * 16), 16);
  55. std::cout << std::endl;
  56. }
  57. }
  58. int main(int argc, char** argv) {
  59. if (argc != 2) {
  60. std::cout << argv[0] << " <seed>" << std::endl;
  61. return 1;
  62. }
  63. uint32_t state[GeneratorCapacity] = { 0 };
  64. int stateIndex = GeneratorCapacity;
  65. char name[] = GEN_NAME(RANDOMX_GEN);
  66. uint64_t seed = strtoull(argv[1], nullptr, 0);
  67. if(seed) {
  68. blake2b(&state, sizeof(state), &seed, sizeof(seed), nullptr, 0);
  69. }
  70. unif01_Gen gen;
  71. gen.state = &state;
  72. gen.param = &stateIndex;
  73. gen.Write = &aesWriteState;
  74. gen.GetU01 = &aesGenDouble;
  75. gen.GetBits = &aesGenBits;
  76. gen.name = (char*)name;
  77. gen.Write(gen.state);
  78. std::cout << std::endl;
  79. TEST_SUITE(RANDOMX_TESTU01)(&gen);
  80. return 0;
  81. }