Evaluate keccak256 of string literals at compile-time.

This commit is contained in:
chriseth
2020-07-08 16:14:14 +02:00
parent ad5ae2eefe
commit 0a0f578d7c
4 changed files with 50 additions and 15 deletions
+8 -4
View File
@@ -792,20 +792,24 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
TypePointer const& argType = arguments.front()->annotation().type;
solAssert(argType, "");
arguments.front()->accept(*this);
// Optimization: If type is bytes or string, then do not encode,
// but directly compute keccak256 on memory.
if (*argType == *TypeProvider::bytesMemory() || *argType == *TypeProvider::stringMemory())
if (auto const* stringLiteral = dynamic_cast<StringLiteralType const*>(argType))
// Optimization: Compute keccak256 on string literals at compile-time.
m_context << u256(keccak256(stringLiteral->value()));
else if (*argType == *TypeProvider::bytesMemory() || *argType == *TypeProvider::stringMemory())
{
// Optimization: If type is bytes or string, then do not encode,
// but directly compute keccak256 on memory.
ArrayUtils(m_context).retrieveLength(*TypeProvider::bytesMemory());
m_context << Instruction::SWAP1 << u256(0x20) << Instruction::ADD;
m_context << Instruction::KECCAK256;
}
else
{
utils().fetchFreeMemoryPointer();
utils().packedEncode({argType}, TypePointers());
utils().toSizeAfterFreeMemoryPointer();
m_context << Instruction::KECCAK256;
}
m_context << Instruction::KECCAK256;
break;
}
case FunctionType::Kind::Log0: