-
Notifications
You must be signed in to change notification settings - Fork 2
/
sharing.go
196 lines (157 loc) · 5.2 KB
/
sharing.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
// SPDX-License-Identifier: MIT
//
// Copyright (C) 2024 Daniel Bourdrez. All Rights Reserved.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree or at
// https://spdx.org/licenses/MIT.html
// Package secretsharing provides Shamir Secret Sharing operations.
package secretsharing
import (
"errors"
group "github.com/bytemare/crypto"
"github.com/bytemare/secret-sharing/keys"
)
var (
errThresholdIsZero = errors.New("threshold is zero")
errNoShares = errors.New("no shares provided")
errSecretIsZero = errors.New("the provided secret is zero")
errTooFewShares = errors.New("number of shares must be equal or greater than the threshold")
errPolyIsWrongSize = errors.New("invalid number of coefficients in polynomial")
errPolySecretNotSet = errors.New("provided polynomial's first coefficient not set to the secret")
errMultiGroup = errors.New("incompatible EC groups found in set of key shares")
)
func makeKeyShare(g group.Group, id uint16, p Polynomial, groupPublicKey *group.Element) *keys.KeyShare {
ids := g.NewScalar().SetUInt64(uint64(id))
yi := p.Evaluate(ids)
return &keys.KeyShare{
Secret: yi,
GroupPublicKey: groupPublicKey,
PublicKeyShare: keys.PublicKeyShare{
PublicKey: g.Base().Multiply(yi),
VssCommitment: nil,
ID: id,
Group: g,
},
}
}
// Shard splits the secret into max shares, recoverable by a subset of threshold shares. If no secret is provided, a
// new random secret is created. To use Verifiable Secret Sharing, use ShardAndCommit.
func Shard(
g group.Group,
secret *group.Scalar,
threshold, max uint16,
polynomial ...*group.Scalar,
) ([]*keys.KeyShare, error) {
shares, p, err := ShardReturnPolynomial(g, secret, threshold, max, polynomial...)
for _, pi := range p {
pi.Zero() // zero-out the polynomial, just to be sure.
}
return shares, err
}
// ShardAndCommit does the same as Shard but populates the returned key shares with the VssCommitment to the polynomial.
// If no secret is provided, a new random secret is created.
func ShardAndCommit(g group.Group,
secret *group.Scalar,
threshold, max uint16,
polynomial ...*group.Scalar,
) ([]*keys.KeyShare, error) {
shares, p, err := ShardReturnPolynomial(g, secret, threshold, max, polynomial...)
if err != nil {
return nil, err
}
commitment := Commit(g, p)
for _, share := range shares {
share.VssCommitment = commitment
}
for _, pi := range p {
pi.Zero() // zero-out the polynomial, just to be sure.
}
return shares, nil
}
// ShardReturnPolynomial splits the secret into max shares, recoverable by a subset of threshold shares, and returns
// the constructed secret polynomial without committing to it. If no secret is provided, a new random secret is created.
// Use the Commit function if you want to commit to the returned polynomial.
func ShardReturnPolynomial(
g group.Group,
secret *group.Scalar,
threshold, max uint16,
polynomial ...*group.Scalar,
) ([]*keys.KeyShare, Polynomial, error) {
if max < threshold {
return nil, nil, errTooFewShares
}
p, err := makePolynomial(g, secret, threshold, polynomial...)
if err != nil {
return nil, nil, err
}
groupPublicKey := g.Base().Multiply(p[0])
// Evaluate the polynomial for each point x=1,...,n
secretKeyShares := make([]*keys.KeyShare, max)
for i := uint16(1); i <= max; i++ {
secretKeyShares[i-1] = makeKeyShare(g, i, p, groupPublicKey)
}
return secretKeyShares, p, nil
}
// RecoverFromKeyShares recovers the constant secret by combining the key shares.
func RecoverFromKeyShares(keyShares []*keys.KeyShare) (*group.Scalar, error) {
s := make([]keys.Share, len(keyShares))
for i, ks := range keyShares {
s[i] = ks
}
return CombineShares(s)
}
// CombineShares recovers the sharded secret by combining the key shares that implement the Share interface. It recovers
// the constant term of the interpolating polynomial defined by the set of key shares.
func CombineShares(shares []keys.Share) (*group.Scalar, error) {
if len(shares) == 0 {
return nil, errNoShares
}
g := shares[0].Group()
xCoords := NewPolynomialFromListFunc(g, shares, func(share keys.Share) *group.Scalar {
return g.NewScalar().SetUInt64(uint64(share.Identifier()))
})
key := g.NewScalar().Zero()
for i, share := range shares {
if share.Group() != g {
return nil, errMultiGroup
}
iv, err := xCoords.DeriveInterpolatingValue(g, xCoords[i])
if err != nil {
return nil, err
}
delta := iv.Multiply(share.SecretKey())
key.Add(delta)
}
return key, nil
}
func makePolynomial(g group.Group, s *group.Scalar, threshold uint16, polynomial ...*group.Scalar) (Polynomial, error) {
if threshold == 0 {
return nil, errThresholdIsZero
}
if s != nil && s.IsZero() {
return nil, errSecretIsZero
}
p := NewPolynomial(threshold)
switch len(polynomial) {
case 0:
i := uint16(0)
if s != nil {
p[0] = s.Copy()
i++
}
for ; i < threshold; i++ {
p[i] = g.NewScalar().Random()
}
case int(threshold):
if s != nil && polynomial[0].Equal(s) != 1 {
return nil, errPolySecretNotSet
}
if err := copyPolynomial(p, polynomial); err != nil {
return nil, err
}
default:
return nil, errPolyIsWrongSize
}
return p, nil
}