Coverage Report

Created: 2026-05-08 10:34

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
479k
{
21
479k
    if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160
22
479k
                            && script[2] == 20 && script[23] == OP_EQUALVERIFY
23
479k
                            && script[24] == OP_CHECKSIG) {
24
88.1k
        memcpy(&hash, &script[3], 20);
25
88.1k
        return true;
26
88.1k
    }
27
390k
    return false;
28
479k
}
29
30
static bool IsToScriptID(const CScript& script, CScriptID &hash)
31
390k
{
32
390k
    if (script.size() == 23 && script[0] == OP_HASH160 && script[1] == 20
33
390k
                            && script[22] == OP_EQUAL) {
34
26.6k
        memcpy(&hash, &script[2], 20);
35
26.6k
        return true;
36
26.6k
    }
37
364k
    return false;
38
390k
}
39
40
static bool IsToPubKey(const CScript& script, CPubKey &pubkey)
41
364k
{
42
364k
    if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG
43
364k
                            && (script[1] == 0x02 || script[1] == 0x03)) {
44
12.8k
        pubkey.Set(&script[1], &script[34]);
45
12.8k
        return true;
46
12.8k
    }
47
351k
    if (script.size() == 67 && script[0] == 65 && script[66] == OP_CHECKSIG
48
351k
                            && 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
351k
    return false;
53
351k
}
54
55
bool CompressScript(const CScript& script, CompressedScript& out)
56
479k
{
57
479k
    CKeyID keyID;
58
479k
    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
390k
    CScriptID scriptID;
65
390k
    if (IsToScriptID(script, scriptID)) {
66
26.6k
        out.resize(21);
67
26.6k
        out[0] = 0x01;
68
26.6k
        memcpy(&out[1], &scriptID, 20);
69
26.6k
        return true;
70
26.6k
    }
71
364k
    CPubKey pubkey;
72
364k
    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
351k
    return false;
84
364k
}
85
86
unsigned int GetSpecialScriptSize(unsigned int nSize)
87
86.6k
{
88
86.6k
    if (nSize == 0 || nSize == 1)
89
69.6k
        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
86.6k
{
97
86.6k
    switch(nSize) {
98
66.1k
    case 0x00:
99
66.1k
        script.resize(25);
100
66.1k
        script[0] = OP_DUP;
101
66.1k
        script[1] = OP_HASH160;
102
66.1k
        script[2] = 20;
103
66.1k
        memcpy(&script[3], in.data(), 20);
104
66.1k
        script[23] = OP_EQUALVERIFY;
105
66.1k
        script[24] = OP_CHECKSIG;
106
66.1k
        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
56
    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
86.6k
    }
137
0
    return false;
138
86.6k
}
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.83M
    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
638k
        int d = (n % 10);
160
638k
        assert(d >= 1 && d <= 9);
161
638k
        n /= 10;
162
638k
        return 1 + (n*9 + d - 1)*10 + e;
163
638k
    } else {
164
475k
        return 1 + (n - 1)*10 + 9;
165
475k
    }
166
1.11M
}
167
168
uint64_t DecompressAmount(uint64_t x)
169
972k
{
170
    // x = 0  OR  x = 1+10*(9*n + d - 1) + e  OR  x = 1+10*(n - 1) + 9
171
972k
    if (x == 0)
172
208
        return 0;
173
972k
    x--;
174
    // x = 10*(9*n + d - 1) + e
175
972k
    int e = x % 10;
176
972k
    x /= 10;
177
972k
    uint64_t n = 0;
178
972k
    if (e < 9) {
179
        // x = 9*n + d - 1
180
396k
        int d = (x % 9) + 1;
181
396k
        x /= 9;
182
        // x = n
183
396k
        n = x*10 + d;
184
575k
    } else {
185
575k
        n = x+1;
186
575k
    }
187
7.29M
    while (e) {
188
6.32M
        n *= 10;
189
6.32M
        e--;
190
6.32M
    }
191
972k
    return n;
192
972k
}