Skip to content

Commit

Permalink
add HashFunctionMiMC_BN254
Browse files Browse the repository at this point in the history
mimc hash implementation over bn254 ecc from gnark
  • Loading branch information
altergui committed Dec 14, 2024
1 parent 67a67ba commit d6f2552
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"crypto/sha256"
"math/big"

"github.com/consensys/gnark-crypto/ecc/bls12-377/fr/mimc"
mimc_bls12_377 "github.com/consensys/gnark-crypto/ecc/bls12-377/fr/mimc"
mimc_bn254 "github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc"
"github.com/iden3/go-iden3-crypto/poseidon"
multiposeidon "github.com/vocdoni/vocdoni-z-sandbox/hash/poseidon"
"golang.org/x/crypto/blake2b"
Expand All @@ -24,6 +25,9 @@ var (
// TypeHashMiMC_BLS12_377 represents the label for the HashFunction of MiMC
// over BLS12-377 curve
TypeHashMiMC_BLS12_377 = []byte("mimc_bls12_377")
// TypeHashMiMC_BN254 represents the label for the HashFunction of MiMC
// over BN254 curve
TypeHashMiMC_BN254 = []byte("mimc_bn254")

// HashFunctionSha256 contains the HashSha256 struct which implements
// the HashFunction interface
Expand All @@ -40,6 +44,9 @@ var (
// HashFunctionMiMC_BLS12_377 contains the HashMiMC_BLS12_377 struct which
// implements the HashFunction interface
HashFunctionMiMC_BLS12_377 HashMiMC_BLS12_377
// HashFunctionMiMC_BN254 contains the HashMiMC_BN254 struct which
// implements the HashFunction interface
HashFunctionMiMC_BN254 HashMiMC_BN254
)

// Once Generics are at Go, this will be updated (August 2021
Expand Down Expand Up @@ -182,16 +189,49 @@ func (f HashMiMC_BLS12_377) Type() []byte {

// Len returns the length of the Hash output for the HashMiMC_BLS12_377
func (f HashMiMC_BLS12_377) Len() int {
return mimc.BlockSize
return mimc_bls12_377.BlockSize
}

// Hash implements the hash method for the HashFunction HashMiMC_BLS12_377
func (f HashMiMC_BLS12_377) Hash(b ...[]byte) ([]byte, error) {
h := mimc.NewMiMC()
h := mimc_bls12_377.NewMiMC()
for i := 0; i < len(b); i++ {
if _, err := h.Write(SwapEndianness(b[i])); err != nil {
return nil, err
}
}
return SwapEndianness(h.Sum(nil)), nil
}

// HashMiMC_BN254 implements the HashFunction interface for the MiMC hash
// over the BN254 curve
type HashMiMC_BN254 struct{}

// Type returns the type of HashFunction for the HashMiMC_BN254
func (f HashMiMC_BN254) Type() []byte {
return TypeHashMiMC_BN254
}

// Len returns the length of the Hash output for the HashMiMC_BN254
func (f HashMiMC_BN254) Len() int {
return mimc_bn254.BlockSize
}

// Hash implements the hash method for the HashFunction HashMiMC_BN254
func (f HashMiMC_BN254) Hash(b ...[]byte) ([]byte, error) {
h := mimc_bn254.NewMiMC()
for _, input := range b {
// Since arbo uses little-endian but MiMC expects inputs as big-endian
// we need to split long inputs and SwapEndianness of each chunk.
for start := 0; start < len(input); start += h.BlockSize() {
end := start + h.BlockSize()
if end > len(input) {
end = len(input)
}
if _, err := h.Write(SwapEndianness(input[start:end])); err != nil {
return nil, err
}
}
}
return SwapEndianness(h.Sum(nil)), nil
}

0 comments on commit d6f2552

Please sign in to comment.