-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpedersen.go
198 lines (158 loc) · 3.9 KB
/
pedersen.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) Pedersen authors.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
package pedersen
import (
"errors"
"fmt"
"runtime"
"github.com/matteoarella/pedersen/big"
)
const (
defaultGroupPrimeBitLen = 128
minThreshold = 2
)
var (
defaultConcLimit = runtime.NumCPU()
)
// errors
var (
ErrInvalidOptions = errors.New("invalid options")
ErrInvalidThreshold = fmt.Errorf("threshold must be at least %d", minThreshold)
)
// Option represents an option for configuring a Pedersen struct.
type Option func(*Pedersen)
// The CyclicGroup option sets the cyclic group to be used.
func CyclicGroup(group *Group) Option {
return func(p *Pedersen) {
p.group = group
}
}
// The ConcLimit option sets the maximum number of concurrent operations.
// If a negative number is provided, the number of concurrent operations
// is set to the number of CPUs.
func ConcLimit(concLimit int) Option {
return func(p *Pedersen) {
if concLimit < 1 {
concLimit = defaultConcLimit
}
p.concLimit = concLimit
}
}
// A Pedersen struct used for splitting, reconstructing, and verifying secrets.
type Pedersen struct {
group *Group
threshold int
parts int
concLimit int
}
func (p *Pedersen) validate() error {
if p.threshold < minThreshold {
return ErrInvalidThreshold
}
if p.parts < p.threshold {
return ErrInsufficientSharesParts
}
return nil
}
// NewPedersen creates a new Pedersen struct with the provided (threshold, parts) scheme.
// With such a scheme a secret is split into parts shares, of which at least threshold
// are required to reconstruct the secret.
// A new randomly generated cyclic group is used if none is provided.
func NewPedersen(parts, threshold int, options ...Option) (*Pedersen, error) {
defaultPedersenOptions := []Option{
ConcLimit(defaultConcLimit),
}
p := &Pedersen{
parts: parts,
threshold: threshold,
}
for _, o := range defaultPedersenOptions {
o(p)
}
// override default options
for _, o := range options {
o(p)
}
if p.group != nil {
if err := p.group.validate(); err != nil {
return nil, err
}
} else {
group, err := NewSchnorrGroup(defaultGroupPrimeBitLen)
if err != nil {
return nil, err
}
p.group = group
}
if err := p.validate(); err != nil {
return nil, err
}
return p, nil
}
// GetThreshold returns the threshold of the Pedersen struct.
func (p *Pedersen) GetThreshold() int {
return p.threshold
}
// GetThreshold returns the parts of the Pedersen struct.
func (p *Pedersen) GetParts() int {
return p.parts
}
// GetGroup returns the cyclic group of the Pedersen struct.
func (p *Pedersen) GetGroup() *Group {
return p.group
}
// GetConcLimit returns the maximum number of concurrent operations
// of the Pedersen struct.
func (p *Pedersen) GetConcLimit() int {
return p.concLimit
}
func (p *Pedersen) adjustConcLimit(num int) int {
concLimit := p.GetConcLimit()
if concLimit > num {
return num
}
return concLimit
}
func (p *Pedersen) balanceIndices(length, numChunks int) []chunkRange {
var ranges []chunkRange
chunkSize := (length + numChunks - 1) / numChunks
for start := 0; start < length; start += chunkSize {
end := start + chunkSize
if end > length {
end = length
}
ranges = append(ranges, chunkRange{
start: start,
end: end,
})
}
return ranges
}
func (p *Pedersen) commit(
mont *big.MontgomeryContext,
ctx *big.IntContext,
s *big.Int,
t *big.Int,
) (*big.Int, error) {
gs, err := big.NewInt()
if err != nil {
return nil, err
}
ht, err := ctx.GetInt()
if err != nil {
return nil, err
}
if err := gs.ModExpMont(mont, ctx, p.group.G, s, p.group.P); err != nil {
return nil, err
}
if err := ht.ModExpMont(mont, ctx, p.group.H, t, p.group.P); err != nil {
return nil, err
}
if err := gs.ModMul(ctx, gs, ht, p.group.P); err != nil {
return nil, err
}
return gs, nil
}