Bitcoin Core 31.99.0
P2P Digital Currency
siphash.cpp
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#include <crypto/siphash.h>
6
7#include <uint256.h>
8
9#include <cassert>
10#include <span>
11
12CSipHasher::CSipHasher(uint64_t k0, uint64_t k1) : m_state{k0, k1} {}
13
15{
16 assert(m_count % 8 == 0);
18 m_count += 8;
19 return *this;
20}
21
22CSipHasher& CSipHasher::Write(std::span<const unsigned char> data)
23{
24 SipHashState state{m_state.Copy()};
25 uint64_t t{m_tmp};
26 uint8_t c{m_count};
27
28 while (data.size() > 0) {
29 t |= uint64_t{data.front()} << (8 * (c % 8));
30 c++;
31 if ((c & 7) == 0) {
32 state.Compress2(t);
33 t = 0;
34 }
35 data = data.subspan(1);
36 }
37
38 m_state = state;
39 m_count = c;
40 m_tmp = t;
41
42 return *this;
43}
44
45uint64_t CSipHasher::Finalize() const
46{
47 return m_state.Copy()
48 .Compress2(m_tmp | (uint64_t{m_count} << 56))
49 .Finalize4();
50}
51
53{
54 m_state.Compress1(data);
55 return *this;
56}
57
59{
60 m_state.Compress1Jumbo(hash);
61 return *this;
62}
63
64uint64_t SipHasher13UJ::Finalize() const noexcept
65{
66 return m_state.Copy().Finalize3U();
67}
68
69uint64_t PresaltedSipHasher::operator()(const uint256& val) const noexcept
70{
71 return m_state.Copy()
72 .Compress2(val.GetUint64(0))
73 .Compress2(val.GetUint64(1))
74 .Compress2(val.GetUint64(2))
75 .Compress2(val.GetUint64(3))
76 .Compress2(uint64_t{32} << 56)
77 .Finalize4();
78}
79
80uint64_t PresaltedSipHasher::operator()(const uint256& val, uint32_t extra) const noexcept
81{
82 return m_state.Copy()
83 .Compress2(val.GetUint64(0))
84 .Compress2(val.GetUint64(1))
85 .Compress2(val.GetUint64(2))
86 .Compress2(val.GetUint64(3))
87 .Compress2((uint64_t{36} << 56) | extra)
88 .Finalize4();
89}
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
uint64_t operator()(const uint256 &val) const noexcept
Equivalent to CSipHasher(k0, k1).Write(val).Finalize().
Definition: siphash.cpp:69
Shared SipHash state (v0..v3) with its round, compression, and finalization primitives.
Definition: siphash.h:18
ALWAYS_INLINE SipHashState Copy() const noexcept
Construct a copy of this state.
Definition: siphash.h:48
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
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 & Compress2(uint64_t data) noexcept
Mutably compress one block into this state, with 2 SipRounds.
Definition: siphash.h:67
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
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
256-bit opaque blob.
Definition: uint256.h:196
static const PrecomputedData data
Precomputed COutPoint and CCoins values.
BlockValidationState m_state
Definition: miner.cpp:371
assert(!tx.IsCoinBase())