LCOV - code coverage report
Current view: top level - src - uint256.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 53 53
Test Date: 2024-08-28 04:44:32 Functions: 75.0 % 20 15
Branches: 21.0 % 3807 800

             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                 :             : #ifndef BITCOIN_UINT256_H
       7                 :             : #define BITCOIN_UINT256_H
       8                 :             : 
       9                 :             : #include <crypto/common.h>
      10                 :             : #include <span.h>
      11                 :             : #include <util/strencodings.h>
      12                 :             : #include <util/string.h>
      13                 :             : 
      14                 :             : #include <algorithm>
      15                 :             : #include <array>
      16                 :             : #include <cassert>
      17                 :             : #include <cstdint>
      18                 :             : #include <cstring>
      19                 :             : #include <optional>
      20                 :             : #include <string>
      21                 :             : #include <string_view>
      22                 :             : 
      23                 :             : /** Template base class for fixed-sized opaque blobs. */
      24                 :             : template<unsigned int BITS>
      25                 :             : class base_blob
      26                 :             : {
      27                 :             : protected:
      28                 :             :     static constexpr int WIDTH = BITS / 8;
      29                 :             :     static_assert(BITS % 8 == 0, "base_blob currently only supports whole bytes.");
      30                 :             :     std::array<uint8_t, WIDTH> m_data;
      31                 :             :     static_assert(WIDTH == sizeof(m_data), "Sanity check");
      32                 :             : 
      33                 :             : public:
      34                 :             :     /* construct 0 value by default */
      35                 :   192298952 :     constexpr base_blob() : m_data() {}
      36                 :             : 
      37                 :             :     /* constructor for constants between 1 and 255 */
      38                 :          89 :     constexpr explicit base_blob(uint8_t v) : m_data{v} {}
      39                 :             : 
      40         [ -  + ]:       13513 :     constexpr explicit base_blob(Span<const unsigned char> vch)
      41                 :             :     {
      42         [ -  + ]:       13513 :         assert(vch.size() == WIDTH);
      43                 :       13513 :         std::copy(vch.begin(), vch.end(), m_data.begin());
      44                 :       13513 :     }
      45                 :             : 
      46                 :             :     consteval explicit base_blob(std::string_view hex_str);
      47                 :             : 
      48                 :      481766 :     constexpr bool IsNull() const
      49                 :             :     {
      50                 :      481766 :         return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
      51                 :             :             return val == 0;
      52                 :      481766 :         });
      53                 :             :     }
      54                 :             : 
      55                 :      136029 :     constexpr void SetNull()
      56                 :             :     {
      57 [ +  + ][ +  -  :      288586 :         std::fill(m_data.begin(), m_data.end(), 0);
          +  -  +  -  +  
                      - ]
      58                 :             :     }
      59                 :             : 
      60                 :             :     /** Lexicographic ordering
      61                 :             :      * @note Does NOT match the ordering on the corresponding \ref
      62                 :             :      *       base_uint::CompareTo, which starts comparing from the end.
      63                 :             :      */
      64   [ +  +  #  #  :   167254630 :     constexpr int Compare(const base_blob& other) const { return std::memcmp(m_data.data(), other.m_data.data(), WIDTH); }
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ -  -  +  
          -  +  -  +  -  
             -  -  -  + ]
           [ -  +  -  + ]
           [ +  +  +  +  
           +  + ][ -  -  
          -  -  -  -  +  
          -  +  +  -  -  
          -  -  -  -  -  
           -  -  - ][ +  
          -  +  +  +  -  
          +  -  +  -  -  
          +  +  +  +  -  
             +  +  #  # ]
           [ -  +  +  -  
          +  -  +  -  -  
          -  -  -  +  +  
          -  -  -  -  +  
          +  -  +  +  -  
             -  -  -  - ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ -  -  -  -  
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
          -  -  -  -  -  
          +  +  -  +  -  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  +  
          +  -  -  +  +  
          +  +  +  -  -  
          -  -  -  +  +  
          +  +  +  +  +  
             -  -  -  -  
           - ][ +  -  +  
          -  +  -  +  -  
          -  -  +  +  +  
           + ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ -  -  -  
          -  +  +  +  +  
          -  +  +  -  +  
          -  +  -  -  -  
          -  -  +  +  -  
          -  +  +  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
          -  -  -  +  +  
             -  -  -  -  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           - ][ +  +  +  
          +  +  +  +  +  
          #  #  #  #  #  
                      # ]
      65                 :             : 
      66   [ +  +  +  +  :    25095642 :     friend constexpr bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
          +  +  +  +  +  
          +  +  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  +  -  +  
          +  +  -  +  -  
          -  -  +  -  +  
          +  +  -  +  -  
          +  -  +  +  +  
          -  +  -  -  -  
              - ][ +  + ]
           [ +  +  +  +  
             +  -  +  - ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
           [ +  +  +  + ]
           [ -  -  -  -  
          -  -  +  +  -  
          +  +  +  +  +  
          +  +  +  +  #  
           # ][ -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           -  + ][ +  -  
             +  +  +  + ]
           [ -  +  +  -  
          -  +  +  -  +  
          +  -  -  +  -  
           -  + ][ -  -  
          -  -  -  -  +  
           +  +  + ][ +  
          -  -  -  +  -  
          +  -  +  -  +  
          -  +  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
      67   [ +  +  +  +  :       53625 :     friend constexpr bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
          +  +  #  #  #  
                      # ]
           [ +  +  #  # ]
           [ -  +  -  -  
          -  -  +  +  +  
           + ][ +  +  +  
             +  #  #  #  
           # ][ +  +  -  
             +  +  +  -  
           - ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
      68   [ -  +  +  -  :    33572106 :     friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
          -  -  -  -  -  
          -  -  -  -  +  
          -  -  -  -  +  
          -  -  -  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ -  
          +  -  -  -  -  
          +  +  +  +  +  
          -  +  -  -  -  
          -  -  +  +  +  
           +  -  - ][ -  
          -  -  -  +  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  -  -  -  
          -  -  -  +  -  
          -  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  -  
             -  -  -  - ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ +  
             -  +  +  -  
           + ][ +  +  +  
          +  +  +  +  +  
          +  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  +  -  
          -  -  -  -  +  
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
          -  +  +  -  -  
          -  -  +  +  -  
          +  -  -  -  +  
          -  +  -  -  -  
             -  -  -  -  
           - ][ +  +  +  
          +  +  -  +  +  
          -  +  +  +  +  
          -  +  -  -  -  
          -  -  +  +  +  
          -  +  -  +  -  
          -  -  -  -  +  
          +  -  +  +  -  
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ -  -  -  
          -  -  -  +  -  
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  +  -  -  -  
          -  -  -  -  +  
          +  +  +  -  +  
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  +  -  -  
          -  -  -  -  -  
          -  -  -  +  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  -  -  +  
          -  -  +  -  -  
             -  +  -  -  
           - ][ -  +  -  
          -  -  -  +  +  
          +  -  +  -  +  
          -  -  -  -  -  
          +  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  +  -  +  -  
          +  -  -  -  -  
          -  +  +  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  +  +  +  +  
          -  -  -  -  -  
          +  +  -  -  -  
          -  -  -  -  -  
             -  -  -  - ]
           [ -  -  -  +  
          -  -  -  -  +  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  +  
          -  +  -  -  -  
          -  -  +  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  -  -  -  -  
          -  -  -  +  +  
          -  -  +  -  -  
          -  -  -  -  -  
           - ][ -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  -  -  -  -  
          +  +  -  +  -  
          +  +  -  +  -  
          +  -  -  -  -  
          -  -  -  -  -  
             -  -  +  + ]
           [ +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  -  
          -  -  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  -  +  -  
          +  -  +  -  +  
          +  -  -  -  -  
          -  -  -  -  -  
          -  +  -  -  +  
          +  +  +  +  +  
           -  +  + ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ +  
          +  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
             -  -  -  - ]
           [ +  +  +  -  
          +  -  +  -  -  
          -  -  -  +  +  
          +  +  +  -  +  
          -  +  -  -  -  
          -  -  +  +  -  
          +  +  -  +  -  
          +  -  -  -  -  
          -  +  +  +  +  
          +  -  +  +  +  
          -  -  +  +  -  
          +  +  -  -  +  
           + ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ +  -  +  
          -  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ +  
          +  +  +  +  +  
          +  -  +  +  -  
             +  +  +  -  
           + ][ -  +  -  
          -  -  -  +  -  
          +  -  +  -  +  
          -  -  -  -  -  
          +  +  +  +  +  
          +  +  +  +  -  
          -  +  +  +  #  
             #  #  #  #  
           # ][ -  -  -  
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  +  +  
          -  -  -  -  -  
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
          -  -  -  -  +  
          +  -  +  -  -  
             -  -  +  + ]
           [ -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
             +  +  -  - ]
           [ +  -  -  +  
          -  -  -  -  -  
          -  -  -  -  +  
          +  +  +  -  +  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  +  
          -  -  -  -  -  
          -  +  +  -  -  
          -  -  +  +  -  
          -  -  -  -  -  
          +  -  +  -  +  
             +  -  -  -  
           - ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
      69                 :             : 
      70                 :             :     /** @name Hex representation
      71                 :             :      *
      72                 :             :      * The reverse-byte hex representation is a convenient way to view the blob
      73                 :             :      * as a number, because it is consistent with the way the base_uint class
      74                 :             :      * converts blobs to numbers.
      75                 :             :      *
      76                 :             :      * @note base_uint treats the blob as an array of bytes with the numerically
      77                 :             :      * least significant byte first and the most significant byte last. Because
      78                 :             :      * numbers are typically written with the most significant digit first and
      79                 :             :      * the least significant digit last, the reverse hex display of the blob
      80                 :             :      * corresponds to the same numeric value that base_uint interprets from the
      81                 :             :      * blob.
      82                 :             :      * @{*/
      83                 :             :     std::string GetHex() const;
      84                 :             :     /** Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
      85                 :             :      *
      86                 :             :      * - Hex numbers that don't specify enough bytes to fill the internal array
      87                 :             :      *   will be treated as setting the beginning of it, which corresponds to
      88                 :             :      *   the least significant bytes when converted to base_uint.
      89                 :             :      *
      90                 :             :      * - Hex numbers specifying too many bytes will have the numerically most
      91                 :             :      *   significant bytes (the beginning of the string) narrowed away.
      92                 :             :      *
      93                 :             :      * - An odd count of hex digits will result in the high bits of the leftmost
      94                 :             :      *   byte being zero.
      95                 :             :      *   "0x123" => {0x23, 0x1, 0x0, ..., 0x0}
      96                 :             :      */
      97                 :             :     void SetHexDeprecated(std::string_view str);
      98                 :             :     std::string ToString() const;
      99                 :             :     /**@}*/
     100                 :             : 
     101 [ +  - ][ -  -  :     6336229 :     constexpr const unsigned char* data() const { return m_data.data(); }
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
             -  +  -  -  
                      - ]
           [ +  -  +  - ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
           - ][ +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     102         [ +  - ]:     4088680 :     constexpr unsigned char* data() { return m_data.data(); }
           [ +  -  +  - ]
           [ +  -  +  -  
           +  - ][ +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     103                 :             : 
     104 [ +  + ][ +  -  :     7958632 :     constexpr unsigned char* begin() { return m_data.data(); }
          +  +  +  -  +  
                      - ]
           [ +  +  +  + ]
           [ +  -  +  +  
                   +  - ]
     105   [ +  -  +  - ]:       11886 :     constexpr unsigned char* end() { return m_data.data() + WIDTH; }
           [ +  -  +  -  
             +  -  +  - ]
                 [ +  - ]
     106                 :             : 
     107 [ +  + ][ +  -  :     4040044 :     constexpr const unsigned char* begin() const { return m_data.data(); }
          +  -  +  -  +  
                -  +  - ]
     108   [ +  -  +  -  :      425328 :     constexpr const unsigned char* end() const { return m_data.data() + WIDTH; }
          +  -  +  -  +  
           - ][ +  -  #  
          #  #  #  #  #  
                   #  # ]
     109                 :             : 
     110                 :             :     static constexpr unsigned int size() { return WIDTH; }
     111                 :             : 
     112         [ -  + ]:   121383668 :     constexpr uint64_t GetUint64(int pos) const { return ReadLE64(m_data.data() + pos * 8); }
     113                 :             : 
     114                 :             :     template<typename Stream>
     115                 :    26626562 :     void Serialize(Stream& s) const
     116                 :             :     {
     117                 :    26626562 :         s << Span(m_data);
     118                 :             :     }
     119                 :             : 
     120                 :             :     template<typename Stream>
     121                 :       85943 :     void Unserialize(Stream& s)
     122                 :             :     {
     123                 :       85943 :         s.read(MakeWritableByteSpan(m_data));
     124                 :       85934 :     }
     125                 :             : };
     126                 :             : 
     127                 :             : template <unsigned int BITS>
     128                 :             : consteval base_blob<BITS>::base_blob(std::string_view hex_str)
     129                 :             : {
     130                 :             :     // Non-lookup table version of HexDigit().
     131                 :             :     auto from_hex = [](const char c) -> int8_t {
     132                 :             :         if (c >= '0' && c <= '9') return c - '0';
     133                 :             :         if (c >= 'a' && c <= 'f') return c - 'a' + 0xA;
     134                 :             :         if (c >= 'A' && c <= 'F') return c - 'A' + 0xA;
     135                 :             : 
     136                 :             :         assert(false); // Reached if ctor is called with an invalid hex digit.
     137                 :             :     };
     138                 :             : 
     139                 :             :     assert(hex_str.length() == m_data.size() * 2); // 2 hex digits per byte.
     140                 :             :     auto str_it = hex_str.rbegin();
     141                 :             :     for (auto& elem : m_data) {
     142                 :             :         auto lo = from_hex(*(str_it++));
     143                 :             :         elem = (from_hex(*(str_it++)) << 4) | lo;
     144                 :             :     }
     145                 :             : }
     146                 :             : 
     147                 :             : namespace detail {
     148                 :             : /**
     149                 :             :  * Writes the hex string (in reverse byte order) into a new uintN_t object
     150                 :             :  * and only returns a value iff all of the checks pass:
     151                 :             :  *   - Input length is uintN_t::size()*2
     152                 :             :  *   - All characters are hex
     153                 :             :  */
     154                 :             : template <class uintN_t>
     155         [ +  + ]:        5262 : std::optional<uintN_t> FromHex(std::string_view str)
     156                 :             : {
     157   [ +  +  +  + ]:        5262 :     if (uintN_t::size() * 2 != str.size() || !IsHex(str)) return std::nullopt;
     158                 :        4913 :     uintN_t rv;
     159                 :        4913 :     rv.SetHexDeprecated(str);
     160                 :        4913 :     return rv;
     161                 :             : }
     162                 :             : /**
     163                 :             :  * @brief Like FromHex(std::string_view str), but allows an "0x" prefix
     164                 :             :  *        and pads the input with leading zeroes if it is shorter than
     165                 :             :  *        the expected length of uintN_t::size()*2.
     166                 :             :  *
     167                 :             :  *        Designed to be used when dealing with user input.
     168                 :             :  */
     169                 :             : template <class uintN_t>
     170                 :          34 : std::optional<uintN_t> FromUserHex(std::string_view input)
     171                 :             : {
     172                 :          34 :     input = util::RemovePrefixView(input, "0x");
     173         [ +  + ]:          34 :     constexpr auto expected_size{uintN_t::size() * 2};
     174         [ +  + ]:          34 :     if (input.size() < expected_size) {
     175                 :          25 :         auto padded = std::string(expected_size, '0');
     176         [ +  - ]:          25 :         std::copy(input.begin(), input.end(), padded.begin() + expected_size - input.size());
     177         [ +  - ]:          25 :         return FromHex<uintN_t>(padded);
     178                 :          25 :     }
     179                 :           9 :     return FromHex<uintN_t>(input);
     180                 :             : }
     181                 :             : } // namespace detail
     182                 :             : 
     183                 :             : /** 160-bit opaque blob.
     184                 :             :  * @note This type is called uint160 for historical reasons only. It is an opaque
     185                 :             :  * blob of 160 bits and has no integer operations.
     186                 :             :  */
     187                 :             : class uint160 : public base_blob<160> {
     188                 :             : public:
     189   [ +  -  +  -  :          87 :     static std::optional<uint160> FromHex(std::string_view str) { return detail::FromHex<uint160>(str); }
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     190   [ +  +  +  +  :      209140 :     constexpr uint160() = default;
           +  +  + ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
           +  - ][ +  + ]
     191                 :       11400 :     constexpr explicit uint160(Span<const unsigned char> vch) : base_blob<160>(vch) {}
     192                 :             : };
     193                 :             : 
     194                 :             : /** 256-bit opaque blob.
     195                 :             :  * @note This type is called uint256 for historical reasons only. It is an
     196                 :             :  * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
     197                 :             :  * those are required.
     198                 :             :  */
     199                 :             : class uint256 : public base_blob<256> {
     200                 :             : public:
     201   [ #  #  #  #  :        5141 :     static std::optional<uint256> FromHex(std::string_view str) { return detail::FromHex<uint256>(str); }
          #  #  #  #  #  
           #  #  # ][ +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ +  -  +  -  
           +  - ][ #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     202   [ +  -  +  - ]:          34 :     static std::optional<uint256> FromUserHex(std::string_view str) { return detail::FromUserHex<uint256>(str); }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     203         [ +  + ]:   191755317 :     constexpr uint256() = default;
           [ +  -  +  - ]
           [ +  +  +  +  
           +  + ][ +  +  
          +  -  +  +  +  
           - ][ +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
                +  +  - ]
     204                 :             :     consteval explicit uint256(std::string_view hex_str) : base_blob<256>(hex_str) {}
     205 [ +  - ][ +  -  :          64 :     constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
             +  -  +  - ]
     206                 :        2113 :     constexpr explicit uint256(Span<const unsigned char> vch) : base_blob<256>(vch) {}
     207                 :             :     static const uint256 ZERO;
     208                 :             :     static const uint256 ONE;
     209                 :             : };
     210                 :             : 
     211                 :             : /* uint256 from std::string_view, containing byte-reversed hex encoding.
     212                 :             :  * DEPRECATED. Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
     213                 :             :  */
     214                 :        1814 : inline uint256 uint256S(std::string_view str)
     215                 :             : {
     216                 :        1814 :     uint256 rv;
     217   [ +  -  +  -  :        1814 :     rv.SetHexDeprecated(str);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
           -  +  - ][ +  
          -  +  -  +  -  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ #  #  #  
                #  #  # ]
     218   [ +  -  +  -  :        1814 :     return rv;
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ +  -  -  +  
          -  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
                   #  # ]
     219                 :             : }
     220                 :             : 
     221                 :             : #endif // BITCOIN_UINT256_H
        

Generated by: LCOV version 2.0-1