Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/compressor.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-2021 The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <compressor.h>
7
8
#include <pubkey.h>
9
#include <script/script.h>
10
11
/*
12
 * These check for scripts for which a special case with a shorter encoding is defined.
13
 * They are implemented separately from the CScript test, as these test for exact byte
14
 * sequence correspondences, and are more strict. For example, IsToPubKey also verifies
15
 * whether the public key is valid (as invalid ones cannot be represented in compressed
16
 * form).
17
 */
18
19
static bool IsToKeyID(const CScript& script, CKeyID &hash)
20
2.26M
{
21
2.26M
    if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160
  Branch (21:9): [True: 0, False: 2.26M]
  Branch (21:32): [True: 0, False: 0]
  Branch (21:55): [True: 0, False: 0]
22
2.26M
                            && script[2] == 20 && script[23] == OP_EQUALVERIFY
  Branch (22:32): [True: 0, False: 0]
  Branch (22:51): [True: 0, False: 0]
23
2.26M
                            && script[24] == OP_CHECKSIG) {
  Branch (23:32): [True: 0, False: 0]
24
0
        memcpy(&hash, &script[3], 20);
25
0
        return true;
26
0
    }
27
2.26M
    return false;
28
2.26M
}
29
30
static bool IsToScriptID(const CScript& script, CScriptID &hash)
31
2.26M
{
32
2.26M
    if (script.size() == 23 && script[0] == OP_HASH160 && script[1] == 20
  Branch (32:9): [True: 1.06k, False: 2.25M]
  Branch (32:32): [True: 1.06k, False: 0]
  Branch (32:59): [True: 1.06k, False: 0]
33
2.26M
                            && script[22] == OP_EQUAL) {
  Branch (33:32): [True: 1.06k, False: 0]
34
1.06k
        memcpy(&hash, &script[2], 20);
35
1.06k
        return true;
36
1.06k
    }
37
2.25M
    return false;
38
2.26M
}
39
40
static bool IsToPubKey(const CScript& script, CPubKey &pubkey)
41
2.25M
{
42
2.25M
    if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG
  Branch (42:9): [True: 0, False: 2.25M]
  Branch (42:32): [True: 0, False: 0]
  Branch (42:51): [True: 0, False: 0]
43
2.25M
                            && (script[1] == 0x02 || script[1] == 0x03)) {
  Branch (43:33): [True: 0, False: 0]
  Branch (43:54): [True: 0, False: 0]
44
0
        pubkey.Set(&script[1], &script[34]);
45
0
        return true;
46
0
    }
47
2.25M
    if (script.size() == 67 && script[0] == 65 && script[66] == OP_CHECKSIG
  Branch (47:9): [True: 0, False: 2.25M]
  Branch (47:32): [True: 0, False: 0]
  Branch (47:51): [True: 0, False: 0]
48
2.25M
                            && script[1] == 0x04) {
  Branch (48:32): [True: 0, False: 0]
49
0
        pubkey.Set(&script[1], &script[66]);
50
0
        return pubkey.IsFullyValid(); // if not fully valid, a case that would not be compressible
51
0
    }
52
2.25M
    return false;
53
2.25M
}
54
55
bool CompressScript(const CScript& script, CompressedScript& out)
56
2.26M
{
57
2.26M
    CKeyID keyID;
58
2.26M
    if (IsToKeyID(script, keyID)) {
  Branch (58:9): [True: 0, False: 2.26M]
59
0
        out.resize(21);
60
0
        out[0] = 0x00;
61
0
        memcpy(&out[1], &keyID, 20);
62
0
        return true;
63
0
    }
64
2.26M
    CScriptID scriptID;
65
2.26M
    if (IsToScriptID(script, scriptID)) {
  Branch (65:9): [True: 1.06k, False: 2.25M]
66
1.06k
        out.resize(21);
67
1.06k
        out[0] = 0x01;
68
1.06k
        memcpy(&out[1], &scriptID, 20);
69
1.06k
        return true;
70
1.06k
    }
71
2.25M
    CPubKey pubkey;
72
2.25M
    if (IsToPubKey(script, pubkey)) {
  Branch (72:9): [True: 0, False: 2.25M]
73
0
        out.resize(33);
74
0
        memcpy(&out[1], &pubkey[1], 32);
75
0
        if (pubkey[0] == 0x02 || pubkey[0] == 0x03) {
  Branch (75:13): [True: 0, False: 0]
  Branch (75:34): [True: 0, False: 0]
76
0
            out[0] = pubkey[0];
77
0
            return true;
78
0
        } else if (pubkey[0] == 0x04) {
  Branch (78:20): [True: 0, False: 0]
79
0
            out[0] = 0x04 | (pubkey[64] & 0x01);
80
0
            return true;
81
0
        }
82
0
    }
83
2.25M
    return false;
84
2.25M
}
85
86
unsigned int GetSpecialScriptSize(unsigned int nSize)
87
353
{
88
353
    if (nSize == 0 || nSize == 1)
  Branch (88:9): [True: 0, False: 353]
  Branch (88:23): [True: 353, False: 0]
89
353
        return 20;
90
0
    if (nSize == 2 || nSize == 3 || nSize == 4 || nSize == 5)
  Branch (90:9): [True: 0, False: 0]
  Branch (90:23): [True: 0, False: 0]
  Branch (90:37): [True: 0, False: 0]
  Branch (90:51): [True: 0, False: 0]
91
0
        return 32;
92
0
    return 0;
93
0
}
94
95
bool DecompressScript(CScript& script, unsigned int nSize, const CompressedScript& in)
96
353
{
97
353
    switch(nSize) {
  Branch (97:12): [True: 0, False: 353]
98
0
    case 0x00:
  Branch (98:5): [True: 0, False: 353]
99
0
        script.resize(25);
100
0
        script[0] = OP_DUP;
101
0
        script[1] = OP_HASH160;
102
0
        script[2] = 20;
103
0
        memcpy(&script[3], in.data(), 20);
104
0
        script[23] = OP_EQUALVERIFY;
105
0
        script[24] = OP_CHECKSIG;
106
0
        return true;
107
353
    case 0x01:
  Branch (107:5): [True: 353, False: 0]
108
353
        script.resize(23);
109
353
        script[0] = OP_HASH160;
110
353
        script[1] = 20;
111
353
        memcpy(&script[2], in.data(), 20);
112
353
        script[22] = OP_EQUAL;
113
353
        return true;
114
0
    case 0x02:
  Branch (114:5): [True: 0, False: 353]
115
0
    case 0x03:
  Branch (115:5): [True: 0, False: 353]
116
0
        script.resize(35);
117
0
        script[0] = 33;
118
0
        script[1] = nSize;
119
0
        memcpy(&script[2], in.data(), 32);
120
0
        script[34] = OP_CHECKSIG;
121
0
        return true;
122
0
    case 0x04:
  Branch (122:5): [True: 0, False: 353]
123
0
    case 0x05:
  Branch (123:5): [True: 0, False: 353]
124
0
        unsigned char vch[33] = {};
125
0
        vch[0] = nSize - 2;
126
0
        memcpy(&vch[1], in.data(), 32);
127
0
        CPubKey pubkey{vch};
128
0
        if (!pubkey.Decompress())
  Branch (128:13): [True: 0, False: 0]
129
0
            return false;
130
0
        assert(pubkey.size() == 65);
  Branch (130:9): [True: 0, False: 0]
131
0
        script.resize(67);
132
0
        script[0] = 65;
133
0
        memcpy(&script[1], pubkey.begin(), 65);
134
0
        script[66] = OP_CHECKSIG;
135
0
        return true;
136
353
    }
137
0
    return false;
138
353
}
139
140
// Amount compression:
141
// * If the amount is 0, output 0
142
// * first, divide the amount (in base units) by the largest power of 10 possible; call the exponent e (e is max 9)
143
// * if e<9, the last digit of the resulting number cannot be 0; store it as d, and drop it (divide by 10)
144
//   * call the result n
145
//   * output 1 + 10*(9*n + d - 1) + e
146
// * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n - 1) + 9
147
// (this is decodable, as d is in [1-9] and e is in [0-9])
148
149
uint64_t CompressAmount(uint64_t n)
150
2.26M
{
151
2.26M
    if (n == 0)
  Branch (151:9): [True: 2.00k, False: 2.25M]
152
2.00k
        return 0;
153
2.25M
    int e = 0;
154
20.2M
    while (((n % 10) == 0) && e < 9) {
  Branch (154:12): [True: 17.9M, False: 2.25M]
  Branch (154:31): [True: 17.9M, False: 0]
155
17.9M
        n /= 10;
156
17.9M
        e++;
157
17.9M
    }
158
2.25M
    if (e < 9) {
  Branch (158:9): [True: 2.25M, False: 144]
159
2.25M
        int d = (n % 10);
160
2.25M
        assert(d >= 1 && d <= 9);
  Branch (160:9): [True: 2.25M, False: 0]
  Branch (160:9): [True: 2.25M, False: 0]
  Branch (160:9): [True: 2.25M, False: 0]
161
2.25M
        n /= 10;
162
2.25M
        return 1 + (n*9 + d - 1)*10 + e;
163
2.25M
    } else {
164
144
        return 1 + (n - 1)*10 + 9;
165
144
    }
166
2.25M
}
167
168
uint64_t DecompressAmount(uint64_t x)
169
29.5k
{
170
    // x = 0  OR  x = 1+10*(9*n + d - 1) + e  OR  x = 1+10*(n - 1) + 9
171
29.5k
    if (x == 0)
  Branch (171:9): [True: 1.40k, False: 28.1k]
172
1.40k
        return 0;
173
28.1k
    x--;
174
    // x = 10*(9*n + d - 1) + e
175
28.1k
    int e = x % 10;
176
28.1k
    x /= 10;
177
28.1k
    uint64_t n = 0;
178
28.1k
    if (e < 9) {
  Branch (178:9): [True: 28.0k, False: 127]
179
        // x = 9*n + d - 1
180
28.0k
        int d = (x % 9) + 1;
181
28.0k
        x /= 9;
182
        // x = n
183
28.0k
        n = x*10 + d;
184
28.0k
    } else {
185
127
        n = x+1;
186
127
    }
187
160k
    while (e) {
  Branch (187:12): [True: 132k, False: 28.1k]
188
132k
        n *= 10;
189
132k
        e--;
190
132k
    }
191
28.1k
    return n;
192
29.5k
}