Skip to content

Commit 6d277b6

Browse files
Merge pull request #390 from gojuukaze/upgrade_edwards
Optimize IsOnCurve and upgrade edwards25519
2 parents 25da6d7 + b54a05a commit 6d277b6

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ require (
5959
)
6060

6161
require (
62-
filippo.io/edwards25519 v1.1.0
62+
filippo.io/edwards25519 v1.2.0
6363
github.com/AlekSi/pointer v1.2.0
6464
github.com/buger/jsonparser v1.1.2
6565
github.com/davecgh/go-spew v1.1.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ cloud.google.com/go/auth/oauth2adapt v0.2.8 h1:keo8NaayQZ6wimpNSmW5OPc283g65QNIi
44
cloud.google.com/go/auth/oauth2adapt v0.2.8/go.mod h1:XQ9y31RkqZCcwJWNSx2Xvric3RrU88hAYYbjDWYDL+c=
55
cloud.google.com/go/compute/metadata v0.9.0 h1:pDUj4QMoPejqq20dK0Pg2N4yG9zIkYGdBtwLoEkH9Zs=
66
cloud.google.com/go/compute/metadata v0.9.0/go.mod h1:E0bWwX5wTnLPedCKqk3pJmVgCBSM6qQI1yTBdEb3C10=
7-
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
8-
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
7+
filippo.io/edwards25519 v1.2.0 h1:crnVqOiS4jqYleHd9vaKZ+HKtHfllngJIiOpNpoJsjo=
8+
filippo.io/edwards25519 v1.2.0/go.mod h1:xzAOLCNug/yB62zG1bQ8uziwrIqIuxhctzJT18Q77mc=
99
github.com/AlekSi/pointer v1.2.0 h1:glcy/gc4h8HnG2Z3ZECSzZ1IX1x2JxRVuDzaJwQE0+w=
1010
github.com/AlekSi/pointer v1.2.0/go.mod h1:gZGfd3dpW4vEc/UlyfKKi1roIqcCgwOIvb0tSNSBle0=
1111
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=

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/v2/bson"
@@ -678,14 +678,35 @@ func CreateProgramAddress(seeds [][]byte, programID PublicKey) (PublicKey, error
678678
return PublicKeyFromBytes(hash[:]), nil
679679
}
680680

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

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

0 commit comments

Comments
 (0)