From 0ce1c780126d75ce89b1e23ac9e632c9973391b2 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sat, 11 May 2024 16:50:42 +0900 Subject: [PATCH] Refactor --- common/siphash.cc | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/common/siphash.cc b/common/siphash.cc index 84c28b73ae..a7b636c845 100644 --- a/common/siphash.cc +++ b/common/siphash.cc @@ -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; @@ -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; @@ -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; @@ -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; }