From 5a4db558980be6bec1f4166d0eebf8524fddb791 Mon Sep 17 00:00:00 2001 From: Leonid Evdokimov Date: Fri, 27 Sep 2024 13:04:14 +0300 Subject: [PATCH] FNV128: fix shift UB and ensure that the test vector match The test vectors are `golden128` from https://github.com/golang/go/blob/master/src/hash/fnv/fnv_test.go FNV128("") = 6c62272e07bb014262b821756295c58d FNV128("a") = d228cb69101a8caf78912b704e4a141e FNV128("ab") = 0880945aeeab1be95aa073305526c088 FNV128("abc") = a68bb2a4348b5822836dbc78c6aee73b --- Hashes.cpp | 2 +- main.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Hashes.cpp b/Hashes.cpp index 55c81fc2..f02bbb53 100644 --- a/Hashes.cpp +++ b/Hashes.cpp @@ -388,7 +388,7 @@ FNV128(uint64_t buf[2], const char *key, int len, uint64_t seed) uint64_t s0 = multhi; // high uint64_t s1 = prime128Lower * buf[1]; // low - s0 += buf[1] << (prime128Shift + prime128Lower * buf[0]); + s0 += (buf[1] << prime128Shift) + prime128Lower * buf[0]; // Update the values buf[1] = s1; diff --git a/main.cpp b/main.cpp index 386af516..8cc72027 100644 --- a/main.cpp +++ b/main.cpp @@ -276,7 +276,7 @@ HashInfo g_hashes[] = {0x811c9dc5, 0x23d4a49d} /* !! */ }, { FNV64a_test, 64, 0x103455FC, "FNV64", "Fowler-Noll-Vo hash, 64-bit", POOR, {0x811c9dc5, 0xcbf29ce4, 0x84222325, 0xcbf29ce484222325} /* TODO */}, -{ FNV128_test, 128, 0xBCAA1426, "FNV128", "Go variant of FNV, 128-bit", POOR, {} }, +{ FNV128_test, 128, 0xC6FF4526, "FNV128", "Go variant of FNV, 128-bit", POOR, {} }, #endif { FNV2_test, __WORDSIZE, FNV2_VERIF, "FNV2", "wordwise FNV", POOR, {} }, { fletcher2_test, 64, 0x890767C0, "fletcher2", "fletcher2 ZFS", POOR, {0UL} /* !! */ },