Branch data Line data Source code
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 : :
12 : 4289852 : CSipHasher::CSipHasher(uint64_t k0, uint64_t k1) : m_state{k0, k1} {}
13 : :
14 : 3968389 : CSipHasher& CSipHasher::Write(uint64_t data)
15 : : {
16 [ - + ]: 3968389 : assert(m_count % 8 == 0);
17 : 3968389 : m_state.Compress2(data);
18 : 3968389 : m_count += 8;
19 : 3968389 : return *this;
20 : : }
21 : :
22 : 4290023 : CSipHasher& CSipHasher::Write(std::span<const unsigned char> data)
23 : : {
24 : 4290023 : SipHashState state{m_state.Copy()};
25 : 4290023 : uint64_t t{m_tmp};
26 : 4290023 : uint8_t c{m_count};
27 : :
28 [ + + ]: 133896182 : while (data.size() > 0) {
29 [ + + ]: 129606159 : t |= uint64_t{data.front()} << (8 * (c % 8));
30 : 129606159 : c++;
31 [ + + ]: 129606159 : if ((c & 7) == 0) {
32 : 16010546 : state.Compress2(t);
33 : 16010546 : t = 0;
34 : : }
35 : 129606159 : data = data.subspan(1);
36 : : }
37 : :
38 : 4290023 : m_state = state;
39 : 4290023 : m_count = c;
40 : 4290023 : m_tmp = t;
41 : :
42 : 4290023 : return *this;
43 : : }
44 : :
45 : 4290076 : uint64_t CSipHasher::Finalize() const
46 : : {
47 : 4290076 : return m_state.Copy()
48 : 4290076 : .Compress2(m_tmp | (uint64_t{m_count} << 56))
49 : 4290076 : .Finalize4();
50 : : }
51 : :
52 : 87 : SipHasher13UJ& SipHasher13UJ::Write(uint64_t data) noexcept
53 : : {
54 : 87 : m_state.Compress1(data);
55 : : return *this;
56 : : }
57 : :
58 : 205 : SipHasher13UJ& SipHasher13UJ::WriteJumbo(const uint256& hash) noexcept
59 : : {
60 : 205 : m_state.Compress1Jumbo(hash);
61 : 205 : return *this;
62 : : }
63 : :
64 : 256 : uint64_t SipHasher13UJ::Finalize() const noexcept
65 : : {
66 : 256 : return m_state.Copy().Finalize3U();
67 : : }
68 : :
69 : 839785 : uint64_t PresaltedSipHasher::operator()(const uint256& val) const noexcept
70 : : {
71 : 839785 : return m_state.Copy()
72 : 839785 : .Compress2(val.GetUint64(0))
73 : : .Compress2(val.GetUint64(1))
74 : 839785 : .Compress2(val.GetUint64(2))
75 : 839785 : .Compress2(val.GetUint64(3))
76 : 839785 : .Compress2(uint64_t{32} << 56)
77 : 839785 : .Finalize4();
78 : : }
79 : :
80 : 9748 : uint64_t PresaltedSipHasher::operator()(const uint256& val, uint32_t extra) const noexcept
81 : : {
82 : 9748 : return m_state.Copy()
83 : 9748 : .Compress2(val.GetUint64(0))
84 : 9748 : .Compress2(val.GetUint64(1))
85 : 9748 : .Compress2(val.GetUint64(2))
86 : 9748 : .Compress2(val.GetUint64(3))
87 : 9748 : .Compress2((uint64_t{36} << 56) | extra)
88 : 9748 : .Finalize4();
89 : : }
|