forked from mapprotocol/atlas
/
public_key.go
126 lines (111 loc) · 3.77 KB
/
public_key.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
//go:build ((linux && amd64) || (linux && arm64) || (darwin && amd64) || (darwin && arm64) || (windows && amd64)) && !blst_disabled
// +build linux,amd64 linux,arm64 darwin,amd64 darwin,arm64 windows,amd64
// +build !blst_disabled
package blst
import (
"fmt"
"github.com/Alexfordev/atlas/chains/eth2/bls12381/common"
lru "github.com/hashicorp/golang-lru"
"github.com/pkg/errors"
)
var maxKeys = 1000000
var pubkeyCache *lru.Cache
// PublicKey used in the BLS signature scheme.
type PublicKey struct {
p *blstPublicKey
}
func init() {
cache, err := lru.New(maxKeys)
if err != nil {
panic(fmt.Errorf("lru new failed: %w", err))
}
pubkeyCache = cache
}
// PublicKeyFromBytes creates a BLS public key from a BigEndian byte slice.
func PublicKeyFromBytes(pubKey []byte) (common.PublicKey, error) {
if len(pubKey) != common.BLSPubkeyLength {
return nil, fmt.Errorf("public key must be %d bytes", common.BLSPubkeyLength)
}
var newKey [common.BLSPubkeyLength]byte
copy(newKey[:], pubKey)
// newKey := (*[common.BLSPubkeyLength]byte)(pubKey)
if cv, ok := pubkeyCache.Get(newKey); ok {
return cv.(*PublicKey).Copy(), nil
}
// Subgroup check NOT done when decompressing pubkey.
p := new(blstPublicKey).Uncompress(pubKey)
if p == nil {
return nil, errors.New("could not unmarshal bytes into public key")
}
// Subgroup and infinity check
if !p.KeyValidate() {
// NOTE: the error is not quite accurate since it includes group check
return nil, common.ErrInfinitePubKey
}
pubKeyObj := &PublicKey{p: p}
copiedKey := pubKeyObj.Copy()
cacheKey := newKey
pubkeyCache.Add(cacheKey, copiedKey)
return pubKeyObj, nil
}
// AggregatePublicKeys aggregates the provided raw public keys into a single key.
func AggregatePublicKeys(pubs [][]byte) (common.PublicKey, error) {
if len(pubs) == 0 {
return nil, errors.New("nil or empty public keys")
}
agg := new(blstAggregatePublicKey)
mulP1 := make([]*blstPublicKey, 0, len(pubs))
for _, pubkey := range pubs {
pubKeyObj, err := PublicKeyFromBytes(pubkey)
if err != nil {
return nil, err
}
mulP1 = append(mulP1, pubKeyObj.(*PublicKey).p)
}
// No group check needed here since it is done in PublicKeyFromBytes
// Note the checks could be moved from PublicKeyFromBytes into Aggregate
// and take advantage of multi-threading.
agg.Aggregate(mulP1, false)
return &PublicKey{p: agg.ToAffine()}, nil
}
// Marshal a public key into a LittleEndian byte slice.
func (p *PublicKey) Marshal() []byte {
return p.p.Compress()
}
// Copy the public key to a new pointer reference.
func (p *PublicKey) Copy() common.PublicKey {
np := *p.p
return &PublicKey{p: &np}
}
// IsInfinite checks if the public key is infinite.
func (p *PublicKey) IsInfinite() bool {
zeroKey := new(blstPublicKey)
return p.p.Equals(zeroKey)
}
// Equals checks if the provided public key is equal to
// the current one.
func (p *PublicKey) Equals(p2 common.PublicKey) bool {
return p.p.Equals(p2.(*PublicKey).p)
}
// Aggregate two public keys.
func (p *PublicKey) Aggregate(p2 common.PublicKey) common.PublicKey {
agg := new(blstAggregatePublicKey)
// No group check here since it is checked at decompression time
agg.Add(p.p, false)
agg.Add(p2.(*PublicKey).p, false)
p.p = agg.ToAffine()
return p
}
// AggregateMultiplePubkeys aggregates the provided decompressed keys into a single key.
func AggregateMultiplePubkeys(pubkeys []common.PublicKey) common.PublicKey {
mulP1 := make([]*blstPublicKey, 0, len(pubkeys))
for _, pubkey := range pubkeys {
mulP1 = append(mulP1, pubkey.(*PublicKey).p)
}
agg := new(blstAggregatePublicKey)
// No group check needed here since it is done in PublicKeyFromBytes
// Note the checks could be moved from PublicKeyFromBytes into Aggregate
// and take advantage of multi-threading.
agg.Aggregate(mulP1, false)
return &PublicKey{p: agg.ToAffine()}
}