Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed May 11, 2024
1 parent a250aea commit 0ce1c78
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions common/siphash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,33 @@
// than SHA256 or Blake3.
//
// Our implementation always outputs a 128 bit hash value.
//
// Here is the reference implementation:
// https://github.com/veorq/SipHash/blob/master/siphash.c

#include "common.h"

namespace mold {

#define ROUND \
do { \
v0 += v1; \
v1 = std::rotl(v1, 13); \
v1 ^= v0; \
v0 = std::rotl(v0, 32); \
v2 += v3; \
v3 = std::rotl(v3, 16); \
v3 ^= v2; \
v0 += v3; \
v3 = std::rotl(v3, 21); \
v3 ^= v0; \
v2 += v1; \
v1 = std::rotl(v1, 17); \
v1 ^= v2; \
v2 = std::rotl(v2, 32); \
} while (0)
v0 += v1; \
v1 = std::rotl(v1, 13); \
v1 ^= v0; \
v0 = std::rotl(v0, 32); \
v2 += v3; \
v3 = std::rotl(v3, 16); \
v3 ^= v2; \
v0 += v3; \
v3 = std::rotl(v3, 21); \
v3 ^= v0; \
v2 += v1; \
v1 = std::rotl(v1, 17); \
v1 ^= v2; \
v2 = std::rotl(v2, 32)

// SipHash-1-3
#define C_ROUND ROUND
#define D_ROUND for (i64 i = 0; i < 3; i++) ROUND
#define C_ROUNDS ROUND
#define D_ROUNDS ROUND; ROUND; ROUND

SipHash::SipHash(u8 *key) {
u64 k0 = *(ul64 *)key;
Expand All @@ -61,7 +62,7 @@ void SipHash::update(u8 *msg, i64 msglen) {

u64 m = *(ul64 *)buf;
v3 ^= m;
C_ROUND;
C_ROUNDS;
v0 ^= m;

msg += j;
Expand All @@ -72,7 +73,7 @@ void SipHash::update(u8 *msg, i64 msglen) {
while (msglen >= 8) {
u64 m = *(ul64 *)msg;
v3 ^= m;
C_ROUND;
C_ROUNDS;
v0 ^= m;

msg += 8;
Expand All @@ -88,15 +89,15 @@ void SipHash::finish(u8 *out) {
u64 b = (total_bytes << 56) | *(ul64 *)buf;

v3 ^= b;
C_ROUND;
C_ROUNDS;
v0 ^= b;

v2 ^= 0xee;
D_ROUND;
D_ROUNDS;
*(ul64 *)out = v0 ^ v1 ^ v2 ^ v3;

v1 ^= 0xdd;
D_ROUND;
D_ROUNDS;
*(ul64 *)(out + 8) = v0 ^ v1 ^ v2 ^ v3;
}

Expand Down

0 comments on commit 0ce1c78

Please sign in to comment.