Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2021 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/standard.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 1212454 : static bool IsToKeyID(const CScript& script, CKeyID &hash)
20 : {
21 1954397 : if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160
22 741943 : && script[2] == 20 && script[23] == OP_EQUALVERIFY
23 741943 : && script[24] == OP_CHECKSIG) {
24 741943 : memcpy(&hash, &script[3], 20);
25 741943 : return true;
26 : }
27 470511 : return false;
28 1212454 : }
29 :
30 470511 : static bool IsToScriptID(const CScript& script, CScriptID &hash)
31 : {
32 470511 : if (script.size() == 23 && script[0] == OP_HASH160 && script[1] == 20
33 238359 : && script[22] == OP_EQUAL) {
34 238359 : memcpy(&hash, &script[2], 20);
35 238359 : return true;
36 : }
37 232152 : return false;
38 470511 : }
39 :
40 232152 : static bool IsToPubKey(const CScript& script, CPubKey &pubkey)
41 : {
42 241695 : if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG
43 20057 : && (script[1] == 0x02 || script[1] == 0x03)) {
44 20057 : pubkey.Set(&script[1], &script[34]);
45 20057 : return true;
46 : }
47 212095 : if (script.size() == 67 && script[0] == 65 && script[66] == OP_CHECKSIG
48 1 : && script[1] == 0x04) {
49 1 : pubkey.Set(&script[1], &script[66]);
50 1 : return pubkey.IsFullyValid(); // if not fully valid, a case that would not be compressible
51 : }
52 212094 : return false;
53 232152 : }
54 :
55 1212454 : bool CompressScript(const CScript& script, CompressedScript& out)
56 : {
57 1212454 : CKeyID keyID;
58 1212454 : if (IsToKeyID(script, keyID)) {
59 741943 : out.resize(21);
60 741943 : out[0] = 0x00;
61 741943 : memcpy(&out[1], &keyID, 20);
62 741943 : return true;
63 : }
64 470511 : CScriptID scriptID;
65 470511 : if (IsToScriptID(script, scriptID)) {
66 238359 : out.resize(21);
67 238359 : out[0] = 0x01;
68 238359 : memcpy(&out[1], &scriptID, 20);
69 238359 : return true;
70 : }
71 232152 : CPubKey pubkey;
72 232152 : if (IsToPubKey(script, pubkey)) {
73 20058 : out.resize(33);
74 20058 : memcpy(&out[1], &pubkey[1], 32);
75 20058 : if (pubkey[0] == 0x02 || pubkey[0] == 0x03) {
76 20057 : out[0] = pubkey[0];
77 20057 : return true;
78 1 : } else if (pubkey[0] == 0x04) {
79 1 : out[0] = 0x04 | (pubkey[64] & 0x01);
80 1 : return true;
81 : }
82 0 : }
83 212094 : return false;
84 1212454 : }
85 :
86 431115 : unsigned int GetSpecialScriptSize(unsigned int nSize)
87 : {
88 431115 : if (nSize == 0 || nSize == 1)
89 423298 : return 20;
90 7817 : if (nSize == 2 || nSize == 3 || nSize == 4 || nSize == 5)
91 7817 : return 32;
92 0 : return 0;
93 431115 : }
94 :
95 431115 : bool DecompressScript(CScript& script, unsigned int nSize, const CompressedScript& in)
96 : {
97 431115 : switch(nSize) {
98 : case 0x00:
99 170299 : script.resize(25);
100 170299 : script[0] = OP_DUP;
101 170299 : script[1] = OP_HASH160;
102 170299 : script[2] = 20;
103 170299 : memcpy(&script[3], in.data(), 20);
104 170299 : script[23] = OP_EQUALVERIFY;
105 170299 : script[24] = OP_CHECKSIG;
106 170299 : return true;
107 : case 0x01:
108 252999 : script.resize(23);
109 252999 : script[0] = OP_HASH160;
110 252999 : script[1] = 20;
111 252999 : memcpy(&script[2], in.data(), 20);
112 252999 : script[22] = OP_EQUAL;
113 252999 : return true;
114 : case 0x02:
115 : case 0x03:
116 7817 : script.resize(35);
117 7817 : script[0] = 33;
118 7817 : script[1] = nSize;
119 7817 : memcpy(&script[2], in.data(), 32);
120 7817 : script[34] = OP_CHECKSIG;
121 7817 : return true;
122 : case 0x04:
123 : case 0x05:
124 0 : unsigned char vch[33] = {};
125 0 : vch[0] = nSize - 2;
126 0 : memcpy(&vch[1], in.data(), 32);
127 0 : CPubKey pubkey{vch};
128 0 : if (!pubkey.Decompress())
129 0 : return false;
130 0 : assert(pubkey.size() == 65);
131 0 : script.resize(67);
132 0 : script[0] = 65;
133 0 : memcpy(&script[1], pubkey.begin(), 65);
134 0 : script[66] = OP_CHECKSIG;
135 0 : return true;
136 : }
137 0 : return false;
138 431115 : }
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 1852456 : uint64_t CompressAmount(uint64_t n)
150 : {
151 1852456 : if (n == 0)
152 125 : return 0;
153 1852331 : int e = 0;
154 9370299 : while (((n % 10) == 0) && e < 9) {
155 7517968 : n /= 10;
156 7517968 : e++;
157 : }
158 1852331 : if (e < 9) {
159 1300349 : int d = (n % 10);
160 1300349 : assert(d >= 1 && d <= 9);
161 1300349 : n /= 10;
162 1300349 : return 1 + (n*9 + d - 1)*10 + e;
163 : } else {
164 551982 : return 1 + (n - 1)*10 + 9;
165 : }
166 1852456 : }
167 :
168 1143583 : uint64_t DecompressAmount(uint64_t x)
169 : {
170 : // x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9
171 1143583 : if (x == 0)
172 65 : return 0;
173 1143518 : x--;
174 : // x = 10*(9*n + d - 1) + e
175 1143518 : int e = x % 10;
176 1143518 : x /= 10;
177 1143518 : uint64_t n = 0;
178 1143518 : if (e < 9) {
179 : // x = 9*n + d - 1
180 493021 : int d = (x % 9) + 1;
181 493021 : x /= 9;
182 : // x = n
183 493021 : n = x*10 + d;
184 493021 : } else {
185 650497 : n = x+1;
186 : }
187 7668719 : while (e) {
188 6525201 : n *= 10;
189 6525201 : e--;
190 : }
191 1143518 : return n;
192 1143583 : }
|