crypto: hide BitCurve behind interface again

This commit is contained in:
Marius van der Wijden 2024-03-15 11:57:18 +01:00
parent 29a7673986
commit 16d89e0d7b
3 changed files with 16 additions and 15 deletions

View file

@ -40,6 +40,8 @@ import (
"hash"
"io"
"math/big"
"github.com/ethereum/go-ethereum/crypto"
)
var (
@ -255,11 +257,7 @@ func Encrypt(rand io.Reader, pub *PublicKey, m, s1, s2 []byte) (ct []byte, err e
d := messageTag(params.Hash, Km, em, s2)
type marshaller interface {
Marshal(x, y *big.Int) []byte
}
if curve, ok := pub.Curve.(marshaller); ok {
if curve, ok := pub.Curve.(crypto.BetterCurve); ok {
Rb := curve.Marshal(R.PublicKey.X, R.PublicKey.Y)
ct = make([]byte, len(Rb)+len(em)+len(d))
copy(ct, Rb)
@ -305,10 +303,7 @@ func (prv *PrivateKey) Decrypt(c, s1, s2 []byte) (m []byte, err error) {
R := new(PublicKey)
R.Curve = prv.PublicKey.Curve
type unmarshaler interface {
Unmarshal([]byte) (x, y *big.Int)
}
if curve, ok := R.Curve.(unmarshaler); ok {
if curve, ok := R.Curve.(crypto.BetterCurve); ok {
R.X, R.Y = curve.Unmarshal(c[:rLen])
if R.X == nil {
return nil, ErrInvalidPublicKey

View file

@ -21,8 +21,10 @@ package crypto
import (
"crypto/ecdsa"
"crypto/elliptic"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
@ -80,7 +82,15 @@ func CompressPubkey(pubkey *ecdsa.PublicKey) []byte {
return secp256k1.CompressPubkey(pubkey.X, pubkey.Y)
}
// BetterCurve is an interface that combines both a curve
// and (un)-marshalling functions to and from that curve.
type BetterCurve interface {
elliptic.Curve
Marshal(x, y *big.Int) []byte
Unmarshal(data []byte) (x, y *big.Int)
}
// S256 returns an instance of the secp256k1 curve.
func S256() *secp256k1.BitCurve {
func S256() BetterCurve {
return secp256k1.S256()
}

View file

@ -29,7 +29,6 @@ import (
"fmt"
"hash"
"io"
"math/big"
mrand "math/rand"
"net"
"time"
@ -664,10 +663,7 @@ func exportPubkey(pub *ecies.PublicKey) []byte {
if pub == nil {
panic("nil pubkey")
}
type marshaller interface {
Marshal(x, y *big.Int) []byte
}
if curve, ok := pub.Curve.(marshaller); ok {
if curve, ok := pub.Curve.(crypto.BetterCurve); ok {
return curve.Marshal(pub.X, pub.Y)[1:]
}
return []byte{}