Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Discussion: Generic Seeds #13

Closed
onkoe opened this issue Feb 20, 2024 · 2 comments · Fixed by #14
Closed

Feature Discussion: Generic Seeds #13

onkoe opened this issue Feb 20, 2024 · 2 comments · Fixed by #14
Assignees
Labels
enhancement New feature or request

Comments

@onkoe
Copy link

onkoe commented Feb 20, 2024

Before coming across this great library, I was about to spin my own noise crate! Thanks for saving me the hassle - this seems fantastic!

However, I do have a weird feature request that only might make sense. I would love to be able to provide theoretically infinite seeds, such as vectors/arrays of values - or even a string slice, to the generator.

Do you know if this is viable from an implementation standpoint? You don't need to worry about implementing it - I don't think it's widely interesting. However, I'm definitely interested!

From what I can tell, rand_chacha happily implements Seed for any byte slice, so you can give it any kind of data to get random data back. That should allow it to work for sources that use core::utils::ptable::PermutationTable, at least if seed also allowed anything serializable to [u8].

Once again, thanks for the great library! 🦀✨

@cookiephone
Copy link
Owner

Thanks for the praise! Currently, seeding is implemented using ChaCha12Rng::seed_from_u64. As you said, this could be changed to support any byte slice in principle.

One thing to keep in mind however is, that there are still a theoretically finite amount of possible configurations of the permutation table, even if the seed was more flexible. Specifically it would be the factorial of the permutation table size. This is set through a constant to 256, meaning a finite set of 256! possible permutations.

In that sense, it might indeed make sense to allow for more flexibility when seeding. After all u64 does not nearly cover enough states to do a permutation table of that size justice.

@cookiephone cookiephone added the enhancement New feature or request label Feb 20, 2024
@cookiephone cookiephone self-assigned this Feb 21, 2024
@cookiephone
Copy link
Owner

cookiephone commented Feb 26, 2024

Upon further inspection, chacha is using 32 byte seeds. This is by no means perfectly generalizable, and without generic const expressions I don't think I can create a nice API for more genuinely mostly arbitrary seeds. However, I have whipped up something that at least exposes the full API of the underlying chacha RNG. That means, seeds can still be u64, but may also be of type [u8; 32]. With this, the RNG can be harnessed to its full potential, even though the user is still responsible for actually building a suitable 32 byte seed should they choose to not use a u64.

This is done by introducing a Seed trait which is implemented for u64 and [u8; 32] respectively. I then use that in the permutation table implementation to construct the RNG with the appropriate seeding function.

I'll wait a bit to see if there are some more suggestions or ideas here. If not, I'll go ahead and merge+publish this with the minor semver bump it entails as version 1.1.2 :)

@cookiephone cookiephone linked a pull request Feb 28, 2024 that will close this issue
cookiephone added a commit that referenced this issue Feb 28, 2024
…-seeds

generalize seeds (see issue #13), minor semver bump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants