Gas Cost for keccak256 – What Is the Gas Cost of keccak256(“abc”) in Solidity?

gaskeccaksha3solidity

According to this answer, the gas cost of keccak256 is:

30 gas + 6 gas for each word (rounded up) for input data to a SHA3 Keccak-256 operation

I'm assuming that the size of a word is 256 bits and the size of a character is 8 bits.

Hence the gas cost keccak256("abc") or any other string of up to 32 characters is 36 gas units.

Is my assumption correct?

If yes, is my conclusion correct?

Thank you!

Best Answer

Yes, you are exactly correct, except for the empty string.

keccak256("") costs 30 gas.

keccak256("0") costs 36 gas.

keccak256("0123456789ABCDEF0123456789ABCDEF") still costs 36 gas.

keccak256("0123456789ABCDEF0123456789ABCDEF_") costs 42 gas.

These gas costs do not include costs other than the actual KECCAK256 opcode, such as pushing the pointer and length of the input onto the stack.