You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uint256.h 4.3KB

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