From 8db10138333937afb5c0f4247e26b2ef859d8f35 Mon Sep 17 00:00:00 2001 From: yellowBirdy Date: Tue, 17 Oct 2023 14:45:49 +0200 Subject: [PATCH] up to uint96 --- src/HuffCastLib.huff | 119 ++++++++++++++++++++++++- src/HuffCastLibImportoor.huff | 72 +++++++++++---- test/HuffCastLib.t.sol | 160 +++++++++++++++++++++++++++++++++- 3 files changed, 333 insertions(+), 18 deletions(-) diff --git a/src/HuffCastLib.huff b/src/HuffCastLib.huff index 8b866b3..ec648f1 100644 --- a/src/HuffCastLib.huff +++ b/src/HuffCastLib.huff @@ -1,5 +1,40 @@ +//#define constant __UINT248_MAX_PLUS_ONE = +//#define constant __UINT240_MAX_PLUS_ONE = +//#define constant __UINT232_MAX_PLUS_ONE = +//#define constant __UINT224_MAX_PLUS_ONE = +//#define constant __UINT216_MAX_PLUS_ONE = +//#define constant __UINT208_MAX_PLUS_ONE = +//#define constant __UINT200_MAX_PLUS_ONE = +//#define constant __UINT192_MAX_PLUS_ONE = +//#define constant __UINT184_MAX_PLUS_ONE = +//#define constant __UINT176_MAX_PLUS_ONE = +//#define constant __UINT168_MAX_PLUS_ONE = +//#define constant __UINT160_MAX_PLUS_ONE = +//#define constant __UINT152_MAX_PLUS_ONE = +//#define constant __UINT144_MAX_PLUS_ONE = +//#define constant __UINT136_MAX_PLUS_ONE = +//#define constant __UINT128_MAX_PLUS_ONE = +//#define constant __UINT120_MAX_PLUS_ONE = +//#define constant __UINT112_MAX_PLUS_ONE = +//#define constant __UINT104_MAX_PLUS_ONE = +//#define constant __UINT96_MAX_PLUS_ONE = +//#define constant __UINT88_MAX_PLUS_ONE = +//#define constant __UINT80_MAX_PLUS_ONE = +//#define constant __UINT72_MAX_PLUS_ONE = +//#define constant __UINT64_MAX_PLUS_ONE = +//#define constant __UINT56_MAX_PLUS_ONE = +//#define constant __UINT48_MAX_PLUS_ONE = +//#define constant __UINT40_MAX_PLUS_ONE = +//#define constant __UINT32_MAX_PLUS_ONE = + + #define error Overflow() + +//#define constant __UINT24_MAX_PLUS_ONE = 0x1000000 +//#define constant __UINT16_MAX_PLUS_ONE = 0x10000 +//#define constant __UINT8_MAX_PLUS_ONE = 0x100 + /* Methods */ #define macro REVERT_OVERFLOW() = { @@ -16,11 +51,93 @@ success: } #define macro TO_UINT16() = takes (1) returns (1) { - 0x10000 + 0x10000 + dup2 + lt + success jumpi + REVERT_OVERFLOW() + success: +} +#define macro TO_UINT24() = takes (1) returns (1) { + 0x1000000 + dup2 + lt + success jumpi + REVERT_OVERFLOW() + success: +} +#define macro TO_UINT32() = takes (1) returns (1) { + 0x100000000 + dup2 + lt + success jumpi + REVERT_OVERFLOW() + success: +} +#define macro TO_UINT40() = takes (1) returns (1) { + 0x10000000000 + dup2 + lt + success jumpi + REVERT_OVERFLOW() + success: +} +#define macro TO_UINT48() = takes (1) returns (1) { + 0x1000000000000 + dup2 + lt + success jumpi + REVERT_OVERFLOW() + success: +} +#define macro TO_UINT56() = takes (1) returns (1) { + 0x100000000000000 dup2 lt success jumpi REVERT_OVERFLOW() success: } +#define macro TO_UINT64() = takes (1) returns (1) { + 0x10000000000000000 + dup2 + lt + success jumpi + REVERT_OVERFLOW() + success: +} +#define macro TO_UINT72() = takes (1) returns (1) { + 0x1000000000000000000 + dup2 + lt + success jumpi + REVERT_OVERFLOW() + success: +} +#define macro TO_UINT80() = takes (1) returns (1) { + 0x100000000000000000000 + dup2 + lt + success jumpi + REVERT_OVERFLOW() + success: +} +#define macro TO_UINT88() = takes (1) returns (1) { + 0x10000000000000000000000 + dup2 + lt + success jumpi + REVERT_OVERFLOW() + success: +} +#define macro TO_UINT96() = takes (1) returns (1) { + 0x1000000000000000000000000 + dup2 + lt + success jumpi + REVERT_OVERFLOW() + success: +} + + diff --git a/src/HuffCastLibImportoor.huff b/src/HuffCastLibImportoor.huff index ca7c3f6..609fa72 100644 --- a/src/HuffCastLibImportoor.huff +++ b/src/HuffCastLibImportoor.huff @@ -3,24 +3,66 @@ /* Interface */ #define function toUint8(uint256) view returns (uint8) #define function toUint16(uint256) view returns (uint16) +#define function toUint24(uint256) view returns (uint24) +#define function toUint32(uint256) view returns (uint32) +#define function toUint40(uint256) view returns (uint40) +#define function toUint48(uint256) view returns (uint48) +#define function toUint56(uint256) view returns (uint56) +#define function toUint64(uint256) view returns (uint64) +#define function toUint72(uint256) view returns (uint72) +#define function toUint80(uint256) view returns (uint80) +#define function toUint88(uint256) view returns (uint88) +#define function toUint96(uint256) view returns (uint96) #define macro MAIN() = takes (0) returns (0) { - // Identify which function is being called. - 0x00 calldataload 0xE0 shr // [sel] - 0x04 calldataload // [input,sel] - dup2 __FUNC_SIG(toUint8) eq u8 jumpi // - dup2 __FUNC_SIG(toUint16) eq u16 jumpi - 0x00 0x00 revert - - u8: - TO_UINT8() - u16: - TO_UINT16() - - ret: - 0x00 mstore - 0x20 0x00 return + // Identify which function is being called. + 0x00 calldataload 0xE0 shr // [sel] + 0x04 calldataload // [input,sel] + dup2 __FUNC_SIG(toUint8) eq u8 jumpi + dup2 __FUNC_SIG(toUint16) eq u16 jumpi + dup2 __FUNC_SIG(toUint24) eq u24 jumpi + dup2 __FUNC_SIG(toUint32) eq u32 jumpi + dup2 __FUNC_SIG(toUint40) eq u40 jumpi + dup2 __FUNC_SIG(toUint48) eq u48 jumpi + dup2 __FUNC_SIG(toUint56) eq u56 jumpi + dup2 __FUNC_SIG(toUint64) eq u64 jumpi + dup2 __FUNC_SIG(toUint72) eq u72 jumpi + dup2 __FUNC_SIG(toUint80) eq u80 jumpi + dup2 __FUNC_SIG(toUint88) eq u88 jumpi + dup2 __FUNC_SIG(toUint96) eq u96 jumpi + + 0x00 0x00 revert + + u8: + TO_UINT8() + u16: + TO_UINT16() + u24: + TO_UINT24() + u32: + TO_UINT32() + u40: + TO_UINT40() + u48: + TO_UINT48() + u56: + TO_UINT56() + u64: + TO_UINT64() + u72: + TO_UINT72() + u80: + TO_UINT80() + u88: + TO_UINT88() + u96: + TO_UINT96() + + + ret: + 0x00 mstore + 0x20 0x00 return } diff --git a/test/HuffCastLib.t.sol b/test/HuffCastLib.t.sol index 47acba7..d33612c 100644 --- a/test/HuffCastLib.t.sol +++ b/test/HuffCastLib.t.sol @@ -28,22 +28,170 @@ contract HuffCastTest is Test { assertEq(casted16, value); assertLe(casted16, type(uint16).max); + } + function testHappyToUint24(uint256 value) public { + vm.assume(value <= type(uint24).max); + + uint24 casted24 = huffUser.toUint24(value); + assertEq(casted24, value); + assertLe(casted24, type(uint24).max); + } + function testHappyToUin32(uint256 value) public { + vm.assume(value <= type(uint32).max); + + uint32 casted32 = huffUser.toUint32(value); + assertEq(casted32, value); + assertLe(casted32, type(uint32).max); + + } + // write testHappyToUinX tests for uint40, uint64, uint72, uint80, uint96 + function testHappyToUint40(uint256 value) public { + vm.assume(value <= type(uint40).max); + + uint40 casted40 = huffUser.toUint40(value); + assertEq(casted40, value); + assertLe(casted40, type(uint40).max); + } + function testHappyToUint48(uint256 value) public { + vm.assume(value <= type(uint48).max); + + uint48 casted48 = huffUser.toUint48(value); + assertEq(casted48, value); + assertLe(casted48, type(uint48).max); + + } + function testHappyToUint56(uint256 value) public { + vm.assume(value <= type(uint56).max); + + uint56 casted56 = huffUser.toUint56(value); + assertEq(casted56, value); + assertLe(casted56, type(uint56).max); + + } + + function testHappyToUint64(uint256 value) public { + vm.assume(value <= type(uint64).max); + + uint64 casted64 = huffUser.toUint64(value); + assertEq(casted64, value); + assertLe(casted64, type(uint64).max); + + } + function testHappyToUint72(uint256 value) public { + vm.assume(value <= type(uint72).max); + + uint72 casted72 = huffUser.toUint72(value); + assertEq(casted72, value); + assertLe(casted72, type(uint72).max); + + } + function testHappyToUint80(uint256 value) public { + vm.assume(value <= type(uint80).max); + + uint80 casted80 = huffUser.toUint80(value); + assertEq(casted80, value); + assertLe(casted80, type(uint80).max); + + } + function testHappyToUint88(uint256 value) public { + vm.assume(value <= type(uint88).max); + + uint88 casted88 = huffUser.toUint88(value); + assertEq(casted88, value); + assertLe(casted88, type(uint88).max); + + } + function testHappyToUint96(uint256 value) public { + vm.assume(value <= type(uint96).max); + + uint96 casted96 = huffUser.toUint96(value); + assertEq(casted96, value); + assertLe(casted96, type(uint96).max); + } function testUnhappyToUint8(uint256 value) public { vm.assume(value > type(uint8).max); - vm.expectRevert(bytes4(0x35278d12)); // Overflow() selector + vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector huffUser.toUint8(value); } function testUnhappyToUint16(uint256 value) public { vm.assume(value > type(uint16).max); - vm.expectRevert(bytes4(0x35278d12)); // Overflow() selector + vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector huffUser.toUint16(value); + } + function testUnhappyToUint24(uint256 value) public { + vm.assume(value > type(uint24).max); + + vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector + huffUser.toUint24(value); + } + function testUnhappyToUint32(uint256 value) public { + + vm.assume(value > type(uint32).max); + + vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector + huffUser.toUint32(value); + } + // testUnhappyToUinX tests for uint40, uint64, uint72, uint80, uint96 + function testUnhappyToUint40(uint256 value) public { + vm.assume(value > type(uint40).max); + + vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector + huffUser.toUint40(value); + } + function testUnhappyToUint48(uint256 value) public { + vm.assume(value > type(uint48).max); + + vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector + huffUser.toUint48(value); } + function testUnhappyToUint56(uint256 value) public { + vm.assume(value > type(uint56).max); + + vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector + huffUser.toUint56(value); + } + function testUnhappyToUint64(uint256 value) public { + + vm.assume(value > type(uint64).max); + + vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector + huffUser.toUint64(value); + } + function testUnhappyToUint72(uint256 value) public { + + vm.assume(value > type(uint72).max); + + vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector + huffUser.toUint72(value); + } + function testUnhappyToUint80(uint256 value) public { + + vm.assume(value > type(uint80).max); + + vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector + huffUser.toUint80(value); + } + function testUnhappyToUint88(uint256 value) public { + + vm.assume(value > type(uint88).max); + + vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector + huffUser.toUint88(value); + } + function testUnhappyToUint96(uint256 value) public { + + vm.assume(value > type(uint96).max); + + vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector + huffUser.toUint96(value); + } + } @@ -51,8 +199,16 @@ contract HuffCastTest is Test { interface SafeCastLibImplementoor { function toUint8(uint256 value) external returns (uint8); function toUint16(uint256 value) external returns (uint16); + function toUint24(uint256 value) external returns (uint24); function toUint32(uint256 value) external returns (uint32); + function toUint40(uint256 value) external returns (uint40); + function toUint48(uint256 value) external returns (uint48); + function toUint56(uint256 value) external returns (uint56); function toUint64(uint256 value) external returns (uint64); + function toUint72(uint256 value) external returns (uint72); + function toUint80(uint256 value) external returns (uint80); + function toUint88(uint256 value) external returns (uint88); + function toUint96(uint256 value) external returns (uint96); function toUint128(uint256 value) external returns (uint128); error Overflow();