LCOV - code coverage report
Current view: top level - src - arith_uint256.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 145 145 100.0 %
Date: 2021-06-29 14:35:33 Functions: 45 45 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: 29 30 96.7 %

           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                 :            : #ifndef BITCOIN_ARITH_UINT256_H
#       7                 :            : #define BITCOIN_ARITH_UINT256_H
#       8                 :            : 
#       9                 :            : #include <cstring>
#      10                 :            : #include <limits>
#      11                 :            : #include <stdexcept>
#      12                 :            : #include <stdint.h>
#      13                 :            : #include <string>
#      14                 :            : 
#      15                 :            : class uint256;
#      16                 :            : 
#      17                 :            : class uint_error : public std::runtime_error {
#      18                 :            : public:
#      19                 :          4 :     explicit uint_error(const std::string& str) : std::runtime_error(str) {}
#      20                 :            : };
#      21                 :            : 
#      22                 :            : /** Template base class for unsigned big integers. */
#      23                 :            : template<unsigned int BITS>
#      24                 :            : class base_uint
#      25                 :            : {
#      26                 :            : protected:
#      27                 :            :     static constexpr int WIDTH = BITS / 32;
#      28                 :            :     uint32_t pn[WIDTH];
#      29                 :            : public:
#      30                 :            : 
#      31                 :            :     base_uint()
#      32                 :   11457377 :     {
#      33                 :   11457377 :         static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
#      34                 :            : 
#      35         [ +  + ]:  103116393 :         for (int i = 0; i < WIDTH; i++)
#      36                 :   91659016 :             pn[i] = 0;
#      37                 :   11457377 :     }
#      38                 :            : 
#      39                 :            :     base_uint(const base_uint& b)
#      40                 :    5181020 :     {
#      41                 :    5181020 :         static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
#      42                 :            : 
#      43         [ +  + ]:   46629180 :         for (int i = 0; i < WIDTH; i++)
#      44                 :   41448160 :             pn[i] = b.pn[i];
#      45                 :    5181020 :     }
#      46                 :            : 
#      47                 :            :     base_uint& operator=(const base_uint& b)
#      48                 :    1131362 :     {
#      49         [ +  + ]:   10182258 :         for (int i = 0; i < WIDTH; i++)
#      50                 :    9050896 :             pn[i] = b.pn[i];
#      51                 :    1131362 :         return *this;
#      52                 :    1131362 :     }
#      53                 :            : 
#      54                 :            :     base_uint(uint64_t b)
#      55                 :    1938267 :     {
#      56                 :    1938267 :         static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
#      57                 :            : 
#      58                 :    1938267 :         pn[0] = (unsigned int)b;
#      59                 :    1938267 :         pn[1] = (unsigned int)(b >> 32);
#      60         [ +  + ]:   13567869 :         for (int i = 2; i < WIDTH; i++)
#      61                 :   11629602 :             pn[i] = 0;
#      62                 :    1938267 :     }
#      63                 :            : 
#      64                 :            :     explicit base_uint(const std::string& str);
#      65                 :            : 
#      66                 :            :     const base_uint operator~() const
#      67                 :     170300 :     {
#      68                 :     170300 :         base_uint ret;
#      69         [ +  + ]:    1532700 :         for (int i = 0; i < WIDTH; i++)
#      70                 :    1362400 :             ret.pn[i] = ~pn[i];
#      71                 :     170300 :         return ret;
#      72                 :     170300 :     }
#      73                 :            : 
#      74                 :            :     const base_uint operator-() const
#      75                 :     198883 :     {
#      76                 :     198883 :         base_uint ret;
#      77         [ +  + ]:    1789947 :         for (int i = 0; i < WIDTH; i++)
#      78                 :    1591064 :             ret.pn[i] = ~pn[i];
#      79                 :     198883 :         ++ret;
#      80                 :     198883 :         return ret;
#      81                 :     198883 :     }
#      82                 :            : 
#      83                 :            :     double getdouble() const;
#      84                 :            : 
#      85                 :            :     base_uint& operator=(uint64_t b)
#      86                 :     172964 :     {
#      87                 :     172964 :         pn[0] = (unsigned int)b;
#      88                 :     172964 :         pn[1] = (unsigned int)(b >> 32);
#      89         [ +  + ]:    1210748 :         for (int i = 2; i < WIDTH; i++)
#      90                 :    1037784 :             pn[i] = 0;
#      91                 :     172964 :         return *this;
#      92                 :     172964 :     }
#      93                 :            : 
#      94                 :            :     base_uint& operator^=(const base_uint& b)
#      95                 :       1068 :     {
#      96         [ +  + ]:       9612 :         for (int i = 0; i < WIDTH; i++)
#      97                 :       8544 :             pn[i] ^= b.pn[i];
#      98                 :       1068 :         return *this;
#      99                 :       1068 :     }
#     100                 :            : 
#     101                 :            :     base_uint& operator&=(const base_uint& b)
#     102                 :         36 :     {
#     103         [ +  + ]:        324 :         for (int i = 0; i < WIDTH; i++)
#     104                 :        288 :             pn[i] &= b.pn[i];
#     105                 :         36 :         return *this;
#     106                 :         36 :     }
#     107                 :            : 
#     108                 :            :     base_uint& operator|=(const base_uint& b)
#     109                 :        548 :     {
#     110         [ +  + ]:       4932 :         for (int i = 0; i < WIDTH; i++)
#     111                 :       4384 :             pn[i] |= b.pn[i];
#     112                 :        548 :         return *this;
#     113                 :        548 :     }
#     114                 :            : 
#     115                 :            :     base_uint& operator^=(uint64_t b)
#     116                 :          4 :     {
#     117                 :          4 :         pn[0] ^= (unsigned int)b;
#     118                 :          4 :         pn[1] ^= (unsigned int)(b >> 32);
#     119                 :          4 :         return *this;
#     120                 :          4 :     }
#     121                 :            : 
#     122                 :            :     base_uint& operator|=(uint64_t b)
#     123                 :          4 :     {
#     124                 :          4 :         pn[0] |= (unsigned int)b;
#     125                 :          4 :         pn[1] |= (unsigned int)(b >> 32);
#     126                 :          4 :         return *this;
#     127                 :          4 :     }
#     128                 :            : 
#     129                 :            :     base_uint& operator<<=(unsigned int shift);
#     130                 :            :     base_uint& operator>>=(unsigned int shift);
#     131                 :            : 
#     132                 :            :     base_uint& operator+=(const base_uint& b)
#     133                 :     808309 :     {
#     134                 :     808309 :         uint64_t carry = 0;
#     135         [ +  + ]:    7274781 :         for (int i = 0; i < WIDTH; i++)
#     136                 :    6466472 :         {
#     137                 :    6466472 :             uint64_t n = carry + pn[i] + b.pn[i];
#     138                 :    6466472 :             pn[i] = n & 0xffffffff;
#     139                 :    6466472 :             carry = n >> 32;
#     140                 :    6466472 :         }
#     141                 :     808309 :         return *this;
#     142                 :     808309 :     }
#     143                 :            : 
#     144                 :            :     base_uint& operator-=(const base_uint& b)
#     145                 :     197845 :     {
#     146                 :     197845 :         *this += -b;
#     147                 :     197845 :         return *this;
#     148                 :     197845 :     }
#     149                 :            : 
#     150                 :            :     base_uint& operator+=(uint64_t b64)
#     151                 :        512 :     {
#     152                 :        512 :         base_uint b;
#     153                 :        512 :         b = b64;
#     154                 :        512 :         *this += b;
#     155                 :        512 :         return *this;
#     156                 :        512 :     }
#     157                 :            : 
#     158                 :            :     base_uint& operator-=(uint64_t b64)
#     159                 :          2 :     {
#     160                 :          2 :         base_uint b;
#     161                 :          2 :         b = b64;
#     162                 :          2 :         *this += -b;
#     163                 :          2 :         return *this;
#     164                 :          2 :     }
#     165                 :            : 
#     166                 :            :     base_uint& operator*=(uint32_t b32);
#     167                 :            :     base_uint& operator*=(const base_uint& b);
#     168                 :            :     base_uint& operator/=(const base_uint& b);
#     169                 :            : 
#     170                 :            :     base_uint& operator++()
#     171                 :     199395 :     {
#     172                 :            :         // prefix operator
#     173                 :     199395 :         int i = 0;
#     174 [ +  + ][ +  + ]:     212896 :         while (i < WIDTH && ++pn[i] == 0)
#     175                 :      13501 :             i++;
#     176                 :     199395 :         return *this;
#     177                 :     199395 :     }
#     178                 :            : 
#     179                 :            :     const base_uint operator++(int)
#     180                 :        510 :     {
#     181                 :            :         // postfix operator
#     182                 :        510 :         const base_uint ret = *this;
#     183                 :        510 :         ++(*this);
#     184                 :        510 :         return ret;
#     185                 :        510 :     }
#     186                 :            : 
#     187                 :            :     base_uint& operator--()
#     188                 :       1022 :     {
#     189                 :            :         // prefix operator
#     190                 :       1022 :         int i = 0;
#     191 [ +  - ][ +  + ]:       4606 :         while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
#     192                 :       3584 :             i++;
#     193                 :       1022 :         return *this;
#     194                 :       1022 :     }
#     195                 :            : 
#     196                 :            :     const base_uint operator--(int)
#     197                 :        510 :     {
#     198                 :            :         // postfix operator
#     199                 :        510 :         const base_uint ret = *this;
#     200                 :        510 :         --(*this);
#     201                 :        510 :         return ret;
#     202                 :        510 :     }
#     203                 :            : 
#     204                 :            :     int CompareTo(const base_uint& b) const;
#     205                 :            :     bool EqualTo(uint64_t b) const;
#     206                 :            : 
#     207                 :     609434 :     friend inline const base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
#     208                 :       3247 :     friend inline const base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
#     209                 :       2234 :     friend inline const base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
#     210                 :     172434 :     friend inline const base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
#     211                 :         26 :     friend inline const base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
#     212                 :         26 :     friend inline const base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
#     213                 :       1058 :     friend inline const base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
#     214                 :     175392 :     friend inline const base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
#     215                 :     108968 :     friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
#     216                 :      13331 :     friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
#     217                 :      15040 :     friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
#     218                 :       1548 :     friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
#     219                 :  331298428 :     friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
#     220                 :  284200507 :     friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
#     221                 :  146776558 :     friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
#     222                 :      29068 :     friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
#     223                 :     962188 :     friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
#     224                 :        520 :     friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
#     225                 :            : 
#     226                 :            :     std::string GetHex() const;
#     227                 :            :     void SetHex(const char* psz);
#     228                 :            :     void SetHex(const std::string& str);
#     229                 :            :     std::string ToString() const;
#     230                 :            : 
#     231                 :            :     unsigned int size() const
#     232                 :          8 :     {
#     233                 :          8 :         return sizeof(pn);
#     234                 :          8 :     }
#     235                 :            : 
#     236                 :            :     /**
#     237                 :            :      * Returns the position of the highest bit set plus one, or zero if the
#     238                 :            :      * value is zero.
#     239                 :            :      */
#     240                 :            :     unsigned int bits() const;
#     241                 :            : 
#     242                 :            :     uint64_t GetLow64() const
#     243                 :     471910 :     {
#     244                 :     471910 :         static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
#     245                 :     471910 :         return pn[0] | (uint64_t)pn[1] << 32;
#     246                 :     471910 :     }
#     247                 :            : };
#     248                 :            : 
#     249                 :            : /** 256-bit unsigned big integer. */
#     250                 :            : class arith_uint256 : public base_uint<256> {
#     251                 :            : public:
#     252                 :   11085444 :     arith_uint256() {}
#     253                 :     601920 :     arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
#     254                 :    1466642 :     arith_uint256(uint64_t b) : base_uint<256>(b) {}
#     255                 :         38 :     explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
#     256                 :            : 
#     257                 :            :     /**
#     258                 :            :      * The "compact" format is a representation of a whole
#     259                 :            :      * number N using an unsigned 32bit number similar to a
#     260                 :            :      * floating point format.
#     261                 :            :      * The most significant 8 bits are the unsigned exponent of base 256.
#     262                 :            :      * This exponent can be thought of as "number of bytes of N".
#     263                 :            :      * The lower 23 bits are the mantissa.
#     264                 :            :      * Bit number 24 (0x800000) represents the sign of N.
#     265                 :            :      * N = (-1^sign) * mantissa * 256^(exponent-3)
#     266                 :            :      *
#     267                 :            :      * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
#     268                 :            :      * MPI uses the most significant bit of the first byte as sign.
#     269                 :            :      * Thus 0x1234560000 is compact (0x05123456)
#     270                 :            :      * and  0xc0de000000 is compact (0x0600c0de)
#     271                 :            :      *
#     272                 :            :      * Bitcoin only uses this "compact" format for encoding difficulty
#     273                 :            :      * targets, which are unsigned 256bit quantities.  Thus, all the
#     274                 :            :      * complexities of the sign bit and using base 256 are probably an
#     275                 :            :      * implementation accident.
#     276                 :            :      */
#     277                 :            :     arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
#     278                 :            :     uint32_t GetCompact(bool fNegative = false) const;
#     279                 :            : 
#     280                 :            :     friend uint256 ArithToUint256(const arith_uint256 &);
#     281                 :            :     friend arith_uint256 UintToArith256(const uint256 &);
#     282                 :            : };
#     283                 :            : 
#     284                 :            : uint256 ArithToUint256(const arith_uint256 &);
#     285                 :            : arith_uint256 UintToArith256(const uint256 &);
#     286                 :            : 
#     287                 :            : #endif // BITCOIN_ARITH_UINT256_H

Generated by: LCOV version 1.14