diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp --- a/src/test/uint256_tests.cpp +++ b/src/test/uint256_tests.cpp @@ -124,24 +124,24 @@ // <= >= < > BOOST_AUTO_TEST_CASE(comparison) { uint256 LastL; - for (int i = 255; i >= 0; --i) { + for (int i = 0; i < 256; i++) { uint256 TmpL; - *(TmpL.begin() + (i >> 3)) |= 1 << (7 - (i & 7)); + *(TmpL.begin() + (i >> 3)) |= 1 << (i & 7); CheckComparison(LastL, TmpL); LastL = TmpL; } CheckComparison(ZeroL, R1L); - CheckComparison(R2L, R1L); + CheckComparison(R1L, R2L); CheckComparison(ZeroL, OneL); CheckComparison(OneL, MaxL); CheckComparison(R1L, MaxL); CheckComparison(R2L, MaxL); uint160 LastS; - for (int i = 159; i >= 0; --i) { + for (int i = 0; i < 160; i++) { uint160 TmpS; - *(TmpS.begin() + (i >> 3)) |= 1 << (7 - (i & 7)); + *(TmpS.begin() + (i >> 3)) |= 1 << (i & 7); CheckComparison(LastS, TmpS); LastS = TmpS; } diff --git a/src/uint256.h b/src/uint256.h --- a/src/uint256.h +++ b/src/uint256.h @@ -38,9 +38,18 @@ void SetNull() { memset(data, 0, sizeof(data)); } inline int Compare(const base_blob &other) const { - // This doesn't quite work as you'd expect because the comparison use - // the wrong endianess. - return memcmp(data, other.data, sizeof(data)); + for (size_t i = 0; i < sizeof(data); i++) { + uint8_t a = data[sizeof(data) - 1 - i]; + uint8_t b = other.data[sizeof(data) - 1 - i]; + if (a > b) { + return 1; + } + if (a < b) { + return -1; + } + } + + return 0; } friend inline bool operator==(const base_blob &a, const base_blob &b) {