-
Notifications
You must be signed in to change notification settings - Fork 0
/
pub_key.go
449 lines (376 loc) · 10.3 KB
/
pub_key.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package crypto
import (
"bytes"
"crypto/sha256"
"github.com/SickleZhang/bls"
secp256k1 "github.com/btcsuite/btcd/btcec"
ethcrypto "github.com/intfoundation/intchain/crypto"
"github.com/tendermint/ed25519"
"github.com/tendermint/ed25519/extra25519"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-data"
"github.com/tendermint/go-wire"
"golang.org/x/crypto/ripemd160"
)
// PubKey is part of Account and Validator.
type PubKey interface {
Address() []byte
Bytes() []byte
KeyString() string
VerifyBytes(msg []byte, sig Signature) bool
Equals(PubKey) bool
}
var pubKeyMapper data.Mapper
// register both public key types with go-data (and thus go-wire)
func init() {
pubKeyMapper = data.NewMapper(PubKeyS{}).
RegisterImplementation(PubKeyEd25519{}, NameEd25519, TypeEd25519).
RegisterImplementation(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1).
RegisterImplementation(EthereumPubKey{}, NameEthereum, TypeEthereum).
RegisterImplementation(BLSPubKey{}, NameBls, TypeBls)
}
// PubKeyS add json serialization to PubKey
type PubKeyS struct {
PubKey
}
func WrapPubKey(pk PubKey) PubKeyS {
for ppk, ok := pk.(PubKeyS); ok; ppk, ok = pk.(PubKeyS) {
pk = ppk.PubKey
}
return PubKeyS{pk}
}
func (p PubKeyS) MarshalJSON() ([]byte, error) {
return pubKeyMapper.ToJSON(p.PubKey)
}
func (p *PubKeyS) UnmarshalJSON(data []byte) (err error) {
parsed, err := pubKeyMapper.FromJSON(data)
if err == nil && parsed != nil {
p.PubKey = parsed.(PubKey)
}
return
}
func (p PubKeyS) Empty() bool {
return p.PubKey == nil
}
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
err = wire.ReadBinaryBytes(pubKeyBytes, &pubKey)
return
}
//-------------------------------------
// Implements PubKey
type PubKeyEd25519 [32]byte
func (pubKey PubKeyEd25519) Address() []byte {
w, n, err := new(bytes.Buffer), new(int), new(error)
wire.WriteBinary(pubKey[:], w, n, err)
if *err != nil {
PanicCrisis(*err)
}
// append type byte
encodedPubkey := append([]byte{TypeEd25519}, w.Bytes()...)
hasher := ripemd160.New()
hasher.Write(encodedPubkey) // does not error
return hasher.Sum(nil)
}
func (pubKey PubKeyEd25519) Bytes() []byte {
return wire.BinaryBytes(struct{ PubKey }{pubKey})
}
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 (p PubKeyEd25519) MarshalJSON() ([]byte, error) {
return data.Encoder.Marshal(p[:])
}
func (p *PubKeyEd25519) UnmarshalJSON(enc []byte) error {
var ref []byte
err := data.Encoder.Unmarshal(&ref, enc)
copy(p[:], ref)
return err
}
// For use with golang/crypto/nacl/box
// If error, returns nil.
func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
keyCurve25519, pubKeyBytes := new([32]byte), [32]byte(pubKey)
ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes)
if !ok {
return nil
}
return keyCurve25519
}
func (pubKey PubKeyEd25519) String() string {
return Fmt("PubKeyEd25519{%X}", pubKey[:])
}
// Must return the full bytes in hex.
// Used for map keying, etc.
func (pubKey PubKeyEd25519) KeyString() string {
return Fmt("%X", pubKey[:])
}
func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
if otherEd, ok := other.(PubKeyEd25519); ok {
return bytes.Equal(pubKey[:], otherEd[:])
} else {
return false
}
}
//-------------------------------------
// Implements PubKey.
// Compressed pubkey (just the x-cord),
// prefixed with 0x02 or 0x03, depending on the y-cord.
type PubKeySecp256k1 [33]byte
// Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
func (pubKey PubKeySecp256k1) Address() []byte {
hasherSHA256 := sha256.New()
hasherSHA256.Write(pubKey[:]) // does not error
sha := hasherSHA256.Sum(nil)
hasherRIPEMD160 := ripemd160.New()
hasherRIPEMD160.Write(sha) // does not error
return hasherRIPEMD160.Sum(nil)
}
func (pubKey PubKeySecp256k1) Bytes() []byte {
return wire.BinaryBytes(struct{ PubKey }{pubKey})
}
func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
// unwrap if needed
if wrap, ok := sig_.(SignatureS); ok {
sig_ = wrap.Signature
}
// and assert same algorithm to sign and verify
sig, ok := sig_.(SignatureSecp256k1)
if !ok {
return false
}
pub__, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256())
if err != nil {
return false
}
sig__, err := secp256k1.ParseDERSignature(sig[:], secp256k1.S256())
if err != nil {
return false
}
return sig__.Verify(Sha256(msg), pub__)
}
func (p PubKeySecp256k1) MarshalJSON() ([]byte, error) {
return data.Encoder.Marshal(p[:])
}
func (p *PubKeySecp256k1) UnmarshalJSON(enc []byte) error {
var ref []byte
err := data.Encoder.Unmarshal(&ref, enc)
copy(p[:], ref)
return err
}
func (pubKey PubKeySecp256k1) String() string {
return Fmt("PubKeySecp256k1{%X}", pubKey[:])
}
// Must return the full bytes in hex.
// Used for map keying, etc.
func (pubKey PubKeySecp256k1) KeyString() string {
return Fmt("%X", pubKey[:])
}
func (pubKey PubKeySecp256k1) Equals(other PubKey) bool {
if otherSecp, ok := other.(PubKeySecp256k1); ok {
return bytes.Equal(pubKey[:], otherSecp[:])
} else {
return false
}
}
type EthereumPubKey []byte
func (pubKey EthereumPubKey) Address() []byte {
cKey := ethcrypto.ToECDSAPub(pubKey[:])
address := ethcrypto.PubkeyToAddress(*cKey)
return address[:]
}
func (pubKey EthereumPubKey) Bytes() []byte {
return wire.BinaryBytes(struct{ PubKey }{pubKey})
}
func (pubKey EthereumPubKey) KeyString() string {
return Fmt("EthPubKey{%X}", pubKey[:])
}
func (pubKey EthereumPubKey) VerifyBytes(msg []byte, sig_ Signature) bool {
msg = ethcrypto.Keccak256(msg)
recoveredPub, err := ethcrypto.Ecrecover(msg, sig_.(EthereumSignature).SigByte())
if err != nil {
return false
}
return bytes.Equal(pubKey[:], recoveredPub[:])
}
func (pubKey EthereumPubKey) Equals(other PubKey) bool {
if otherEd, ok := other.(EthereumPubKey); ok {
return bytes.Equal(pubKey[:], otherEd[:])
} else {
return false
}
}
func (pubKey EthereumPubKey) MarshalJSON() ([]byte, error) {
return data.Encoder.Marshal(pubKey[:])
}
func (p *EthereumPubKey) UnmarshalJSON(enc []byte) error {
var ref []byte
err := data.Encoder.Unmarshal(&ref, enc)
copy((*p)[:], ref)
return err
}
/*
//-------------------------------------
// Implements PubKey.
type BLSPubKey []byte
func (pubKey BLSPubKey) getElement() *pbc.Element {
return pairing.NewG2().SetBytes(pubKey)
}
func (pubKey BLSPubKey) GetElement() *pbc.Element {
return pairing.NewG2().SetBytes(pubKey)
}
func (pubKey BLSPubKey) Set1() {
copy(pubKey, pairing.NewG1().Set1().Bytes())
}
func CreateBLSPubKey() BLSPubKey {
pubKey := pairing.NewG2().Rand()
return pubKey.Bytes()
}
func PubKeyMul(l, r BLSPubKey) BLSPubKey {
el1 := l.getElement()
el2 := r.getElement()
rs := pairing.NewG2().Mul(el1, el2)
return rs.Bytes()
}
func (pubKey BLSPubKey) Mul(other PubKey) bool {
if otherPub, ok := other.(BLSPubKey); ok {
el1 := pubKey.getElement()
el2 := otherPub.getElement()
rs := pairing.NewG2().Mul(el1, el2)
copy(pubKey, rs.Bytes())
return true
} else {
return false
}
}
func (pubKey BLSPubKey) MulWithSet1(other PubKey) bool {
if otherPub, ok := other.(BLSPubKey); ok {
el1 := pubKey.getElement()
el1.Set1()
el2 := otherPub.getElement()
rs := pairing.NewG2().Mul(el1, el2)
copy(pubKey, rs.Bytes())
return true
} else {
return false
}
}
func (pubKey BLSPubKey) Bytes() []byte {
return pubKey
}
func (pubKey BLSPubKey) Address() []byte {
hasherSHA256 := sha256.New()
hasherSHA256.Write(pubKey[:]) // does not error
sha := hasherSHA256.Sum(nil)
hasherRIPEMD160 := ripemd160.New()
hasherRIPEMD160.Write(sha) // does not error
return hasherRIPEMD160.Sum(nil)
}
func (pubKey BLSPubKey) KeyString() string {
return Fmt("EthPubKey{%X}", pubKey[:])
}
func (pubKey BLSPubKey) VerifyBytes(msg []byte, sig_ Signature) bool {
if otherSign, ok := sig_.(BLSSignature); ok {
h := pairing.NewG1().SetFromStringHash(string(msg), sha256.New())
temp1 := pairing.NewGT().Pair(h, pubKey.getElement())
temp2 := pairing.NewGT().Pair(otherSign.getElement(), g)
return temp1.Equals(temp2)
} else {
return false;
}
}
func (pubKey BLSPubKey) Equals(other PubKey) bool {
if otherBLS, ok := other.(BLSPubKey); ok {
return pubKey.getElement().Equals(otherBLS.getElement())
} else {
return false
}
}
func (pubKey BLSPubKey) MarshalJSON() ([]byte, error) {
return data.Encoder.Marshal(pubKey)
}
func (p *BLSPubKey) UnmarshalJSON(enc []byte) error {
var ref []byte
err := data.Encoder.Unmarshal(&ref, enc)
copy(*p, ref)
return err
}
*/
type BLSPubKey [128]byte
func (pubKey BLSPubKey) getElement() *bls.PublicKey {
pb := &bls.PublicKey{}
err := pb.Unmarshal(pubKey[:])
if err != nil {
return nil
} else {
return pb
}
}
func (pubKey BLSPubKey) Bytes() []byte {
return pubKey[:]
}
func BLSPubKeyAggregate(pks []*PubKey) *BLSPubKey {
var _pks []*bls.PublicKey
for _, pk := range pks {
if _pk, ok := (*pk).(BLSPubKey); ok {
_pks = append(_pks, _pk.getElement())
} else {
return nil
}
}
var pub BLSPubKey
copy(pub[:], new(bls.PublicKey).Aggregate(_pks...).Marshal())
return &pub
}
func (pubKey BLSPubKey) Address() []byte {
hasherSHA256 := sha256.New()
hasherSHA256.Write(pubKey[:]) // does not error
sha := hasherSHA256.Sum(nil)
hasherRIPEMD160 := ripemd160.New()
hasherRIPEMD160.Write(sha) // does not error
return hasherRIPEMD160.Sum(nil)
}
func (pubKey BLSPubKey) Equals(other PubKey) bool {
if otherPk, ok := other.(BLSPubKey); ok {
return pubKey == otherPk
} else {
return false
}
}
func (pubKey BLSPubKey) VerifyBytes(msg []byte, sig_ Signature) bool {
if otherSign, ok := sig_.(BLSSignature); ok {
sign := otherSign.getElement()
if sign == nil {
return false
}
pub := pubKey.getElement()
if pub == nil {
return false
}
return bls.Verify(sign, msg, pub)
} else {
return false;
}
}
func (pubKey BLSPubKey) KeyString() string {
return Fmt("0x%X", pubKey[:])
}
func (pubKey BLSPubKey) MarshalJSON() ([]byte, error) {
return data.Encoder.Marshal(pubKey[:])
}
func (pubKey *BLSPubKey) UnmarshalJSON(enc []byte) error {
var ref []byte
err := data.Encoder.Unmarshal(&ref, enc)
copy(pubKey[:], ref)
return err
}