From 56515380452baad9fcd32c5d4502002af0183ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Mon, 4 Jun 2018 20:04:08 -0300 Subject: [PATCH] Clarified the rationaly behind the mul branch test. (#970) --- contracts/math/SafeMath.sol | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contracts/math/SafeMath.sol b/contracts/math/SafeMath.sol index c603c7558..3b8d75905 100644 --- a/contracts/math/SafeMath.sol +++ b/contracts/math/SafeMath.sol @@ -11,9 +11,13 @@ library SafeMath { * @dev Multiplies two numbers, throws on overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256 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; } + c = a * b; assert(c / a == b); return c;