LCOV - code coverage report
Current view: top level - src - arith_uint256.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 128 128
Test Date: 2026-03-13 04:28:15 Functions: 100.0 % 21 21
Branches: 96.9 % 98 95

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present 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 <arith_uint256.h>
       7                 :             : 
       8                 :             : #include <crypto/common.h>
       9                 :             : #include <uint256.h>
      10                 :             : #include <util/overflow.h>
      11                 :             : 
      12                 :             : #include <cassert>
      13                 :             : 
      14                 :             : template <unsigned int BITS>
      15                 :     4290655 : base_uint<BITS>& base_uint<BITS>::operator<<=(unsigned int shift)
      16                 :             : {
      17                 :     4290655 :     base_uint<BITS> a(*this);
      18         [ +  + ]:    38708447 :     for (int i = 0; i < WIDTH; i++)
      19                 :    34417792 :         pn[i] = 0;
      20                 :     4290655 :     int k = shift / 32;
      21                 :     4290655 :     shift = shift % 32;
      22         [ +  + ]:    38708447 :     for (int i = 0; i < WIDTH; i++) {
      23   [ +  +  +  + ]:    34417792 :         if (i + k + 1 < WIDTH && shift != 0)
      24                 :     7363557 :             pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
      25         [ +  + ]:    34417792 :         if (i + k < WIDTH)
      26                 :    17286932 :             pn[i + k] |= (a.pn[i] << shift);
      27                 :             :     }
      28                 :     4290655 :     return *this;
      29                 :             : }
      30                 :             : 
      31                 :             : template <unsigned int BITS>
      32                 :    91648503 : base_uint<BITS>& base_uint<BITS>::operator>>=(unsigned int shift)
      33                 :             : {
      34                 :    91648503 :     base_uint<BITS> a(*this);
      35         [ +  + ]:   903899119 :     for (int i = 0; i < WIDTH; i++)
      36                 :   812250616 :         pn[i] = 0;
      37                 :    91648503 :     int k = shift / 32;
      38                 :    91648503 :     shift = shift % 32;
      39         [ +  + ]:   903899119 :     for (int i = 0; i < WIDTH; i++) {
      40   [ +  +  +  + ]:   812250616 :         if (i - k - 1 >= 0 && shift != 0)
      41                 :   713168214 :             pn[i - k - 1] |= (a.pn[i] << (32 - shift));
      42         [ +  + ]:   812250616 :         if (i - k >= 0)
      43                 :   804891704 :             pn[i - k] |= (a.pn[i] >> shift);
      44                 :             :     }
      45                 :    91648503 :     return *this;
      46                 :             : }
      47                 :             : 
      48                 :             : template <unsigned int BITS>
      49                 :      334249 : base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32)
      50                 :             : {
      51                 :      334249 :     uint64_t carry = 0;
      52         [ +  + ]:     3008241 :     for (int i = 0; i < WIDTH; i++) {
      53                 :     2673992 :         uint64_t n = carry + (uint64_t)b32 * pn[i];
      54                 :     2673992 :         pn[i] = n & 0xffffffff;
      55                 :     2673992 :         carry = n >> 32;
      56                 :             :     }
      57                 :      334249 :     return *this;
      58                 :             : }
      59                 :             : 
      60                 :             : template <unsigned int BITS>
      61                 :      303578 : base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b)
      62                 :             : {
      63                 :      303578 :     base_uint<BITS> a;
      64         [ +  + ]:     2939018 :     for (int j = 0; j < WIDTH; j++) {
      65                 :             :         uint64_t carry = 0;
      66         [ +  + ]:    34349256 :         for (int i = 0; i + j < WIDTH; i++) {
      67                 :    31713816 :             uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i];
      68                 :    31713816 :             a.pn[i + j] = n & 0xffffffff;
      69                 :    31713816 :             carry = n >> 32;
      70                 :             :         }
      71                 :             :     }
      72                 :      303578 :     *this = a;
      73                 :      303578 :     return *this;
      74                 :             : }
      75                 :             : 
      76                 :             : template <unsigned int BITS>
      77                 :     2322946 : base_uint<BITS>& base_uint<BITS>::operator/=(const base_uint& b)
      78                 :             : {
      79                 :     2322946 :     base_uint<BITS> div = b;     // make a copy, so we can shift.
      80                 :     2322946 :     base_uint<BITS> num = *this; // make a copy, so we can subtract.
      81                 :     2322946 :     *this = 0;                   // the quotient.
      82                 :     2322946 :     int num_bits = num.bits();
      83                 :     2322946 :     int div_bits = div.bits();
      84         [ +  + ]:     2322946 :     if (div_bits == 0)
      85         [ +  - ]:      277294 :         throw uint_error("Division by zero");
      86         [ +  + ]:     2184299 :     if (div_bits > num_bits) // the result is certainly 0.
      87                 :             :         return *this;
      88                 :     1963350 :     int shift = num_bits - div_bits;
      89                 :     1963350 :     div <<= shift; // shift so that div and num align.
      90         [ +  + ]:    92512807 :     while (shift >= 0) {
      91         [ +  + ]:    90549457 :         if (num >= div) {
      92                 :    30223330 :             num -= div;
      93                 :    30223330 :             pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result.
      94                 :             :         }
      95                 :    90549457 :         div >>= 1; // shift back.
      96                 :    90549457 :         shift--;
      97                 :             :     }
      98                 :             :     // num now contains the remainder of the division.
      99                 :             :     return *this;
     100                 :             : }
     101                 :             : 
     102                 :             : template <unsigned int BITS>
     103                 :   578712348 : int base_uint<BITS>::CompareTo(const base_uint<BITS>& b) const
     104                 :             : {
     105         [ +  + ]:  4176129903 :     for (int i = WIDTH - 1; i >= 0; i--) {
     106         [ +  + ]:  4168742179 :         if (pn[i] < b.pn[i])
     107                 :             :             return -1;
     108         [ +  + ]:  3796795792 :         if (pn[i] > b.pn[i])
     109                 :             :             return 1;
     110                 :             :     }
     111                 :             :     return 0;
     112                 :             : }
     113                 :             : 
     114                 :             : template <unsigned int BITS>
     115                 :     1964674 : bool base_uint<BITS>::EqualTo(uint64_t b) const
     116                 :             : {
     117         [ +  + ]:     4735177 :     for (int i = WIDTH - 1; i >= 2; i--) {
     118         [ +  + ]:     4517370 :         if (pn[i])
     119                 :             :             return false;
     120                 :             :     }
     121         [ +  + ]:      217807 :     if (pn[1] != (b >> 32))
     122                 :             :         return false;
     123         [ +  + ]:      196311 :     if (pn[0] != (b & 0xfffffffful))
     124                 :       25137 :         return false;
     125                 :             :     return true;
     126                 :             : }
     127                 :             : 
     128                 :             : template <unsigned int BITS>
     129                 :      206872 : double base_uint<BITS>::getdouble() const
     130                 :             : {
     131                 :      206872 :     double ret = 0.0;
     132                 :      206872 :     double fact = 1.0;
     133         [ +  + ]:     1861848 :     for (int i = 0; i < WIDTH; i++) {
     134                 :     1654976 :         ret += fact * pn[i];
     135                 :     1654976 :         fact *= 4294967296.0;
     136                 :             :     }
     137                 :      206872 :     return ret;
     138                 :             : }
     139                 :             : 
     140                 :             : template <unsigned int BITS>
     141                 :        4458 : std::string base_uint<BITS>::GetHex() const
     142                 :             : {
     143                 :        4458 :     base_blob<BITS> b;
     144         [ +  + ]:       40122 :     for (int x = 0; x < this->WIDTH; ++x) {
     145                 :       35664 :         WriteLE32(b.begin() + x*4, this->pn[x]);
     146                 :             :     }
     147                 :        4458 :     return b.GetHex();
     148                 :             : }
     149                 :             : 
     150                 :             : template <unsigned int BITS>
     151                 :         495 : std::string base_uint<BITS>::ToString() const
     152                 :             : {
     153                 :         495 :     return GetHex();
     154                 :             : }
     155                 :             : 
     156                 :             : template <unsigned int BITS>
     157                 :     6025312 : unsigned int base_uint<BITS>::bits() const
     158                 :             : {
     159         [ +  + ]:    16121998 :     for (int pos = WIDTH - 1; pos >= 0; pos--) {
     160         [ +  + ]:    15512968 :         if (pn[pos]) {
     161         [ +  + ]:    21650370 :             for (int nbits = 31; nbits > 0; nbits--) {
     162         [ +  + ]:    21562387 :                 if (pn[pos] & 1U << nbits)
     163                 :     5328299 :                     return 32 * pos + nbits + 1;
     164                 :             :             }
     165                 :       87983 :             return 32 * pos + 1;
     166                 :             :         }
     167                 :             :     }
     168                 :             :     return 0;
     169                 :             : }
     170                 :             : 
     171                 :             : // Explicit instantiations for base_uint<256>
     172                 :             : template class base_uint<256>;
     173                 :             : 
     174                 :             : // This implementation directly uses shifts instead of going
     175                 :             : // through an intermediate MPI representation.
     176                 :     2535944 : arith_uint256& arith_uint256::SetCompact(uint32_t nCompact, bool* pfNegative, bool* pfOverflow)
     177                 :             : {
     178                 :     2535944 :     int nSize = nCompact >> 24;
     179                 :     2535944 :     uint32_t nWord = nCompact & 0x007fffff;
     180         [ +  + ]:     2535944 :     if (nSize <= 3) {
     181                 :      209022 :         nWord >>= 8 * (3 - nSize);
     182                 :      418044 :         *this = nWord;
     183                 :             :     } else {
     184                 :     2326922 :         *this = nWord;
     185                 :     2326922 :         *this <<= 8 * (nSize - 3);
     186                 :             :     }
     187         [ +  + ]:     2535944 :     if (pfNegative)
     188   [ +  +  +  + ]:     4393871 :         *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
     189         [ +  + ]:     2535944 :     if (pfOverflow)
     190   [ +  +  +  + ]:     4228036 :         *pfOverflow = nWord != 0 && ((nSize > 34) ||
     191         [ +  + ]:     1819689 :                                      (nWord > 0xff && nSize > 33) ||
     192         [ +  + ]:     1818453 :                                      (nWord > 0xffff && nSize > 32));
     193                 :     2535944 :     return *this;
     194                 :             : }
     195                 :             : 
     196                 :     1256068 : uint32_t arith_uint256::GetCompact(bool fNegative) const
     197                 :             : {
     198         [ +  + ]:     1256068 :     int nSize = CeilDiv(bits(), 8u);
     199                 :     1256068 :     uint32_t nCompact = 0;
     200         [ +  + ]:     1256068 :     if (nSize <= 3) {
     201                 :      157022 :         nCompact = GetLow64() << 8 * (3 - nSize);
     202                 :             :     } else {
     203                 :     1099046 :         arith_uint256 bn = *this >> 8 * (nSize - 3);
     204                 :     1099046 :         nCompact = bn.GetLow64();
     205                 :             :     }
     206                 :             :     // The 0x00800000 bit denotes the sign.
     207                 :             :     // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
     208         [ +  + ]:     1256068 :     if (nCompact & 0x00800000) {
     209                 :       43773 :         nCompact >>= 8;
     210                 :       43773 :         nSize++;
     211                 :             :     }
     212         [ -  + ]:     1256068 :     assert((nCompact & ~0x007fffffU) == 0);
     213         [ -  + ]:     1256068 :     assert(nSize < 256);
     214                 :     1256068 :     nCompact |= nSize << 24;
     215   [ +  +  +  + ]:     1256068 :     nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
     216                 :     1256068 :     return nCompact;
     217                 :             : }
     218                 :             : 
     219                 :         528 : uint256 ArithToUint256(const arith_uint256 &a)
     220                 :             : {
     221                 :         528 :     uint256 b;
     222         [ +  + ]:        4752 :     for(int x=0; x<a.WIDTH; ++x)
     223                 :        4224 :         WriteLE32(b.begin() + x*4, a.pn[x]);
     224                 :         528 :     return b;
     225                 :             : }
     226                 :     1479014 : arith_uint256 UintToArith256(const uint256 &a)
     227                 :             : {
     228                 :     1479014 :     arith_uint256 b;
     229         [ +  + ]:    13311126 :     for(int x=0; x<b.WIDTH; ++x)
     230                 :    11832112 :         b.pn[x] = ReadLE32(a.begin() + x*4);
     231                 :     1479014 :     return b;
     232                 :             : }
     233                 :             : 
     234                 :             : // Explicit instantiations for base_uint<6144> (used in test/fuzz/muhash.cpp).
     235                 :             : template base_uint<6144>& base_uint<6144>::operator*=(const base_uint<6144>& b);
     236                 :             : template base_uint<6144>& base_uint<6144>::operator/=(const base_uint<6144>& b);
        

Generated by: LCOV version 2.0-1