Skip to content

Conversation

NotsoanoNimus
Copy link
Contributor

@NotsoanoNimus NotsoanoNimus commented Jul 10, 2025

Implementation of the SipHash family of pseudorandom functions, complete with accompanying tests.

Predefined modules include:

  • siphash24 - Performant - 64-bit hash from 2 rounds-per-block and 4 finalization rounds.
  • siphash24_128 - Same as above but for 128-bit outputs.
  • siphash48 - Hardened - 64-bit hash from 4 rounds-per-block and 8 finalization rounds.
  • siphash48_128 - Same as above but for 128-bit outputs.

Out-of-the-box usage:

ulong hash = siphash24::hash(input, runtime_key);

for (usz i = 0; i < 4; ++i)
{
    // overloaded '[]' operator sets Bloom filter bits accordingly
    bloom_filter[(hash >> (16 * i)) & 0xFFFF] = true;
}

Other non-standard variations of SipHash can be used with an alias declaration like so:

import std::hash::siphash;

alias MySipHashFlavor = SipHash { uint128, 8, 16 };
alias hash = siphash::hash { uint128, 8, 16 };

@lerno
Copy link
Collaborator

lerno commented Jul 11, 2025

I posted some suggested improvements above.

@NotsoanoNimus
Copy link
Contributor Author

I incorporated your feedback, and then some. I tend to put a lot of idiosyncrasies around explicit casts and parentheses in my code because I'm paranoid of C's UB and other spooky behaviors, so it'll take a bit for me to be a little more lax with the help of C3.

Thank you for your time reviewing this.

@lerno
Copy link
Collaborator

lerno commented Jul 18, 2025

Updated the test to use this:

char[64] plaintext = math::iota(char[64]);

@lerno lerno merged commit b8d0747 into c3lang:master Jul 18, 2025
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants