diff --git a/ecc/ecc.go b/ecc/ecc.go index 70925ca1b1..b45637946f 100644 --- a/ecc/ecc.go +++ b/ecc/ecc.go @@ -45,8 +45,9 @@ func Implemented() []ID { } func IDFromString(s string) (ID, error) { + s = strings.ToLower(s) for _, id := range Implemented() { - if strings.ToLower(s) == id.String() { + if s == id.String() { return id, nil } } diff --git a/hash/all/allhashes.go b/hash/all/allhashes.go index a06636e056..6036a3c4cf 100644 --- a/hash/all/allhashes.go +++ b/hash/all/allhashes.go @@ -3,10 +3,29 @@ package all import ( _ "github.com/consensys/gnark-crypto/ecc/bls12-377/fr/mimc" _ "github.com/consensys/gnark-crypto/ecc/bls12-377/fr/poseidon2" + _ "github.com/consensys/gnark-crypto/ecc/bls12-381/fr/mimc" + _ "github.com/consensys/gnark-crypto/ecc/bls12-381/fr/poseidon2" + _ "github.com/consensys/gnark-crypto/ecc/bls24-315/fr/mimc" + _ "github.com/consensys/gnark-crypto/ecc/bls24-315/fr/poseidon2" + _ "github.com/consensys/gnark-crypto/ecc/bls24-317/fr/mimc" + _ "github.com/consensys/gnark-crypto/ecc/bls24-317/fr/poseidon2" + _ "github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc" + _ "github.com/consensys/gnark-crypto/ecc/bn254/fr/poseidon2" + _ "github.com/consensys/gnark-crypto/ecc/bw6-633/fr/mimc" + _ "github.com/consensys/gnark-crypto/ecc/bw6-633/fr/poseidon2" + _ "github.com/consensys/gnark-crypto/ecc/bw6-761/fr/mimc" + _ "github.com/consensys/gnark-crypto/ecc/bw6-761/fr/poseidon2" + + _ "github.com/consensys/gnark-crypto/field/babybear/poseidon2" + _ "github.com/consensys/gnark-crypto/field/goldilocks/poseidon2" + _ "github.com/consensys/gnark-crypto/field/koalabear/poseidon2" + + _ "github.com/consensys/gnark-crypto/ecc/grumpkin/fr/mimc" + _ "github.com/consensys/gnark-crypto/ecc/grumpkin/fr/poseidon2" ) diff --git a/hash/hashes.go b/hash/hashes.go index 140b674b6f..e43bea331e 100644 --- a/hash/hashes.go +++ b/hash/hashes.go @@ -9,7 +9,36 @@ import ( "strings" ) -var hashes = make([]func() hash.Hash, maxHash) +var ( + hashes = make([]func() hash.Hash, maxHash) + hashesByName = make(map[string]func() hash.Hash) +) + +func findStdHashByName(name string) Hash { + for h := range maxHash { + if h.String() == name { + return h + } + } + return maxHash +} + +// NewHash returns a new hash.Hash object for the given hash function name. +// It can be a standard hash function (e.g. "MIMC_BN254"), +// or a custom hash function defined by the user through [RegisterCustomHash]. +func NewHash(name string) hash.Hash { + // first see if it's a standard hash function + if h := findStdHashByName(name); h < maxHash { + return h.New() + } + + // see if it's a custom hash function - registered by the user + if f, ok := hashesByName[name]; ok { + return f() + } + + panic(fmt.Errorf("hash function \"%s\" not registered", name)) +} // RegisterHash registers a new hash function constructor. Should be called in // the init function of the hash package. @@ -20,7 +49,16 @@ func RegisterHash(h Hash, new func() hash.Hash) { hashes[h] = new } -// Hash defines an unique identifier for a hash function. +// RegisterCustomHash registers a new hash function constructor, retrievable by name +// using NewHash. It does not allow overwriting standard hash functions. +func RegisterCustomHash(name string, new func() hash.Hash) { + if h := findStdHashByName(name); h < maxHash { + panic(fmt.Errorf("cannot overwrite standard hash function \"%s\"", name)) + } + hashesByName[name] = new +} + +// Hash defines a unique identifier for a hash function. type Hash uint const (