From 8d2bcc831029f091e5898ed0018e7b2b4875d9be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Rica=20Pais=20da=20Silva?= Date: Tue, 26 Mar 2024 16:30:19 +0100 Subject: [PATCH 1/3] feat: Enable using `thread_rng` for faster WyHash initialisation --- Cargo.toml | 2 ++ README.md | 1 + src/hasher/builder.rs | 13 +++++++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b759d2d..8bb4d59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/README.md b/README.md index c2de2f6..c347b7a 100644 --- a/README.md +++ b/README.md @@ -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. - **`v4_2`** - Switches the PRNG/Hashing algorithms to use the final v4.2 implementation. ## Building for WASM/Web diff --git a/src/hasher/builder.rs b/src/hasher/builder.rs index 95999c8..2c8a3e0 100644 --- a/src/hasher/builder.rs +++ b/src/hasher/builder.rs @@ -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 } } From 701ebaea07afb19c721526b7f2f8a1c767f9f5ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Rica=20Pais=20da=20Silva?= Date: Tue, 26 Mar 2024 16:46:23 +0100 Subject: [PATCH 2/3] fix: rename wyrand module for less ambiguity --- src/lib.rs | 4 ++-- src/{rand.rs => wyrand.rs} | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename src/{rand.rs => wyrand.rs} (100%) diff --git a/src/lib.rs b/src/lib.rs index 7d4b78d..43ac0d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/rand.rs b/src/wyrand.rs similarity index 100% rename from src/rand.rs rename to src/wyrand.rs From 19d21b3be5ca0e87b3c9e3ab6820dbb34905eb37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Rica=20Pais=20da=20Silva?= Date: Tue, 26 Mar 2024 16:58:26 +0100 Subject: [PATCH 3/3] doc: Note about `thread_rng` usage assuming std environments --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c347b7a..2af5339 100644 --- a/README.md +++ b/README.md @@ -37,7 +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. +- **`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