From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Implement std::random_device::entropy() for other sources
Date: Tue, 19 Oct 2021 17:31:45 +0100 [thread overview]
Message-ID: <YW7y8dUoCz/fzjn6@redhat.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 723 bytes --]
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.
Tested powerpc64le-linux. Committed to trunk.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 4528 bytes --]
commit 58f339fc5eaae7db9526f81ab91f282ad4a9b8cc
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Tue Oct 19 12:31:06 2021
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 --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 <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();
+}
reply other threads:[~2021-10-19 16:31 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YW7y8dUoCz/fzjn6@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).