@@ -96,6 +96,13 @@ func GenerateKey(rand io.Reader) (PublicKey, PrivateKey, error) {
96
96
// with RFC 8032. RFC 8032's private keys correspond to seeds in this
97
97
// package.
98
98
func NewKeyFromSeed (seed []byte ) PrivateKey {
99
+ // Outline the function body so that the returned key can be stack-allocated.
100
+ privateKey := make ([]byte , PrivateKeySize )
101
+ newKeyFromSeed (privateKey , seed )
102
+ return privateKey
103
+ }
104
+
105
+ func newKeyFromSeed (privateKey , seed []byte ) {
99
106
if l := len (seed ); l != SeedSize {
100
107
panic ("ed25519: bad seed length: " + strconv .Itoa (l ))
101
108
}
@@ -112,16 +119,21 @@ func NewKeyFromSeed(seed []byte) PrivateKey {
112
119
var publicKeyBytes [32 ]byte
113
120
A .ToBytes (& publicKeyBytes )
114
121
115
- privateKey := make ([]byte , PrivateKeySize )
116
122
copy (privateKey , seed )
117
123
copy (privateKey [32 :], publicKeyBytes [:])
118
-
119
- return privateKey
120
124
}
121
125
122
126
// Sign signs the message with privateKey and returns a signature. It will
123
127
// panic if len(privateKey) is not PrivateKeySize.
124
128
func Sign (privateKey PrivateKey , message []byte ) []byte {
129
+ // Outline the function body so that the returned signature can be
130
+ // stack-allocated.
131
+ signature := make ([]byte , SignatureSize )
132
+ sign (signature , privateKey , message )
133
+ return signature
134
+ }
135
+
136
+ func sign (signature , privateKey , message []byte ) {
125
137
if l := len (privateKey ); l != PrivateKeySize {
126
138
panic ("ed25519: bad private key length: " + strconv .Itoa (l ))
127
139
}
@@ -161,11 +173,8 @@ func Sign(privateKey PrivateKey, message []byte) []byte {
161
173
var s [32 ]byte
162
174
edwards25519 .ScMulAdd (& s , & hramDigestReduced , & expandedSecretKey , & messageDigestReduced )
163
175
164
- signature := make ([]byte , SignatureSize )
165
176
copy (signature [:], encodedR [:])
166
177
copy (signature [32 :], s [:])
167
-
168
- return signature
169
178
}
170
179
171
180
// Verify reports whether sig is a valid signature of message by publicKey. It
0 commit comments