-
Notifications
You must be signed in to change notification settings - Fork 202
/
headerSignatureVerify.go
264 lines (220 loc) · 7.23 KB
/
headerSignatureVerify.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
package headerCheck
import (
"math/bits"
"github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/crypto"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/hashing"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
)
var _ process.InterceptedHeaderSigVerifier = (*HeaderSigVerifier)(nil)
var log = logger.GetOrCreate("process/headerCheck")
// ArgsHeaderSigVerifier is used to store all components that are needed to create a new HeaderSigVerifier
type ArgsHeaderSigVerifier struct {
Marshalizer marshal.Marshalizer
Hasher hashing.Hasher
NodesCoordinator sharding.NodesCoordinator
MultiSigVerifier crypto.MultiSigVerifier
SingleSigVerifier crypto.SingleSigner
KeyGen crypto.KeyGenerator
}
//HeaderSigVerifier is component used to check if a header is valid
type HeaderSigVerifier struct {
marshalizer marshal.Marshalizer
hasher hashing.Hasher
nodesCoordinator sharding.NodesCoordinator
multiSigVerifier crypto.MultiSigVerifier
singleSigVerifier crypto.SingleSigner
keyGen crypto.KeyGenerator
}
// NewHeaderSigVerifier will create a new instance of HeaderSigVerifier
func NewHeaderSigVerifier(arguments *ArgsHeaderSigVerifier) (*HeaderSigVerifier, error) {
err := checkArgsHeaderSigVerifier(arguments)
if err != nil {
return nil, err
}
return &HeaderSigVerifier{
marshalizer: arguments.Marshalizer,
hasher: arguments.Hasher,
nodesCoordinator: arguments.NodesCoordinator,
multiSigVerifier: arguments.MultiSigVerifier,
singleSigVerifier: arguments.SingleSigVerifier,
keyGen: arguments.KeyGen,
}, nil
}
func checkArgsHeaderSigVerifier(arguments *ArgsHeaderSigVerifier) error {
if arguments == nil {
return process.ErrNilArgumentStruct
}
if check.IfNil(arguments.Hasher) {
return process.ErrNilHasher
}
if check.IfNil(arguments.KeyGen) {
return process.ErrNilKeyGen
}
if check.IfNil(arguments.Marshalizer) {
return process.ErrNilMarshalizer
}
if check.IfNil(arguments.MultiSigVerifier) {
return process.ErrNilMultiSigVerifier
}
if check.IfNil(arguments.NodesCoordinator) {
return process.ErrNilNodesCoordinator
}
if check.IfNil(arguments.SingleSigVerifier) {
return process.ErrNilSingleSigner
}
return nil
}
// VerifySignature will check if signature is correct
func (hsv *HeaderSigVerifier) VerifySignature(header data.HeaderHandler) error {
randSeed := header.GetPrevRandSeed()
bitmap := header.GetPubKeysBitmap()
if len(bitmap) == 0 {
return process.ErrNilPubKeysBitmap
}
if bitmap[0]&1 == 0 {
return process.ErrBlockProposerSignatureMissing
}
// TODO: remove if start of epoch block needs to be validated by the new epoch nodes
epoch := header.GetEpoch()
if header.IsStartOfEpochBlock() && epoch > 0 {
epoch = epoch - 1
}
consensusPubKeys, err := hsv.nodesCoordinator.GetConsensusValidatorsPublicKeys(
randSeed,
header.GetRound(),
header.GetShardID(),
epoch,
)
if err != nil {
return err
}
err = hsv.verifyConsensusSize(consensusPubKeys, header)
if err != nil {
return err
}
verifier, err := hsv.multiSigVerifier.Create(consensusPubKeys, 0)
if err != nil {
return err
}
err = verifier.SetAggregatedSig(header.GetSignature())
if err != nil {
return err
}
// get marshalled block header without signature and bitmap
// as this is the message that was signed
headerCopy := hsv.copyHeaderWithoutSig(header)
hash, err := core.CalculateHash(hsv.marshalizer, hsv.hasher, headerCopy)
if err != nil {
return err
}
return verifier.Verify(hash, bitmap)
}
func (hsv *HeaderSigVerifier) verifyConsensusSize(consensusPubKeys []string, header data.HeaderHandler) error {
consensusSize := len(consensusPubKeys)
bitmap := header.GetPubKeysBitmap()
expectedBitmapSize := consensusSize / 8
if consensusSize%8 != 0 {
expectedBitmapSize++
}
if len(bitmap) != expectedBitmapSize {
log.Debug("wrong size bitmap",
"expected number of bytes", expectedBitmapSize,
"actual", len(bitmap))
return ErrWrongSizeBitmap
}
numOfOnesInBitmap := 0
for index := range bitmap {
numOfOnesInBitmap += bits.OnesCount8(bitmap[index])
}
minNumRequiredSignatures := consensusSize*2/3 + 1
if numOfOnesInBitmap >= minNumRequiredSignatures {
return nil
}
log.Debug("not enough signatures",
"minimum expected", minNumRequiredSignatures,
"actual", numOfOnesInBitmap)
return ErrNotEnoughSignatures
}
// VerifyRandSeed will check if rand seed is correct
func (hsv *HeaderSigVerifier) VerifyRandSeed(header data.HeaderHandler) error {
leaderPubKey, err := hsv.getLeader(header)
if err != nil {
return err
}
err = hsv.verifyRandSeed(leaderPubKey, header)
if err != nil {
log.Trace("block rand seed",
"error", err.Error())
return err
}
return nil
}
// VerifyRandSeedAndLeaderSignature will check if rand seed and leader signature is correct
func (hsv *HeaderSigVerifier) VerifyRandSeedAndLeaderSignature(header data.HeaderHandler) error {
leaderPubKey, err := hsv.getLeader(header)
if err != nil {
return err
}
err = hsv.verifyRandSeed(leaderPubKey, header)
if err != nil {
log.Trace("block rand seed",
"error", err.Error())
return err
}
err = hsv.verifyLeaderSignature(leaderPubKey, header)
if err != nil {
log.Trace("block leader's signature",
"error", err.Error())
return err
}
return nil
}
// IsInterfaceNil will check if interface is nil
func (hsv *HeaderSigVerifier) IsInterfaceNil() bool {
return hsv == nil
}
func (hsv *HeaderSigVerifier) verifyRandSeed(leaderPubKey crypto.PublicKey, header data.HeaderHandler) error {
prevRandSeed := header.GetPrevRandSeed()
randSeed := header.GetRandSeed()
return hsv.singleSigVerifier.Verify(leaderPubKey, prevRandSeed, randSeed)
}
func (hsv *HeaderSigVerifier) verifyLeaderSignature(leaderPubKey crypto.PublicKey, header data.HeaderHandler) error {
headerCopy := hsv.copyHeaderWithoutLeaderSig(header)
headerBytes, err := hsv.marshalizer.Marshal(headerCopy)
if err != nil {
return err
}
return hsv.singleSigVerifier.Verify(leaderPubKey, headerBytes, header.GetLeaderSignature())
}
func (hsv *HeaderSigVerifier) getLeader(header data.HeaderHandler) (crypto.PublicKey, error) {
prevRandSeed := header.GetPrevRandSeed()
// TODO: remove if start of epoch block needs to be validated by the new epoch nodes
epoch := header.GetEpoch()
if header.IsStartOfEpochBlock() && epoch > 0 {
epoch = epoch - 1
}
headerConsensusGroup, err := hsv.nodesCoordinator.ComputeConsensusGroup(prevRandSeed, header.GetRound(), header.GetShardID(), epoch)
if err != nil {
return nil, err
}
leaderPubKeyValidator := headerConsensusGroup[0]
return hsv.keyGen.PublicKeyFromByteArray(leaderPubKeyValidator.PubKey())
}
func (hsv *HeaderSigVerifier) copyHeaderWithoutSig(header data.HeaderHandler) data.HeaderHandler {
headerCopy := header.Clone()
headerCopy.SetSignature(nil)
headerCopy.SetPubKeysBitmap(nil)
headerCopy.SetLeaderSignature(nil)
return headerCopy
}
func (hsv *HeaderSigVerifier) copyHeaderWithoutLeaderSig(header data.HeaderHandler) data.HeaderHandler {
headerCopy := header.Clone()
headerCopy.SetLeaderSignature(nil)
return headerCopy
}