Skip to content

Commit

Permalink
crypto, hash: replace custom rotl32 with std::rotl
Browse files Browse the repository at this point in the history
  • Loading branch information
fjahr committed Dec 24, 2023
1 parent 08e6aaa commit 842c288
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
11 changes: 5 additions & 6 deletions src/crypto/chacha20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
#include <span.h>

#include <algorithm>
#include <bit>
#include <string.h>

constexpr static inline uint32_t rotl32(uint32_t v, int c) { return (v << c) | (v >> (32 - c)); }

#define QUARTERROUND(a,b,c,d) \
a += b; d = rotl32(d ^ a, 16); \
c += d; b = rotl32(b ^ c, 12); \
a += b; d = rotl32(d ^ a, 8); \
c += d; b = rotl32(b ^ c, 7);
a += b; d = std::rotl(d ^ a, 16); \
c += d; b = std::rotl(b ^ c, 12); \
a += b; d = std::rotl(d ^ a, 8); \
c += d; b = std::rotl(b ^ c, 7);

#define REPEAT10(a) do { {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; } while(0)

Expand Down
12 changes: 4 additions & 8 deletions src/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@
#include <crypto/common.h>
#include <crypto/hmac_sha512.h>

#include <bit>
#include <string>

inline uint32_t ROTL32(uint32_t x, int8_t r)
{
return (x << r) | (x >> (32 - r));
}

unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash)
{
// The following is MurmurHash3 (x86_32), see https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp
Expand All @@ -31,11 +27,11 @@ unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vData
uint32_t k1 = ReadLE32(blocks + i*4);

k1 *= c1;
k1 = ROTL32(k1, 15);
k1 = std::rotl(k1, 15);
k1 *= c2;

h1 ^= k1;
h1 = ROTL32(h1, 13);
h1 = std::rotl(h1, 13);
h1 = h1 * 5 + 0xe6546b64;
}

Expand All @@ -55,7 +51,7 @@ unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vData
case 1:
k1 ^= tail[0];
k1 *= c1;
k1 = ROTL32(k1, 15);
k1 = std::rotl(k1, 15);
k1 *= c2;
h1 ^= k1;
}
Expand Down
1 change: 0 additions & 1 deletion test/sanitizer_suppressions/ubsan
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ implicit-signed-integer-truncation:crypto/
implicit-unsigned-integer-truncation:crypto/
shift-base:arith_uint256.cpp
shift-base:crypto/
shift-base:ROTL32
shift-base:streams.h
shift-base:FormatHDKeypath
shift-base:xoroshiro128plusplus.h

0 comments on commit 842c288

Please sign in to comment.