gxhash icon indicating copy to clipboard operation
gxhash copied to clipboard

Add no `no_std` support

Open ogxd opened this issue 1 year ago • 6 comments

ogxd avatar Dec 30 '23 23:12 ogxd

It would be possible to get the hasher module working if rand was swapped out for rand_chacha in GxBuildHasher and if it was given a constant seed in no_std environments.

const-random could be used, or the seed could be generated in the build script.

notsatvrn avatar Dec 31 '23 03:12 notsatvrn

I got this working, generating the seed using the latter method here. I wanted to avoid drawing in more dependencies, and this actually reduces the number of dependencies, as rand depends on all of these.

notsatvrn avatar Dec 31 '23 05:12 notsatvrn

Interesting, so if I understand correctly, with your changes, the Hasher remains even in no std builds, but with the following subtleties:

  • When std feature, it creates a seed using a ChaCha12 rng, that is itself seeded using getrandom (from OS)
  • When no std
    • When possible, getrandom will use rdrand
    • When not, hardcode a seed but a build time (so it's less dynamic, but still better than completely hardcoded seeds)

The author of ahash has done something similar, but making a separate crate for the build time random. Maybe we can reuse it to avoid the complexity introduced in build.rs

ogxd avatar Jan 01 '24 21:01 ogxd

Switched over to const-random here (also fixed the entopy issue here :p)

notsatvrn avatar Jan 02 '24 04:01 notsatvrn

Nice! I have just cherry-picked your changes. Next step is to have this covered by tests/CI and add a few lines about it in the readme. I can do it when I have some time this week.

ogxd avatar Jan 02 '24 22:01 ogxd

Hello! I'm sorry I haven't provided any update here. The no_std is still very relevant. Since there might be some changes regarding the Hasher construction and the dependencies, I've cherry-picked the first commit of this PR, even if the no_std removes the Hasher. This can be done as a second step once we have figured out what's best for the Hasher seeds generation.

https://github.com/ogxd/gxhash/pull/74

ogxd avatar May 27 '24 12:05 ogxd