diff options
author | chriseth <chris@ethereum.org> | 2017-05-31 01:34:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-31 01:34:28 +0800 |
commit | d4a57d81ba7f2a9b5db57397d36f1a17adb4b142 (patch) | |
tree | 15d8048ad9ed48dc7d010e37fd2125df8d5db436 /test/libsolidity | |
parent | 254b55728f66ea164e9085700a294ac4837f2029 (diff) | |
parent | dcb7c51920a947e57371b7dc92417f47ee5cc65c (diff) | |
download | dexon-solidity-d4a57d81ba7f2a9b5db57397d36f1a17adb4b142.tar.gz dexon-solidity-d4a57d81ba7f2a9b5db57397d36f1a17adb4b142.tar.zst dexon-solidity-d4a57d81ba7f2a9b5db57397d36f1a17adb4b142.zip |
Merge pull request #2317 from ethereum/keccak256
Use keccak256 in tests and replace the SHA3 instruction in assembly
Diffstat (limited to 'test/libsolidity')
-rw-r--r-- | test/libsolidity/GasMeter.cpp | 6 | ||||
-rw-r--r-- | test/libsolidity/InlineAssembly.cpp | 8 | ||||
-rw-r--r-- | test/libsolidity/SolidityEndToEndTest.cpp | 100 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 8 | ||||
-rw-r--r-- | test/libsolidity/SolidityOptimizer.cpp | 80 |
5 files changed, 133 insertions, 69 deletions
diff --git a/test/libsolidity/GasMeter.cpp b/test/libsolidity/GasMeter.cpp index f90cb105..ef560b12 100644 --- a/test/libsolidity/GasMeter.cpp +++ b/test/libsolidity/GasMeter.cpp @@ -151,20 +151,20 @@ BOOST_AUTO_TEST_CASE(simple_contract) contract test { bytes32 public shaValue; function f(uint a) { - shaValue = sha3(a); + shaValue = keccak256(a); } } )"; testCreationTimeGas(sourceCode); } -BOOST_AUTO_TEST_CASE(store_sha3) +BOOST_AUTO_TEST_CASE(store_keccak256) { char const* sourceCode = R"( contract test { bytes32 public shaValue; function test(uint a) { - shaValue = sha3(a); + shaValue = keccak256(a); } } )"; diff --git a/test/libsolidity/InlineAssembly.cpp b/test/libsolidity/InlineAssembly.cpp index b390a40b..f0543101 100644 --- a/test/libsolidity/InlineAssembly.cpp +++ b/test/libsolidity/InlineAssembly.cpp @@ -481,6 +481,14 @@ BOOST_AUTO_TEST_CASE(revert) BOOST_CHECK(successAssemble("{ revert(0, 0) }")); } +BOOST_AUTO_TEST_CASE(keccak256) +{ + BOOST_CHECK(successAssemble("{ 0 0 keccak256 pop }")); + BOOST_CHECK(successAssemble("{ pop(keccak256(0, 0)) }")); + BOOST_CHECK(successAssemble("{ 0 0 sha3 pop }")); + BOOST_CHECK(successAssemble("{ pop(sha3(0, 0)) }")); +} + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END() diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 1ff0b6cb..aae8b146 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -1355,7 +1355,7 @@ BOOST_AUTO_TEST_CASE(multiple_elementary_accessors) function test() { data = 8; name = "Celina"; - a_hash = sha3(123); + a_hash = keccak256(123); an_address = address(0x1337); super_secret_data = 42; } @@ -1864,12 +1864,12 @@ BOOST_AUTO_TEST_CASE(selfdestruct) BOOST_CHECK_EQUAL(balanceAt(address), amount); } -BOOST_AUTO_TEST_CASE(sha3) +BOOST_AUTO_TEST_CASE(keccak256) { char const* sourceCode = R"( contract test { - function a(bytes32 input) returns (bytes32 sha3hash) { - return sha3(input); + function a(bytes32 input) returns (bytes32 hash) { + return keccak256(input); } } )"; @@ -1883,6 +1883,23 @@ BOOST_AUTO_TEST_CASE(sha3) testContractAgainstCpp("a(bytes32)", f, u256(-1)); } +BOOST_AUTO_TEST_CASE(sha3) +{ + char const* sourceCode = R"( + contract test { + // to confuse the optimiser + function b(bytes32 input) returns (bytes32) { + return sha3(input); + } + function a(bytes32 input) returns (bool) { + return keccak256(input) == b(input); + } + } + )"; + compileAndRun(sourceCode); + BOOST_REQUIRE(callContractFunction("a(bytes32)", u256(42)) == encodeArgs(true)); +} + BOOST_AUTO_TEST_CASE(sha256) { char const* sourceCode = R"( @@ -3110,13 +3127,13 @@ BOOST_AUTO_TEST_CASE(empty_name_return_parameter) BOOST_CHECK(callContractFunction("f(uint256)", 9) == encodeArgs(9)); } -BOOST_AUTO_TEST_CASE(sha3_multiple_arguments) +BOOST_AUTO_TEST_CASE(keccak256_multiple_arguments) { char const* sourceCode = R"( contract c { function foo(uint a, uint b, uint c) returns (bytes32 d) { - d = sha3(a, b, c); + d = keccak256(a, b, c); } } )"; @@ -3129,13 +3146,13 @@ BOOST_AUTO_TEST_CASE(sha3_multiple_arguments) toBigEndian(u256(13))))); } -BOOST_AUTO_TEST_CASE(sha3_multiple_arguments_with_numeric_literals) +BOOST_AUTO_TEST_CASE(keccak256_multiple_arguments_with_numeric_literals) { char const* sourceCode = R"( contract c { function foo(uint a, uint16 b) returns (bytes32 d) { - d = sha3(a, b, 145); + d = keccak256(a, b, 145); } } )"; @@ -3148,17 +3165,17 @@ BOOST_AUTO_TEST_CASE(sha3_multiple_arguments_with_numeric_literals) bytes(1, 0x91)))); } -BOOST_AUTO_TEST_CASE(sha3_multiple_arguments_with_string_literals) +BOOST_AUTO_TEST_CASE(keccak256_multiple_arguments_with_string_literals) { char const* sourceCode = R"( contract c { function foo() returns (bytes32 d) { - d = sha3("foo"); + d = keccak256("foo"); } function bar(uint a, uint16 b) returns (bytes32 d) { - d = sha3(a, b, 145, "foo"); + d = keccak256(a, b, 145, "foo"); } } )"; @@ -3174,7 +3191,7 @@ BOOST_AUTO_TEST_CASE(sha3_multiple_arguments_with_string_literals) bytes{0x66, 0x6f, 0x6f}))); } -BOOST_AUTO_TEST_CASE(sha3_with_bytes) +BOOST_AUTO_TEST_CASE(keccak256_with_bytes) { char const* sourceCode = R"( contract c { @@ -3185,7 +3202,7 @@ BOOST_AUTO_TEST_CASE(sha3_with_bytes) data[0] = "f"; data[1] = "o"; data[2] = "o"; - return sha3(data) == sha3("foo"); + return keccak256(data) == keccak256("foo"); } } )"; @@ -3193,7 +3210,7 @@ BOOST_AUTO_TEST_CASE(sha3_with_bytes) BOOST_CHECK(callContractFunction("foo()") == encodeArgs(true)); } -BOOST_AUTO_TEST_CASE(iterated_sha3_with_bytes) +BOOST_AUTO_TEST_CASE(iterated_keccak256_with_bytes) { char const* sourceCode = R"( contract c { @@ -3204,7 +3221,7 @@ BOOST_AUTO_TEST_CASE(iterated_sha3_with_bytes) data[0] = "x"; data[1] = "y"; data[2] = "z"; - return sha3("b", sha3(data), "a"); + return keccak256("b", keccak256(data), "a"); } } )"; @@ -3214,13 +3231,13 @@ BOOST_AUTO_TEST_CASE(iterated_sha3_with_bytes) )); } -BOOST_AUTO_TEST_CASE(keccak256_multiple_arguments) +BOOST_AUTO_TEST_CASE(sha3_multiple_arguments) { char const* sourceCode = R"( contract c { function foo(uint a, uint b, uint c) returns (bytes32 d) { - d = keccak256(a, b, c); + d = sha3(a, b, c); } })"; compileAndRun(sourceCode); @@ -3245,7 +3262,7 @@ BOOST_AUTO_TEST_CASE(generic_call) function sender() payable {} function doSend(address rec) returns (uint d) { - bytes4 signature = bytes4(bytes32(sha3("receive(uint256)"))); + bytes4 signature = bytes4(bytes32(keccak256("receive(uint256)"))); rec.call.value(2)(signature, 23); return receiver(rec).received(); } @@ -3270,7 +3287,7 @@ BOOST_AUTO_TEST_CASE(generic_callcode) function Sender() payable { } function doSend(address rec) returns (uint d) { - bytes4 signature = bytes4(bytes32(sha3("receive(uint256)"))); + bytes4 signature = bytes4(bytes32(keccak256("receive(uint256)"))); rec.callcode.value(2)(signature, 23); return Receiver(rec).received(); } @@ -3307,7 +3324,7 @@ BOOST_AUTO_TEST_CASE(generic_delegatecall) function Sender() payable {} function doSend(address rec) payable { - bytes4 signature = bytes4(bytes32(sha3("receive(uint256)"))); + bytes4 signature = bytes4(bytes32(keccak256("receive(uint256)"))); if (rec.delegatecall(signature, 23)) {} } } @@ -3372,7 +3389,7 @@ BOOST_AUTO_TEST_CASE(bytes_from_calldata_to_memory) char const* sourceCode = R"( contract C { function f() returns (bytes32) { - return sha3("abc", msg.data); + return keccak256("abc", msg.data); } } )"; @@ -5294,7 +5311,7 @@ BOOST_AUTO_TEST_CASE(reusing_memory) mapping(uint => uint) map; function f(uint x) returns (uint) { map[x] = x; - return (new Helper(uint(sha3(this.g(map[x]))))).flag(); + return (new Helper(uint(keccak256(this.g(map[x]))))).flag(); } function g(uint a) returns (uint) { @@ -9321,6 +9338,45 @@ BOOST_AUTO_TEST_CASE(interface) BOOST_CHECK(callContractFunction("f(address)", recipient) == encodeArgs(true)); } +BOOST_AUTO_TEST_CASE(keccak256_assembly) +{ + char const* sourceCode = R"( + contract C { + function f() returns (bytes32 ret) { + assembly { + ret := keccak256(0, 0) + } + } + function g() returns (bytes32 ret) { + assembly { + 0 + 0 + keccak256 + =: ret + } + } + function h() returns (bytes32 ret) { + assembly { + ret := sha3(0, 0) + } + } + function i() returns (bytes32 ret) { + assembly { + 0 + 0 + sha3 + =: ret + } + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f()") == fromHex("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")); + BOOST_CHECK(callContractFunction("g()") == fromHex("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")); + BOOST_CHECK(callContractFunction("h()") == fromHex("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")); + BOOST_CHECK(callContractFunction("i()") == fromHex("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 0553c691..71726b93 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2934,12 +2934,12 @@ BOOST_AUTO_TEST_CASE(non_initialized_references) CHECK_WARNING(text, "Uninitialized storage pointer"); } -BOOST_AUTO_TEST_CASE(sha3_with_large_integer_constant) +BOOST_AUTO_TEST_CASE(keccak256_with_large_integer_constant) { char const* text = R"( contract c { - function f() { sha3(2**500); } + function f() { keccak256(2**500); } } )"; CHECK_ERROR(text, TypeError, ""); @@ -5401,7 +5401,7 @@ BOOST_AUTO_TEST_CASE(cyclic_dependency_for_constants) contract C { uint constant a = b * c; uint constant b = 7; - uint constant c = b + uint(sha3(d)); + uint constant c = b + uint(keccak256(d)); uint constant d = 2 + a; } )"; @@ -5410,7 +5410,7 @@ BOOST_AUTO_TEST_CASE(cyclic_dependency_for_constants) contract C { uint constant a = b * c; uint constant b = 7; - uint constant c = 4 + uint(sha3(d)); + uint constant c = 4 + uint(keccak256(d)); uint constant d = 2 + b; } )"; diff --git a/test/libsolidity/SolidityOptimizer.cpp b/test/libsolidity/SolidityOptimizer.cpp index d705e3c8..bdcdacff 100644 --- a/test/libsolidity/SolidityOptimizer.cpp +++ b/test/libsolidity/SolidityOptimizer.cpp @@ -322,18 +322,18 @@ BOOST_AUTO_TEST_CASE(storage_write_in_loops) // Information in joining branches is not retained anymore. BOOST_AUTO_TEST_CASE(retain_information_in_branches) { - // This tests that the optimizer knows that we already have "z == sha3(y)" inside both branches. + // This tests that the optimizer knows that we already have "z == keccak256(y)" inside both branches. char const* sourceCode = R"( contract c { bytes32 d; uint a; function f(uint x, bytes32 y) returns (uint r_a, bytes32 r_d) { - bytes32 z = sha3(y); + bytes32 z = keccak256(y); if (x > 8) { - z = sha3(y); + z = keccak256(y); a = x; } else { - z = sha3(y); + z = keccak256(y); a = x; } r_a = a; @@ -349,7 +349,7 @@ BOOST_AUTO_TEST_CASE(retain_information_in_branches) bytes optimizedBytecode = compileAndRunWithOptimizer(sourceCode, 0, "c", true); size_t numSHA3s = 0; eachInstruction(optimizedBytecode, [&](Instruction _instr, u256 const&) { - if (_instr == Instruction::SHA3) + if (_instr == Instruction::KECCAK256) numSHA3s++; }); // TEST DISABLED - OPTIMIZER IS NOT EFFECTIVE ON THIS ONE ANYMORE @@ -358,7 +358,7 @@ BOOST_AUTO_TEST_CASE(retain_information_in_branches) BOOST_AUTO_TEST_CASE(store_tags_as_unions) { - // This calls the same function from two sources and both calls have a certain sha3 on + // This calls the same function from two sources and both calls have a certain Keccak-256 on // the stack at the same position. // Without storing tags as unions, the return from the shared function would not know where to // jump and thus all jumpdests are forced to clear their state and we do not know about the @@ -370,19 +370,19 @@ BOOST_AUTO_TEST_CASE(store_tags_as_unions) contract test { bytes32 data; function f(uint x, bytes32 y) external returns (uint r_a, bytes32 r_d) { - r_d = sha3(y); + r_d = keccak256(y); shared(y); - r_d = sha3(y); + r_d = keccak256(y); r_a = 5; } function g(uint x, bytes32 y) external returns (uint r_a, bytes32 r_d) { - r_d = sha3(y); + r_d = keccak256(y); shared(y); - r_d = bytes32(uint(sha3(y)) + 2); + r_d = bytes32(uint(keccak256(y)) + 2); r_a = 7; } function shared(bytes32 y) internal { - data = sha3(y); + data = keccak256(y); } } )"; @@ -392,7 +392,7 @@ BOOST_AUTO_TEST_CASE(store_tags_as_unions) bytes optimizedBytecode = compileAndRunWithOptimizer(sourceCode, 0, "test", true); size_t numSHA3s = 0; eachInstruction(optimizedBytecode, [&](Instruction _instr, u256 const&) { - if (_instr == Instruction::SHA3) + if (_instr == Instruction::KECCAK256) numSHA3s++; }); // TEST DISABLED UNTIL 93693404 IS IMPLEMENTED @@ -401,8 +401,8 @@ BOOST_AUTO_TEST_CASE(store_tags_as_unions) BOOST_AUTO_TEST_CASE(incorrect_storage_access_bug) { - // This bug appeared because a sha3 operation with too low sequence number was used, - // resulting in memory not being rewritten before the sha3. The fix was to + // This bug appeared because a Keccak-256 operation with too low sequence number was used, + // resulting in memory not being rewritten before the Keccak-256. The fix was to // take the max of the min sequence numbers when merging the states. char const* sourceCode = R"( contract C @@ -821,19 +821,19 @@ BOOST_AUTO_TEST_CASE(cse_jumpi_jump) }); } -BOOST_AUTO_TEST_CASE(cse_empty_sha3) +BOOST_AUTO_TEST_CASE(cse_empty_keccak256) { AssemblyItems input{ u256(0), Instruction::DUP2, - Instruction::SHA3 + Instruction::KECCAK256 }; checkCSE(input, { u256(dev::keccak256(bytesConstRef())) }); } -BOOST_AUTO_TEST_CASE(cse_partial_sha3) +BOOST_AUTO_TEST_CASE(cse_partial_keccak256) { AssemblyItems input{ u256(0xabcd) << (256 - 16), @@ -841,7 +841,7 @@ BOOST_AUTO_TEST_CASE(cse_partial_sha3) Instruction::MSTORE, u256(2), u256(0), - Instruction::SHA3 + Instruction::KECCAK256 }; checkCSE(input, { u256(0xabcd) << (256 - 16), @@ -851,19 +851,19 @@ BOOST_AUTO_TEST_CASE(cse_partial_sha3) }); } -BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_location) +BOOST_AUTO_TEST_CASE(cse_keccak256_twice_same_location) { - // sha3 twice from same dynamic location + // Keccak-256 twice from same dynamic location AssemblyItems input{ Instruction::DUP2, Instruction::DUP1, Instruction::MSTORE, u256(64), Instruction::DUP2, - Instruction::SHA3, + Instruction::KECCAK256, u256(64), Instruction::DUP3, - Instruction::SHA3 + Instruction::KECCAK256 }; checkCSE(input, { Instruction::DUP2, @@ -871,27 +871,27 @@ BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_location) Instruction::MSTORE, u256(64), Instruction::DUP2, - Instruction::SHA3, + Instruction::KECCAK256, Instruction::DUP1 }); } -BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content) +BOOST_AUTO_TEST_CASE(cse_keccak256_twice_same_content) { - // sha3 twice from different dynamic location but with same content + // Keccak-256 twice from different dynamic location but with same content AssemblyItems input{ Instruction::DUP1, u256(0x80), Instruction::MSTORE, // m[128] = DUP1 u256(0x20), u256(0x80), - Instruction::SHA3, // sha3(m[128..(128+32)]) + Instruction::KECCAK256, // keccak256(m[128..(128+32)]) Instruction::DUP2, u256(12), Instruction::MSTORE, // m[12] = DUP1 u256(0x20), u256(12), - Instruction::SHA3 // sha3(m[12..(12+32)]) + Instruction::KECCAK256 // keccak256(m[12..(12+32)]) }; checkCSE(input, { u256(0x80), @@ -900,7 +900,7 @@ BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content) Instruction::MSTORE, u256(0x20), Instruction::SWAP1, - Instruction::SHA3, + Instruction::KECCAK256, u256(12), Instruction::DUP3, Instruction::SWAP1, @@ -909,10 +909,10 @@ BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content) }); } -BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content_dynamic_store_in_between) +BOOST_AUTO_TEST_CASE(cse_keccak256_twice_same_content_dynamic_store_in_between) { - // sha3 twice from different dynamic location but with same content, - // dynamic mstore in between, which forces us to re-calculate the sha3 + // Keccak-256 twice from different dynamic location but with same content, + // dynamic mstore in between, which forces us to re-calculate the hash AssemblyItems input{ u256(0x80), Instruction::DUP2, @@ -921,7 +921,7 @@ BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content_dynamic_store_in_between) u256(0x20), Instruction::DUP1, Instruction::DUP3, - Instruction::SHA3, // sha3(m[128..(128+32)]) + Instruction::KECCAK256, // keccak256(m[128..(128+32)]) u256(12), Instruction::DUP5, Instruction::DUP2, @@ -932,15 +932,15 @@ BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content_dynamic_store_in_between) Instruction::SWAP2, Instruction::SWAP1, Instruction::SWAP2, - Instruction::SHA3 // sha3(m[12..(12+32)]) + Instruction::KECCAK256 // keccak256(m[12..(12+32)]) }; checkCSE(input, input); } -BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content_noninterfering_store_in_between) +BOOST_AUTO_TEST_CASE(cse_keccak256_twice_same_content_noninterfering_store_in_between) { - // sha3 twice from different dynamic location but with same content, - // dynamic mstore in between, but does not force us to re-calculate the sha3 + // Keccak-256 twice from different dynamic location but with same content, + // dynamic mstore in between, but does not force us to re-calculate the hash AssemblyItems input{ u256(0x80), Instruction::DUP2, @@ -949,7 +949,7 @@ BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content_noninterfering_store_in_between u256(0x20), Instruction::DUP1, Instruction::DUP3, - Instruction::SHA3, // sha3(m[128..(128+32)]) + Instruction::KECCAK256, // keccak256(m[128..(128+32)]) u256(12), Instruction::DUP5, Instruction::DUP2, @@ -962,12 +962,12 @@ BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content_noninterfering_store_in_between Instruction::MSTORE, // does not destoy memory knowledge u256(0x20), u256(12), - Instruction::SHA3 // sha3(m[12..(12+32)]) + Instruction::KECCAK256 // keccak256(m[12..(12+32)]) }; // if this changes too often, only count the number of SHA3 and MSTORE instructions AssemblyItems output = CSE(input); BOOST_CHECK_EQUAL(4, count(output.begin(), output.end(), AssemblyItem(Instruction::MSTORE))); - BOOST_CHECK_EQUAL(1, count(output.begin(), output.end(), AssemblyItem(Instruction::SHA3))); + BOOST_CHECK_EQUAL(1, count(output.begin(), output.end(), AssemblyItem(Instruction::KECCAK256))); } BOOST_AUTO_TEST_CASE(cse_with_initially_known_stack) @@ -1296,7 +1296,7 @@ BOOST_AUTO_TEST_CASE(constant_optimization_early_exit) // Store and hash assembly { mstore(32, x) - ret := sha3(0, 40) + ret := keccak256(0, 40) } } } |