Solidity: using input of `0` in sha3 function produces incorrect output

Created on 29 Aug 2016  路  5Comments  路  Source: ethereum/solidity

I suspect this is a bug but wanted to report and confirm.
When passing 0 to sha3 function in Solidity, instead of a predefined uint zero-value variable the produced output is incorrect. Sample below

contract MyContract
{
    bytes32 public sha3test1;
    bytes32 public sha3test2;
    uint256 zero = 0;

    function MyContract(){

       sha3test1 = sha3(0);
       sha3test2 = sha3(zero);
    }
}

produces:

sha3test1 = 0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a
sha3test2 = 0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563
documentation enhancement

Most helpful comment

The document here could be made a tiny bit more explicit about how it behaves.

All 5 comments

@area has rightly pointed out that Solidity casts to the smallest type hence using a uint8 for the supplied 0 value. Tests confirm this is the case so closing this.

I'm not sure that is the correct behaviour though for sha3.

In the above case you coerce with sha3(uint256(0));, but I would be include to say that constants for tight packing should either:

  • be disallowed
  • or taken as uin256.

If finer granularity is needed they can be typecasted or defined as variables.

The document here could be made a tiny bit more explicit about how it behaves.

Ok, so sha3(zero) doesn't work... how does one get it to work as expected? It would be quite helpful to show how to actually hash a f*ing uint256 somewhere. Tightly packed or not, I don't really care and just want it to work like any sane person would expect.

@beether how do you expect it to work? sha3(0) should be the same as sha3(uint8(0)), should be the same as

uint 8 x = 0;
sha3(x);

On the other hand, sha3(uint(0)) is a different value which should be equal to uint x = 0; sha3(x);.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chriseth picture chriseth  路  4Comments

chriseth picture chriseth  路  3Comments

chriseth picture chriseth  路  4Comments

leviadam picture leviadam  路  4Comments

hiqua picture hiqua  路  4Comments