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: allow custom hash function in backends #873

Merged
merged 30 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b65af20
feat: add backend options
ivokub Oct 18, 2023
ef9c625
feat: choosable field to hash in plonk verifier
ivokub Oct 18, 2023
da1565f
chore: generate
ivokub Oct 18, 2023
ba889a4
fix: hash result len max(modlen, digestsize)
ivokub Oct 19, 2023
774e8ce
chore: generate
ivokub Oct 19, 2023
59c47e5
feat: allow custom hash to field during proving
ivokub Oct 19, 2023
c956c1e
chore: generate
ivokub Oct 19, 2023
06ebd95
test: add custom hash-to-field test using constant hash fn
ivokub Oct 19, 2023
e1a64d6
chore: go.mod update
ivokub Oct 19, 2023
d305f99
refactor: split backend option to prover and verifier opts
ivokub Oct 19, 2023
b20a0c2
refactor: split backend option in templates
ivokub Oct 19, 2023
2af8a9f
chore: generate
ivokub Oct 19, 2023
426b45f
test: enable custom hash-to-field test
ivokub Oct 19, 2023
168a141
refactor: simplify hash-to-field init
ivokub Oct 19, 2023
4625a0b
feat: add custom hash to field to groth16
ivokub Oct 19, 2023
ff715ff
refactor: use import template
ivokub Oct 19, 2023
d82502b
chore: generate
ivokub Oct 19, 2023
99634fc
feat: pass options to verifiers
ivokub Oct 19, 2023
6b7934a
test: test custom hash to field
ivokub Oct 19, 2023
110ee9d
docs: add option descriptions
ivokub Oct 19, 2023
e029d79
feat: add custom challenge hash
ivokub Oct 19, 2023
a2f5484
test: add custom challenge hash test
ivokub Oct 19, 2023
e3d9015
feat: add custom KZG folding challenge hash
ivokub Oct 19, 2023
afc599e
feat: use custom KZG folding hash
ivokub Oct 19, 2023
092aa3d
test: add custom KZG folding hash test
ivokub Oct 19, 2023
9a1c27f
chore: generate
ivokub Oct 19, 2023
c2cd091
feat: use custom challenge hash in plonk+fri
ivokub Oct 19, 2023
191746a
chore: generate
ivokub Oct 19, 2023
47dcdbf
feat: implement verifier options in tests
ivokub Oct 19, 2023
bb66151
chore: go mod update
ivokub Oct 20, 2023
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
114 changes: 111 additions & 3 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
// Package backend implements Zero Knowledge Proof systems: it consumes circuit compiled with gnark/frontend.
package backend

import "github.com/consensys/gnark/constraint/solver"
import (
"crypto/sha256"
"hash"

"github.com/consensys/gnark/constraint/solver"
)

// ID represent a unique ID for a proving scheme
type ID uint16
Expand Down Expand Up @@ -53,13 +58,21 @@ type ProverOption func(*ProverConfig) error

// ProverConfig is the configuration for the prover with the options applied.
type ProverConfig struct {
SolverOpts []solver.Option
SolverOpts []solver.Option
HashToFieldFn hash.Hash
ChallengeHash hash.Hash
KZGFoldingHash hash.Hash
}

// NewProverConfig returns a default ProverConfig with given prover options opts
// applied.
func NewProverConfig(opts ...ProverOption) (ProverConfig, error) {
opt := ProverConfig{}
opt := ProverConfig{
// we cannot initialize HashToFieldFn here as we use different domain
// separation tags for PLONK and Groth16
ChallengeHash: sha256.New(),
KZGFoldingHash: sha256.New(),
}
for _, option := range opts {
if err := option(&opt); err != nil {
return ProverConfig{}, err
Expand All @@ -75,3 +88,98 @@ func WithSolverOptions(solverOpts ...solver.Option) ProverOption {
return nil
}
}

// WithProverHashToFieldFunction changes the hash function used for hashing
// bytes to field. If not set then the default hash function based on RFC 9380
// is used. Used mainly for compatibility between different systems and
// efficient recursion.
func WithProverHashToFieldFunction(hFunc hash.Hash) ProverOption {
return func(cfg *ProverConfig) error {
cfg.HashToFieldFn = hFunc
return nil
}
}

// WithProverChallengeHashFunction sets the hash function used for computing
// non-interactive challenges in Fiat-Shamir heuristic. If not set then by
// default SHA2-256 is used. Used mainly for compatibility between different
// systems and efficient recursion.
func WithProverChallengeHashFunction(hFunc hash.Hash) ProverOption {
return func(pc *ProverConfig) error {
pc.ChallengeHash = hFunc
return nil
}
}

// WithProverKZGFoldingHashFunction sets the hash function used for computing
// the challenge when folding the KZG opening proofs. If not set then by default
// SHA2-256 is used. Used mainly for compatibility between different systems and
// efficient recursion.
func WithProverKZGFoldingHashFunction(hFunc hash.Hash) ProverOption {
return func(pc *ProverConfig) error {
pc.KZGFoldingHash = hFunc
return nil
}
}

// VerifierOption defines option for altering the behavior of the verifier. See
// the descriptions of functions returning instances of this type for
// implemented options.
type VerifierOption func(*VerifierConfig) error

// VerifierConfig is the configuration for the verifier with the options applied.
type VerifierConfig struct {
HashToFieldFn hash.Hash
ChallengeHash hash.Hash
KZGFoldingHash hash.Hash
}

// NewVerifierConfig returns a default [VerifierConfig] with given verifier
// options applied.
func NewVerifierConfig(opts ...VerifierOption) (VerifierConfig, error) {
opt := VerifierConfig{
// we cannot initialize HashToFieldFn here as we use different domain
// separation tags for PLONK and Groth16
ChallengeHash: sha256.New(),
KZGFoldingHash: sha256.New(),
}
for _, option := range opts {
if err := option(&opt); err != nil {
return VerifierConfig{}, err
}
}
return opt, nil
}

// WithVerifierHashToFieldFunction changes the hash function used for hashing
// bytes to field. If not set then the default hash function based on RFC 9380
// is used. Used mainly for compatibility between different systems and
// efficient recursion.
func WithVerifierHashToFieldFunction(hFunc hash.Hash) VerifierOption {
return func(cfg *VerifierConfig) error {
cfg.HashToFieldFn = hFunc
return nil
}
}

// WithVerifierChallengeHashFunction sets the hash function used for computing
// non-interactive challenges in Fiat-Shamir heuristic. If not set then by
// default SHA2-256 is used. Used mainly for compatibility between different
// systems and efficient recursion.
func WithVerifierChallengeHashFunction(hFunc hash.Hash) VerifierOption {
return func(pc *VerifierConfig) error {
pc.ChallengeHash = hFunc
return nil
}
}

// WithVerifierKZGFoldingHashFunction sets the hash function used for computing
// the challenge when folding the KZG opening proofs. If not set then by default
// SHA2-256 is used. Used mainly for compatibility between different systems and
// efficient recursion.
func WithVerifierKZGFoldingHashFunction(hFunc hash.Hash) VerifierOption {
return func(pc *VerifierConfig) error {
pc.KZGFoldingHash = hFunc
return nil
}
}
29 changes: 0 additions & 29 deletions backend/groth16/bls12-377/commitment.go

This file was deleted.

16 changes: 14 additions & 2 deletions backend/groth16/bls12-377/prove.go

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

31 changes: 23 additions & 8 deletions backend/groth16/bls12-377/verify.go

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

29 changes: 0 additions & 29 deletions backend/groth16/bls12-381/commitment.go

This file was deleted.

16 changes: 14 additions & 2 deletions backend/groth16/bls12-381/prove.go

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

Loading