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.
This commit is contained in:
parent
3cfbe5dc08
commit
58f339fc5e
2 changed files with 100 additions and 7 deletions
|
@ -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;
|
||||
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
// { dg-do run { target c++11 } }
|
||||
|
||||
#include <random>
|
||||
#include <testsuite_hooks.h>
|
||||
#include <testsuite_random.h>
|
||||
|
||||
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<result_type>::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();
|
||||
}
|
Loading…
Add table
Reference in a new issue