Coverage Report

Created: 2026-07-29 23:27

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