Skip to content

Commit b54a05a

Browse files
author
gojuukaze
committed
isOnCurve: remove unnecessary computations from SetBytes
1 parent 0ba48ba commit b54a05a

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

keys.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"os"
3030
"sort"
3131

32-
"filippo.io/edwards25519"
32+
"filippo.io/edwards25519/field"
3333
"github.com/gagliardetto/solana-go/base58"
3434
mrtronbase58 "github.com/mr-tron/base58"
3535
"go.mongodb.org/mongo-driver/bson"
@@ -679,14 +679,35 @@ func CreateProgramAddress(seeds [][]byte, programID PublicKey) (PublicKey, error
679679
return PublicKeyFromBytes(hash[:]), nil
680680
}
681681

682+
var feOne = new(field.Element).One()
683+
var d, _ = new(field.Element).SetBytes([]byte{
684+
0xa3, 0x78, 0x59, 0x13, 0xca, 0x4d, 0xeb, 0x75,
685+
0xab, 0xd8, 0x41, 0x41, 0x4d, 0x0a, 0x70, 0x00,
686+
0x98, 0xe8, 0x79, 0x77, 0x79, 0x40, 0xc7, 0x8c,
687+
0x73, 0xfe, 0x6f, 0x2b, 0xee, 0x6c, 0x03, 0x52})
688+
682689
// Check if the provided `b` is on the ed25519 curve.
683690
func IsOnCurve(b []byte) bool {
684691
if len(b) != ed25519.PublicKeySize {
685692
return false
686693
}
687-
_, err := new(edwards25519.Point).SetBytes(b)
688-
isOnCurve := err == nil
689-
return isOnCurve
694+
//_, err := new(edwards25519.Point).SetBytes(b)
695+
y, err := new(field.Element).SetBytes(b)
696+
if err != nil {
697+
return false
698+
}
699+
700+
y2 := new(field.Element).Square(y)
701+
u := new(field.Element).Subtract(y2, feOne)
702+
703+
vv := new(field.Element).Multiply(y2, d)
704+
vv = vv.Add(vv, feOne)
705+
706+
_, wasSquare := new(field.Element).SqrtRatio(u, vv)
707+
if wasSquare == 0 {
708+
return false
709+
}
710+
return true
690711
}
691712

692713
// Find a valid program address and its corresponding bump seed.

0 commit comments

Comments
 (0)