-
Notifications
You must be signed in to change notification settings - Fork 1
/
ed25519.go
147 lines (122 loc) · 3.21 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
package ed25519
import (
"bytes"
"errors"
"fmt"
. "github.com/33cn/chat33/pkg/btrade/crypto"
"github.com/tendermint/ed25519"
)
type Ed25519Driver struct{}
const (
TypeEd25519 = byte(0x01)
NameEd25519 = "ed25519"
)
// Crypto
func (d Ed25519Driver) GenKey() (PrivKey, error) {
privKeyBytes := new([64]byte)
copy(privKeyBytes[:32], CRandBytes(32))
ed25519.MakePublicKey(privKeyBytes)
return PrivKeyEd25519(*privKeyBytes), nil
}
func (d Ed25519Driver) PrivKeyFromBytes(b []byte) (privKey PrivKey, err error) {
if len(b) != 64 {
return nil, errors.New("invalid priv key byte")
}
privKeyBytes := new([64]byte)
copy(privKeyBytes[:32], b[:32])
ed25519.MakePublicKey(privKeyBytes)
return PrivKeyEd25519(*privKeyBytes), nil
}
func (d Ed25519Driver) PubKeyFromBytes(b []byte) (pubKey PubKey, err error) {
if len(b) != 32 {
return nil, errors.New("invalid pub key byte")
}
pubKeyBytes := new([32]byte)
copy(pubKeyBytes[:], b[:])
return PubKeyEd25519(*pubKeyBytes), nil
}
func (d Ed25519Driver) SignatureFromBytes(b []byte) (sig Signature, err error) {
sigBytes := new([64]byte)
copy(sigBytes[:], b[:])
return SignatureEd25519(*sigBytes), nil
}
// PrivKey
type PrivKeyEd25519 [64]byte
func (privKey PrivKeyEd25519) Bytes() []byte {
s := make([]byte, 64)
copy(s, privKey[:])
return s
}
func (privKey PrivKeyEd25519) Sign(msg []byte) Signature {
privKeyBytes := [64]byte(privKey)
signatureBytes := ed25519.Sign(&privKeyBytes, msg)
return SignatureEd25519(*signatureBytes)
}
func (privKey PrivKeyEd25519) PubKey() PubKey {
privKeyBytes := [64]byte(privKey)
return PubKeyEd25519(*ed25519.MakePublicKey(&privKeyBytes))
}
func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
if otherEd, ok := other.(PrivKeyEd25519); ok {
return bytes.Equal(privKey[:], otherEd[:])
} else {
return false
}
}
// PubKey
type PubKeyEd25519 [32]byte
func (pubKey PubKeyEd25519) Bytes() []byte {
s := make([]byte, 32)
copy(s, pubKey[:])
return s
}
func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
// unwrap if needed
if wrap, ok := sig_.(SignatureS); ok {
sig_ = wrap.Signature
}
// make sure we use the same algorithm to sign
sig, ok := sig_.(SignatureEd25519)
if !ok {
return false
}
pubKeyBytes := [32]byte(pubKey)
sigBytes := [64]byte(sig)
return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
}
func (pubKey PubKeyEd25519) KeyString() string {
return fmt.Sprintf("%X", pubKey[:])
}
func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
if otherEd, ok := other.(PubKeyEd25519); ok {
return bytes.Equal(pubKey[:], otherEd[:])
} else {
return false
}
}
// Signature
type SignatureEd25519 [64]byte
type SignatureS struct {
Signature
}
func (sig SignatureEd25519) Bytes() []byte {
s := make([]byte, 64)
copy(s, sig[:])
return s
}
func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
func (sig SignatureEd25519) String() string {
fingerprint := make([]byte, len(sig[:]))
copy(fingerprint, sig[:])
return fmt.Sprintf("/%X.../", fingerprint)
}
func (sig SignatureEd25519) Equals(other Signature) bool {
if otherEd, ok := other.(SignatureEd25519); ok {
return bytes.Equal(sig[:], otherEd[:])
} else {
return false
}
}
func init() {
Register(NameEd25519, &Ed25519Driver{})
}