Created
October 17, 2026 16:21
-
-
Save ef1500/de9b454da2595f4c0663928a29bee78f to your computer and use it in GitHub Desktop.
wifehash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef WIFEHASH_H | |
| #define WIFEHASH_H | |
| // WIFEHASH128 - MEIHASH128 Variation | |
| // Written by ef1500 (Christopher J. Cole) | |
| // Written for my wife, Makara. I love you to the point of innovation. | |
| // :-3 | |
| // Technical notes: Doesn't use the full dual mixing technique used in the other hashes | |
| // Here, we do a partial path of each, alternating between P and S paths, and alternating parameters | |
| // Each time. | |
| // Passes all SMHasher3 tests with flying colors. | |
| #include <stddef.h> | |
| #include <stdint.h> | |
| // Define force_inline | |
| #ifndef FORCE_INLINE | |
| #if defined(_MSC_VER) | |
| #define FORCE_INLINE __forceinline | |
| #else | |
| #define FORCE_INLINE inline __attribute__((always_inline)) | |
| #endif | |
| #endif | |
| // Define uint128 | |
| #if defined(__SIZEOF_INT128__) | |
| typedef __uint128_t uint128_t; | |
| #else | |
| // Only works with systems that support 128-bit ints | |
| #error "128-bit integer type is not supported on this platform." | |
| #endif | |
| // Define fallthrough | |
| #ifndef WIFEHASH_FALLTHROUGH | |
| #if defined(__GNUC__) && __GNUC__ >= 7 | |
| #define WIFEHASH_FALLTHROUGH __attribute__((fallthrough)) | |
| #elif defined(__clang__) | |
| #define WIFEHASH_FALLTHROUGH [[clang::fallthrough]] | |
| #elif defined(__cplusplus) && __cplusplus >= 201703L | |
| #define WIFEHASH_FALLTHROUGH [[fallthrough]] | |
| #else | |
| #define WIFEHASH_FALLTHROUGH ((void)0) | |
| #endif | |
| #endif | |
| // Constants | |
| // Secret S0/S1 constants can be set to custom values if preferred | |
| // As well as the seed P0/P1 constants | |
| static const uint64_t P0_DEF = 0x59757a7541696861ULL; // "YuzuAiha" in ASCII | |
| static const uint64_t P1_DEF = 0x4d65694169686172ULL; // "MeiAihar" in ASCII | |
| static const uint64_t S0_DEF = 0x4d495a5553415741ULL; // "MIZUSAWA" in ASCII | |
| static const uint64_t S1_DEF = 0x494c4f5645594f55ULL; // "ILOVEYOU" in ASCII | |
| static FORCE_INLINE uint64_t meirotl64(uint64_t x, unsigned int r) | |
| { | |
| return (x << r) | (x >> (64 - r)); | |
| } | |
| // Partial Paths, uses only one multiplication | |
| static FORCE_INLINE uint128_t meimix_P(uint64_t Mi, uint64_t Yi, uint64_t P0, uint64_t P1) | |
| { | |
| uint64_t Mp = Mi ^ P0; | |
| uint64_t Yp = Yi ^ P1; | |
| return (uint128_t)Mp * Yp; | |
| } | |
| static FORCE_INLINE uint128_t meimix_S(uint64_t Mi, uint64_t Yi, uint64_t S0, uint64_t S1) | |
| { | |
| uint64_t Ms = Mi ^ S0; | |
| uint64_t Ys = Yi ^ S1; | |
| return (uint128_t)Ms * Ys; | |
| } | |
| // Read. Do it unaligned in weird if we're in x86 because | |
| // For whatever reason, it saves a few cycles. | |
| static FORCE_INLINE uint64_t read64x(const void *ptr) | |
| { | |
| #if defined(__x86_64__) || defined(_M_X64) | |
| return *(const uint64_t *)ptr; | |
| // Handle reading on other systems | |
| #else | |
| uint64_t v; | |
| memcpy(&v, ptr, 8); | |
| return v; | |
| #endif | |
| } | |
| static FORCE_INLINE void wifehash_128(const void *input, size_t len, uint64_t seed, void *output) | |
| { | |
| const uint8_t *ptr = (const uint8_t *)input; | |
| // Initialize state | |
| uint64_t p0 = P0_DEF ^ seed; | |
| uint64_t p1 = P1_DEF ^ (seed * S1_DEF); // Multiply here to pass the seed tests | |
| uint64_t s0 = S0_DEF ^ meirotl64(seed, 32); // "..." | |
| uint64_t s1 = S1_DEF; | |
| uint64_t final_m = 0; | |
| uint64_t final_y = 0; | |
| // Accumulators | |
| uint64_t accumulator1 = P1_DEF; | |
| uint64_t accumulator2 = P0_DEF; | |
| // 128-bit lanes for mixing stuff up | |
| uint128_t lane1 = 0; | |
| uint128_t lane2 = 0; | |
| uint128_t lane3 = 0; | |
| uint128_t lane4 = 0; | |
| // OoO Should be able to perform multiple lanes concurrently | |
| while (len >= 64) | |
| { | |
| uint64_t mix1 = read64x(ptr); | |
| uint64_t mix2 = read64x(ptr + 8); | |
| lane1 ^= meimix_S(mix1, mix2, S0_DEF, P1_DEF); | |
| uint64_t mix3 = read64x(ptr + 16); | |
| uint64_t mix4 = read64x(ptr + 24); | |
| lane2 ^= meimix_P(mix3, mix4, P0_DEF, S1_DEF); | |
| uint64_t mix5 = read64x(ptr + 32); | |
| uint64_t mix6 = read64x(ptr + 40); | |
| lane3 ^= meimix_S(mix5, mix6, P1_DEF, S0_DEF); | |
| uint64_t mix7 = read64x(ptr + 48); | |
| uint64_t mix8 = read64x(ptr + 56); | |
| lane4 ^= meimix_P(mix7, mix8, S1_DEF, P0_DEF); | |
| accumulator1 ^= (uint64_t)lane1; | |
| accumulator2 ^= (uint64_t)(lane1 >> 64); | |
| accumulator1 ^= meirotl64(accumulator1, 1); | |
| accumulator2 ^= meirotl64(accumulator2, 1); | |
| accumulator1 ^= (uint64_t)lane2; | |
| accumulator2 ^= (uint64_t)(lane2 >> 64); | |
| accumulator1 ^= meirotl64(accumulator1, 1); | |
| accumulator2 ^= meirotl64(accumulator2, 1); | |
| accumulator1 ^= (uint64_t)lane3; | |
| accumulator2 ^= (uint64_t)(lane3 >> 64); | |
| accumulator1 ^= meirotl64(accumulator1, 1); | |
| accumulator2 ^= meirotl64(accumulator2, 1); | |
| accumulator1 ^= (uint64_t)lane4; | |
| accumulator2 ^= (uint64_t)(lane4 >> 64); | |
| uint64_t temp = accumulator1; | |
| accumulator1 = meirotl64(accumulator1, 1) + accumulator2; | |
| accumulator2 = meirotl64(temp, 1) + accumulator1; | |
| ptr += 64; | |
| len -= 64; | |
| } | |
| while (len >= 16) | |
| { | |
| uint64_t mix1 = read64x(ptr); | |
| uint64_t mix2 = read64x(ptr + 8); | |
| uint128_t mixed = meimix_P(mix1, mix2, P0_DEF, P1_DEF); | |
| accumulator1 ^= (uint64_t)mixed; | |
| accumulator2 ^= (uint64_t)(mixed >> 64); | |
| uint64_t temp = accumulator1; | |
| accumulator1 = meirotl64(accumulator1, 1) + accumulator2; | |
| accumulator2 = meirotl64(temp, 1) ^ accumulator1; // XOR Instead of add here to pass Permutation Tests. | |
| ptr += 16; | |
| len -= 16; | |
| } | |
| if (len > 0) | |
| { | |
| switch (len) | |
| { | |
| case 15: final_y |= ((uint64_t)ptr[14]) << 48; WIFEHASH_FALLTHROUGH; | |
| case 14: final_y |= ((uint64_t)ptr[13]) << 40; WIFEHASH_FALLTHROUGH; | |
| case 13: final_y |= ((uint64_t)ptr[12]) << 32; WIFEHASH_FALLTHROUGH; | |
| case 12: final_y |= ((uint64_t)ptr[11]) << 24; WIFEHASH_FALLTHROUGH; | |
| case 11: final_y |= ((uint64_t)ptr[10]) << 16; WIFEHASH_FALLTHROUGH; | |
| case 10: final_y |= ((uint64_t)ptr[9]) << 8; WIFEHASH_FALLTHROUGH; | |
| case 9: final_y |= ((uint64_t)ptr[8]); WIFEHASH_FALLTHROUGH; | |
| case 8: final_m |= read64x(ptr); | |
| break; | |
| case 7: final_m |= ((uint64_t)ptr[6]) << 48; WIFEHASH_FALLTHROUGH; | |
| case 6: final_m |= ((uint64_t)ptr[5]) << 40; WIFEHASH_FALLTHROUGH; | |
| case 5: final_m |= ((uint64_t)ptr[4]) << 32; WIFEHASH_FALLTHROUGH; | |
| case 4: final_m |= ((uint64_t)ptr[3]) << 24; WIFEHASH_FALLTHROUGH; | |
| case 3: final_m |= ((uint64_t)ptr[2]) << 16; WIFEHASH_FALLTHROUGH; | |
| case 2: final_m |= ((uint64_t)ptr[1]) << 8; WIFEHASH_FALLTHROUGH; | |
| case 1: final_m |= (uint64_t)ptr[0]; break; | |
| } | |
| uint128_t meimixed = meimix_P(final_m, final_y, s0, s1); // Mix in s0, s1 here to pass SeedSparse and SeedBIC tests | |
| accumulator1 ^= (uint64_t)meimixed; | |
| accumulator2 ^= (uint64_t)(meimixed >> 64); | |
| } | |
| accumulator1 ^= len; | |
| // Avalanche | |
| uint128_t finalmix = (uint128_t)accumulator1 * s0; | |
| finalmix ^= (uint128_t)accumulator2 * p0; | |
| accumulator1 = (uint64_t)finalmix ^ (uint64_t)(finalmix >> 64); | |
| accumulator2 = (uint64_t)(finalmix >> 64) + accumulator1; | |
| accumulator1 ^= accumulator1 >> 33; accumulator1 *= P0_DEF; | |
| accumulator1 ^= accumulator1 >> 33; accumulator1 *= S1_DEF; | |
| accumulator1 ^= accumulator1 >> 33; | |
| accumulator2 ^= accumulator2 >> 33; accumulator2 *= P1_DEF; | |
| accumulator2 ^= accumulator2 >> 33; accumulator2 *= S0_DEF; | |
| accumulator2 ^= accumulator2 >> 33; | |
| // Finalize output | |
| ((uint64_t *)output)[0] = accumulator1; | |
| ((uint64_t *)output)[1] = accumulator2; | |
| } | |
| #endif // WIFEHASH_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment