Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make gkr hash registries private and threadsafe #920

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading