github.com/lbryio/lbcd@v0.22.119/btcec/privkey_test.go (about)

     1  // Copyright (c) 2013-2016 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package btcec
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  )
    11  
    12  func TestPrivKeys(t *testing.T) {
    13  	tests := []struct {
    14  		name string
    15  		key  []byte
    16  	}{
    17  		{
    18  			name: "check curve",
    19  			key: []byte{
    20  				0xea, 0xf0, 0x2c, 0xa3, 0x48, 0xc5, 0x24, 0xe6,
    21  				0x39, 0x26, 0x55, 0xba, 0x4d, 0x29, 0x60, 0x3c,
    22  				0xd1, 0xa7, 0x34, 0x7d, 0x9d, 0x65, 0xcf, 0xe9,
    23  				0x3c, 0xe1, 0xeb, 0xff, 0xdc, 0xa2, 0x26, 0x94,
    24  			},
    25  		},
    26  	}
    27  
    28  	for _, test := range tests {
    29  		priv, pub := PrivKeyFromBytes(S256(), test.key)
    30  
    31  		_, err := ParsePubKey(pub.SerializeUncompressed(), S256())
    32  		if err != nil {
    33  			t.Errorf("%s privkey: %v", test.name, err)
    34  			continue
    35  		}
    36  
    37  		hash := []byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9}
    38  		sig, err := priv.Sign(hash)
    39  		if err != nil {
    40  			t.Errorf("%s could not sign: %v", test.name, err)
    41  			continue
    42  		}
    43  
    44  		if !sig.Verify(hash, pub) {
    45  			t.Errorf("%s could not verify: %v", test.name, err)
    46  			continue
    47  		}
    48  
    49  		serializedKey := priv.Serialize()
    50  		if !bytes.Equal(serializedKey, test.key) {
    51  			t.Errorf("%s unexpected serialized bytes - got: %x, "+
    52  				"want: %x", test.name, serializedKey, test.key)
    53  		}
    54  	}
    55  }