From 9881c0ac9a38bbcaae8f37757bb0162d18af79f9 Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Tue, 22 Sep 2026 21:47:46 +0100 Subject: [PATCH] Update hasher data to context --- src/base/hash/hasher/murmur3.c | 12 ++++++------ src/base/hash/hasher/murmur3.h | 10 +++++----- src/base/hash/hasher/siphash.c | 28 ++++++++++++++-------------- src/base/hash/hasher/siphash.h | 14 +++++++------- tests/hasher/test_hasher.c | 30 +++++++++++++++--------------- tests/hasher/test_hasher.cc | 30 +++++++++++++++--------------- 6 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/base/hash/hasher/murmur3.c b/src/base/hash/hasher/murmur3.c index 1167b8d..6a5e945 100644 --- a/src/base/hash/hasher/murmur3.c +++ b/src/base/hash/hasher/murmur3.c @@ -6,16 +6,16 @@ #include "../../../common/assert/assert.h" #include "../../../common/misc/misc_utils.h" -wp_intern inline void validateMur3HasherData(const WpMur3HasherData *io); +wp_intern inline void validateMur3HasherCtx(const WpMur3HasherCtx *io); wp_intern inline u64 fmix64(u64 k); -WpHasher wpX64Mur3Hasher(WpMur3HasherData *data) { +WpHasher wpX64Mur3Hasher(WpMur3HasherCtx *data) { return (WpHasher){ .func = wpX64Mur3Hasher128, .data = (void *)data }; } WpU128Hash wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io) { - WpMur3HasherData *io = (WpMur3HasherData *)hasher_io; - validateMur3HasherData(io); + WpMur3HasherCtx *io = (WpMur3HasherCtx *)hasher_io; + validateMur3HasherCtx(io); const i32 nblocks = (bytes->count * bytes->item_size) / 16; @@ -99,8 +99,8 @@ WpU128Hash wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io) { return (WpU128Hash){ .hash = { [0] = h1, [1] = h2 } }; } -wp_intern inline void validateMur3HasherData(const WpMur3HasherData *data) { - wpRuntimeAssert(data->magic == WP_MUR3_HASHER_MAGIC, "Invalid Murmur3 hash data"); +wp_intern inline void validateMur3HasherCtx(const WpMur3HasherCtx *ctx) { + wpRuntimeAssert(ctx->magic == WP_MUR3_HASHER_MAGIC, "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 03870fb..b51aed2 100644 --- a/src/base/hash/hasher/murmur3.h +++ b/src/base/hash/hasher/murmur3.h @@ -19,18 +19,18 @@ BEGIN_C_LINKAGE #define WP_MUR3_HASHER_MAGIC wpU64Const(0x57504d4841534833) -typedef struct WpMur3HasherData { +typedef struct WpMur3HasherCtx { u64 magic; u32 seed; -} WpMur3HasherData; +} WpMur3HasherCtx; #ifdef WP_PLATFORM_CPP -#define wpMur3HasherData(SEED) (WpMur3HasherData{WP_MUR3_HASHER_MAGIC, SEED}) +#define wpMur3HasherCtx(SEED) (WpMur3HasherCtx{WP_MUR3_HASHER_MAGIC, SEED}) #else -#define wpMur3HasherData(SEED) ((WpMur3HasherData){ .magic = WP_MUR3_HASHER_MAGIC, .seed = SEED }) +#define wpMur3HasherCtx(SEED) ((WpMur3HasherCtx){ .magic = WP_MUR3_HASHER_MAGIC, .seed = SEED }) #endif -WpHasher wpX64Mur3Hasher(WpMur3HasherData *data); +WpHasher wpX64Mur3Hasher(WpMur3HasherCtx *ctx); WpU128Hash wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io); #ifdef WP_PLATFORM_CPP diff --git a/src/base/hash/hasher/siphash.c b/src/base/hash/hasher/siphash.c index 70916f3..8ed1009 100644 --- a/src/base/hash/hasher/siphash.c +++ b/src/base/hash/hasher/siphash.c @@ -6,9 +6,9 @@ #include "../../../common/assert/assert.h" #include "../../../common/misc/misc_utils.h" -wp_intern inline void siphash(WpStream *bytes, const WpSipHasherData *params, u8 *out, u64 outlen); -wp_intern inline void validateSipHasherData(const WpSipHasherData *io); -wp_intern inline b8 checkRounds(const WpSipHasherData *params); +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); #define _u32To8LE(p, v) \ (p)[0] = (u8)((v)); \ @@ -44,13 +44,13 @@ wp_intern inline b8 checkRounds(const WpSipHasherData *params); v2 = wpMiscUtilsRotl64(v2, 32); \ } while (0) -WpHasher wpSipHasher(WpSipHasherData *data) { +WpHasher wpSipHasher(WpSipHasherCtx *data) { return (WpHasher){ .func = wpSipHasher128, .data = (void *)data }; } WpU128Hash wpSipHasher128(WpStream *bytes, void *hasher_data) { - WpSipHasherData *data = (WpSipHasherData *)hasher_data; - validateSipHasherData(data); + WpSipHasherCtx *data = (WpSipHasherCtx *)hasher_data; + validateSipHasherCtx(data); WpU128Hash output = {0}; siphash(bytes, data, (u8 *)&output.hash, sizeof(u64) * 2); return output; @@ -59,7 +59,7 @@ WpU128Hash wpSipHasher128(WpStream *bytes, void *hasher_data) { /** * Implementation of the 128-variant of the siphash algorithm */ -wp_intern inline void siphash(WpStream *bytes, const WpSipHasherData *params, u8 *out, +wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8 *out, u64 outlen) { const u8 *kk = (const u8 *)¶ms->key; u64 inlen = bytes->count * bytes->item_size; @@ -150,14 +150,14 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherData *params, u8 _u64To8LE(out + 8, b); } -wp_intern inline void validateSipHasherData(const WpSipHasherData *data) { - b8 magic_match = data->magic == WP_SIP_HASHER_MAGIC; - b8 rounds = checkRounds(data); - wpRuntimeAssert(magic_match && rounds, "Invalid Siphash data"); +wp_intern inline void validateSipHasherCtx(const WpSipHasherCtx *ctx) { + b8 magic_match = ctx->magic == WP_SIP_HASHER_MAGIC; + b8 rounds = checkRounds(ctx); + wpRuntimeAssert(magic_match && rounds, "Invalid Siphash context"); } -wp_intern inline b8 checkRounds(const WpSipHasherData *data) { - b8 rounds24 = data->compression == 2 && data->finalisation == 4; - b8 rounds48 = data->compression == 4 && data->finalisation == 8; +wp_intern inline b8 checkRounds(const WpSipHasherCtx *ctx) { + b8 rounds24 = ctx->compression == 2 && ctx->finalisation == 4; + b8 rounds48 = ctx->compression == 4 && ctx->finalisation == 8; return rounds24 || rounds48; } diff --git a/src/base/hash/hasher/siphash.h b/src/base/hash/hasher/siphash.h index bfe7e17..f7dfddc 100644 --- a/src/base/hash/hasher/siphash.h +++ b/src/base/hash/hasher/siphash.h @@ -24,24 +24,24 @@ typedef struct WpSipHasherKey { u64 hi; } WpSipHasherKey; -typedef struct WpSipHasherData { +typedef struct WpSipHasherCtx { WpSipHasherKey key; u64 magic; u64 compression; u64 finalisation; -} WpSipHasherData; +} WpSipHasherCtx; #ifdef WP_PLATFORM_CPP -#define wpSip24HasherData(KEY) (WpSipHasherData{(KEY), WP_SIP_HASHER_MAGIC, 2, 4}) -#define wpSip48HasherData(KEY) (WpSipHasherData{(KEY), WP_SIP_HASHER_MAGIC, 4, 8}) +#define wpSip24HasherCtx(KEY) (WpSipHasherCtx{(KEY), WP_SIP_HASHER_MAGIC, 2, 4}) +#define wpSip48HasherCtx(KEY) (WpSipHasherCtx{(KEY), WP_SIP_HASHER_MAGIC, 4, 8}) #else -#define wpSip24HasherData(KEY) ((WpSipHasherData){ \ +#define wpSip24HasherCtx(KEY) ((WpSipHasherCtx){ \ .key = (KEY), \ .magic = WP_SIP_HASHER_MAGIC, \ .compression = 2, \ .finalisation = 4, \ }) -#define wpSip48HasherData(KEY) ((WpSipHasherData){ \ +#define wpSip48HasherCtx(KEY) ((WpSipHasherCtx){ \ .key = (KEY), \ .magic = WP_SIP_HASHER_MAGIC, \ .compression = 4, \ @@ -49,7 +49,7 @@ typedef struct WpSipHasherData { }) #endif -WpHasher wpSipHasher(WpSipHasherData *data); +WpHasher wpSipHasher(WpSipHasherCtx *ctx); WpU128Hash wpSipHasher128(WpU8Stream *bytes, void *hasher_io); #ifdef WP_PLATFORM_CPP diff --git a/tests/hasher/test_hasher.c b/tests/hasher/test_hasher.c index c6a65e5..1da7d6b 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; - WpMur3HasherData data = wpMur3HasherData(873923); + WpMur3HasherCtx ctx = wpMur3HasherCtx(873923); WpU8Stream str_bytes = wpStream(u8, str.buf, str.size); WpU8Stream arr_bytes = wpStream(u8, (u8 *)arr, wpArrayCount(arr) * wpArrayItemSize(arr)); @@ -21,20 +21,20 @@ WpTestFuncResult test_murmur3(void) { u64 hash[2] = {0}; // Sring hash - WpU128Hash mur_hash = wpX64Mur3Hasher128(&str_bytes, (void *)&data); - MurmurHash3_x64_128(str_bytes.data, str_bytes.count * str_bytes.item_size, data.seed, (void *)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); result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; // Array hash - mur_hash = wpX64Mur3Hasher128(&arr_bytes, (void *)&data); - MurmurHash3_x64_128(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, data.seed, (void *)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); result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; // Number hash - mur_hash = wpX64Mur3Hasher128(&num_bytes, (void *)&data); - MurmurHash3_x64_128(num_bytes.data, num_bytes.count * num_bytes.item_size, data.seed, (void *)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); result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; @@ -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 }; - WpSipHasherData data = wpSip24HasherData(key); + WpSipHasherKey key = { .lo = 238742, .hi = 671734 }; + WpSipHasherCtx ctx = wpSip24HasherCtx(key); WpU8Stream str_bytes = wpStream(u8, str.buf, str.size); WpU8Stream arr_bytes = wpStream(u8, (u8 *)arr, wpArrayCount(arr) * wpArrayItemSize(arr)); @@ -58,22 +58,22 @@ WpTestFuncResult test_siphash_128(void) { u64 hash[2] = {0}; // Sring hash - WpU128Hash sip_hash = wpSipHasher128(&str_bytes, (void *)&data); - siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&data.key, + WpU128Hash sip_hash = wpSipHasher128(&str_bytes, (void *)&ctx); + siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&ctx.key, (u8 *)hash, sizeof(u64) * 2); result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; // Array hash - sip_hash = wpSipHasher128(&arr_bytes, (void *)&data); - siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&data.key, + sip_hash = wpSipHasher128(&arr_bytes, (void *)&ctx); + siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&ctx.key, (u8 *)hash, sizeof(u64) * 2); result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; // Number hash - sip_hash = wpSipHasher128(&num_bytes, (void *)&data); - siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&data.key, + sip_hash = wpSipHasher128(&num_bytes, (void *)&ctx); + siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&ctx.key, (u8 *)hash, sizeof(u64) * 2); result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; diff --git a/tests/hasher/test_hasher.cc b/tests/hasher/test_hasher.cc index 7084641..9f8c9e8 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; - WpMur3HasherData data = wpMur3HasherData(873923); + WpMur3HasherCtx ctx = wpMur3HasherCtx(873923); WpU8Stream str_bytes = wpStream(u8, str.buf, str.size); WpU8Stream arr_bytes = wpStream(u8, (u8 *)arr, wpArrayCount(arr) * wpArrayItemSize(arr)); @@ -21,20 +21,20 @@ WpTestFuncResult test_murmur3(void) { u64 hash[2] = {0}; // Sring hash - WpU128Hash mur_hash = wpX64Mur3Hasher128(&str_bytes, (void *)&data); - MurmurHash3_x64_128(str_bytes.data, str_bytes.count * str_bytes.item_size, data.seed, (void *)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); result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; // Array hash - mur_hash = wpX64Mur3Hasher128(&arr_bytes, (void *)&data); - MurmurHash3_x64_128(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, data.seed, (void *)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); result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; // Number hash - mur_hash = wpX64Mur3Hasher128(&num_bytes, (void *)&data); - MurmurHash3_x64_128(num_bytes.data, num_bytes.count * num_bytes.item_size, data.seed, (void *)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); result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; @@ -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 }; - WpSipHasherData data = wpSip24HasherData(key); + WpSipHasherKey key = { 238742, 671734 }; + WpSipHasherCtx ctx = wpSip24HasherCtx(key); WpU8Stream str_bytes = wpStream(u8, str.buf, str.size); WpU8Stream arr_bytes = wpStream(u8, (u8 *)arr, wpArrayCount(arr) * wpArrayItemSize(arr)); @@ -58,22 +58,22 @@ WpTestFuncResult test_siphash_128(void) { u64 hash[2] = {0}; // Sring hash - WpU128Hash sip_hash = wpSipHasher128(&str_bytes, (void *)&data); - siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&data.key, + WpU128Hash sip_hash = wpSipHasher128(&str_bytes, (void *)&ctx); + siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&ctx.key, (u8 *)hash, sizeof(u64) * 2); result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; // Array hash - sip_hash = wpSipHasher128(&arr_bytes, (void *)&data); - siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&data.key, + sip_hash = wpSipHasher128(&arr_bytes, (void *)&ctx); + siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&ctx.key, (u8 *)hash, sizeof(u64) * 2); result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; // Number hash - sip_hash = wpSipHasher128(&num_bytes, (void *)&data); - siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&data.key, + sip_hash = wpSipHasher128(&num_bytes, (void *)&ctx); + siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&ctx.key, (u8 *)hash, sizeof(u64) * 2); result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1];