Skip to content
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

feat: Enable using thread_rng for faster WyHash initialisation #8

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ serde1 = ["dep:serde"]
wyhash = []
randomised_wyhash = ["wyhash", "dep:getrandom"]
fully_randomised_wyhash = ["randomised_wyhash"]
threadrng_wyhash = ["dep:rand", "randomised_wyhash"]
v4_2 = []

[dependencies]
getrandom = { version = "0.2", optional = true }
rand = { version = "0.8", optional = true }
rand_core = { version = "0.6", default-features = false, optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The crate will always export `WyRand` and will do so when set as `default-featur
- **`wyhash`** - Enables `WyHash`, a fast & portable hashing algorithm. Based on the final v4 C implementation.
- **`randomised_wyhash`** - Enables `RandomisedWyHashBuilder`, 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 `RandomisedWyHashBuilder`, but also the secret. Incurs a performance hit every time `WyHash` is initialised but it is more secure as a result. Enables `randomised_wyhash` if not already enabled.
- **`threadrng_wyhash`** - Enables sourcing entropy from `rand`'s `thread_rng()` method. Much quicker than `getrandom` and best used without the `fully_randomised_wyhash` flag as the overhead of calculating new secrets dwarfs any gains in entropy sourcing. Enables `randomised_wyhash` if not already enabled. Requires `std` environments.
- **`v4_2`** - Switches the PRNG/Hashing algorithms to use the final v4.2 implementation.

## Building for WASM/Web
Expand Down
13 changes: 11 additions & 2 deletions src/hasher/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,17 @@ impl RandomWyHashState {

let mut state = [0; SIZE];

getrandom::getrandom(&mut state)
.expect("Failed to source entropy for WyHash randomised state");
#[cfg(not(feature = "threadrng_wyhash"))]
{
getrandom::getrandom(&mut state)
.expect("Failed to source entropy for WyHash randomised state");
}
#[cfg(feature = "threadrng_wyhash")]
{
use rand::RngCore;

rand::thread_rng().fill_bytes(&mut state);
}

Self { state }
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
mod constants;
#[cfg(feature = "wyhash")]
mod hasher;
mod rand;
mod wyrand;
mod utils;

#[cfg(feature = "wyhash")]
pub use hasher::*;
pub use rand::WyRand;
pub use wyrand::WyRand;
File renamed without changes.