-
Notifications
You must be signed in to change notification settings - Fork 347
/
opts.go
105 lines (96 loc) · 3.43 KB
/
opts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package plonk
import (
"fmt"
"math/big"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/std/recursion"
)
// GetNativeProverOptions returns PLONK prover options for the native prover to
// initialize the configuration suitable for in-circuit verification.
func GetNativeProverOptions(outer, field *big.Int) backend.ProverOption {
return func(pc *backend.ProverConfig) error {
fsProverHasher, err := recursion.NewShort(outer, field)
if err != nil {
return fmt.Errorf("get prover fs hash: %w", err)
}
kzgProverHasher, err := recursion.NewShort(outer, field)
if err != nil {
return fmt.Errorf("get prover kzg hash: %w", err)
}
htfProverHasher, err := recursion.NewShort(outer, field)
if err != nil {
return fmt.Errorf("get hash to field: %w", err)
}
fsOpt := backend.WithProverChallengeHashFunction(fsProverHasher)
if err = fsOpt(pc); err != nil {
return fmt.Errorf("apply prover fs hash option: %w", err)
}
kzgOpt := backend.WithProverKZGFoldingHashFunction(kzgProverHasher)
if err = kzgOpt(pc); err != nil {
return fmt.Errorf("apply prover kzg folding hash option: %w", err)
}
htfOpt := backend.WithProverHashToFieldFunction(htfProverHasher)
if err = htfOpt(pc); err != nil {
return fmt.Errorf("apply prover htf option: %w", err)
}
return nil
}
}
// GetNativeVerifierOptions returns PLONK verifier options to initialize the
// configuration to be compatible with in-circuit verification.
func GetNativeVerifierOptions(outer, field *big.Int) backend.VerifierOption {
return func(vc *backend.VerifierConfig) error {
fsVerifierHasher, err := recursion.NewShort(outer, field)
if err != nil {
return fmt.Errorf("get verifier fs hash: %w", err)
}
kzgVerifierHasher, err := recursion.NewShort(outer, field)
if err != nil {
return fmt.Errorf("get verifier kzg hash: %w", err)
}
htfVerifierHasher, err := recursion.NewShort(outer, field)
if err != nil {
return fmt.Errorf("get hash to field: %w", err)
}
fsOpt := backend.WithVerifierChallengeHashFunction(fsVerifierHasher)
if err = fsOpt(vc); err != nil {
return fmt.Errorf("apply verifier fs hash option: %w", err)
}
kzgOpt := backend.WithVerifierKZGFoldingHashFunction(kzgVerifierHasher)
if err = kzgOpt(vc); err != nil {
return fmt.Errorf("apply verifier kzg folding hash option: %w", err)
}
htfOpt := backend.WithVerifierHashToFieldFunction(htfVerifierHasher)
if err = htfOpt(vc); err != nil {
return fmt.Errorf("apply verifier htf option: %w", err)
}
return nil
}
}
type verifierCfg struct {
withCompleteArithmetic bool
}
// VerifierOption allows to modify the behaviour of PLONK verifier.
type VerifierOption func(cfg *verifierCfg) error
// WithCompleteArithmetic forces the usage of complete formulas for point
// addition and multi-scalar multiplication. The option is necessary when
// recursing simple inner circuits whose selector polynomials may have
// exceptional cases (zeros, equal to each other, inverses of each other).
//
// Safe formulas are less efficient to use, so using this option has performance
// impact on the outer circuit size.
func WithCompleteArithmetic() VerifierOption {
return func(cfg *verifierCfg) error {
cfg.withCompleteArithmetic = true
return nil
}
}
func newCfg(opts ...VerifierOption) (*verifierCfg, error) {
cfg := new(verifierCfg)
for i := range opts {
if err := opts[i](cfg); err != nil {
return nil, fmt.Errorf("option %d: %w", i, err)
}
}
return cfg, nil
}