2016-12-08 23:56:37 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2016-12-08 23:56:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSignatureSerializeDeserialize(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-12-08 23:56:37 +03:00
|
|
|
// 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 {
|
2018-01-31 06:41:52 +03:00
|
|
|
sig, err := NewSigFromSignature(&e)
|
2016-12-08 23:56:37 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-31 06:41:52 +03:00
|
|
|
|
|
|
|
e2, err := sig.ToSignature()
|
2016-12-08 23:56:37 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-31 06:41:52 +03:00
|
|
|
|
2016-12-08 23:56:37 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
err = signatureSerializeDeserialize(sig)
|
|
|
|
if err.Error() != "signature S isn't 1 or more" {
|
|
|
|
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))
|
|
|
|
err = signatureSerializeDeserialize(sig)
|
|
|
|
if err.Error() != "Pre/post-serialize Ss don't match: 115792089237316"+
|
|
|
|
"195423570985008687907852837564279074904382605163141518161494"+
|
|
|
|
"336, 1" {
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|