Coverage Report

Created: 2026-04-29 19:21

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
482k
{
21
482k
    if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160
22
482k
                            && script[2] == 20 && script[23] == OP_EQUALVERIFY
23
482k
                            && script[24] == OP_CHECKSIG) {
24
88.1k
        memcpy(&hash, &script[3], 20);
25
88.1k
        return true;
26
88.1k
    }
27
394k
    return false;
28
482k
}
29
30
static bool IsToScriptID(const CScript& script, CScriptID &hash)
31
394k
{
32
394k
    if (script.size() == 23 && script[0] == OP_HASH160 && script[1] == 20
33
394k
                            && script[22] == OP_EQUAL) {
34
25.9k
        memcpy(&hash, &script[2], 20);
35
25.9k
        return true;
36
25.9k
    }
37
368k
    return false;
38
394k
}
39
40
static bool IsToPubKey(const CScript& script, CPubKey &pubkey)
41
368k
{
42
368k
    if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG
43
368k
                            && (script[1] == 0x02 || script[1] == 0x03)) {
44
12.8k
        pubkey.Set(&script[1], &script[34]);
45
12.8k
        return true;
46
12.8k
    }
47
355k
    if (script.size() == 67 && script[0] == 65 && script[66] == OP_CHECKSIG
48
355k
                            && script[1] == 0x04) {
49
65
        pubkey.Set(&script[1], &script[66]);
50
65
        return pubkey.IsFullyValid(); // if not fully valid, a case that would not be compressible
51
65
    }
52
355k
    return false;
53
355k
}
54
55
bool CompressScript(const CScript& script, CompressedScript& out)
56
482k
{
57
482k
    CKeyID keyID;
58
482k
    if (IsToKeyID(script, keyID)) {
59
88.1k
        out.resize(21);
60
88.1k
        out[0] = 0x00;
61
88.1k
        memcpy(&out[1], &keyID, 20);
62
88.1k
        return true;
63
88.1k
    }
64
394k
    CScriptID scriptID;
65
394k
    if (IsToScriptID(script, scriptID)) {
66
25.9k
        out.resize(21);
67
25.9k
        out[0] = 0x01;
68
25.9k
        memcpy(&out[1], &scriptID, 20);
69
25.9k
        return true;
70
25.9k
    }
71
368k
    CPubKey pubkey;
72
368k
    if (IsToPubKey(script, pubkey)) {
73
12.9k
        out.resize(33);
74
12.9k
        memcpy(&out[1], &pubkey[1], 32);
75
12.9k
        if (pubkey[0] == 0x02 || pubkey[0] == 0x03) {
76
12.8k
            out[0] = pubkey[0];
77
12.8k
            return true;
78
12.8k
        } else if (pubkey[0] == 0x04) {
79
64
            out[0] = 0x04 | (pubkey[64] & 0x01);
80
64
            return true;
81
64
        }
82
12.9k
    }
83
355k
    return false;
84
368k
}
85
86
unsigned int GetSpecialScriptSize(unsigned int nSize)
87
85.7k
{
88
85.7k
    if (nSize == 0 || nSize == 1)
89
68.7k
        return 20;
90
16.9k
    if (nSize == 2 || nSize == 3 || nSize == 4 || nSize == 5)
91
16.9k
        return 32;
92
0
    return 0;
93
16.9k
}
94
95
bool DecompressScript(CScript& script, unsigned int nSize, const CompressedScript& in)
96
85.7k
{
97
85.7k
    switch(nSize) {
98
65.2k
    case 0x00:
99
65.2k
        script.resize(25);
100
65.2k
        script[0] = OP_DUP;
101
65.2k
        script[1] = OP_HASH160;
102
65.2k
        script[2] = 20;
103
65.2k
        memcpy(&script[3], in.data(), 20);
104
65.2k
        script[23] = OP_EQUALVERIFY;
105
65.2k
        script[24] = OP_CHECKSIG;
106
65.2k
        return true;
107
3.53k
    case 0x01:
108
3.53k
        script.resize(23);
109
3.53k
        script[0] = OP_HASH160;
110
3.53k
        script[1] = 20;
111
3.53k
        memcpy(&script[2], in.data(), 20);
112
3.53k
        script[22] = OP_EQUAL;
113
3.53k
        return true;
114
16.7k
    case 0x02:
115
16.8k
    case 0x03:
116
16.8k
        script.resize(35);
117
16.8k
        script[0] = 33;
118
16.8k
        script[1] = nSize;
119
16.8k
        memcpy(&script[2], in.data(), 32);
120
16.8k
        script[34] = OP_CHECKSIG;
121
16.8k
        return true;
122
103
    case 0x04:
123
130
    case 0x05:
124
130
        unsigned char vch[33] = {};
125
130
        vch[0] = nSize - 2;
126
130
        memcpy(&vch[1], in.data(), 32);
127
130
        CPubKey pubkey{vch};
128
130
        if (!pubkey.Decompress())
129
2
            return false;
130
130
        assert(pubkey.size() == 65);
131
128
        script.resize(67);
132
128
        script[0] = 65;
133
128
        memcpy(&script[1], pubkey.begin(), 65);
134
128
        script[66] = OP_CHECKSIG;
135
128
        return true;
136
85.7k
    }
137
0
    return false;
138
85.7k
}
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.12M
{
151
1.12M
    if (n == 0)
152
5.07k
        return 0;
153
1.11M
    int e = 0;
154
6.84M
    while (((n % 10) == 0) && e < 9) {
155
5.72M
        n /= 10;
156
5.72M
        e++;
157
5.72M
    }
158
1.11M
    if (e < 9) {
159
641k
        int d = (n % 10);
160
641k
        assert(d >= 1 && d <= 9);
161
641k
        n /= 10;
162
641k
        return 1 + (n*9 + d - 1)*10 + e;
163
641k
    } else {
164
475k
        return 1 + (n - 1)*10 + 9;
165
475k
    }
166
1.11M
}
167
168
uint64_t DecompressAmount(uint64_t x)
169
979k
{
170
    // x = 0  OR  x = 1+10*(9*n + d - 1) + e  OR  x = 1+10*(n - 1) + 9
171
979k
    if (x == 0)
172
208
        return 0;
173
979k
    x--;
174
    // x = 10*(9*n + d - 1) + e
175
979k
    int e = x % 10;
176
979k
    x /= 10;
177
979k
    uint64_t n = 0;
178
979k
    if (e < 9) {
179
        // x = 9*n + d - 1
180
404k
        int d = (x % 9) + 1;
181
404k
        x /= 9;
182
        // x = n
183
404k
        n = x*10 + d;
184
574k
    } else {
185
574k
        n = x+1;
186
574k
    }
187
7.30M
    while (e) {
188
6.32M
        n *= 10;
189
6.32M
        e--;
190
6.32M
    }
191
979k
    return n;
192
979k
}