diff --git a/CHANGELOG.md b/CHANGELOG.md index 8278c8c..0d3a457 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,14 +11,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `wpMiscUtilsRotl64` utility - `WpHasher` interface +- `wpCustomHasher` constructor for custom hasher with user-defined context - `wpHasherGetHash128` and `wpHasherGetHash64` utilities -- `wpMur3HasherData` to setup the MurmurHash3 data struct -- `wpX64Mur3Hasher` constructor for the hasher interface -- `wpX64Mur3Hasher128` implementation for the x64 128-bit variant of the MurmurHash3 algorithm -- `wpSip24HasherData` and `wpSip48HasherData` to setup the siphash data structs for the different variants -- `wpSipHasher` constructor for the hasher interface -- `wpSipHasher128` implementation for the 128 variant of siphash -- Add `wpU8Const`, `wpU16Const`, `wpU32Const`, `wpU64Const`, `wpI8Const`, `wpI16Const`, `wpI32Const`, and `wpI64Const` +- `wpX64Mur3Hasher` and `wpX64Mur3HasherWithSeed` constructors for the hasher interface +- `wpX64Mur3GetHash128` implementation for the x64 128-bit variant of the MurmurHash3 algorithm +- `wpSip24Hasher`, `wpSip48Hasher`, `wpSip24HasherWithKey` and `wpSip48HasherWithKey` constructors for the hasher interface +- `wpSipGetHash128` implementation for the 128 variant of siphash +- `wpU8Const`, `wpU16Const`, `wpU32Const`, `wpU64Const`, `wpI8Const`, `wpI16Const`, `wpI32Const`, and `wpI64Const` aliases - `wpPrngXorshiftInitWithSeed` to allow for user control with PRNG - `wpPrngXorshift256InRange`, `wpPrngXorshift256ssInRange`, `wpPrngXorshift256pInRange`, `wpPrngXorshift256Choice`, `wpPrngXorshift256ssChoice` and `wpPrngXorshift256pChoice` utilities - `WpSplitmix64State` diff --git a/src/base/hash/hasher/hasher.c b/src/base/hash/hasher/hasher.c index 2c18714..3cd95ca 100644 --- a/src/base/hash/hasher/hasher.c +++ b/src/base/hash/hasher/hasher.c @@ -3,13 +3,23 @@ #include "hasher.h" #include "../../../common/assert/assert.h" -WpU128Hash wpHasherGetHash128(const WpHasher *hasher, WpU8Stream *stream) { +WpHasher wpCustomHasher(WpHashFunc func, void *ctx) { + return (WpHasher){ + .func = func, + .ctx = (WpHasherCtx){ + .type = WP_HASHER_CTX_USER_DEFINED, + .user = ctx, + } + }; +} + +WpU128 wpHasherGetHash128(const WpHasher *hasher, WpU8Stream *stream) { wpDebugAssert(hasher != NULL && stream != NULL, "hasher and stream shouldn't be NULL"); - return hasher->func(stream, hasher->data); + return hasher->func(stream, hasher->ctx); } u64 wpHasherGetHash64(const WpHasher *hasher, WpU8Stream *stream) { wpDebugAssert(hasher != NULL && stream != NULL, "hasher and stream shouldn't be NULL"); - WpU128Hash result = hasher->func(stream, hasher->data); - return result.hash[0] ^ result.hash[1]; + WpU128 result = hasher->func(stream, hasher->ctx); + return result.value[0] ^ result.value[1]; } diff --git a/src/base/hash/hasher/hasher.h b/src/base/hash/hasher/hasher.h index 66ce4b7..36b236a 100644 --- a/src/base/hash/hasher/hasher.h +++ b/src/base/hash/hasher/hasher.h @@ -4,24 +4,49 @@ #define HASHER_H #include "../../stream/stream.h" +#include "../../../common/aliases/aliases.h" #ifdef WP_PLATFORM_CPP BEGIN_C_LINKAGE #endif // !WP_PLATFORM_CPP -typedef struct WpU128Hash { - u64 hash[2]; -} WpU128Hash; +typedef struct WpU128 { + u64 value[2]; +} WpU128; -typedef WpU128Hash (*WpHashFunc)(WpU8Stream *stream, void *hasher_data); +typedef enum WpHasherCtxType { + WP_HASHER_CTX_MURMUR3, + WP_HASHER_CTX_SIPHASH, + WP_HASHER_CTX_USER_DEFINED, + + COUNT_HASHER_CTX, +} WpHasherCtxType; + +typedef struct WpSiphashCtx { + WpU128 key; + u64 compression; + u64 finalisation; +} WpSiphashCtx; + +typedef struct WpHasherCtx { + WpHasherCtxType type; + union { + WpSiphashCtx siphash; + void *user; + u32 murmur3; + }; +} WpHasherCtx; + +typedef WpU128 (*WpHashFunc)(WpU8Stream *stream, WpHasherCtx ctx); typedef struct WpHasher { - WpHashFunc func; - void *data; + WpHashFunc func; + WpHasherCtx ctx; } WpHasher; -WpU128Hash wpHasherGetHash128(const WpHasher *hasher, WpU8Stream *stream); -u64 wpHasherGetHash64 (const WpHasher *hasher, WpU8Stream *stream); +WpHasher wpCustomHasher(WpHashFunc func, void *ctx); +WpU128 wpHasherGetHash128(const WpHasher *hasher, WpU8Stream *stream); +u64 wpHasherGetHash64(const WpHasher *hasher, WpU8Stream *stream); #ifdef WP_PLATFORM_CPP END_C_LINKAGE diff --git a/src/base/hash/hasher/murmur3.c b/src/base/hash/hasher/murmur3.c index 6a5e945..ea536a1 100644 --- a/src/base/hash/hasher/murmur3.c +++ b/src/base/hash/hasher/murmur3.c @@ -5,22 +5,34 @@ #include "../../../common/aliases/aliases.h" #include "../../../common/assert/assert.h" #include "../../../common/misc/misc_utils.h" +#include "../../../prng/xorshift/xorshift.h" -wp_intern inline void validateMur3HasherCtx(const WpMur3HasherCtx *io); +wp_intern inline void validateMur3HasherCtx(const WpHasherCtx *ctx); wp_intern inline u64 fmix64(u64 k); -WpHasher wpX64Mur3Hasher(WpMur3HasherCtx *data) { - return (WpHasher){ .func = wpX64Mur3Hasher128, .data = (void *)data }; +WpHasher wpX64Mur3Hasher(void) { + WpXor256State state = wpPrngXorshiftInit(); + u32 seed = (u32)wpPrngXorshift256(&state); + return wpX64Mur3HasherWithSeed(seed); } -WpU128Hash wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io) { - WpMur3HasherCtx *io = (WpMur3HasherCtx *)hasher_io; - validateMur3HasherCtx(io); +WpHasher wpX64Mur3HasherWithSeed(u32 seed) { + return (WpHasher){ + .func = wpX64Mur3GetHash128, + .ctx = (WpHasherCtx){ + .type = WP_HASHER_CTX_MURMUR3, + .murmur3 = seed, + }, + }; +} + +WpU128 wpX64Mur3GetHash128(WpU8Stream *bytes, WpHasherCtx ctx) { + validateMur3HasherCtx(&ctx); const i32 nblocks = (bytes->count * bytes->item_size) / 16; - u64 h1 = io->seed; - u64 h2 = io->seed; + u64 h1 = ctx.murmur3; + u64 h2 = ctx.murmur3; const u64 c1 = wpU64Const(0x87c37b91114253d5); const u64 c2 = wpU64Const(0x4cf5ad432745937f); @@ -96,11 +108,11 @@ WpU128Hash wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io) { h1 += h2; h2 += h1; - return (WpU128Hash){ .hash = { [0] = h1, [1] = h2 } }; + return (WpU128){ .value = { [0] = h1, [1] = h2 } }; } -wp_intern inline void validateMur3HasherCtx(const WpMur3HasherCtx *ctx) { - wpRuntimeAssert(ctx->magic == WP_MUR3_HASHER_MAGIC, "Invalid Murmur3 hash context"); +wp_intern inline void validateMur3HasherCtx(const WpHasherCtx *ctx) { + wpRuntimeAssert(ctx->type == WP_HASHER_CTX_MURMUR3, "Invalid Murmur3 hash context"); } wp_intern inline u64 fmix64(u64 k) { diff --git a/src/base/hash/hasher/murmur3.h b/src/base/hash/hasher/murmur3.h index b51aed2..37049d4 100644 --- a/src/base/hash/hasher/murmur3.h +++ b/src/base/hash/hasher/murmur3.h @@ -17,21 +17,9 @@ BEGIN_C_LINKAGE #endif // !WP_PLATFORM_CPP -#define WP_MUR3_HASHER_MAGIC wpU64Const(0x57504d4841534833) - -typedef struct WpMur3HasherCtx { - u64 magic; - u32 seed; -} WpMur3HasherCtx; - -#ifdef WP_PLATFORM_CPP -#define wpMur3HasherCtx(SEED) (WpMur3HasherCtx{WP_MUR3_HASHER_MAGIC, SEED}) -#else -#define wpMur3HasherCtx(SEED) ((WpMur3HasherCtx){ .magic = WP_MUR3_HASHER_MAGIC, .seed = SEED }) -#endif - -WpHasher wpX64Mur3Hasher(WpMur3HasherCtx *ctx); -WpU128Hash wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io); +WpHasher wpX64Mur3Hasher(void); +WpHasher wpX64Mur3HasherWithSeed(u32 seed); +WpU128 wpX64Mur3GetHash128(WpU8Stream *bytes, WpHasherCtx ctx); #ifdef WP_PLATFORM_CPP END_C_LINKAGE diff --git a/src/base/hash/hasher/siphash.c b/src/base/hash/hasher/siphash.c index 8ed1009..3afa139 100644 --- a/src/base/hash/hasher/siphash.c +++ b/src/base/hash/hasher/siphash.c @@ -5,10 +5,11 @@ #include "../../../common/aliases/aliases.h" #include "../../../common/assert/assert.h" #include "../../../common/misc/misc_utils.h" +#include "../../../prng/xorshift/xorshift.h" -wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8 *out, u64 outlen); -wp_intern inline void validateSipHasherCtx(const WpSipHasherCtx *io); -wp_intern inline b8 checkRounds(const WpSipHasherCtx *params); +wp_intern inline void siphash(WpStream *bytes, const WpHasherCtx *ctx, u8 *out, u64 outlen); +wp_intern inline void validateSipHasherCtx(const WpHasherCtx *ctx); +wp_intern inline b8 checkRounds(const WpHasherCtx *ctx); #define _u32To8LE(p, v) \ (p)[0] = (u8)((v)); \ @@ -44,24 +45,50 @@ wp_intern inline b8 checkRounds(const WpSipHasherCtx *params); v2 = wpMiscUtilsRotl64(v2, 32); \ } while (0) -WpHasher wpSipHasher(WpSipHasherCtx *data) { - return (WpHasher){ .func = wpSipHasher128, .data = (void *)data }; +WpHasher wpSip24Hasher(void) { + WpXor256State state = wpPrngXorshiftInit(); + WpU128 key = { .value = { wpPrngXorshift256(&state), wpPrngXorshift256(&state) } }; + return wpSip24HasherWithKey(key); } -WpU128Hash wpSipHasher128(WpStream *bytes, void *hasher_data) { - WpSipHasherCtx *data = (WpSipHasherCtx *)hasher_data; - validateSipHasherCtx(data); - WpU128Hash output = {0}; - siphash(bytes, data, (u8 *)&output.hash, sizeof(u64) * 2); +WpHasher wpSip48Hasher(void) { + WpXor256State state = wpPrngXorshiftInit(); + WpU128 key = { .value = { wpPrngXorshift256(&state), wpPrngXorshift256(&state) } }; + return wpSip48HasherWithKey(key); +} + +WpHasher wpSip24HasherWithKey(WpU128 key) { + return (WpHasher){ + .func = wpSipGetHash128, + .ctx = (WpHasherCtx){ + .type = WP_HASHER_CTX_SIPHASH, + .siphash = (WpSiphashCtx){ .key = key, .compression = 2, .finalisation = 4 }, + }, + }; +} + +WpHasher wpSip48HasherWithKey(WpU128 key) { + return (WpHasher){ + .func = wpSipGetHash128, + .ctx = (WpHasherCtx){ + .type = WP_HASHER_CTX_SIPHASH, + .siphash = (WpSiphashCtx){ .key = key, .compression = 4, .finalisation = 8 }, + }, + }; +} + +WpU128 wpSipGetHash128(WpStream *bytes, WpHasherCtx ctx) { + validateSipHasherCtx(&ctx); + WpU128 output = {0}; + siphash(bytes, &ctx, (u8 *)&output.value, sizeof(u64) * 2); return output; } /** * Implementation of the 128-variant of the siphash algorithm */ -wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8 *out, - u64 outlen) { - const u8 *kk = (const u8 *)¶ms->key; +wp_intern inline void siphash(WpStream *bytes, const WpHasherCtx *ctx, u8 *out, u64 outlen) { + const u8 *kk = (const u8 *)&ctx->siphash.key; u64 inlen = bytes->count * bytes->item_size; wpRuntimeAssert(outlen == 16, "outlen should be 16"); @@ -89,7 +116,7 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8 m = _u8To64LE(data); v3 ^= m; - for (i = 0; i < params->compression; ++i) { + for (i = 0; i < ctx->siphash.compression; ++i) { _sipRound; } @@ -125,7 +152,7 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8 v3 ^= b; - for (i = 0; i < params->compression; ++i) { + for (i = 0; i < ctx->siphash.compression; ++i) { _sipRound; } @@ -133,7 +160,7 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8 v2 ^= 0xee; - for (i = 0; i < params->finalisation; ++i) { + for (i = 0; i < ctx->siphash.finalisation; ++i) { _sipRound; } @@ -142,7 +169,7 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8 v1 ^= 0xdd; - for (i = 0; i < params->finalisation; ++i) { + for (i = 0; i < ctx->siphash.finalisation; ++i) { _sipRound; } @@ -150,14 +177,14 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8 _u64To8LE(out + 8, b); } -wp_intern inline void validateSipHasherCtx(const WpSipHasherCtx *ctx) { - b8 magic_match = ctx->magic == WP_SIP_HASHER_MAGIC; +wp_intern inline void validateSipHasherCtx(const WpHasherCtx *ctx) { + b8 magic_match = ctx->type == WP_HASHER_CTX_SIPHASH; b8 rounds = checkRounds(ctx); wpRuntimeAssert(magic_match && rounds, "Invalid Siphash context"); } -wp_intern inline b8 checkRounds(const WpSipHasherCtx *ctx) { - b8 rounds24 = ctx->compression == 2 && ctx->finalisation == 4; - b8 rounds48 = ctx->compression == 4 && ctx->finalisation == 8; +wp_intern inline b8 checkRounds(const WpHasherCtx *ctx) { + b8 rounds24 = ctx->siphash.compression == 2 && ctx->siphash.finalisation == 4; + b8 rounds48 = ctx->siphash.compression == 4 && ctx->siphash.finalisation == 8; return rounds24 || rounds48; } diff --git a/src/base/hash/hasher/siphash.h b/src/base/hash/hasher/siphash.h index f7dfddc..972a8d8 100644 --- a/src/base/hash/hasher/siphash.h +++ b/src/base/hash/hasher/siphash.h @@ -10,47 +10,17 @@ #include "hasher.h" #include "../../stream/stream.h" -#include "../../../common/aliases/aliases.h" #include "../../../common/platform/platform.h" #ifdef WP_PLATFORM_CPP BEGIN_C_LINKAGE #endif // !WP_PLATFORM_CPP -#define WP_SIP_HASHER_MAGIC wpU64Const(0x5750534950485348) - -typedef struct WpSipHasherKey { - u64 lo; - u64 hi; -} WpSipHasherKey; - -typedef struct WpSipHasherCtx { - WpSipHasherKey key; - u64 magic; - u64 compression; - u64 finalisation; -} WpSipHasherCtx; - -#ifdef WP_PLATFORM_CPP -#define wpSip24HasherCtx(KEY) (WpSipHasherCtx{(KEY), WP_SIP_HASHER_MAGIC, 2, 4}) -#define wpSip48HasherCtx(KEY) (WpSipHasherCtx{(KEY), WP_SIP_HASHER_MAGIC, 4, 8}) -#else -#define wpSip24HasherCtx(KEY) ((WpSipHasherCtx){ \ - .key = (KEY), \ - .magic = WP_SIP_HASHER_MAGIC, \ - .compression = 2, \ - .finalisation = 4, \ -}) -#define wpSip48HasherCtx(KEY) ((WpSipHasherCtx){ \ - .key = (KEY), \ - .magic = WP_SIP_HASHER_MAGIC, \ - .compression = 4, \ - .finalisation = 8, \ -}) -#endif - -WpHasher wpSipHasher(WpSipHasherCtx *ctx); -WpU128Hash wpSipHasher128(WpU8Stream *bytes, void *hasher_io); +WpHasher wpSip24Hasher(void); +WpHasher wpSip48Hasher(void); +WpHasher wpSip24HasherWithKey(WpU128 key); +WpHasher wpSip48HasherWithKey(WpU128 key); +WpU128 wpSipGetHash128(WpU8Stream *bytes, WpHasherCtx ctx); #ifdef WP_PLATFORM_CPP END_C_LINKAGE diff --git a/tests/hasher/test_hasher.c b/tests/hasher/test_hasher.c index 1da7d6b..b2c6439 100644 --- a/tests/hasher/test_hasher.c +++ b/tests/hasher/test_hasher.c @@ -12,7 +12,7 @@ WpTestFuncResult test_murmur3(void) { WpI32Array arr = wpArray(i32, 1, 2, 3, 4, 5); u64 num = 287324; - WpMur3HasherCtx ctx = wpMur3HasherCtx(873923); + WpHasher hasher = wpX64Mur3HasherWithSeed(873923); WpU8Stream str_bytes = wpStream(u8, str.buf, str.size); WpU8Stream arr_bytes = wpStream(u8, (u8 *)arr, wpArrayCount(arr) * wpArrayItemSize(arr)); @@ -21,22 +21,22 @@ WpTestFuncResult test_murmur3(void) { u64 hash[2] = {0}; // Sring hash - WpU128Hash mur_hash = wpX64Mur3Hasher128(&str_bytes, (void *)&ctx); - MurmurHash3_x64_128(str_bytes.data, str_bytes.count * str_bytes.item_size, ctx.seed, (void *)hash); + WpU128 mur_hash = hasher.func(&str_bytes, hasher.ctx); + MurmurHash3_x64_128(str_bytes.data, str_bytes.count * str_bytes.item_size, hasher.ctx.murmur3, (void *)hash); - result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; + result = result && mur_hash.value[0] == hash[0] && mur_hash.value[1] == hash[1]; // Array hash - mur_hash = wpX64Mur3Hasher128(&arr_bytes, (void *)&ctx); - MurmurHash3_x64_128(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, ctx.seed, (void *)hash); + mur_hash = hasher.func(&arr_bytes, hasher.ctx); + MurmurHash3_x64_128(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, hasher.ctx.murmur3, (void *)hash); - result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; + result = result && mur_hash.value[0] == hash[0] && mur_hash.value[1] == hash[1]; // Number hash - mur_hash = wpX64Mur3Hasher128(&num_bytes, (void *)&ctx); - MurmurHash3_x64_128(num_bytes.data, num_bytes.count * num_bytes.item_size, ctx.seed, (void *)hash); + mur_hash = hasher.func(&num_bytes, hasher.ctx); + MurmurHash3_x64_128(num_bytes.data, num_bytes.count * num_bytes.item_size, hasher.ctx.murmur3, (void *)hash); - result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; + result = result && mur_hash.value[0] == hash[0] && mur_hash.value[1] == hash[1]; return wpTesterResult(result); } @@ -48,8 +48,8 @@ WpTestFuncResult test_siphash_128(void) { WpI32Array arr = wpArray(i32, 1, 2, 3, 4, 5); u64 num = 287324; - WpSipHasherKey key = { .lo = 238742, .hi = 671734 }; - WpSipHasherCtx ctx = wpSip24HasherCtx(key); + WpU128 key = { .value = { 238742, 671734 } }; + WpHasher hasher = wpSip24HasherWithKey(key); WpU8Stream str_bytes = wpStream(u8, str.buf, str.size); WpU8Stream arr_bytes = wpStream(u8, (u8 *)arr, wpArrayCount(arr) * wpArrayItemSize(arr)); @@ -58,25 +58,25 @@ WpTestFuncResult test_siphash_128(void) { u64 hash[2] = {0}; // Sring hash - WpU128Hash sip_hash = wpSipHasher128(&str_bytes, (void *)&ctx); - siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&ctx.key, + WpU128 sip_hash = hasher.func(&str_bytes, hasher.ctx); + siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&hasher.ctx.siphash.key, (u8 *)hash, sizeof(u64) * 2); - result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; + result = result && sip_hash.value[0] == hash[0] && sip_hash.value[1] == hash[1]; // Array hash - sip_hash = wpSipHasher128(&arr_bytes, (void *)&ctx); - siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&ctx.key, + sip_hash = hasher.func(&arr_bytes, hasher.ctx); + siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&hasher.ctx.siphash.key, (u8 *)hash, sizeof(u64) * 2); - result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; + result = result && sip_hash.value[0] == hash[0] && sip_hash.value[1] == hash[1]; // Number hash - sip_hash = wpSipHasher128(&num_bytes, (void *)&ctx); - siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&ctx.key, + sip_hash = hasher.func(&num_bytes, hasher.ctx); + siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&hasher.ctx.siphash.key, (u8 *)hash, sizeof(u64) * 2); - result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; + result = result && sip_hash.value[0] == hash[0] && sip_hash.value[1] == hash[1]; return wpTesterResult(result); } diff --git a/tests/hasher/test_hasher.cc b/tests/hasher/test_hasher.cc index 9f8c9e8..6212d46 100644 --- a/tests/hasher/test_hasher.cc +++ b/tests/hasher/test_hasher.cc @@ -12,7 +12,7 @@ WpTestFuncResult test_murmur3(void) { WpI32Array arr = wpArray(i32, 1, 2, 3, 4, 5); u64 num = 287324; - WpMur3HasherCtx ctx = wpMur3HasherCtx(873923); + WpHasher hasher = wpX64Mur3HasherWithSeed(873923); WpU8Stream str_bytes = wpStream(u8, str.buf, str.size); WpU8Stream arr_bytes = wpStream(u8, (u8 *)arr, wpArrayCount(arr) * wpArrayItemSize(arr)); @@ -21,22 +21,22 @@ WpTestFuncResult test_murmur3(void) { u64 hash[2] = {0}; // Sring hash - WpU128Hash mur_hash = wpX64Mur3Hasher128(&str_bytes, (void *)&ctx); - MurmurHash3_x64_128(str_bytes.data, str_bytes.count * str_bytes.item_size, ctx.seed, (void *)hash); + WpU128 mur_hash = hasher.func(&str_bytes, hasher.ctx); + MurmurHash3_x64_128(str_bytes.data, str_bytes.count * str_bytes.item_size, hasher.ctx.murmur3, (void *)hash); - result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; + result = result && mur_hash.value[0] == hash[0] && mur_hash.value[1] == hash[1]; // Array hash - mur_hash = wpX64Mur3Hasher128(&arr_bytes, (void *)&ctx); - MurmurHash3_x64_128(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, ctx.seed, (void *)hash); + mur_hash = hasher.func(&arr_bytes, hasher.ctx); + MurmurHash3_x64_128(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, hasher.ctx.murmur3, (void *)hash); - result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; + result = result && mur_hash.value[0] == hash[0] && mur_hash.value[1] == hash[1]; // Number hash - mur_hash = wpX64Mur3Hasher128(&num_bytes, (void *)&ctx); - MurmurHash3_x64_128(num_bytes.data, num_bytes.count * num_bytes.item_size, ctx.seed, (void *)hash); + mur_hash = hasher.func(&num_bytes, hasher.ctx); + MurmurHash3_x64_128(num_bytes.data, num_bytes.count * num_bytes.item_size, hasher.ctx.murmur3, (void *)hash); - result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; + result = result && mur_hash.value[0] == hash[0] && mur_hash.value[1] == hash[1]; return wpTesterResult(result); } @@ -48,8 +48,8 @@ WpTestFuncResult test_siphash_128(void) { WpI32Array arr = wpArray(i32, 1, 2, 3, 4, 5); u64 num = 287324; - WpSipHasherKey key = { 238742, 671734 }; - WpSipHasherCtx ctx = wpSip24HasherCtx(key); + WpU128 key = { { 238742, 671734 } }; + WpHasher hasher = wpSip24HasherWithKey(key); WpU8Stream str_bytes = wpStream(u8, str.buf, str.size); WpU8Stream arr_bytes = wpStream(u8, (u8 *)arr, wpArrayCount(arr) * wpArrayItemSize(arr)); @@ -58,25 +58,25 @@ WpTestFuncResult test_siphash_128(void) { u64 hash[2] = {0}; // Sring hash - WpU128Hash sip_hash = wpSipHasher128(&str_bytes, (void *)&ctx); - siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&ctx.key, + WpU128 sip_hash = hasher.func(&str_bytes, hasher.ctx); + siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&hasher.ctx.siphash.key, (u8 *)hash, sizeof(u64) * 2); - result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; + result = result && sip_hash.value[0] == hash[0] && sip_hash.value[1] == hash[1]; // Array hash - sip_hash = wpSipHasher128(&arr_bytes, (void *)&ctx); - siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&ctx.key, + sip_hash = hasher.func(&arr_bytes, hasher.ctx); + siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&hasher.ctx.siphash.key, (u8 *)hash, sizeof(u64) * 2); - result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; + result = result && sip_hash.value[0] == hash[0] && sip_hash.value[1] == hash[1]; // Number hash - sip_hash = wpSipHasher128(&num_bytes, (void *)&ctx); - siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&ctx.key, + sip_hash = hasher.func(&num_bytes, hasher.ctx); + siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&hasher.ctx.siphash.key, (u8 *)hash, sizeof(u64) * 2); - result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; + result = result && sip_hash.value[0] == hash[0] && sip_hash.value[1] == hash[1]; return wpTesterResult(result); }