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