[Sol->Yul] Implement keccak256

This commit is contained in:
Mathias Baumann 2019-07-08 21:57:02 +02:00
parent 15eb8fec50
commit 37f04976a2
3 changed files with 38 additions and 2 deletions

View File

@ -607,6 +607,26 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
break;
}
case FunctionType::Kind::KECCAK256:
{
solAssert(arguments.size() == 1, "");
ArrayType const* arrayType = TypeProvider::bytesMemory();
string const& array = m_context.newYulVariable();
m_code << "let " << array << " := " << expressionAsType(*arguments[0], *arrayType) << "\n";
defineExpression(_functionCall) <<
"keccak256(" <<
m_utils.arrayDataAreaFunction(*arrayType) << "(" <<
array <<
"), " <<
m_utils.arrayLengthFunction(*arrayType) <<
"(" <<
array <<
"))\n";
break;
}
default:
solUnimplemented("FunctionKind " + toString(static_cast<int>(functionType->kind())) + " not yet implemented");
}

View File

@ -3470,8 +3470,10 @@ BOOST_AUTO_TEST_CASE(keccak256_empty)
}
}
)";
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f()"), fromHex("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"));
ALSO_VIA_YUL(
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f()"), fromHex("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"));
)
}
BOOST_AUTO_TEST_CASE(keccak256_multiple_arguments)

View File

@ -0,0 +1,14 @@
contract C {
function keccak1() public pure returns (bytes32) {
return keccak256("123");
}
function keccak2() public pure returns (bytes32) {
bytes memory a = "123";
return keccak256(a);
}
}
// ====
// compileViaYul: only
// ----
// keccak1() -> 0x64e604787cbf194841e7b68d7cd28786f6c9a0a3ab9f8b0a0e87cb4387ab0107
// keccak2() -> 0x64e604787cbf194841e7b68d7cd28786f6c9a0a3ab9f8b0a0e87cb4387ab0107