OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / go-crypto / pub_key.go
1 package crypto
2
3 import (
4         "bytes"
5         "crypto/sha256"
6
7         secp256k1 "github.com/btcsuite/btcd/btcec"
8         "github.com/tendermint/ed25519"
9         "github.com/tendermint/ed25519/extra25519"
10         "github.com/tendermint/go-wire"
11         data "github.com/tendermint/go-wire/data"
12         . "github.com/tendermint/tmlibs/common"
13         "golang.org/x/crypto/ripemd160"
14 )
15
16 func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
17         err = wire.ReadBinaryBytes(pubKeyBytes, &pubKey)
18         return
19 }
20
21 //----------------------------------------
22
23 // DO NOT USE THIS INTERFACE.
24 // You probably want to use PubKey
25 // +gen wrapper:"PubKey,Impl[PubKeyEd25519,PubKeySecp256k1],ed25519,secp256k1"
26 type PubKeyInner interface {
27         AssertIsPubKeyInner()
28         Address() []byte
29         Bytes() []byte
30         KeyString() string
31         VerifyBytes(msg []byte, sig Signature) bool
32         Equals(PubKey) bool
33         Wrap() PubKey
34 }
35
36 //-------------------------------------
37
38 var _ PubKeyInner = PubKeyEd25519{}
39
40 // Implements PubKeyInner
41 type PubKeyEd25519 [32]byte
42
43 func (pubKey PubKeyEd25519) AssertIsPubKeyInner() {}
44
45 func (pubKey PubKeyEd25519) Address() []byte {
46         w, n, err := new(bytes.Buffer), new(int), new(error)
47         wire.WriteBinary(pubKey[:], w, n, err)
48         if *err != nil {
49                 PanicCrisis(*err)
50         }
51         // append type byte
52         encodedPubkey := append([]byte{TypeEd25519}, w.Bytes()...)
53         hasher := ripemd160.New()
54         hasher.Write(encodedPubkey) // does not error
55         return hasher.Sum(nil)
56 }
57
58 func (pubKey PubKeyEd25519) Bytes() []byte {
59         return wire.BinaryBytes(PubKey{pubKey})
60 }
61
62 func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
63         // make sure we use the same algorithm to sign
64         sig, ok := sig_.Unwrap().(SignatureEd25519)
65         if !ok {
66                 return false
67         }
68         pubKeyBytes := [32]byte(pubKey)
69         sigBytes := [64]byte(sig)
70         return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
71 }
72
73 func (p PubKeyEd25519) MarshalJSON() ([]byte, error) {
74         return data.Encoder.Marshal(p[:])
75 }
76
77 func (p *PubKeyEd25519) UnmarshalJSON(enc []byte) error {
78         var ref []byte
79         err := data.Encoder.Unmarshal(&ref, enc)
80         copy(p[:], ref)
81         return err
82 }
83
84 // For use with golang/crypto/nacl/box
85 // If error, returns nil.
86 func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
87         keyCurve25519, pubKeyBytes := new([32]byte), [32]byte(pubKey)
88         ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes)
89         if !ok {
90                 return nil
91         }
92         return keyCurve25519
93 }
94
95 func (pubKey PubKeyEd25519) String() string {
96         return Fmt("PubKeyEd25519{%X}", pubKey[:])
97 }
98
99 // Must return the full bytes in hex.
100 // Used for map keying, etc.
101 func (pubKey PubKeyEd25519) KeyString() string {
102         return Fmt("%X", pubKey[:])
103 }
104
105 func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
106         if otherEd, ok := other.Unwrap().(PubKeyEd25519); ok {
107                 return bytes.Equal(pubKey[:], otherEd[:])
108         } else {
109                 return false
110         }
111 }
112
113 //-------------------------------------
114
115 var _ PubKeyInner = PubKeySecp256k1{}
116
117 // Implements PubKey.
118 // Compressed pubkey (just the x-cord),
119 // prefixed with 0x02 or 0x03, depending on the y-cord.
120 type PubKeySecp256k1 [33]byte
121
122 func (pubKey PubKeySecp256k1) AssertIsPubKeyInner() {}
123
124 // Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
125 func (pubKey PubKeySecp256k1) Address() []byte {
126         hasherSHA256 := sha256.New()
127         hasherSHA256.Write(pubKey[:]) // does not error
128         sha := hasherSHA256.Sum(nil)
129
130         hasherRIPEMD160 := ripemd160.New()
131         hasherRIPEMD160.Write(sha) // does not error
132         return hasherRIPEMD160.Sum(nil)
133 }
134
135 func (pubKey PubKeySecp256k1) Bytes() []byte {
136         return wire.BinaryBytes(PubKey{pubKey})
137 }
138
139 func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
140         // and assert same algorithm to sign and verify
141         sig, ok := sig_.Unwrap().(SignatureSecp256k1)
142         if !ok {
143                 return false
144         }
145
146         pub__, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256())
147         if err != nil {
148                 return false
149         }
150         sig__, err := secp256k1.ParseDERSignature(sig[:], secp256k1.S256())
151         if err != nil {
152                 return false
153         }
154         return sig__.Verify(Sha256(msg), pub__)
155 }
156
157 func (p PubKeySecp256k1) MarshalJSON() ([]byte, error) {
158         return data.Encoder.Marshal(p[:])
159 }
160
161 func (p *PubKeySecp256k1) UnmarshalJSON(enc []byte) error {
162         var ref []byte
163         err := data.Encoder.Unmarshal(&ref, enc)
164         copy(p[:], ref)
165         return err
166 }
167
168 func (pubKey PubKeySecp256k1) String() string {
169         return Fmt("PubKeySecp256k1{%X}", pubKey[:])
170 }
171
172 // Must return the full bytes in hex.
173 // Used for map keying, etc.
174 func (pubKey PubKeySecp256k1) KeyString() string {
175         return Fmt("%X", pubKey[:])
176 }
177
178 func (pubKey PubKeySecp256k1) Equals(other PubKey) bool {
179         if otherSecp, ok := other.Unwrap().(PubKeySecp256k1); ok {
180                 return bytes.Equal(pubKey[:], otherSecp[:])
181         } else {
182                 return false
183         }
184 }