uint256.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 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. #ifndef BITCOIN_UINT256_H
  6. #define BITCOIN_UINT256_H
  7. #include <assert.h>
  8. #include <cstring>
  9. #include <stdexcept>
  10. #include <stdint.h>
  11. #include <string>
  12. #include <vector>
  13. /** Template base class for fixed-sized opaque blobs. */
  14. template<unsigned int BITS>
  15. class base_blob
  16. {
  17. protected:
  18. enum { WIDTH=BITS/8 };
  19. alignas(uint32_t) uint8_t data[WIDTH];
  20. public:
  21. base_blob()
  22. {
  23. memset(data, 0, sizeof(data));
  24. }
  25. explicit base_blob(const std::vector<unsigned char>& vch);
  26. bool IsNull() const
  27. {
  28. for (int i = 0; i < WIDTH; i++)
  29. if (data[i] != 0)
  30. return false;
  31. return true;
  32. }
  33. void SetNull()
  34. {
  35. memset(data, 0, sizeof(data));
  36. }
  37. friend inline bool operator==(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) == 0; }
  38. friend inline bool operator!=(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) != 0; }
  39. friend inline bool operator<(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) < 0; }
  40. std::string GetHex() const;
  41. void SetHex(const char* psz);
  42. void SetHex(const std::string& str);
  43. std::string ToString() const;
  44. unsigned char* begin()
  45. {
  46. return &data[0];
  47. }
  48. unsigned char* end()
  49. {
  50. return &data[WIDTH];
  51. }
  52. const unsigned char* begin() const
  53. {
  54. return &data[0];
  55. }
  56. const unsigned char* end() const
  57. {
  58. return &data[WIDTH];
  59. }
  60. unsigned int size() const
  61. {
  62. return sizeof(data);
  63. }
  64. unsigned int GetSerializeSize(int nType, int nVersion) const
  65. {
  66. return sizeof(data);
  67. }
  68. template<typename Stream>
  69. void Serialize(Stream& s, int nType, int nVersion) const
  70. {
  71. s.write((char*)data, sizeof(data));
  72. }
  73. template<typename Stream>
  74. void Unserialize(Stream& s, int nType, int nVersion)
  75. {
  76. s.read((char*)data, sizeof(data));
  77. }
  78. };
  79. /** 160-bit opaque blob.
  80. * @note This type is called uint160 for historical reasons only. It is an opaque
  81. * blob of 160 bits and has no integer operations.
  82. */
  83. class uint160 : public base_blob<160> {
  84. public:
  85. uint160() {}
  86. uint160(const base_blob<160>& b) : base_blob<160>(b) {}
  87. explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
  88. };
  89. /** 256-bit opaque blob.
  90. * @note This type is called uint256 for historical reasons only. It is an
  91. * opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
  92. * those are required.
  93. */
  94. class uint256 : public base_blob<256> {
  95. public:
  96. uint256() {}
  97. uint256(const base_blob<256>& b) : base_blob<256>(b) {}
  98. explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
  99. /** A cheap hash function that just returns 64 bits from the result, it can be
  100. * used when the contents are considered uniformly random. It is not appropriate
  101. * when the value can easily be influenced from outside as e.g. a network adversary could
  102. * provide values to trigger worst-case behavior.
  103. * @note The result of this function is not stable between little and big endian.
  104. */
  105. uint64_t GetCheapHash() const
  106. {
  107. uint64_t result;
  108. memcpy((void*)&result, (void*)data, 8);
  109. return result;
  110. }
  111. /** A more secure, salted hash function.
  112. * @note This hash is not stable between little and big endian.
  113. */
  114. uint64_t GetHash(const uint256& salt) const;
  115. };
  116. /* uint256 from const char *.
  117. * This is a separate function because the constructor uint256(const char*) can result
  118. * in dangerously catching uint256(0).
  119. */
  120. inline uint256 uint256S(const char *str)
  121. {
  122. uint256 rv;
  123. rv.SetHex(str);
  124. return rv;
  125. }
  126. /* uint256 from std::string.
  127. * This is a separate function because the constructor uint256(const std::string &str) can result
  128. * in dangerously catching uint256(0) via std::string(const char*).
  129. */
  130. inline uint256 uint256S(const std::string& str)
  131. {
  132. uint256 rv;
  133. rv.SetHex(str);
  134. return rv;
  135. }
  136. #endif // BITCOIN_UINT256_H