5

I have two uint256 values -> 2 and 0

When performing: keccak256(abi.encodePacked(uint(2), uint(0)))

I got the correct result:

0xabbb5caa7dda850e60932de0934eb1f9d0f59695050f761dc64e443e5030a569

What I am confused about is when I am trying to use solely keccak256 on the value: 0x00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000 which is the result of abi.encodePacked(uint(2), uint(0))

In that case with keccak256 I am getting: 0x608133906c7feb1802e3582d4a7765ca241f9618f3eabaa89a6a14fdea761d9d

What am I missing here? I would expect that I will get the same results.

To sum it up. Why am I getting different results with functions A and B? What raw value do I need to provide to keccak256 of testB to get the same results as testA ?

function testEncode() public pure returns(bytes memory) {
  return abi.encodePacked(uint(2), uint(0)); 
  // returns 0x00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000
}

function testA() public pure returns(bytes32) {
  return keccak256(abi.encodePacked(uint(2), uint(0)));
  // returns 0xabbb5caa7dda850e60932de0934eb1f9d0f59695050f761dc64e443e5030a569
}

function testB() public pure returns(bytes32) {
  // abi.encodePacked(uint(2), uint(0)) // 0x00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000
  return keccak256("0x00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000"); 
  // returns 0x608133906c7feb1802e3582d4a7765ca241f9618f3eabaa89a6a14fdea761d9d
}

I will be really thankful for your assistance. I am looking into this for hours. I am just trying to understand how it's working internally.

Cheers,

2

The issue is how you are passing the result of abi.encodePacked to the keccak256 function. keccak256 takes a hex argument and not a string as you did in your testB().

using an online tool i was able to get the correct result by passing a hex input type without the 0x at the start of the input. for your case here passing just the hex directly without the string quotes ("") will do the job.

1
  • Thank you! Such a mistake. I was trying the online tool before but I didn't switch from "text" to "hex". and that was the issue! so silly :D
    – Rhonyn
    Aug 2 at 15:01
3

In testB, a string is the argument of keccak256 while in testA, a hexadecimal literal is the argument. There are two solutions:

1- As @Kaki Master Of Time mentioned, you can remove quotes ("")

2- Replace by hex"0000....0002000....0000"

Maybe the following codes make the concept more clear:

bytes2 a1 = "12"; // fine
bytes2 a2 = "0x1234"; // compile error
bytes2 a3 = "0x"; // fine
bytes2 a4 = hex"1234"; // fine
bytes2 a5 = 0x1234; // fine

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.