From 478f9d0248cb5fd425c68a47ed8ce939d576b41a Mon Sep 17 00:00:00 2001 From: t11s Date: Tue, 2 Aug 2022 19:17:11 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20safeCastTo24?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 13 +++++++------ src/test/SafeCastLib.t.sol | 11 +++++++++++ src/utils/SafeCastLib.sol | 6 ++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 0f195157..b890d19b 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -177,19 +177,20 @@ SSTORE2Test:testWriteReadFullBoundedRead() (gas: 53672) SSTORE2Test:testWriteReadFullStartBound() (gas: 34764) SafeCastLibTest:testFailSafeCastTo128() (gas: 321) SafeCastLibTest:testFailSafeCastTo160() (gas: 342) -SafeCastLibTest:testFailSafeCastTo192() (gas: 344) +SafeCastLibTest:testFailSafeCastTo192() (gas: 322) SafeCastLibTest:testFailSafeCastTo224() (gas: 343) SafeCastLibTest:testFailSafeCastTo248() (gas: 365) SafeCastLibTest:testFailSafeCastTo32() (gas: 364) SafeCastLibTest:testFailSafeCastTo64() (gas: 321) -SafeCastLibTest:testFailSafeCastTo8() (gas: 296) +SafeCastLibTest:testFailSafeCastTo8() (gas: 341) SafeCastLibTest:testFailSafeCastTo96() (gas: 321) -SafeCastLibTest:testSafeCastTo128() (gas: 449) +SafeCastLibTest:testSafeCastTo128() (gas: 472) SafeCastLibTest:testSafeCastTo160() (gas: 470) -SafeCastLibTest:testSafeCastTo192() (gas: 471) +SafeCastLibTest:testSafeCastTo192() (gas: 449) SafeCastLibTest:testSafeCastTo224() (gas: 491) -SafeCastLibTest:testSafeCastTo248() (gas: 427) -SafeCastLibTest:testSafeCastTo32() (gas: 471) +SafeCastLibTest:testSafeCastTo24() (gas: 470) +SafeCastLibTest:testSafeCastTo248() (gas: 450) +SafeCastLibTest:testSafeCastTo32() (gas: 449) SafeCastLibTest:testSafeCastTo64() (gas: 470) SafeCastLibTest:testSafeCastTo8() (gas: 469) SafeCastLibTest:testSafeCastTo96() (gas: 469) diff --git a/src/test/SafeCastLib.t.sol b/src/test/SafeCastLib.t.sol index 80491caa..9953100b 100644 --- a/src/test/SafeCastLib.t.sol +++ b/src/test/SafeCastLib.t.sol @@ -46,6 +46,11 @@ contract SafeCastLibTest is DSTestPlus { assertEq(SafeCastLib.safeCastTo32(2.5e7), 2.5e7); } + function testSafeCastTo24() public { + assertEq(SafeCastLib.safeCastTo24(2.5e4), 2.5e4); + assertEq(SafeCastLib.safeCastTo24(2.5e3), 2.5e3); + } + function testSafeCastTo8() public { assertEq(SafeCastLib.safeCastTo8(100), 100); assertEq(SafeCastLib.safeCastTo8(250), 250); @@ -189,6 +194,12 @@ contract SafeCastLibTest is DSTestPlus { SafeCastLib.safeCastTo32(x); } + function testFailSafeCastTo24(uint256 x) public { + x = bound(x, type(uint24).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo24(x); + } + function testFailSafeCastTo8(uint256 x) public { x = bound(x, type(uint8).max + 1, type(uint256).max); diff --git a/src/utils/SafeCastLib.sol b/src/utils/SafeCastLib.sol index bb4aa380..b29e6248 100644 --- a/src/utils/SafeCastLib.sol +++ b/src/utils/SafeCastLib.sol @@ -53,6 +53,12 @@ library SafeCastLib { y = uint32(x); } + function safeCastTo24(uint256 x) internal pure returns (uint24 y) { + require(x < 1 << 24); + + y = uint24(x); + } + function safeCastTo8(uint256 x) internal pure returns (uint8 y) { require(x < 1 << 8);