github.com/core-coin/go-core/v2@v2.1.9/crypto/signature_test.go (about)

     1  // Copyright 2017 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-core library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package crypto
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"github.com/core-coin/go-core/v2/common"
    24  	"github.com/core-coin/go-core/v2/common/hexutil"
    25  )
    26  
    27  var (
    28  	testmsg    = common.Hex2Bytes("ce0677bb30baa8cf067c88db9811f4333d131bf8bcf12fe7065d211dce971008")
    29  	testsig    = common.Hex2Bytes("ea535a535ff0dbfda0b2c1394bad87311789c1c6eafe6eef48fd509c2e7ba0e67c4774fab8c45abf1c7e22532bb816115bf1da8438fdb81e00e13ca01494adc201c9c35bc32cdd7c1922a0b1121f1d8ed72b37786dfd6e5583b06ad172bdb4f1d2afd41b4444abd2b5901c851fcb3d641200fadc64a37e95ad1bcbaf19625bf95826e6a8cbab42b57fc91b72da98d26bae8bda2d1fc52c508a03724aded17b8cef8253f2116307bbbf7580")
    30  	testpubkey = common.Hex2Bytes("fadc64a37e95ad1bcbaf19625bf95826e6a8cbab42b57fc91b72da98d26bae8bda2d1fc52c508a03724aded17b8cef8253f2116307bbbf7580")
    31  )
    32  
    33  func TestEcrecover(t *testing.T) {
    34  	pubkey, err := Ecrecover(testmsg, testsig)
    35  	if err != nil {
    36  		t.Fatalf("recover error: %s", err)
    37  	}
    38  	if !bytes.Equal(pubkey, testpubkey) {
    39  		t.Errorf("pubkey mismatch: want: %x have: %x", testpubkey, pubkey)
    40  	}
    41  }
    42  
    43  func TestVerifySignature(t *testing.T) {
    44  	if !VerifySignature(testpubkey, testmsg, testsig) {
    45  		t.Errorf("can't verify signature with pub key")
    46  	}
    47  
    48  	if VerifySignature(nil, testmsg, testsig) {
    49  		t.Errorf("signature valid with no key")
    50  	}
    51  	if VerifySignature(testpubkey, nil, testsig) {
    52  		t.Errorf("signature valid with no message")
    53  	}
    54  	if VerifySignature(testpubkey, testmsg, nil) {
    55  		t.Errorf("nil signature valid")
    56  	}
    57  	if VerifySignature(testpubkey, testmsg, append(common.CopyBytes(testsig), 1, 2, 3)) {
    58  		t.Errorf("signature valid with extra bytes at the end")
    59  	}
    60  	if VerifySignature(testpubkey, testmsg, testsig[:len(testsig)-2]) {
    61  		t.Errorf("signature valid even though it's incomplete")
    62  	}
    63  	wrongkey := common.CopyBytes(testpubkey)
    64  	wrongkey[10]++
    65  	if VerifySignature(wrongkey, testmsg, testsig) {
    66  		t.Errorf("signature valid with with wrong public key")
    67  	}
    68  }
    69  
    70  // This test checks that VerifySignature rejects malleable signatures with s > N/2.
    71  func TestVerifySignatureMalleable(t *testing.T) {
    72  	sig := hexutil.MustDecode("0x638a54215d80a6713c8d523a6adc4e6e73652d859103a36b700851cb0e61b66b8ebfc1a610c57d732ec6e0a8f06a9a7a28df5051ece514702ff9cdff0b11f454")
    73  	key := hexutil.MustDecode("0x03ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd3138")
    74  	msg := hexutil.MustDecode("0xd301ce462d3e639518f482c7f03821fec1e602018630ce621e1e7851c12343a6")
    75  	if VerifySignature(key, msg, sig) {
    76  		t.Error("VerifySignature returned true for malleable signature")
    77  	}
    78  }
    79  
    80  func BenchmarkEcrecoverSignature(b *testing.B) {
    81  	for i := 0; i < b.N; i++ {
    82  		if _, err := Ecrecover(testmsg, testsig); err != nil {
    83  			b.Fatal("ecrecover error", err)
    84  		}
    85  	}
    86  }
    87  
    88  func BenchmarkVerifySignature(b *testing.B) {
    89  	sig := testsig[:len(testsig)-1] // remove recovery id
    90  	for i := 0; i < b.N; i++ {
    91  		if !VerifySignature(testpubkey, testmsg, sig) {
    92  			b.Fatal("verify error")
    93  		}
    94  	}
    95  }