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