github.com/igggame/nebulas-go@v2.1.0+incompatible/crypto/keystore/secp256k1/ecdsa_test.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package secp256k1
    20  
    21  import (
    22  	"testing"
    23  
    24  	"crypto/ecdsa"
    25  	"crypto/rand"
    26  	"io"
    27  
    28  	"github.com/nebulasio/go-nebulas/crypto/hash"
    29  	"github.com/nebulasio/go-nebulas/util/byteutils"
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestFromECDSAPri(t *testing.T) {
    34  	priv, err := NewECDSAPrivateKey()
    35  	assert.Nil(t, err)
    36  	_, err = FromECDSAPrivateKey(priv)
    37  	assert.Nil(t, err)
    38  }
    39  
    40  func TestFromECDSAPub(t *testing.T) {
    41  	priv, err := NewECDSAPrivateKey()
    42  	assert.Nil(t, err)
    43  	_, err = FromECDSAPublicKey(&priv.PublicKey)
    44  	assert.Nil(t, err)
    45  }
    46  
    47  func TestToECDSAPrivate(t *testing.T) {
    48  	priv, err := NewECDSAPrivateKey()
    49  	assert.Nil(t, err)
    50  	privByte, err := FromECDSAPrivateKey(priv)
    51  	assert.Nil(t, err)
    52  	aPriv, err := ToECDSAPrivateKey(privByte)
    53  	assert.Nil(t, err)
    54  	assert.Equal(t, priv, aPriv)
    55  }
    56  
    57  func TestToECDSAPublic(t *testing.T) {
    58  	priv, err := NewECDSAPrivateKey()
    59  	assert.Nil(t, err)
    60  	pubByte, err := FromECDSAPublicKey(&priv.PublicKey)
    61  	assert.Nil(t, err)
    62  	_, err = ToECDSAPublicKey(pubByte)
    63  	assert.Nil(t, err)
    64  }
    65  
    66  func TestSign(t *testing.T) {
    67  	type args struct {
    68  		s []byte
    69  	}
    70  	type test struct {
    71  		name  string
    72  		priv  *ecdsa.PrivateKey
    73  		args  args
    74  		count int
    75  	}
    76  
    77  	tests := []test{}
    78  	for index := 0; index < 10; index++ {
    79  		mainBuff := make([]byte, 32)
    80  		io.ReadFull(rand.Reader, mainBuff)
    81  		priv, err := NewECDSAPrivateKey()
    82  		assert.Nil(t, err)
    83  		test := test{string(index), priv, args{hash.Sha3256(mainBuff)}, 1}
    84  		tests = append(tests, test)
    85  	}
    86  	for _, tt := range tests {
    87  		for index := 0; index < tt.count; index++ {
    88  			t.Run(tt.name, func(t *testing.T) {
    89  				privData, err := FromECDSAPrivateKey(tt.priv)
    90  				assert.Nil(t, err)
    91  				got, err := Sign(tt.args.s, privData)
    92  				assert.Nil(t, err)
    93  				gpub, err := RecoverECDSAPublicKey(tt.args.s, got)
    94  				assert.Nil(t, err)
    95  				originPub, err := FromECDSAPublicKey(&tt.priv.PublicKey)
    96  				assert.Nil(t, err)
    97  				if !byteutils.Equal(originPub, gpub) {
    98  					t.Errorf("recover failed: pub not equal")
    99  					seckey, _ := FromECDSAPrivateKey(tt.priv)
   100  					t.Log("private:", byteutils.Hex(seckey))
   101  					t.Log("public:", byteutils.Hex(originPub))
   102  					return
   103  				}
   104  			})
   105  		}
   106  	}
   107  }