Skip to content

Commit 859685e

Browse files
committed
Rework into SipHashState
1 parent a2edcd1 commit 859685e

2 files changed

Lines changed: 104 additions & 106 deletions

File tree

‎src/crypto/siphash.cpp‎

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,31 @@
99
#include <cassert>
1010
#include <span>
1111

12-
using siphash_detail::Compress1;
13-
using siphash_detail::Compress2;
14-
using siphash_detail::Finalize3U;
15-
using siphash_detail::Finalize4;
16-
1712
CSipHasher::CSipHasher(uint64_t k0, uint64_t k1) : m_state{k0, k1} {}
1813

1914
CSipHasher& CSipHasher::Write(uint64_t data)
2015
{
21-
uint64_t v0{m_state.v[0]}, v1{m_state.v[1]}, v2{m_state.v[2]}, v3{m_state.v[3]};
22-
23-
assert(m_count % 8 == 0);
24-
25-
Compress2(v0, v1, v2, v3, data);
26-
27-
m_state.v[0] = v0; m_state.v[1] = v1; m_state.v[2] = v2; m_state.v[3] = v3;
28-
16+
Assume(m_count % 8 == 0);
17+
m_state.Compress2(data);
2918
m_count += 8;
3019
return *this;
3120
}
3221

3322
CSipHasher& CSipHasher::Write(std::span<const unsigned char> data)
3423
{
35-
uint64_t v0{m_state.v[0]}, v1{m_state.v[1]}, v2{m_state.v[2]}, v3{m_state.v[3]};
3624
uint64_t t{m_tmp};
3725
uint8_t c{m_count};
3826

3927
while (data.size() > 0) {
4028
t |= uint64_t{data.front()} << (8 * (c % 8));
4129
c++;
4230
if ((c & 7) == 0) {
43-
Compress2(v0, v1, v2, v3, t);
31+
m_state.Compress2(t);
4432
t = 0;
4533
}
4634
data = data.subspan(1);
4735
}
4836

49-
m_state.v[0] = v0; m_state.v[1] = v1; m_state.v[2] = v2; m_state.v[3] = v3;
5037
m_count = c;
5138
m_tmp = t;
5239

@@ -55,47 +42,47 @@ CSipHasher& CSipHasher::Write(std::span<const unsigned char> data)
5542

5643
uint64_t CSipHasher::Finalize() const
5744
{
58-
uint64_t v0{m_state.v[0]}, v1{m_state.v[1]}, v2{m_state.v[2]}, v3{m_state.v[3]};
59-
Compress2(v0, v1, v2, v3, m_tmp | (uint64_t{m_count} << 56));
60-
return Finalize4(v0, v1, v2, v3);
45+
return m_state.Copy()
46+
.Compress2(m_tmp | (uint64_t{m_count} << 56))
47+
.Finalize4();
6148
}
6249

6350
SipHasher13UJ& SipHasher13UJ::Write(uint64_t data) noexcept
6451
{
65-
Compress1(m_state.v[0], m_state.v[1], m_state.v[2], m_state.v[3], data);
52+
m_state.Compress1(data);
6653
return *this;
6754
}
6855

6956
SipHasher13UJ& SipHasher13UJ::WriteJumbo(const uint256& hash) noexcept
7057
{
71-
Compress1(m_state.v[0], m_state.v[1], m_state.v[2], m_state.v[3], hash);
58+
m_state.Compress1Jumbo(hash);
7259
return *this;
7360
}
7461

7562
uint64_t SipHasher13UJ::Finalize() const noexcept
7663
{
77-
return Finalize3U(m_state.v[0], m_state.v[1], m_state.v[2], m_state.v[3]);
64+
return m_state.Copy().Finalize3U();
7865
}
7966

8067
uint64_t PresaltedSipHasher::operator()(const uint256& val) const noexcept
8168
{
82-
uint64_t v0{m_state.v[0]}, v1{m_state.v[1]}, v2{m_state.v[2]}, v3{m_state.v[3]};
83-
Compress2(v0, v1, v2, v3, val.GetUint64(0));
84-
Compress2(v0, v1, v2, v3, val.GetUint64(1));
85-
Compress2(v0, v1, v2, v3, val.GetUint64(2));
86-
Compress2(v0, v1, v2, v3, val.GetUint64(3));
87-
Compress2(v0, v1, v2, v3, uint64_t{4} << 59);
88-
return Finalize4(v0, v1, v2, v3);
69+
return m_state.Copy()
70+
.Compress2(val.GetUint64(0))
71+
.Compress2(val.GetUint64(1))
72+
.Compress2(val.GetUint64(2))
73+
.Compress2(val.GetUint64(3))
74+
.Compress2(uint64_t{32} << 56)
75+
.Finalize4();
8976
}
9077

9178
/** Specialized implementation for efficiency */
9279
uint64_t PresaltedSipHasher::operator()(const uint256& val, uint32_t extra) const noexcept
9380
{
94-
uint64_t v0{m_state.v[0]}, v1{m_state.v[1]}, v2{m_state.v[2]}, v3{m_state.v[3]};
95-
Compress2(v0, v1, v2, v3, val.GetUint64(0));
96-
Compress2(v0, v1, v2, v3, val.GetUint64(1));
97-
Compress2(v0, v1, v2, v3, val.GetUint64(2));
98-
Compress2(v0, v1, v2, v3, val.GetUint64(3));
99-
Compress2(v0, v1, v2, v3, (uint64_t{36} << 56) | extra);
100-
return Finalize4(v0, v1, v2, v3);
81+
return m_state.Copy()
82+
.Compress2(val.GetUint64(0))
83+
.Compress2(val.GetUint64(1))
84+
.Compress2(val.GetUint64(2))
85+
.Compress2(val.GetUint64(3))
86+
.Compress2((uint64_t{36} << 56) | extra)
87+
.Finalize4();
10188
}

‎src/crypto/siphash.h‎

Lines changed: 81 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,81 +7,92 @@
77

88
#include <attributes.h>
99
#include <uint256.h>
10+
#include <util/check.h>
1011

1112
#include <array>
1213
#include <bit>
1314
#include <cstdint>
1415
#include <span>
1516

16-
namespace siphash_detail {
17-
18-
inline constexpr uint64_t SIPHASH_FINALIZER{0xFF};
19-
inline constexpr uint64_t SIPHASH_FINALIZER_UNPADDED{0x6465646461706E75};
20-
static_assert(SIPHASH_FINALIZER_UNPADDED != SIPHASH_FINALIZER);
21-
22-
ALWAYS_INLINE void SipRound(uint64_t& v0, uint64_t& v1, uint64_t& v2, uint64_t& v3)
23-
{
24-
v0 += v1; v1 = std::rotl(v1, 13); v1 ^= v0;
25-
v0 = std::rotl(v0, 32);
26-
v2 += v3; v3 = std::rotl(v3, 16); v3 ^= v2;
27-
v0 += v3; v3 = std::rotl(v3, 21); v3 ^= v0;
28-
v2 += v1; v1 = std::rotl(v1, 17); v1 ^= v2;
29-
v2 = std::rotl(v2, 32);
30-
}
31-
32-
ALWAYS_INLINE void Compress2(uint64_t& v0, uint64_t& v1, uint64_t& v2, uint64_t& v3, uint64_t data)
33-
{
34-
v3 ^= data;
35-
SipRound(v0, v1, v2, v3);
36-
SipRound(v0, v1, v2, v3);
37-
v0 ^= data;
38-
}
39-
40-
ALWAYS_INLINE uint64_t Finalize4(uint64_t v0, uint64_t v1, uint64_t v2, uint64_t v3)
41-
{
42-
v2 ^= SIPHASH_FINALIZER;
43-
SipRound(v0, v1, v2, v3);
44-
SipRound(v0, v1, v2, v3);
45-
SipRound(v0, v1, v2, v3);
46-
SipRound(v0, v1, v2, v3);
47-
return v0 ^ v1 ^ v2 ^ v3;
48-
}
49-
50-
ALWAYS_INLINE void Compress1(uint64_t& v0, uint64_t& v1, uint64_t& v2, uint64_t& v3, uint64_t data)
51-
{
52-
v3 ^= data;
53-
SipRound(v0, v1, v2, v3);
54-
v0 ^= data;
55-
}
56-
57-
ALWAYS_INLINE void Compress1(uint64_t& v0, uint64_t& v1, uint64_t& v2, uint64_t& v3, const uint256& data)
58-
{
59-
const uint64_t d0{data.GetUint64(0)}, d1{data.GetUint64(1)}, d2{data.GetUint64(2)}, d3{data.GetUint64(3)};
60-
v3 ^= d0; v0 ^= d1; v1 ^= d2; v2 ^= d3;
61-
SipRound(v0, v1, v2, v3);
62-
v0 ^= d0; v1 ^= d1; v2 ^= d2; v3 ^= d3;
63-
}
64-
65-
ALWAYS_INLINE uint64_t Finalize3U(uint64_t v0, uint64_t v1, uint64_t v2, uint64_t v3)
66-
{
67-
v2 ^= SIPHASH_FINALIZER_UNPADDED;
68-
SipRound(v0, v1, v2, v3);
69-
SipRound(v0, v1, v2, v3);
70-
SipRound(v0, v1, v2, v3);
71-
return v0 ^ v1 ^ v2 ^ v3;
72-
}
73-
74-
} // namespace siphash_detail
75-
7617
/** Shared SipHash internal state v[0..3], initialized from (k0, k1). */
7718
class SipHashState
7819
{
20+
/** SipHash initialization constants. */
7921
static constexpr uint64_t C0{0x736f6d6570736575ULL}, C1{0x646f72616e646f6dULL}, C2{0x6c7967656e657261ULL}, C3{0x7465646279746573ULL};
22+
/** SipHash v2 finalizer constant. */
23+
static inline constexpr uint64_t FINALIZER{0xFF};
24+
/** SipHash custom unpadded finalizer constant. */
25+
static inline constexpr uint64_t FINALIZER_UNPADDED{0x6465646461706E75};
26+
static_assert(FINALIZER_UNPADDED != FINALIZER);
8027

81-
public:
82-
explicit SipHashState(uint64_t k0, uint64_t k1) noexcept : v{C0 ^ k0, C1 ^ k1, C2 ^ k0, C3 ^ k1} {}
28+
/** State variables. */
29+
uint64_t m_v0, m_v1, m_v2, m_v3;
30+
31+
/** Construct a SipHashState with the specified values as state. */
32+
ALWAYS_INLINE SipHashState(uint64_t v0, uint64_t v1, uint64_t v2, uint64_t v3) noexcept : m_v0{v0}, m_v1{v1}, m_v2{v2}, m_v3{v3} {}
8333

84-
std::array<uint64_t, 4> v{};
34+
/** Mutably perform one SipRound on this state. */
35+
ALWAYS_INLINE void SipRound() noexcept
36+
{
37+
m_v0 += m_v1; m_v1 = std::rotl(m_v1, 13); m_v1 ^= m_v0;
38+
m_v0 = std::rotl(m_v0, 32);
39+
m_v2 += m_v3; m_v3 = std::rotl(m_v3, 16); m_v3 ^= m_v2;
40+
m_v0 += m_v3; m_v3 = std::rotl(m_v3, 21); m_v3 ^= m_v0;
41+
m_v2 += m_v1; m_v1 = std::rotl(m_v1, 17); m_v1 ^= m_v2;
42+
m_v2 = std::rotl(m_v2, 32);
43+
}
44+
45+
public:
46+
/** Construct a SipHashState initialized with the specified key. */
47+
explicit ALWAYS_INLINE SipHashState(uint64_t k0, uint64_t k1) noexcept : SipHashState{C0 ^ k0, C1 ^ k1, C2 ^ k0, C3 ^ k1} {}
48+
/** Construct a copy of this state. */
49+
ALWAYS_INLINE SipHashState Copy() const noexcept { return {m_v0, m_v1, m_v2, m_v3}; }
50+
/** Mutably compress one block into this state, with 1 SipRound. */
51+
ALWAYS_INLINE SipHashState& Compress1(uint64_t data) noexcept
52+
{
53+
m_v3 ^= data;
54+
SipRound();
55+
m_v0 ^= data;
56+
return *this;
57+
}
58+
/** Mutably compress one jumbo block into this state, with 1 SipRound. */
59+
ALWAYS_INLINE SipHashState& Compress1Jumbo(const uint256& data) noexcept
60+
{
61+
const uint64_t d0{data.GetUint64(0)}, d1{data.GetUint64(1)}, d2{data.GetUint64(2)}, d3{data.GetUint64(3)};
62+
m_v3 ^= d0; m_v0 ^= d1; m_v1 ^= d2; m_v2 ^= d3;
63+
SipRound();
64+
m_v0 ^= d0; m_v1 ^= d1; m_v2 ^= d2; m_v3 ^= d3;
65+
return *this;
66+
}
67+
/** Mutably compress one block into this state, with 2 SipRounds. */
68+
ALWAYS_INLINE SipHashState& Compress2(uint64_t data) noexcept
69+
{
70+
m_v3 ^= data;
71+
SipRound();
72+
SipRound();
73+
m_v0 ^= data;
74+
return *this;
75+
}
76+
/** Mutably finalize this state with 4 SipRounds, and return the resulting hash. */
77+
ALWAYS_INLINE uint64_t Finalize4() noexcept
78+
{
79+
m_v2 ^= FINALIZER;
80+
SipRound();
81+
SipRound();
82+
SipRound();
83+
SipRound();
84+
return m_v0 ^ m_v1 ^ m_v2 ^ m_v3;
85+
}
86+
/** Mutably finalize this state with 3 SipRounds using the unpadded finalizer, and return the
87+
* resulting hash. */
88+
ALWAYS_INLINE uint64_t Finalize3U() noexcept
89+
{
90+
m_v2 ^= FINALIZER_UNPADDED;
91+
SipRound();
92+
SipRound();
93+
SipRound();
94+
return m_v0 ^ m_v1 ^ m_v2 ^ m_v3;
95+
}
8596
};
8697

8798
/** General SipHash-2-4 implementation. */
@@ -164,9 +175,9 @@ class SipHasher13UJ
164175
/** Hash a jumbo block after the data written so far and finalize without modifying the object. */
165176
ALWAYS_INLINE uint64_t Hash(const uint256& hash) const noexcept
166177
{
167-
uint64_t v0{m_state.v[0]}, v1{m_state.v[1]}, v2{m_state.v[2]}, v3{m_state.v[3]};
168-
siphash_detail::Compress1(v0, v1, v2, v3, hash);
169-
return siphash_detail::Finalize3U(v0, v1, v2, v3);
178+
return m_state.Copy()
179+
.Compress1Jumbo(hash)
180+
.Finalize3U();
170181
}
171182

172183
/**
@@ -175,10 +186,10 @@ class SipHasher13UJ
175186
*/
176187
ALWAYS_INLINE uint64_t Hash(const uint256& hash, uint64_t extra) const noexcept
177188
{
178-
uint64_t v0{m_state.v[0]}, v1{m_state.v[1]}, v2{m_state.v[2]}, v3{m_state.v[3]};
179-
siphash_detail::Compress1(v0, v1, v2, v3, hash);
180-
siphash_detail::Compress1(v0, v1, v2, v3, extra);
181-
return siphash_detail::Finalize3U(v0, v1, v2, v3);
189+
return m_state.Copy()
190+
.Compress1Jumbo(hash)
191+
.Compress1(extra)
192+
.Finalize3U();
182193
}
183194
};
184195

0 commit comments

Comments
 (0)