Skip to content

Commit

Permalink
Add SafeMath lib for uint8 (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
bingen authored and sohkai committed Jul 13, 2018
1 parent 395b325 commit 9e673e7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions contracts/lib/zeppelin/math/SafeMath8.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
pragma solidity ^0.4.11;


/**
* @title SafeMath8
* @dev Math operations for uint8 with safety checks that throw on error
*/
library SafeMath8 {
function mul(uint8 a, uint8 b) internal pure returns (uint8) {
uint8 c = a * b;
require(a == 0 || c / a == b);
return c;
}

function div(uint8 a, uint8 b) internal pure returns (uint8) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint8 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}

function sub(uint8 a, uint8 b) internal pure returns (uint8) {
require(b <= a);
return a - b;
}

function add(uint8 a, uint8 b) internal pure returns (uint8) {
uint8 c = a + b;
require(c >= a);
return c;
}
}

0 comments on commit 9e673e7

Please sign in to comment.