github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/btcec/privkey_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  )
    14  
    15  func TestPrivKeys(t *testing.T) {
    16  	tests := []struct {
    17  		name string
    18  		key  []byte
    19  	}{
    20  		{
    21  			name: "check curve",
    22  			key: []byte{
    23  				0xea, 0xf0, 0x2c, 0xa3, 0x48, 0xc5, 0x24, 0xe6,
    24  				0x39, 0x26, 0x55, 0xba, 0x4d, 0x29, 0x60, 0x3c,
    25  				0xd1, 0xa7, 0x34, 0x7d, 0x9d, 0x65, 0xcf, 0xe9,
    26  				0x3c, 0xe1, 0xeb, 0xff, 0xdc, 0xa2, 0x26, 0x94,
    27  			},
    28  		},
    29  	}
    30  
    31  	for _, test := range tests {
    32  		priv, pub := btcec.PrivKeyFromBytes(btcec.S256(), test.key)
    33  
    34  		_, err := btcec.ParsePubKey(
    35  			pub.SerializeUncompressed(), btcec.S256())
    36  		if err != nil {
    37  			t.Errorf("%s privkey: %v", test.name, err)
    38  			continue
    39  		}
    40  
    41  		hash := []byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9}
    42  		sig, err := priv.Sign(hash)
    43  		if err != nil {
    44  			t.Errorf("%s could not sign: %v", test.name, err)
    45  			continue
    46  		}
    47  
    48  		if !sig.Verify(hash, pub) {
    49  			t.Errorf("%s could not verify: %v", test.name, err)
    50  			continue
    51  		}
    52  
    53  		serializedKey := priv.Serialize()
    54  		if !bytes.Equal(serializedKey, test.key) {
    55  			t.Errorf("%s unexpected serialized bytes - got: %x, "+
    56  				"want: %x", test.name, serializedKey, test.key)
    57  		}
    58  	}
    59  }