@@ -10,8 +10,7 @@ import (
10
10
11
11
"github.com/cometbft/cometbft/crypto"
12
12
"github.com/cometbft/cometbft/crypto/tmhash"
13
-
14
- bls12381 "github.com/cosmos/crypto/curves/bls12381"
13
+ "github.com/cometbft/cometbft/crypto/bls12381"
15
14
16
15
"github.com/cosmos/cosmos-sdk/codec"
17
16
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
@@ -32,20 +31,20 @@ var (
32
31
33
32
// NewPrivateKeyFromBytes build a new key from the given bytes.
34
33
func NewPrivateKeyFromBytes (bz []byte ) (PrivKey , error ) {
35
- secretKey , err := bls12381 .SecretKeyFromBytes (bz )
34
+ secretKey , err := bls12381 .NewPrivateKeyFromBytes (bz )
36
35
if err != nil {
37
36
return PrivKey {}, err
38
37
}
39
38
return PrivKey {
40
- Key : secretKey .Marshal (),
39
+ Key : secretKey .Bytes (),
41
40
}, nil
42
41
}
43
42
44
43
// GenPrivKey generates a new key.
45
44
func GenPrivKey () (PrivKey , error ) {
46
- secretKey , err := bls12381 .RandKey ()
45
+ secretKey , err := bls12381 .GenPrivKey ()
47
46
return PrivKey {
48
- Key : secretKey .Marshal (),
47
+ Key : secretKey .Bytes (),
49
48
}, err
50
49
}
51
50
@@ -57,13 +56,13 @@ func (privKey PrivKey) Bytes() []byte {
57
56
// PubKey returns the private key's public key. If the privkey is not valid
58
57
// it returns a nil value.
59
58
func (privKey PrivKey ) PubKey () cryptotypes.PubKey {
60
- secretKey , err := bls12381 .SecretKeyFromBytes (privKey .Key )
59
+ secretKey , err := bls12381 .NewPrivateKeyFromBytes (privKey .Key )
61
60
if err != nil {
62
61
return nil
63
62
}
64
63
65
64
return & PubKey {
66
- Key : secretKey .PublicKey ().Marshal (),
65
+ Key : secretKey .PubKey ().Bytes (),
67
66
}
68
67
}
69
68
@@ -74,24 +73,23 @@ func (privKey PrivKey) Equals(other cryptotypes.LedgerPrivKey) bool {
74
73
75
74
// Type returns the type.
76
75
func (PrivKey ) Type () string {
77
- return KeyType
76
+ return bls12381 . KeyType
78
77
}
79
78
80
79
// Sign signs the given byte array. If msg is larger than
81
80
// MaxMsgLen, SHA256 sum will be signed instead of the raw bytes.
82
81
func (privKey PrivKey ) Sign (msg []byte ) ([]byte , error ) {
83
- secretKey , err := bls12381 .SecretKeyFromBytes (privKey .Key )
82
+ secretKey , err := bls12381 .NewPrivateKeyFromBytes (privKey .Key )
84
83
if err != nil {
85
84
return nil , err
86
85
}
87
86
88
- if len (msg ) > MaxMsgLen {
87
+ if len (msg ) > bls12381 . MaxMsgLen {
89
88
hash := sha256 .Sum256 (msg )
90
- sig := secretKey .Sign (hash [:])
91
- return sig .Marshal (), nil
89
+ return secretKey .Sign (hash [:])
92
90
}
93
- sig := secretKey . Sign ( msg )
94
- return sig . Marshal (), nil
91
+
92
+ return secretKey . Sign ( msg )
95
93
}
96
94
97
95
// MarshalAmino overrides Amino binary marshaling.
@@ -101,7 +99,7 @@ func (privKey PrivKey) MarshalAmino() ([]byte, error) {
101
99
102
100
// UnmarshalAmino overrides Amino binary marshaling.
103
101
func (privKey * PrivKey ) UnmarshalAmino (bz []byte ) error {
104
- if len (bz ) != PrivKeySize {
102
+ if len (bz ) != bls12381 . PrivKeySize {
105
103
return errors .New ("invalid privkey size" )
106
104
}
107
105
privKey .Key = bz
@@ -135,35 +133,30 @@ var _ cryptotypes.PubKey = &PubKey{}
135
133
//
136
134
// The function will panic if the public key is invalid.
137
135
func (pubKey PubKey ) Address () crypto.Address {
138
- pk , _ := bls12381 .PublicKeyFromBytes (pubKey .Key )
139
- if len (pk .Marshal ()) != PubKeySize {
136
+ pk , _ := bls12381 .NewPublicKeyFromBytes (pubKey .Key )
137
+ if len (pk .Bytes ()) != bls12381 . PubKeySize {
140
138
panic ("pubkey is incorrect size" )
141
139
}
142
140
return crypto .Address (tmhash .SumTruncated (pubKey .Key ))
143
141
}
144
142
145
143
// VerifySignature verifies the given signature.
146
144
func (pubKey PubKey ) VerifySignature (msg , sig []byte ) bool {
147
- if len (sig ) != SignatureLength {
145
+ if len (sig ) != bls12381 . SignatureLength {
148
146
return false
149
147
}
150
148
151
- pubK , err := bls12381 .PublicKeyFromBytes (pubKey .Key )
149
+ pubK , err := bls12381 .NewPublicKeyFromBytes (pubKey .Key )
152
150
if err != nil { // invalid pubkey
153
151
return false
154
152
}
155
153
156
- if len (msg ) > MaxMsgLen {
154
+ if len (msg ) > bls12381 . MaxMsgLen {
157
155
hash := sha256 .Sum256 (msg )
158
156
msg = hash [:]
159
157
}
160
158
161
- ok , err := bls12381 .VerifySignature (sig , [MaxMsgLen ]byte (msg [:MaxMsgLen ]), pubK )
162
- if err != nil { // bad signature
163
- return false
164
- }
165
-
166
- return ok
159
+ return pubK .VerifySignature (msg , sig )
167
160
}
168
161
169
162
// Bytes returns the byte format.
@@ -173,7 +166,7 @@ func (pubKey PubKey) Bytes() []byte {
173
166
174
167
// Type returns the key's type.
175
168
func (PubKey ) Type () string {
176
- return KeyType
169
+ return bls12381 . KeyType
177
170
}
178
171
179
172
// Equals returns true if the other's type is the same and their bytes are deeply equal.
0 commit comments