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