-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathbls.go
151 lines (119 loc) · 3.24 KB
/
bls.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
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package auth
import (
"context"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/crypto"
"github.com/ava-labs/hypersdk/crypto/bls"
"github.com/ava-labs/hypersdk/utils"
)
var _ chain.Auth = (*BLS)(nil)
const (
BLSComputeUnits = 10
BLSSize = bls.PublicKeyLen + bls.SignatureLen
)
type BLS struct {
Signer *bls.PublicKey `json:"signer,omitempty"`
Signature *bls.Signature `json:"signature,omitempty"`
addr codec.Address
}
func (b *BLS) address() codec.Address {
if b.addr == codec.EmptyAddress {
b.addr = NewBLSAddress(b.Signer)
}
return b.addr
}
func (*BLS) GetTypeID() uint8 {
return BLSID
}
func (*BLS) ComputeUnits(chain.Rules) uint64 {
return BLSComputeUnits
}
func (*BLS) ValidRange(chain.Rules) (int64, int64) {
return -1, -1
}
func (b *BLS) Verify(_ context.Context, msg []byte) error {
if !bls.Verify(msg, b.Signer, b.Signature) {
return crypto.ErrInvalidSignature
}
return nil
}
func (b *BLS) Actor() codec.Address {
return b.address()
}
func (b *BLS) Sponsor() codec.Address {
return b.address()
}
func (*BLS) Size() int {
return BLSSize
}
func (b *BLS) Marshal(p *codec.Packer) {
p.PackFixedBytes(bls.PublicKeyToBytes(b.Signer))
p.PackFixedBytes(bls.SignatureToBytes(b.Signature))
}
func UnmarshalBLS(p *codec.Packer) (chain.Auth, error) {
var b BLS
signer := make([]byte, bls.PublicKeyLen)
p.UnpackFixedBytes(bls.PublicKeyLen, &signer)
signature := make([]byte, bls.SignatureLen)
p.UnpackFixedBytes(bls.SignatureLen, &signature)
pk, err := bls.PublicKeyFromBytes(signer)
if err != nil {
return nil, err
}
b.Signer = pk
sig, err := bls.SignatureFromBytes(signature)
if err != nil {
return nil, err
}
b.Signature = sig
return &b, p.Err()
}
var _ chain.AuthFactory = (*BLSFactory)(nil)
type BLSFactory struct {
priv *bls.PrivateKey
}
func NewBLSFactory(priv *bls.PrivateKey) *BLSFactory {
return &BLSFactory{priv}
}
func (b *BLSFactory) Sign(msg []byte) (chain.Auth, error) {
return &BLS{Signer: bls.PublicFromPrivateKey(b.priv), Signature: bls.Sign(msg, b.priv)}, nil
}
func (*BLSFactory) MaxUnits() (uint64, uint64) {
return BLSSize, BLSComputeUnits
}
func (b *BLSFactory) Address() codec.Address {
return NewBLSAddress(bls.PublicFromPrivateKey(b.priv))
}
func NewBLSAddress(pk *bls.PublicKey) codec.Address {
return codec.CreateAddress(BLSID, utils.ToID(bls.PublicKeyToBytes(pk)))
}
type BLSPrivateKeyFactory struct{}
func NewBLSPrivateKeyFactory() *BLSPrivateKeyFactory {
return &BLSPrivateKeyFactory{}
}
func (*BLSPrivateKeyFactory) GeneratePrivateKey() (*PrivateKey, error) {
p, err := bls.GeneratePrivateKey()
if err != nil {
return nil, err
}
return &PrivateKey{
Address: NewBLSAddress(bls.PublicFromPrivateKey(p)),
Bytes: bls.PrivateKeyToBytes(p),
}, nil
}
func (*BLSPrivateKeyFactory) LoadPrivateKey(privateKey []byte) (*PrivateKey, error) {
if len(privateKey) != bls.PrivateKeyLen {
return nil, ErrInvalidPrivateKeySize
}
privKey, err := bls.PrivateKeyFromBytes(privateKey)
if err != nil {
return nil, err
}
return &PrivateKey{
Address: NewBLSAddress(bls.PublicFromPrivateKey(privKey)),
Bytes: privateKey,
}, nil
}