Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2019 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 435757 : static bool IsToKeyID(const CScript& script, CKeyID &hash)
20 : {
21 481374 : if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160
22 45617 : && script[2] == 20 && script[23] == OP_EQUALVERIFY
23 45617 : && script[24] == OP_CHECKSIG) {
24 45617 : memcpy(&hash, &script[3], 20);
25 45617 : return true;
26 : }
27 390140 : return false;
28 435757 : }
29 :
30 390140 : static bool IsToScriptID(const CScript& script, CScriptID &hash)
31 : {
32 390140 : if (script.size() == 23 && script[0] == OP_HASH160 && script[1] == 20
33 134571 : && script[22] == OP_EQUAL) {
34 134571 : memcpy(&hash, &script[2], 20);
35 134571 : return true;
36 : }
37 255569 : return false;
38 390140 : }
39 :
40 255569 : static bool IsToPubKey(const CScript& script, CPubKey &pubkey)
41 : {
42 260239 : if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG
43 4755 : && (script[1] == 0x02 || script[1] == 0x03)) {
44 4755 : pubkey.Set(&script[1], &script[34]);
45 4755 : return true;
46 : }
47 250814 : if (script.size() == 67 && script[0] == 65 && script[66] == OP_CHECKSIG
48 76 : && script[1] == 0x04) {
49 76 : pubkey.Set(&script[1], &script[66]);
50 76 : return pubkey.IsFullyValid(); // if not fully valid, a case that would not be compressible
51 : }
52 250738 : return false;
53 255569 : }
54 :
55 435757 : bool CompressScript(const CScript& script, std::vector<unsigned char> &out)
56 : {
57 435757 : CKeyID keyID;
58 435757 : if (IsToKeyID(script, keyID)) {
59 45617 : out.resize(21);
60 45617 : out[0] = 0x00;
61 45617 : memcpy(&out[1], &keyID, 20);
62 45617 : return true;
63 : }
64 390140 : CScriptID scriptID;
65 390140 : if (IsToScriptID(script, scriptID)) {
66 134571 : out.resize(21);
67 134571 : out[0] = 0x01;
68 134571 : memcpy(&out[1], &scriptID, 20);
69 134571 : return true;
70 : }
71 255569 : CPubKey pubkey;
72 255569 : if (IsToPubKey(script, pubkey)) {
73 4831 : out.resize(33);
74 4831 : memcpy(&out[1], &pubkey[1], 32);
75 4831 : if (pubkey[0] == 0x02 || pubkey[0] == 0x03) {
76 4755 : out[0] = pubkey[0];
77 4755 : return true;
78 76 : } else if (pubkey[0] == 0x04) {
79 76 : out[0] = 0x04 | (pubkey[64] & 0x01);
80 76 : return true;
81 : }
82 : }
83 250738 : return false;
84 435757 : }
85 :
86 19078 : unsigned int GetSpecialScriptSize(unsigned int nSize)
87 : {
88 19078 : if (nSize == 0 || nSize == 1)
89 17908 : return 20;
90 1170 : if (nSize == 2 || nSize == 3 || nSize == 4 || nSize == 5)
91 1170 : return 32;
92 0 : return 0;
93 19078 : }
94 :
95 19078 : bool DecompressScript(CScript& script, unsigned int nSize, const std::vector<unsigned char> &in)
96 : {
97 19078 : switch(nSize) {
98 : case 0x00:
99 14177 : script.resize(25);
100 14177 : script[0] = OP_DUP;
101 14177 : script[1] = OP_HASH160;
102 14177 : script[2] = 20;
103 14177 : memcpy(&script[3], in.data(), 20);
104 14177 : script[23] = OP_EQUALVERIFY;
105 14177 : script[24] = OP_CHECKSIG;
106 14177 : return true;
107 : case 0x01:
108 3731 : script.resize(23);
109 3731 : script[0] = OP_HASH160;
110 3731 : script[1] = 20;
111 3731 : memcpy(&script[2], in.data(), 20);
112 3731 : script[22] = OP_EQUAL;
113 3731 : return true;
114 : case 0x02:
115 : case 0x03:
116 1144 : script.resize(35);
117 1144 : script[0] = 33;
118 1144 : script[1] = nSize;
119 1144 : memcpy(&script[2], in.data(), 32);
120 1144 : script[34] = OP_CHECKSIG;
121 1144 : return true;
122 : case 0x04:
123 : case 0x05:
124 26 : unsigned char vch[33] = {};
125 26 : vch[0] = nSize - 2;
126 26 : memcpy(&vch[1], in.data(), 32);
127 26 : CPubKey pubkey(&vch[0], &vch[33]);
128 26 : if (!pubkey.Decompress())
129 0 : return false;
130 26 : assert(pubkey.size() == 65);
131 26 : script.resize(67);
132 26 : script[0] = 65;
133 26 : memcpy(&script[1], pubkey.begin(), 65);
134 26 : script[66] = OP_CHECKSIG;
135 26 : return true;
136 26 : }
137 0 : return false;
138 19078 : }
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 1075759 : uint64_t CompressAmount(uint64_t n)
150 : {
151 1075759 : if (n == 0)
152 43 : return 0;
153 : int e = 0;
154 6455186 : while (((n % 10) == 0) && e < 9) {
155 5379470 : n /= 10;
156 5379470 : e++;
157 : }
158 1075716 : if (e < 9) {
159 616007 : int d = (n % 10);
160 616007 : assert(d >= 1 && d <= 9);
161 616007 : n /= 10;
162 616007 : return 1 + (n*9 + d - 1)*10 + e;
163 : } else {
164 459709 : return 1 + (n - 1)*10 + 9;
165 : }
166 1075759 : }
167 :
168 754589 : 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 754589 : if (x == 0)
172 25 : return 0;
173 754564 : x--;
174 : // x = 10*(9*n + d - 1) + e
175 754564 : int e = x % 10;
176 754564 : x /= 10;
177 : uint64_t n = 0;
178 754564 : if (e < 9) {
179 : // x = 9*n + d - 1
180 314611 : int d = (x % 9) + 1;
181 314611 : x /= 9;
182 : // x = n
183 314611 : n = x*10 + d;
184 314611 : } else {
185 439953 : n = x+1;
186 : }
187 5278925 : while (e) {
188 4524361 : n *= 10;
189 4524361 : e--;
190 : }
191 : return n;
192 754589 : }
|