0

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!

1

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.

  • Thank you very much! BTW, no null-character issue (i.e., a string of 32 characters would cost 40 gas units)? – goodvibration Dec 13 '18 at 11:50
  • 1
    @goodvibration No, 32 characters costs 36 gas. Solidity stores the length of the string before the actual string characters. It does not use a null terminator like C. – Jesse Busman Dec 13 '18 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.