From 16d89e0d7b1b7696ef00f0c6677295668d97def7 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Fri, 15 Mar 2024 11:57:18 +0100 Subject: [PATCH] crypto: hide BitCurve behind interface again --- crypto/ecies/ecies.go | 13 ++++--------- crypto/signature_cgo.go | 12 +++++++++++- p2p/rlpx/rlpx.go | 6 +----- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/crypto/ecies/ecies.go b/crypto/ecies/ecies.go index 688a223b40..bd7ab1d583 100644 --- a/crypto/ecies/ecies.go +++ b/crypto/ecies/ecies.go @@ -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 diff --git a/crypto/signature_cgo.go b/crypto/signature_cgo.go index 835bbf717d..dd187f142d 100644 --- a/crypto/signature_cgo.go +++ b/crypto/signature_cgo.go @@ -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() } diff --git a/p2p/rlpx/rlpx.go b/p2p/rlpx/rlpx.go index 2d38834f4d..d34ef60d50 100644 --- a/p2p/rlpx/rlpx.go +++ b/p2p/rlpx/rlpx.go @@ -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{}