Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/hash.cpp
Line
Count
Source
1
// Copyright (c) 2013-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 <hash.h>
6
#include <span.h>
7
#include <crypto/common.h>
8
#include <crypto/hmac_sha512.h>
9
10
#include <bit>
11
#include <string>
12
13
unsigned int MurmurHash3(unsigned int nHashSeed, std::span<const unsigned char> vDataToHash)
14
123M
{
15
    // The following is MurmurHash3 (x86_32), see https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp
16
123M
    uint32_t h1 = nHashSeed;
17
123M
    const uint32_t c1 = 0xcc9e2d51;
18
123M
    const uint32_t c2 = 0x1b873593;
19
20
123M
    const int nblocks = vDataToHash.size() / 4;
21
22
    //----------
23
    // body
24
123M
    const uint8_t* blocks = vDataToHash.data();
25
26
1.11G
    for (int i = 0; i < nblocks; ++i) {
  Branch (26:21): [True: 991M, False: 123M]
27
991M
        uint32_t k1 = ReadLE32(blocks + i*4);
28
29
991M
        k1 *= c1;
30
991M
        k1 = std::rotl(k1, 15);
31
991M
        k1 *= c2;
32
33
991M
        h1 ^= k1;
34
991M
        h1 = std::rotl(h1, 13);
35
991M
        h1 = h1 * 5 + 0xe6546b64;
36
991M
    }
37
38
    //----------
39
    // tail
40
123M
    const uint8_t* tail = vDataToHash.data() + nblocks * 4;
41
42
123M
    uint32_t k1 = 0;
43
44
123M
    switch (vDataToHash.size() & 3) {
  Branch (44:13): [True: 123M, False: 61.2k]
45
404
        case 3:
  Branch (45:9): [True: 404, False: 123M]
46
404
            k1 ^= tail[2] << 16;
47
404
            [[fallthrough]];
48
61.2k
        case 2:
  Branch (48:9): [True: 60.8k, False: 123M]
49
61.2k
            k1 ^= tail[1] << 8;
50
61.2k
            [[fallthrough]];
51
61.2k
        case 1:
  Branch (51:9): [True: 90, False: 123M]
52
61.2k
            k1 ^= tail[0];
53
61.2k
            k1 *= c1;
54
61.2k
            k1 = std::rotl(k1, 15);
55
61.2k
            k1 *= c2;
56
61.2k
            h1 ^= k1;
57
123M
    }
58
59
    //----------
60
    // finalization
61
123M
    h1 ^= vDataToHash.size();
62
123M
    h1 ^= h1 >> 16;
63
123M
    h1 *= 0x85ebca6b;
64
123M
    h1 ^= h1 >> 13;
65
123M
    h1 *= 0xc2b2ae35;
66
123M
    h1 ^= h1 >> 16;
67
68
123M
    return h1;
69
123M
}
70
71
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
72
1.24M
{
73
1.24M
    unsigned char num[4];
74
1.24M
    WriteBE32(num, nChild);
75
1.24M
    CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output);
76
1.24M
}
77
78
uint256 SHA256Uint256(const uint256& input)
79
175k
{
80
175k
    uint256 result;
81
175k
    CSHA256().Write(input.begin(), 32).Finalize(result.begin());
82
175k
    return result;
83
175k
}
84
85
HashWriter TaggedHash(const std::string& tag)
86
55.4k
{
87
55.4k
    HashWriter writer{};
88
55.4k
    uint256 taghash;
89
55.4k
    CSHA256().Write((const unsigned char*)tag.data(), tag.size()).Finalize(taghash.begin());
90
55.4k
    writer << taghash << taghash;
91
55.4k
    return writer;
92
55.4k
}