diff --git a/contracts/libraries/AddressStringUtil.sol b/contracts/libraries/AddressStringUtil.sol index dd71ac38..245cf031 100644 --- a/contracts/libraries/AddressStringUtil.sol +++ b/contracts/libraries/AddressStringUtil.sol @@ -8,7 +8,7 @@ library AddressStringUtil { require(len % 2 == 0 && len > 0 && len <= 40, 'AddressStringUtil: INVALID_LEN'); bytes memory s = new bytes(len); - uint256 addrNum = uint256(addr); + uint256 addrNum = uint160(addr); for (uint256 i = 0; i < len / 2; i++) { // shift right and truncate all but the least significant byte to extract the byte at position 19-i uint8 b = uint8(addrNum >> (8 * (19 - i))); diff --git a/contracts/libraries/BitMath.sol b/contracts/libraries/BitMath.sol index d570c2d6..6fc6d2ff 100644 --- a/contracts/libraries/BitMath.sol +++ b/contracts/libraries/BitMath.sol @@ -45,27 +45,27 @@ library BitMath { require(x > 0, 'BitMath::leastSignificantBit: zero'); r = 255; - if (x & uint128(-1) > 0) { + if (x & type(uint128).max > 0) { r -= 128; } else { x >>= 128; } - if (x & uint64(-1) > 0) { + if (x & type(uint64).max > 0) { r -= 64; } else { x >>= 64; } - if (x & uint32(-1) > 0) { + if (x & type(uint32).max > 0) { r -= 32; } else { x >>= 32; } - if (x & uint16(-1) > 0) { + if (x & type(uint16).max > 0) { r -= 16; } else { x >>= 16; } - if (x & uint8(-1) > 0) { + if (x & type(uint8).max > 0) { r -= 8; } else { x >>= 8; diff --git a/contracts/libraries/FixedPoint.sol b/contracts/libraries/FixedPoint.sol index 27d0ed64..e800202c 100644 --- a/contracts/libraries/FixedPoint.sol +++ b/contracts/libraries/FixedPoint.sol @@ -19,9 +19,9 @@ library FixedPoint { uint256 _x; } - uint8 public constant RESOLUTION = 112; - uint256 public constant Q112 = 0x10000000000000000000000000000; // 2**112 - uint256 private constant Q224 = 0x100000000000000000000000000000000000000000000000000000000; // 2**224 + uint8 private constant RESOLUTION = 112; + uint256 private constant Q112 = 0x10000000000000000000000000000; + uint256 private constant Q224 = 0x100000000000000000000000000000000000000000000000000000000; uint256 private constant LOWER_MASK = 0xffffffffffffffffffffffffffff; // decimal of UQ*x112 (lower 112 bits) // encode a uint112 as a UQ112x112 @@ -78,13 +78,13 @@ library FixedPoint { uint224 uppero_lowers = uint224(upper_other) * lower_self; // * 2^-112 // so the bit shift does not overflow - require(upper <= uint112(-1), 'FixedPoint::muluq: upper overflow'); + require(upper <= type(uint112).max, 'FixedPoint::muluq: upper overflow'); // this cannot exceed 256 bits, all values are 224 bits uint256 sum = uint256(upper << RESOLUTION) + uppers_lowero + uppero_lowers + (lower >> RESOLUTION); // so the cast does not overflow - require(sum <= uint224(-1), 'FixedPoint::muluq: sum overflow'); + require(sum <= type(uint224).max, 'FixedPoint::muluq: sum overflow'); return uq112x112(uint224(sum)); } @@ -95,30 +95,30 @@ library FixedPoint { if (self._x == other._x) { return uq112x112(uint224(Q112)); } - if (self._x <= uint144(-1)) { + if (self._x <= type(uint144).max) { uint256 value = (uint256(self._x) << RESOLUTION) / other._x; - require(value <= uint224(-1), 'FixedPoint::divuq: overflow'); + require(value <= type(uint224).max, 'FixedPoint::divuq: overflow'); return uq112x112(uint224(value)); } uint256 result = FullMath.mulDiv(Q112, self._x, other._x); - require(result <= uint224(-1), 'FixedPoint::divuq: overflow'); + require(result <= type(uint224).max, 'FixedPoint::divuq: overflow'); return uq112x112(uint224(result)); } // returns a UQ112x112 which represents the ratio of the numerator to the denominator - // can be lossy + // lossy if either numerator or denominator is greater than 112 bits function fraction(uint256 numerator, uint256 denominator) internal pure returns (uq112x112 memory) { require(denominator > 0, 'FixedPoint::fraction: division by zero'); if (numerator == 0) return FixedPoint.uq112x112(0); - if (numerator <= uint144(-1)) { + if (numerator <= type(uint144).max) { uint256 result = (numerator << RESOLUTION) / denominator; - require(result <= uint224(-1), 'FixedPoint::fraction: overflow'); + require(result <= type(uint224).max, 'FixedPoint::fraction: overflow'); return uq112x112(uint224(result)); } else { uint256 result = FullMath.mulDiv(numerator, Q112, denominator); - require(result <= uint224(-1), 'FixedPoint::fraction: overflow'); + require(result <= type(uint224).max, 'FixedPoint::fraction: overflow'); return uq112x112(uint224(result)); } } @@ -135,7 +135,7 @@ library FixedPoint { // square root of a UQ112x112 // lossy between 0/1 and 40 bits function sqrt(uq112x112 memory self) internal pure returns (uq112x112 memory) { - if (self._x <= uint144(-1)) { + if (self._x <= type(uint144).max) { return uq112x112(uint224(Babylonian.sqrt(uint256(self._x) << 112))); } diff --git a/contracts/libraries/FullMath.sol b/contracts/libraries/FullMath.sol index f06356c7..d6b6af73 100644 --- a/contracts/libraries/FullMath.sol +++ b/contracts/libraries/FullMath.sol @@ -5,7 +5,7 @@ pragma solidity >=0.4.0; // license is CC-BY-4.0 library FullMath { function fullMul(uint256 x, uint256 y) internal pure returns (uint256 l, uint256 h) { - uint256 mm = mulmod(x, y, uint256(-1)); + uint256 mm = mulmod(x, y, type(uint256).max); l = x * y; h = mm - l; if (mm < l) h -= 1; @@ -16,10 +16,10 @@ library FullMath { uint256 h, uint256 d ) private pure returns (uint256) { - uint256 pow2 = d & -d; + uint256 pow2 = d & (~d+1); d /= pow2; l /= pow2; - l += h * ((-pow2) / pow2 + 1); + l += h * ((~pow2+1) / pow2 + 1); uint256 r = 1; r *= 2 - d * r; r *= 2 - d * r; @@ -29,6 +29,7 @@ library FullMath { r *= 2 - d * r; r *= 2 - d * r; r *= 2 - d * r; + return l * r; } @@ -48,4 +49,5 @@ library FullMath { require(h < d, 'FullMath: FULLDIV_OVERFLOW'); return fullDiv(l, h, d); } + }