Skip to content

Commit 0d1096a

Browse files
committed
address review commnets
1 parent c7cb94e commit 0d1096a

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

keys.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ func ValidatePrivateKey(b []byte) (bool, error) {
7676
// ed25519 private keys are seed(32) + public(32); ensure they match.
7777
derived := ed25519.NewKeyFromSeed(b[:ed25519.SeedSize])
7878
if !bytes.Equal(derived, b) {
79+
if !IsOnCurve(b[ed25519.SeedSize:]) {
80+
return false, errors.New("invalid private key: seed/public key mismatch (provided public key is NOT on the ed25519 curve)")
81+
}
7982
return false, errors.New("invalid private key: seed/public key mismatch")
8083
}
8184
return true, nil

keys_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package solana
1919

2020
import (
21+
"crypto/ed25519"
2122
"encoding/binary"
2223
"encoding/hex"
2324
"errors"
@@ -158,6 +159,28 @@ func TestPrivateKeyFromBase58RejectsMismatchedSeedAndPublicKey(t *testing.T) {
158159
require.EqualError(t, err, "invalid private key: seed/public key mismatch")
159160
}
160161

162+
func TestPrivateKeyFromBase58ReturnsDiagnosticForOffCurvePublicKey(t *testing.T) {
163+
original := MustPrivateKeyFromBase58("66cDvko73yAf8LYvFMM3r8vF5vJtkk7JKMgEKwkmBC86oHdq41C7i1a2vS3zE1yCcdLLk6VUatUb32ZzVjSBXtRs")
164+
require.Len(t, original, PrivateKeyLength)
165+
166+
tampered := append([]byte(nil), original...)
167+
offCurve := make([]byte, ed25519.PublicKeySize)
168+
found := false
169+
for i := uint32(0); i < 100_000; i++ {
170+
binary.LittleEndian.PutUint32(offCurve[:4], i)
171+
if !IsOnCurve(offCurve) {
172+
found = true
173+
break
174+
}
175+
}
176+
require.True(t, found, "expected to find an off-curve public key test vector")
177+
copy(tampered[ed25519.SeedSize:], offCurve)
178+
179+
valid, err := ValidatePrivateKey(tampered)
180+
require.False(t, valid)
181+
require.EqualError(t, err, "invalid private key: seed/public key mismatch (provided public key is NOT on the ed25519 curve)")
182+
}
183+
161184
func TestPublicKey_MarshalText(t *testing.T) {
162185
keyString := "4wBqpZM9k69W87zdYXT2bRtLViWqTiJV3i2Kn9q7S6j"
163186
keyParsed := MustPublicKeyFromBase58(keyString)

0 commit comments

Comments
 (0)