-
Notifications
You must be signed in to change notification settings - Fork 38.9k
[wip] rng: adds support for x86 rdrand/rdseed instructions when building using MSVC #22158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Copyright (c) 2011-2019 The Bitcoin Core developers | ||
| // Distributed under the MIT software license, see the accompanying | ||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
| // | ||
| #include <test/util/setup_common.h> | ||
| #include <boost/test/unit_test.hpp> | ||
|
|
||
| #include <random.cpp> | ||
|
|
||
| using namespace std; | ||
|
|
||
| BOOST_FIXTURE_TEST_SUITE(hwrand_tests, BasicTestingSetup) | ||
|
|
||
| BOOST_AUTO_TEST_CASE(rdx86seedhwslow_test) | ||
| { | ||
| const size_t DIGEST_LEN = 512/8; // 512 bits = 64 bytes | ||
|
|
||
| unsigned char emptyoutput[DIGEST_LEN]; | ||
| memset(emptyoutput, 0, DIGEST_LEN); | ||
| CSHA512 emptyhasher; | ||
| emptyhasher.Finalize(emptyoutput); | ||
|
|
||
| for (int i = 0; i < 5; i++) | ||
| { | ||
| CSHA512 hasher; | ||
| unsigned char output[DIGEST_LEN]; | ||
| memset(output, 0, DIGEST_LEN); | ||
|
|
||
| SeedHardwareSlow(hasher); | ||
| hasher.Finalize(output); | ||
|
|
||
| #if defined(__x86_64__) || defined(__amd64__) || defined(__i386__) || (defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))) | ||
| bool isNotEqual = (memcmp(output, emptyoutput, DIGEST_LEN) != 0); | ||
EthanHeilman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (g_rdseed_supported) | ||
| { | ||
| BOOST_CHECK(isNotEqual); | ||
| } | ||
| else | ||
| { | ||
| BOOST_CHECK(!isNotEqual); | ||
| } | ||
| #else | ||
| // RDRand not supported on this platform so the hasher should have no data written to it | ||
| bool isEqual = (memcmp(output, emptyoutput, DIGEST_LEN) == 0); | ||
| BOOST_CHECK(isEqual); | ||
| #endif | ||
| } | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_CASE(rdx86seedhwfast_test) | ||
| { | ||
| const size_t DIGEST_LEN = 512/8; // 512 bits = 64 bytes | ||
|
|
||
| unsigned char emptyoutput[DIGEST_LEN]; | ||
| memset(emptyoutput, 0, DIGEST_LEN); | ||
| CSHA512 emptyhasher; | ||
| emptyhasher.Finalize(emptyoutput); | ||
|
|
||
| for (int i = 0; i < 5; i++) | ||
| { | ||
| CSHA512 hasher; | ||
| unsigned char output[DIGEST_LEN]; | ||
| memset(output, 0, DIGEST_LEN); | ||
|
|
||
| SeedHardwareFast(hasher); | ||
| hasher.Finalize(output); | ||
|
|
||
| #if defined(__x86_64__) || defined(__amd64__) || defined(__i386__) || (defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))) | ||
| bool isNotEqual = (memcmp(output, emptyoutput, DIGEST_LEN) != 0); | ||
| if (g_rdrand_supported) | ||
| { | ||
| BOOST_CHECK(isNotEqual); | ||
| } | ||
| else | ||
| { | ||
| BOOST_CHECK(!isNotEqual); | ||
| } | ||
| #else | ||
| // RDRand not supported on this platform so the hasher should have no data written to it | ||
| bool isEqual = (memcmp(output, emptyoutput, DIGEST_LEN) == 0); | ||
| BOOST_CHECK(isEqual); | ||
| #endif | ||
| } | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_SUITE_END() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really care about x86 32 bit here? We don't build binaries for it anymore. I'm also not sure if there are actually existing x86 32-bit CPUs that support these instructions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have 32-bit x86 support only because bitcoin currently supports 32-bit x86 rd instructions and I am just extending what already exists to the MSVC compiler. If it is decided that bitcoin-core will no longer target 32-bit x86 than it seems reasonable to remove.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can put together a PR which removes 32-bit rdrand and rdseed support if that is something you think would be mergeable.