Skip to content

Instantly share code, notes, and snippets.

@itsN1X
Created April 12, 2018 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsN1X/6e619934c2cb76110bd570ad5af9de46 to your computer and use it in GitHub Desktop.
Save itsN1X/6e619934c2cb76110bd570ad5af9de46 to your computer and use it in GitHub Desktop.
[ N1X ] !# safe <= sm.sol
pragma solidity ^0.4.8;
/**
* Math operations used by ConsenSys, with safety // sanity checks incorporated!
* Proformed by itsN1X@n1x.site, with ++ <3 .
*/
library SafeMath {
function mul(uint a, uint b) internal returns (uint) {
uint c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function div(uint a, uint b) internal returns (uint) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
function sub(uint a, uint b) internal returns (uint) {
assert(b <= a);
return a - b;
}
function add(uint a, uint b) internal returns (uint) {
uint c = a + b;
assert(c >= a);
return c;
}
function max64(uint64 a, uint64 b) internal constant returns (uint64) {
return a >= b ? a : b;
}
function min64(uint64 a, uint64 b) internal constant returns (uint64) {
return a < b ? a : b;
}
function max256(uint256 a, uint256 b) internal constant returns (uint256) {
return a >= b ? a : b;
}
function min256(uint256 a, uint256 b) internal constant returns (uint256) {
return a < b ? a : b;
}
function assert(bool assertion) internal {
if (!assertion) {
throw;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment