Struct RdRand

Source
pub struct RdRand(/* private fields */);
Expand description

A cryptographically secure statistically uniform, non-periodic and non-deterministic random bit generator.

Note that this generator may be implemented using a deterministic algorithm that is reseeded routinely from a non-deterministic entropy source to achieve the desirable properties.

This generator is a viable replacement to any generator, however, since nobody has audited this hardware implementation yet, the usual disclaimers as to their suitability apply.

It is potentially faster than OsRng, but is only supported by more recent architectures such as Intel Ivy Bridge and AMD Zen.

Implementations§

Source§

impl RdRand

Source

pub fn new() -> Result<Self, ErrorCode>

Create a new instance of the random number generator.

This constructor checks whether the CPU the program is running on supports the instruction necessary for this generator to operate. If the instruction is not supported, an error is returned.

Source

pub unsafe fn new_unchecked() -> Self

Create a new instance of the random number generator.

§Safety

This constructor is unsafe because it doesn’t check that the CPU supports the instruction, but devolves this responsibility to the caller.

Source

pub fn try_next_u16(&self) -> Result<u16, ErrorCode>

Generate a single random u16 value.

The underlying instruction may fail for variety reasons (such as actual hardware failure or exhausted entropy), however the exact reason for the failure is not usually exposed.

This method will retry calling the instruction a few times, however if all the attempts fail, it will return None.

In case Err is returned, the caller should assume that a non-recoverable failure has occured and use another random number genrator instead.

Source

pub fn try_next_u32(&self) -> Result<u32, ErrorCode>

Generate a single random u32 value.

The underlying instruction may fail for variety reasons (such as actual hardware failure or exhausted entropy), however the exact reason for the failure is not usually exposed.

This method will retry calling the instruction a few times, however if all the attempts fail, it will return None.

In case Err is returned, the caller should assume that a non-recoverable failure has occured and use another random number genrator instead.

Source

pub fn try_next_u64(&self) -> Result<u64, ErrorCode>

Generate a single random u64 value.

The underlying instruction may fail for variety reasons (such as actual hardware failure or exhausted entropy), however the exact reason for the failure is not usually exposed.

This method will retry calling the instruction a few times, however if all the attempts fail, it will return None.

In case Err is returned, the caller should assume that a non-recoverable failure has occured and use another random number genrator instead.

Note, that on 32-bit targets, there’s no underlying instruction to generate a 64-bit number, so it is emulated with the 32-bit version of the instruction.

Source

pub fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), ErrorCode>

Fill a buffer dest with random data.

This method will use the most appropriate variant of the instruction available on the machine to achieve the greatest single-core throughput, however it has a slightly higher setup cost than the plain next_u32 or next_u64 methods.

The underlying instruction may fail for variety reasons (such as actual hardware failure or exhausted entropy), however the exact reason for the failure is not usually exposed.

This method will retry calling the instruction a few times, however if all the attempts fail, it will return an error.

If an error is returned, the caller should assume that an non-recoverable hardware failure has occured and use another random number genrator instead.

Trait Implementations§

Source§

impl Clone for RdRand

Source§

fn clone(&self) -> RdRand

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl RngCore for RdRand

Source§

fn next_u32(&mut self) -> u32

Generate a single random u32 value.

The underlying instruction may fail for variety reasons (such as actual hardware failure or exhausted entropy), however the exact reason for the failure is not usually exposed.

§Panic

This method will retry calling the instruction a few times, however if all the attempts fail, it will panic.

In case panic occurs, the caller should assume that an non-recoverable hardware failure has occured and use another random number genrator instead.

Source§

fn next_u64(&mut self) -> u64

Generate a single random u64 value.

The underlying instruction may fail for variety reasons (such as actual hardware failure or exhausted entropy), however the exact reason for the failure is not usually exposed.

Note, that on 32-bit targets, there’s no underlying instruction to generate a 64-bit number, so it is emulated with the 32-bit version of the instruction.

§Panic

This method will retry calling the instruction a few times, however if all the attempts fail, it will panic.

In case panic occurs, the caller should assume that an non-recoverable hardware failure has occured and use another random number genrator instead.

Source§

fn fill_bytes(&mut self, dest: &mut [u8])

Fill a buffer dest with random data.

See try_fill_bytes for a more extensive documentation.

§Panic

This method will panic any time try_fill_bytes would return an error.

Source§

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>

Fill a buffer dest with random data.

This method will use the most appropriate variant of the instruction available on the machine to achieve the greatest single-core throughput, however it has a slightly higher setup cost than the plain next_u32 or next_u64 methods.

The underlying instruction may fail for variety reasons (such as actual hardware failure or exhausted entropy), however the exact reason for the failure is not usually exposed.

This method will retry calling the instruction a few times, however if all the attempts fail, it will return an error.

If an error is returned, the caller should assume that an non-recoverable hardware failure has occured and use another random number genrator instead.

Source§

impl Copy for RdRand

Source§

impl CryptoRng for RdRand

Auto Trait Implementations§

§

impl Freeze for RdRand

§

impl RefUnwindSafe for RdRand

§

impl Send for RdRand

§

impl Sync for RdRand

§

impl Unpin for RdRand

§

impl UnwindSafe for RdRand

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> CryptoRngCore for T
where T: CryptoRng + RngCore,

Source§

fn as_rngcore(&mut self) -> &mut dyn RngCore

Upcast to an RngCore trait object.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.