-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do over/underflow checks for addition (SafeMath)
Related to #153
- Loading branch information
Showing
7 changed files
with
98 additions
and
3 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
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
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,25 @@ | ||
use yultsur::*; | ||
|
||
/// Add two u256 numbers. Revert if result overflows. | ||
pub fn checked_add_u256() -> yul::Statement { | ||
function_definition! { | ||
function checked_add_u256(val1, val2) -> sum { | ||
// overflow, if val1 > (max_value - val2) | ||
(if (gt(val1, (sub(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, val2)))) { (revert(0, 0)) }) | ||
(sum := add(val1, val2)) | ||
} | ||
} | ||
} | ||
|
||
/// Add two i256 numbers. Revert if result over- or underflows. | ||
pub fn checked_add_i256() -> yul::Statement { | ||
function_definition! { | ||
function checked_add_i256(val1, val2) -> sum { | ||
// overflow, if val1 >= 0 and val2 > (max_value - val1) | ||
(if (and((iszero((slt(val1, 0)))), (sgt(val2, (sub(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, val1)))))) { (revert(0, 0)) }) | ||
// underflow, if val1 < 0 and val2 < (min_val - val1) | ||
(if (and((slt(val1, 0)), (slt(val2, (sub(0x8000000000000000000000000000000000000000000000000000000000000000, val1)))))) { (revert(0, 0)) }) | ||
(sum := add(val1, val2)) | ||
} | ||
} | ||
} |
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
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
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,7 @@ | ||
contract CheckedArithmetic: | ||
|
||
pub def add_u256(left: u256, right: u256) -> u256: | ||
return left + right | ||
|
||
pub def add_i256(left: i256, right: i256) -> i256: | ||
return left + right |
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,4 @@ | ||
Do over/underflow checks for additions (SafeMath). | ||
|
||
With this change all additions (e.g `x + y`) for signed and unsigned | ||
integers check for over- and underflows and revert if necessary. |