Skip to content

Conversation

finalyards
Copy link
Contributor

The version of rand used in the project has fallen behind, mainly due to p256 still relying on the earlier version.

I wanted to give it a try, and attached is a PR that:

  • raises rand to 0.10.0-rc.0 (currently latest)
  • cleans up some dependencies that seem to be not used
  • fixes some conditional compilation, especially with regard to rand (security feature)
  • occasional non-functional cleanup..

The changes to 0.9.x were extensive, covering not only the API but the whole rand ecosystem, as well.

I chose to push past the 0.9, since we already can see what the 0.10 API would look like. Happy to keep maintaining this, as a PR, until the 0.10 proper is released.

Some questions to authors in the comments below. I need guidance.

Discussed previously: #423

@finalyards finalyards marked this pull request as draft October 6, 2025 05:48
bt-hci = { version = "0.6" }
embassy-futures = "0.1.1"
embassy-sync = { version = "0.7" }
embassy-sync = { version = "0.7" } # host-macros
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

marked the reason of existence for certain dependencies (here and static_cell, heapless, below).

pub mod ble_advertise;
pub mod ble_advertise_multiple;
pub mod ble_bas_central;
#[cfg(feature = "security")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*_sec were treated as feature-flagged, except here.

panic-probe = { version = "1.0", features = ["print-defmt"] }

cortex-m = { version = "0.7.6" }
chacha20 = { version = "0.10.0-rc.2", default-features = false, features = ["rng"], optional = true }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rand 0.10 changelog:

The dependency on rand_chacha has been replaced with a dependency on chacha20.

Moved it upwards, to have the list in roughly alphabetical order (imho, a good idea). Do you agree with this, or should it be in-place of the earlier rand_core?

chacha20 = { version = "0.10.0-rc.2", default-features = false, features = ["rng"], optional = true }
cortex-m-rt = "0.7.0"
rand = { version = "0.8.5", default-features = false }
rand_core = { version = "0.9.3", default-features = false, optional = true }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rand_core is interesting. crates.io for 0.9.3 states:

This crate is mainly of interest to crates publishing implementations of RngCore. Other users are encouraged to use the rand crate instead which re-exports the main traits and error types.

But the current README says it differently:

The main rand crate re-exports most items defined in this crate, along with tools [...] Most users should prefer to use the main rand crate.

I feel there's a justification for keeping rand_core, if the code only needs the interface of the RNG's, and doesn't actually create random numbers. What do you think?

That's the approach I took here - nrf-sdc - but in host I've opted for rand and use the interfaces as rand::rand_core::....

rand_core = "0.6"
rand_chacha = { version = "0.3", default-features = false, optional = true }
rand = { version="0.8", default-features = false, optional = true}
p256 = { version = "0.14.0-pre.11", default-features = false, features = ["ecdh","arithmetic"], optional = true }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 0.14.0-pre.3 seems to be the release that has the rand 0.6 -> 0.9 upgrade.

This actually is the BIG THING about the PR. Whether we want to jump onto -pre or wait until p256 0.14.0 proper has been released.

use heapless::Vec;
use rand_core::{CryptoRng, RngCore};
#[cfg(feature = "security")]
use rand::rand_core::{CryptoRng, RngCore};
Copy link
Contributor Author

@finalyards finalyards Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: here, we could go on with the earlier lines, if rand_core is added (alongside rand), as a dependency to trouble-host. It's a compromise between either:

  • shorter code; two dependencies
  • slightly longer code; just rand dep

The upstream seems to recomment going rand only, but it's really our choice. Which way is better?

  • please inform of the choice.

///
/// Only relevant if the feature `security` is enabled.
pub fn set_io_capabilities(self, io_capabilities: IoCapabilities) -> Self {
#[cfg(feature = "security")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the conditional above the method name. This was made possible by making the security feature handling more consistent, elsewhere.

Should be beneficial: one only needs to see such method signatures in IDE if the security feature is enabled.

pairing_data.local_secret_ra =
rng.sample(rand::distributions::Uniform::new_inclusive(0, 999999));
rng.sample(rand::distr::Uniform::new_inclusive(0, 999999).unwrap());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I chose to leave the original code (after API catch-up), but the rand authors recommend:

Constructors may do extra work up front to allow faster sampling of multiple values. Where only a single sample is required it is suggested to use Rng::random_range [...]".

Should we?

@finalyards finalyards marked this pull request as ready for review October 6, 2025 13:28
@finalyards

This comment was marked as outdated.

@finalyards finalyards marked this pull request as draft October 7, 2025 09:46
@finalyards
Copy link
Contributor Author

A bit about the status:

  • it's ready for reviews, and use, actually
  • when I get conclusion on the discussion places (might tag them with some eye catching emoji...), I'll do fine-tuning and
  • ..squash into a single commit

@finalyards
Copy link
Contributor Author

Issues unrelated to random/crypto are now in #476 . If we merge that in, I can remove them from this PR.

@lulf
Copy link
Member

lulf commented Oct 13, 2025

/test

@finalyards finalyards force-pushed the pr-rand-catchup branch 2 times, most recently from 8d76b38 to 9655720 Compare October 15, 2025 16:04
@finalyards finalyards marked this pull request as ready for review October 15, 2025 16:22
@HaoboGu
Copy link
Collaborator

HaoboGu commented Oct 16, 2025

/test

…cludes changes to 'security' feature flags, making its use more consistent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants