-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathsecp256r1.go
138 lines (110 loc) · 3.25 KB
/
secp256r1.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
// 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/secp256r1"
"github.com/ava-labs/hypersdk/utils"
)
var _ chain.Auth = (*SECP256R1)(nil)
const (
SECP256R1ComputeUnits = 10 // can't be batched like ed25519
SECP256R1Size = secp256r1.PublicKeyLen + secp256r1.SignatureLen
)
type SECP256R1 struct {
Signer secp256r1.PublicKey `json:"signer"`
Signature secp256r1.Signature `json:"signature"`
addr codec.Address
}
func (d *SECP256R1) address() codec.Address {
if d.addr == codec.EmptyAddress {
d.addr = NewSECP256R1Address(d.Signer)
}
return d.addr
}
func (*SECP256R1) GetTypeID() uint8 {
return SECP256R1ID
}
func (*SECP256R1) ComputeUnits(chain.Rules) uint64 {
return SECP256R1ComputeUnits
}
func (*SECP256R1) ValidRange(chain.Rules) (int64, int64) {
return -1, -1
}
func (d *SECP256R1) Verify(_ context.Context, msg []byte) error {
if !secp256r1.Verify(msg, d.Signer, d.Signature) {
return crypto.ErrInvalidSignature
}
return nil
}
func (d *SECP256R1) Actor() codec.Address {
return d.address()
}
func (d *SECP256R1) Sponsor() codec.Address {
return d.address()
}
func (*SECP256R1) Size() int {
return SECP256R1Size
}
func (d *SECP256R1) Marshal(p *codec.Packer) {
p.PackFixedBytes(d.Signer[:])
p.PackFixedBytes(d.Signature[:])
}
func UnmarshalSECP256R1(p *codec.Packer) (chain.Auth, error) {
var d SECP256R1
signer := d.Signer[:] // avoid allocating additional memory
p.UnpackFixedBytes(secp256r1.PublicKeyLen, &signer)
signature := d.Signature[:] // avoid allocating additional memory
p.UnpackFixedBytes(secp256r1.SignatureLen, &signature)
return &d, p.Err()
}
var _ chain.AuthFactory = (*SECP256R1Factory)(nil)
type SECP256R1Factory struct {
priv secp256r1.PrivateKey
}
func NewSECP256R1Factory(priv secp256r1.PrivateKey) *SECP256R1Factory {
return &SECP256R1Factory{priv}
}
func (d *SECP256R1Factory) Sign(msg []byte) (chain.Auth, error) {
sig, err := secp256r1.Sign(msg, d.priv)
if err != nil {
return nil, err
}
return &SECP256R1{Signer: d.priv.PublicKey(), Signature: sig}, nil
}
func (*SECP256R1Factory) MaxUnits() (uint64, uint64) {
return SECP256R1Size, SECP256R1ComputeUnits
}
func (d *SECP256R1Factory) Address() codec.Address {
return NewSECP256R1Address(d.priv.PublicKey())
}
func NewSECP256R1Address(pk secp256r1.PublicKey) codec.Address {
return codec.CreateAddress(SECP256R1ID, utils.ToID(pk[:]))
}
type SECP256R1PrivateKeyFactory struct{}
func NewSECP256R1PrivateKeyFactory() *SECP256R1PrivateKeyFactory {
return &SECP256R1PrivateKeyFactory{}
}
func (*SECP256R1PrivateKeyFactory) GeneratePrivateKey() (*PrivateKey, error) {
p, err := secp256r1.GeneratePrivateKey()
if err != nil {
return nil, err
}
return &PrivateKey{
Address: NewSECP256R1Address(p.PublicKey()),
Bytes: p[:],
}, nil
}
func (*SECP256R1PrivateKeyFactory) LoadPrivateKey(p []byte) (*PrivateKey, error) {
if len(p) != secp256r1.PrivateKeyLen {
return nil, ErrInvalidPrivateKeySize
}
pk := secp256r1.PrivateKey(p)
return &PrivateKey{
Address: NewSECP256R1Address(pk.PublicKey()),
Bytes: p,
}, nil
}