@ -4,10 +4,10 @@ pragma solidity ^0.6.0;
/**
* @ dev Wrappers over Solidity ' s uintXX casting operators with added overflow
* @ dev Wrappers over Solidity ' s uintXX/intXX casting operators with added overflow
* checks .
*
* Downcasting from uint256 in Solidity does not revert on overflow . This can
* Downcasting from uint256 / int256 in Solidity does not revert on overflow . This can
* easily result in undesired exploitation or bugs , since developers usually
* assume that overflows raise errors . ` SafeCast ` restores this intuition by
* reverting the transaction when such an operation overflows .
@ -15,8 +15,8 @@ pragma solidity ^0.6.0;
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs , so it ' s recommended to use it always.
*
* Can be combined with { SafeMath } to extend it to smaller types , by performing
* all math on ` uint256 ` and then downcasting .
* Can be combined with { SafeMath } and { SignedSafeMath } to extend it to smaller types , by performing
* all math on ` uint256 ` and ` int256 ` and then downcasting .
* /
library SafeCast {
@ -107,6 +107,86 @@ library SafeCast {
return uint256 ( value ) ;
}
/**
* @ dev Returns the downcasted int128 from int256 , reverting on
* overflow ( when the input is less than smallest int128 or
* greater than largest int128 ) .
*
* Counterpart to Solidity ' s `int128` operator.
*
* Requirements :
*
* - input must fit into 128 bits
* /
function toInt128 ( int256 value ) internal pure returns ( int128 ) {
require ( value >= - 2 ** 127 && value < 2 ** 127 , " SafeCast: value doesn \' t fit in 128 bits " ) ;
return int128 ( value ) ;
}
/**
* @ dev Returns the downcasted int64 from int256 , reverting on
* overflow ( when the input is less than smallest int64 or
* greater than largest int64 ) .
*
* Counterpart to Solidity ' s `int64` operator.
*
* Requirements :
*
* - input must fit into 64 bits
* /
function toInt64 ( int256 value ) internal pure returns ( int64 ) {
require ( value >= - 2 ** 63 && value < 2 ** 63 , " SafeCast: value doesn \' t fit in 64 bits " ) ;
return int64 ( value ) ;
}
/**
* @ dev Returns the downcasted int32 from int256 , reverting on
* overflow ( when the input is less than smallest int32 or
* greater than largest int32 ) .
*
* Counterpart to Solidity ' s `int32` operator.
*
* Requirements :
*
* - input must fit into 32 bits
* /
function toInt32 ( int256 value ) internal pure returns ( int32 ) {
require ( value >= - 2 ** 31 && value < 2 ** 31 , " SafeCast: value doesn \' t fit in 32 bits " ) ;
return int32 ( value ) ;
}
/**
* @ dev Returns the downcasted int16 from int256 , reverting on
* overflow ( when the input is less than smallest int16 or
* greater than largest int16 ) .
*
* Counterpart to Solidity ' s `int16` operator.
*
* Requirements :
*
* - input must fit into 16 bits
* /
function toInt16 ( int256 value ) internal pure returns ( int16 ) {
require ( value >= - 2 ** 15 && value < 2 ** 15 , " SafeCast: value doesn \' t fit in 16 bits " ) ;
return int16 ( value ) ;
}
/**
* @ dev Returns the downcasted int8 from int256 , reverting on
* overflow ( when the input is less than smallest int8 or
* greater than largest int8 ) .
*
* Counterpart to Solidity ' s `int8` operator.
*
* Requirements :
*
* - input must fit into 8 bits .
* /
function toInt8 ( int256 value ) internal pure returns ( int8 ) {
require ( value >= - 2 ** 7 && value < 2 ** 7 , " SafeCast: value doesn \' t fit in 8 bits " ) ;
return int8 ( value ) ;
}
/**
* @ dev Converts an unsigned uint256 into a signed int256 .
*