Coverage Report

Created: 2026-05-06 07:53

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
475k
{
21
475k
    if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160
22
475k
                            && script[2] == 20 && script[23] == OP_EQUALVERIFY
23
475k
                            && script[24] == OP_CHECKSIG) {
24
87.9k
        memcpy(&hash, &script[3], 20);
25
87.9k
        return true;
26
87.9k
    }
27
387k
    return false;
28
475k
}
29
30
static bool IsToScriptID(const CScript& script, CScriptID &hash)
31
387k
{
32
387k
    if (script.size() == 23 && script[0] == OP_HASH160 && script[1] == 20
33
387k
                            && script[22] == OP_EQUAL) {
34
25.8k
        memcpy(&hash, &script[2], 20);
35
25.8k
        return true;
36
25.8k
    }
37
361k
    return false;
38
387k
}
39
40
static bool IsToPubKey(const CScript& script, CPubKey &pubkey)
41
361k
{
42
361k
    if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG
43
361k
                            && (script[1] == 0x02 || script[1] == 0x03)) {
44
12.8k
        pubkey.Set(&script[1], &script[34]);
45
12.8k
        return true;
46
12.8k
    }
47
348k
    if (script.size() == 67 && script[0] == 65 && script[66] == OP_CHECKSIG
48
348k
                            && 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
348k
    return false;
53
348k
}
54
55
bool CompressScript(const CScript& script, CompressedScript& out)
56
475k
{
57
475k
    CKeyID keyID;
58
475k
    if (IsToKeyID(script, keyID)) {
59
87.9k
        out.resize(21);
60
87.9k
        out[0] = 0x00;
61
87.9k
        memcpy(&out[1], &keyID, 20);
62
87.9k
        return true;
63
87.9k
    }
64
387k
    CScriptID scriptID;
65
387k
    if (IsToScriptID(script, scriptID)) {
66
25.8k
        out.resize(21);
67
25.8k
        out[0] = 0x01;
68
25.8k
        memcpy(&out[1], &scriptID, 20);
69
25.8k
        return true;
70
25.8k
    }
71
361k
    CPubKey pubkey;
72
361k
    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
348k
    return false;
84
361k
}
85
86
unsigned int GetSpecialScriptSize(unsigned int nSize)
87
87.5k
{
88
87.5k
    if (nSize == 0 || nSize == 1)
89
70.5k
        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
87.5k
{
97
87.5k
    switch(nSize) {
98
67.0k
    case 0x00:
99
67.0k
        script.resize(25);
100
67.0k
        script[0] = OP_DUP;
101
67.0k
        script[1] = OP_HASH160;
102
67.0k
        script[2] = 20;
103
67.0k
        memcpy(&script[3], in.data(), 20);
104
67.0k
        script[23] = OP_EQUALVERIFY;
105
67.0k
        script[24] = OP_CHECKSIG;
106
67.0k
        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
112
    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
87.5k
    }
137
0
    return false;
138
87.5k
}
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.11M
{
151
1.11M
    if (n == 0)
152
5.07k
        return 0;
153
1.11M
    int e = 0;
154
6.82M
    while (((n % 10) == 0) && e < 9) {
155
5.71M
        n /= 10;
156
5.71M
        e++;
157
5.71M
    }
158
1.11M
    if (e < 9) {
159
634k
        int d = (n % 10);
160
634k
        assert(d >= 1 && d <= 9);
161
634k
        n /= 10;
162
634k
        return 1 + (n*9 + d - 1)*10 + e;
163
634k
    } else {
164
475k
        return 1 + (n - 1)*10 + 9;
165
475k
    }
166
1.11M
}
167
168
uint64_t DecompressAmount(uint64_t x)
169
982k
{
170
    // x = 0  OR  x = 1+10*(9*n + d - 1) + e  OR  x = 1+10*(n - 1) + 9
171
982k
    if (x == 0)
172
208
        return 0;
173
982k
    x--;
174
    // x = 10*(9*n + d - 1) + e
175
982k
    int e = x % 10;
176
982k
    x /= 10;
177
982k
    uint64_t n = 0;
178
982k
    if (e < 9) {
179
        // x = 9*n + d - 1
180
405k
        int d = (x % 9) + 1;
181
405k
        x /= 9;
182
        // x = n
183
405k
        n = x*10 + d;
184
576k
    } else {
185
576k
        n = x+1;
186
576k
    }
187
7.31M
    while (e) {
188
6.33M
        n *= 10;
189
6.33M
        e--;
190
6.33M
    }
191
982k
    return n;
192
982k
}