github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/portxo/keygen_test.go (about)

     1  package portxo
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  // Bytes(KeyGen method)
     9  // no tests in case as follows
    10  // because these cases are tested by built-in tests
    11  //   KeyGen.Depth: nil, more than 2^8 - 1, less than 0
    12  //   KeyGen.Step: nil, length more than 5,
    13  //                element more than 2^32 - 1, element less than 0
    14  func TestBytesForDepth1(t *testing.T) {
    15  	// test for a normal situation
    16  	// input: inKG, KeyGen
    17  	//  Depth: 1
    18  	//  Step: [5]uint32{0, 1, 2, 3, 4}
    19  	//  PrivateKey: [32]byte{0x00, ..., 0x01}
    20  	// want: wantB
    21  	inKG := KeyGen{1,
    22  		[5]uint32{0, 1, 2, 3, 4},
    23  		[32]byte{
    24  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    25  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    26  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    27  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}
    28  	wantB := []byte{0x01}
    29  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x00}...)
    30  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x01}...)
    31  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x02}...)
    32  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x03}...)
    33  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x04}...)
    34  	wantB = append(wantB, []byte{
    35  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    36  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    37  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    38  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}...)
    39  
    40  	if !bytes.Equal(inKG.Bytes(), wantB) {
    41  		t.Fatalf("it needs to be equal")
    42  	}
    43  }
    44  
    45  func TestBytesForDepth0(t *testing.T) {
    46  	// test for an anomaly situation
    47  	// input: inKG, KeyGen
    48  	//  Depth: 0
    49  	//  Step: [5]uint32{0, 1, 2, 3, 4}
    50  	//  PrivateKey: [32]byte{0x00, ..., 0x01}
    51  	// want: wantB
    52  	inKG := KeyGen{0,
    53  		[5]uint32{0, 1, 2, 3, 4},
    54  		[32]byte{
    55  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    56  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    57  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    58  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}
    59  	wantB := []byte{0x00}
    60  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x00}...)
    61  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x01}...)
    62  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x02}...)
    63  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x03}...)
    64  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x04}...)
    65  	wantB = append(wantB, []byte{
    66  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    67  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    68  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    69  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}...)
    70  
    71  	if !bytes.Equal(inKG.Bytes(), wantB) {
    72  		t.Fatalf("it needs to be equal")
    73  	}
    74  }
    75  
    76  func TestBytesForDepth6(t *testing.T) {
    77  	// test for an anomaly situation
    78  	// input: inKG, KeyGen
    79  	//  Depth: 6
    80  	//  Step: [5]uint32{0, 1, 2, 3, 4}
    81  	//  PrivateKey: [32]byte{0x00, ..., 0x01}
    82  	// want: wantB
    83  	inKG := KeyGen{6,
    84  		[5]uint32{0, 1, 2, 3, 4},
    85  		[32]byte{
    86  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    87  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    88  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    89  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}
    90  
    91  	wantB := []byte{0x06}
    92  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x00}...)
    93  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x01}...)
    94  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x02}...)
    95  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x03}...)
    96  	wantB = append(wantB, []byte{0x00, 0x00, 0x00, 0x04}...)
    97  	wantB = append(wantB, []byte{
    98  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    99  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   100  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   101  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}...)
   102  
   103  	if !bytes.Equal(inKG.Bytes(), wantB) {
   104  		t.Fatalf("it needs to be equal")
   105  	}
   106  }
   107  
   108  // KeyGenFromBytes
   109  func TestKeyGenFromBytesForDepth1(t *testing.T) {
   110  	// test for a normal situation
   111  	// input: inB
   112  	// want: wantKG, KeyGen
   113  	//  Depth: 1
   114  	//  Step: [5]uint32{0, 1, 2, 3, 4}
   115  	//  PrivateKey: [32]byte{0x00, ..., 0x01}
   116  	inB := [53]byte{0x01,
   117  		0x00, 0x00, 0x00, 0x00,
   118  		0x00, 0x00, 0x00, 0x01,
   119  		0x00, 0x00, 0x00, 0x02,
   120  		0x00, 0x00, 0x00, 0x03,
   121  		0x00, 0x00, 0x00, 0x04,
   122  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   123  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   124  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   125  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
   126  	wantKG := KeyGen{1,
   127  		[5]uint32{0, 1, 2, 3, 4},
   128  		[32]byte{
   129  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   130  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   131  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   132  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}
   133  
   134  	if KeyGenFromBytes(inB) != wantKG {
   135  		t.Fatalf("it needs to be equal")
   136  	}
   137  }
   138  
   139  func TestKeyGenFromBytesForDepth0(t *testing.T) {
   140  	// test for an anomaly situation
   141  	// input: inB
   142  	// want: wantKG, KeyGen
   143  	//  Depth: 0
   144  	//  Step: [5]uint32{0, 1, 2, 3, 4}
   145  	//  PrivateKey: [32]byte{0x00, ..., 0x01}
   146  	inB := [53]byte{0x00,
   147  		0x00, 0x00, 0x00, 0x00,
   148  		0x00, 0x00, 0x00, 0x01,
   149  		0x00, 0x00, 0x00, 0x02,
   150  		0x00, 0x00, 0x00, 0x03,
   151  		0x00, 0x00, 0x00, 0x04,
   152  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   153  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   154  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   155  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
   156  	wantKG := KeyGen{0,
   157  		[5]uint32{0, 1, 2, 3, 4},
   158  		[32]byte{
   159  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   160  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   161  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   162  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}
   163  
   164  	if KeyGenFromBytes(inB) != wantKG {
   165  		t.Fatalf("it needs to be equal")
   166  	}
   167  }
   168  
   169  func TestKeyGenFromBytesForDepth6(t *testing.T) {
   170  	// test for an anomaly situation
   171  	// input: inB
   172  	// want: wantKG, KeyGen
   173  	//  Depth: 6
   174  	//  Step: [5]uint32{0, 1, 2, 3, 4}
   175  	//  PrivateKey: [32]byte{0x00, ..., 0x01}
   176  	inB := [53]byte{0x06,
   177  		0x00, 0x00, 0x00, 0x00,
   178  		0x00, 0x00, 0x00, 0x01,
   179  		0x00, 0x00, 0x00, 0x02,
   180  		0x00, 0x00, 0x00, 0x03,
   181  		0x00, 0x00, 0x00, 0x04,
   182  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   183  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   184  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   185  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
   186  	wantKG := KeyGen{6,
   187  		[5]uint32{0, 1, 2, 3, 4},
   188  		[32]byte{
   189  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   190  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   191  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   192  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}
   193  
   194  	if KeyGenFromBytes(inB) != wantKG {
   195  		t.Fatalf("it needs to be equal")
   196  	}
   197  }
   198  
   199  // String(KeyGen method)
   200  func TestStringForDepth2(t *testing.T) {
   201  	// test for a normal situation
   202  	// input: inKG, KeyGen
   203  	//  Depth: 2
   204  	//  Step: [5]uint32{0, 1, 2, 3, 4}
   205  	//  PrivateKey: [32]byte{0x00, ..., 0x01}
   206  	// want: wantS, String
   207  	inKG := KeyGen{2,
   208  		[5]uint32{44, 0, 2, 3, 4},
   209  		[32]byte{
   210  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   211  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   212  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   213  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}
   214  	wantS := "/44/0"
   215  
   216  	if inKG.String() != wantS {
   217  		t.Fatalf("it needs to be equal")
   218  	}
   219  }
   220  
   221  func TestStringForHardened(t *testing.T) {
   222  	// test for a normal situation
   223  	// input: inKG, KeyGen
   224  	//  Depth: 3
   225  	//  Step: [5]uint32{44, 0, 2147483648, 3, 4}
   226  	//  PrivateKey: [32]byte{0x00, ..., 0x01}
   227  	// want: wantS, String
   228  	inKG := KeyGen{3,
   229  		[5]uint32{44, 0, 2147483648, 3, 4},
   230  		[32]byte{
   231  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   232  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   233  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   234  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}
   235  	wantS := "/44/0/0'"
   236  
   237  	if inKG.String() != wantS {
   238  		t.Fatalf("it needs to be equal")
   239  	}
   240  }
   241  
   242  func TestStringForDepth0(t *testing.T) {
   243  	// test for an anomaly situation
   244  	// input: inKG, KeyGen
   245  	//  Depth: 0
   246  	//  Step: [5]uint32{0, 1, 2, 3, 4}
   247  	//  PrivateKey: [32]byte{0x00, ..., 0x01}
   248  	// want: wantS, empty string
   249  	inKG := KeyGen{0,
   250  		[5]uint32{0, 1, 2, 3, 4},
   251  		[32]byte{
   252  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   253  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   254  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   255  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}
   256  	wantS := ""
   257  
   258  	if inKG.String() != wantS {
   259  		t.Fatalf("it needs to be equal")
   260  	}
   261  }
   262  
   263  func TestStringForDepth6(t *testing.T) {
   264  	// TODO: This case occurs panic because KeyGen.Depth is 6 but KeyGen.Step[5] is nothing
   265  	//       It is better to add this test code after fixing the panic
   266  	//
   267  	// test for an anomaly situation
   268  	// input: inKG, KeyGen
   269  	//  Depth: 6
   270  	//  Step: [5]uint32{0, 1, 2, 3, 4}
   271  	//  PrivateKey: [32]byte{0x00, ..., 0x01}
   272  	// want: wantS, String
   273  	/*
   274  		inKG := KeyGen{6,
   275  			[5]uint32{0, 1, 2, 3, 4},
   276  			[32]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   277  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   278  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   279  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}
   280  		wantS := "/0/1/2/3/4"
   281  
   282  		if inKG.String() != wantS {
   283  			t.Fatalf("it needs to be equal")
   284  		}
   285  	*/
   286  }
   287  
   288  func TestKeygenSerdes(t *testing.T) {
   289  
   290  	k := KeyGen{
   291  		Depth:   5,
   292  		Step:    [5]uint32{12345, 23456, 34567, 45678, 56789},
   293  		PrivKey: [32]byte{}, // should be al 0
   294  	}
   295  
   296  	kb := k.Bytes()
   297  	kb53 := [53]byte{}
   298  	copy(kb53[:], kb)
   299  
   300  	k2 := KeyGenFromBytes(kb53)
   301  
   302  	if k != k2 {
   303  		t.Fail()
   304  	}
   305  
   306  }