@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user