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
13 cryptorand "crypto/rand"
20 "github.com/vapor/crypto/ed25519/internal/edwards25519"
24 // PublicKeySize is the size, in bytes, of public keys as used in this package.
26 // PrivateKeySize is the size, in bytes, of private keys as used in this package.
28 // SignatureSize is the size, in bytes, of signatures generated and verified by this package.
32 // PublicKey is the type of Ed25519 public keys.
35 // PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer.
36 type PrivateKey []byte
38 // Public returns the PublicKey corresponding to priv.
39 func (priv PrivateKey) Public() crypto.PublicKey {
40 publicKey := make([]byte, PublicKeySize)
41 copy(publicKey, priv[32:])
42 return PublicKey(publicKey)
45 // Sign signs the given message with priv.
46 // Ed25519 performs two passes over messages to be signed and therefore cannot
47 // handle pre-hashed messages. Thus opts.HashFunc() must return zero to
48 // indicate the message hasn't been hashed. This can be achieved by passing
49 // crypto.Hash(0) as the value for opts.
50 func (priv PrivateKey) Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) (signature []byte, err error) {
51 if opts.HashFunc() != crypto.Hash(0) {
52 return nil, errors.New("ed25519: cannot sign hashed message")
55 return Sign(priv, message), nil
58 // GenerateKey generates a public/private key pair using entropy from rand.
59 // If rand is nil, crypto/rand.Reader will be used.
60 func GenerateKey(rand io.Reader) (publicKey PublicKey, privateKey PrivateKey, err error) {
62 rand = cryptorand.Reader
65 privateKey = make([]byte, PrivateKeySize)
66 publicKey = make([]byte, PublicKeySize)
67 _, err = io.ReadFull(rand, privateKey[:32])
72 digest := sha512.Sum512(privateKey[:32])
77 var A edwards25519.ExtendedGroupElement
79 copy(hBytes[:], digest[:])
80 edwards25519.GeScalarMultBase(&A, &hBytes)
81 var publicKeyBytes [32]byte
82 A.ToBytes(&publicKeyBytes)
84 copy(privateKey[32:], publicKeyBytes[:])
85 copy(publicKey, publicKeyBytes[:])
87 return publicKey, privateKey, nil
90 // Sign signs the message with privateKey and returns a signature. It will
91 // panic if len(privateKey) is not PrivateKeySize.
92 func Sign(privateKey PrivateKey, message []byte) []byte {
93 if l := len(privateKey); l != PrivateKeySize {
94 panic("ed25519: bad private key length: " + strconv.Itoa(l))
98 h.Write(privateKey[:32])
100 var digest1, messageDigest, hramDigest [64]byte
101 var expandedSecretKey [32]byte
103 copy(expandedSecretKey[:], digest1[:])
104 expandedSecretKey[0] &= 248
105 expandedSecretKey[31] &= 63
106 expandedSecretKey[31] |= 64
109 h.Write(digest1[32:])
111 h.Sum(messageDigest[:0])
113 var messageDigestReduced [32]byte
114 edwards25519.ScReduce(&messageDigestReduced, &messageDigest)
115 var R edwards25519.ExtendedGroupElement
116 edwards25519.GeScalarMultBase(&R, &messageDigestReduced)
118 var encodedR [32]byte
123 h.Write(privateKey[32:])
125 h.Sum(hramDigest[:0])
126 var hramDigestReduced [32]byte
127 edwards25519.ScReduce(&hramDigestReduced, &hramDigest)
130 edwards25519.ScMulAdd(&s, &hramDigestReduced, &expandedSecretKey, &messageDigestReduced)
132 signature := make([]byte, SignatureSize)
133 copy(signature[:], encodedR[:])
134 copy(signature[32:], s[:])
139 // Verify reports whether sig is a valid signature of message by publicKey. It
140 // will panic if len(publicKey) is not PublicKeySize.
141 func Verify(publicKey PublicKey, message, sig []byte) bool {
142 if l := len(publicKey); l != PublicKeySize {
143 panic("ed25519: bad public key length: " + strconv.Itoa(l))
146 if len(sig) != SignatureSize || sig[63]&224 != 0 {
150 var A edwards25519.ExtendedGroupElement
151 var publicKeyBytes [32]byte
152 copy(publicKeyBytes[:], publicKey)
153 if !A.FromBytes(&publicKeyBytes) {
156 edwards25519.FeNeg(&A.X, &A.X)
157 edwards25519.FeNeg(&A.T, &A.T)
161 h.Write(publicKey[:])
166 var hReduced [32]byte
167 edwards25519.ScReduce(&hReduced, &digest)
169 var R edwards25519.ProjectiveGroupElement
172 edwards25519.GeDoubleScalarMultVartime(&R, &hReduced, &A, &b)
176 return subtle.ConstantTimeCompare(sig[:32], checkR[:]) == 1