-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvote.go
62 lines (54 loc) · 1.48 KB
/
vote.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
package main
import (
"encoding/hex"
"github.com/prysmaticlabs/prysm/crypto/bls"
"github.com/prysmaticlabs/prysm/crypto/bls/common"
)
type Vote struct {
PubKey [48]byte
Signature [96]byte
EvenType int
EventHash []byte
}
func (vote *Vote) Verify(eventHash []byte) {
blsPubKey, err := bls.PublicKeyFromBytes(vote.PubKey[:])
if err != nil {
return
}
sig, err := bls.SignatureFromBytes(vote.Signature[:])
if err != nil {
return
}
if !sig.Verify(blsPubKey, eventHash[:]) {
return
}
println("successfully verified")
}
func AggregatedSignatureV1(votes []*Vote) (common.Signature, error) {
// Prepare aggregated vote signature
voteAddrSet := make(map[string]struct{}, len(votes))
signatures := make([][]byte, 0, len(votes))
for _, v := range votes {
voteAddrSet[hex.EncodeToString(v.PubKey[:])] = struct{}{}
signatures = append(signatures, v.Signature[:])
}
sigs, err := bls.MultipleSignaturesFromBytes(signatures)
if err != nil {
return nil, err
}
return bls.AggregateSignatures(sigs), nil
}
func AggregatedSignature(votes []*Vote) ([]byte, error) {
// Prepare aggregated vote signature
voteAddrSet := make(map[string]struct{}, len(votes))
signatures := make([][]byte, 0, len(votes))
for _, v := range votes {
voteAddrSet[hex.EncodeToString(v.PubKey[:])] = struct{}{}
signatures = append(signatures, v.Signature[:])
}
sigs, err := bls.MultipleSignaturesFromBytes(signatures)
if err != nil {
return nil, err
}
return bls.AggregateSignatures(sigs).Marshal(), nil
}