rust-lang / rust-lang/rustc-hash
Initial value of `FxHasher` leads to avoidable collisions
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 596
- Forks
- 62
- PR merge metrics
- No merged PRs in 30d
Description
It's easy to see that FxHasher { hash: 0 }.add_to_hash(0) is a fixed point (i.e. does not result in any change to the internal state self.hash):
In particular 0.rotate_left(5).bitxor(0).wrapping_mul(K) == 0.
Therefore initialising FxHasher with a hash value of 0 is a bit undesirable, as sequences of 0s (written to the hash before any non-zero value) cannot be distinguished by their hashes.
Of course, there's always the possibility that a value of self.hash.rotate_left(5) could be written to the hasher, which will result in the internal state reaching 0—but in practice writing values of 0 can arise quite often, and choosing some other initial value would be quite beneficial? Perhaps even K?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/lib.rs at the linked FxHasher initialization and add_to_hash implementation around lines 69–81. Examine the consequences of choosing a nonzero initial state and determine how leading zero inputs should behave. Done means the selected initialization avoids the described fixed point without introducing an unintended hash compatibility change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- performance
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100