From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id E1BB23858D39; Tue, 19 Oct 2021 16:31:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1BB23858D39 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-4515] libstdc++: Implement std::random_device::entropy() for other sources X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 3cfbe5dc08b574bccc398256946cc03e2a767329 X-Git-Newrev: 58f339fc5eaae7db9526f81ab91f282ad4a9b8cc Message-Id: <20211019163132.E1BB23858D39@sourceware.org> Date: Tue, 19 Oct 2021 16:31:32 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 19 Oct 2021 16:31:33 -0000 https://gcc.gnu.org/g:58f339fc5eaae7db9526f81ab91f282ad4a9b8cc commit r12-4515-g58f339fc5eaae7db9526f81ab91f282ad4a9b8cc Author: Jonathan Wakely Date: Tue Oct 19 12:31:06 2021 +0100 libstdc++: Implement std::random_device::entropy() for other sources Currently this function only returns a non-zero value for /dev/random and /dev/urandom. When a hardware instruction such as RDRAND is in use it should (in theory) be perfectly random and produce 32 bits of entropy in each 32-bit result. Add a helper function to identify the source of randomness from the _M_func and _M_file data members, and return a suitable value when RDRAND or RDSEED is being used. libstdc++-v3/ChangeLog: * src/c++11/random.cc (which_source): New helper function. (random_device::_M_getentropy()): Use which_source and return suitable values for sources other than device files. * testsuite/26_numerics/random/random_device/entropy.cc: New test. Diff: --- libstdc++-v3/src/c++11/random.cc | 70 +++++++++++++++++++--- .../26_numerics/random/random_device/entropy.cc | 37 ++++++++++++ 2 files changed, 100 insertions(+), 7 deletions(-) diff --git a/libstdc++-v3/src/c++11/random.cc b/libstdc++-v3/src/c++11/random.cc index 44b9f30e4a9..4b64bde00ea 100644 --- a/libstdc++-v3/src/c++11/random.cc +++ b/libstdc++-v3/src/c++11/random.cc @@ -192,6 +192,51 @@ namespace std _GLIBCXX_VISIBILITY(default) return lcg(); } #endif + + enum Which { + rand_s = 1, rdseed = 2, rdrand = 4, device_file = 8, prng = 16, + any = 0xffff + }; + + inline Which + which_source(random_device::result_type (*func [[maybe_unused]])(void*), + void* file [[maybe_unused]]) + { +#ifdef _GLIBCXX_USE_CRT_RAND_S + if (func == &__winxp_rand_s) + return rand_s; +#endif + +#ifdef USE_RDSEED +#ifdef USE_RDRAND + if (func == &__x86_rdseed_rdrand) + return rdseed; +#endif + if (func == &__x86_rdseed) + return rdseed; +#endif + +#ifdef USE_RDRAND + if (func == &__x86_rdrand) + return rdrand; +#endif + +#ifdef _GLIBCXX_USE_DEV_RANDOM + if (file != nullptr) + return device_file; +#endif + +#ifdef USE_LCG + if (func == &__lcg) + return prng; +#endif + +#ifdef USE_MT19937 + return prng; +#endif + + return any; // should be unreachable + } } void @@ -209,10 +254,7 @@ namespace std _GLIBCXX_VISIBILITY(default) const char* fname [[gnu::unused]] = nullptr; - enum { - rand_s = 1, rdseed = 2, rdrand = 4, device_file = 8, prng = 16, - any = 0xffff - } which; + Which which; if (token == "default") { @@ -449,10 +491,25 @@ namespace std _GLIBCXX_VISIBILITY(default) double random_device::_M_getentropy() const noexcept { + const int max = sizeof(result_type) * __CHAR_BIT__; + + switch(which_source(_M_func, _M_file)) + { + case rdrand: + case rdseed: + return (double) max; + case rand_s: + case prng: + return 0.0; + case device_file: + // handled below + break; + default: + return 0.0; + } + #if defined _GLIBCXX_USE_DEV_RANDOM \ && defined _GLIBCXX_HAVE_SYS_IOCTL_H && defined RNDGETENTCNT - if (!_M_file) - return 0.0; #ifdef USE_POSIX_FILE_IO const int fd = _M_fd; @@ -469,7 +526,6 @@ namespace std _GLIBCXX_VISIBILITY(default) if (ent < 0) return 0.0; - const int max = sizeof(result_type) * __CHAR_BIT__; if (ent > max) ent = max; diff --git a/libstdc++-v3/testsuite/26_numerics/random/random_device/entropy.cc b/libstdc++-v3/testsuite/26_numerics/random/random_device/entropy.cc new file mode 100644 index 00000000000..9ef1538d2bb --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/random/random_device/entropy.cc @@ -0,0 +1,37 @@ +// { dg-do run { target c++11 } } + +#include +#include +#include + +void +test01() +{ + for (auto token : { "mt19937", "prng", "rand_s" }) + if (__gnu_test::random_device_available(token)) + VERIFY( std::random_device(token).entropy() == 0.0 ); + + using result_type = std::random_device::result_type; + const double max = std::log2(std::numeric_limits::max() + 1.0); + + for (auto token : { "/dev/random", "/dev/urandom" }) + if (__gnu_test::random_device_available(token)) + { + const double entropy = std::random_device(token).entropy(); + VERIFY( entropy >= 0.0 ); + VERIFY( entropy <= max ); + } + + for (auto token : { "rdrand", "rdseed" }) + if (__gnu_test::random_device_available(token)) + { + const double entropy = std::random_device(token).entropy(); + VERIFY( entropy == max ); + } +} + +int +main() +{ + test01(); +}