Coverage Report

Created: 2026-07-29 23:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
6.20M
CSipHasher::CSipHasher(uint64_t k0, uint64_t k1) : m_state{k0, k1} {}
13
14
CSipHasher& CSipHasher::Write(uint64_t data)
15
4.40M
{
16
4.40M
    assert(m_count % 8 == 0);
17
4.40M
    m_state.Compress2(data);
18
4.40M
    m_count += 8;
19
4.40M
    return *this;
20
4.40M
}
21
22
CSipHasher& CSipHasher::Write(std::span<const unsigned char> data)
23
6.19M
{
24
6.19M
    SipHashState state{m_state.Copy()};
25
6.19M
    uint64_t t{m_tmp};
26
6.19M
    uint8_t c{m_count};
27
28
179M
    while (data.size() > 0) {
29
173M
        t |= uint64_t{data.front()} << (8 * (c % 8));
30
173M
        c++;
31
173M
        if ((c & 7) == 0) {
32
20.4M
            state.Compress2(t);
33
20.4M
            t = 0;
34
20.4M
        }
35
173M
        data = data.subspan(1);
36
173M
    }
37
38
6.19M
    m_state = state;
39
6.19M
    m_count = c;
40
6.19M
    m_tmp = t;
41
42
6.19M
    return *this;
43
6.19M
}
44
45
uint64_t CSipHasher::Finalize() const
46
6.20M
{
47
6.20M
    return m_state.Copy()
48
6.20M
                  .Compress2(m_tmp | (uint64_t{m_count} << 56))
49
6.20M
                  .Finalize4();
50
6.20M
}
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
27.7M
{
71
27.7M
    return m_state.Copy()
72
27.7M
                  .Compress2(val.GetUint64(0))
73
27.7M
                  .Compress2(val.GetUint64(1))
74
27.7M
                  .Compress2(val.GetUint64(2))
75
27.7M
                  .Compress2(val.GetUint64(3))
76
27.7M
                  .Compress2(uint64_t{32} << 56)
77
27.7M
                  .Finalize4();
78
27.7M
}
79
80
uint64_t PresaltedSipHasher::operator()(const uint256& val, uint32_t extra) const noexcept
81
1.76M
{
82
1.76M
    return m_state.Copy()
83
1.76M
                  .Compress2(val.GetUint64(0))
84
1.76M
                  .Compress2(val.GetUint64(1))
85
1.76M
                  .Compress2(val.GetUint64(2))
86
1.76M
                  .Compress2(val.GetUint64(3))
87
1.76M
                  .Compress2((uint64_t{36} << 56) | extra)
88
1.76M
                  .Finalize4();
89
1.76M
}