github.com/decred/dcrlnd@v0.7.6/channeldb/migration/lnwire21/signature_test.go (about) 1 package lnwire 2 3 import ( 4 "math/big" 5 "testing" 6 7 secpv2 "github.com/decred/dcrd/dcrec/secp256k1/v2" 8 "github.com/decred/dcrd/dcrec/secp256k1/v4" 9 ) 10 11 func TestSignatureSerializeDeserialize(t *testing.T) { 12 t.Parallel() 13 14 // Local-scoped closure to serialize and deserialize a Signature and 15 // check for errors as well as check if the results are correct. 16 signatureSerializeDeserialize := func(e secpv2.Signature) error { 17 // NOTE(decred): Instead of using NewSigFromSignature we use 18 // NewSigFromRawSignature due to performing the test with the 19 // v2 secp256k1 lib that allows internal access to R and S and 20 // allows creating invalid encoded sigs. 21 sig, err := NewSigFromRawSignature(e.Serialize()) 22 if err != nil { 23 return err 24 } 25 26 _, err = sig.ToSignature() 27 if err != nil { 28 return err 29 } 30 31 // NOTE(decred) These are commented because the secp256k1/v4 32 // doesn't provide access to the internal R and S. 33 /* 34 if e.R.Cmp(e2.R) != 0 { 35 return fmt.Errorf("Pre/post-serialize Rs don't match"+ 36 ": %s, %s", e.R, e2.R) 37 } 38 if e.S.Cmp(e2.S) != 0 { 39 return fmt.Errorf("Pre/post-serialize Ss don't match"+ 40 ": %s, %s", e.S, e2.S) 41 } 42 */ 43 return nil 44 } 45 46 sig := secpv2.Signature{} 47 48 // Check R = N-1, S = 128. 49 sig.R = big.NewInt(1) // Allocate a big.Int before we call .Sub. 50 sig.R.Sub(secp256k1.S256().N, sig.R) 51 sig.S = big.NewInt(128) 52 err := signatureSerializeDeserialize(sig) 53 if err != nil { 54 t.Fatalf("R = N-1, S = 128: %s", err.Error()) 55 } 56 57 // Check R = N-1, S = 127. 58 sig.S = big.NewInt(127) 59 err = signatureSerializeDeserialize(sig) 60 if err != nil { 61 t.Fatalf("R = N-1, S = 127: %s", err.Error()) 62 } 63 64 // Check R = N-1, S = N>>1. 65 sig.S.Set(secp256k1.S256().N) 66 sig.S.Rsh(sig.S, 1) 67 err = signatureSerializeDeserialize(sig) 68 if err != nil { 69 t.Fatalf("R = N-1, S = N>>1: %s", err.Error()) 70 } 71 72 // Check R = N-1, S = N. 73 sig.S.Set(secp256k1.S256().N) 74 err = signatureSerializeDeserialize(sig) 75 if err.Error() != "invalid signature: S is 0" { 76 t.Fatalf("R = N-1, S = N should become R = N-1, S = 0: %s", 77 err.Error()) 78 } 79 80 // Check R = N-1, S = N-1. 81 sig.S.Sub(sig.S, big.NewInt(1)) 82 // NOTE(decred): this is commented because secp256k1/v4 doesn't provide 83 // access to R and S for the comparison to be performed. 84 /* 85 err = signatureSerializeDeserialize(sig) 86 if err.Error() != "Pre/post-serialize Ss don't match: 115792089237316"+ 87 "195423570985008687907852837564279074904382605163141518161494"+ 88 "336, 1" { 89 t.Fatalf("R = N-1, S = N-1 should become R = N-1, S = 1: %s", 90 err.Error()) 91 } 92 */ 93 94 // Check R = 2N, S = 128 95 sig.R.Mul(secp256k1.S256().N, big.NewInt(2)) 96 sig.S.Set(big.NewInt(127)) 97 err = signatureSerializeDeserialize(sig) 98 if err.Error() != "R is over 32 bytes long without padding" { 99 t.Fatalf("R = 2N, S = 128, R should be over 32 bytes: %s", 100 err.Error()) 101 } 102 }