LCOV - code coverage report
Current view: top level - src - arith_uint256.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 62 115 53.9 %
Date: 2023-11-10 23:46:46 Functions: 23 43 53.5 %
Branches: 18 30 60.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :            : // Copyright (c) 2009-2022 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                 :          0 : class uint_error : public std::runtime_error {
      18                 :            : public:
      19                 :          0 :     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_assert(BITS / 32 > 0 && BITS % 32 == 0, "Template parameter BITS must be a positive multiple of 32.");
      28                 :            :     static constexpr int WIDTH = BITS / 32;
      29                 :            :     uint32_t pn[WIDTH];
      30                 :            : public:
      31                 :            : 
      32                 :       3576 :     base_uint()
      33                 :            :     {
      34         [ +  + ]:      32184 :         for (int i = 0; i < WIDTH; i++)
      35                 :      28608 :             pn[i] = 0;
      36                 :       3576 :     }
      37                 :            : 
      38                 :       5708 :     base_uint(const base_uint& b)
      39                 :            :     {
      40         [ +  + ]:      51372 :         for (int i = 0; i < WIDTH; i++)
      41                 :      45664 :             pn[i] = b.pn[i];
      42                 :       5708 :     }
      43                 :            : 
      44                 :       1042 :     base_uint& operator=(const base_uint& b)
      45                 :            :     {
      46         [ +  + ]:       9378 :         for (int i = 0; i < WIDTH; i++)
      47                 :       8336 :             pn[i] = b.pn[i];
      48                 :       1042 :         return *this;
      49                 :            :     }
      50                 :            : 
      51                 :       1195 :     base_uint(uint64_t b)
      52                 :            :     {
      53                 :       1195 :         pn[0] = (unsigned int)b;
      54                 :       1195 :         pn[1] = (unsigned int)(b >> 32);
      55         [ +  + ]:       8365 :         for (int i = 2; i < WIDTH; i++)
      56                 :       7170 :             pn[i] = 0;
      57                 :       1195 :     }
      58                 :            : 
      59                 :            :     explicit base_uint(const std::string& str);
      60                 :            : 
      61                 :        151 :     base_uint operator~() const
      62                 :            :     {
      63                 :        151 :         base_uint ret;
      64         [ +  + ]:       1359 :         for (int i = 0; i < WIDTH; i++)
      65                 :       1208 :             ret.pn[i] = ~pn[i];
      66                 :        151 :         return ret;
      67                 :            :     }
      68                 :            : 
      69                 :        151 :     base_uint operator-() const
      70                 :            :     {
      71                 :        151 :         base_uint ret;
      72         [ +  + ]:       1359 :         for (int i = 0; i < WIDTH; i++)
      73                 :       1208 :             ret.pn[i] = ~pn[i];
      74                 :        151 :         ++ret;
      75                 :        151 :         return ret;
      76                 :            :     }
      77                 :            : 
      78                 :            :     double getdouble() const;
      79                 :            : 
      80                 :        151 :     base_uint& operator=(uint64_t b)
      81                 :            :     {
      82                 :        151 :         pn[0] = (unsigned int)b;
      83                 :        151 :         pn[1] = (unsigned int)(b >> 32);
      84         [ +  + ]:       1057 :         for (int i = 2; i < WIDTH; i++)
      85                 :        906 :             pn[i] = 0;
      86                 :        151 :         return *this;
      87                 :            :     }
      88                 :            : 
      89                 :          0 :     base_uint& operator^=(const base_uint& b)
      90                 :            :     {
      91         [ #  # ]:          0 :         for (int i = 0; i < WIDTH; i++)
      92                 :          0 :             pn[i] ^= b.pn[i];
      93                 :          0 :         return *this;
      94                 :            :     }
      95                 :            : 
      96                 :          0 :     base_uint& operator&=(const base_uint& b)
      97                 :            :     {
      98         [ #  # ]:          0 :         for (int i = 0; i < WIDTH; i++)
      99                 :          0 :             pn[i] &= b.pn[i];
     100                 :          0 :         return *this;
     101                 :            :     }
     102                 :            : 
     103                 :          0 :     base_uint& operator|=(const base_uint& b)
     104                 :            :     {
     105         [ #  # ]:          0 :         for (int i = 0; i < WIDTH; i++)
     106                 :          0 :             pn[i] |= b.pn[i];
     107                 :          0 :         return *this;
     108                 :            :     }
     109                 :            : 
     110                 :          0 :     base_uint& operator^=(uint64_t b)
     111                 :            :     {
     112                 :          0 :         pn[0] ^= (unsigned int)b;
     113                 :          0 :         pn[1] ^= (unsigned int)(b >> 32);
     114                 :          0 :         return *this;
     115                 :            :     }
     116                 :            : 
     117                 :          0 :     base_uint& operator|=(uint64_t b)
     118                 :            :     {
     119                 :          0 :         pn[0] |= (unsigned int)b;
     120                 :          0 :         pn[1] |= (unsigned int)(b >> 32);
     121                 :          0 :         return *this;
     122                 :            :     }
     123                 :            : 
     124                 :            :     base_uint& operator<<=(unsigned int shift);
     125                 :            :     base_uint& operator>>=(unsigned int shift);
     126                 :            : 
     127                 :        604 :     base_uint& operator+=(const base_uint& b)
     128                 :            :     {
     129                 :        604 :         uint64_t carry = 0;
     130         [ +  + ]:       5436 :         for (int i = 0; i < WIDTH; i++)
     131                 :            :         {
     132                 :       4832 :             uint64_t n = carry + pn[i] + b.pn[i];
     133                 :       4832 :             pn[i] = n & 0xffffffff;
     134                 :       4832 :             carry = n >> 32;
     135                 :       4832 :         }
     136                 :        604 :         return *this;
     137                 :            :     }
     138                 :            : 
     139                 :        151 :     base_uint& operator-=(const base_uint& b)
     140                 :            :     {
     141                 :        151 :         *this += -b;
     142                 :        151 :         return *this;
     143                 :            :     }
     144                 :            : 
     145                 :          0 :     base_uint& operator+=(uint64_t b64)
     146                 :            :     {
     147                 :          0 :         base_uint b;
     148                 :          0 :         b = b64;
     149                 :          0 :         *this += b;
     150                 :          0 :         return *this;
     151                 :            :     }
     152                 :            : 
     153                 :          0 :     base_uint& operator-=(uint64_t b64)
     154                 :            :     {
     155                 :          0 :         base_uint b;
     156                 :          0 :         b = b64;
     157                 :          0 :         *this += -b;
     158                 :          0 :         return *this;
     159                 :            :     }
     160                 :            : 
     161                 :            :     base_uint& operator*=(uint32_t b32);
     162                 :            :     base_uint& operator*=(const base_uint& b);
     163                 :            :     base_uint& operator/=(const base_uint& b);
     164                 :            : 
     165                 :        151 :     base_uint& operator++()
     166                 :            :     {
     167                 :            :         // prefix operator
     168                 :        151 :         int i = 0;
     169 [ -  + ][ -  + ]:        151 :         while (i < WIDTH && ++pn[i] == 0)
     170                 :          0 :             i++;
     171                 :        151 :         return *this;
     172                 :            :     }
     173                 :            : 
     174                 :          0 :     base_uint operator++(int)
     175                 :            :     {
     176                 :            :         // postfix operator
     177                 :          0 :         const base_uint ret = *this;
     178                 :          0 :         ++(*this);
     179                 :          0 :         return ret;
     180                 :            :     }
     181                 :            : 
     182                 :          0 :     base_uint& operator--()
     183                 :            :     {
     184                 :            :         // prefix operator
     185                 :          0 :         int i = 0;
     186 [ #  # ][ #  # ]:          0 :         while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
     187                 :          0 :             i++;
     188                 :          0 :         return *this;
     189                 :            :     }
     190                 :            : 
     191                 :          0 :     base_uint operator--(int)
     192                 :            :     {
     193                 :            :         // postfix operator
     194                 :          0 :         const base_uint ret = *this;
     195                 :          0 :         --(*this);
     196                 :          0 :         return ret;
     197                 :            :     }
     198                 :            : 
     199                 :            :     int CompareTo(const base_uint& b) const;
     200                 :            :     bool EqualTo(uint64_t b) const;
     201                 :            : 
     202                 :        453 :     friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
     203                 :          0 :     friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
     204                 :          0 :     friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
     205                 :        151 :     friend inline base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
     206                 :            :     friend inline base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
     207                 :            :     friend inline base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
     208                 :            :     friend inline base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
     209                 :        600 :     friend inline base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
     210                 :            :     friend inline base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
     211                 :          0 :     friend inline base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
     212                 :            :     friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
     213                 :            :     friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
     214                 :     105959 :     friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
     215                 :      69913 :     friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
     216                 :      34427 :     friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
     217                 :          0 :     friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
     218                 :        891 :     friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
     219                 :            :     friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
     220                 :            : 
     221                 :            :     std::string GetHex() const;
     222                 :            :     void SetHex(const char* psz);
     223                 :            :     void SetHex(const std::string& str);
     224                 :            :     std::string ToString() const;
     225                 :            : 
     226                 :          0 :     unsigned int size() const
     227                 :            :     {
     228                 :          0 :         return sizeof(pn);
     229                 :            :     }
     230                 :            : 
     231                 :            :     /**
     232                 :            :      * Returns the position of the highest bit set plus one, or zero if the
     233                 :            :      * value is zero.
     234                 :            :      */
     235                 :            :     unsigned int bits() const;
     236                 :            : 
     237                 :        600 :     uint64_t GetLow64() const
     238                 :            :     {
     239                 :            :         static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
     240                 :        600 :         return pn[0] | (uint64_t)pn[1] << 32;
     241                 :            :     }
     242                 :            : };
     243                 :            : 
     244                 :            : /** 256-bit unsigned big integer. */
     245                 :          0 : class arith_uint256 : public base_uint<256> {
     246                 :            : public:
     247                 :       3274 :     arith_uint256() {}
     248                 :        902 :     arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
     249                 :        893 :     arith_uint256(uint64_t b) : base_uint<256>(b) {}
     250                 :            :     explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
     251                 :            : 
     252                 :            :     /**
     253                 :            :      * The "compact" format is a representation of a whole
     254                 :            :      * number N using an unsigned 32bit number similar to a
     255                 :            :      * floating point format.
     256                 :            :      * The most significant 8 bits are the unsigned exponent of base 256.
     257                 :            :      * This exponent can be thought of as "number of bytes of N".
     258                 :            :      * The lower 23 bits are the mantissa.
     259                 :            :      * Bit number 24 (0x800000) represents the sign of N.
     260                 :            :      * N = (-1^sign) * mantissa * 256^(exponent-3)
     261                 :            :      *
     262                 :            :      * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
     263                 :            :      * MPI uses the most significant bit of the first byte as sign.
     264                 :            :      * Thus 0x1234560000 is compact (0x05123456)
     265                 :            :      * and  0xc0de000000 is compact (0x0600c0de)
     266                 :            :      *
     267                 :            :      * Bitcoin only uses this "compact" format for encoding difficulty
     268                 :            :      * targets, which are unsigned 256bit quantities.  Thus, all the
     269                 :            :      * complexities of the sign bit and using base 256 are probably an
     270                 :            :      * implementation accident.
     271                 :            :      */
     272                 :            :     arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
     273                 :            :     uint32_t GetCompact(bool fNegative = false) const;
     274                 :            : 
     275                 :            :     friend uint256 ArithToUint256(const arith_uint256 &);
     276                 :            :     friend arith_uint256 UintToArith256(const uint256 &);
     277                 :            : };
     278                 :            : 
     279                 :            : uint256 ArithToUint256(const arith_uint256 &);
     280                 :            : arith_uint256 UintToArith256(const uint256 &);
     281                 :            : 
     282                 :            : extern template class base_uint<256>;
     283                 :            : 
     284                 :            : #endif // BITCOIN_ARITH_UINT256_H

Generated by: LCOV version 1.14