forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algopts.go
69 lines (62 loc) · 2.06 KB
/
algopts.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
// Package algopts provides shareable options for modifying algebraic operations.
//
// This package is separate to avoid cyclic imports and sharing the structures
// between interface definition, implementation getters and actual
// implementations.
package algopts
import "fmt"
type algebraCfg struct {
NbScalarBits int
FoldMulti bool
CompleteArithmetic bool
}
// AlgebraOption allows modifying algebraic operation behaviour.
type AlgebraOption func(*algebraCfg) error
// WithNbScalarBits defines the number bits when doing scalar multiplication.
// May be used when it is known that only bits least significant bits are
// non-zero. Reduces the cost for scalar multiplication. If not set then full
// width of scalars used.
func WithNbScalarBits(bits int) AlgebraOption {
return func(ac *algebraCfg) error {
if ac.NbScalarBits != 0 {
return fmt.Errorf("WithNbBits already set")
}
ac.NbScalarBits = bits
return nil
}
}
// WithFoldingScalarMul can be used when calling MultiScalarMul. By using this
// option we assume that the scalars are `1, scalar, scalar^2, ...`. We use the
// first element as the scalar to be used as a folding coefficients. By using
// this option we avoid one scalar multiplication and do not need to compute the
// powers of the folding coefficient.
func WithFoldingScalarMul() AlgebraOption {
return func(ac *algebraCfg) error {
if ac.FoldMulti {
return fmt.Errorf("withFoldingScalarMul already set")
}
ac.FoldMulti = true
return nil
}
}
// WithCompleteArithmetic forces the use of safe addition formulas for scalar
// multiplication.
func WithCompleteArithmetic() AlgebraOption {
return func(ac *algebraCfg) error {
if ac.CompleteArithmetic {
return fmt.Errorf("WithCompleteArithmetic already set")
}
ac.CompleteArithmetic = true
return nil
}
}
// NewConfig applies all given options and returns a configuration to be used.
func NewConfig(opts ...AlgebraOption) (*algebraCfg, error) {
ret := new(algebraCfg)
for i := range opts {
if err := opts[i](ret); err != nil {
return nil, err
}
}
return ret, nil
}