PolymurHash for Rust
A fast, non-cryptographic hash function for Rust. This is a Rust port of the PolymurHash universal hash function.
Features
- 🚀 High Performance: Optimized for speed with performance comparable to XXH3
- 🔒 No Unsafe Code: Completely safe Rust implementation
- 📦 No Dependencies: Zero runtime dependencies
- 🎯
no_stdCompatible: Can be used in embedded and kernel contexts - 🔧 Flexible Seeding: Multiple ways to initialize with different seed types
Installation
Add this to your Cargo.toml:
[]
= "0.2"
Usage
Basic Usage
use PolymurHash;
// Create a hasher with a default seed
let hasher = new;
// Hash some data
let data = b"Hello, world!";
let hash = hasher.hash;
println!;
Custom Seeds
use PolymurHash;
// From a 64-bit seed
let hasher = from_u64_seed;
// From a 128-bit seed
let hasher = new;
// From two 64-bit seeds (key and state)
let hasher = from_u64x2_seed;
Using Tweaks
Tweaks allow you to generate different hash values from the same key without reinitializing:
use PolymurHash;
let hasher = new;
let data = b"Some data";
// Generate multiple independent hashes
let hash1 = hasher.hash_with_tweak;
let hash2 = hasher.hash_with_tweak;
let hash3 = hasher.hash_with_tweak;
// All hashes will be different
assert_ne!;
assert_ne!;
Performance
PolymurHash is designed for high performance. On modern processors, it achieves speeds comparable to other fast non-cryptographic hash functions like XXH3.
To run the benchmarks:
Algorithm Details
PolymurHash is based on polynomial evaluation in the finite field GF(2^61-1). It uses:
- Efficient 61-bit arithmetic with lazy reduction
- Optimized mixing functions for good avalanche properties
- Different code paths for small (<= 7 bytes), medium (8-49 bytes), and large (>= 50 bytes) inputs
Safety
This implementation:
- Uses no
unsafecode - Is
#![forbid(unsafe_code)]enforced - Has been tested against the reference C implementation test vectors
License
This project is licensed under the MIT License - see the LICENSE file for details.