-
-
Notifications
You must be signed in to change notification settings - Fork 461
async rand_core trait #1155
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
Comments
I think it makes sense to add such a trait to There are some open questions about how this would interact with the existing traits. For instance: Can we implement |
One other question is if
It might actually make more sense have an async version of |
So, will there be direct interoperability between async and synchronous RNGs? If so this may make sense; if not a dedicated crate may be preferable(?).
How? Technically, yes, by spinning until The reverse, implementing Note that if we do this, a type cannot directly support both async and sync usage. But if we don't, an adapter is required to use a sync RNG in an async function; this is probably fine, thus it may be better not to have any auto impl.
Is your point that Or is it simply that derived RNGs might implement both Another question: should |
This is a design decision, but personally I would keep these separate. The choice to
This would be a good fit for the hardware RNG I am currently working with. Hopefully other embedded users can comment on what the ideal trait would be.
I should explain the cycle counts a bit more; the question asked in the embedded-rust matrix chat was if async RNG traits make sense at all. Polling is faster than async if the time to switch context is longer than polling hardware for completion. Sidetracking for a moment; the STM32WL hardware is quite fast as compared to software algorithms.
That being said there are still valid reasons to use software random number generation when hardware acceleration is available:
|
So, my current understanding from the above:
|
I can add a fourth reason to the above list to prefer software RNGs: verifiability. Hardware RNGs are much harder to scrutinize than software RNGs, and we have seen failures in RDRAND. To the main topic here: This requires no interaction with the rest of It would be easy enough to adapt a synchronous Presumably the interest in having an In the mean-time, I'm closing this issue. |
Background
The
embedded-hal
crate provides traits for embedded hardware. In #291embedded-hal
removed their RNG trait in favor ofrand_core
.embedded-hal
is now working on addingasync
traits using GATs in #285.This issue is really more of a question than a feature request: Is
rand_core
the appropriate place to addasync
RNG traits? If there are use-cases forasync
RNG traits outside of embedded rust I think it would be a good idea to include this inrand_core
, otherwise it would probably be more appropriate forembedded-hal
.What is your motivation?
Developing
async
on embedded targets has been progressing nicely, and with GAT stabilization on the way it will soon be possible to write#![no_std]
friendlyasync
traits on rust stable.HW RNG peripherals are fast, but can still benefit from
async
methods that allow the CPU to yield to other tasks while waiting for the hardware to generate entropy. On the STM32WL (ARM Cortex-M4), generating a[u32; 4]
block of entropy takes ~2662 CPU cycles on a cold-boot, and ~412 cycles when a steady state is reached. Anasync
context switch takes a minimum of 62 cycles using the embassy executor.What type of application is this?
Hardware entropy generation for embedded development on
#![no_std]
targets.Feature request
Add an
async
RNG trait.This would look something like this (prior art from embassy-rs):
The text was updated successfully, but these errors were encountered: