#[non_exhaustive]pub struct Trng { /* private fields */ }unstable only.Expand description
True Random Number Generator (TRNG)
The Trng struct represents a true random number generator that combines
the randomness from the hardware RNG and an ADC. This struct provides
methods to generate random numbers and fill buffers with random bytes.
Due to pulling the entropy source from the ADC, it uses the associated
registers, so to use TRNG we need to “occupy” the ADC peripheral.
To generate true random numbers, an instance of TrngSource is required. Once created, you
can create Trng instances at any time, as long as the TrngSource is alive.
§Example
use esp_hal::rng::{Trng, TrngSource};
let mut buf = [0u8; 16];
// ADC is not available from now
let trng_source = TrngSource::new(peripherals.RNG, peripherals.ADC1.reborrow());
let trng = Trng::try_new()?;
// Generate true random numbers
trng.read(&mut buf);
let true_random_number = trng.random();
// Downgrade to Rng to allow disabling the TrngSource
let rng = trng.downgrade();
// Drop the true random number source. ADC is available now.
core::mem::drop(trng_source);
let mut adc1_config = AdcConfig::new();
let mut adc1_pin = adc1_config.enable_pin(peripherals.GPIO3, Attenuation::_11dB);
let mut adc1 = Adc::new(peripherals.ADC1, adc1_config);
let pin_value = adc1.read_oneshot(&mut adc1_pin)?;
// Now we can only generate pseudo-random numbers...
rng.read(&mut buf);
let pseudo_random_number = rng.random();
// ... but the ADC is available for use.
let pin_value: u16 = adc1.read_oneshot(&mut adc1_pin)?;§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Implementations§
Source§impl Trng
impl Trng
Sourcepub fn try_new() -> Result<Self, TrngError>
pub fn try_new() -> Result<Self, TrngError>
Attempts to create a new True Random Number Generator (TRNG) instance.
This function returns a new Trng instance on success, or an error if the
TrngSource is not active.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Sourcepub fn random(&self) -> u32
pub fn random(&self) -> u32
Returns a new, random u32 integer.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Trait Implementations§
Source§impl RngCore for Trng
Compatibility with rand_core 0.6. Documentation can be found at
https://docs.rs/rand_core/0.6.4/rand_core/trait.RngCore.html.
impl RngCore for Trng
Compatibility with rand_core 0.6. Documentation can be found at
https://docs.rs/rand_core/0.6.4/rand_core/trait.RngCore.html.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Source§fn fill_bytes(&mut self, dest: &mut [u8])
fn fill_bytes(&mut self, dest: &mut [u8])
dest with random data. Read moreSource§fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>
dest entirely with random data. Read moreSource§impl RngCore for Trng
Compatibility with rand_core 0.9. Documentation can be found at
https://docs.rs/rand_core/0.9.5/rand_core/trait.RngCore.html.
impl RngCore for Trng
Compatibility with rand_core 0.9. Documentation can be found at
https://docs.rs/rand_core/0.9.5/rand_core/trait.RngCore.html.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Source§impl TryRng for Trng
Compatibility with rand_core 0.10
impl TryRng for Trng
Compatibility with rand_core 0.10
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Source§fn try_next_u32(&mut self) -> Result<u32, Self::Error>
fn try_next_u32(&mut self) -> Result<u32, Self::Error>
u32.Source§fn try_next_u64(&mut self) -> Result<u64, Self::Error>
fn try_next_u64(&mut self) -> Result<u64, Self::Error>
u64.Source§fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error>
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error>
dst entirely with random data.impl CryptoRng for Trng
Compatibility with rand_core 0.6. Documentation can be found at
https://docs.rs/rand_core/0.6.4/rand_core/trait.CryptoRng.html.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
impl CryptoRng for Trng
Compatibility with rand_core 0.9. Documentation can be found at
https://docs.rs/rand_core/0.9.5/rand_core/trait.CryptoRng.html.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
impl TryCryptoRng for Trng
Compatibility with rand_core 0.10
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Auto Trait Implementations§
impl Freeze for Trng
impl RefUnwindSafe for Trng
impl Send for Trng
impl Sync for Trng
impl Unpin for Trng
impl UnsafeUnpin for Trng
impl UnwindSafe for Trng
Blanket Implementations§
Source§impl<R> TryRngCore for Rwhere
R: TryRng,
impl<R> TryRngCore for Rwhere
R: TryRng,
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)Source§impl<T> CryptoRngCore for T
impl<T> CryptoRngCore for T
Source§fn as_rngcore(&mut self) -> &mut dyn RngCore
fn as_rngcore(&mut self) -> &mut dyn RngCore
RngCore trait object.§impl<T, U> Into<U> for Twhere
U: From<T>,
impl<T, U> Into<U> for Twhere
U: From<T>,
§impl<T, U> TryFrom<U> for Twhere
U: Into<T>,
impl<T, U> TryFrom<U> for Twhere
U: Into<T>,
§impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,
impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,
Source§impl<R> TryRngCore for Rwhere
R: RngCore + ?Sized,
impl<R> TryRngCore for Rwhere
R: RngCore + ?Sized,
Source§fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
u32.Source§fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
u64.Source§fn try_fill_bytes(
&mut self,
dst: &mut [u8],
) -> Result<(), <R as TryRngCore>::Error>
fn try_fill_bytes( &mut self, dst: &mut [u8], ) -> Result<(), <R as TryRngCore>::Error>
dest entirely with random data.Source§fn unwrap_err(self) -> UnwrapErr<Self>where
Self: Sized,
fn unwrap_err(self) -> UnwrapErr<Self>where
Self: Sized,
UnwrapErr wrapper.Source§fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
UnwrapMut wrapper.