LCOV - code coverage report
Current view: top level - src - uint256.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 34 43 79.1 %
Date: 2023-09-26 12:08:55 Functions: 27 67 40.3 %

          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_UINT256_H
       7             : #define BITCOIN_UINT256_H
       8             : 
       9             : #include <crypto/common.h>
      10             : #include <span.h>
      11             : 
      12             : #include <algorithm>
      13             : #include <array>
      14             : #include <cassert>
      15             : #include <cstring>
      16             : #include <stdint.h>
      17             : #include <string>
      18             : 
      19             : /** Template base class for fixed-sized opaque blobs. */
      20             : template<unsigned int BITS>
      21             : class base_blob
      22             : {
      23             : protected:
      24             :     static constexpr int WIDTH = BITS / 8;
      25             :     static_assert(BITS % 8 == 0, "base_blob currently only supports whole bytes.");
      26             :     std::array<uint8_t, WIDTH> m_data;
      27             :     static_assert(WIDTH == sizeof(m_data), "Sanity check");
      28             : 
      29             : public:
      30             :     /* construct 0 value by default */
      31    74405527 :     constexpr base_blob() : m_data() {}
      32             : 
      33             :     /* constructor for constants between 1 and 255 */
      34           0 :     constexpr explicit base_blob(uint8_t v) : m_data{v} {}
      35             : 
      36         176 :     constexpr explicit base_blob(Span<const unsigned char> vch)
      37             :     {
      38         176 :         assert(vch.size() == WIDTH);
      39         176 :         std::copy(vch.begin(), vch.end(), m_data.begin());
      40         176 :     }
      41             : 
      42      430440 :     constexpr bool IsNull() const
      43             :     {
      44     2802600 :         return std::all_of(m_data.begin(), m_data.end(), [](uint8_t val) {
      45     2372160 :             return val == 0;
      46             :         });
      47             :     }
      48             : 
      49       98542 :     constexpr void SetNull()
      50             :     {
      51       98542 :         std::fill(m_data.begin(), m_data.end(), 0);
      52       98542 :     }
      53             : 
      54    53687567 :     constexpr int Compare(const base_blob& other) const { return std::memcmp(m_data.data(), other.m_data.data(), WIDTH); }
      55             : 
      56     1210438 :     friend constexpr bool operator==(const base_blob& a, const base_blob& b) { return a.Compare(b) == 0; }
      57         602 :     friend constexpr bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
      58      169108 :     friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
      59             : 
      60             :     std::string GetHex() const;
      61             :     void SetHex(const char* psz);
      62             :     void SetHex(const std::string& str);
      63             :     std::string ToString() const;
      64             : 
      65           1 :     constexpr const unsigned char* data() const { return m_data.data(); }
      66      374828 :     constexpr unsigned char* data() { return m_data.data(); }
      67             : 
      68      322317 :     constexpr unsigned char* begin() { return m_data.data(); }
      69           0 :     constexpr unsigned char* end() { return m_data.data() + WIDTH; }
      70             : 
      71      229310 :     constexpr const unsigned char* begin() const { return m_data.data(); }
      72          16 :     constexpr const unsigned char* end() const { return m_data.data() + WIDTH; }
      73             : 
      74      374829 :     static constexpr unsigned int size() { return WIDTH; }
      75             : 
      76    11636920 :     constexpr uint64_t GetUint64(int pos) const { return ReadLE64(m_data.data() + pos * 8); }
      77             : 
      78             :     template<typename Stream>
      79     2640542 :     void Serialize(Stream& s) const
      80             :     {
      81     2640542 :         s << Span(m_data);
      82     2640542 :     }
      83             : 
      84             :     template<typename Stream>
      85       24799 :     void Unserialize(Stream& s)
      86             :     {
      87       24799 :         s.read(MakeWritableByteSpan(m_data));
      88       24799 :     }
      89             : };
      90             : 
      91             : /** 160-bit opaque blob.
      92             :  * @note This type is called uint160 for historical reasons only. It is an opaque
      93             :  * blob of 160 bits and has no integer operations.
      94             :  */
      95             : class uint160 : public base_blob<160> {
      96             : public:
      97           0 :     constexpr uint160() = default;
      98           0 :     constexpr explicit uint160(Span<const unsigned char> vch) : base_blob<160>(vch) {}
      99             : };
     100             : 
     101             : /** 256-bit opaque blob.
     102             :  * @note This type is called uint256 for historical reasons only. It is an
     103             :  * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
     104             :  * those are required.
     105             :  */
     106             : class uint256 : public base_blob<256> {
     107             : public:
     108    74405508 :     constexpr uint256() = default;
     109           0 :     constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
     110         176 :     constexpr explicit uint256(Span<const unsigned char> vch) : base_blob<256>(vch) {}
     111             :     static const uint256 ZERO;
     112             :     static const uint256 ONE;
     113             : };
     114             : 
     115             : /* uint256 from const char *.
     116             :  * This is a separate function because the constructor uint256(const char*) can result
     117             :  * in dangerously catching uint256(0).
     118             :  */
     119          46 : inline uint256 uint256S(const char *str)
     120             : {
     121          46 :     uint256 rv;
     122          46 :     rv.SetHex(str);
     123          46 :     return rv;
     124             : }
     125             : /* uint256 from std::string.
     126             :  * This is a separate function because the constructor uint256(const std::string &str) can result
     127             :  * in dangerously catching uint256(0) via std::string(const char*).
     128             :  */
     129           0 : inline uint256 uint256S(const std::string& str)
     130             : {
     131           0 :     uint256 rv;
     132           0 :     rv.SetHex(str);
     133           0 :     return rv;
     134             : }
     135             : 
     136             : #endif // BITCOIN_UINT256_H

Generated by: LCOV version 1.14