Skip to content

Commit

Permalink
chore: update to latest rand beta and getrandom rc version
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluefinger committed Dec 1, 2024
1 parent 067ae9e commit 193ebfe
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/final_v4_2/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::{secret::Secret, WyHash};
#[cfg(feature = "fully_randomised_wyhash")]
static SECRET: OnceLock<Secret> = OnceLock::new();

#[cfg(feature = "fully_randomised_wyhash")]
#[inline]
fn gen_new_secret() -> Secret {
use super::secret::make_secret;
Expand Down
1 change: 1 addition & 0 deletions src/legacy_final_v4/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::{secret::LegacySecret, WyHashLegacy};
#[cfg(feature = "fully_randomised_wyhash")]
static SECRET: OnceLock<LegacySecret> = OnceLock::new();

#[cfg(feature = "fully_randomised_wyhash")]
#[inline]
fn gen_new_secret() -> LegacySecret {
use super::secret::make_secret_legacy;
Expand Down
9 changes: 1 addition & 8 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u64>();

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")]
{
Expand Down

0 comments on commit 193ebfe

Please sign in to comment.