LCOV - code coverage report
Current view: top level - src - compressor.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 140 146 95.9 %
Date: 2021-06-29 14:35:33 Functions: 8 8 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 74 92 80.4 %

           Branch data     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                 :            : static bool IsToKeyID(const CScript& script, CKeyID &hash)
#      20                 :     597720 : {
#      21 [ +  + ][ +  + ]:     597720 :     if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160
#                 [ +  - ]
#      22 [ +  - ][ +  - ]:     597720 :                             && script[2] == 20 && script[23] == OP_EQUALVERIFY
#      23         [ +  - ]:     597720 :                             && script[24] == OP_CHECKSIG) {
#      24                 :      65095 :         memcpy(&hash, &script[3], 20);
#      25                 :      65095 :         return true;
#      26                 :      65095 :     }
#      27                 :     532625 :     return false;
#      28                 :     532625 : }
#      29                 :            : 
#      30                 :            : static bool IsToScriptID(const CScript& script, CScriptID &hash)
#      31                 :     532625 : {
#      32 [ +  + ][ +  + ]:     532625 :     if (script.size() == 23 && script[0] == OP_HASH160 && script[1] == 20
#                 [ +  - ]
#      33         [ +  - ]:     532625 :                             && script[22] == OP_EQUAL) {
#      34                 :     135077 :         memcpy(&hash, &script[2], 20);
#      35                 :     135077 :         return true;
#      36                 :     135077 :     }
#      37                 :     397548 :     return false;
#      38                 :     397548 : }
#      39                 :            : 
#      40                 :            : static bool IsToPubKey(const CScript& script, CPubKey &pubkey)
#      41                 :     397548 : {
#      42 [ +  + ][ +  + ]:     397548 :     if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG
#                 [ +  - ]
#      43 [ +  + ][ +  - ]:     397548 :                             && (script[1] == 0x02 || script[1] == 0x03)) {
#      44                 :      14154 :         pubkey.Set(&script[1], &script[34]);
#      45                 :      14154 :         return true;
#      46                 :      14154 :     }
#      47 [ +  + ][ +  - ]:     383394 :     if (script.size() == 67 && script[0] == 65 && script[66] == OP_CHECKSIG
#                 [ +  - ]
#      48         [ +  - ]:     383394 :                             && script[1] == 0x04) {
#      49                 :        161 :         pubkey.Set(&script[1], &script[66]);
#      50                 :        161 :         return pubkey.IsFullyValid(); // if not fully valid, a case that would not be compressible
#      51                 :        161 :     }
#      52                 :     383233 :     return false;
#      53                 :     383233 : }
#      54                 :            : 
#      55                 :            : bool CompressScript(const CScript& script, CompressedScript& out)
#      56                 :     597720 : {
#      57                 :     597720 :     CKeyID keyID;
#      58         [ +  + ]:     597720 :     if (IsToKeyID(script, keyID)) {
#      59                 :      65095 :         out.resize(21);
#      60                 :      65095 :         out[0] = 0x00;
#      61                 :      65095 :         memcpy(&out[1], &keyID, 20);
#      62                 :      65095 :         return true;
#      63                 :      65095 :     }
#      64                 :     532625 :     CScriptID scriptID;
#      65         [ +  + ]:     532625 :     if (IsToScriptID(script, scriptID)) {
#      66                 :     135077 :         out.resize(21);
#      67                 :     135077 :         out[0] = 0x01;
#      68                 :     135077 :         memcpy(&out[1], &scriptID, 20);
#      69                 :     135077 :         return true;
#      70                 :     135077 :     }
#      71                 :     397548 :     CPubKey pubkey;
#      72         [ +  + ]:     397548 :     if (IsToPubKey(script, pubkey)) {
#      73                 :      14315 :         out.resize(33);
#      74                 :      14315 :         memcpy(&out[1], &pubkey[1], 32);
#      75 [ +  + ][ +  + ]:      14315 :         if (pubkey[0] == 0x02 || pubkey[0] == 0x03) {
#      76                 :      14154 :             out[0] = pubkey[0];
#      77                 :      14154 :             return true;
#      78         [ +  - ]:      14154 :         } else if (pubkey[0] == 0x04) {
#      79                 :        161 :             out[0] = 0x04 | (pubkey[64] & 0x01);
#      80                 :        161 :             return true;
#      81                 :        161 :         }
#      82                 :     383233 :     }
#      83                 :     383233 :     return false;
#      84                 :     383233 : }
#      85                 :            : 
#      86                 :            : unsigned int GetSpecialScriptSize(unsigned int nSize)
#      87                 :      24732 : {
#      88 [ +  + ][ +  + ]:      24732 :     if (nSize == 0 || nSize == 1)
#      89                 :      20472 :         return 20;
#      90 [ +  + ][ +  + ]:       4260 :     if (nSize == 2 || nSize == 3 || nSize == 4 || nSize == 5)
#         [ +  - ][ #  # ]
#      91                 :       4260 :         return 32;
#      92                 :          0 :     return 0;
#      93                 :          0 : }
#      94                 :            : 
#      95                 :            : bool DecompressScript(CScript& script, unsigned int nSize, const CompressedScript& in)
#      96                 :      24732 : {
#      97         [ -  + ]:      24732 :     switch(nSize) {
#      98         [ +  + ]:      13704 :     case 0x00:
#      99                 :      13704 :         script.resize(25);
#     100                 :      13704 :         script[0] = OP_DUP;
#     101                 :      13704 :         script[1] = OP_HASH160;
#     102                 :      13704 :         script[2] = 20;
#     103                 :      13704 :         memcpy(&script[3], in.data(), 20);
#     104                 :      13704 :         script[23] = OP_EQUALVERIFY;
#     105                 :      13704 :         script[24] = OP_CHECKSIG;
#     106                 :      13704 :         return true;
#     107         [ +  + ]:       6768 :     case 0x01:
#     108                 :       6768 :         script.resize(23);
#     109                 :       6768 :         script[0] = OP_HASH160;
#     110                 :       6768 :         script[1] = 20;
#     111                 :       6768 :         memcpy(&script[2], in.data(), 20);
#     112                 :       6768 :         script[22] = OP_EQUAL;
#     113                 :       6768 :         return true;
#     114         [ +  + ]:       4214 :     case 0x02:
#     115         [ +  + ]:       4229 :     case 0x03:
#     116                 :       4229 :         script.resize(35);
#     117                 :       4229 :         script[0] = 33;
#     118                 :       4229 :         script[1] = nSize;
#     119                 :       4229 :         memcpy(&script[2], in.data(), 32);
#     120                 :       4229 :         script[34] = OP_CHECKSIG;
#     121                 :       4229 :         return true;
#     122         [ +  + ]:       4214 :     case 0x04:
#     123         [ -  + ]:         31 :     case 0x05:
#     124                 :         31 :         unsigned char vch[33] = {};
#     125                 :         31 :         vch[0] = nSize - 2;
#     126                 :         31 :         memcpy(&vch[1], in.data(), 32);
#     127                 :         31 :         CPubKey pubkey{vch};
#     128         [ -  + ]:         31 :         if (!pubkey.Decompress())
#     129                 :          0 :             return false;
#     130                 :         31 :         assert(pubkey.size() == 65);
#     131                 :         31 :         script.resize(67);
#     132                 :         31 :         script[0] = 65;
#     133                 :         31 :         memcpy(&script[1], pubkey.begin(), 65);
#     134                 :         31 :         script[66] = OP_CHECKSIG;
#     135                 :         31 :         return true;
#     136                 :          0 :     }
#     137                 :          0 :     return false;
#     138                 :          0 : }
#     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                 :    1877724 : {
#     151         [ +  + ]:    1877724 :     if (n == 0)
#     152                 :         70 :         return 0;
#     153                 :    1877654 :     int e = 0;
#     154 [ +  + ][ +  + ]:   12241181 :     while (((n % 10) == 0) && e < 9) {
#     155                 :   10363527 :         n /= 10;
#     156                 :   10363527 :         e++;
#     157                 :   10363527 :     }
#     158         [ +  + ]:    1877654 :     if (e < 9) {
#     159                 :     975799 :         int d = (n % 10);
#     160                 :     975799 :         assert(d >= 1 && d <= 9);
#     161                 :     975799 :         n /= 10;
#     162                 :     975799 :         return 1 + (n*9 + d - 1)*10 + e;
#     163                 :     975799 :     } else {
#     164                 :     901855 :         return 1 + (n - 1)*10 + 9;
#     165                 :     901855 :     }
#     166                 :    1877654 : }
#     167                 :            : 
#     168                 :            : uint64_t DecompressAmount(uint64_t x)
#     169                 :    1459986 : {
#     170                 :            :     // x = 0  OR  x = 1+10*(9*n + d - 1) + e  OR  x = 1+10*(n - 1) + 9
#     171         [ +  + ]:    1459986 :     if (x == 0)
#     172                 :         30 :         return 0;
#     173                 :    1459956 :     x--;
#     174                 :            :     // x = 10*(9*n + d - 1) + e
#     175                 :    1459956 :     int e = x % 10;
#     176                 :    1459956 :     x /= 10;
#     177                 :    1459956 :     uint64_t n = 0;
#     178         [ +  + ]:    1459956 :     if (e < 9) {
#     179                 :            :         // x = 9*n + d - 1
#     180                 :     584154 :         int d = (x % 9) + 1;
#     181                 :     584154 :         x /= 9;
#     182                 :            :         // x = n
#     183                 :     584154 :         n = x*10 + d;
#     184                 :     875802 :     } else {
#     185                 :     875802 :         n = x+1;
#     186                 :     875802 :     }
#     187         [ +  + ]:   10427186 :     while (e) {
#     188                 :    8967230 :         n *= 10;
#     189                 :    8967230 :         e--;
#     190                 :    8967230 :     }
#     191                 :    1459956 :     return n;
#     192                 :    1459956 : }

Generated by: LCOV version 1.14