Bitcoin Core 31.99.0
P2P Digital Currency
siphash.h
Go to the documentation of this file.
1// Copyright (c) 2016-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_CRYPTO_SIPHASH_H
6#define BITCOIN_CRYPTO_SIPHASH_H
7
8#include <attributes.h>
9#include <uint256.h>
10
11#include <bit>
12#include <cstdint>
13#include <span>
14
18{
20 static constexpr uint64_t C0{0x736f6d6570736575}, C1{0x646f72616e646f6d}, C2{0x6c7967656e657261}, C3{0x7465646279746573};
22 static constexpr uint64_t FINALIZER{0xFF};
24 static constexpr uint64_t FINALIZER_UNPADDED{0x6465646461706e75};
25 static_assert(FINALIZER_UNPADDED != FINALIZER);
26
28 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} {}
29
31 uint64_t m_v0, m_v1, m_v2, m_v3;
32
34 ALWAYS_INLINE void SipRound() noexcept
35 {
36 m_v0 += m_v1; m_v1 = std::rotl(m_v1, 13); m_v1 ^= m_v0;
37 m_v0 = std::rotl(m_v0, 32);
38 m_v2 += m_v3; m_v3 = std::rotl(m_v3, 16); m_v3 ^= m_v2;
39 m_v0 += m_v3; m_v3 = std::rotl(m_v3, 21); m_v3 ^= m_v0;
40 m_v2 += m_v1; m_v1 = std::rotl(m_v1, 17); m_v1 ^= m_v2;
41 m_v2 = std::rotl(m_v2, 32);
42 }
43
44public:
46 explicit ALWAYS_INLINE SipHashState(uint64_t k0, uint64_t k1) noexcept : SipHashState{C0 ^ k0, C1 ^ k1, C2 ^ k0, C3 ^ k1} {}
48 ALWAYS_INLINE SipHashState Copy() const noexcept { return {m_v0, m_v1, m_v2, m_v3}; }
50 ALWAYS_INLINE SipHashState& Compress1(uint64_t data) noexcept
51 {
52 m_v3 ^= data;
53 SipRound();
54 m_v0 ^= data;
55 return *this;
56 }
58 ALWAYS_INLINE SipHashState& Compress1Jumbo(const uint256& data) noexcept
59 {
60 const uint64_t d0{data.GetUint64(0)}, d1{data.GetUint64(1)}, d2{data.GetUint64(2)}, d3{data.GetUint64(3)};
61 m_v3 ^= d0; m_v0 ^= d1; m_v1 ^= d2; m_v2 ^= d3;
62 SipRound();
63 m_v0 ^= d0; m_v1 ^= d1; m_v2 ^= d2; m_v3 ^= d3;
64 return *this;
65 }
67 ALWAYS_INLINE SipHashState& Compress2(uint64_t data) noexcept
68 {
69 m_v3 ^= data;
70 SipRound();
71 SipRound();
72 m_v0 ^= data;
73 return *this;
74 }
76 ALWAYS_INLINE uint64_t Finalize4() noexcept
77 {
78 m_v2 ^= FINALIZER;
79 SipRound();
80 SipRound();
81 SipRound();
82 SipRound();
83 return m_v0 ^ m_v1 ^ m_v2 ^ m_v3;
84 }
87 ALWAYS_INLINE uint64_t Finalize3U() noexcept
88 {
90 SipRound();
91 SipRound();
92 SipRound();
93 return m_v0 ^ m_v1 ^ m_v2 ^ m_v3;
94 }
95};
96
99{
101 uint64_t m_tmp{0};
102 uint8_t m_count{0};
103
104public:
106 CSipHasher(uint64_t k0, uint64_t k1);
111 CSipHasher& Write(uint64_t data);
113 CSipHasher& Write(std::span<const unsigned char> data);
115 uint64_t Finalize() const;
116};
117
161{
163
164public:
166 SipHasher13UJ(uint64_t k0, uint64_t k1) noexcept : m_state{k0, k1} {}
168 SipHasher13UJ& Write(uint64_t data) noexcept;
170 SipHasher13UJ& WriteJumbo(const uint256& hash) noexcept;
172 uint64_t Finalize() const noexcept;
173
175 ALWAYS_INLINE uint64_t Hash(const uint256& hash) const noexcept
176 {
177 return m_state.Copy()
178 .Compress1Jumbo(hash)
179 .Finalize3U();
180 }
181
186 ALWAYS_INLINE uint64_t Hash(const uint256& hash, uint64_t extra) const noexcept
187 {
188 return m_state.Copy()
189 .Compress1Jumbo(hash)
190 .Compress1(extra)
191 .Finalize3U();
192 }
193};
194
205{
207
208public:
209 explicit PresaltedSipHasher(uint64_t k0, uint64_t k1) noexcept : m_state{k0, k1} {}
210
212 uint64_t operator()(const uint256& val) const noexcept;
213
218 uint64_t operator()(const uint256& val, uint32_t extra) const noexcept;
219};
220
221#endif // BITCOIN_CRYPTO_SIPHASH_H
General SipHash-2-4 implementation.
Definition: siphash.h:99
uint8_t m_count
Only the low 8 bits of the input size matter.
Definition: siphash.h:102
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
Definition: siphash.cpp:45
uint64_t m_tmp
Definition: siphash.h:101
SipHashState m_state
Definition: siphash.h:100
CSipHasher(uint64_t k0, uint64_t k1)
Construct a SipHash calculator initialized with 128-bit key (k0, k1).
Definition: siphash.cpp:12
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data.
Definition: siphash.cpp:14
Optimized SipHash-2-4 implementation for uint256.
Definition: siphash.h:205
uint64_t operator()(const uint256 &val) const noexcept
Equivalent to CSipHasher(k0, k1).Write(val).Finalize().
Definition: siphash.cpp:69
PresaltedSipHasher(uint64_t k0, uint64_t k1) noexcept
Definition: siphash.h:209
const SipHashState m_state
Definition: siphash.h:206
Shared SipHash state (v0..v3) with its round, compression, and finalization primitives.
Definition: siphash.h:18
ALWAYS_INLINE SipHashState(uint64_t k0, uint64_t k1) noexcept
Construct a SipHashState initialized with the specified key.
Definition: siphash.h:46
ALWAYS_INLINE SipHashState & Compress1Jumbo(const uint256 &data) noexcept
Mutably compress one jumbo block into this state, with 1 SipRound.
Definition: siphash.h:58
ALWAYS_INLINE SipHashState Copy() const noexcept
Construct a copy of this state.
Definition: siphash.h:48
static constexpr uint64_t FINALIZER
SipHash v2 finalizer constant.
Definition: siphash.h:22
uint64_t m_v3
Definition: siphash.h:31
ALWAYS_INLINE uint64_t Finalize3U() noexcept
Mutably finalize this state with 3 SipRounds using the unpadded finalizer, and return the resulting h...
Definition: siphash.h:87
static constexpr uint64_t C3
Definition: siphash.h:20
ALWAYS_INLINE void SipRound() noexcept
Mutably perform one SipRound on this state.
Definition: siphash.h:34
static constexpr uint64_t FINALIZER_UNPADDED
SipHash custom unpadded finalizer constant.
Definition: siphash.h:24
uint64_t m_v1
Definition: siphash.h:31
ALWAYS_INLINE uint64_t Finalize4() noexcept
Mutably finalize this state with 4 SipRounds, and return the resulting hash.
Definition: siphash.h:76
ALWAYS_INLINE SipHashState(uint64_t v0, uint64_t v1, uint64_t v2, uint64_t v3) noexcept
Construct a SipHashState with the specified values as state.
Definition: siphash.h:28
static constexpr uint64_t C2
Definition: siphash.h:20
static constexpr uint64_t C1
Definition: siphash.h:20
ALWAYS_INLINE SipHashState & Compress1(uint64_t data) noexcept
Mutably compress one block into this state, with 1 SipRound.
Definition: siphash.h:50
uint64_t m_v2
Definition: siphash.h:31
uint64_t m_v0
State variables.
Definition: siphash.h:31
ALWAYS_INLINE SipHashState & Compress2(uint64_t data) noexcept
Mutably compress one block into this state, with 2 SipRounds.
Definition: siphash.h:67
static constexpr uint64_t C0
SipHash initialization constants.
Definition: siphash.h:20
A custom weaker variant of SipHash-1-3 without padding, and supporting "jumbo" inputs.
Definition: siphash.h:161
SipHasher13UJ & Write(uint64_t data) noexcept
Hash a normal 64-bit value.
Definition: siphash.cpp:52
ALWAYS_INLINE uint64_t Hash(const uint256 &hash, uint64_t extra) const noexcept
Hash a jumbo block followed by a normal block after the data written so far, and finalize without mod...
Definition: siphash.h:186
SipHasher13UJ & WriteJumbo(const uint256 &hash) noexcept
Hash a 256-bit value as a jumbo block.
Definition: siphash.cpp:58
SipHashState m_state
Definition: siphash.h:162
uint64_t Finalize() const noexcept
Compute the 64-bit SipHash-1-3-UJ of the data written so far.
Definition: siphash.cpp:64
SipHasher13UJ(uint64_t k0, uint64_t k1) noexcept
Construct a SipHash-1-3-UJ calculator initialized with 128-bit key (k0, k1).
Definition: siphash.h:166
ALWAYS_INLINE uint64_t Hash(const uint256 &hash) const noexcept
Hash a jumbo block after the data written so far and finalize without modifying the object.
Definition: siphash.h:175
256-bit opaque blob.
Definition: uint256.h:196
static const PrecomputedData data
Precomputed COutPoint and CCoins values.
static SECP256K1_INLINE uint64_t rotl(const uint64_t x, int k)
Definition: testrand_impl.h:40