@@ -20,7 +20,6 @@ package solana
2020import (
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,17 +69,17 @@ func PrivateKeyFromBase58(privkey string) (PrivateKey, error) {
6969}
7070
7171func 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 ))
74- }
75-
76- // ed25519 private keys are seed(32) + public(32); ensure they match.
77- derived := ed25519 . NewKeyFromSeed ( b [: ed25519 . SeedSize ])
78- 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- }
82- return false , errors .New ("invalid private key: seed/ public key mismatch " )
72+ if len (b ) != voied25519 .PrivateKeySize {
73+ return false , fmt .Errorf ("invalid private key size, expected %v, got %d" , voied25519 .PrivateKeySize , len (b ))
74+ }
75+ << << << < HEAD
76+ // check if the public key is on the ed25519 curve
77+ == == == =
78+ // check if the public key is on the voied25519 curve
79+ >> >> >> > f4f19e0 ( rename )
80+ pub := voied25519 . PrivateKey ( b ). Public ().(voied25519. PublicKey )
81+ if ! IsOnCurve ( pub ) {
82+ return false , errors .New ("the corresponding public key is NOT on the voied25519 curve " )
8383 }
8484 return true , nil
8585}
@@ -114,7 +114,7 @@ func (k PrivateKey) String() string {
114114}
115115
116116func 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
@@ -144,8 +144,8 @@ func (k PrivateKey) PublicKey() PublicKey {
144144 panic (err )
145145 }
146146
147- p := ed25519 .PrivateKey (k )
148- pub := p .Public ().(ed25519 .PublicKey )
147+ p := voied25519 .PrivateKey (k )
148+ pub := p .Public ().(voied25519 .PublicKey )
149149
150150 var publicKey PublicKey
151151 copy (publicKey [:], pub )
@@ -156,9 +156,17 @@ func (k PrivateKey) PublicKey() PublicKey {
156156// PK is a convenience alias for PublicKey
157157type PK = PublicKey
158158
159+ << << << < HEAD
160+ // done to keep verify the same as stdlib crypto/ed25519
161+ == == == =
162+ // done to keep verify the same as stdlib crypto/voied25519
163+ >> >> >> > f4f19e0 (rename )
164+ var verifyOptsStdLib = & voied25519.Options {
165+ Verify : voied25519 .VerifyOptionsStdLib ,
166+ }
167+
159168func (p PublicKey ) Verify (message []byte , signature Signature ) bool {
160- pub := ed25519 .PublicKey (p [:])
161- return ed25519 .Verify (pub , message , signature [:])
169+ return voied25519 .VerifyWithOptions (p [:], message , signature [:], verifyOptsStdLib )
162170}
163171
164172type PublicKey [PublicKeyLength ]byte
@@ -300,7 +308,7 @@ func (p PublicKey) Bytes() []byte {
300308 return []byte (p [:])
301309}
302310
303- // Check if a `Pubkey` is on the ed25519 curve.
311+ // Check if a `Pubkey` is on the voied25519 curve.
304312func (p PublicKey ) IsOnCurve () bool {
305313 return IsOnCurve (p [:])
306314}
@@ -621,7 +629,7 @@ const (
621629 SignatureLength = 64
622630
623631 // Number of bytes in a private key.
624- PrivateKeyLength = ed25519 .PrivateKeySize
632+ PrivateKeyLength = voied25519 .PrivateKeySize
625633
626634 // // Maximum string length of a base58 encoded pubkey.
627635 // MaxBase58Length = 44
@@ -682,32 +690,20 @@ func CreateProgramAddress(seeds [][]byte, programID PublicKey) (PublicKey, error
682690 return PublicKeyFromBytes (hash [:]), nil
683691}
684692
685- var feOne = new (field.Element ).One ()
686- var d , _ = new (field.Element ).SetBytes ([]byte {
687- 0xa3 , 0x78 , 0x59 , 0x13 , 0xca , 0x4d , 0xeb , 0x75 ,
688- 0xab , 0xd8 , 0x41 , 0x41 , 0x4d , 0x0a , 0x70 , 0x00 ,
689- 0x98 , 0xe8 , 0x79 , 0x77 , 0x79 , 0x40 , 0xc7 , 0x8c ,
690- 0x73 , 0xfe , 0x6f , 0x2b , 0xee , 0x6c , 0x03 , 0x52 })
691-
692693// Check if the provided `b` is on the ed25519 curve.
693694func IsOnCurve (b []byte ) bool {
694- if len (b ) != ed25519 .PublicKeySize {
695+ if len (b ) != voied25519 .PublicKeySize {
695696 return false
696697 }
697- //_, err := new(edwards25519.Point).SetBytes(b)
698- y , err := new (field.Element ).SetBytes (b )
699- if err != nil {
698+ var compressed curve.CompressedEdwardsY
699+ if _ , err := compressed .SetBytes (b ); err != nil {
700700 return false
701701 }
702-
703- y2 := new (field.Element ).Square (y )
704- u := new (field.Element ).Subtract (y2 , feOne )
705-
706- vv := new (field.Element ).Multiply (y2 , d )
707- vv = vv .Add (vv , feOne )
708-
709- _ , wasSquare := new (field.Element ).SqrtRatio (u , vv )
710- return wasSquare != 0
702+ var p curve.EdwardsPoint
703+ if _ , err := p .SetCompressedY (& compressed ); err != nil {
704+ return false
705+ }
706+ return true
711707}
712708
713709// Find a valid program address and its corresponding bump seed.
0 commit comments