Coverage Report

Created: 2026-10-19 15:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/compressor.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present 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
509k
{
21
509k
    if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160
22
509k
                            && script[2] == 20 && script[23] == OP_EQUALVERIFY
23
509k
                            && script[24] == OP_CHECKSIG) {
24
89.2k
        memcpy(&hash, &script[3], 20);
25
89.2k
        return true;
26
89.2k
    }
27
420k
    return false;
28
509k
}
29
30
static bool IsToScriptID(const CScript& script, CScriptID &hash)
31
420k
{
32
420k
    if (script.size() == 23 && script[0] == OP_HASH160 && script[1] == 20
33
420k
                            && script[22] == OP_EQUAL) {
34
25.5k
        memcpy(&hash, &script[2], 20);
35
25.5k
        return true;
36
25.5k
    }
37
394k
    return false;
38
420k
}
39
40
static bool IsToPubKey(const CScript& script, CPubKey &pubkey)
41
394k
{
42
394k
    if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG
43
394k
                            && (script[1] == 0x02 || script[1] == 0x03)) {
44
13.1k
        pubkey.Set(&script[1], &script[34]);
45
13.1k
        return true;
46
13.1k
    }
47
381k
    if (script.size() == 67 && script[0] == 65 && script[66] == OP_CHECKSIG
48
381k
                            && script[1] == 0x04) {
49
70
        pubkey.Set(&script[1], &script[66]);
50
70
        return pubkey.IsFullyValid(); // if not fully valid, a case that would not be compressible
51
70
    }
52
381k
    return false;
53
381k
}
54
55
bool CompressScript(const CScript& script, CompressedScript& out)
56
509k
{
57
509k
    CKeyID keyID;
58
509k
    if (IsToKeyID(script, keyID)) {
59
89.2k
        out.resize(21);
60
89.2k
        out[0] = 0x00;
61
89.2k
        memcpy(&out[1], &keyID, 20);
62
89.2k
        return true;
63
89.2k
    }
64
420k
    CScriptID scriptID;
65
420k
    if (IsToScriptID(script, scriptID)) {
66
25.5k
        out.resize(21);
67
25.5k
        out[0] = 0x01;
68
25.5k
        memcpy(&out[1], &scriptID, 20);
69
25.5k
        return true;
70
25.5k
    }
71
394k
    CPubKey pubkey;
72
394k
    if (IsToPubKey(script, pubkey)) {
73
13.2k
        out.resize(33);
74
13.2k
        memcpy(&out[1], &pubkey[1], 32);
75
13.2k
        if (pubkey[0] == 0x02 || pubkey[0] == 0x03) {
76
13.1k
            out[0] = pubkey[0];
77
13.1k
            return true;
78
13.1k
        } else if (pubkey[0] == 0x04) {
79
69
            out[0] = 0x04 | (pubkey[64] & 0x01);
80
69
            return true;
81
69
        }
82
13.2k
    }
83
381k
    return false;
84
394k
}
85
86
unsigned int GetSpecialScriptSize(unsigned int nSize)
87
128k
{
88
128k
    if (nSize == 0 || nSize == 1)
89
99.3k
        return 20;
90
28.6k
    if (nSize == 2 || nSize == 3 || nSize == 4 || nSize == 5)
91
28.6k
        return 32;
92
0
    return 0;
93
28.6k
}
94
95
bool DecompressScript(CScript& script, unsigned int nSize, const CompressedScript& in)
96
128k
{
97
128k
    switch(nSize) {
98
90.1k
    case 0x00:
99
90.1k
        script.resize(25);
100
90.1k
        script[0] = OP_DUP;
101
90.1k
        script[1] = OP_HASH160;
102
90.1k
        script[2] = 20;
103
90.1k
        memcpy(&script[3], in.data(), 20);
104
90.1k
        script[23] = OP_EQUALVERIFY;
105
90.1k
        script[24] = OP_CHECKSIG;
106
90.1k
        return true;
107
9.17k
    case 0x01:
108
9.17k
        script.resize(23);
109
9.17k
        script[0] = OP_HASH160;
110
9.17k
        script[1] = 20;
111
9.17k
        memcpy(&script[2], in.data(), 20);
112
9.17k
        script[22] = OP_EQUAL;
113
9.17k
        return true;
114
24.7k
    case 0x02:
115
28.5k
    case 0x03:
116
28.5k
        script.resize(35);
117
28.5k
        script[0] = 33;
118
28.5k
        script[1] = nSize;
119
28.5k
        memcpy(&script[2], in.data(), 32);
120
28.5k
        script[34] = OP_CHECKSIG;
121
28.5k
        return true;
122
99
    case 0x04:
123
144
    case 0x05:
124
144
        unsigned char vch[33] = {};
125
144
        vch[0] = nSize - 2;
126
144
        memcpy(&vch[1], in.data(), 32);
127
144
        CPubKey pubkey{vch};
128
144
        if (!pubkey.Decompress())
129
2
            return false;
130
144
        assert(pubkey.size() == 65);
131
142
        script.resize(67);
132
142
        script[0] = 65;
133
142
        memcpy(&script[1], pubkey.begin(), 65);
134
142
        script[66] = OP_CHECKSIG;
135
142
        return true;
136
128k
    }
137
0
    return false;
138
128k
}
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
1.14M
{
151
1.14M
    if (n == 0)
152
4.00k
        return 0;
153
1.14M
    int e = 0;
154
6.94M
    while (((n % 10) == 0) && e < 9) {
155
5.79M
        n /= 10;
156
5.79M
        e++;
157
5.79M
    }
158
1.14M
    if (e < 9) {
159
668k
        int d = (n % 10);
160
668k
        assert(d >= 1 && d <= 9);
161
668k
        n /= 10;
162
668k
        return 1 + (n*9 + d - 1)*10 + e;
163
668k
    } else {
164
477k
        return 1 + (n - 1)*10 + 9;
165
477k
    }
166
1.14M
}
167
168
uint64_t DecompressAmount(uint64_t x)
169
1.05M
{
170
    // x = 0  OR  x = 1+10*(9*n + d - 1) + e  OR  x = 1+10*(n - 1) + 9
171
1.05M
    if (x == 0)
172
286
        return 0;
173
1.05M
    x--;
174
    // x = 10*(9*n + d - 1) + e
175
1.05M
    int e = x % 10;
176
1.05M
    x /= 10;
177
1.05M
    uint64_t n = 0;
178
1.05M
    if (e < 9) {
179
        // x = 9*n + d - 1
180
470k
        int d = (x % 9) + 1;
181
470k
        x /= 9;
182
        // x = n
183
470k
        n = x*10 + d;
184
580k
    } else {
185
580k
        n = x+1;
186
580k
    }
187
7.53M
    while (e) {
188
6.48M
        n *= 10;
189
6.48M
        e--;
190
6.48M
    }
191
1.05M
    return n;
192
1.05M
}