-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathed25519.go
185 lines (150 loc) · 4.19 KB
/
ed25519.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
// 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/ed25519"
"github.com/ava-labs/hypersdk/utils"
)
var _ chain.Auth = (*ED25519)(nil)
const (
ED25519ComputeUnits = 5
ED25519Size = ed25519.PublicKeyLen + ed25519.SignatureLen
)
type ED25519 struct {
Signer ed25519.PublicKey `json:"signer"`
Signature ed25519.Signature `json:"signature"`
addr codec.Address
}
func (d *ED25519) address() codec.Address {
if d.addr == codec.EmptyAddress {
d.addr = NewED25519Address(d.Signer)
}
return d.addr
}
func (*ED25519) GetTypeID() uint8 {
return ED25519ID
}
func (*ED25519) ComputeUnits(chain.Rules) uint64 {
return ED25519ComputeUnits
}
func (*ED25519) ValidRange(chain.Rules) (int64, int64) {
return -1, -1
}
func (d *ED25519) Verify(_ context.Context, msg []byte) error {
if !ed25519.Verify(msg, d.Signer, d.Signature) {
return crypto.ErrInvalidSignature
}
return nil
}
func (d *ED25519) Actor() codec.Address {
return d.address()
}
func (d *ED25519) Sponsor() codec.Address {
return d.address()
}
func (*ED25519) Size() int {
return ED25519Size
}
func (d *ED25519) Marshal(p *codec.Packer) {
p.PackFixedBytes(d.Signer[:])
p.PackFixedBytes(d.Signature[:])
}
func UnmarshalED25519(p *codec.Packer) (chain.Auth, error) {
var d ED25519
signer := d.Signer[:] // avoid allocating additional memory
p.UnpackFixedBytes(ed25519.PublicKeyLen, &signer)
signature := d.Signature[:] // avoid allocating additional memory
p.UnpackFixedBytes(ed25519.SignatureLen, &signature)
return &d, p.Err()
}
var _ chain.AuthFactory = (*ED25519Factory)(nil)
func NewED25519Factory(priv ed25519.PrivateKey) *ED25519Factory {
return &ED25519Factory{priv}
}
type ED25519Factory struct {
priv ed25519.PrivateKey
}
func (d *ED25519Factory) Sign(msg []byte) (chain.Auth, error) {
sig := ed25519.Sign(msg, d.priv)
return &ED25519{Signer: d.priv.PublicKey(), Signature: sig}, nil
}
func (*ED25519Factory) MaxUnits() (uint64, uint64) {
return ED25519Size, ED25519ComputeUnits
}
func (d *ED25519Factory) Address() codec.Address {
return NewED25519Address(d.priv.PublicKey())
}
type ED25519AuthEngine struct{}
func (*ED25519AuthEngine) GetBatchVerifier(cores int, count int) chain.AuthBatchVerifier {
batchSize := max(count/cores, ed25519.MinBatchSize)
return &ED25519Batch{
batchSize: batchSize,
total: count,
}
}
func (*ED25519AuthEngine) Cache(chain.Auth) {
// TODO: add support for caching expanded public key to make batch verification faster
}
type ED25519Batch struct {
batchSize int
total int
counter int
totalCounter int
batch *ed25519.Batch
}
func (b *ED25519Batch) Add(msg []byte, rauth chain.Auth) func() error {
auth := rauth.(*ED25519)
if b.batch == nil {
b.batch = ed25519.NewBatch(b.batchSize)
}
b.batch.Add(msg, auth.Signer, auth.Signature)
b.counter++
b.totalCounter++
if b.counter == b.batchSize {
last := b.batch
b.counter = 0
if b.totalCounter < b.total {
// don't create a new batch if we are done
b.batch = ed25519.NewBatch(b.batchSize)
}
return last.VerifyAsync()
}
return nil
}
func (b *ED25519Batch) Done() []func() error {
if b.batch == nil {
return nil
}
return []func() error{b.batch.VerifyAsync()}
}
func NewED25519Address(pk ed25519.PublicKey) codec.Address {
return codec.CreateAddress(ED25519ID, utils.ToID(pk[:]))
}
type ED25519PrivateKeyFactory struct{}
func NewED25519PrivateKeyFactory() *ED25519PrivateKeyFactory {
return &ED25519PrivateKeyFactory{}
}
func (*ED25519PrivateKeyFactory) GeneratePrivateKey() (*PrivateKey, error) {
p, err := ed25519.GeneratePrivateKey()
if err != nil {
return nil, err
}
return &PrivateKey{
Address: NewED25519Address(p.PublicKey()),
Bytes: p[:],
}, nil
}
func (*ED25519PrivateKeyFactory) LoadPrivateKey(privateKey []byte) (*PrivateKey, error) {
if len(privateKey) != ed25519.PrivateKeyLen {
return nil, ErrInvalidPrivateKeySize
}
pk := ed25519.PrivateKey(privateKey)
return &PrivateKey{
Address: NewED25519Address(pk.PublicKey()),
Bytes: privateKey,
}, nil
}