multi: use btcd's btcec/v2 and btcutil modules

This commit was previously split into the following parts to ease
review:
 - 2d746f68: replace imports
 - 4008f0fd: use ecdsa.Signature
 - 849e33d1: remove btcec.S256()
 - b8f6ebbd: use v2 library correctly
 - fa80bca9: bump go modules
This commit is contained in:
Oliver Gugger committed 2022-03-09 19:02:37 +01:00
1 parent 8ee9fc837b
commit 7dfe4018ce
350 files changed
+2421 -1289

No files matched your search

+2 -2
View File
@@ -4,8 +4,8 @@ import (
"bytes"
"io"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/tlv"
)
+2 -2
View File
@@ -4,7 +4,7 @@ import (
"bytes"
"testing"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
)
// TestDecodeAcceptChannel tests decoding of an accept channel wire message with
@@ -32,7 +32,7 @@ func TestDecodeAcceptChannel(t *testing.T) {
test := test
t.Run(test.name, func(t *testing.T) {
priv, err := btcec.NewPrivateKey(btcec.S256())
priv, err := btcec.NewPrivateKey()
if err != nil {
t.Fatalf("cannot create privkey: %v", err)
}
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"bytes"
"io"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
)
// ChannelReestablish is a message sent between peers that have an existing
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"bytes"
"io"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcd/btcutil"
)
// ClosingSigned is sent by both parties to a channel once the channel is clear
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"bytes"
"io"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
)
// FundingLocked is the message that both parties to a new channel creation
+3 -3
View File
@@ -9,10 +9,10 @@ import (
"math"
"net"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/tor"
)
@@ -541,7 +541,7 @@ func ReadElement(r io.Reader, element interface{}) error {
return err
}
pubKey, err := btcec.ParsePubKey(b[:], btcec.S256())
pubKey, err := btcec.ParsePubKey(b[:])
if err != nil {
return err
}
+13 -11
View File
@@ -6,7 +6,6 @@ import (
"encoding/hex"
"image/color"
"math"
"math/big"
"math/rand"
"net"
"reflect"
@@ -14,10 +13,11 @@ import (
"testing/quick"
"time"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/tor"
"github.com/stretchr/testify/assert"
)
@@ -26,12 +26,14 @@ var (
shaHash1Bytes, _ = hex.DecodeString("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
shaHash1, _ = chainhash.NewHash(shaHash1Bytes)
outpoint1 = wire.NewOutPoint(shaHash1, 0)
testSig = &btcec.Signature{
R: new(big.Int),
S: new(big.Int),
}
_, _ = testSig.R.SetString("63724406601629180062774974542967536251589935445068131219452686511677818569431", 10)
_, _ = testSig.S.SetString("18801056069249825825291287104931333862866033135609736119018462340006816851118", 10)
testRBytes, _ = hex.DecodeString("8ce2bc69281ce27da07e6683571319d18e949ddfa2965fb6caa1bf0314f882d7")
testSBytes, _ = hex.DecodeString("299105481d63e0f4bc2a88121167221b6700d72a0ead154c03be696a292d24ae")
testRScalar = new(btcec.ModNScalar)
testSScalar = new(btcec.ModNScalar)
_ = testRScalar.SetByteSlice(testRBytes)
_ = testSScalar.SetByteSlice(testSBytes)
testSig = ecdsa.NewSignature(testRScalar, testSScalar)
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -46,7 +48,7 @@ func randAlias(r *rand.Rand) NodeAlias {
}
func randPubKey() (*btcec.PublicKey, error) {
priv, err := btcec.NewPrivateKey(btcec.S256())
priv, err := btcec.NewPrivateKey()
if err != nil {
return nil, err
}
@@ -57,7 +59,7 @@ func randPubKey() (*btcec.PublicKey, error) {
func randRawKey() ([33]byte, error) {
var n [33]byte
priv, err := btcec.NewPrivateKey(btcec.S256())
priv, err := btcec.NewPrivateKey()
if err != nil {
return n, err
}
+6 -9
View File
@@ -7,14 +7,14 @@ import (
"image/color"
"io"
"math"
"math/big"
"math/rand"
"net"
"sync"
"testing"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/tor"
"github.com/stretchr/testify/mock"
@@ -25,10 +25,7 @@ const deliveryAddressMaxSize = 34
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var (
testSig = &btcec.Signature{
R: new(big.Int),
S: new(big.Int),
}
testSig = &ecdsa.Signature{}
testNodeSig, _ = lnwire.NewSigFromSignature(testSig)
testNumExtraBytes = 1000
@@ -865,7 +862,7 @@ func randRawKey(t testing.TB) [33]byte {
var n [33]byte
priv, err := btcec.NewPrivateKey(btcec.S256())
priv, err := btcec.NewPrivateKey()
require.NoError(t, err, "failed to create privKey")
copy(n[:], priv.PubKey().SerializeCompressed())
@@ -876,7 +873,7 @@ func randRawKey(t testing.TB) [33]byte {
func randPubKey(t testing.TB) *btcec.PublicKey {
t.Helper()
priv, err := btcec.NewPrivateKey(btcec.S256())
priv, err := btcec.NewPrivateKey()
require.NoError(t, err, "failed to create pubkey")
return priv.PubKey()
+1 -1
View File
@@ -3,7 +3,7 @@ package lnwire
import (
"fmt"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcd/btcutil"
)
const (
+1 -1
View File
@@ -3,7 +3,7 @@ package lnwire
import (
"testing"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcd/btcutil"
)
func TestMilliSatoshiConversion(t *testing.T) {
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"fmt"
"net"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/wire"
)
+2 -2
View File
@@ -5,7 +5,7 @@ import (
"net"
"testing"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
)
func TestNetAddressDisplay(t *testing.T) {
@@ -17,7 +17,7 @@ func TestNetAddressDisplay(t *testing.T) {
t.Fatalf("unable to decode str: %v", err)
}
pubKey, err := btcec.ParsePubKey(pubHex, btcec.S256())
pubKey, err := btcec.ParsePubKey(pubHex)
if err != nil {
t.Fatalf("unable to parse pubkey: %v", err)
}
+2 -2
View File
@@ -4,9 +4,9 @@ import (
"bytes"
"io"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/tlv"
)
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"bytes"
"io"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
)
// RevokeAndAck is sent by either side once a CommitSig message has been
+8 -8
View File
@@ -3,14 +3,14 @@ package lnwire
import (
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/lightningnetwork/lnd/input"
)
// Sig is a fixed-sized ECDSA signature. Unlike Bitcoin, we use fixed sized
// signatures on the wire, instead of DER encoded signatures. This type
// provides several methods to convert to/from a regular Bitcoin DER encoded
// signature (raw bytes and *btcec.Signature).
// signature (raw bytes and *ecdsa.Signature).
type Sig [64]byte
// NewSigFromRawSignature returns a Sig from a Bitcoin raw signature encoded in
@@ -27,7 +27,7 @@ func NewSigFromRawSignature(sig []byte) (Sig, error) {
// which means the length of R is the 4th byte and the length of S
// is the second byte after R ends. 0x02 signifies a length-prefixed,
// zero-padded, big-endian bigint. 0x30 signifies a DER signature.
// See the Serialize() method for btcec.Signature for details.
// See the Serialize() method for ecdsa.Signature for details.
rLen := sig[3]
sLen := sig[5+rLen]
@@ -64,7 +64,7 @@ func NewSigFromRawSignature(sig []byte) (Sig, error) {
}
// NewSigFromSignature creates a new signature as used on the wire, from an
// existing btcec.Signature.
// existing ecdsa.Signature.
func NewSigFromSignature(e input.Signature) (Sig, error) {
if e == nil {
return Sig{}, fmt.Errorf("cannot decode empty signature")
@@ -72,7 +72,7 @@ func NewSigFromSignature(e input.Signature) (Sig, error) {
// Nil is still a valid interface, apparently. So we need a more
// explicit check here.
if ecsig, ok := e.(*btcec.Signature); ok && ecsig == nil {
if ecsig, ok := e.(*ecdsa.Signature); ok && ecsig == nil {
return Sig{}, fmt.Errorf("cannot decode empty signature")
}
@@ -80,12 +80,12 @@ func NewSigFromSignature(e input.Signature) (Sig, error) {
return NewSigFromRawSignature(e.Serialize())
}
// ToSignature converts the fixed-sized signature to a btcec.Signature objects
// ToSignature converts the fixed-sized signature to a ecdsa.Signature objects
// which can be used for signature validation checks.
func (b *Sig) ToSignature() (*btcec.Signature, error) {
func (b *Sig) ToSignature() (*ecdsa.Signature, error) {
// Parse the signature with strict checks.
sigBytes := b.ToSignatureBytes()
sig, err := btcec.ParseDERSignature(sigBytes, btcec.S256())
sig, err := ecdsa.ParseDERSignature(sigBytes)
if err != nil {
return nil, err
}
+35 -31
View File
@@ -5,7 +5,8 @@ import (
"math/big"
"testing"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
)
func TestSignatureSerializeDeserialize(t *testing.T) {
@@ -13,8 +14,8 @@ func TestSignatureSerializeDeserialize(t *testing.T) {
// Local-scoped closure to serialize and deserialize a Signature and
// check for errors as well as check if the results are correct.
signatureSerializeDeserialize := func(e btcec.Signature) error {
sig, err := NewSigFromSignature(&e)
signatureSerializeDeserialize := func(e *ecdsa.Signature) error {
sig, err := NewSigFromSignature(e)
if err != nil {
return err
}
@@ -24,67 +25,70 @@ func TestSignatureSerializeDeserialize(t *testing.T) {
return err
}
if e.R.Cmp(e2.R) != 0 {
return fmt.Errorf("Pre/post-serialize Rs don't match"+
": %s, %s", e.R, e2.R)
}
if e.S.Cmp(e2.S) != 0 {
return fmt.Errorf("Pre/post-serialize Ss don't match"+
": %s, %s", e.S, e2.S)
if !e.IsEqual(e2) {
return fmt.Errorf("pre/post-serialize sigs don't " +
"match")
}
return nil
}
sig := btcec.Signature{}
// Check R = N-1, S = 128.
sig.R = big.NewInt(1) // Allocate a big.Int before we call .Sub.
sig.R.Sub(btcec.S256().N, sig.R)
sig.S = big.NewInt(128)
r := big.NewInt(1) // Allocate a big.Int before we call .Sub.
r.Sub(btcec.S256().N, r)
rScalar := new(btcec.ModNScalar)
rScalar.SetByteSlice(r.Bytes())
sig := ecdsa.NewSignature(rScalar, new(btcec.ModNScalar).SetInt(128))
err := signatureSerializeDeserialize(sig)
if err != nil {
t.Fatalf("R = N-1, S = 128: %s", err.Error())
}
// Check R = N-1, S = 127.
sig.S = big.NewInt(127)
sig = ecdsa.NewSignature(rScalar, new(btcec.ModNScalar).SetInt(127))
err = signatureSerializeDeserialize(sig)
if err != nil {
t.Fatalf("R = N-1, S = 127: %s", err.Error())
}
// Check R = N-1, S = N>>1.
sig.S.Set(btcec.S256().N)
sig.S.Rsh(sig.S, 1)
s := new(big.Int).Set(btcec.S256().N)
s.Rsh(s, 1)
sScalar := new(btcec.ModNScalar)
sScalar.SetByteSlice(s.Bytes())
sig = ecdsa.NewSignature(rScalar, sScalar)
err = signatureSerializeDeserialize(sig)
if err != nil {
t.Fatalf("R = N-1, S = N>>1: %s", err.Error())
}
// Check R = N-1, S = N.
sig.S.Set(btcec.S256().N)
s = new(big.Int).Set(btcec.S256().N)
overflow := sScalar.SetByteSlice(s.Bytes())
if !overflow {
t.Fatalf("Expect ModNScalar to overflow when setting N but " +
"didn't")
}
sig = ecdsa.NewSignature(rScalar, sScalar)
err = signatureSerializeDeserialize(sig)
if err.Error() != "signature S isn't 1 or more" {
if err.Error() != "invalid signature: S is 0" {
t.Fatalf("R = N-1, S = N should become R = N-1, S = 0: %s",
err.Error())
}
// Check R = N-1, S = N-1.
sig.S.Sub(sig.S, big.NewInt(1))
s = new(big.Int).Set(btcec.S256().N)
s.Sub(s, big.NewInt(1))
sScalar.SetByteSlice(s.Bytes())
sig = ecdsa.NewSignature(rScalar, sScalar)
err = signatureSerializeDeserialize(sig)
if err.Error() != "Pre/post-serialize Ss don't match: 115792089237316"+
"195423570985008687907852837564279074904382605163141518161494"+
"336, 1" {
if err.Error() != "pre/post-serialize sigs don't match" {
t.Fatalf("R = N-1, S = N-1 should become R = N-1, S = 1: %s",
err.Error())
}
// Check R = 2N, S = 128
sig.R.Mul(btcec.S256().N, big.NewInt(2))
sig.S.Set(big.NewInt(127))
err = signatureSerializeDeserialize(sig)
if err.Error() != "R is over 32 bytes long without padding" {
t.Fatalf("R = 2N, S = 128, R should be over 32 bytes: %s",
err.Error())
}
// This cannot be tested anymore since the new ecdsa package creates
// the signature from ModNScalar values which don't allow setting a
// value larger than N (hence the name mod n).
}
+2 -2
View File
@@ -9,9 +9,9 @@ import (
"math"
"net"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/tor"
)
+1 -1
View File
@@ -8,9 +8,9 @@ import (
"net"
"testing"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/tor"
"github.com/stretchr/testify/require"
)