From 193ebfe32185825e373045699303424ba241d467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Rica=20Pais=20da=20Silva?= Date: Sun, 1 Dec 2024 12:20:27 +0100 Subject: [PATCH] chore: update to latest rand beta and getrandom rc version --- Cargo.toml | 8 ++++---- README.md | 13 ++++++++----- src/final_v4_2/builder.rs | 1 + src/legacy_final_v4/builder.rs | 1 + src/utils.rs | 9 +-------- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c3e9dea..096b0a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,15 +27,15 @@ threadrng_wyhash = ["dep:rand", "randomised_wyhash"] legacy_v4 = [] [dependencies] -getrandom = { version = "0.2", optional = true } -rand = { version = "=0.9.0-beta.0", optional = true } -rand_core = { version = "=0.9.0-beta.0", default-features = false, optional = true } +getrandom = { version = "=0.3.0-rc.0", optional = true } +rand = { version = "=0.9.0-beta.1", optional = true } +rand_core = { version = "=0.9.0-beta.1", default-features = false, optional = true } serde = { version = "1.0", features = ["derive"], optional = true } [dev-dependencies] criterion = "0.5" serde_test = "1.0" -rand = "=0.9.0-beta.0" +rand = "=0.9.0-beta.1" [[bench]] name = "rand_bench" diff --git a/README.md b/README.md index e3ff394..75664b0 100644 --- a/README.md +++ b/README.md @@ -36,17 +36,20 @@ The crate will always export `WyRand` and will do so when set as `default-featur - **`hash`** - Enables `core::hash::Hash` implementation for `WyRand`. - **`wyhash`** - Enables `WyHash`, a fast & portable hashing algorithm. Based on the final v4 C implementation. - **`randomised_wyhash`** - Enables `RandomWyHashState`, a means to source a randomised state for `WyHash` for use in collections like `HashMap`/`HashSet`. Enables `wyhash` feature if it is not already enabled. -- **`fully_randomised_wyhash`** - Randomises not just the seed for `RandomWyHashState`, but also the secret. The new secret is generated once per runtime, and then is used for every subsequent new `WyHash` (with each `WyHash` instance having its own unique seed). Enables `randomised_wyhash` if not already enabled, and requires `std` environments. -- **`threadrng_wyhash`** - Enables sourcing entropy from `rand`'s `thread_rng()` method. Much quicker than `getrandom`. Enables `randomised_wyhash` if not already enabled. Requires `std` environments. - **`legacy_v4`** - Exposes the legacy PRNG/Hashing algorithms that use the final v4 implementation. +Below are **application only features**, meant only to be enabled by app/bin crates, **NOT** lib crates as this changes runtime behaviour and also can pull in crates that change whether this crate can compile for `no-std` environments or not: + +- **`fully_randomised_wyhash`** - Randomises not just the seed for `RandomWyHashState`, but also the secret. The new secret is generated once per runtime, and then is used for every subsequent new `WyHash` (with each `WyHash` instance having its own unique seed). Enables `randomised_wyhash` if not already enabled, and requires `std` environments. ONLY FOR BIN CRATES. +- **`threadrng_wyhash`** - Enables sourcing entropy from `rand`'s `thread_rng()` method. Much quicker than `getrandom`. Enables `randomised_wyhash` if not already enabled. Requires `std` environments. ONLY FOR BIN CRATES. + ## Building for WASM/Web -If you are using `WyRand` with `rand_core` and/or `WyHash` with `randomised_wyhash` then for building for the web/WASM, you'll need to configure `getrandom` to make use of the browser APIs in order to source entropy from. Add the following to your project `Cargo.toml` if your WASM builds target the web: +If you are using `WyRand` with `rand_core` and/or `WyHash` with `randomised_wyhash` then for building for the web/WASM, you'll need to configure `getrandom` and its backend to make use of the browser APIs in order to source entropy from. Add the following to your project `.cargo/config.toml` if your WASM builds target the web: ```toml -[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies] -getrandom = { version = "0.2", features = ["js"] } +[target.wasm32-unknown-unknown] +rustflags = ['--cfg', 'getrandom_backend="wasm_js"'] ``` ## License diff --git a/src/final_v4_2/builder.rs b/src/final_v4_2/builder.rs index 018c26a..6c48520 100644 --- a/src/final_v4_2/builder.rs +++ b/src/final_v4_2/builder.rs @@ -13,6 +13,7 @@ use super::{secret::Secret, WyHash}; #[cfg(feature = "fully_randomised_wyhash")] static SECRET: OnceLock = OnceLock::new(); +#[cfg(feature = "fully_randomised_wyhash")] #[inline] fn gen_new_secret() -> Secret { use super::secret::make_secret; diff --git a/src/legacy_final_v4/builder.rs b/src/legacy_final_v4/builder.rs index 378d33f..8786676 100644 --- a/src/legacy_final_v4/builder.rs +++ b/src/legacy_final_v4/builder.rs @@ -13,6 +13,7 @@ use super::{secret::LegacySecret, WyHashLegacy}; #[cfg(feature = "fully_randomised_wyhash")] static SECRET: OnceLock = OnceLock::new(); +#[cfg(feature = "fully_randomised_wyhash")] #[inline] fn gen_new_secret() -> LegacySecret { use super::secret::make_secret_legacy; diff --git a/src/utils.rs b/src/utils.rs index 1170ee4..06b9285 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -38,18 +38,11 @@ pub(crate) const fn check_for_valid_secret_value(current_value: usize, secret: & pub(crate) fn get_random_u64() -> u64 { #[cfg(not(feature = "threadrng_wyhash"))] { - const SIZE: usize = core::mem::size_of::(); - - let mut state = [0; SIZE]; - // Don't bother trying to handle the result. If we can't obtain // entropy with getrandom, then there is no hope and we might as // well panic. It is up to the user to ensure getrandom is configured // correctly for their platform. - getrandom::getrandom(&mut state) - .expect("Failed to source entropy for WyHash randomised state"); - - u64::from_ne_bytes(state) + getrandom::u64().expect("Failed to source entropy for WyHash randomised state") } #[cfg(feature = "threadrng_wyhash")] {