Branch data 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 : : static bool IsToKeyID(const CScript& script, CKeyID &hash)
# 20 : 519653 : {
# 21 [ + + ][ + + ]: 519653 : if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160
# [ + - ]
# 22 [ + - ][ + - ]: 519653 : && script[2] == 20 && script[23] == OP_EQUALVERIFY
# 23 [ + - ]: 519653 : && script[24] == OP_CHECKSIG) {
# 24 : 57409 : memcpy(&hash, &script[3], 20);
# 25 : 57409 : return true;
# 26 : 57409 : }
# 27 : 462244 : return false;
# 28 : 519653 : }
# 29 : :
# 30 : : static bool IsToScriptID(const CScript& script, CScriptID &hash)
# 31 : 462244 : {
# 32 [ + + ][ + + ]: 462244 : if (script.size() == 23 && script[0] == OP_HASH160 && script[1] == 20
# [ + - ]
# 33 [ + - ]: 462244 : && script[22] == OP_EQUAL) {
# 34 : 35330 : memcpy(&hash, &script[2], 20);
# 35 : 35330 : return true;
# 36 : 35330 : }
# 37 : 426914 : return false;
# 38 : 462244 : }
# 39 : :
# 40 : : static bool IsToPubKey(const CScript& script, CPubKey &pubkey)
# 41 : 426914 : {
# 42 [ + + ][ + + ]: 426914 : if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG
# [ + - ]
# 43 [ + + ][ + - ]: 426914 : && (script[1] == 0x02 || script[1] == 0x03)) {
# 44 : 13344 : pubkey.Set(&script[1], &script[34]);
# 45 : 13344 : return true;
# 46 : 13344 : }
# 47 [ + + ][ + - ]: 413570 : if (script.size() == 67 && script[0] == 65 && script[66] == OP_CHECKSIG
# [ + - ]
# 48 [ + - ]: 413570 : && script[1] == 0x04) {
# 49 : 141 : pubkey.Set(&script[1], &script[66]);
# 50 : 141 : return pubkey.IsFullyValid(); // if not fully valid, a case that would not be compressible
# 51 : 141 : }
# 52 : 413429 : return false;
# 53 : 413570 : }
# 54 : :
# 55 : : bool CompressScript(const CScript& script, CompressedScript& out)
# 56 : 519653 : {
# 57 : 519653 : CKeyID keyID;
# 58 [ + + ]: 519653 : if (IsToKeyID(script, keyID)) {
# 59 : 57409 : out.resize(21);
# 60 : 57409 : out[0] = 0x00;
# 61 : 57409 : memcpy(&out[1], &keyID, 20);
# 62 : 57409 : return true;
# 63 : 57409 : }
# 64 : 462244 : CScriptID scriptID;
# 65 [ + + ]: 462244 : if (IsToScriptID(script, scriptID)) {
# 66 : 35330 : out.resize(21);
# 67 : 35330 : out[0] = 0x01;
# 68 : 35330 : memcpy(&out[1], &scriptID, 20);
# 69 : 35330 : return true;
# 70 : 35330 : }
# 71 : 426914 : CPubKey pubkey;
# 72 [ + + ]: 426914 : if (IsToPubKey(script, pubkey)) {
# 73 : 13485 : out.resize(33);
# 74 : 13485 : memcpy(&out[1], &pubkey[1], 32);
# 75 [ + + ][ + + ]: 13485 : if (pubkey[0] == 0x02 || pubkey[0] == 0x03) {
# 76 : 13344 : out[0] = pubkey[0];
# 77 : 13344 : return true;
# 78 [ + - ]: 13344 : } else if (pubkey[0] == 0x04) {
# 79 : 141 : out[0] = 0x04 | (pubkey[64] & 0x01);
# 80 : 141 : return true;
# 81 : 141 : }
# 82 : 13485 : }
# 83 : 413429 : return false;
# 84 : 426914 : }
# 85 : :
# 86 : : unsigned int GetSpecialScriptSize(unsigned int nSize)
# 87 : 21941 : {
# 88 [ + + ][ + + ]: 21941 : if (nSize == 0 || nSize == 1)
# 89 : 17245 : return 20;
# 90 [ + + ][ + + ]: 4696 : if (nSize == 2 || nSize == 3 || nSize == 4 || nSize == 5)
# [ + - ][ # # ]
# 91 : 4696 : return 32;
# 92 : 0 : return 0;
# 93 : 4696 : }
# 94 : :
# 95 : : bool DecompressScript(CScript& script, unsigned int nSize, const CompressedScript& in)
# 96 : 21941 : {
# 97 [ - + ]: 21941 : switch(nSize) {
# 98 [ + + ]: 13506 : case 0x00:
# 99 : 13506 : script.resize(25);
# 100 : 13506 : script[0] = OP_DUP;
# 101 : 13506 : script[1] = OP_HASH160;
# 102 : 13506 : script[2] = 20;
# 103 : 13506 : memcpy(&script[3], in.data(), 20);
# 104 : 13506 : script[23] = OP_EQUALVERIFY;
# 105 : 13506 : script[24] = OP_CHECKSIG;
# 106 : 13506 : return true;
# 107 [ + + ]: 3739 : case 0x01:
# 108 : 3739 : script.resize(23);
# 109 : 3739 : script[0] = OP_HASH160;
# 110 : 3739 : script[1] = 20;
# 111 : 3739 : memcpy(&script[2], in.data(), 20);
# 112 : 3739 : script[22] = OP_EQUAL;
# 113 : 3739 : return true;
# 114 [ + + ]: 4658 : case 0x02:
# 115 [ + + ]: 4670 : case 0x03:
# 116 : 4670 : script.resize(35);
# 117 : 4670 : script[0] = 33;
# 118 : 4670 : script[1] = nSize;
# 119 : 4670 : memcpy(&script[2], in.data(), 32);
# 120 : 4670 : script[34] = OP_CHECKSIG;
# 121 : 4670 : return true;
# 122 [ + + ]: 26 : case 0x04:
# 123 [ - + ]: 26 : 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};
# 128 [ - + ]: 26 : if (!pubkey.Decompress())
# 129 : 0 : return false;
# 130 : 26 : assert(pubkey.size() == 65);
# 131 : 0 : 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 : 21941 : }
# 137 : 0 : return false;
# 138 : 21941 : }
# 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 : 1799657 : {
# 151 [ + + ]: 1799657 : if (n == 0)
# 152 : 67 : return 0;
# 153 : 1799590 : int e = 0;
# 154 [ + + ][ + + ]: 12064018 : while (((n % 10) == 0) && e < 9) {
# 155 : 10264428 : n /= 10;
# 156 : 10264428 : e++;
# 157 : 10264428 : }
# 158 [ + + ]: 1799590 : if (e < 9) {
# 159 : 896344 : int d = (n % 10);
# 160 : 896344 : assert(d >= 1 && d <= 9);
# 161 : 0 : n /= 10;
# 162 : 896344 : return 1 + (n*9 + d - 1)*10 + e;
# 163 : 903246 : } else {
# 164 : 903246 : return 1 + (n - 1)*10 + 9;
# 165 : 903246 : }
# 166 : 1799590 : }
# 167 : :
# 168 : : uint64_t DecompressAmount(uint64_t x)
# 169 : 1470458 : {
# 170 : : // x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9
# 171 [ + + ]: 1470458 : if (x == 0)
# 172 : 30 : return 0;
# 173 : 1470428 : x--;
# 174 : : // x = 10*(9*n + d - 1) + e
# 175 : 1470428 : int e = x % 10;
# 176 : 1470428 : x /= 10;
# 177 : 1470428 : uint64_t n = 0;
# 178 [ + + ]: 1470428 : if (e < 9) {
# 179 : : // x = 9*n + d - 1
# 180 : 591686 : int d = (x % 9) + 1;
# 181 : 591686 : x /= 9;
# 182 : : // x = n
# 183 : 591686 : n = x*10 + d;
# 184 : 878742 : } else {
# 185 : 878742 : n = x+1;
# 186 : 878742 : }
# 187 [ + + ]: 10480715 : while (e) {
# 188 : 9010287 : n *= 10;
# 189 : 9010287 : e--;
# 190 : 9010287 : }
# 191 : 1470428 : return n;
# 192 : 1470458 : }
|