1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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.
6 #include "compressor.h"
10 #include "script/standard.h"
12 bool CScriptCompressor::IsToKeyID(CKeyID
&hash
) const
14 if (script
.size() == 25 && script
[0] == OP_DUP
&& script
[1] == OP_HASH160
15 && script
[2] == 20 && script
[23] == OP_EQUALVERIFY
16 && script
[24] == OP_CHECKSIG
) {
17 memcpy(&hash
, &script
[3], 20);
23 bool CScriptCompressor::IsToScriptID(CScriptID
&hash
) const
25 if (script
.size() == 23 && script
[0] == OP_HASH160
&& script
[1] == 20
26 && script
[22] == OP_EQUAL
) {
27 memcpy(&hash
, &script
[2], 20);
33 bool CScriptCompressor::IsToPubKey(CPubKey
&pubkey
) const
35 if (script
.size() == 35 && script
[0] == 33 && script
[34] == OP_CHECKSIG
36 && (script
[1] == 0x02 || script
[1] == 0x03)) {
37 pubkey
.Set(&script
[1], &script
[34]);
40 if (script
.size() == 67 && script
[0] == 65 && script
[66] == OP_CHECKSIG
41 && script
[1] == 0x04) {
42 pubkey
.Set(&script
[1], &script
[66]);
43 return pubkey
.IsFullyValid(); // if not fully valid, a case that would not be compressible
48 bool CScriptCompressor::Compress(std::vector
<unsigned char> &out
) const
51 if (IsToKeyID(keyID
)) {
54 memcpy(&out
[1], &keyID
, 20);
58 if (IsToScriptID(scriptID
)) {
61 memcpy(&out
[1], &scriptID
, 20);
65 if (IsToPubKey(pubkey
)) {
67 memcpy(&out
[1], &pubkey
[1], 32);
68 if (pubkey
[0] == 0x02 || pubkey
[0] == 0x03) {
71 } else if (pubkey
[0] == 0x04) {
72 out
[0] = 0x04 | (pubkey
[64] & 0x01);
79 unsigned int CScriptCompressor::GetSpecialSize(unsigned int nSize
) const
81 if (nSize
== 0 || nSize
== 1)
83 if (nSize
== 2 || nSize
== 3 || nSize
== 4 || nSize
== 5)
88 bool CScriptCompressor::Decompress(unsigned int nSize
, const std::vector
<unsigned char> &in
)
94 script
[1] = OP_HASH160
;
96 memcpy(&script
[3], in
.data(), 20);
97 script
[23] = OP_EQUALVERIFY
;
98 script
[24] = OP_CHECKSIG
;
102 script
[0] = OP_HASH160
;
104 memcpy(&script
[2], in
.data(), 20);
105 script
[22] = OP_EQUAL
;
112 memcpy(&script
[2], in
.data(), 32);
113 script
[34] = OP_CHECKSIG
;
117 unsigned char vch
[33] = {};
119 memcpy(&vch
[1], in
.data(), 32);
120 CPubKey
pubkey(&vch
[0], &vch
[33]);
121 if (!pubkey
.Decompress())
123 assert(pubkey
.size() == 65);
126 memcpy(&script
[1], pubkey
.begin(), 65);
127 script
[66] = OP_CHECKSIG
;
133 // Amount compression:
134 // * If the amount is 0, output 0
135 // * first, divide the amount (in base units) by the largest power of 10 possible; call the exponent e (e is max 9)
136 // * if e<9, the last digit of the resulting number cannot be 0; store it as d, and drop it (divide by 10)
137 // * call the result n
138 // * output 1 + 10*(9*n + d - 1) + e
139 // * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n - 1) + 9
140 // (this is decodable, as d is in [1-9] and e is in [0-9])
142 uint64_t CTxOutCompressor::CompressAmount(uint64_t n
)
147 while (((n
% 10) == 0) && e
< 9) {
153 assert(d
>= 1 && d
<= 9);
155 return 1 + (n
*9 + d
- 1)*10 + e
;
157 return 1 + (n
- 1)*10 + 9;
161 uint64_t CTxOutCompressor::DecompressAmount(uint64_t x
)
163 // x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9
167 // x = 10*(9*n + d - 1) + e