mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-30 07:35:07 +02:00
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:
@@ -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).
|
||||
}
|
||||
|
Reference in New Issue
Block a user