1 // Package ed25519 implements the Ed25519 signature algorithm. See
2 // http://ed25519.cr.yp.to/.
4 // These functions are also compatible with the “Ed25519” function defined in
5 // https://tools.ietf.org/html/draft-irtf-cfrg-eddsa-05.
8 // This code is a port of the public domain, “ref10” implementation of ed25519
12 cryptorand "crypto/rand"
19 "github.com/vapor/crypto/ed25519/internal/edwards25519"
23 // PublicKeySize is the size, in bytes, of public keys as used in this package.
25 // PrivateKeySize is the size, in bytes, of private keys as used in this package.
27 // SignatureSize is the size, in bytes, of signatures generated and verified by this package.
29 // SeedSize is the size, in bytes, of private key seeds. These are the private key representations used by RFC 8032.
33 // PublicKey is the type of Ed25519 public keys.
36 // PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer.
37 type PrivateKey []byte
39 // Public returns the PublicKey corresponding to priv.
40 func (priv PrivateKey) Public() PublicKey {
41 publicKey := make([]byte, PublicKeySize)
42 copy(publicKey, priv[32:])
43 return PublicKey(publicKey)
46 // Seed returns the private key seed corresponding to priv. It is provided for
47 // interoperability with RFC 8032. RFC 8032's private keys correspond to seeds
49 func (priv PrivateKey) Seed() []byte {
50 seed := make([]byte, SeedSize)
55 func (priv PrivateKey) String() string {
56 return hex.EncodeToString(priv)
59 // GenerateKey generates a public/private key pair using entropy from rand.
60 // If rand is nil, crypto/rand.Reader will be used.
61 func GenerateKey(rand io.Reader) (publicKey PublicKey, privateKey PrivateKey, err error) {
63 rand = cryptorand.Reader
66 privateKey = make([]byte, PrivateKeySize)
67 publicKey = make([]byte, PublicKeySize)
68 _, err = io.ReadFull(rand, privateKey[:32])
73 digest := sha512.Sum512(privateKey[:32])
78 var A edwards25519.ExtendedGroupElement
80 copy(hBytes[:], digest[:])
81 edwards25519.GeScalarMultBase(&A, &hBytes)
82 var publicKeyBytes [32]byte
83 A.ToBytes(&publicKeyBytes)
85 copy(privateKey[32:], publicKeyBytes[:])
86 copy(publicKey, publicKeyBytes[:])
88 return publicKey, privateKey, nil
91 // NewKeyFromSeed calculates a private key from a seed. It will panic if
92 // len(seed) is not SeedSize. This function is provided for interoperability
93 // with RFC 8032. RFC 8032's private keys correspond to seeds in this
95 func NewKeyFromSeed(seed []byte) PrivateKey {
96 if l := len(seed); l != SeedSize {
97 panic("ed25519: bad seed length: " + strconv.Itoa(l))
100 digest := sha512.Sum512(seed)
105 var A edwards25519.ExtendedGroupElement
107 copy(hBytes[:], digest[:])
108 edwards25519.GeScalarMultBase(&A, &hBytes)
109 var publicKeyBytes [32]byte
110 A.ToBytes(&publicKeyBytes)
112 privateKey := make([]byte, PrivateKeySize)
113 copy(privateKey, seed)
114 copy(privateKey[32:], publicKeyBytes[:])
119 // Sign signs the message with privateKey and returns a signature. It will
120 // panic if len(privateKey) is not PrivateKeySize.
121 func Sign(privateKey PrivateKey, message []byte) []byte {
122 if l := len(privateKey); l != PrivateKeySize {
123 panic("ed25519: bad private key length: " + strconv.Itoa(l))
127 h.Write(privateKey[:32])
129 var digest1, messageDigest, hramDigest [64]byte
130 var expandedSecretKey [32]byte
132 copy(expandedSecretKey[:], digest1[:])
133 expandedSecretKey[0] &= 248
134 expandedSecretKey[31] &= 63
135 expandedSecretKey[31] |= 64
138 h.Write(digest1[32:])
140 h.Sum(messageDigest[:0])
142 var messageDigestReduced [32]byte
143 edwards25519.ScReduce(&messageDigestReduced, &messageDigest)
144 var R edwards25519.ExtendedGroupElement
145 edwards25519.GeScalarMultBase(&R, &messageDigestReduced)
147 var encodedR [32]byte
152 h.Write(privateKey[32:])
154 h.Sum(hramDigest[:0])
155 var hramDigestReduced [32]byte
156 edwards25519.ScReduce(&hramDigestReduced, &hramDigest)
159 edwards25519.ScMulAdd(&s, &hramDigestReduced, &expandedSecretKey, &messageDigestReduced)
161 signature := make([]byte, SignatureSize)
162 copy(signature[:], encodedR[:])
163 copy(signature[32:], s[:])
168 // Verify reports whether sig is a valid signature of message by publicKey. It
169 // will panic if len(publicKey) is not PublicKeySize.
170 func Verify(publicKey PublicKey, message, sig []byte) bool {
171 if l := len(publicKey); l != PublicKeySize {
172 panic("ed25519: bad public key length: " + strconv.Itoa(l))
175 if len(sig) != SignatureSize || sig[63]&224 != 0 {
179 var A edwards25519.ExtendedGroupElement
180 var publicKeyBytes [32]byte
181 copy(publicKeyBytes[:], publicKey)
182 if !A.FromBytes(&publicKeyBytes) {
185 edwards25519.FeNeg(&A.X, &A.X)
186 edwards25519.FeNeg(&A.T, &A.T)
190 h.Write(publicKey[:])
195 var hReduced [32]byte
196 edwards25519.ScReduce(&hReduced, &digest)
198 var R edwards25519.ProjectiveGroupElement
201 edwards25519.GeDoubleScalarMultVartime(&R, &hReduced, &A, &b)
205 return subtle.ConstantTimeCompare(sig[:32], checkR[:]) == 1