diff --git a/src/Makefile.test.include b/src/Makefile.test.include --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -65,6 +65,8 @@ test/jsonutil.cpp \ test/jsonutil.h \ test/key_tests.cpp \ + test/lcg_tests.cpp \ + test/lcg_tests.h \ test/limitedmap_tests.cpp \ test/main_tests.cpp \ test/mempool_tests.cpp \ diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -82,6 +82,7 @@ inv_tests.cpp jsonutil.cpp key_tests.cpp + lcg_tests.cpp limitedmap_tests.cpp main_tests.cpp mempool_tests.cpp diff --git a/src/test/lcg.h b/src/test/lcg.h new file mode 100644 --- /dev/null +++ b/src/test/lcg.h @@ -0,0 +1,33 @@ +// Copyright (c) 2019 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_TEST_LCH_H +#define BITCOIN_TEST_LCH_H + +#include + +// Simple 32-bit linear congruential generator with 64-bit internal state, +// often called as "MMIX by Donald Knuth". Knuth attributes the multiplier +// to C. E. Haynes and the increment is not crucial (it only need be odd). +// Knuth, Donald (1997). Seminumerical Algorithms Vol2. Sec 3.3.4. 3rd Ed. +// +// Low bits have short period, hence we use high bits which should have +// the same period as the entire generator (2^64). +class MMIXLinearCongruentialGenerator { + uint64_t state; + +public: + MMIXLinearCongruentialGenerator(uint64_t initialstate = 0) + : state(initialstate) {} + + // pseudorandom, except the first value returned is the seed provided + // (thus starting with 0, by default) + uint32_t next() { + uint32_t ret = state >> 32; + state = state * 6364136223846793005 + 1442695040888963407; + return ret; + } +}; + +#endif diff --git a/src/test/lcg_tests.cpp b/src/test/lcg_tests.cpp new file mode 100644 --- /dev/null +++ b/src/test/lcg_tests.cpp @@ -0,0 +1,47 @@ +// Copyright (c) 2019 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "test/lcg.h" + +#include + +BOOST_AUTO_TEST_CASE(lcg_tests) { + { + MMIXLinearCongruentialGenerator lcg; + // We want that the first iteration is 0 which is a helpful special + // case. + BOOST_CHECK_EQUAL(lcg.next(), 0x00000000); + for (int i = 0; i < 99; i++) { + lcg.next(); + } + // Make sure the LCG is producing expected value after many iterations. + // This ensures mul and add overflows are acting as expected on this + // architecture. + BOOST_CHECK_EQUAL(lcg.next(), 0xf306b780); + } + { + MMIXLinearCongruentialGenerator lcg(42); + // We this also should make first iteration as 0. + BOOST_CHECK_EQUAL(lcg.next(), 0x00000000); + for (int i = 0; i < 99; i++) { + lcg.next(); + } + // Make sure the LCG is producing expected value after many iterations. + // This ensures mul and add overflows are acting as expected on this + // architecture. + BOOST_CHECK_EQUAL(lcg.next(), 0x3b96faf3); + } + { + // just some big seed + MMIXLinearCongruentialGenerator lcg(0xdeadbeef00000000); + BOOST_CHECK_EQUAL(lcg.next(), 0xdeadbeef); + for (int i = 0; i < 99; i++) { + lcg.next(); + } + // Make sure the LCG is producing expected value after many iterations. + // This ensures mul and add overflows are acting as expected on this + // architecture. + BOOST_CHECK_EQUAL(lcg.next(), 0x6b00b1df); + } +}