Skip to content

Commit

Permalink
toInt8
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowBirdy committed Dec 29, 2023
1 parent b477346 commit 9e0f2ff
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 14 deletions.
13 changes: 11 additions & 2 deletions src/HuffCastLib.huff
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,14 @@
success jumpi
REVERT_OVERFLOW()
success:
}

}


#define macro TO_INT8() = takes (1) returns (1) {
0x80
dup2
lt
success jumpi
REVERT_OVERFLOW()
success:
}
60 changes: 49 additions & 11 deletions src/HuffCastLibImportoor.huff
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#define function toUint232(uint256) view returns (uint232)
#define function toUint240(uint256) view returns (uint240)
#define function toUint248(uint256) view returns (uint248)
#define function toInt8(uint256) view returns (int8)




Expand Down Expand Up @@ -70,73 +72,109 @@
dup2 __FUNC_SIG(toUint232) eq u232 jumpi
dup2 __FUNC_SIG(toUint240) eq u240 jumpi
dup2 __FUNC_SIG(toUint248) eq u248 jumpi
dup2 __FUNC_SIG(toInt8) eq i8 jumpi



0x00 0x00 revert

u8:
TO_UINT8()
TO_UINT8()
ret jump
u16:
TO_UINT16()
TO_UINT16()
ret jump
u24:
TO_UINT24()
TO_UINT24()
ret jump
u32:
TO_UINT32()
TO_UINT32()
ret jump
u40:
TO_UINT40()
TO_UINT40()
ret jump
u48:
TO_UINT48()
TO_UINT48()
ret jump
u56:
TO_UINT56()
TO_UINT56()
ret jump
u64:
TO_UINT64()
TO_UINT64()
ret jump
u72:
TO_UINT72()
ret jump
u80:
TO_UINT80()
ret jump
u88:
TO_UINT88()
ret jump
u96:
TO_UINT96()
ret jump
u104:
TO_UINT104()
ret jump
u112:
TO_UINT112()
ret jump
u120:
TO_UINT120()
ret jump
u128:
TO_UINT128()
ret jump
u136:
TO_UINT136()
ret jump
u144:
TO_UINT144()
ret jump
u152:
TO_UINT152()
ret jump
u160:
TO_UINT160()
ret jump
u168:
TO_UINT168()
ret jump
u176:
TO_UINT176()
ret jump
u184:
TO_UINT184()
ret jump
u192:
TO_UINT192()
ret jump
u200:
TO_UINT200()
ret jump
u208:
TO_UINT208()
TO_UINT208()
ret jump
u216:
TO_UINT216()
TO_UINT216()
ret jump
u224:
TO_UINT224()
ret jump
u232:
TO_UINT232()
ret jump
u240:
TO_UINT240()
ret jump
u248:
TO_UINT248()

ret jump

i8:
TO_INT8()
ret jump
ret:
0x00 mstore
0x20 0x00 return
Expand Down
35 changes: 35 additions & 0 deletions src/IHuffCastLibImportoor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
interface IHuffCastLibImportoor {
error Overflow();
function toInt8(uint256) external view returns (int8);
function toUint104(uint256) external view returns (uint104);
function toUint112(uint256) external view returns (uint112);
function toUint120(uint256) external view returns (uint120);
function toUint128(uint256) external view returns (uint128);
function toUint136(uint256) external view returns (uint136);
function toUint144(uint256) external view returns (uint144);
function toUint152(uint256) external view returns (uint152);
function toUint16(uint256) external view returns (uint16);
function toUint160(uint256) external view returns (uint160);
function toUint168(uint256) external view returns (uint168);
function toUint176(uint256) external view returns (uint176);
function toUint184(uint256) external view returns (uint184);
function toUint192(uint256) external view returns (uint192);
function toUint200(uint256) external view returns (uint200);
function toUint208(uint256) external view returns (uint208);
function toUint216(uint256) external view returns (uint216);
function toUint224(uint256) external view returns (uint224);
function toUint232(uint256) external view returns (uint232);
function toUint24(uint256) external view returns (uint24);
function toUint240(uint256) external view returns (uint240);
function toUint248(uint256) external view returns (uint248);
function toUint32(uint256) external view returns (uint32);
function toUint40(uint256) external view returns (uint40);
function toUint48(uint256) external view returns (uint48);
function toUint56(uint256) external view returns (uint56);
function toUint64(uint256) external view returns (uint64);
function toUint72(uint256) external view returns (uint72);
function toUint8(uint256) external view returns (uint8);
function toUint80(uint256) external view returns (uint80);
function toUint88(uint256) external view returns (uint88);
function toUint96(uint256) external view returns (uint96);
}
16 changes: 15 additions & 1 deletion test/HuffCastLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,20 @@ contract HuffCastTest is Test {
assertLe(casted248, type(uint248).max);

}
function testHappyToInt8(uint256 value) public {
vm.assume(value <= type(uint8).max/2);

int8 casted8 = huffUser.toInt8(value);
assertEq(abi.decode(abi.encode(casted8),(uint256)), value);
assertLe(casted8, type(int8).max);
}
// unhappy paths
function testUnhappyToInt8(uint256 value) public {
vm.assume(value > type(uint8).max/2);

vm.expectRevert(SafeCastLibImplementoor.Overflow.selector); // Overflow() selector
huffUser.toInt8(value);
}

function testUnhappyToUint8(uint256 value) public {
vm.assume(value > type(uint8).max);
Expand Down Expand Up @@ -482,7 +495,6 @@ contract HuffCastTest is Test {
huffUser.toUint248(value);
}



}

Expand Down Expand Up @@ -520,5 +532,7 @@ interface SafeCastLibImplementoor {
function toUint240(uint256 value) external returns (uint240);
function toUint248(uint256 value) external returns (uint248);

function toInt8(uint256) external view returns (int8);

error Overflow();
}

0 comments on commit 9e0f2ff

Please sign in to comment.