-
Notifications
You must be signed in to change notification settings - Fork 107
/
vault.go
85 lines (74 loc) 路 1.98 KB
/
vault.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
package vault
import (
"sync"
"github.com/drand/drand/chain"
"github.com/drand/drand/crypto"
"github.com/drand/drand/key"
"github.com/drand/kyber/share"
)
// CryptoSafe holds the cryptographic information to generate a partial beacon
type CryptoSafe interface {
// SignPartial returns the partial signature
SignPartial(msg []byte) ([]byte, error)
}
// Vault stores the information necessary to validate partial beacon, full
// beacons and to sign new partial beacons (it implements CryptoSafe interface).
// Vault is thread safe when using the methods.
type Vault struct {
mu sync.RWMutex
*crypto.Scheme
// current share of the node
share *key.Share
// public polynomial to verify a partial beacon
pub *share.PubPoly
// chian info to verify final random beacon
chain *chain.Info
// to know the threshold, transition time etc
group *key.Group
}
func NewVault(currentGroup *key.Group, ks *key.Share, sch *crypto.Scheme) *Vault {
return &Vault{
Scheme: sch,
chain: chain.NewChainInfo(currentGroup),
share: ks,
pub: currentGroup.PublicKey.PubPoly(sch),
group: currentGroup,
}
}
// GetGroup returns the current group
func (v *Vault) GetGroup() *key.Group {
v.mu.RLock()
defer v.mu.RUnlock()
return v.group
}
func (v *Vault) GetPub() *share.PubPoly {
v.mu.RLock()
defer v.mu.RUnlock()
return v.pub
}
func (v *Vault) GetInfo() *chain.Info {
v.mu.RLock()
defer v.mu.RUnlock()
return v.chain
}
// SignPartial implemements the CryptoSafe interface
func (v *Vault) SignPartial(msg []byte) ([]byte, error) {
v.mu.RLock()
defer v.mu.RUnlock()
return v.Scheme.ThresholdScheme.Sign(v.share.PrivateShare(), msg)
}
// Index returns the index of the share
func (v *Vault) Index() int {
v.mu.RLock()
defer v.mu.RUnlock()
return v.share.Share.I
}
func (v *Vault) SetInfo(newGroup *key.Group, ks *key.Share) {
v.mu.Lock()
defer v.mu.Unlock()
v.share = ks
v.group = newGroup
v.pub = newGroup.PublicKey.PubPoly(v.Scheme)
// v.chain info is constant
// Scheme cannot change either
}