-
Notifications
You must be signed in to change notification settings - Fork 276
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
Implemented rdrand and rdseed intrinsics #326
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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,80 @@ | ||
//! RDRAND and RDSEED instructions for returning random numbers from an Intel | ||
//! on-chip hardware random number generator which has been seeded by an on-chip | ||
//! entropy source. | ||
extern "platform-intrinsic" { | ||
fn x86_rdrand16_step() -> (u16, i32); | ||
fn x86_rdrand32_step() -> (u32, i32); | ||
fn x86_rdrand64_step() -> (u64, i32); | ||
fn x86_rdseed16_step() -> (u16, i32); | ||
fn x86_rdseed32_step() -> (u32, i32); | ||
fn x86_rdseed64_step() -> (u64, i32); | ||
} | ||
|
||
#[cfg(test)] | ||
use stdsimd_test::assert_instr; | ||
|
||
/// Read a hardware generated 16-bit random value and store the result in val. | ||
/// Return 1 if a random value was generated, and 0 otherwise. | ||
#[inline] | ||
#[target_feature(enable = "rdrand")] | ||
#[cfg_attr(test, assert_instr(rdrand))] | ||
pub unsafe fn _rdrand16_step(val: &mut u16) -> i32 { | ||
let (v, flag) = x86_rdrand16_step(); | ||
*val = v; | ||
flag | ||
} | ||
|
||
/// Read a hardware generated 32-bit random value and store the result in val. | ||
/// Return 1 if a random value was generated, and 0 otherwise. | ||
#[inline] | ||
#[target_feature(enable = "rdrand")] | ||
#[cfg_attr(test, assert_instr(rdrand))] | ||
pub unsafe fn _rdrand32_step(val: &mut u32) -> i32 { | ||
let (v, flag) = x86_rdrand32_step(); | ||
*val = v; | ||
flag | ||
} | ||
|
||
/// Read a hardware generated 64-bit random value and store the result in val. | ||
/// Return 1 if a random value was generated, and 0 otherwise. | ||
#[inline] | ||
#[target_feature(enable = "rdrand")] | ||
#[cfg_attr(test, assert_instr(rdrand))] | ||
pub unsafe fn _rdrand64_step(val: &mut u64) -> i32 { | ||
let (v, flag) = x86_rdrand64_step(); | ||
*val = v; | ||
flag | ||
} | ||
|
||
/// Read a 16-bit NIST SP800-90B and SP800-90C compliant random value and store | ||
/// in val. Return 1 if a random value was generated, and 0 otherwise. | ||
#[inline] | ||
#[target_feature(enable = "rdseed")] | ||
#[cfg_attr(test, assert_instr(rdseed))] | ||
pub unsafe fn _rdseed16_step(val: &mut u16) -> i32 { | ||
let (v, flag) = x86_rdseed16_step(); | ||
*val = v; | ||
flag | ||
} | ||
|
||
/// Read a 32-bit NIST SP800-90B and SP800-90C compliant random value and store | ||
/// in val. Return 1 if a random value was generated, and 0 otherwise. | ||
#[inline] | ||
#[target_feature(enable = "rdseed")] | ||
#[cfg_attr(test, assert_instr(rdseed))] | ||
pub unsafe fn _rdseed32_step(val: &mut u32) -> i32 { | ||
let (v, flag) = x86_rdseed32_step(); | ||
*val = v; | ||
flag | ||
} | ||
|
||
/// Read a 64-bit NIST SP800-90B and SP800-90C compliant random value and store | ||
/// in val. Return 1 if a random value was generated, and 0 otherwise. | ||
#[inline] | ||
#[target_feature(enable = "rdseed")] | ||
#[cfg_attr(test, assert_instr(rdseed))] | ||
pub unsafe fn _rdseed64_step(val: &mut u64) -> i32 { | ||
let (v, flag) = x86_rdseed64_step(); | ||
*val = v; | ||
flag | ||
} |
This file contains 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
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.
We don't use
platform-intrinsic
in this crate. Can you link to the LLVM intrinsics directly?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.
No, as was discussed in the issue LLVM type {I<width>, i32} can not be currently represented by Rust.
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'm not sure that means we want to start using platform-intrinsics here.
We should probably wait for @alexcrichton to review this and help decide how to proceed.
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.
He suggested to use
asm!
block as a temporary solution, so I think platform-intrinsics will be an ok temporary solution as well.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.
Ah yeah the platform intrinsic ABI here is the way to go for now. This may change in the future, but we can always just update the crate!
In any case this PR looks good to me, just waiting on the rust-lang/rust PR!