marioortizmanero · GitHub

Problem

When creating a KnownKey, the hash builder is discarded:

image

In order to ensure that the hash is going to be the same later on, the hash builder used for the pre-calculated hash (i.e., the screenshot) must be the same one from whatever hash map is being used. Some hashing algorithms start with a random seed, so if their hash builder isn't reused they return different hashes.

We currently use halfbrown's hash builder, which is deterministic and is why everything works right now. But it seems somewhat wrong to rely on a deterministic hash builder with no warning whatsoever.

Steps

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=3aa7c4f91991ae7c0535202567df5013

Possible Solution(s)

  • Proper fix: restructure KnownKey so that we are guaranteed that the hash builder is going to be the same. We currently construct them with a From<Cow>. To fix this, we could either have a constructor that takes Cow, HashBuilder, or that initizalizes the HashBuilder itself and returns it in the constructor, too. That way it can be reused with the hash map. There may be another better solution which enforces this statically, since one could always forget to reuse it manually.
  • Realistic fix: continue using the current KnownKey implementation if there are no plans to change the hashing algorithm. But there should be at least a warning in KnownKey so that noone changes it, explaining this issue. We could also enforce halfbrown::DefaultHashBuilder in the parameters of KnownKey's methods, which should at least emit compile-time warnings.

Read the original on github.com ↗