github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/utilities/crypto/signature_test.go (about)

     1  package crypto
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/ecdsa"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/neatlab/neatio/utilities/common"
    10  	"github.com/neatlab/neatio/utilities/common/hexutil"
    11  	"github.com/neatlab/neatio/utilities/common/math"
    12  )
    13  
    14  var (
    15  	testmsg     = hexutil.MustDecode("0xce0677bb30baa8cf067c88db9811f4333d131bf8bcf12fe7065d211dce971008")
    16  	testsig     = hexutil.MustDecode("0x90f27b8b488db00b00606796d2987f6a5f59ae62ea05effe84fef5b8b0e549984a691139ad57a3f0b906637673aa2f63d1f55cb1a69199d4009eea23ceaddc9301")
    17  	testpubkey  = hexutil.MustDecode("0x04e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652")
    18  	testpubkeyc = hexutil.MustDecode("0x02e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a")
    19  )
    20  
    21  func TestEcrecover(t *testing.T) {
    22  	pubkey, err := Ecrecover(testmsg, testsig)
    23  	if err != nil {
    24  		t.Fatalf("recover error: %s", err)
    25  	}
    26  	if !bytes.Equal(pubkey, testpubkey) {
    27  		t.Errorf("pubkey mismatch: want: %x have: %x", testpubkey, pubkey)
    28  	}
    29  }
    30  
    31  func TestVerifySignature(t *testing.T) {
    32  	sig := testsig[:len(testsig)-1]
    33  	if !VerifySignature(testpubkey, testmsg, sig) {
    34  		t.Errorf("can't verify signature with uncompressed key")
    35  	}
    36  	if !VerifySignature(testpubkeyc, testmsg, sig) {
    37  		t.Errorf("can't verify signature with compressed key")
    38  	}
    39  
    40  	if VerifySignature(nil, testmsg, sig) {
    41  		t.Errorf("signature valid with no key")
    42  	}
    43  	if VerifySignature(testpubkey, nil, sig) {
    44  		t.Errorf("signature valid with no message")
    45  	}
    46  	if VerifySignature(testpubkey, testmsg, nil) {
    47  		t.Errorf("nil signature valid")
    48  	}
    49  	if VerifySignature(testpubkey, testmsg, append(common.CopyBytes(sig), 1, 2, 3)) {
    50  		t.Errorf("signature valid with extra bytes at the end")
    51  	}
    52  	if VerifySignature(testpubkey, testmsg, sig[:len(sig)-2]) {
    53  		t.Errorf("signature valid even though it's incomplete")
    54  	}
    55  	wrongkey := common.CopyBytes(testpubkey)
    56  	wrongkey[10]++
    57  	if VerifySignature(wrongkey, testmsg, sig) {
    58  		t.Errorf("signature valid with with wrong public key")
    59  	}
    60  }
    61  
    62  func TestVerifySignatureMalleable(t *testing.T) {
    63  	sig := hexutil.MustDecode("0x638a54215d80a6713c8d523a6adc4e6e73652d859103a36b700851cb0e61b66b8ebfc1a610c57d732ec6e0a8f06a9a7a28df5051ece514702ff9cdff0b11f454")
    64  	key := hexutil.MustDecode("0x03ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd3138")
    65  	msg := hexutil.MustDecode("0xd301ce462d3e639518f482c7f03821fec1e602018630ce621e1e7851c12343a6")
    66  	if VerifySignature(key, msg, sig) {
    67  		t.Error("VerifySignature returned true for malleable signature")
    68  	}
    69  }
    70  
    71  func TestDecompressPubkey(t *testing.T) {
    72  	key, err := DecompressPubkey(testpubkeyc)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	if uncompressed := FromECDSAPub(key); !bytes.Equal(uncompressed, testpubkey) {
    77  		t.Errorf("wrong public key result: got %x, want %x", uncompressed, testpubkey)
    78  	}
    79  	if _, err := DecompressPubkey(nil); err == nil {
    80  		t.Errorf("no error for nil pubkey")
    81  	}
    82  	if _, err := DecompressPubkey(testpubkeyc[:5]); err == nil {
    83  		t.Errorf("no error for incomplete pubkey")
    84  	}
    85  	if _, err := DecompressPubkey(append(common.CopyBytes(testpubkeyc), 1, 2, 3)); err == nil {
    86  		t.Errorf("no error for pubkey with extra bytes at the end")
    87  	}
    88  }
    89  
    90  func TestCompressPubkey(t *testing.T) {
    91  	key := &ecdsa.PublicKey{
    92  		Curve: S256(),
    93  		X:     math.MustParseBig256("0xe32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a"),
    94  		Y:     math.MustParseBig256("0x0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652"),
    95  	}
    96  	compressed := CompressPubkey(key)
    97  	if !bytes.Equal(compressed, testpubkeyc) {
    98  		t.Errorf("wrong public key result: got %x, want %x", compressed, testpubkeyc)
    99  	}
   100  }
   101  
   102  func TestPubkeyRandom(t *testing.T) {
   103  	const runs = 200
   104  
   105  	for i := 0; i < runs; i++ {
   106  		key, err := GenerateKey()
   107  		if err != nil {
   108  			t.Fatalf("iteration %d: %v", i, err)
   109  		}
   110  		pubkey2, err := DecompressPubkey(CompressPubkey(&key.PublicKey))
   111  		if err != nil {
   112  			t.Fatalf("iteration %d: %v", i, err)
   113  		}
   114  		if !reflect.DeepEqual(key.PublicKey, *pubkey2) {
   115  			t.Fatalf("iteration %d: keys not equal", i)
   116  		}
   117  	}
   118  }
   119  
   120  func BenchmarkEcrecoverSignature(b *testing.B) {
   121  	for i := 0; i < b.N; i++ {
   122  		if _, err := Ecrecover(testmsg, testsig); err != nil {
   123  			b.Fatal("ecrecover error", err)
   124  		}
   125  	}
   126  }
   127  
   128  func BenchmarkVerifySignature(b *testing.B) {
   129  	sig := testsig[:len(testsig)-1]
   130  	for i := 0; i < b.N; i++ {
   131  		if !VerifySignature(testpubkey, testmsg, sig) {
   132  			b.Fatal("verify error")
   133  		}
   134  	}
   135  }
   136  
   137  func BenchmarkDecompressPubkey(b *testing.B) {
   138  	for i := 0; i < b.N; i++ {
   139  		if _, err := DecompressPubkey(testpubkeyc); err != nil {
   140  			b.Fatal(err)
   141  		}
   142  	}
   143  }