OSDN Git Service

feat: add node discovery and status check (#374)
[bytom/vapor.git] / crypto / ed25519 / ed25519.go
1 // Package ed25519 implements the Ed25519 signature algorithm. See
2 // http://ed25519.cr.yp.to/.
3 //
4 // These functions are also compatible with the “Ed25519” function defined in
5 // https://tools.ietf.org/html/draft-irtf-cfrg-eddsa-05.
6 package ed25519
7
8 // This code is a port of the public domain, “ref10” implementation of ed25519
9 // from SUPERCOP.
10
11 import (
12         cryptorand "crypto/rand"
13         "crypto/sha512"
14         "crypto/subtle"
15         "encoding/hex"
16         "io"
17         "strconv"
18
19         "github.com/vapor/crypto/ed25519/internal/edwards25519"
20 )
21
22 const (
23         // PublicKeySize is the size, in bytes, of public keys as used in this package.
24         PublicKeySize = 32
25         // PrivateKeySize is the size, in bytes, of private keys as used in this package.
26         PrivateKeySize = 64
27         // SignatureSize is the size, in bytes, of signatures generated and verified by this package.
28         SignatureSize = 64
29         // SeedSize is the size, in bytes, of private key seeds. These are the private key representations used by RFC 8032.
30         SeedSize = 32
31 )
32
33 // PublicKey is the type of Ed25519 public keys.
34 type PublicKey []byte
35
36 // PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer.
37 type PrivateKey []byte
38
39 func (pub PublicKey) String() string {
40         return hex.EncodeToString(pub)
41 }
42
43 // Public returns the PublicKey corresponding to priv.
44 func (priv PrivateKey) Public() PublicKey {
45         publicKey := make([]byte, PublicKeySize)
46         copy(publicKey, priv[32:])
47         return PublicKey(publicKey)
48 }
49
50 // Seed returns the private key seed corresponding to priv. It is provided for
51 // interoperability with RFC 8032. RFC 8032's private keys correspond to seeds
52 // in this package.
53 func (priv PrivateKey) Seed() []byte {
54         seed := make([]byte, SeedSize)
55         copy(seed, priv[:32])
56         return seed
57 }
58
59 func (priv PrivateKey) String() string {
60         return hex.EncodeToString(priv)
61 }
62
63 // GenerateKey generates a public/private key pair using entropy from rand.
64 // If rand is nil, crypto/rand.Reader will be used.
65 func GenerateKey(rand io.Reader) (publicKey PublicKey, privateKey PrivateKey, err error) {
66         if rand == nil {
67                 rand = cryptorand.Reader
68         }
69
70         privateKey = make([]byte, PrivateKeySize)
71         publicKey = make([]byte, PublicKeySize)
72         _, err = io.ReadFull(rand, privateKey[:32])
73         if err != nil {
74                 return nil, nil, err
75         }
76
77         digest := sha512.Sum512(privateKey[:32])
78         digest[0] &= 248
79         digest[31] &= 127
80         digest[31] |= 64
81
82         var A edwards25519.ExtendedGroupElement
83         var hBytes [32]byte
84         copy(hBytes[:], digest[:])
85         edwards25519.GeScalarMultBase(&A, &hBytes)
86         var publicKeyBytes [32]byte
87         A.ToBytes(&publicKeyBytes)
88
89         copy(privateKey[32:], publicKeyBytes[:])
90         copy(publicKey, publicKeyBytes[:])
91
92         return publicKey, privateKey, nil
93 }
94
95 // NewKeyFromSeed calculates a private key from a seed. It will panic if
96 // len(seed) is not SeedSize. This function is provided for interoperability
97 // with RFC 8032. RFC 8032's private keys correspond to seeds in this
98 // package.
99 func NewKeyFromSeed(seed []byte) PrivateKey {
100         if l := len(seed); l != SeedSize {
101                 panic("ed25519: bad seed length: " + strconv.Itoa(l))
102         }
103
104         digest := sha512.Sum512(seed)
105         digest[0] &= 248
106         digest[31] &= 127
107         digest[31] |= 64
108
109         var A edwards25519.ExtendedGroupElement
110         var hBytes [32]byte
111         copy(hBytes[:], digest[:])
112         edwards25519.GeScalarMultBase(&A, &hBytes)
113         var publicKeyBytes [32]byte
114         A.ToBytes(&publicKeyBytes)
115
116         privateKey := make([]byte, PrivateKeySize)
117         copy(privateKey, seed)
118         copy(privateKey[32:], publicKeyBytes[:])
119
120         return privateKey
121 }
122
123 // Sign signs the message with privateKey and returns a signature. It will
124 // panic if len(privateKey) is not PrivateKeySize.
125 func Sign(privateKey PrivateKey, message []byte) []byte {
126         if l := len(privateKey); l != PrivateKeySize {
127                 panic("ed25519: bad private key length: " + strconv.Itoa(l))
128         }
129
130         h := sha512.New()
131         h.Write(privateKey[:32])
132
133         var digest1, messageDigest, hramDigest [64]byte
134         var expandedSecretKey [32]byte
135         h.Sum(digest1[:0])
136         copy(expandedSecretKey[:], digest1[:])
137         expandedSecretKey[0] &= 248
138         expandedSecretKey[31] &= 63
139         expandedSecretKey[31] |= 64
140
141         h.Reset()
142         h.Write(digest1[32:])
143         h.Write(message)
144         h.Sum(messageDigest[:0])
145
146         var messageDigestReduced [32]byte
147         edwards25519.ScReduce(&messageDigestReduced, &messageDigest)
148         var R edwards25519.ExtendedGroupElement
149         edwards25519.GeScalarMultBase(&R, &messageDigestReduced)
150
151         var encodedR [32]byte
152         R.ToBytes(&encodedR)
153
154         h.Reset()
155         h.Write(encodedR[:])
156         h.Write(privateKey[32:])
157         h.Write(message)
158         h.Sum(hramDigest[:0])
159         var hramDigestReduced [32]byte
160         edwards25519.ScReduce(&hramDigestReduced, &hramDigest)
161
162         var s [32]byte
163         edwards25519.ScMulAdd(&s, &hramDigestReduced, &expandedSecretKey, &messageDigestReduced)
164
165         signature := make([]byte, SignatureSize)
166         copy(signature[:], encodedR[:])
167         copy(signature[32:], s[:])
168
169         return signature
170 }
171
172 // Verify reports whether sig is a valid signature of message by publicKey. It
173 // will panic if len(publicKey) is not PublicKeySize.
174 func Verify(publicKey PublicKey, message, sig []byte) bool {
175         if l := len(publicKey); l != PublicKeySize {
176                 panic("ed25519: bad public key length: " + strconv.Itoa(l))
177         }
178
179         if len(sig) != SignatureSize || sig[63]&224 != 0 {
180                 return false
181         }
182
183         var A edwards25519.ExtendedGroupElement
184         var publicKeyBytes [32]byte
185         copy(publicKeyBytes[:], publicKey)
186         if !A.FromBytes(&publicKeyBytes) {
187                 return false
188         }
189         edwards25519.FeNeg(&A.X, &A.X)
190         edwards25519.FeNeg(&A.T, &A.T)
191
192         h := sha512.New()
193         h.Write(sig[:32])
194         h.Write(publicKey[:])
195         h.Write(message)
196         var digest [64]byte
197         h.Sum(digest[:0])
198
199         var hReduced [32]byte
200         edwards25519.ScReduce(&hReduced, &digest)
201
202         var R edwards25519.ProjectiveGroupElement
203         var b [32]byte
204         copy(b[:], sig[32:])
205         edwards25519.GeDoubleScalarMultVartime(&R, &hReduced, &A, &b)
206
207         var checkR [32]byte
208         R.ToBytes(&checkR)
209         return subtle.ConstantTimeCompare(sig[:32], checkR[:]) == 1
210 }