/
keys.go
393 lines (344 loc) 路 9.77 KB
/
keys.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package key
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"net"
kyber "github.com/drand/kyber"
"github.com/drand/kyber/share"
dkg "github.com/drand/kyber/share/dkg"
"github.com/drand/kyber/util/random"
proto "github.com/drand/drand/protobuf/drand"
)
// Pair is a wrapper around a random scalar and the corresponding public
// key in G2
type Pair struct {
Key kyber.Scalar
Public *Identity
}
// Identity holds the corresponding public key of a Private. It also includes a
// valid internet facing ipv4 address where to this reach the node holding the
// public / private key pair.
type Identity struct {
Key kyber.Point
Addr string
TLS bool
Signature []byte
}
// Address implements the net.Peer interface
func (i *Identity) Address() string {
return i.Addr
}
// IsTLS returns true if this address is reachable over TLS.
func (i *Identity) IsTLS() bool {
return i.TLS
}
func (i *Identity) String() string {
return fmt.Sprintf("{%s - %s}", i.Address(), i.Key.String())
}
// Hash returns the hash of the public key without signing the signature. The hash
// is the input to the signature scheme. It does _not_ hash the address & tls
// field as those may need to change while the node keeps the same key.
func (i *Identity) Hash() []byte {
h := hashFunc()
_, _ = i.Key.MarshalTo(h)
return h.Sum(nil)
}
// ValidSignature returns true if the signature included in this identity is
// correct or not
func (i *Identity) ValidSignature() error {
msg := i.Hash()
return AuthScheme.Verify(i.Key, msg, i.Signature)
}
// Equal indicates if two identities are equal
func (i *Identity) Equal(i2 *Identity) bool {
if i.Addr != i2.Addr {
return false
}
if i.TLS != i2.TLS {
return false
}
if !i.Key.Equal(i2.Key) {
return false
}
return true
}
// SelfSign signs the public key with the key pair
func (p *Pair) SelfSign() {
msg := p.Public.Hash()
signature, _ := AuthScheme.Sign(p.Key, msg)
p.Public.Signature = signature
}
// NewKeyPair returns a freshly created private / public key pair. The group is
// decided by the group variable by default. Currently, drand only supports
// bn256.
func NewKeyPair(address string) *Pair {
key := KeyGroup.Scalar().Pick(random.New())
pubKey := KeyGroup.Point().Mul(key, nil)
pub := &Identity{
Key: pubKey,
Addr: address,
}
p := &Pair{
Key: key,
Public: pub,
}
p.SelfSign()
return p
}
// NewTLSKeyPair returns a fresh keypair associated with the given address
// reachable over TLS.
func NewTLSKeyPair(address string) *Pair {
kp := NewKeyPair(address)
kp.Public.TLS = true
kp.SelfSign()
return kp
}
// PairTOML is the TOML-able version of a private key
type PairTOML struct {
Key string
}
// PublicTOML is the TOML-able version of a public key
type PublicTOML struct {
Address string
Key string
TLS bool
Signature string
}
// TOML returns a struct that can be marshaled using a TOML-encoding library
func (p *Pair) TOML() interface{} {
hexKey := ScalarToString(p.Key)
return &PairTOML{hexKey}
}
// FromTOML constructs the private key from an unmarshalled structure from TOML
func (p *Pair) FromTOML(i interface{}) error {
ptoml, ok := i.(*PairTOML)
if !ok {
return errors.New("private can't decode toml from non PairTOML struct")
}
var err error
p.Key, err = StringToScalar(KeyGroup, ptoml.Key)
p.Public = new(Identity)
return err
}
// TOMLValue returns an empty TOML-compatible interface value
func (p *Pair) TOMLValue() interface{} {
return &PairTOML{}
}
// FromTOML loads reads the TOML description of the public key
func (i *Identity) FromTOML(t interface{}) error {
ptoml, ok := t.(*PublicTOML)
if !ok {
return errors.New("public can't decode from non PublicTOML struct")
}
var err error
i.Key, err = StringToPoint(KeyGroup, ptoml.Key)
if err != nil {
return fmt.Errorf("decoding public key: %s", err)
}
i.Addr = ptoml.Address
i.TLS = ptoml.TLS
if ptoml.Signature != "" {
i.Signature, err = hex.DecodeString(ptoml.Signature)
}
return err
}
// TOML returns a empty TOML-compatible version of the public key
func (i *Identity) TOML() interface{} {
hexKey := PointToString(i.Key)
return &PublicTOML{
Address: i.Addr,
Key: hexKey,
TLS: i.TLS,
Signature: hex.EncodeToString(i.Signature),
}
}
// TOMLValue returns a TOML-compatible interface value
func (i *Identity) TOMLValue() interface{} {
return &PublicTOML{}
}
// ByKey is simply an interface to sort lexig
type ByKey []*Identity
func (b ByKey) Len() int {
return len(b)
}
func (b ByKey) Swap(i, j int) {
(b)[i], (b)[j] = (b)[j], (b)[i]
}
func (b ByKey) Less(i, j int) bool {
is, _ := (b)[i].Key.MarshalBinary()
js, _ := (b)[j].Key.MarshalBinary()
return bytes.Compare(is, js) < 0
}
// IdentityFromProto creates an identity from its wire representation and
// verifies it validity.
func IdentityFromProto(n *proto.Identity) (*Identity, error) {
_, _, err := net.SplitHostPort(n.GetAddress())
if err != nil {
return nil, err
}
public := KeyGroup.Point()
if err := public.UnmarshalBinary(n.GetKey()); err != nil {
return nil, err
}
id := &Identity{
Addr: n.GetAddress(),
TLS: n.Tls,
Key: public,
Signature: n.GetSignature(),
}
return id, nil
}
// ToProto marshals an identity into protobuf format
func (i *Identity) ToProto() *proto.Identity {
buff, _ := i.Key.MarshalBinary()
return &proto.Identity{
Address: i.Addr,
Key: buff,
Tls: i.TLS,
Signature: i.Signature,
}
}
// Share represents the private information that a node holds after a successful
// DKG. This information MUST stay private !
type Share dkg.DistKeyShare
// PubPoly returns the public polynomial that can be used to verify any
// individual patial signature
func (s *Share) PubPoly() *share.PubPoly {
return share.NewPubPoly(KeyGroup, KeyGroup.Point().Base(), s.Commits)
}
// PrivateShare returns the private share used to produce a partial signature
func (s *Share) PrivateShare() *share.PriShare {
return s.Share
}
// Public returns the distributed public key associated with the distributed key
// share
func (s *Share) Public() *DistPublic {
return &DistPublic{s.Commits}
}
// TOML returns a TOML-compatible version of this share
func (s *Share) TOML() interface{} {
dtoml := &ShareTOML{}
dtoml.Commits = make([]string, len(s.Commits))
for i, c := range s.Commits {
dtoml.Commits[i] = PointToString(c)
}
dtoml.Share = ScalarToString(s.Share.V)
dtoml.Index = s.Share.I
return dtoml
}
// FromTOML initializes the share from the given TOML-compatible share interface
func (s *Share) FromTOML(i interface{}) error {
t, ok := i.(*ShareTOML)
if !ok {
return errors.New("invalid struct received for share")
}
s.Commits = make([]kyber.Point, len(t.Commits))
for i, c := range t.Commits {
p, err := StringToPoint(KeyGroup, c)
if err != nil {
return fmt.Errorf("share.Commit[%d] corruputed: %s", i, err)
}
s.Commits[i] = p
}
sshare, err := StringToScalar(KeyGroup, t.Share)
if err != nil {
return fmt.Errorf("share.Share corrupted: %s", err)
}
s.Share = &share.PriShare{V: sshare, I: t.Index}
return nil
}
// TOMLValue returns an empty TOML compatible interface of that Share
func (s *Share) TOMLValue() interface{} {
return &ShareTOML{}
}
// ShareTOML is the TOML representation of a dkg.DistKeyShare
type ShareTOML struct {
// index of the share.
Index int
// evaluation of the private polynomial.
Share string
// coefficients of the public polynomial.
Commits []string
// coefficients of the individual private polynomial generated by the node
// at the given index.
PrivatePoly []string
}
// DistPublic represents the distributed public key generated during a DKG. This
// is the information that can be safely exported to end users verifying a
// drand signature. It is the list of all commitments of the coefficients of the
// private distributed polynomial.
type DistPublic struct {
Coefficients []kyber.Point
}
// PubPoly provides the public polynomial commitment
func (d *DistPublic) PubPoly() *share.PubPoly {
return share.NewPubPoly(KeyGroup, KeyGroup.Point().Base(), d.Coefficients)
}
// Key returns the first coefficient as representing the public key to be used
// to verify signatures issued by the distributed key.
func (d *DistPublic) Key() kyber.Point {
return d.Coefficients[0]
}
// Hash computes the hash of this distributed key.
func (d *DistPublic) Hash() []byte {
h := hashFunc()
for _, c := range d.Coefficients {
buff, _ := c.MarshalBinary()
_, _ = h.Write(buff)
}
return h.Sum(nil)
}
// DistPublicTOML is a TOML compatible value of a DistPublic
type DistPublicTOML struct {
Coefficients []string
}
// TOML returns a TOML-compatible version of d
func (d *DistPublic) TOML() interface{} {
strings := make([]string, len(d.Coefficients))
for i, s := range d.Coefficients {
strings[i] = PointToString(s)
}
return &DistPublicTOML{strings}
}
// FromTOML initializes d from the TOML-compatible version of a DistPublic
func (d *DistPublic) FromTOML(i interface{}) error {
dtoml, ok := i.(*DistPublicTOML)
if !ok {
return errors.New("wrong interface: expected DistPublicTOML")
}
points := make([]kyber.Point, len(dtoml.Coefficients))
var err error
for i, s := range dtoml.Coefficients {
points[i], err = StringToPoint(KeyGroup, s)
if err != nil {
return err
}
}
d.Coefficients = points
return nil
}
// TOMLValue returns an empty TOML-compatible dist public interface
func (d *DistPublic) TOMLValue() interface{} {
return &DistPublicTOML{}
}
// Equal returns if all coefficients of the public key d are equal to those of
// d2
func (d *DistPublic) Equal(d2 *DistPublic) bool {
if len(d.Coefficients) != len(d2.Coefficients) {
return false
}
for i := range d.Coefficients {
p1 := d.Coefficients[i]
p2 := d2.Coefficients[i]
if !p1.Equal(p2) {
return false
}
}
return true
}
// DefaultThreshold return floor(n / 2) + 1
func DefaultThreshold(n int) int {
return MinimumT(n)
}