Skip to content

WIP: Bump to rand[_core] 0.9 #729

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

Closed
wants to merge 1 commit into from

Conversation

pinkforest
Copy link
Contributor

@pinkforest pinkforest commented Jan 28, 2025

Pending / Clarifications

  • Breaking/Minor (Opt-in): Requires MSRV 1.63 - need to bump from current 1.60
  • Breaking/Minor (Opt-in): rand_core 0.9 via API
  • Breaking/Minor (Opt-in): We need to implement TryCryptoRng which needs API changes from current infallible API.
  • group and ff needs to bump rand + rand_core - Bump rand + rand_core zkcrypto/group#55

Notes

@pinkforest pinkforest changed the title Bump to rand[_core] 0.9 WIP: Bump to rand[_core] 0.9 Jan 28, 2025
@tarcieri
Copy link
Contributor

@pinkforest it needs to be os_rng now

@NimonSour
Copy link

Since rand_core changed the trait for OsRng from CryptoRngCore( which is RngCore + CryptoRng ) to TryRngCore +TryCryptoRngit makes sense to keep all the methods of types across dalek crates that are dependent on CryptoRngCore ( like generate and random ) as R : RngCore + CryptoRng plus adding the additional methods ( try_generate and try_random ) respectively where R : TryRngCore + CryptoRngCore.

Example for ed25519/src/signing.rs

    pub fn generate<R: RngCore + CryptoRng + ?Sized>(csprng: &mut R) -> SigningKey {
        let mut secret = SecretKey::default();
        csprng.fill_bytes(&mut secret);
        Self::from_bytes(&secret)
    }
    
    pub fn try_generate<R: TryRngCore + TryCryptoRng + ?Sized>(csprng: &mut R) -> Result<SigningKey,<R as TryRngCore>::Error> {
        let mut secret = SecretKey::default();
        csprng.try_fill_bytes(&mut secret)?;
        Ok(Self::from_bytes(&secret))
    }

It will offer some backward compatibility while aligning with rand_core's RngCoreand TryRngCore traits, where the user has a choice on RNG implementation.

@tarcieri
Copy link
Contributor

tarcieri commented Feb 4, 2025

it makes sense to keep all the methods of types across dalek crates that are dependent on CryptoRngCore ( like generate and random ) as R : RngCore + CryptoRng

CryptoRng now has a supertrait bound on RngCore, so instead CryptoRngCore should just be replaced with just CryptoRng. Adding RngCore + ... is redundant due to the supertrait bound. See:

https://docs.rs/rand_core/0.9.0/rand_core/trait.CryptoRng.html

Also you can call OsRng.unwrap_err() to get a CryptoRng-friendly version of OsRng.

@pinkforest
Copy link
Contributor Author

I assume we can live with ff/group will get duplicate 0.8 rand until ff/group upgrades - unblocking us to adopt 0.9 ?

@baloo
Copy link
Contributor

baloo commented Mar 14, 2025

Could you retarget rustcrypto-new-releases?

@nresare
Copy link
Contributor

nresare commented Mar 20, 2025

Since it seems we have ended up in a bit of a holding pattern where everyone waits on everyone else to make the move to rand 0.9.x I went ahead and scoped out what would be needed for getting at least all the tests to compile. The changes needed can be viewed in this commit but it seemed impolite to open a second PR pretty much the same thing.

@pinkforest please let me know how/if you would like to incorporate those changes, I'm happy to see this addressed in whatever way. Some observations:

  • To the best of my understanding, it is not possible to have both rand 0.9 and <0.9 in the dependency tree at the same time. Which means that we need to ensure that no dependency brings the old versions.
  • This translates into a requirement to have new releases of ff and group. There are already a prerelease branch of ff 0.14 and a PR that updates group
  • A somewhat more complex case is the dev-dependency merlin which in turn depends back on curve25519. I'll open a PR to at least get the ball rolling on getting that sorted, but I would imagine that this requires a coordingated release of new packages. Fun.
  • The bulk of the changes needed are related to OsRng no longer implementing CryptoRng, instead opting to implement TryCryptorRng (presumably because things like reading from /dev/urandom means filesystem IO theoretically can fail). I have opted to use the ThreadRng available from rand::rng() but I guess we could change that to a OsRng wrapper that panics on failures

@tarcieri
Copy link
Contributor

The bulk of the changes needed are related to OsRng no longer implementing CryptoRng, instead opting to implement TryCryptorRng (presumably because things like reading from /dev/urandom means filesystem IO theoretically can fail). I have opted to use the ThreadRng available from rand::rng() but I guess we could change that to a OsRng wrapper that panics on failures

You can get an OsRng that panics on failures with:

OsRng.unwrap_err()

...which returns an UnwrapErr<OsRng> that impls CryptoRng.

OsRng is a more conservative choice for things like key generation. Userspace RNGs have more sharp edges, e.g. fork safety, VM suspend/resume

rand = "0.8"
rand_core = { version = "0.6", default-features = false, features = ["getrandom"] }
rand = "0.9"
rand_core = { version = "0.9", default-features = false, features = ["getrandom"] }

Choose a reason for hiding this comment

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

Suggested change
rand_core = { version = "0.9", default-features = false, features = ["getrandom"] }
rand_core = { version = "0.9", default-features = false, features = ["os_rng"] }

serde = { version = "1", default-features = false, optional = true, features = ["derive"] }
zeroize = { version = "1", default-features = false, optional = true, features = ["zeroize_derive"] }

[dev-dependencies]
bincode = "1"
criterion = "0.5"
rand_core = { version = "0.6", default-features = false, features = ["getrandom"] }
rand_core = { version = "0.9", default-features = false, features = ["getrandom"] }

Choose a reason for hiding this comment

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

Suggested change
rand_core = { version = "0.9", default-features = false, features = ["getrandom"] }
rand_core = { version = "0.9", default-features = false, features = ["os_rng"] }

@tarcieri
Copy link
Contributor

tarcieri commented Jul 8, 2025

Closing in favor of #777

@tarcieri tarcieri closed this Jul 8, 2025
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.

6 participants