Skip to content

Commit 404f8e5

Browse files
Tabaieivokub
andauthored
feat/hashregistry (#687)
Co-authored-by: Ivo Kubjas <[email protected]>
1 parent 5660088 commit 404f8e5

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

ecc/ecc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ func Implemented() []ID {
4545
}
4646

4747
func IDFromString(s string) (ID, error) {
48+
s = strings.ToLower(s)
4849
for _, id := range Implemented() {
49-
if strings.ToLower(s) == id.String() {
50+
if s == id.String() {
5051
return id, nil
5152
}
5253
}

hash/all/allhashes.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,29 @@ package all
33
import (
44
_ "github.com/consensys/gnark-crypto/ecc/bls12-377/fr/mimc"
55
_ "github.com/consensys/gnark-crypto/ecc/bls12-377/fr/poseidon2"
6+
67
_ "github.com/consensys/gnark-crypto/ecc/bls12-381/fr/mimc"
8+
_ "github.com/consensys/gnark-crypto/ecc/bls12-381/fr/poseidon2"
9+
710
_ "github.com/consensys/gnark-crypto/ecc/bls24-315/fr/mimc"
11+
_ "github.com/consensys/gnark-crypto/ecc/bls24-315/fr/poseidon2"
12+
813
_ "github.com/consensys/gnark-crypto/ecc/bls24-317/fr/mimc"
14+
_ "github.com/consensys/gnark-crypto/ecc/bls24-317/fr/poseidon2"
15+
916
_ "github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc"
17+
_ "github.com/consensys/gnark-crypto/ecc/bn254/fr/poseidon2"
18+
1019
_ "github.com/consensys/gnark-crypto/ecc/bw6-633/fr/mimc"
20+
_ "github.com/consensys/gnark-crypto/ecc/bw6-633/fr/poseidon2"
21+
1122
_ "github.com/consensys/gnark-crypto/ecc/bw6-761/fr/mimc"
23+
_ "github.com/consensys/gnark-crypto/ecc/bw6-761/fr/poseidon2"
24+
25+
_ "github.com/consensys/gnark-crypto/field/babybear/poseidon2"
26+
_ "github.com/consensys/gnark-crypto/field/goldilocks/poseidon2"
27+
_ "github.com/consensys/gnark-crypto/field/koalabear/poseidon2"
28+
29+
_ "github.com/consensys/gnark-crypto/ecc/grumpkin/fr/mimc"
30+
_ "github.com/consensys/gnark-crypto/ecc/grumpkin/fr/poseidon2"
1231
)

hash/hashes.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,36 @@ import (
99
"strings"
1010
)
1111

12-
var hashes = make([]func() hash.Hash, maxHash)
12+
var (
13+
hashes = make([]func() hash.Hash, maxHash)
14+
hashesByName = make(map[string]func() hash.Hash)
15+
)
16+
17+
func findStdHashByName(name string) Hash {
18+
for h := range maxHash {
19+
if h.String() == name {
20+
return h
21+
}
22+
}
23+
return maxHash
24+
}
25+
26+
// NewHash returns a new hash.Hash object for the given hash function name.
27+
// It can be a standard hash function (e.g. "MIMC_BN254"),
28+
// or a custom hash function defined by the user through [RegisterCustomHash].
29+
func NewHash(name string) hash.Hash {
30+
// first see if it's a standard hash function
31+
if h := findStdHashByName(name); h < maxHash {
32+
return h.New()
33+
}
34+
35+
// see if it's a custom hash function - registered by the user
36+
if f, ok := hashesByName[name]; ok {
37+
return f()
38+
}
39+
40+
panic(fmt.Errorf("hash function \"%s\" not registered", name))
41+
}
1342

1443
// RegisterHash registers a new hash function constructor. Should be called in
1544
// the init function of the hash package.
@@ -20,7 +49,16 @@ func RegisterHash(h Hash, new func() hash.Hash) {
2049
hashes[h] = new
2150
}
2251

23-
// Hash defines an unique identifier for a hash function.
52+
// RegisterCustomHash registers a new hash function constructor, retrievable by name
53+
// using NewHash. It does not allow overwriting standard hash functions.
54+
func RegisterCustomHash(name string, new func() hash.Hash) {
55+
if h := findStdHashByName(name); h < maxHash {
56+
panic(fmt.Errorf("cannot overwrite standard hash function \"%s\"", name))
57+
}
58+
hashesByName[name] = new
59+
}
60+
61+
// Hash defines a unique identifier for a hash function.
2462
type Hash uint
2563

2664
const (

0 commit comments

Comments
 (0)