Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix signed safemath #1

Open
wants to merge 3 commits into
base: feature/705_add_safe_math_int_ops
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions contracts/math/SafeMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pragma solidity ^0.4.24;
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
int256 constant INT256_MIN = int256((uint256(1) << 255));

/**
* @dev Multiplies two unsigned integers, throws on overflow.
Expand All @@ -26,16 +27,15 @@ library SafeMath {
/**
* @dev Multiplies two signed integers, throws on overflow.
*/
function mul(int256 a, int256 b) internal pure returns (int256) {
function mul(int256 a, int256 b) internal pure returns (int256 c) {
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
int256 c = a * b;
assert(c / a == b);
return c;
c = a * b;
assert((a != -1 || b != INT256_MIN) && c / a == b);
}

/**
Expand All @@ -52,9 +52,8 @@ library SafeMath {
* @dev Integer division of two signed integers, truncating the quotient.
*/
function div(int256 a, int256 b) internal pure returns (int256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
// assert(b != 0); // Solidity automatically throws when dividing by 0
// Overflow only happens when the smallest negative int is multiplied by -1.
int256 INT256_MIN = int256((uint256(1) << 255));
assert(a != INT256_MIN || b != -1);
return a / b;
}
Expand All @@ -70,10 +69,9 @@ library SafeMath {
/**
* @dev Subtracts two signed integers, throws on overflow.
*/
function sub(int256 a, int256 b) internal pure returns (int256) {
int256 c = a - b;
function sub(int256 a, int256 b) internal pure returns (int256 c) {
c = a - b;
assert((b >= 0 && c <= a) || (b < 0 && c > a));
return c;
}

/**
Expand All @@ -88,9 +86,8 @@ library SafeMath {
/**
* @dev Adds two signed integers, throws on overflow.
*/
function add(int256 a, int256 b) internal pure returns (int256) {
int256 c = a + b;
function add(int256 a, int256 b) internal pure returns (int256 c) {
c = a + b;
assert((b >= 0 && c >= a) || (b < 0 && c < a));
return c;
}
}
1 change: 1 addition & 0 deletions test/math/SafeMath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ contract('SafeMath', () => {
const b = new BigNumber(-1);

await assertJump(this.safeMath.mulInts(a, b));
await assertJump(this.safeMath.mulInts(b, a));
});
});

Expand Down