diff --git a/signature_test.go b/signature_test.go index 130e49ed..02dd3d2e 100644 --- a/signature_test.go +++ b/signature_test.go @@ -5,7 +5,9 @@ package btcec_test import ( + "bytes" "github.com/conformal/btcec" + "math/big" "testing" ) @@ -331,3 +333,90 @@ func TestSignatures(t *testing.T) { } } } + +// TestSignatureSerialize ensures that serializing signatures works as expected. +func TestSignatureSerialize(t *testing.T) { + tests := []struct { + name string + ecsig *btcec.Signature + expected []byte + }{ + // signature from bitcoin blockchain tx + // 0437cd7f8525ceed2324359c2d0ba26006d92d85 + { + "valid 1 - r and s most significant bits are zero", + &btcec.Signature{ + R: fromHex("4e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd41"), + S: fromHex("181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d09"), + }, + []byte{ + 0x30, 0x44, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69, + 0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, + 0xa1, 0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, + 0xe9, 0xd6, 0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, + 0x5f, 0xb8, 0xcd, 0x41, 0x02, 0x20, 0x18, 0x15, + 0x22, 0xec, 0x8e, 0xca, 0x07, 0xde, 0x48, 0x60, + 0xa4, 0xac, 0xdd, 0x12, 0x90, 0x9d, 0x83, 0x1c, + 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22, 0x08, 0x22, + 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09, + }, + }, + // signature from bitcoin blockchain tx + // cb00f8a0573b18faa8c4f467b049f5d202bf1101d9ef2633bc611be70376a4b4 + { + "valid 2 - r most significant bit is one", + &btcec.Signature{ + R: fromHex("0082235e21a2300022738dabb8e1bbd9d19cfb1e7ab8c30a23b0afbb8d178abcf3"), + S: fromHex("24bf68e256c534ddfaf966bf908deb944305596f7bdcc38d69acad7f9c868724"), + }, + []byte{ + 0x30, 0x45, 0x02, 0x21, 0x00, 0x82, 0x23, 0x5e, + 0x21, 0xa2, 0x30, 0x00, 0x22, 0x73, 0x8d, 0xab, + 0xb8, 0xe1, 0xbb, 0xd9, 0xd1, 0x9c, 0xfb, 0x1e, + 0x7a, 0xb8, 0xc3, 0x0a, 0x23, 0xb0, 0xaf, 0xbb, + 0x8d, 0x17, 0x8a, 0xbc, 0xf3, 0x02, 0x20, 0x24, + 0xbf, 0x68, 0xe2, 0x56, 0xc5, 0x34, 0xdd, 0xfa, + 0xf9, 0x66, 0xbf, 0x90, 0x8d, 0xeb, 0x94, 0x43, + 0x05, 0x59, 0x6f, 0x7b, 0xdc, 0xc3, 0x8d, 0x69, + 0xac, 0xad, 0x7f, 0x9c, 0x86, 0x87, 0x24, + }, + }, + // signature from bitcoin blockchain tx + // fda204502a3345e08afd6af27377c052e77f1fefeaeb31bdd45f1e1237ca5470 + { + "valid 3 - s most significant bit is one", + &btcec.Signature{ + R: fromHex("1cadddc2838598fee7dc35a12b340c6bde8b389f7bfd19a1252a17c4b5ed2d71"), + S: fromHex("00c1a251bbecb14b058a8bd77f65de87e51c47e95904f4c0e9d52eddc21c1415ac"), + }, + []byte{ + 0x30, 0x45, 0x02, 0x20, 0x1c, 0xad, 0xdd, 0xc2, + 0x83, 0x85, 0x98, 0xfe, 0xe7, 0xdc, 0x35, 0xa1, + 0x2b, 0x34, 0x0c, 0x6b, 0xde, 0x8b, 0x38, 0x9f, + 0x7b, 0xfd, 0x19, 0xa1, 0x25, 0x2a, 0x17, 0xc4, + 0xb5, 0xed, 0x2d, 0x71, 0x02, 0x21, 0x00, 0xc1, + 0xa2, 0x51, 0xbb, 0xec, 0xb1, 0x4b, 0x05, 0x8a, + 0x8b, 0xd7, 0x7f, 0x65, 0xde, 0x87, 0xe5, 0x1c, + 0x47, 0xe9, 0x59, 0x04, 0xf4, 0xc0, 0xe9, 0xd5, + 0x2e, 0xdd, 0xc2, 0x1c, 0x14, 0x15, 0xac, + }, + }, + { + "zero signature", + &btcec.Signature{ + R: big.NewInt(0), + S: big.NewInt(0), + }, + []byte{0x30, 0x06, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00}, + }, + } + + for i, test := range tests { + result := test.ecsig.Serialize() + if !bytes.Equal(result, test.expected) { + t.Errorf("Serialize #%d (%s) unexpected result:\n"+ + "got: %x\nwant: %x", i, test.name, result, + test.expected) + } + } +} diff --git a/test_coverage.txt b/test_coverage.txt index 2295eefc..ce26afcc 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -1,8 +1,8 @@ github.com/conformal/btcec/field.go fieldVal.Normalize 100.00% (93/93) github.com/conformal/btcec/field.go fieldVal.Inverse 100.00% (88/88) -github.com/conformal/btcec/field.go fieldVal.Mul2 100.00% (73/73) github.com/conformal/btcec/field.go fieldVal.SquareVal 100.00% (73/73) +github.com/conformal/btcec/field.go fieldVal.Mul2 100.00% (73/73) github.com/conformal/btcec/signature.go parseSig 100.00% (51/51) github.com/conformal/btcec/btcec.go KoblitzCurve.addGeneric 100.00% (35/35) github.com/conformal/btcec/btcec.go KoblitzCurve.addZ2EqualsOne 100.00% (35/35) @@ -12,47 +12,48 @@ github.com/conformal/btcec/btcec.go KoblitzCurve.addZ1AndZ2EqualsOne 100.00% ( github.com/conformal/btcec/btcec.go KoblitzCurve.addJacobian 100.00% (22/22) github.com/conformal/btcec/btcec.go KoblitzCurve.doubleZ1EqualsOne 100.00% (18/18) github.com/conformal/btcec/btcec.go KoblitzCurve.doubleGeneric 100.00% (18/18) +github.com/conformal/btcec/signature.go Signature.Serialize 100.00% (13/13) github.com/conformal/btcec/field.go fieldVal.MulInt 100.00% (12/12) github.com/conformal/btcec/btcec.go KoblitzCurve.fieldJacobianToBigAffine 100.00% (12/12) -github.com/conformal/btcec/field.go fieldVal.Add 100.00% (11/11) github.com/conformal/btcec/field.go fieldVal.Add2 100.00% (11/11) -github.com/conformal/btcec/field.go fieldVal.SetBytes 100.00% (11/11) +github.com/conformal/btcec/field.go fieldVal.Add 100.00% (11/11) github.com/conformal/btcec/field.go fieldVal.NegateVal 100.00% (11/11) +github.com/conformal/btcec/field.go fieldVal.SetBytes 100.00% (11/11) github.com/conformal/btcec/btcec.go KoblitzCurve.ScalarMult 100.00% (10/10) github.com/conformal/btcec/field.go fieldVal.Zero 100.00% (10/10) github.com/conformal/btcec/btcec.go KoblitzCurve.doubleJacobian 100.00% (9/9) github.com/conformal/btcec/btcec.go KoblitzCurve.Add 100.00% (9/9) github.com/conformal/btcec/btcec.go initS256 100.00% (8/8) +github.com/conformal/btcec/signature.go canonicalizeInt 100.00% (8/8) github.com/conformal/btcec/pubkey.go PublicKey.SerializeHybrid 100.00% (8/8) github.com/conformal/btcec/pubkey.go PublicKey.SerializeCompressed 100.00% (7/7) github.com/conformal/btcec/btcec.go KoblitzCurve.Double 100.00% (6/6) github.com/conformal/btcec/field.go fieldVal.SetByteSlice 100.00% (5/5) github.com/conformal/btcec/pubkey.go PublicKey.SerializeUncompressed 100.00% (5/5) +github.com/conformal/btcec/btcec.go KoblitzCurve.IsOnCurve 100.00% (4/4) github.com/conformal/btcec/signature.go canonicalPadding 100.00% (4/4) github.com/conformal/btcec/field.go fieldVal.SetHex 100.00% (4/4) github.com/conformal/btcec/btcec.go KoblitzCurve.bigAffineToField 100.00% (4/4) -github.com/conformal/btcec/btcec.go KoblitzCurve.IsOnCurve 100.00% (4/4) github.com/conformal/btcec/field.go fieldVal.SetInt 100.00% (3/3) github.com/conformal/btcec/field.go fieldVal.Bytes 100.00% (3/3) -github.com/conformal/btcec/field.go fieldVal.Set 100.00% (2/2) github.com/conformal/btcec/field.go fieldVal.String 100.00% (2/2) +github.com/conformal/btcec/field.go fieldVal.Set 100.00% (2/2) github.com/conformal/btcec/btcec.go S256 100.00% (2/2) github.com/conformal/btcec/field.go fieldVal.IsZero 100.00% (2/2) github.com/conformal/btcec/field.go fieldVal.Equals 100.00% (2/2) github.com/conformal/btcec/field.go fieldVal.AddInt 100.00% (2/2) -github.com/conformal/btcec/field.go fieldVal.Square 100.00% (1/1) -github.com/conformal/btcec/btcec.go KoblitzCurve.ScalarBaseMult 100.00% (1/1) -github.com/conformal/btcec/btcec.go KoblitzCurve.Params 100.00% (1/1) -github.com/conformal/btcec/field.go NewFieldVal 100.00% (1/1) +github.com/conformal/btcec/field.go fieldVal.Mul 100.00% (1/1) github.com/conformal/btcec/pubkey.go isOdd 100.00% (1/1) github.com/conformal/btcec/signature.go ParseDERSignature 100.00% (1/1) -github.com/conformal/btcec/field.go fieldVal.Mul 100.00% (1/1) -github.com/conformal/btcec/btcec.go KoblitzCurve.QPlus1Div4 100.00% (1/1) -github.com/conformal/btcec/btcec.go initAll 100.00% (1/1) -github.com/conformal/btcec/field.go fieldVal.Negate 100.00% (1/1) github.com/conformal/btcec/field.go fieldVal.IsOdd 100.00% (1/1) +github.com/conformal/btcec/btcec.go KoblitzCurve.Params 100.00% (1/1) +github.com/conformal/btcec/btcec.go KoblitzCurve.ScalarBaseMult 100.00% (1/1) +github.com/conformal/btcec/btcec.go KoblitzCurve.QPlus1Div4 100.00% (1/1) +github.com/conformal/btcec/field.go fieldVal.Square 100.00% (1/1) +github.com/conformal/btcec/field.go fieldVal.Negate 100.00% (1/1) +github.com/conformal/btcec/btcec.go initAll 100.00% (1/1) github.com/conformal/btcec/signature.go ParseSignature 100.00% (1/1) github.com/conformal/btcec/pubkey.go ParsePubKey 96.88% (31/32) github.com/conformal/btcec/pubkey.go pad 80.00% (4/5) -github.com/conformal/btcec ------------------------------------- 99.76% (823/825) +github.com/conformal/btcec ------------------------------------- 99.76% (843/845)