Skip to content

Commit

Permalink
FNV128: fix shift UB and ensure that the test vector match
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darkk authored and rurban committed Sep 28, 2024
1 parent 2e07153 commit 5a4db55
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Hashes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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} /* !! */ },
Expand Down

0 comments on commit 5a4db55

Please sign in to comment.