Skip to content

Commit 38e864d

Browse files
authored
Merge branch 'main' into fix/rm-panics-fr-lib
2 parents ea8c7c0 + 20713fb commit 38e864d

4 files changed

Lines changed: 34 additions & 39 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/google/uuid v1.6.0
1111
github.com/json-iterator/go v1.1.12
1212
github.com/mr-tron/base58 v1.2.0
13+
github.com/oasisprotocol/curve25519-voi v0.0.0-20251114093237-2ab5a27a1729
1314
go.mongodb.org/mongo-driver/v2 v2.5.0
1415
)
1516

@@ -61,7 +62,6 @@ require (
6162
)
6263

6364
require (
64-
filippo.io/edwards25519 v1.2.0
6565
github.com/AlekSi/pointer v1.2.0
6666
github.com/buger/jsonparser v1.1.2
6767
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,6 @@ 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.2.0 h1:crnVqOiS4jqYleHd9vaKZ+HKtHfllngJIiOpNpoJsjo=
8-
filippo.io/edwards25519 v1.2.0/go.mod h1:xzAOLCNug/yB62zG1bQ8uziwrIqIuxhctzJT18Q77mc=
97
github.com/AlekSi/pointer v1.2.0 h1:glcy/gc4h8HnG2Z3ZECSzZ1IX1x2JxRVuDzaJwQE0+w=
108
github.com/AlekSi/pointer v1.2.0/go.mod h1:gZGfd3dpW4vEc/UlyfKKi1roIqcCgwOIvb0tSNSBle0=
119
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
@@ -106,6 +104,8 @@ github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
106104
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
107105
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
108106
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
107+
github.com/oasisprotocol/curve25519-voi v0.0.0-20251114093237-2ab5a27a1729 h1:yfQ2sO9WJXUAIUR+g7NUkxJSKCAFJcR5sUDu+ZmjTZI=
108+
github.com/oasisprotocol/curve25519-voi v0.0.0-20251114093237-2ab5a27a1729/go.mod h1:hVoHR2EVESiICEMbg137etN/Lx+lSrHPTD39Z/uE+2s=
109109
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
110110
github.com/onsi/ginkgo v1.12.1 h1:mFwc4LvZ0xpSvDZ3E+k8Yte0hLOMxXUlP+yXtJqkYfQ=
111111
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=

keys.go

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package solana
2020
import (
2121
"bytes"
2222
"crypto"
23-
"crypto/ed25519"
2423
crypto_rand "crypto/rand"
2524
"crypto/sha256"
2625
"errors"
@@ -29,9 +28,10 @@ import (
2928
"os"
3029
"sort"
3130

32-
"filippo.io/edwards25519/field"
3331
"github.com/gagliardetto/solana-go/base58"
3432
mrtronbase58 "github.com/mr-tron/base58"
33+
"github.com/oasisprotocol/curve25519-voi/curve"
34+
voied25519 "github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
3535
"go.mongodb.org/mongo-driver/v2/bson"
3636
)
3737

@@ -69,14 +69,14 @@ func PrivateKeyFromBase58(privkey string) (PrivateKey, error) {
6969
}
7070

7171
func ValidatePrivateKey(b []byte) (bool, error) {
72-
if len(b) != ed25519.PrivateKeySize {
73-
return false, fmt.Errorf("invalid private key size, expected %v, got %d", ed25519.PrivateKeySize, len(b))
72+
if len(b) != voied25519.PrivateKeySize {
73+
return false, fmt.Errorf("invalid private key size, expected %v, got %d", voied25519.PrivateKeySize, len(b))
7474
}
7575

7676
// ed25519 private keys are seed(32) + public(32); ensure they match.
77-
derived := ed25519.NewKeyFromSeed(b[:ed25519.SeedSize])
77+
derived := voied25519.NewKeyFromSeed(b[:voied25519.SeedSize])
7878
if !bytes.Equal(derived, b) {
79-
if !IsOnCurve(b[ed25519.SeedSize:]) {
79+
if !IsOnCurve(b[voied25519.SeedSize:]) {
8080
return false, errors.New("invalid private key: seed/public key mismatch (provided public key is NOT on the ed25519 curve)")
8181
}
8282
return false, errors.New("invalid private key: seed/public key mismatch")
@@ -114,7 +114,7 @@ func (k PrivateKey) String() string {
114114
}
115115

116116
func NewRandomPrivateKey() (PrivateKey, error) {
117-
pub, priv, err := ed25519.GenerateKey(crypto_rand.Reader)
117+
pub, priv, err := voied25519.GenerateKey(crypto_rand.Reader)
118118
if err != nil {
119119
return nil, err
120120
}
@@ -127,7 +127,7 @@ func (k PrivateKey) Sign(payload []byte) (Signature, error) {
127127
if err := k.Validate(); err != nil {
128128
return Signature{}, err
129129
}
130-
p := ed25519.PrivateKey(k)
130+
p := voied25519.PrivateKey(k)
131131
signData, err := p.Sign(crypto_rand.Reader, payload, crypto.Hash(0))
132132
if err != nil {
133133
return Signature{}, err
@@ -143,8 +143,10 @@ func (k PrivateKey) PublicKeyOrErr() (PublicKey, error) {
143143
if err := k.Validate(); err != nil {
144144
return PublicKey{}, err
145145
}
146-
p := ed25519.PrivateKey(k)
147-
pub := p.Public().(ed25519.PublicKey)
146+
147+
p := voied25519.PrivateKey(k)
148+
pub := p.Public().(voied25519.PublicKey)
149+
148150
var publicKey PublicKey
149151
copy(publicKey[:], pub)
150152
return publicKey, nil
@@ -158,9 +160,13 @@ func (k PrivateKey) PublicKey() PublicKey {
158160
// PK is a convenience alias for PublicKey
159161
type PK = PublicKey
160162

163+
// done to keep verify the same as stdlib crypto/ed25519
164+
var verifyOptsStdLib = &voied25519.Options{
165+
Verify: voied25519.VerifyOptionsStdLib,
166+
}
167+
161168
func (p PublicKey) Verify(message []byte, signature Signature) bool {
162-
pub := ed25519.PublicKey(p[:])
163-
return ed25519.Verify(pub, message, signature[:])
169+
return voied25519.VerifyWithOptions(p[:], message, signature[:], verifyOptsStdLib)
164170
}
165171

166172
type PublicKey [PublicKeyLength]byte
@@ -302,7 +308,7 @@ func (p PublicKey) Bytes() []byte {
302308
return []byte(p[:])
303309
}
304310

305-
// Check if a `Pubkey` is on the ed25519 curve.
311+
// Check if a `Pubkey` is on the voied25519 curve.
306312
func (p PublicKey) IsOnCurve() bool {
307313
return IsOnCurve(p[:])
308314
}
@@ -623,7 +629,7 @@ const (
623629
SignatureLength = 64
624630

625631
// Number of bytes in a private key.
626-
PrivateKeyLength = ed25519.PrivateKeySize
632+
PrivateKeyLength = voied25519.PrivateKeySize
627633

628634
// // Maximum string length of a base58 encoded pubkey.
629635
// MaxBase58Length = 44
@@ -684,32 +690,20 @@ func CreateProgramAddress(seeds [][]byte, programID PublicKey) (PublicKey, error
684690
return PublicKeyFromBytes(hash[:]), nil
685691
}
686692

687-
var feOne = new(field.Element).One()
688-
var d, _ = new(field.Element).SetBytes([]byte{
689-
0xa3, 0x78, 0x59, 0x13, 0xca, 0x4d, 0xeb, 0x75,
690-
0xab, 0xd8, 0x41, 0x41, 0x4d, 0x0a, 0x70, 0x00,
691-
0x98, 0xe8, 0x79, 0x77, 0x79, 0x40, 0xc7, 0x8c,
692-
0x73, 0xfe, 0x6f, 0x2b, 0xee, 0x6c, 0x03, 0x52})
693-
694693
// Check if the provided `b` is on the ed25519 curve.
695694
func IsOnCurve(b []byte) bool {
696-
if len(b) != ed25519.PublicKeySize {
695+
if len(b) != voied25519.PublicKeySize {
697696
return false
698697
}
699-
//_, err := new(edwards25519.Point).SetBytes(b)
700-
y, err := new(field.Element).SetBytes(b)
701-
if err != nil {
698+
var compressed curve.CompressedEdwardsY
699+
if _, err := compressed.SetBytes(b); err != nil {
702700
return false
703701
}
704-
705-
y2 := new(field.Element).Square(y)
706-
u := new(field.Element).Subtract(y2, feOne)
707-
708-
vv := new(field.Element).Multiply(y2, d)
709-
vv = vv.Add(vv, feOne)
710-
711-
_, wasSquare := new(field.Element).SqrtRatio(u, vv)
712-
return wasSquare != 0
702+
var p curve.EdwardsPoint
703+
if _, err := p.SetCompressedY(&compressed); err != nil {
704+
return false
705+
}
706+
return true
713707
}
714708

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

nativetypes.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
package solana
1919

2020
import (
21-
"crypto/ed25519"
2221
"encoding/base64"
2322
"fmt"
2423
"io"
2524

25+
voied25519 "github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
26+
2627
bin "github.com/gagliardetto/binary"
2728
"github.com/gagliardetto/solana-go/base58"
2829
"github.com/mostynb/zstdpool-freelist"
@@ -186,7 +187,7 @@ func (p *Signature) UnmarshalJSON(data []byte) (err error) {
186187

187188
// Verify checks that the signature is valid for the given public key and message.
188189
func (s Signature) Verify(pubkey PublicKey, msg []byte) bool {
189-
return ed25519.Verify(pubkey[:], msg, s[:])
190+
return voied25519.VerifyWithOptions(pubkey[:], msg, s[:], verifyOptsStdLib)
190191
}
191192

192193
func (p Signature) String() string {

0 commit comments

Comments
 (0)