-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine.go
165 lines (131 loc) · 3.03 KB
/
combine.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
// 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 (
"github.com/matteoarella/pedersen/big"
"golang.org/x/sync/errgroup"
)
type combineValue struct {
index int
value *big.Int
}
func (p *Pedersen) combine(
ctx *big.IntContext,
index int,
abscissae []*big.Int,
parts []SecretPart,
) (combineValue, error) {
var (
xSamples []*big.Int
ySamples []*big.Int
)
for idx, p := range parts {
if (SecretPart{}) == p {
continue
}
ySamples = append(ySamples, p.SShare)
xSamples = append(xSamples, abscissae[idx])
}
ctx.Attach()
defer ctx.Detach()
zero, err := ctx.GetInt()
if err != nil {
return combineValue{}, err
}
if err := zero.SetUInt64(0); err != nil {
return combineValue{}, err
}
secret, err := interpolatePolynomial(ctx, xSamples, ySamples, zero, p.group.Q)
if err != nil {
return combineValue{}, err
}
return combineValue{
index: index,
value: secret,
}, nil
}
func bigIntUnpadding(ctx *big.IntContext, n *big.Int) ([]byte, error) {
ctx.Attach()
defer ctx.Detach()
// extract leading zeros info from trailing bytes
mask, err := ctx.GetInt()
if err != nil {
return nil, err
}
if err := mask.SetUInt64(0x00000000FFFFFFFF); err != nil {
return nil, err
}
if err := mask.And(n, mask); err != nil {
return nil, err
}
zeros := mask.Uint64()
// cut trailer
if err := n.Rsh(n, zerosInfoSizeBytes*8); err != nil {
return nil, err
}
res := make([]byte, zeros)
for i := 0; i < int(zeros); i++ {
res[i] = 0
}
nBytes, err := n.Bytes()
if err != nil {
return nil, err
}
return append(res, nBytes...), nil // nozero
}
// Combine combines the secret shares into the original secret.
func (p *Pedersen) Combine(shares *Shares) ([]byte, error) {
err := p.validateShares(shares)
if err != nil {
return nil, err
}
splittedLen := len(shares.Parts[0])
values := make([]*big.Int, splittedLen)
concLimit := p.adjustConcLimit(splittedLen)
chunksIndex := p.balanceIndices(splittedLen, concLimit)
group := errgroup.Group{}
group.SetLimit(concLimit)
for _, chunk := range chunksIndex {
chunk := chunk
group.Go(func() error {
ctx, err := big.NewIntContext()
if err != nil {
return err
}
defer ctx.Destroy()
parts := make([]SecretPart, p.parts)
for idx := chunk.start; idx < chunk.end; idx++ {
for shareIdx := 0; shareIdx < p.parts; shareIdx++ {
parts[shareIdx] = shares.Parts[shareIdx][idx]
}
value, err := p.combine(ctx, idx, shares.Abscissae, parts)
if err != nil {
return err
}
values[value.index] = value.value
}
return nil
})
}
err = group.Wait()
if err != nil {
return nil, err
}
var res []byte
ctx, err := big.NewIntContext()
if err != nil {
return nil, err
}
defer ctx.Destroy()
for i := 0; i < splittedLen; i++ {
chunk, err := bigIntUnpadding(ctx, values[i])
if err != nil {
return nil, err
}
res = append(res, chunk...)
}
return res, nil
}