crypto: rename

This commit is contained in:
Felix Lange 2024-03-18 16:40:39 +01:00
parent 8f03199f36
commit 1b5993e690
5 changed files with 14 additions and 13 deletions

View file

@ -51,10 +51,11 @@ var (
var errInvalidPubkey = errors.New("invalid secp256k1 public key") var errInvalidPubkey = errors.New("invalid secp256k1 public key")
// BetterCurve is an interface that combines both a curve // EllipticCurve contains curve operations.
// and (un)-marshalling functions to and from that curve. type EllipticCurve interface {
type BetterCurve interface {
elliptic.Curve elliptic.Curve
// Point marshaling/unmarshaing.
Marshal(x, y *big.Int) []byte Marshal(x, y *big.Int) []byte
Unmarshal(data []byte) (x, y *big.Int) Unmarshal(data []byte) (x, y *big.Int)
} }

View file

@ -257,7 +257,7 @@ func Encrypt(rand io.Reader, pub *PublicKey, m, s1, s2 []byte) (ct []byte, err e
d := messageTag(params.Hash, Km, em, s2) d := messageTag(params.Hash, Km, em, s2)
if curve, ok := pub.Curve.(crypto.BetterCurve); ok { if curve, ok := pub.Curve.(crypto.EllipticCurve); ok {
Rb := curve.Marshal(R.PublicKey.X, R.PublicKey.Y) Rb := curve.Marshal(R.PublicKey.X, R.PublicKey.Y)
ct = make([]byte, len(Rb)+len(em)+len(d)) ct = make([]byte, len(Rb)+len(em)+len(d))
copy(ct, Rb) copy(ct, Rb)
@ -303,7 +303,7 @@ func (prv *PrivateKey) Decrypt(c, s1, s2 []byte) (m []byte, err error) {
R := new(PublicKey) R := new(PublicKey)
R.Curve = prv.PublicKey.Curve R.Curve = prv.PublicKey.Curve
if curve, ok := R.Curve.(crypto.BetterCurve); ok { if curve, ok := R.Curve.(crypto.EllipticCurve); ok {
R.X, R.Y = curve.Unmarshal(c[:rLen]) R.X, R.Y = curve.Unmarshal(c[:rLen])
if R.X == nil { if R.X == nil {
return nil, ErrInvalidPublicKey return nil, ErrInvalidPublicKey

View file

@ -81,6 +81,6 @@ func CompressPubkey(pubkey *ecdsa.PublicKey) []byte {
} }
// S256 returns an instance of the secp256k1 curve. // S256 returns an instance of the secp256k1 curve.
func S256() BetterCurve { func S256() EllipticCurve {
return secp256k1.S256() return secp256k1.S256()
} }

View file

@ -159,16 +159,16 @@ func CompressPubkey(pubkey *ecdsa.PublicKey) []byte {
} }
// S256 returns an instance of the secp256k1 curve. // S256 returns an instance of the secp256k1 curve.
func S256() BetterCurve { func S256() EllipticCurve {
return KoblitzCurve{btcec.S256()} return btCurve{btcec.S256()}
} }
type KoblitzCurve struct { type btCurve struct {
*btcec.KoblitzCurve *btcec.btCurve
} }
// Marshall converts a point given as (x, y) into a byte slice. // Marshall converts a point given as (x, y) into a byte slice.
func (curve KoblitzCurve) Marshal(x, y *big.Int) []byte { func (curve btCurve) Marshal(x, y *big.Int) []byte {
byteLen := (curve.Params().BitSize + 7) / 8 byteLen := (curve.Params().BitSize + 7) / 8
ret := make([]byte, 1+2*byteLen) ret := make([]byte, 1+2*byteLen)
@ -182,7 +182,7 @@ func (curve KoblitzCurve) Marshal(x, y *big.Int) []byte {
// Unmarshal converts a point, serialised by Marshal, into an x, y pair. On // Unmarshal converts a point, serialised by Marshal, into an x, y pair. On
// error, x = nil. // error, x = nil.
func (curve KoblitzCurve) Unmarshal(data []byte) (x, y *big.Int) { func (curve btCurve) Unmarshal(data []byte) (x, y *big.Int) {
byteLen := (curve.Params().BitSize + 7) / 8 byteLen := (curve.Params().BitSize + 7) / 8
if len(data) != 1+2*byteLen { if len(data) != 1+2*byteLen {
return nil, nil return nil, nil

View file

@ -663,7 +663,7 @@ func exportPubkey(pub *ecies.PublicKey) []byte {
if pub == nil { if pub == nil {
panic("nil pubkey") panic("nil pubkey")
} }
if curve, ok := pub.Curve.(crypto.BetterCurve); ok { if curve, ok := pub.Curve.(crypto.EllipticCurve); ok {
return curve.Marshal(pub.X, pub.Y)[1:] return curve.Marshal(pub.X, pub.Y)[1:]
} }
return []byte{} return []byte{}