LCOV - code coverage report
Current view: top level - src - arith_uint256.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 128 128
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 15 15
Branches: 92.0 % 100 92

             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                 :             : #include <arith_uint256.h>
       7                 :             : 
       8                 :             : #include <uint256.h>
       9                 :             : #include <crypto/common.h>
      10                 :             : 
      11                 :             : #include <cassert>
      12                 :             : 
      13                 :             : template <unsigned int BITS>
      14                 :      309344 : base_uint<BITS>& base_uint<BITS>::operator<<=(unsigned int shift)
      15                 :             : {
      16                 :     2784096 :     base_uint<BITS> a(*this);
      17         [ +  + ]:     2784096 :     for (int i = 0; i < WIDTH; i++)
      18                 :     2474752 :         pn[i] = 0;
      19                 :      309344 :     int k = shift / 32;
      20                 :      309344 :     shift = shift % 32;
      21         [ +  + ]:     2784096 :     for (int i = 0; i < WIDTH; i++) {
      22   [ +  +  +  + ]:     2474752 :         if (i + k + 1 < WIDTH && shift != 0)
      23                 :      587635 :             pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
      24         [ +  + ]:     2474752 :         if (i + k < WIDTH)
      25                 :     1048716 :             pn[i + k] |= (a.pn[i] << shift);
      26                 :             :     }
      27                 :      309344 :     return *this;
      28                 :             : }
      29                 :             : 
      30                 :             : template <unsigned int BITS>
      31                 :      229415 : base_uint<BITS>& base_uint<BITS>::operator>>=(unsigned int shift)
      32                 :             : {
      33                 :     2064735 :     base_uint<BITS> a(*this);
      34         [ +  + ]:     2064735 :     for (int i = 0; i < WIDTH; i++)
      35                 :     1835320 :         pn[i] = 0;
      36                 :      229415 :     int k = shift / 32;
      37                 :      229415 :     shift = shift % 32;
      38         [ +  + ]:     2064735 :     for (int i = 0; i < WIDTH; i++) {
      39   [ +  +  +  + ]:     1835320 :         if (i - k - 1 >= 0 && shift != 0)
      40                 :     1378898 :             pn[i - k - 1] |= (a.pn[i] << (32 - shift));
      41         [ +  + ]:     1835320 :         if (i - k >= 0)
      42                 :     1609074 :             pn[i - k] |= (a.pn[i] >> shift);
      43                 :             :     }
      44                 :      229415 :     return *this;
      45                 :             : }
      46                 :             : 
      47                 :             : template <unsigned int BITS>
      48                 :          55 : base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32)
      49                 :             : {
      50                 :          55 :     uint64_t carry = 0;
      51         [ +  + ]:         495 :     for (int i = 0; i < WIDTH; i++) {
      52                 :         440 :         uint64_t n = carry + (uint64_t)b32 * pn[i];
      53                 :         440 :         pn[i] = n & 0xffffffff;
      54                 :         440 :         carry = n >> 32;
      55                 :             :     }
      56                 :          55 :     return *this;
      57                 :             : }
      58                 :             : 
      59                 :             : template <unsigned int BITS>
      60                 :        1013 : base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b)
      61                 :             : {
      62                 :        9117 :     base_uint<BITS> a;
      63         [ +  + ]:        9117 :     for (int j = 0; j < WIDTH; j++) {
      64                 :             :         uint64_t carry = 0;
      65         [ +  + ]:       44572 :         for (int i = 0; i + j < WIDTH; i++) {
      66                 :       36468 :             uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i];
      67                 :       36468 :             a.pn[i + j] = n & 0xffffffff;
      68                 :       36468 :             carry = n >> 32;
      69                 :             :         }
      70                 :             :     }
      71                 :        1013 :     *this = a;
      72                 :        1013 :     return *this;
      73                 :             : }
      74                 :             : 
      75                 :             : template <unsigned int BITS>
      76                 :       81495 : base_uint<BITS>& base_uint<BITS>::operator/=(const base_uint& b)
      77                 :             : {
      78                 :       81495 :     base_uint<BITS> div = b;     // make a copy, so we can shift.
      79                 :       81495 :     base_uint<BITS> num = *this; // make a copy, so we can subtract.
      80                 :       81495 :     *this = 0;                   // the quotient.
      81                 :       81495 :     int num_bits = num.bits();
      82                 :       81495 :     int div_bits = div.bits();
      83         [ +  + ]:       81495 :     if (div_bits == 0)
      84         [ +  - ]:           4 :         throw uint_error("Division by zero");
      85         [ +  + ]:       81493 :     if (div_bits > num_bits) // the result is certainly 0.
      86                 :             :         return *this;
      87                 :       81492 :     int shift = num_bits - div_bits;
      88                 :       81492 :     div <<= shift; // shift so that div and num align.
      89         [ +  + ]:      274755 :     while (shift >= 0) {
      90         [ +  + ]:      193263 :         if (num >= div) {
      91                 :       91479 :             num -= div;
      92                 :       91479 :             pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result.
      93                 :             :         }
      94                 :      193263 :         div >>= 1; // shift back.
      95                 :      193263 :         shift--;
      96                 :             :     }
      97                 :             :     // num now contains the remainder of the division.
      98                 :             :     return *this;
      99                 :             : }
     100                 :             : 
     101                 :             : template <unsigned int BITS>
     102                 :    38902995 : int base_uint<BITS>::CompareTo(const base_uint<BITS>& b) const
     103                 :             : {
     104         [ +  + ]:   308911498 :     for (int i = WIDTH - 1; i >= 0; i--) {
     105         [ +  + ]:   308625406 :         if (pn[i] < b.pn[i])
     106                 :             :             return -1;
     107         [ +  + ]:   283026825 :         if (pn[i] > b.pn[i])
     108                 :             :             return 1;
     109                 :             :     }
     110                 :             :     return 0;
     111                 :             : }
     112                 :             : 
     113                 :             : template <unsigned int BITS>
     114                 :      172700 : bool base_uint<BITS>::EqualTo(uint64_t b) const
     115                 :             : {
     116         [ +  + ]:      174205 :     for (int i = WIDTH - 1; i >= 2; i--) {
     117         [ +  + ]:      174129 :         if (pn[i])
     118                 :             :             return false;
     119                 :             :     }
     120         [ +  + ]:          76 :     if (pn[1] != (b >> 32))
     121                 :             :         return false;
     122         [ +  + ]:          44 :     if (pn[0] != (b & 0xfffffffful))
     123                 :          32 :         return false;
     124                 :             :     return true;
     125                 :             : }
     126                 :             : 
     127                 :             : template <unsigned int BITS>
     128                 :        8183 : double base_uint<BITS>::getdouble() const
     129                 :             : {
     130                 :        8183 :     double ret = 0.0;
     131                 :        8183 :     double fact = 1.0;
     132         [ +  + ]:       73647 :     for (int i = 0; i < WIDTH; i++) {
     133                 :       65464 :         ret += fact * pn[i];
     134                 :       65464 :         fact *= 4294967296.0;
     135                 :             :     }
     136                 :        8183 :     return ret;
     137                 :             : }
     138                 :             : 
     139                 :             : template <unsigned int BITS>
     140                 :         219 : std::string base_uint<BITS>::GetHex() const
     141                 :             : {
     142                 :         219 :     base_blob<BITS> b;
     143         [ +  + ]:        1971 :     for (int x = 0; x < this->WIDTH; ++x) {
     144                 :        1752 :         WriteLE32(b.begin() + x*4, this->pn[x]);
     145                 :             :     }
     146                 :         219 :     return b.GetHex();
     147                 :             : }
     148                 :             : 
     149                 :             : template <unsigned int BITS>
     150                 :          29 : std::string base_uint<BITS>::ToString() const
     151                 :             : {
     152                 :          29 :     return GetHex();
     153                 :             : }
     154                 :             : 
     155                 :             : template <unsigned int BITS>
     156                 :      192627 : unsigned int base_uint<BITS>::bits() const
     157                 :             : {
     158         [ +  + ]:      214357 :     for (int pos = WIDTH - 1; pos >= 0; pos--) {
     159         [ +  + ]:      214341 :         if (pn[pos]) {
     160         [ +  + ]:      353431 :             for (int nbits = 31; nbits > 0; nbits--) {
     161         [ +  + ]:      353428 :                 if (pn[pos] & 1U << nbits)
     162                 :      192608 :                     return 32 * pos + nbits + 1;
     163                 :             :             }
     164                 :           3 :             return 32 * pos + 1;
     165                 :             :         }
     166                 :             :     }
     167                 :             :     return 0;
     168                 :             : }
     169                 :             : 
     170                 :             : // Explicit instantiations for base_uint<256>
     171                 :             : template class base_uint<256>;
     172                 :             : 
     173                 :             : // This implementation directly uses shifts instead of going
     174                 :             : // through an intermediate MPI representation.
     175                 :      172490 : arith_uint256& arith_uint256::SetCompact(uint32_t nCompact, bool* pfNegative, bool* pfOverflow)
     176                 :             : {
     177                 :      172490 :     int nSize = nCompact >> 24;
     178                 :      172490 :     uint32_t nWord = nCompact & 0x007fffff;
     179         [ +  + ]:      172490 :     if (nSize <= 3) {
     180                 :          17 :         nWord >>= 8 * (3 - nSize);
     181         [ +  - ]:          34 :         *this = nWord;
     182                 :             :     } else {
     183         [ +  - ]:      344946 :         *this = nWord;
     184                 :      172473 :         *this <<= 8 * (nSize - 3);
     185                 :             :     }
     186         [ +  + ]:      172490 :     if (pfNegative)
     187   [ +  +  +  + ]:      344913 :         *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
     188         [ +  + ]:      172490 :     if (pfOverflow)
     189   [ +  +  +  + ]:      344914 :         *pfOverflow = nWord != 0 && ((nSize > 34) ||
     190         [ +  - ]:      172441 :                                      (nWord > 0xff && nSize > 33) ||
     191         [ +  - ]:      172441 :                                      (nWord > 0xffff && nSize > 32));
     192                 :      172490 :     return *this;
     193                 :             : }
     194                 :             : 
     195                 :       28637 : uint32_t arith_uint256::GetCompact(bool fNegative) const
     196                 :             : {
     197                 :       28637 :     int nSize = (bits() + 7) / 8;
     198                 :       28637 :     uint32_t nCompact = 0;
     199         [ +  + ]:       28637 :     if (nSize <= 3) {
     200                 :          20 :         nCompact = GetLow64() << 8 * (3 - nSize);
     201                 :             :     } else {
     202                 :       28617 :         arith_uint256 bn = *this >> 8 * (nSize - 3);
     203                 :       28617 :         nCompact = bn.GetLow64();
     204                 :             :     }
     205                 :             :     // The 0x00800000 bit denotes the sign.
     206                 :             :     // Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
     207         [ +  + ]:       28637 :     if (nCompact & 0x00800000) {
     208                 :         161 :         nCompact >>= 8;
     209                 :         161 :         nSize++;
     210                 :             :     }
     211         [ -  + ]:       28637 :     assert((nCompact & ~0x007fffffU) == 0);
     212         [ -  + ]:       28637 :     assert(nSize < 256);
     213                 :       28637 :     nCompact |= nSize << 24;
     214   [ +  +  -  + ]:       28637 :     nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
     215                 :       28637 :     return nCompact;
     216                 :             : }
     217                 :             : 
     218                 :      250010 : uint256 ArithToUint256(const arith_uint256 &a)
     219                 :             : {
     220                 :      250010 :     uint256 b;
     221         [ +  + ]:     2250090 :     for(int x=0; x<a.WIDTH; ++x)
     222                 :     2000080 :         WriteLE32(b.begin() + x*4, a.pn[x]);
     223                 :      250010 :     return b;
     224                 :             : }
     225                 :      368799 : arith_uint256 UintToArith256(const uint256 &a)
     226                 :             : {
     227                 :      368799 :     arith_uint256 b;
     228         [ +  + ]:     3319191 :     for(int x=0; x<b.WIDTH; ++x)
     229                 :     2950392 :         b.pn[x] = ReadLE32(a.begin() + x*4);
     230                 :      368799 :     return b;
     231                 :             : }
        

Generated by: LCOV version 2.0-1