test: Move hashing algorithm tests to semanticTests

This commit is contained in:
Alex Beregszaszi 2022-10-20 16:02:33 +02:00
parent 08af255658
commit 4f07be6348
8 changed files with 77 additions and 196 deletions

View File

@ -912,202 +912,6 @@ BOOST_AUTO_TEST_CASE(transfer_ether)
) )
} }
BOOST_AUTO_TEST_CASE(keccak256)
{
char const* sourceCode = R"(
contract test {
function a(bytes32 input) public returns (bytes32 hash) {
return keccak256(abi.encodePacked(input));
}
}
)";
ALSO_VIA_YUL(
DISABLE_EWASM_TESTRUN()
compileAndRun(sourceCode);
auto f = [&](u256 const& _x) -> u256
{
return util::keccak256(toBigEndian(_x));
};
testContractAgainstCpp("a(bytes32)", f, u256(4));
testContractAgainstCpp("a(bytes32)", f, u256(5));
testContractAgainstCpp("a(bytes32)", f, u256(-1));
);
}
BOOST_AUTO_TEST_CASE(sha256)
{
char const* sourceCode = R"(
contract test {
function a(bytes32 input) public returns (bytes32 sha256hash) {
return sha256(abi.encodePacked(input));
}
}
)";
auto f = [&](u256 const& _x) -> bytes
{
if (_x == u256(4))
return fromHex("e38990d0c7fc009880a9c07c23842e886c6bbdc964ce6bdd5817ad357335ee6f");
if (_x == u256(5))
return fromHex("96de8fc8c256fa1e1556d41af431cace7dca68707c78dd88c3acab8b17164c47");
if (_x == u256(-1))
return fromHex("af9613760f72635fbdb44a5a0a63c39f12af30f950a6ee5c971be188e89c4051");
return fromHex("");
};
ALSO_VIA_YUL(
DISABLE_EWASM_TESTRUN()
compileAndRun(sourceCode);
testContractAgainstCpp("a(bytes32)", f, u256(4));
testContractAgainstCpp("a(bytes32)", f, u256(5));
testContractAgainstCpp("a(bytes32)", f, u256(-1));
)
}
BOOST_AUTO_TEST_CASE(ripemd)
{
char const* sourceCode = R"(
contract test {
function a(bytes32 input) public returns (bytes32 sha256hash) {
return ripemd160(abi.encodePacked(input));
}
}
)";
auto f = [&](u256 const& _x) -> bytes
{
if (_x == u256(4))
return fromHex("1b0f3c404d12075c68c938f9f60ebea4f74941a0000000000000000000000000");
if (_x == u256(5))
return fromHex("ee54aa84fc32d8fed5a5fe160442ae84626829d9000000000000000000000000");
if (_x == u256(-1))
return fromHex("1cf4e77f5966e13e109703cd8a0df7ceda7f3dc3000000000000000000000000");
return fromHex("");
};
ALSO_VIA_YUL(
DISABLE_EWASM_TESTRUN()
compileAndRun(sourceCode);
testContractAgainstCpp("a(bytes32)", f, u256(4));
testContractAgainstCpp("a(bytes32)", f, u256(5));
testContractAgainstCpp("a(bytes32)", f, u256(-1));
)
}
BOOST_AUTO_TEST_CASE(packed_keccak256)
{
char const* sourceCode = R"(
contract test {
function a(bytes32 input) public returns (bytes32 hash) {
uint24 b = 65536;
uint c = 256;
return keccak256(abi.encodePacked(uint8(8), input, b, input, c));
}
}
)";
auto f = [&](u256 const& _x) -> u256
{
return util::keccak256(
toCompactBigEndian(unsigned(8)) +
toBigEndian(_x) +
toCompactBigEndian(unsigned(65536)) +
toBigEndian(_x) +
toBigEndian(u256(256))
);
};
ALSO_VIA_YUL(
DISABLE_EWASM_TESTRUN()
compileAndRun(sourceCode);
testContractAgainstCpp("a(bytes32)", f, u256(4));
testContractAgainstCpp("a(bytes32)", f, u256(5));
testContractAgainstCpp("a(bytes32)", f, u256(-1));
)
}
BOOST_AUTO_TEST_CASE(packed_keccak256_complex_types)
{
char const* sourceCode = R"(
contract test {
uint120[3] x;
function f() public returns (bytes32 hash1, bytes32 hash2, bytes32 hash3) {
uint120[] memory y = new uint120[](3);
x[0] = y[0] = uint120(type(uint).max - 1);
x[1] = y[1] = uint120(type(uint).max - 2);
x[2] = y[2] = uint120(type(uint).max - 3);
hash1 = keccak256(abi.encodePacked(x));
hash2 = keccak256(abi.encodePacked(y));
hash3 = keccak256(abi.encodePacked(this.f));
}
}
)";
ALSO_VIA_YUL(
DISABLE_EWASM_TESTRUN()
compileAndRun(sourceCode);
// Strangely, arrays are encoded with intra-element padding.
ABI_CHECK(callContractFunction("f()"), encodeArgs(
util::keccak256(encodeArgs(u256("0xfffffffffffffffffffffffffffffe"), u256("0xfffffffffffffffffffffffffffffd"), u256("0xfffffffffffffffffffffffffffffc"))),
util::keccak256(encodeArgs(u256("0xfffffffffffffffffffffffffffffe"), u256("0xfffffffffffffffffffffffffffffd"), u256("0xfffffffffffffffffffffffffffffc"))),
util::keccak256(fromHex(m_contractAddress.hex() + "26121ff0"))
));
)
}
BOOST_AUTO_TEST_CASE(packed_sha256)
{
char const* sourceCode = R"(
contract test {
function a(bytes32 input) public returns (bytes32 hash) {
uint24 b = 65536;
uint c = 256;
return sha256(abi.encodePacked(uint8(8), input, b, input, c));
}
}
)";
auto f = [&](u256 const& _x) -> bytes
{
if (_x == u256(4))
return fromHex("804e0d7003cfd70fc925dc103174d9f898ebb142ecc2a286da1abd22ac2ce3ac");
if (_x == u256(5))
return fromHex("e94921945f9068726c529a290a954f412bcac53184bb41224208a31edbf63cf0");
if (_x == u256(-1))
return fromHex("f14def4d07cd185ddd8b10a81b2238326196a38867e6e6adbcc956dc913488c7");
return fromHex("");
};
ALSO_VIA_YUL(
DISABLE_EWASM_TESTRUN()
compileAndRun(sourceCode);
testContractAgainstCpp("a(bytes32)", f, u256(4));
testContractAgainstCpp("a(bytes32)", f, u256(5));
testContractAgainstCpp("a(bytes32)", f, u256(-1));
)
}
BOOST_AUTO_TEST_CASE(packed_ripemd160)
{
char const* sourceCode = R"(
contract test {
function a(bytes32 input) public returns (bytes32 hash) {
uint24 b = 65536;
uint c = 256;
return ripemd160(abi.encodePacked(uint8(8), input, b, input, c));
}
}
)";
auto f = [&](u256 const& _x) -> bytes
{
if (_x == u256(4))
return fromHex("f93175303eba2a7b372174fc9330237f5ad202fc000000000000000000000000");
if (_x == u256(5))
return fromHex("04f4fc112e2bfbe0d38f896a46629e08e2fcfad5000000000000000000000000");
if (_x == u256(-1))
return fromHex("c0a2e4b1f3ff766a9a0089e7a410391730872495000000000000000000000000");
return fromHex("");
};
ALSO_VIA_YUL(
DISABLE_EWASM_TESTRUN()
compileAndRun(sourceCode);
testContractAgainstCpp("a(bytes32)", f, u256(4));
testContractAgainstCpp("a(bytes32)", f, u256(5));
testContractAgainstCpp("a(bytes32)", f, u256(-1));
)
}
BOOST_AUTO_TEST_CASE(inter_contract_calls) BOOST_AUTO_TEST_CASE(inter_contract_calls)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(

View File

@ -0,0 +1,9 @@
contract C {
function f(int256 input) public returns (bytes32 sha256hash) {
return keccak256(abi.encodePacked(bytes32(uint256(input))));
}
}
// ----
// f(int256): 4 -> 0x8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd19b
// f(int256): 5 -> 0x036b6384b5eca791c62761152d0c79bb0604c104a5fb6f4eb0703f3154bb3db0
// f(int256): -1 -> 0xa9c584056064687e149968cbab758a3376d22aedc6a55823d1b3ecbee81b8fb9

View File

@ -0,0 +1,12 @@
contract C {
function f(int256 _input) public returns (bytes32 hash) {
uint24 b = 65536;
uint c = 256;
bytes32 input = bytes32(uint256(_input));
return keccak256(abi.encodePacked(uint8(8), input, b, input, c));
}
}
// ----
// f(int256): 4 -> 0xd270285b9966fefc715561efcd09d5b6a8deb15596f7c53cb4a1bb73aa55ac3a
// f(int256): 5 -> 0xf2f92566c5653600c1e527a7073e5d881576d12bb51887c0b8f3e1f81865b03d
// f(int256): -1 -> 0xbc78b45e0db67af5af72e4ab62757c67aefa7388cdf0c4e74f8b5fe9dd5d9d13

View File

@ -0,0 +1,14 @@
contract C {
uint120[3] x;
function f() public returns (bytes32 hash1, bytes32 hash2, bytes32 hash3) {
uint120[] memory y = new uint120[](3);
x[0] = y[0] = uint120(type(uint).max - 1);
x[1] = y[1] = uint120(type(uint).max - 2);
x[2] = y[2] = uint120(type(uint).max - 3);
hash1 = keccak256(abi.encodePacked(x));
hash2 = keccak256(abi.encodePacked(y));
hash3 = keccak256(abi.encodePacked(this.f));
}
}
// ----
// f() -> 0xba4f20407251e4607cd66b90bfea19ec6971699c03e4a4f3ea737d5818ac27ae, 0xba4f20407251e4607cd66b90bfea19ec6971699c03e4a4f3ea737d5818ac27ae, 0x0e9229fb1d2cd02cee4b6c9f25497777014a8766e3479666d1c619066d2887ec

View File

@ -0,0 +1,9 @@
contract C {
function f(int256 input) public returns (bytes32 sha256hash) {
return ripemd160(abi.encodePacked(bytes32(uint256(input))));
}
}
// ----
// f(int256): 4 -> 0x1b0f3c404d12075c68c938f9f60ebea4f74941a0000000000000000000000000
// f(int256): 5 -> 0xee54aa84fc32d8fed5a5fe160442ae84626829d9000000000000000000000000
// f(int256): -1 -> 0x1cf4e77f5966e13e109703cd8a0df7ceda7f3dc3000000000000000000000000

View File

@ -0,0 +1,12 @@
contract C {
function f(int256 _input) public returns (bytes32 hash) {
uint24 b = 65536;
uint c = 256;
bytes32 input = bytes32(uint256(_input));
return ripemd160(abi.encodePacked(uint8(8), input, b, input, c));
}
}
// ----
// f(int256): 4 -> 0xf93175303eba2a7b372174fc9330237f5ad202fc000000000000000000000000
// f(int256): 5 -> 0x04f4fc112e2bfbe0d38f896a46629e08e2fcfad5000000000000000000000000
// f(int256): -1 -> 0xc0a2e4b1f3ff766a9a0089e7a410391730872495000000000000000000000000

View File

@ -0,0 +1,9 @@
contract C {
function f(int256 input) public returns (bytes32 sha256hash) {
return sha256(abi.encodePacked(bytes32(uint256(input))));
}
}
// ----
// f(int256): 4 -> 0xe38990d0c7fc009880a9c07c23842e886c6bbdc964ce6bdd5817ad357335ee6f
// f(int256): 5 -> 0x96de8fc8c256fa1e1556d41af431cace7dca68707c78dd88c3acab8b17164c47
// f(int256): -1 -> 0xaf9613760f72635fbdb44a5a0a63c39f12af30f950a6ee5c971be188e89c4051

View File

@ -0,0 +1,12 @@
contract C {
function f(int256 _input) public returns (bytes32 hash) {
uint24 b = 65536;
uint c = 256;
bytes32 input = bytes32(uint256(_input));
return sha256(abi.encodePacked(uint8(8), input, b, input, c));
}
}
// ----
// f(int256): 4 -> 0x804e0d7003cfd70fc925dc103174d9f898ebb142ecc2a286da1abd22ac2ce3ac
// f(int256): 5 -> 0xe94921945f9068726c529a290a954f412bcac53184bb41224208a31edbf63cf0
// f(int256): -1 -> 0xf14def4d07cd185ddd8b10a81b2238326196a38867e6e6adbcc956dc913488c7