-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.go
272 lines (219 loc) · 5.57 KB
/
split.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// 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"
"math"
"github.com/matteoarella/pedersen/big"
"golang.org/x/sync/errgroup"
)
var (
ErrEmptySecret = errors.New("cannot split an empty secret")
ErrInsufficientAbscissae = errors.New("abscissae cannot be less than parts")
)
const (
zerosInfoSizeBytes = 4
)
type splitValue struct {
index int
secretParts []SecretPart
secretComm []*big.Int
}
func leadingZeros(buff []byte) uint64 {
sum := uint64(0)
for _, n := range buff {
if n == 0 {
sum++
} else {
break
}
}
return sum
}
func bigIntPadding(ctx *big.IntContext, buff []byte) (*big.Int, error) {
ctx.Attach()
defer ctx.Detach()
zeros, err := ctx.GetInt()
if err != nil {
return nil, err
}
zerosCount := leadingZeros(buff)
if err := zeros.SetUInt64(zerosCount); err != nil {
return nil, err
}
n := new(big.Int).SetBytes(buff)
// append leading zeros info to n
if err := n.Lsh(n, zerosInfoSizeBytes*8); err != nil {
return nil, err
}
if zerosCount > 0 {
if err := n.Or(n, zeros); err != nil {
return nil, err
}
}
return n, nil
}
func splitSecret(ctx *big.IntContext, value []byte, max *big.Int) ([]*big.Int, error) {
valueLen := len(value)
// ensure every chunk value is smaller than max
partLen := int(math.Floor(float64(max.BitLen())/float64(8))) - zerosInfoSizeBytes
if partLen <= 0 {
partLen = 1
}
partCount := valueLen / partLen
if partCount*partLen < valueLen {
partCount++
}
splitted := make([]*big.Int, partCount)
var err error
for i := 0; i < partCount; i++ {
end := (i + 1) * partLen
if end > valueLen {
end = valueLen
}
valuePart := value[i*partLen : end]
splitted[i], err = bigIntPadding(ctx, valuePart)
if err != nil {
return nil, err
}
}
return splitted, nil
}
type chunkRange struct {
start, end int
}
func (p *Pedersen) split(
mont *big.MontgomeryContext,
ctx *big.IntContext,
index int,
secret *big.Int,
abscissae []*big.Int,
) (splitValue, error) {
ctx.Attach()
defer ctx.Detach()
F, err := newPolynomial(secret, p.threshold-1, p.group.Q)
if err != nil {
return splitValue{}, err
}
K, err := newPolynomial(nil, p.threshold-1, p.group.Q)
if err != nil {
return splitValue{}, err
}
secretParts := make([]SecretPart, p.parts)
for i := 0; i < p.parts; i++ {
s, err := F.evaluate(ctx, abscissae[i])
if err != nil {
return splitValue{}, err
}
t, err := K.evaluate(ctx, abscissae[i])
if err != nil {
return splitValue{}, err
}
secretParts[i] = SecretPart{
SShare: s,
TShare: t,
}
}
commitments := make([]*big.Int, p.threshold)
for i := 0; i < p.threshold; i++ {
commitment, err := p.commit(mont, ctx, F.coefficients[i], K.coefficients[i])
if err != nil {
return splitValue{}, err
}
commitments[i] = commitment
}
return splitValue{
index: index,
secretParts: secretParts,
secretComm: commitments,
}, nil
}
// Split takes a secret and generates a `parts`
// number of shares, `threshold` of which are required to reconstruct
// the secret.
// If the secret that has to be split is not representable in the cyclic group,
// the secret is split into chunks, and each chunk is split into secret parts according
// to Pedersen verifiable secret sharing.
// The abscissae are used to evaluate the polynomials at the given points.
// If abscissae is nil, random abscissae are generated.
func (p *Pedersen) Split(secret []byte, abscissae []*big.Int) (*Shares, error) {
if len(secret) == 0 {
return nil, ErrEmptySecret
}
if abscissae == nil {
abscissae = make([]*big.Int, p.parts)
err := randInts(abscissae, big.One(), p.group.Q, true)
if err != nil {
return nil, err
}
} else if len(abscissae) < p.parts {
return nil, ErrInsufficientAbscissae
}
ctx, err := big.NewIntContext()
if err != nil {
return nil, err
}
defer ctx.Destroy()
// split secret into many byte slices and process them
splitted, err := splitSecret(ctx, secret, p.group.Q)
if err != nil {
return nil, err
}
splittedLen := len(splitted)
concLimit := p.adjustConcLimit(splittedLen)
chunksIndex := p.balanceIndices(splittedLen, concLimit)
parts := make([][]SecretPart, p.parts)
commitments := make([][]*big.Int, splittedLen)
for shareIdx := 0; shareIdx < p.parts; shareIdx++ {
parts[shareIdx] = make([]SecretPart, splittedLen)
}
group := errgroup.Group{}
group.SetLimit(concLimit)
for _, chunk := range chunksIndex {
secrets := splitted[chunk.start:chunk.end]
chunk := chunk
group.Go(func() error {
/* create new IntContext for each goroutine since it's not safe to
use the same context concurrently */
ctx, err := big.NewIntContext()
if err != nil {
return err
}
defer ctx.Destroy()
mont, err := big.NewMontgomeryContext()
if err != nil {
return err
}
defer mont.Destroy()
if err := mont.Set(p.group.P, ctx); err != nil {
return err
}
for idx, secret := range secrets {
if secret.Cmp(p.group.Q) > 0 {
return ErrInvalidPrimeSize
}
result, err := p.split(mont, ctx, chunk.start+idx, secret, abscissae)
if err != nil {
return err
}
for shareIdx := 0; shareIdx < p.parts; shareIdx++ {
parts[shareIdx][result.index] = result.secretParts[shareIdx]
}
commitments[result.index] = result.secretComm
}
return nil
})
}
err = group.Wait()
if err != nil {
return nil, err
}
return &Shares{
Abscissae: abscissae,
Parts: parts,
Commitments: commitments,
}, nil
}