Consensys / gnark-crypto

gnark-crypto provides elliptic curve and pairing-based cryptography on BN, BLS12, BLS24 and BW6 curves. It also provides various algorithms (algebra, crypto) of particular interest to zero knowledge proof systems.
Apache License 2.0
502 stars 162 forks source link

How to call mimc to input a string and output the mimc hashed result? #443

Closed fengyin450 closed 1 year ago

fengyin450 commented 1 year ago

In the example of https://play.gnark.io/#, it can be seen that the hash result of "Secret": "0xdeadf00d" is "Hash": "1037254799353855871006189384309576393135431139055333626960622147300727 796413", how to call https://github.com/Consensys/gnark-crypto/blob/master/ecc/bn254/fr/mimc/mimc.go can input Secret to get Hash by implementing mimc function? I tried many methods, but the output is 0 or wrong result. The following is one of them:


func mimcHash(data []byte) string {

    f := bn254.NewMiMC()

    f.Write(data)

    hash := f.Sum(nil)

    hashInt := big.NewInt(0).SetBytes(hash)

    return hashInt.String()

}
gbotrel commented 1 year ago

Hi:

    h := mimc.NewMiMC()
    var message fr.Element
    message.SetString("0xdeadf00d")
    h.Write(message.Marshal())
    res := h.Sum(nil)

    var result fr.Element
    result.SetBytes(res)
    fmt.Println(result.String())