[Ethereum] From golang sha3 to solidity sha3

golanghash-algorithmkeccaksha3solidity

Trying to match "golang.org/x/crypto/sha3" lib
with solidity sha3() is giving me a hard time.
Its been discussed in here, but I somehow cant apply it in go.
How do I handle a big.Int type s.t i get solidity.sha3(uint256(1))==golang.sha3.Sum256(convertMyBigInt(myBigInt)) ?

I turn the bigInt into a hex byte array, then fill up the left site with zeros, hash and then output.

//create a big int for test and set to 1
b1 := new(big.Int)
b1.SetInt64(1)
//create empty array with 32 bytes for padding
empty := make([]byte,32)
//turn the big int into a hex string (let me know if there is a more 
      //elegant way^^)
String := bytes.NewBufferString(b1.Text(16))
copy(empty[len(empty)-len(String.Bytes()):],String.Bytes())
//output of empty : [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 49]
res:= new(big.Int)
temp:=sha3.Sum256(empty)
res.SetBytes(temp[:])   
fmt.Println("\nresult ",res.Text(16))

output: 26502030f0954243c70cb7fa68e0752ce2bf99aabfc0219d5184635d4c61dbd8

solidity gives me:

sha3(uint(1))
b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6

can someone give me a short example how to match solidity sha3 with golang sha3 in an elegant manner? I'd be very thankful!

Best Answer

Here's a full working example on generating solidity sha3 hashes from uint256:

package main

import (
    "fmt"
    "math/big"

    "github.com/ethereum/go-ethereum/accounts/abi"
    "github.com/ethereum/go-ethereum/crypto/sha3"
)

func main() {
    h := sha3.NewKeccak256()
    h.Write(abi.U256(big.NewInt(1)))
    hash := h.Sum(nil)
    fmt.Printf("%x", hash) // b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6
}

Solidity:

sha3(uint(1))
b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6

I've made the library go-solidity-sha3 to be able to easily convert solidity types into solidity sha3 hashes in Go. Here's an example:

package main

import (
    "fmt"
    "math/big"

    solsha3 "github.com/miguelmota/go-solidity-sha3"
)

func main() {
    hash := solsha3.SoliditySHA3(
        solsha3.Uint256(big.NewInt(1)),
    )
    fmt.Printf("%x", hash) // b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6
}

A more complex example:

package main

import (
    "encoding/hex"
    "fmt"
    "math/big"

    "github.com/miguelmota/go-solidity-sha3"
)

func main() {
    hash := solsha3.SoliditySHA3(
        solsha3.Address("0x12459c951127e0c374ff9105dda097662a027093"),
        solsha3.Uint256(big.NewInt(100)),
        solsha3.String("foo"),
        solsha3.Bytes32("bar"),
        solsha3.Bool(true),
    )

    fmt.Println(hex.EncodeToString(hash)) // 417a4c44724701ba79bb363151dff48909bc058a2c75a81e9cf5208ae4699369
}
Related Topic