diff --git a/src/arith_uint256.cpp b/src/arith_uint256.cpp --- a/src/arith_uint256.cpp +++ b/src/arith_uint256.cpp @@ -63,16 +63,16 @@ template base_uint &base_uint::operator*=(const base_uint &b) { - base_uint a = *this; - *this = 0; + base_uint a; for (int j = 0; j < WIDTH; j++) { uint64_t carry = 0; for (int i = 0; i + j < WIDTH; i++) { - uint64_t n = carry + pn[i + j] + (uint64_t)a.pn[j] * b.pn[i]; - pn[i + j] = n & 0xffffffff; + uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i]; + a.pn[i + j] = n & 0xffffffff; carry = n >> 32; } } + *this = a; return *this; } 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 @@ -280,4 +280,16 @@ BOOST_CHECK(R2L.GetHex() == UintToArith256(R2L).GetHex()); } +BOOST_AUTO_TEST_CASE(operator_with_self) { + arith_uint256 v = UintToArith256(uint256S("02")); + v *= v; + BOOST_CHECK(v == UintToArith256(uint256S("04"))); + v /= v; + BOOST_CHECK(v == UintToArith256(uint256S("01"))); + v += v; + BOOST_CHECK(v == UintToArith256(uint256S("02"))); + v -= v; + BOOST_CHECK(v == UintToArith256(uint256S("0"))); +} + BOOST_AUTO_TEST_SUITE_END()