-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |