Skip to content

Commit

Permalink
Merge pull request #8 from Bluefinger/threadrng_entropy_sourcing
Browse files Browse the repository at this point in the history
feat: Enable using `thread_rng` for faster WyHash initialisation
  • Loading branch information
Bluefinger authored Mar 26, 2024
2 parents acb1d74 + 19d21b3 commit 6844aab
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
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.

0 comments on commit 6844aab

Please sign in to comment.