Skip to content

Commit 5c5045e

Browse files
authored
Merge pull request #13644 from ethereum/builtin-tests
test: Move hashing algorithm tests to semanticTests
2 parents 9800e19 + 4f07be6 commit 5c5045e

File tree

8 files changed

+77
-196
lines changed

8 files changed

+77
-196
lines changed

test/libsolidity/SolidityEndToEndTest.cpp

Lines changed: 0 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -912,202 +912,6 @@ BOOST_AUTO_TEST_CASE(transfer_ether)
912912
)
913913
}
914914

915-
BOOST_AUTO_TEST_CASE(keccak256)
916-
{
917-
char const* sourceCode = R"(
918-
contract test {
919-
function a(bytes32 input) public returns (bytes32 hash) {
920-
return keccak256(abi.encodePacked(input));
921-
}
922-
}
923-
)";
924-
ALSO_VIA_YUL(
925-
DISABLE_EWASM_TESTRUN()
926-
compileAndRun(sourceCode);
927-
auto f = [&](u256 const& _x) -> u256
928-
{
929-
return util::keccak256(toBigEndian(_x));
930-
};
931-
testContractAgainstCpp("a(bytes32)", f, u256(4));
932-
testContractAgainstCpp("a(bytes32)", f, u256(5));
933-
testContractAgainstCpp("a(bytes32)", f, u256(-1));
934-
);
935-
}
936-
937-
BOOST_AUTO_TEST_CASE(sha256)
938-
{
939-
char const* sourceCode = R"(
940-
contract test {
941-
function a(bytes32 input) public returns (bytes32 sha256hash) {
942-
return sha256(abi.encodePacked(input));
943-
}
944-
}
945-
)";
946-
auto f = [&](u256 const& _x) -> bytes
947-
{
948-
if (_x == u256(4))
949-
return fromHex("e38990d0c7fc009880a9c07c23842e886c6bbdc964ce6bdd5817ad357335ee6f");
950-
if (_x == u256(5))
951-
return fromHex("96de8fc8c256fa1e1556d41af431cace7dca68707c78dd88c3acab8b17164c47");
952-
if (_x == u256(-1))
953-
return fromHex("af9613760f72635fbdb44a5a0a63c39f12af30f950a6ee5c971be188e89c4051");
954-
return fromHex("");
955-
};
956-
ALSO_VIA_YUL(
957-
DISABLE_EWASM_TESTRUN()
958-
compileAndRun(sourceCode);
959-
testContractAgainstCpp("a(bytes32)", f, u256(4));
960-
testContractAgainstCpp("a(bytes32)", f, u256(5));
961-
testContractAgainstCpp("a(bytes32)", f, u256(-1));
962-
)
963-
}
964-
965-
BOOST_AUTO_TEST_CASE(ripemd)
966-
{
967-
char const* sourceCode = R"(
968-
contract test {
969-
function a(bytes32 input) public returns (bytes32 sha256hash) {
970-
return ripemd160(abi.encodePacked(input));
971-
}
972-
}
973-
)";
974-
auto f = [&](u256 const& _x) -> bytes
975-
{
976-
if (_x == u256(4))
977-
return fromHex("1b0f3c404d12075c68c938f9f60ebea4f74941a0000000000000000000000000");
978-
if (_x == u256(5))
979-
return fromHex("ee54aa84fc32d8fed5a5fe160442ae84626829d9000000000000000000000000");
980-
if (_x == u256(-1))
981-
return fromHex("1cf4e77f5966e13e109703cd8a0df7ceda7f3dc3000000000000000000000000");
982-
return fromHex("");
983-
};
984-
ALSO_VIA_YUL(
985-
DISABLE_EWASM_TESTRUN()
986-
compileAndRun(sourceCode);
987-
testContractAgainstCpp("a(bytes32)", f, u256(4));
988-
testContractAgainstCpp("a(bytes32)", f, u256(5));
989-
testContractAgainstCpp("a(bytes32)", f, u256(-1));
990-
)
991-
}
992-
993-
BOOST_AUTO_TEST_CASE(packed_keccak256)
994-
{
995-
char const* sourceCode = R"(
996-
contract test {
997-
function a(bytes32 input) public returns (bytes32 hash) {
998-
uint24 b = 65536;
999-
uint c = 256;
1000-
return keccak256(abi.encodePacked(uint8(8), input, b, input, c));
1001-
}
1002-
}
1003-
)";
1004-
auto f = [&](u256 const& _x) -> u256
1005-
{
1006-
return util::keccak256(
1007-
toCompactBigEndian(unsigned(8)) +
1008-
toBigEndian(_x) +
1009-
toCompactBigEndian(unsigned(65536)) +
1010-
toBigEndian(_x) +
1011-
toBigEndian(u256(256))
1012-
);
1013-
};
1014-
ALSO_VIA_YUL(
1015-
DISABLE_EWASM_TESTRUN()
1016-
compileAndRun(sourceCode);
1017-
testContractAgainstCpp("a(bytes32)", f, u256(4));
1018-
testContractAgainstCpp("a(bytes32)", f, u256(5));
1019-
testContractAgainstCpp("a(bytes32)", f, u256(-1));
1020-
)
1021-
}
1022-
1023-
BOOST_AUTO_TEST_CASE(packed_keccak256_complex_types)
1024-
{
1025-
char const* sourceCode = R"(
1026-
contract test {
1027-
uint120[3] x;
1028-
function f() public returns (bytes32 hash1, bytes32 hash2, bytes32 hash3) {
1029-
uint120[] memory y = new uint120[](3);
1030-
x[0] = y[0] = uint120(type(uint).max - 1);
1031-
x[1] = y[1] = uint120(type(uint).max - 2);
1032-
x[2] = y[2] = uint120(type(uint).max - 3);
1033-
hash1 = keccak256(abi.encodePacked(x));
1034-
hash2 = keccak256(abi.encodePacked(y));
1035-
hash3 = keccak256(abi.encodePacked(this.f));
1036-
}
1037-
}
1038-
)";
1039-
ALSO_VIA_YUL(
1040-
DISABLE_EWASM_TESTRUN()
1041-
compileAndRun(sourceCode);
1042-
// Strangely, arrays are encoded with intra-element padding.
1043-
ABI_CHECK(callContractFunction("f()"), encodeArgs(
1044-
util::keccak256(encodeArgs(u256("0xfffffffffffffffffffffffffffffe"), u256("0xfffffffffffffffffffffffffffffd"), u256("0xfffffffffffffffffffffffffffffc"))),
1045-
util::keccak256(encodeArgs(u256("0xfffffffffffffffffffffffffffffe"), u256("0xfffffffffffffffffffffffffffffd"), u256("0xfffffffffffffffffffffffffffffc"))),
1046-
util::keccak256(fromHex(m_contractAddress.hex() + "26121ff0"))
1047-
));
1048-
)
1049-
}
1050-
1051-
BOOST_AUTO_TEST_CASE(packed_sha256)
1052-
{
1053-
char const* sourceCode = R"(
1054-
contract test {
1055-
function a(bytes32 input) public returns (bytes32 hash) {
1056-
uint24 b = 65536;
1057-
uint c = 256;
1058-
return sha256(abi.encodePacked(uint8(8), input, b, input, c));
1059-
}
1060-
}
1061-
)";
1062-
auto f = [&](u256 const& _x) -> bytes
1063-
{
1064-
if (_x == u256(4))
1065-
return fromHex("804e0d7003cfd70fc925dc103174d9f898ebb142ecc2a286da1abd22ac2ce3ac");
1066-
if (_x == u256(5))
1067-
return fromHex("e94921945f9068726c529a290a954f412bcac53184bb41224208a31edbf63cf0");
1068-
if (_x == u256(-1))
1069-
return fromHex("f14def4d07cd185ddd8b10a81b2238326196a38867e6e6adbcc956dc913488c7");
1070-
return fromHex("");
1071-
};
1072-
ALSO_VIA_YUL(
1073-
DISABLE_EWASM_TESTRUN()
1074-
compileAndRun(sourceCode);
1075-
testContractAgainstCpp("a(bytes32)", f, u256(4));
1076-
testContractAgainstCpp("a(bytes32)", f, u256(5));
1077-
testContractAgainstCpp("a(bytes32)", f, u256(-1));
1078-
)
1079-
}
1080-
1081-
BOOST_AUTO_TEST_CASE(packed_ripemd160)
1082-
{
1083-
char const* sourceCode = R"(
1084-
contract test {
1085-
function a(bytes32 input) public returns (bytes32 hash) {
1086-
uint24 b = 65536;
1087-
uint c = 256;
1088-
return ripemd160(abi.encodePacked(uint8(8), input, b, input, c));
1089-
}
1090-
}
1091-
)";
1092-
auto f = [&](u256 const& _x) -> bytes
1093-
{
1094-
if (_x == u256(4))
1095-
return fromHex("f93175303eba2a7b372174fc9330237f5ad202fc000000000000000000000000");
1096-
if (_x == u256(5))
1097-
return fromHex("04f4fc112e2bfbe0d38f896a46629e08e2fcfad5000000000000000000000000");
1098-
if (_x == u256(-1))
1099-
return fromHex("c0a2e4b1f3ff766a9a0089e7a410391730872495000000000000000000000000");
1100-
return fromHex("");
1101-
};
1102-
ALSO_VIA_YUL(
1103-
DISABLE_EWASM_TESTRUN()
1104-
compileAndRun(sourceCode);
1105-
testContractAgainstCpp("a(bytes32)", f, u256(4));
1106-
testContractAgainstCpp("a(bytes32)", f, u256(5));
1107-
testContractAgainstCpp("a(bytes32)", f, u256(-1));
1108-
)
1109-
}
1110-
1111915
BOOST_AUTO_TEST_CASE(inter_contract_calls)
1112916
{
1113917
char const* sourceCode = R"(
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
contract C {
2+
function f(int256 input) public returns (bytes32 sha256hash) {
3+
return keccak256(abi.encodePacked(bytes32(uint256(input))));
4+
}
5+
}
6+
// ----
7+
// f(int256): 4 -> 0x8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd19b
8+
// f(int256): 5 -> 0x036b6384b5eca791c62761152d0c79bb0604c104a5fb6f4eb0703f3154bb3db0
9+
// f(int256): -1 -> 0xa9c584056064687e149968cbab758a3376d22aedc6a55823d1b3ecbee81b8fb9
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
contract C {
2+
function f(int256 _input) public returns (bytes32 hash) {
3+
uint24 b = 65536;
4+
uint c = 256;
5+
bytes32 input = bytes32(uint256(_input));
6+
return keccak256(abi.encodePacked(uint8(8), input, b, input, c));
7+
}
8+
}
9+
// ----
10+
// f(int256): 4 -> 0xd270285b9966fefc715561efcd09d5b6a8deb15596f7c53cb4a1bb73aa55ac3a
11+
// f(int256): 5 -> 0xf2f92566c5653600c1e527a7073e5d881576d12bb51887c0b8f3e1f81865b03d
12+
// f(int256): -1 -> 0xbc78b45e0db67af5af72e4ab62757c67aefa7388cdf0c4e74f8b5fe9dd5d9d13
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
contract C {
2+
uint120[3] x;
3+
function f() public returns (bytes32 hash1, bytes32 hash2, bytes32 hash3) {
4+
uint120[] memory y = new uint120[](3);
5+
x[0] = y[0] = uint120(type(uint).max - 1);
6+
x[1] = y[1] = uint120(type(uint).max - 2);
7+
x[2] = y[2] = uint120(type(uint).max - 3);
8+
hash1 = keccak256(abi.encodePacked(x));
9+
hash2 = keccak256(abi.encodePacked(y));
10+
hash3 = keccak256(abi.encodePacked(this.f));
11+
}
12+
}
13+
// ----
14+
// f() -> 0xba4f20407251e4607cd66b90bfea19ec6971699c03e4a4f3ea737d5818ac27ae, 0xba4f20407251e4607cd66b90bfea19ec6971699c03e4a4f3ea737d5818ac27ae, 0x0e9229fb1d2cd02cee4b6c9f25497777014a8766e3479666d1c619066d2887ec
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
contract C {
2+
function f(int256 input) public returns (bytes32 sha256hash) {
3+
return ripemd160(abi.encodePacked(bytes32(uint256(input))));
4+
}
5+
}
6+
// ----
7+
// f(int256): 4 -> 0x1b0f3c404d12075c68c938f9f60ebea4f74941a0000000000000000000000000
8+
// f(int256): 5 -> 0xee54aa84fc32d8fed5a5fe160442ae84626829d9000000000000000000000000
9+
// f(int256): -1 -> 0x1cf4e77f5966e13e109703cd8a0df7ceda7f3dc3000000000000000000000000
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
contract C {
2+
function f(int256 _input) public returns (bytes32 hash) {
3+
uint24 b = 65536;
4+
uint c = 256;
5+
bytes32 input = bytes32(uint256(_input));
6+
return ripemd160(abi.encodePacked(uint8(8), input, b, input, c));
7+
}
8+
}
9+
// ----
10+
// f(int256): 4 -> 0xf93175303eba2a7b372174fc9330237f5ad202fc000000000000000000000000
11+
// f(int256): 5 -> 0x04f4fc112e2bfbe0d38f896a46629e08e2fcfad5000000000000000000000000
12+
// f(int256): -1 -> 0xc0a2e4b1f3ff766a9a0089e7a410391730872495000000000000000000000000
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
contract C {
2+
function f(int256 input) public returns (bytes32 sha256hash) {
3+
return sha256(abi.encodePacked(bytes32(uint256(input))));
4+
}
5+
}
6+
// ----
7+
// f(int256): 4 -> 0xe38990d0c7fc009880a9c07c23842e886c6bbdc964ce6bdd5817ad357335ee6f
8+
// f(int256): 5 -> 0x96de8fc8c256fa1e1556d41af431cace7dca68707c78dd88c3acab8b17164c47
9+
// f(int256): -1 -> 0xaf9613760f72635fbdb44a5a0a63c39f12af30f950a6ee5c971be188e89c4051
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
contract C {
2+
function f(int256 _input) public returns (bytes32 hash) {
3+
uint24 b = 65536;
4+
uint c = 256;
5+
bytes32 input = bytes32(uint256(_input));
6+
return sha256(abi.encodePacked(uint8(8), input, b, input, c));
7+
}
8+
}
9+
// ----
10+
// f(int256): 4 -> 0x804e0d7003cfd70fc925dc103174d9f898ebb142ecc2a286da1abd22ac2ce3ac
11+
// f(int256): 5 -> 0xe94921945f9068726c529a290a954f412bcac53184bb41224208a31edbf63cf0
12+
// f(int256): -1 -> 0xf14def4d07cd185ddd8b10a81b2238326196a38867e6e6adbcc956dc913488c7

0 commit comments

Comments
 (0)