github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/btcec/pubkey_test.go (about)

     1  // Copyright (c) 2013-2014 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package btcec_test
     7  
     8  import (
     9  	"bytes"
    10  	"testing"
    11  
    12  	"github.com/dashpay/godash/btcec"
    13  	"github.com/davecgh/go-spew/spew"
    14  )
    15  
    16  type pubKeyTest struct {
    17  	name    string
    18  	key     []byte
    19  	format  byte
    20  	isValid bool
    21  }
    22  
    23  var pubKeyTests = []pubKeyTest{
    24  	// pubkey from bitcoin blockchain tx
    25  	// 0437cd7f8525ceed2324359c2d0ba26006d92d85
    26  	{
    27  		name: "uncompressed ok",
    28  		key: []byte{0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
    29  			0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
    30  			0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
    31  			0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
    32  			0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
    33  			0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
    34  			0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
    35  			0xb4, 0x12, 0xa3,
    36  		},
    37  		isValid: true,
    38  		format:  btcec.TstPubkeyUncompressed,
    39  	},
    40  	{
    41  		name: "uncompressed x changed",
    42  		key: []byte{0x04, 0x15, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
    43  			0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
    44  			0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
    45  			0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
    46  			0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
    47  			0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
    48  			0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
    49  			0xb4, 0x12, 0xa3,
    50  		},
    51  		isValid: false,
    52  	},
    53  	{
    54  		name: "uncompressed y changed",
    55  		key: []byte{0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
    56  			0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
    57  			0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
    58  			0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
    59  			0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
    60  			0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
    61  			0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
    62  			0xb4, 0x12, 0xa4,
    63  		},
    64  		isValid: false,
    65  	},
    66  	{
    67  		name: "uncompressed claims compressed",
    68  		key: []byte{0x03, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
    69  			0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
    70  			0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
    71  			0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
    72  			0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
    73  			0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
    74  			0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
    75  			0xb4, 0x12, 0xa3,
    76  		},
    77  		isValid: false,
    78  	},
    79  	{
    80  		name: "uncompressed as hybrid ok",
    81  		key: []byte{0x07, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
    82  			0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
    83  			0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
    84  			0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
    85  			0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
    86  			0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
    87  			0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
    88  			0xb4, 0x12, 0xa3,
    89  		},
    90  		isValid: true,
    91  		format:  btcec.TstPubkeyHybrid,
    92  	},
    93  	{
    94  		name: "uncompressed as hybrid wrong",
    95  		key: []byte{0x06, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
    96  			0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
    97  			0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
    98  			0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
    99  			0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
   100  			0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
   101  			0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
   102  			0xb4, 0x12, 0xa3,
   103  		},
   104  		isValid: false,
   105  	},
   106  	// from tx 0b09c51c51ff762f00fb26217269d2a18e77a4fa87d69b3c363ab4df16543f20
   107  	{
   108  		name: "compressed ok (ybit = 0)",
   109  		key: []byte{0x02, 0xce, 0x0b, 0x14, 0xfb, 0x84, 0x2b, 0x1b,
   110  			0xa5, 0x49, 0xfd, 0xd6, 0x75, 0xc9, 0x80, 0x75, 0xf1,
   111  			0x2e, 0x9c, 0x51, 0x0f, 0x8e, 0xf5, 0x2b, 0xd0, 0x21,
   112  			0xa9, 0xa1, 0xf4, 0x80, 0x9d, 0x3b, 0x4d,
   113  		},
   114  		isValid: true,
   115  		format:  btcec.TstPubkeyCompressed,
   116  	},
   117  	// from tx fdeb8e72524e8dab0da507ddbaf5f88fe4a933eb10a66bc4745bb0aa11ea393c
   118  	{
   119  		name: "compressed ok (ybit = 1)",
   120  		key: []byte{0x03, 0x26, 0x89, 0xc7, 0xc2, 0xda, 0xb1, 0x33,
   121  			0x09, 0xfb, 0x14, 0x3e, 0x0e, 0x8f, 0xe3, 0x96, 0x34,
   122  			0x25, 0x21, 0x88, 0x7e, 0x97, 0x66, 0x90, 0xb6, 0xb4,
   123  			0x7f, 0x5b, 0x2a, 0x4b, 0x7d, 0x44, 0x8e,
   124  		},
   125  		isValid: true,
   126  		format:  btcec.TstPubkeyCompressed,
   127  	},
   128  	{
   129  		name: "compressed claims uncompressed (ybit = 0)",
   130  		key: []byte{0x04, 0xce, 0x0b, 0x14, 0xfb, 0x84, 0x2b, 0x1b,
   131  			0xa5, 0x49, 0xfd, 0xd6, 0x75, 0xc9, 0x80, 0x75, 0xf1,
   132  			0x2e, 0x9c, 0x51, 0x0f, 0x8e, 0xf5, 0x2b, 0xd0, 0x21,
   133  			0xa9, 0xa1, 0xf4, 0x80, 0x9d, 0x3b, 0x4d,
   134  		},
   135  		isValid: false,
   136  	},
   137  	{
   138  		name: "compressed claims uncompressed (ybit = 1)",
   139  		key: []byte{0x05, 0x26, 0x89, 0xc7, 0xc2, 0xda, 0xb1, 0x33,
   140  			0x09, 0xfb, 0x14, 0x3e, 0x0e, 0x8f, 0xe3, 0x96, 0x34,
   141  			0x25, 0x21, 0x88, 0x7e, 0x97, 0x66, 0x90, 0xb6, 0xb4,
   142  			0x7f, 0x5b, 0x2a, 0x4b, 0x7d, 0x44, 0x8e,
   143  		},
   144  		isValid: false,
   145  	},
   146  	{
   147  		name:    "wrong length)",
   148  		key:     []byte{0x05},
   149  		isValid: false,
   150  	},
   151  	{
   152  		name: "X == P",
   153  		key: []byte{0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   154  			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   155  			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   156  			0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFC, 0x2F, 0xb2, 0xe0,
   157  			0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
   158  			0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
   159  			0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
   160  			0xb4, 0x12, 0xa3,
   161  		},
   162  		isValid: false,
   163  	},
   164  	{
   165  		name: "X > P",
   166  		key: []byte{0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   167  			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   168  			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   169  			0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFD, 0x2F, 0xb2, 0xe0,
   170  			0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
   171  			0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
   172  			0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
   173  			0xb4, 0x12, 0xa3,
   174  		},
   175  		isValid: false,
   176  	},
   177  	{
   178  		name: "Y == P",
   179  		key: []byte{0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
   180  			0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
   181  			0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
   182  			0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xFF, 0xFF,
   183  			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   184  			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   185  			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF,
   186  			0xFF, 0xFC, 0x2F,
   187  		},
   188  		isValid: false,
   189  	},
   190  	{
   191  		name: "Y > P",
   192  		key: []byte{0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
   193  			0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
   194  			0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
   195  			0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xFF, 0xFF,
   196  			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   197  			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
   198  			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF,
   199  			0xFF, 0xFD, 0x2F,
   200  		},
   201  		isValid: false,
   202  	},
   203  	{
   204  		name: "hybrid",
   205  		key: []byte{0x06, 0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb,
   206  			0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07,
   207  			0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59,
   208  			0xf2, 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
   209  			0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4, 0xfb,
   210  			0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17, 0xb4, 0x48,
   211  			0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47, 0xd0, 0x8f, 0xfb,
   212  			0x10, 0xd4, 0xb8,
   213  		},
   214  		format:  btcec.TstPubkeyHybrid,
   215  		isValid: true,
   216  	},
   217  }
   218  
   219  func TestPubKeys(t *testing.T) {
   220  	for _, test := range pubKeyTests {
   221  		pk, err := btcec.ParsePubKey(test.key, btcec.S256())
   222  		if err != nil {
   223  			if test.isValid {
   224  				t.Errorf("%s pubkey failed when shouldn't %v",
   225  					test.name, err)
   226  			}
   227  			continue
   228  		}
   229  		if !test.isValid {
   230  			t.Errorf("%s counted as valid when it should fail",
   231  				test.name)
   232  			continue
   233  		}
   234  		var pkStr []byte
   235  		switch test.format {
   236  		case btcec.TstPubkeyUncompressed:
   237  			pkStr = (*btcec.PublicKey)(pk).SerializeUncompressed()
   238  		case btcec.TstPubkeyCompressed:
   239  			pkStr = (*btcec.PublicKey)(pk).SerializeCompressed()
   240  		case btcec.TstPubkeyHybrid:
   241  			pkStr = (*btcec.PublicKey)(pk).SerializeHybrid()
   242  		}
   243  		if !bytes.Equal(test.key, pkStr) {
   244  			t.Errorf("%s pubkey: serialized keys do not match.",
   245  				test.name)
   246  			spew.Dump(test.key)
   247  			spew.Dump(pkStr)
   248  		}
   249  	}
   250  }
   251  
   252  func TestPublicKeyIsEqual(t *testing.T) {
   253  	pubKey1, err := btcec.ParsePubKey(
   254  		[]byte{0x03, 0x26, 0x89, 0xc7, 0xc2, 0xda, 0xb1, 0x33,
   255  			0x09, 0xfb, 0x14, 0x3e, 0x0e, 0x8f, 0xe3, 0x96, 0x34,
   256  			0x25, 0x21, 0x88, 0x7e, 0x97, 0x66, 0x90, 0xb6, 0xb4,
   257  			0x7f, 0x5b, 0x2a, 0x4b, 0x7d, 0x44, 0x8e,
   258  		},
   259  		btcec.S256(),
   260  	)
   261  	if err != nil {
   262  		t.Fatalf("failed to parse raw bytes for pubKey1: %v", err)
   263  	}
   264  
   265  	pubKey2, err := btcec.ParsePubKey(
   266  		[]byte{0x02, 0xce, 0x0b, 0x14, 0xfb, 0x84, 0x2b, 0x1b,
   267  			0xa5, 0x49, 0xfd, 0xd6, 0x75, 0xc9, 0x80, 0x75, 0xf1,
   268  			0x2e, 0x9c, 0x51, 0x0f, 0x8e, 0xf5, 0x2b, 0xd0, 0x21,
   269  			0xa9, 0xa1, 0xf4, 0x80, 0x9d, 0x3b, 0x4d,
   270  		},
   271  		btcec.S256(),
   272  	)
   273  	if err != nil {
   274  		t.Fatalf("failed to parse raw bytes for pubKey2: %v", err)
   275  	}
   276  
   277  	if !pubKey1.IsEqual(pubKey1) {
   278  		t.Fatalf("value of IsEqual is incorrect, %v is "+
   279  			"equal to %v", pubKey1, pubKey1)
   280  	}
   281  
   282  	if pubKey1.IsEqual(pubKey2) {
   283  		t.Fatalf("value of IsEqual is incorrect, %v is not "+
   284  			"equal to %v", pubKey1, pubKey2)
   285  	}
   286  }