From d525f519405745c9133e112da40b954302c128c3 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Fri, 27 Aug 2021 16:10:01 -0400 Subject: [PATCH 1/6] Add a convenience next(range) to Rng. --- embassy-stm32/src/rng.rs | 20 ++++++++++++++++++++ embassy-traits/src/rng.rs | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/embassy-stm32/src/rng.rs b/embassy-stm32/src/rng.rs index 77e408d3d..e94c813de 100644 --- a/embassy-stm32/src/rng.rs +++ b/embassy-stm32/src/rng.rs @@ -126,6 +126,26 @@ impl traits::rng::Rng for Random { Ok(()) } } + + #[rustfmt::skip] + type NextFuture<'a> where Self: 'a = impl Future> + 'a; + + fn next<'a>(&'a mut self, range: u32) -> Self::NextFuture<'a> { + async move { + let t = (-(range as i32) % (range as i32)) as u32; + loop { + let mut buf = [0; 4]; + traits::rng::Rng::fill_bytes(self, &mut buf).await?; + let x = u32::from_le_bytes(buf); + let m = x as u64 * range as u64; + let l = m as u32; + if l < t { + continue; + } + return Ok((m >> 32) as u32); + } + } + } } pub(crate) mod sealed { diff --git a/embassy-traits/src/rng.rs b/embassy-traits/src/rng.rs index ddc4c20e0..320d9afc2 100644 --- a/embassy-traits/src/rng.rs +++ b/embassy-traits/src/rng.rs @@ -4,6 +4,7 @@ use core::future::Future; pub trait Rng { type Error; + #[rustfmt::skip] type RngFuture<'a>: Future> + 'a where Self: 'a; @@ -14,4 +15,11 @@ pub trait Rng { /// filling the buffer. Upon completion, the buffer will be completely /// filled or an error will have been reported. fn fill_bytes<'a>(&'a mut self, dest: &'a mut [u8]) -> Self::RngFuture<'a>; + + #[rustfmt::skip] + type NextFuture<'a>: Future> + 'a + where + Self: 'a; + + fn next<'a>(&'a mut self, range: u32) -> Self::NextFuture<'a>; } From 78f7d1b786d6b838cf80ddd0199707b0db3dc854 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Fri, 27 Aug 2021 16:10:50 -0400 Subject: [PATCH 2/6] Add example using ranged RNG. --- examples/stm32h7/src/bin/rng.rs | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/stm32h7/src/bin/rng.rs diff --git a/examples/stm32h7/src/bin/rng.rs b/examples/stm32h7/src/bin/rng.rs new file mode 100644 index 000000000..48aad26b1 --- /dev/null +++ b/examples/stm32h7/src/bin/rng.rs @@ -0,0 +1,35 @@ +#![no_std] +#![no_main] +#![feature(trait_alias)] +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +#[path = "../example_common.rs"] +mod example_common; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_stm32::Peripherals; +use embedded_hal::digital::v2::OutputPin; +use example_common::*; +use embassy_stm32::rng::Random; +use embassy::traits::rng::Rng; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); + + let mut led = Output::new(p.PB14, Level::High, Speed::Low); + + let mut rng = Random::new(p.RNG); + + loop { + info!("high {}", unwrap!(rng.next(16).await) ); + unwrap!(led.set_high()); + Timer::after(Duration::from_millis(500)).await; + + info!("low {}", unwrap!(rng.next(16).await) ); + unwrap!(led.set_low()); + Timer::after(Duration::from_millis(500)).await; + } +} From 7fa3b27cac6f647ae5e11bd04a802027fb96a03c Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Mon, 30 Aug 2021 09:55:29 -0400 Subject: [PATCH 3/6] Move random utils to another trait. --- embassy-stm32/src/rng.rs | 2 ++ embassy-traits/src/rng.rs | 8 +++++--- examples/stm32h7/src/bin/rng.rs | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/embassy-stm32/src/rng.rs b/embassy-stm32/src/rng.rs index e94c813de..305e26771 100644 --- a/embassy-stm32/src/rng.rs +++ b/embassy-stm32/src/rng.rs @@ -126,7 +126,9 @@ impl traits::rng::Rng for Random { Ok(()) } } +} +impl traits::rng::Random for Random { #[rustfmt::skip] type NextFuture<'a> where Self: 'a = impl Future> + 'a; diff --git a/embassy-traits/src/rng.rs b/embassy-traits/src/rng.rs index 320d9afc2..181faa515 100644 --- a/embassy-traits/src/rng.rs +++ b/embassy-traits/src/rng.rs @@ -5,9 +5,9 @@ pub trait Rng { type Error; #[rustfmt::skip] - type RngFuture<'a>: Future> + 'a + type RngFuture<'a>: Future > + 'a where - Self: 'a; + Self: 'a; /// Completely fill the provided buffer with random bytes. /// @@ -15,9 +15,11 @@ pub trait Rng { /// filling the buffer. Upon completion, the buffer will be completely /// filled or an error will have been reported. fn fill_bytes<'a>(&'a mut self, dest: &'a mut [u8]) -> Self::RngFuture<'a>; +} +pub trait Random: Rng { #[rustfmt::skip] - type NextFuture<'a>: Future> + 'a + type NextFuture<'a>: Future::Error>> + 'a where Self: 'a; diff --git a/examples/stm32h7/src/bin/rng.rs b/examples/stm32h7/src/bin/rng.rs index 48aad26b1..f779b2f69 100644 --- a/examples/stm32h7/src/bin/rng.rs +++ b/examples/stm32h7/src/bin/rng.rs @@ -13,7 +13,7 @@ use embassy_stm32::Peripherals; use embedded_hal::digital::v2::OutputPin; use example_common::*; use embassy_stm32::rng::Random; -use embassy::traits::rng::Rng; +use embassy::traits::rng::Random as _; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { From fd7a76c59e8a190781aea476d199bda826dd5031 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Mon, 30 Aug 2021 09:57:31 -0400 Subject: [PATCH 4/6] Formatting. Always formatting. --- examples/stm32h7/src/bin/rng.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/stm32h7/src/bin/rng.rs b/examples/stm32h7/src/bin/rng.rs index f779b2f69..fea88c410 100644 --- a/examples/stm32h7/src/bin/rng.rs +++ b/examples/stm32h7/src/bin/rng.rs @@ -8,12 +8,12 @@ mod example_common; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; +use embassy::traits::rng::Random as _; use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_stm32::rng::Random; use embassy_stm32::Peripherals; use embedded_hal::digital::v2::OutputPin; use example_common::*; -use embassy_stm32::rng::Random; -use embassy::traits::rng::Random as _; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { @@ -24,11 +24,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { let mut rng = Random::new(p.RNG); loop { - info!("high {}", unwrap!(rng.next(16).await) ); + info!("high {}", unwrap!(rng.next(16).await)); unwrap!(led.set_high()); Timer::after(Duration::from_millis(500)).await; - info!("low {}", unwrap!(rng.next(16).await) ); + info!("low {}", unwrap!(rng.next(16).await)); unwrap!(led.set_low()); Timer::after(Duration::from_millis(500)).await; } From 37ceae908b39fcf1ef3b38680cef240ed6b1e867 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Wed, 1 Sep 2021 09:39:33 -0400 Subject: [PATCH 5/6] Rename Random impl to Rng. Create Random struct providing next_x(range) for all T:Rng. --- embassy-stm32/src/rng.rs | 32 +++-------------- embassy-traits/src/rng.rs | 63 +++++++++++++++++++++++++++++---- examples/stm32h7/src/bin/eth.rs | 6 ++-- examples/stm32h7/src/bin/rng.rs | 10 +++--- 4 files changed, 69 insertions(+), 42 deletions(-) diff --git a/embassy-stm32/src/rng.rs b/embassy-stm32/src/rng.rs index 305e26771..0afba3ba7 100644 --- a/embassy-stm32/src/rng.rs +++ b/embassy-stm32/src/rng.rs @@ -19,11 +19,11 @@ pub enum Error { ClockError, } -pub struct Random { +pub struct Rng { _inner: T, } -impl Random { +impl Rng { pub fn new(inner: impl Unborrow) -> Self { T::enable(); T::reset(); @@ -49,7 +49,7 @@ impl Random { } } -impl RngCore for Random { +impl RngCore for Rng { fn next_u32(&mut self) -> u32 { loop { let bits = unsafe { T::regs().sr().read() }; @@ -80,9 +80,9 @@ impl RngCore for Random { } } -impl CryptoRng for Random {} +impl CryptoRng for Rng {} -impl traits::rng::Rng for Random { +impl traits::rng::Rng for Rng { type Error = Error; #[rustfmt::skip] type RngFuture<'a> where Self: 'a = impl Future> + 'a; @@ -128,28 +128,6 @@ impl traits::rng::Rng for Random { } } -impl traits::rng::Random for Random { - #[rustfmt::skip] - type NextFuture<'a> where Self: 'a = impl Future> + 'a; - - fn next<'a>(&'a mut self, range: u32) -> Self::NextFuture<'a> { - async move { - let t = (-(range as i32) % (range as i32)) as u32; - loop { - let mut buf = [0; 4]; - traits::rng::Rng::fill_bytes(self, &mut buf).await?; - let x = u32::from_le_bytes(buf); - let m = x as u64 * range as u64; - let l = m as u32; - if l < t { - continue; - } - return Ok((m >> 32) as u32); - } - } - } -} - pub(crate) mod sealed { use super::*; diff --git a/embassy-traits/src/rng.rs b/embassy-traits/src/rng.rs index 181faa515..3cc4b2a0b 100644 --- a/embassy-traits/src/rng.rs +++ b/embassy-traits/src/rng.rs @@ -17,11 +17,60 @@ pub trait Rng { fn fill_bytes<'a>(&'a mut self, dest: &'a mut [u8]) -> Self::RngFuture<'a>; } -pub trait Random: Rng { - #[rustfmt::skip] - type NextFuture<'a>: Future::Error>> + 'a - where - Self: 'a; - - fn next<'a>(&'a mut self, range: u32) -> Self::NextFuture<'a>; +pub struct Random { + rng: T, +} + +impl Random { + pub fn new(rng: T) -> Self { + Self { rng } + } + + pub async fn next_u8<'a>(&'a mut self, range: u8) -> Result { + // Lemire's method + let t = (-(range as i8) % (range as i8)) as u8; + loop { + let mut buf = [0; 1]; + self.rng.fill_bytes(&mut buf).await?; + let x = u8::from_le_bytes(buf); + let m = x as u16 * range as u16; + let l = m as u8; + if l < t { + continue; + } + return Ok((m >> 8) as u8); + } + } + + pub async fn next_u16<'a>(&'a mut self, range: u16) -> Result { + // Lemire's method + let t = (-(range as i16) % (range as i16)) as u16; + loop { + let mut buf = [0; 2]; + self.rng.fill_bytes(&mut buf).await?; + let x = u16::from_le_bytes(buf); + let m = x as u32 * range as u32; + let l = m as u16; + if l < t { + continue; + } + return Ok((m >> 16) as u16); + } + } + + pub async fn next_u32<'a>(&'a mut self, range: u32) -> Result { + // Lemire's method + let t = (-(range as i32) % (range as i32)) as u32; + loop { + let mut buf = [0; 4]; + self.rng.fill_bytes(&mut buf).await?; + let x = u32::from_le_bytes(buf); + let m = x as u64 * range as u64; + let l = m as u32; + if l < t { + continue; + } + return Ok((m >> 32) as u32); + } + } } diff --git a/examples/stm32h7/src/bin/eth.rs b/examples/stm32h7/src/bin/eth.rs index 4a9f261c2..df4931455 100644 --- a/examples/stm32h7/src/bin/eth.rs +++ b/examples/stm32h7/src/bin/eth.rs @@ -21,7 +21,7 @@ use embassy_net::{ }; use embassy_stm32::eth::lan8742a::LAN8742A; use embassy_stm32::eth::{Ethernet, State}; -use embassy_stm32::rng::Random; +use embassy_stm32::rng::Rng; use embassy_stm32::{interrupt, peripherals}; use heapless::Vec; use panic_probe as _; @@ -81,7 +81,7 @@ fn _embassy_rand(buf: &mut [u8]) { }); } -static mut RNG_INST: Option> = None; +static mut RNG_INST: Option> = None; static EXECUTOR: Forever = Forever::new(); static STATE: Forever> = Forever::new(); @@ -97,7 +97,7 @@ fn main() -> ! { let p = embassy_stm32::init(config()); - let rng = Random::new(p.RNG); + let rng = Rng::new(p.RNG); unsafe { RNG_INST.replace(rng); } diff --git a/examples/stm32h7/src/bin/rng.rs b/examples/stm32h7/src/bin/rng.rs index fea88c410..cf1b14ea5 100644 --- a/examples/stm32h7/src/bin/rng.rs +++ b/examples/stm32h7/src/bin/rng.rs @@ -8,9 +8,9 @@ mod example_common; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy::traits::rng::Random as _; use embassy_stm32::gpio::{Level, Output, Speed}; -use embassy_stm32::rng::Random; +use embassy_stm32::rng::Rng; +use embassy::traits::rng::Random; use embassy_stm32::Peripherals; use embedded_hal::digital::v2::OutputPin; use example_common::*; @@ -21,14 +21,14 @@ async fn main(_spawner: Spawner, p: Peripherals) { let mut led = Output::new(p.PB14, Level::High, Speed::Low); - let mut rng = Random::new(p.RNG); + let mut rng = Random::new(Rng::new(p.RNG)); loop { - info!("high {}", unwrap!(rng.next(16).await)); + info!("high {}", unwrap!(rng.next_u8(16).await)); unwrap!(led.set_high()); Timer::after(Duration::from_millis(500)).await; - info!("low {}", unwrap!(rng.next(16).await)); + info!("low {}", unwrap!(rng.next_u8(16).await)); unwrap!(led.set_low()); Timer::after(Duration::from_millis(500)).await; } From aaa4a477d56bdcaeff36723ce7fdaba0dce054d1 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Wed, 1 Sep 2021 09:47:46 -0400 Subject: [PATCH 6/6] Formatting. --- examples/stm32h7/src/bin/rng.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/stm32h7/src/bin/rng.rs b/examples/stm32h7/src/bin/rng.rs index cf1b14ea5..2dc8268b5 100644 --- a/examples/stm32h7/src/bin/rng.rs +++ b/examples/stm32h7/src/bin/rng.rs @@ -8,9 +8,9 @@ mod example_common; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; +use embassy::traits::rng::Random; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::rng::Rng; -use embassy::traits::rng::Random; use embassy_stm32::Peripherals; use embedded_hal::digital::v2::OutputPin; use example_common::*;