Skip to content

Commit

Permalink
feat: make registries for gkr thread safe (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbotrel authored Nov 16, 2023
1 parent 835adfe commit 088a222
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 40 deletions.
29 changes: 26 additions & 3 deletions constraint/bls12-377/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 26 additions & 3 deletions constraint/bls12-381/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 26 additions & 3 deletions constraint/bls24-315/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 26 additions & 3 deletions constraint/bls24-317/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 26 additions & 3 deletions constraint/bn254/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 26 additions & 3 deletions constraint/bw6-633/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 26 additions & 3 deletions constraint/bw6-761/gkr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 26 additions & 3 deletions internal/generator/backend/template/representations/gkr.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/consensys/gnark/std/utils/algo_utils"
"hash"
"math/big"
"sync"
)

type GkrSolvingData struct {
Expand Down Expand Up @@ -146,9 +147,12 @@ func GkrProveHint(hashName string, data *GkrSolvingData) hint.Hint {
return b[:]
})

hsh := HashBuilderRegistry[hashName]()
hsh, err := GetHashBuilder(hashName)
if err != nil {
return err
}

proof, err := gkr.Prove(data.circuit, data.assignments, fiatshamir.WithHash(hsh, insBytes...), gkr.WithPool(&data.memoryPool), gkr.WithWorkers(data.workers))
proof, err := gkr.Prove(data.circuit, data.assignments, fiatshamir.WithHash(hsh(), insBytes...), gkr.WithPool(&data.memoryPool), gkr.WithWorkers(data.workers))
if err != nil {
return err
}
Expand All @@ -175,4 +179,23 @@ func GkrProveHint(hashName string, data *GkrSolvingData) hint.Hint {
}

// TODO: Move to gnark-crypto
var HashBuilderRegistry = make(map[string]func() hash.Hash)
var (
hashBuilderRegistry = make(map[string]func() hash.Hash)
hasBuilderLock sync.RWMutex
)

func RegisterHashBuilder(name string, builder func() hash.Hash) {
hasBuilderLock.Lock()
defer hasBuilderLock.Unlock()
hashBuilderRegistry[name] = builder
}

func GetHashBuilder(name string) (func() hash.Hash, error) {
hasBuilderLock.RLock()
defer hasBuilderLock.RUnlock()
builder, ok := hashBuilderRegistry[name]
if !ok {
return nil, fmt.Errorf("hash function not found")
}
return builder, nil
}
Loading

0 comments on commit 088a222

Please sign in to comment.