diff --git a/CHANGELOG.md b/CHANGELOG.md index 883ecf3..e0b2238 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,9 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `wpMiscUtilsRotl64` utility -- `wpMur3HasherIO` to setup the MurmurHash3 IO struct +- `wpMur3HasherData` to setup the MurmurHash3 data struct - `wpX64Mur3Hasher128` implementation for the x64 128-bit variant of the MurmurHash3 algorithm -- `wpSip24Hasher128IO`, `wpSip48Hasher128IO`, `wpSip24Hasher64IO` and `wpSip48Hasher64IO` to setup the siphash IO structs for the different variants +- `wpSip24HasherData` and `wpSip48HasherData` to setup the siphash data structs for the different variants - `wpSipHasher128` and `wpSipHasher64` implementation for the 128 and 64 variants of siphash - Add `wpU8Const`, `wpU16Const`, `wpU32Const`, `wpU64Const`, `wpI8Const`, `wpI16Const`, `wpI32Const`, and `wpI64Const` - `wpPrngXorshiftInitWithSeed` to allow for user control with PRNG diff --git a/src/base/hash/hasher/hasher.h b/src/base/hash/hasher/hasher.h index c03301a..c31908c 100644 --- a/src/base/hash/hasher/hasher.h +++ b/src/base/hash/hasher/hasher.h @@ -9,7 +9,35 @@ BEGIN_C_LINKAGE #endif // !WP_PLATFORM_CPP -typedef void (*WpHasher)(WpU8Stream *stream, void *hasher_io); +typedef struct { + u64 hash[2]; +} WpU128Hash; + +typedef enum { + WP_HASH_TYPE_64, + WP_HASH_TYPE_128, + + COUNT_HASH_TYPES, +} WpHasherType; + +typedef u64 (*WpHasher64) (WpU8Stream *stream, void *hasher_data); +typedef WpU128Hash (*WpHasher128)(WpU8Stream *stream, void *hasher_data); + +typedef struct { + WpHasherType type; + union { + WpHasher64 hasher_64; + WpHasher128 hasher_128; + }; +} WpHasher; + +#ifdef WP_PLATFORM_CPP +#define wpHasher64(HASHER) (WpHasher64{ WP_HASH_TYPE_64, (HASHER) }) +#define wpHasher128(HASHER) (WpHasher128{ WP_HASH_TYPE_128, (HASHER) }) +#else +#define wpHasher64(HASHER) ((WpHasher64){ .type = WP_HASH_TYPE_64, .hasher_64 = (HASHER) }) +#define wpHasher128(HASHER) ((WpHasher128){ .type = WP_HASH_TYPE_128, .hasher_128 = (HASHER) }) +#endif #ifdef WP_PLATFORM_CPP END_C_LINKAGE diff --git a/src/base/hash/hasher/murmur3.c b/src/base/hash/hasher/murmur3.c index 912599f..b00f5d1 100644 --- a/src/base/hash/hasher/murmur3.c +++ b/src/base/hash/hasher/murmur3.c @@ -1,16 +1,17 @@ // vim:fileencoding=utf-8:foldmethod=marker #include "murmur3.h" +#include "hasher.h" #include "../../../common/aliases/aliases.h" #include "../../../common/assert/assert.h" #include "../../../common/misc/misc_utils.h" -wp_intern inline void validateIO(const WpMur3HasherIO *io); +wp_intern inline void validateMur3HasherData(const WpMur3HasherData *io); wp_intern inline u64 fmix64(u64 k); -void wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io) { - WpMur3HasherIO *io = (WpMur3HasherIO *)hasher_io; - validateIO(io); +WpU128Hash wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io) { + WpMur3HasherData *io = (WpMur3HasherData *)hasher_io; + validateMur3HasherData(io); const i32 nblocks = (bytes->count * bytes->item_size) / 16; @@ -91,12 +92,11 @@ void wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io) { h1 += h2; h2 += h1; - io->hash[0] = h1; - io->hash[1] = h2; + return (WpU128Hash){ .hash = { [0] = h1, [1] = h2 } }; } -wp_intern inline void validateIO(const WpMur3HasherIO *io) { - wpRuntimeAssert(io->magic == WP_MUR3_HASHER_MAGIC, "Invalid Murmur3 hash IO"); +wp_intern inline void validateMur3HasherData(const WpMur3HasherData *data) { + wpRuntimeAssert(data->magic == WP_MUR3_HASHER_MAGIC, "Invalid Murmur3 hash data"); } wp_intern inline u64 fmix64(u64 k) { diff --git a/src/base/hash/hasher/murmur3.h b/src/base/hash/hasher/murmur3.h index 2dd16b5..bb4019a 100644 --- a/src/base/hash/hasher/murmur3.h +++ b/src/base/hash/hasher/murmur3.h @@ -8,6 +8,7 @@ #ifndef MURMUR3_H #define MURMUR3_H +#include "hasher.h" #include "../../stream/stream.h" #include "../../../common/aliases/aliases.h" #include "../../../common/platform/platform.h" @@ -19,18 +20,17 @@ BEGIN_C_LINKAGE #define WP_MUR3_HASHER_MAGIC wpU64Const(0x57504d4841534833) typedef struct { - u64 hash[2]; u64 magic; u32 seed; -} WpMur3HasherIO; +} WpMur3HasherData; #ifdef WP_PLATFORM_CPP -#define wpMur3HasherIO(SEED) (WpMur3HasherIO{{}, WP_MUR3_HASHER_MAGIC, SEED}) +#define wpMur3HasherData(SEED) (WpMur3HasherData{WP_MUR3_HASHER_MAGIC, SEED}) #else -#define wpMur3HasherIO(SEED) ((WpMur3HasherIO){ .magic = WP_MUR3_HASHER_MAGIC, .seed = SEED }) +#define wpMur3HasherData(SEED) ((WpMur3HasherData){ .magic = WP_MUR3_HASHER_MAGIC, .seed = SEED }) #endif -void wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io); +WpU128Hash wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io); #ifdef WP_PLATFORM_CPP END_C_LINKAGE diff --git a/src/base/hash/hasher/siphash.c b/src/base/hash/hasher/siphash.c index d7e8399..b4aefec 100644 --- a/src/base/hash/hasher/siphash.c +++ b/src/base/hash/hasher/siphash.c @@ -1,14 +1,14 @@ // vim:fileencoding=utf-8:foldmethod=marker #include "siphash.h" +#include "hasher.h" #include "../../../common/aliases/aliases.h" #include "../../../common/assert/assert.h" #include "../../../common/misc/misc_utils.h" -wp_intern inline void siphash(WpStream *bytes, const WpSipHasherParams *params, u8 *out, u64 outlen); -wp_intern inline void validate128IO(const WpSipHasher128IO *io); -wp_intern inline void validate64IO(const WpSipHasher64IO *io); -wp_intern inline b8 checkRounds(const WpSipHasherParams *params); +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); #define _u32To8LE(p, v) \ (p)[0] = (u8)((v)); \ @@ -44,20 +44,24 @@ wp_intern inline b8 checkRounds(const WpSipHasherParams *params); v2 = wpMiscUtilsRotl64(v2, 32); \ } while (0) -void wpSipHasher128(WpStream *bytes, void *hasher_io) { - WpSipHasher128IO *io = (WpSipHasher128IO *)hasher_io; - validate128IO(io); - siphash(bytes, &io->params, (u8 *)&io->hash, sizeof(u64) * 2); +WpU128Hash wpSipHasher128(WpStream *bytes, void *hasher_data) { + WpSipHasherData *data = (WpSipHasherData *)hasher_data; + validateSipHasherData(data); + WpU128Hash output = {0}; + siphash(bytes, data, (u8 *)&output.hash, sizeof(u64) * 2); + return output; } -void wpSipHasher64(WpStream *bytes, void *hasher_io) { - WpSipHasher64IO *io = (WpSipHasher64IO *)hasher_io; - validate64IO(io); - siphash(bytes, &io->params, (u8 *)&io->hash, sizeof(u64)); +u64 wpSipHasher64(WpStream *bytes, void *hasher_data) { + WpSipHasherData *data = (WpSipHasherData *)hasher_data; + validateSipHasherData(data); + u64 output = 0; + siphash(bytes, data, (u8 *)&output, sizeof(u64)); + return output; } -wp_intern inline void siphash(WpStream *bytes, const WpSipHasherParams *params, u8 *out, - u64 outlen) { +wp_intern inline void siphash(WpStream *bytes, const WpSipHasherData *params, u8 *out, + u64 outlen) { const u8 *kk = (const u8 *)¶ms->key; u64 inlen = bytes->count * bytes->item_size; @@ -157,20 +161,14 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherParams *params, _u64To8LE(out + 8, b); } -wp_intern inline void validate128IO(const WpSipHasher128IO *io) { - b8 magic_match = io->params.magic == WP_SIP_HASHER_128_MAGIC; - b8 rounds = checkRounds(&io->params); - wpRuntimeAssert(magic_match && rounds, "Invalid Siphash 128 IO"); +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 validate64IO(const WpSipHasher64IO *io) { - b8 magic_match = io->params.magic == WP_SIP_HASHER_64_MAGIC; - b8 rounds = checkRounds(&io->params); - wpRuntimeAssert(magic_match && rounds, "Invalid Siphash 64 IO"); -} - -wp_intern inline b8 checkRounds(const WpSipHasherParams *params) { - b8 rounds24 = params->compression == 2 && params->finalisation == 4; - b8 rounds48 = params->compression == 4 && params->finalisation == 8; +wp_intern inline b8 checkRounds(const WpSipHasherData *data) { + b8 rounds24 = data->compression == 2 && data->finalisation == 4; + b8 rounds48 = data->compression == 4 && data->finalisation == 8; return rounds24 || rounds48; } diff --git a/src/base/hash/hasher/siphash.h b/src/base/hash/hasher/siphash.h index bdef8cf..f51c690 100644 --- a/src/base/hash/hasher/siphash.h +++ b/src/base/hash/hasher/siphash.h @@ -8,6 +8,7 @@ #ifndef SIPHASH_H #define SIPHASH_H +#include "hasher.h" #include "../../stream/stream.h" #include "../../../common/aliases/aliases.h" #include "../../../common/platform/platform.h" @@ -16,8 +17,7 @@ BEGIN_C_LINKAGE #endif // !WP_PLATFORM_CPP -#define WP_SIP_HASHER_128_MAGIC wpU64Const(0x5750534950313238) -#define WP_SIP_HASHER_64_MAGIC wpU64Const(0x57505349503634) +#define WP_SIP_HASHER_MAGIC wpU64Const(0x5750534950485348) typedef struct { u64 lo; @@ -29,72 +29,28 @@ typedef struct { u64 magic; u64 compression; u64 finalisation; -} WpSipHasherParams; - -typedef struct { - WpSipHasherParams params; - u64 hash[2]; -} WpSipHasher128IO; - -typedef struct { - WpSipHasherParams params; - u64 hash; -} WpSipHasher64IO; +} WpSipHasherData; #ifdef WP_PLATFORM_CPP -#define wpSip24Hasher128IO(KEY) (WpSipHasher128IO{ \ - WpSipHasherParams{KEY, WP_SIP_HASHER_128_MAGIC, 2, 4}, \ - {}, \ -}) -#define wpSip48Hasher128IO(KEY) (WpSipHasher128IO{ \ - WpSipHasherParams{KEY, WP_SIP_HASHER_128_MAGIC, 4, 8}, \ - {}, \ -}) -#define wpSip24Hasher64IO(KEY) (WpSipHasher64IO{ \ - WpSipHasherParams{KEY, WP_SIP_HASHER_64_MAGIC, 2, 4}, \ - 0, \ -}) -#define wpSip48Hasher64IO(KEY) (WpSipHasher64IO{ \ - WpSipHasherParams{KEY, WP_SIP_HASHER_64_MAGIC, 4, 8}, \ - 0, \ -}) +#define wpSip24HasherData(KEY) (WpSipHasherData{KEY, WP_SIP_HASHER_MAGIC, 2, 4}) +#define wpSip48HasherData(KEY) (WpSipHasherData{KEY, WP_SIP_HASHER_MAGIC, 4, 8}) #else -#define wpSip24Hasher128IO(KEY) ((WpSipHasher128IO){ \ - .params = (WpSipHasherParams){ \ - .key = KEY, \ - .magic = WP_SIP_HASHER_128_MAGIC, \ - .compression = 2, \ - .finalisation = 4, \ - } \ +#define wpSip24HasherData(KEY) ((WpSipHasherData){ \ + .key = KEY, \ + .magic = WP_SIP_HASHER_MAGIC, \ + .compression = 2, \ + .finalisation = 4, \ }) -#define wpSip48Hasher128IO(KEY) ((WpSipHasher128IO){ \ - .params = (WpSipHasherParams){ \ - .key = KEY, \ - .magic = WP_SIP_HASHER_128_MAGIC, \ - .compression = 4, \ - .finalisation = 8, \ - } \ -}) -#define wpSip24Hasher64IO(KEY) ((WpSipHasher64IO){ \ - .params = (WpSipHasherParams){ \ - .key = KEY, \ - .magic = WP_SIP_HASHER_64_MAGIC, \ - .compression = 2, \ - .finalisation = 4, \ - } \ -}) -#define wpSip48Hasher64IO(KEY) ((WpSipHasher64IO){ \ - .params = (WpSipHasherParams){ \ - .key = KEY, \ - .magic = WP_SIP_HASHER_64_MAGIC, \ - .compression = 4, \ - .finalisation = 8, \ - } \ +#define wpSip48HasherData(KEY) ((WpSipHasherData){ \ + .key = KEY, \ + .magic = WP_SIP_HASHER_MAGIC, \ + .compression = 4, \ + .finalisation = 8, \ }) #endif -void wpSipHasher128(WpU8Stream *bytes, void *hasher_io); -void wpSipHasher64(WpU8Stream *bytes, void *hasher_io); +WpU128Hash wpSipHasher128(WpU8Stream *bytes, void *hasher_io); +u64 wpSipHasher64(WpU8Stream *bytes, void *hasher_io); #ifdef WP_PLATFORM_CPP END_C_LINKAGE diff --git a/tests/hasher/test_hasher.c b/tests/hasher/test_hasher.c index 76c9555..0559efc 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; - WpMur3HasherIO io = wpMur3HasherIO(873923); + WpMur3HasherData data = wpMur3HasherData(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 - wpX64Mur3Hasher128(&str_bytes, (void *)&io); - MurmurHash3_x64_128(str_bytes.data, str_bytes.count * str_bytes.item_size, io.seed, (void *)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); - result = result && io.hash[0] == hash[0] && io.hash[1] == hash[1]; + result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; // Array hash - wpX64Mur3Hasher128(&arr_bytes, (void *)&io); - MurmurHash3_x64_128(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, io.seed, (void *)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); - result = result && io.hash[0] == hash[0] && io.hash[1] == hash[1]; + result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; // Number hash - wpX64Mur3Hasher128(&num_bytes, (void *)&io); - MurmurHash3_x64_128(num_bytes.data, num_bytes.count * num_bytes.item_size, io.seed, (void *)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); - result = result && io.hash[0] == hash[0] && io.hash[1] == hash[1]; + result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[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 }; - WpSipHasher128IO io = wpSip24Hasher128IO(key); + WpSipHasherKey key = { .lo = 238742, .hi = 671734 }; + WpSipHasherData data = wpSip24HasherData(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 - wpSipHasher128(&str_bytes, (void *)&io); - siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&io.params.key, + WpU128Hash sip_hash = wpSipHasher128(&str_bytes, (void *)&data); + siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&data.key, (u8 *)hash, sizeof(u64) * 2); - result = result && io.hash[0] == hash[0] && io.hash[1] == hash[1]; + result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; // Array hash - wpSipHasher128(&arr_bytes, (void *)&io); - siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&io.params.key, + sip_hash = wpSipHasher128(&arr_bytes, (void *)&data); + siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&data.key, (u8 *)hash, sizeof(u64) * 2); - result = result && io.hash[0] == hash[0] && io.hash[1] == hash[1]; + result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; // Number hash - wpSipHasher128(&num_bytes, (void *)&io); - siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&io.params.key, + sip_hash = wpSipHasher128(&num_bytes, (void *)&data); + siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&data.key, (u8 *)hash, sizeof(u64) * 2); - result = result && io.hash[0] == hash[0] && io.hash[1] == hash[1]; + result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; return wpTesterResult(result); } @@ -88,8 +88,8 @@ WpTestFuncResult test_siphash_64(void) { WpI32Array arr = wpArray(i32, 1, 2, 3, 4, 5); u64 num = 287324; - WpSipHasherKey key = { .lo = 238742, .hi = 671734 }; - WpSipHasher64IO io = wpSip24Hasher64IO(key); + WpSipHasherKey key = { .lo = 238742, .hi = 671734 }; + WpSipHasherData data = wpSip24HasherData(key); WpU8Stream str_bytes = wpStream(u8, str.buf, str.size); WpU8Stream arr_bytes = wpStream(u8, (u8 *)arr, wpArrayCount(arr) * wpArrayItemSize(arr)); @@ -98,25 +98,25 @@ WpTestFuncResult test_siphash_64(void) { u64 hash = {0}; // Sring hash - wpSipHasher64(&str_bytes, (void *)&io); - siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&io.params.key, + u64 sip_hash = wpSipHasher64(&str_bytes, (void *)&data); + siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&data.key, (u8 *)&hash, sizeof(u64)); - result = result && io.hash == hash; + result = result && sip_hash == hash; // Array hash - wpSipHasher64(&arr_bytes, (void *)&io); - siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&io.params.key, + sip_hash = wpSipHasher64(&arr_bytes, (void *)&data); + siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&data.key, (u8 *)&hash, sizeof(u64)); - result = result && io.hash == hash; + result = result && sip_hash == hash; // Number hash - wpSipHasher64(&num_bytes, (void *)&io); - siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&io.params.key, + sip_hash = wpSipHasher64(&num_bytes, (void *)&data); + siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&data.key, (u8 *)&hash, sizeof(u64)); - result = result && io.hash == hash; + result = result && sip_hash == hash; return wpTesterResult(result); } diff --git a/tests/hasher/test_hasher.cc b/tests/hasher/test_hasher.cc index b24a950..2297d32 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; - WpMur3HasherIO io = wpMur3HasherIO(873923); + WpMur3HasherData data = wpMur3HasherData(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 - wpX64Mur3Hasher128(&str_bytes, (void *)&io); - MurmurHash3_x64_128(str_bytes.data, str_bytes.count * str_bytes.item_size, io.seed, (void *)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); - result = result && io.hash[0] == hash[0] && io.hash[1] == hash[1]; + result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; // Array hash - wpX64Mur3Hasher128(&arr_bytes, (void *)&io); - MurmurHash3_x64_128(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, io.seed, (void *)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); - result = result && io.hash[0] == hash[0] && io.hash[1] == hash[1]; + result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[1] == hash[1]; // Number hash - wpX64Mur3Hasher128(&num_bytes, (void *)&io); - MurmurHash3_x64_128(num_bytes.data, num_bytes.count * num_bytes.item_size, io.seed, (void *)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); - result = result && io.hash[0] == hash[0] && io.hash[1] == hash[1]; + result = result && mur_hash.hash[0] == hash[0] && mur_hash.hash[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 }; - WpSipHasher128IO io = wpSip24Hasher128IO(key); + WpSipHasherKey key = { 238742, 671734 }; + WpSipHasherData data = wpSip24HasherData(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 - wpSipHasher128(&str_bytes, (void *)&io); - siphash((void *)str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&io.params.key, + WpU128Hash sip_hash = wpSipHasher128(&str_bytes, (void *)&data); + siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&data.key, (u8 *)hash, sizeof(u64) * 2); - result = result && io.hash[0] == hash[0] && io.hash[1] == hash[1]; + result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; // Array hash - wpSipHasher128(&arr_bytes, (void *)&io); - siphash((void *)arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&io.params.key, + sip_hash = wpSipHasher128(&arr_bytes, (void *)&data); + siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&data.key, (u8 *)hash, sizeof(u64) * 2); - result = result && io.hash[0] == hash[0] && io.hash[1] == hash[1]; + result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; // Number hash - wpSipHasher128(&num_bytes, (void *)&io); - siphash((void *)num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&io.params.key, + sip_hash = wpSipHasher128(&num_bytes, (void *)&data); + siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&data.key, (u8 *)hash, sizeof(u64) * 2); - result = result && io.hash[0] == hash[0] && io.hash[1] == hash[1]; + result = result && sip_hash.hash[0] == hash[0] && sip_hash.hash[1] == hash[1]; return wpTesterResult(result); } @@ -88,8 +88,8 @@ WpTestFuncResult test_siphash_64(void) { WpI32Array arr = wpArray(i32, 1, 2, 3, 4, 5); u64 num = 287324; - WpSipHasherKey key = { 238742, 671734 }; - WpSipHasher64IO io = wpSip24Hasher64IO(key); + WpSipHasherKey key = { 238742, 671734 }; + WpSipHasherData data = wpSip24HasherData(key); WpU8Stream str_bytes = wpStream(u8, str.buf, str.size); WpU8Stream arr_bytes = wpStream(u8, (u8 *)arr, wpArrayCount(arr) * wpArrayItemSize(arr)); @@ -98,25 +98,25 @@ WpTestFuncResult test_siphash_64(void) { u64 hash = {0}; // Sring hash - wpSipHasher64(&str_bytes, (void *)&io); - siphash((void *)str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&io.params.key, + u64 sip_hash = wpSipHasher64(&str_bytes, (void *)&data); + siphash(str_bytes.data, str_bytes.count * str_bytes.item_size, (void *)&data.key, (u8 *)&hash, sizeof(u64)); - result = result && io.hash == hash; + result = result && sip_hash == hash; // Array hash - wpSipHasher64(&arr_bytes, (void *)&io); - siphash((void *)arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&io.params.key, + sip_hash = wpSipHasher64(&arr_bytes, (void *)&data); + siphash(arr_bytes.data, arr_bytes.count * arr_bytes.item_size, (void *)&data.key, (u8 *)&hash, sizeof(u64)); - result = result && io.hash == hash; + result = result && sip_hash == hash; // Number hash - wpSipHasher64(&num_bytes, (void *)&io); - siphash((void *)num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&io.params.key, + sip_hash = wpSipHasher64(&num_bytes, (void *)&data); + siphash(num_bytes.data, num_bytes.count * num_bytes.item_size, (void *)&data.key, (u8 *)&hash, sizeof(u64)); - result = result && io.hash == hash; + result = result && sip_hash == hash; return wpTesterResult(result); }