github.com/ethersphere/bee/v2@v2.2.0/pkg/crypto/crypto_test.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package crypto_test
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/hex"
    10  	"errors"
    11  	"testing"
    12  
    13  	"github.com/ethereum/go-ethereum/common"
    14  	"github.com/ethersphere/bee/v2/pkg/crypto"
    15  	"github.com/ethersphere/bee/v2/pkg/swarm"
    16  )
    17  
    18  func TestGenerateSecp256k1Key(t *testing.T) {
    19  	t.Parallel()
    20  
    21  	k1, err := crypto.GenerateSecp256k1Key()
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	if k1 == nil {
    26  		t.Fatal("nil key")
    27  	}
    28  	k2, err := crypto.GenerateSecp256k1Key()
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	if k2 == nil {
    33  		t.Fatal("nil key")
    34  	}
    35  
    36  	if bytes.Equal(k1.D.Bytes(), k2.D.Bytes()) {
    37  		t.Fatal("two generated keys are equal")
    38  	}
    39  }
    40  
    41  func TestGenerateSecp256k1EDG(t *testing.T) {
    42  	t.Parallel()
    43  
    44  	k1, err := crypto.EDGSecp256_K1.Generate()
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	if k1 == nil {
    49  		t.Fatal("nil key")
    50  	}
    51  	k2, err := crypto.EDGSecp256_K1.Generate()
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	if k2 == nil {
    56  		t.Fatal("nil key")
    57  	}
    58  
    59  	if bytes.Equal(k1.D.Bytes(), k2.D.Bytes()) {
    60  		t.Fatal("two generated keys are equal")
    61  	}
    62  }
    63  
    64  func TestNewAddress(t *testing.T) {
    65  	t.Parallel()
    66  
    67  	k, err := crypto.GenerateSecp256k1Key()
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	a, err := crypto.NewOverlayAddress(k.PublicKey, 1, common.HexToHash("0x1").Bytes())
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	if l := len(a.Bytes()); l != 32 {
    76  		t.Errorf("got address length %v, want %v", l, 32)
    77  	}
    78  
    79  	_, err = crypto.NewOverlayAddress(k.PublicKey, 1, nil)
    80  	if !errors.Is(err, crypto.ErrBadHashLength) {
    81  		t.Fatalf("expected %v, got %v", crypto.ErrBadHashLength, err)
    82  	}
    83  }
    84  
    85  func TestEncodeSecp256k1PrivateKey(t *testing.T) {
    86  	t.Parallel()
    87  
    88  	k1, err := crypto.GenerateSecp256k1Key()
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	d, err := crypto.EncodeSecp256k1PrivateKey(k1)
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	k2, err := crypto.DecodeSecp256k1PrivateKey(d)
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	if !bytes.Equal(k1.D.Bytes(), k2.D.Bytes()) {
   101  		t.Fatal("encoded and decoded keys are not equal")
   102  	}
   103  }
   104  
   105  func TestEncodeSecp256k1EDG(t *testing.T) {
   106  	t.Parallel()
   107  
   108  	k1, err := crypto.EDGSecp256_K1.Generate()
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	d, err := crypto.EDGSecp256_K1.Encode(k1)
   113  	if err != nil {
   114  		t.Fatal(err)
   115  	}
   116  	k2, err := crypto.EDGSecp256_K1.Decode(d)
   117  	if err != nil {
   118  		t.Fatal(err)
   119  	}
   120  	if !bytes.Equal(k1.D.Bytes(), k2.D.Bytes()) {
   121  		t.Fatal("encoded and decoded keys are not equal")
   122  	}
   123  }
   124  
   125  func TestSecp256k1PrivateKeyFromBytes(t *testing.T) {
   126  	t.Parallel()
   127  
   128  	data := []byte("data")
   129  
   130  	k1 := crypto.Secp256k1PrivateKeyFromBytes(data)
   131  	if k1 == nil {
   132  		t.Fatal("nil key")
   133  	}
   134  
   135  	k2 := crypto.Secp256k1PrivateKeyFromBytes(data)
   136  	if k2 == nil {
   137  		t.Fatal("nil key")
   138  	}
   139  
   140  	if !bytes.Equal(k1.D.Bytes(), k2.D.Bytes()) {
   141  		t.Fatal("two generated keys are not equal")
   142  	}
   143  }
   144  
   145  func TestGenerateSecp256r1Key(t *testing.T) {
   146  	t.Parallel()
   147  
   148  	k1, err := crypto.GenerateSecp256r1Key()
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  	if k1 == nil {
   153  		t.Fatal("nil key")
   154  	}
   155  	k2, err := crypto.GenerateSecp256r1Key()
   156  	if err != nil {
   157  		t.Fatal(err)
   158  	}
   159  	if k2 == nil {
   160  		t.Fatal("nil key")
   161  	}
   162  
   163  	if bytes.Equal(k1.D.Bytes(), k2.D.Bytes()) {
   164  		t.Fatal("two generated keys are equal")
   165  	}
   166  }
   167  
   168  func TestGenerateSecp256r1EDG(t *testing.T) {
   169  	t.Parallel()
   170  
   171  	r1, err := crypto.EDGSecp256_R1.Generate()
   172  	if err != nil {
   173  		t.Fatal(err)
   174  	}
   175  	if r1 == nil {
   176  		t.Fatal("nil key")
   177  	}
   178  	r2, err := crypto.EDGSecp256_R1.Generate()
   179  	if err != nil {
   180  		t.Fatal(err)
   181  	}
   182  	if r2 == nil {
   183  		t.Fatal("nil key")
   184  	}
   185  
   186  	if bytes.Equal(r1.D.Bytes(), r2.D.Bytes()) {
   187  		t.Fatal("two generated keys are equal")
   188  	}
   189  }
   190  
   191  func TestEncodeSecp256r1PrivateKey(t *testing.T) {
   192  	t.Parallel()
   193  
   194  	r1, err := crypto.GenerateSecp256r1Key()
   195  	if err != nil {
   196  		t.Fatal(err)
   197  	}
   198  	d, err := crypto.EncodeSecp256r1PrivateKey(r1)
   199  	if err != nil {
   200  		t.Fatal(err)
   201  	}
   202  	r2, err := crypto.DecodeSecp256r1PrivateKey(d)
   203  	if err != nil {
   204  		t.Fatal(err)
   205  	}
   206  	if !bytes.Equal(r1.D.Bytes(), r2.D.Bytes()) {
   207  		t.Fatal("encoded and decoded keys are not equal")
   208  	}
   209  }
   210  
   211  func TestEncodeSecp256r1EDG(t *testing.T) {
   212  	t.Parallel()
   213  
   214  	r1, err := crypto.EDGSecp256_R1.Generate()
   215  	if err != nil {
   216  		t.Fatal(err)
   217  	}
   218  	d, err := crypto.EDGSecp256_R1.Encode(r1)
   219  	if err != nil {
   220  		t.Fatal(err)
   221  	}
   222  	r2, err := crypto.EDGSecp256_R1.Decode(d)
   223  	if err != nil {
   224  		t.Fatal(err)
   225  	}
   226  	if !bytes.Equal(r1.D.Bytes(), r2.D.Bytes()) {
   227  		t.Fatal("encoded and decoded keys are not equal")
   228  	}
   229  }
   230  
   231  func TestNewEthereumAddress(t *testing.T) {
   232  	t.Parallel()
   233  
   234  	privKeyHex := "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
   235  	privKeyBytes, err := hex.DecodeString(privKeyHex)
   236  	if err != nil {
   237  		t.Fatal(err)
   238  	}
   239  	privKey, err := crypto.DecodeSecp256k1PrivateKey(privKeyBytes)
   240  	if err != nil {
   241  		t.Fatal(err)
   242  	}
   243  	expectAddressHex := "2f63cbeb054ce76050827e42dd75268f6b9d87c5"
   244  	expectAddress, err := hex.DecodeString(expectAddressHex)
   245  	if err != nil {
   246  		t.Fatal(err)
   247  	}
   248  	address, err := crypto.NewEthereumAddress(privKey.PublicKey)
   249  	if err != nil {
   250  		t.Fatal(err)
   251  	}
   252  	if !bytes.Equal(address, expectAddress) {
   253  		t.Fatalf("address mismatch %x %x", address, expectAddress)
   254  	}
   255  }
   256  
   257  func TestNewOverlayFromEthereumAddress(t *testing.T) {
   258  	t.Parallel()
   259  
   260  	testCases := []struct {
   261  		wantAddress     swarm.Address
   262  		overlayID       uint64
   263  		hash            []byte
   264  		expectedAddress string
   265  	}{
   266  		{
   267  			wantAddress:     swarm.MustParseHexAddress("1815cac638d1525b47f848daf02b7953e4edd15c"),
   268  			overlayID:       1,
   269  			hash:            common.HexToHash("0x1").Bytes(),
   270  			expectedAddress: "a38f7a814d4b249ae9d3821e9b898019c78ac9abe248fff171782c32a3849a17",
   271  		},
   272  		{
   273  			wantAddress:     swarm.MustParseHexAddress("1815cac638d1525b47f848daf02b7953e4edd15c"),
   274  			overlayID:       1,
   275  			hash:            common.HexToHash("0x2").Bytes(),
   276  			expectedAddress: "c63c10b1728dfc463c64c264f71a621fe640196979375840be42dc496b702610",
   277  		},
   278  		{
   279  			wantAddress:     swarm.MustParseHexAddress("d26bc1715e933bd5f8fad16310042f13abc16159"),
   280  			overlayID:       2,
   281  			hash:            common.HexToHash("0x1").Bytes(),
   282  			expectedAddress: "9f421f9149b8e31e238cfbdc6e5e833bacf1e42f77f60874d49291292858968e",
   283  		},
   284  		{
   285  			wantAddress:     swarm.MustParseHexAddress("ac485e3c63dcf9b4cda9f007628bb0b6fed1c063"),
   286  			overlayID:       1,
   287  			hash:            common.HexToHash("0x0").Bytes(),
   288  			expectedAddress: "fe3a6d582c577404fb19df64a44e00d3a3b71230a8464c0dd34af3f0791b45f2",
   289  		},
   290  	}
   291  
   292  	for _, tc := range testCases {
   293  
   294  		gotAddress, err := crypto.NewOverlayFromEthereumAddress(tc.wantAddress.Bytes(), tc.overlayID, tc.hash)
   295  		if err != nil {
   296  			t.Fatal(err)
   297  		}
   298  
   299  		if l := len(gotAddress.Bytes()); l != swarm.HashSize {
   300  			t.Errorf("got address length %v, want %v", l, swarm.HashSize)
   301  		}
   302  
   303  		wantAddress, err := swarm.ParseHexAddress(tc.expectedAddress)
   304  		if err != nil {
   305  			t.Fatal(err)
   306  		}
   307  
   308  		if !wantAddress.Equal(gotAddress) {
   309  			t.Errorf("Expected %s, but got %s", wantAddress, gotAddress)
   310  		}
   311  	}
   312  }