LCOV - code coverage report
Current view: top level - src - uint256.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 62 63 98.4 %
Date: 2021-06-29 14:35:33 Functions: 53 56 94.6 %
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: 8 8 100.0 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
#       2                 :            : // Copyright (c) 2009-2020 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                 :            : #ifndef BITCOIN_UINT256_H
#       7                 :            : #define BITCOIN_UINT256_H
#       8                 :            : 
#       9                 :            : #include <assert.h>
#      10                 :            : #include <cstring>
#      11                 :            : #include <stdint.h>
#      12                 :            : #include <string>
#      13                 :            : #include <vector>
#      14                 :            : 
#      15                 :            : /** Template base class for fixed-sized opaque blobs. */
#      16                 :            : template<unsigned int BITS>
#      17                 :            : class base_blob
#      18                 :            : {
#      19                 :            : protected:
#      20                 :            :     static constexpr int WIDTH = BITS / 8;
#      21                 :            :     uint8_t m_data[WIDTH];
#      22                 :            : public:
#      23                 :            :     /* construct 0 value by default */
#      24                 : 1055024866 :     constexpr base_blob() : m_data() {}
#      25                 :            : 
#      26                 :            :     /* constructor for constants between 1 and 255 */
#      27                 :            :     constexpr explicit base_blob(uint8_t v) : m_data{v} {}
#      28                 :            : 
#      29                 :            :     explicit base_blob(const std::vector<unsigned char>& vch);
#      30                 :            : 
#      31                 :            :     bool IsNull() const
#      32                 :   18772486 :     {
#      33 [ +  + ][ +  + ]:  130532084 :         for (int i = 0; i < WIDTH; i++)
#      34 [ +  + ][ +  + ]:  127028173 :             if (m_data[i] != 0)
#      35                 :   15268575 :                 return false;
#      36                 :   18772486 :         return true;
#      37                 :   18772486 :     }
#      38                 :            : 
#      39                 :            :     void SetNull()
#      40                 :    2972617 :     {
#      41                 :    2972617 :         memset(m_data, 0, sizeof(m_data));
#      42                 :    2972617 :     }
#      43                 :            : 
#      44                 : 2340676849 :     inline int Compare(const base_blob& other) const { return memcmp(m_data, other.m_data, sizeof(m_data)); }
#      45                 :            : 
#      46                 :  135852099 :     friend inline bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
#      47                 :     774768 :     friend inline bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
#      48                 : 1881131417 :     friend inline bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
#      49                 :            : 
#      50                 :            :     std::string GetHex() const;
#      51                 :            :     void SetHex(const char* psz);
#      52                 :            :     void SetHex(const std::string& str);
#      53                 :            :     std::string ToString() const;
#      54                 :            : 
#      55                 :    1594018 :     const unsigned char* data() const { return m_data; }
#      56                 :    9328051 :     unsigned char* data() { return m_data; }
#      57                 :            : 
#      58                 :            :     unsigned char* begin()
#      59                 : 1187187470 :     {
#      60                 : 1187187470 :         return &m_data[0];
#      61                 : 1187187470 :     }
#      62                 :            : 
#      63                 :            :     unsigned char* end()
#      64                 :      56779 :     {
#      65                 :      56779 :         return &m_data[WIDTH];
#      66                 :      56779 :     }
#      67                 :            : 
#      68                 :            :     const unsigned char* begin() const
#      69                 :   98087288 :     {
#      70                 :   98087288 :         return &m_data[0];
#      71                 :   98087288 :     }
#      72                 :            : 
#      73                 :            :     const unsigned char* end() const
#      74                 :    1893903 :     {
#      75                 :    1893903 :         return &m_data[WIDTH];
#      76                 :    1893903 :     }
#      77                 :            : 
#      78                 :            :     unsigned int size() const
#      79                 :   20295832 :     {
#      80                 :   20295832 :         return sizeof(m_data);
#      81                 :   20295832 :     }
#      82                 :            : 
#      83                 :            :     uint64_t GetUint64(int pos) const
#      84                 :  662071294 :     {
#      85                 :  662071294 :         const uint8_t* ptr = m_data + pos * 8;
#      86                 :  662071294 :         return ((uint64_t)ptr[0]) | \
#      87                 :  662071294 :                ((uint64_t)ptr[1]) << 8 | \
#      88                 :  662071294 :                ((uint64_t)ptr[2]) << 16 | \
#      89                 :  662071294 :                ((uint64_t)ptr[3]) << 24 | \
#      90                 :  662071294 :                ((uint64_t)ptr[4]) << 32 | \
#      91                 :  662071294 :                ((uint64_t)ptr[5]) << 40 | \
#      92                 :  662071294 :                ((uint64_t)ptr[6]) << 48 | \
#      93                 :  662071294 :                ((uint64_t)ptr[7]) << 56;
#      94                 :  662071294 :     }
#      95                 :            : 
#      96                 :            :     template<typename Stream>
#      97                 :            :     void Serialize(Stream& s) const
#      98                 :  357657538 :     {
#      99                 :  357657538 :         s.write((char*)m_data, sizeof(m_data));
#     100                 :  357657538 :     }
#     101                 :            : 
#     102                 :            :     template<typename Stream>
#     103                 :            :     void Unserialize(Stream& s)
#     104                 :    1311974 :     {
#     105                 :    1311974 :         s.read((char*)m_data, sizeof(m_data));
#     106                 :    1311974 :     }
#     107                 :            : };
#     108                 :            : 
#     109                 :            : /** 160-bit opaque blob.
#     110                 :            :  * @note This type is called uint160 for historical reasons only. It is an opaque
#     111                 :            :  * blob of 160 bits and has no integer operations.
#     112                 :            :  */
#     113                 :            : class uint160 : public base_blob<160> {
#     114                 :            : public:
#     115                 :   13041348 :     constexpr uint160() {}
#     116                 :    3216283 :     explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
#     117                 :            : };
#     118                 :            : 
#     119                 :            : /** 256-bit opaque blob.
#     120                 :            :  * @note This type is called uint256 for historical reasons only. It is an
#     121                 :            :  * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
#     122                 :            :  * those are required.
#     123                 :            :  */
#     124                 :            : class uint256 : public base_blob<256> {
#     125                 :            : public:
#     126                 : 1041983611 :     constexpr uint256() {}
#     127                 :          0 :     constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
#     128                 :      17292 :     explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
#     129                 :            :     static const uint256 ZERO;
#     130                 :            :     static const uint256 ONE;
#     131                 :            : };
#     132                 :            : 
#     133                 :            : /* uint256 from const char *.
#     134                 :            :  * This is a separate function because the constructor uint256(const char*) can result
#     135                 :            :  * in dangerously catching uint256(0).
#     136                 :            :  */
#     137                 :            : inline uint256 uint256S(const char *str)
#     138                 :      89566 : {
#     139                 :      89566 :     uint256 rv;
#     140                 :      89566 :     rv.SetHex(str);
#     141                 :      89566 :     return rv;
#     142                 :      89566 : }
#     143                 :            : /* uint256 from std::string.
#     144                 :            :  * This is a separate function because the constructor uint256(const std::string &str) can result
#     145                 :            :  * in dangerously catching uint256(0) via std::string(const char*).
#     146                 :            :  */
#     147                 :            : inline uint256 uint256S(const std::string& str)
#     148                 :      22012 : {
#     149                 :      22012 :     uint256 rv;
#     150                 :      22012 :     rv.SetHex(str);
#     151                 :      22012 :     return rv;
#     152                 :      22012 : }
#     153                 :            : 
#     154                 :            : #endif // BITCOIN_UINT256_H

Generated by: LCOV version 1.14