github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/mining/tensority/go_algorithm/algorithm_test.go (about)

     1  package go_algorithm
     2  
     3  import (
     4  	"reflect"
     5  	"runtime"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/bytom/bytom/protocol/bc"
    10  )
    11  
    12  var tests = []struct {
    13  	blockHeader [32]byte
    14  	seed        [32]byte
    15  	hash        [32]byte
    16  }{
    17  	{
    18  		blockHeader: [32]byte{
    19  			0xd0, 0xda, 0xd7, 0x3f, 0xb2, 0xda, 0xbf, 0x33,
    20  			0x53, 0xfd, 0xa1, 0x55, 0x71, 0xb4, 0xe5, 0xf6,
    21  			0xac, 0x62, 0xff, 0x18, 0x7b, 0x35, 0x4f, 0xad,
    22  			0xd4, 0x84, 0x0d, 0x9f, 0xf2, 0xf1, 0xaf, 0xdf,
    23  		},
    24  		seed: [32]byte{
    25  			0x07, 0x37, 0x52, 0x07, 0x81, 0x34, 0x5b, 0x11,
    26  			0xb7, 0xbd, 0x0f, 0x84, 0x3c, 0x1b, 0xdd, 0x9a,
    27  			0xea, 0x81, 0xb6, 0xda, 0x94, 0xfd, 0x14, 0x1c,
    28  			0xc9, 0xf2, 0xdf, 0x53, 0xac, 0x67, 0x44, 0xd2,
    29  		},
    30  		hash: [32]byte{
    31  			0xe3, 0x5d, 0xa5, 0x47, 0x95, 0xd8, 0x2f, 0x85,
    32  			0x49, 0xc0, 0xe5, 0x80, 0xcb, 0xf2, 0xe3, 0x75,
    33  			0x7a, 0xb5, 0xef, 0x8f, 0xed, 0x1b, 0xdb, 0xe4,
    34  			0x39, 0x41, 0x6c, 0x7e, 0x6f, 0x8d, 0xf2, 0x27,
    35  		},
    36  	},
    37  	{
    38  		blockHeader: [32]byte{
    39  			0xd0, 0xda, 0xd7, 0x3f, 0xb2, 0xda, 0xbf, 0x33,
    40  			0x53, 0xfd, 0xa1, 0x55, 0x71, 0xb4, 0xe5, 0xf6,
    41  			0xac, 0x62, 0xff, 0x18, 0x7b, 0x35, 0x4f, 0xad,
    42  			0xd4, 0x84, 0x0d, 0x9f, 0xf2, 0xf1, 0xaf, 0xdf,
    43  		},
    44  		seed: [32]byte{
    45  			0x07, 0x37, 0x52, 0x07, 0x81, 0x34, 0x5b, 0x11,
    46  			0xb7, 0xbd, 0x0f, 0x84, 0x3c, 0x1b, 0xdd, 0x9a,
    47  			0xea, 0x81, 0xb6, 0xda, 0x94, 0xfd, 0x14, 0x1c,
    48  			0xc9, 0xf2, 0xdf, 0x53, 0xac, 0x67, 0x44, 0xd2,
    49  		},
    50  		hash: [32]byte{
    51  			0xe3, 0x5d, 0xa5, 0x47, 0x95, 0xd8, 0x2f, 0x85,
    52  			0x49, 0xc0, 0xe5, 0x80, 0xcb, 0xf2, 0xe3, 0x75,
    53  			0x7a, 0xb5, 0xef, 0x8f, 0xed, 0x1b, 0xdb, 0xe4,
    54  			0x39, 0x41, 0x6c, 0x7e, 0x6f, 0x8d, 0xf2, 0x27,
    55  		},
    56  	},
    57  	{
    58  		blockHeader: [32]byte{
    59  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    60  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    61  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    62  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    63  		},
    64  		seed: [32]byte{
    65  			0x48, 0xdd, 0xa5, 0xbb, 0xe9, 0x17, 0x1a, 0x66,
    66  			0x56, 0x20, 0x6e, 0xc5, 0x6c, 0x59, 0x5c, 0x58,
    67  			0x34, 0xb6, 0xcf, 0x38, 0xc5, 0xfe, 0x71, 0xbc,
    68  			0xb4, 0x4f, 0xe4, 0x38, 0x33, 0xae, 0xe9, 0xdf,
    69  		},
    70  		hash: [32]byte{
    71  			0x26, 0xdb, 0x94, 0xef, 0xa4, 0x22, 0xd7, 0x6c,
    72  			0x40, 0x2a, 0x54, 0xee, 0xb6, 0x1d, 0xd5, 0xf5,
    73  			0x32, 0x82, 0xcd, 0x3c, 0xe1, 0xa0, 0xac, 0x67,
    74  			0x7e, 0x17, 0x70, 0x51, 0xed, 0xaa, 0x98, 0xc1,
    75  		},
    76  	},
    77  	{
    78  		blockHeader: [32]byte{
    79  			0x8d, 0x96, 0x9e, 0xef, 0x6e, 0xca, 0xd3, 0xc2,
    80  			0x9a, 0x3a, 0x62, 0x92, 0x80, 0xe6, 0x86, 0xcf,
    81  			0x0c, 0x3f, 0x5d, 0x5a, 0x86, 0xaf, 0xf3, 0xca,
    82  			0x12, 0x02, 0x0c, 0x92, 0x3a, 0xdc, 0x6c, 0x92,
    83  		},
    84  		seed: [32]byte{
    85  			0x0e, 0x3b, 0x78, 0xd8, 0x38, 0x08, 0x44, 0xb0,
    86  			0xf6, 0x97, 0xbb, 0x91, 0x2d, 0xa7, 0xf4, 0xd2,
    87  			0x10, 0x38, 0x2c, 0x67, 0x14, 0x19, 0x4f, 0xd1,
    88  			0x60, 0x39, 0xef, 0x2a, 0xcd, 0x92, 0x4d, 0xcf,
    89  		},
    90  		hash: [32]byte{
    91  			0xfe, 0xce, 0xc3, 0x36, 0x69, 0x73, 0x75, 0x92,
    92  			0xf7, 0x75, 0x4b, 0x21, 0x5b, 0x20, 0xba, 0xce,
    93  			0xfb, 0xa6, 0x4d, 0x2e, 0x4c, 0xa1, 0x65, 0x6f,
    94  			0x85, 0xea, 0x1d, 0x3d, 0xbe, 0x16, 0x28, 0x39,
    95  		},
    96  	},
    97  	{
    98  		blockHeader: [32]byte{
    99  			0x2f, 0x01, 0x43, 0x11, 0xe0, 0x92, 0x6f, 0xa8,
   100  			0xb3, 0xd6, 0xe6, 0xde, 0x20, 0x51, 0xbf, 0x69,
   101  			0x33, 0x21, 0x23, 0xba, 0xad, 0xfe, 0x52, 0x2b,
   102  			0x62, 0xf4, 0x64, 0x56, 0x55, 0x85, 0x9e, 0x7a,
   103  		},
   104  		seed: [32]byte{
   105  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   106  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   107  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   108  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   109  		},
   110  		hash: [32]byte{
   111  			0xc1, 0xc3, 0xcf, 0x4c, 0x76, 0x96, 0x8e, 0x29,
   112  			0x67, 0xf0, 0x05, 0x3c, 0x76, 0xf2, 0x08, 0x4c,
   113  			0xc0, 0x1e, 0xd0, 0xfe, 0x97, 0x66, 0x42, 0x8d,
   114  			0xb9, 0x9c, 0x45, 0xbe, 0xdf, 0x0c, 0xdb, 0xe2,
   115  		},
   116  	},
   117  	{
   118  		blockHeader: [32]byte{
   119  			0xe0, 0xe3, 0xc4, 0x31, 0x78, 0xa1, 0x26, 0xd0,
   120  			0x48, 0x71, 0xb9, 0xc5, 0xd0, 0xc6, 0x42, 0xe5,
   121  			0xe0, 0x8b, 0x96, 0x79, 0xa5, 0xf6, 0x6b, 0x82,
   122  			0x1b, 0xd9, 0xa0, 0x30, 0xef, 0xf0, 0x2c, 0xe7,
   123  		},
   124  		seed: [32]byte{
   125  			0x6a, 0xb2, 0x1e, 0x13, 0x01, 0xf5, 0x75, 0x2c,
   126  			0x2f, 0xca, 0x1b, 0x55, 0x98, 0xf4, 0x9d, 0x37,
   127  			0x69, 0x48, 0x2e, 0x07, 0x3c, 0x1f, 0x26, 0xe3,
   128  			0xb8, 0x36, 0x5f, 0x40, 0x55, 0x53, 0xea, 0x31,
   129  		},
   130  		hash: [32]byte{
   131  			0xab, 0xbc, 0x2c, 0xb3, 0x96, 0x38, 0xf6, 0x84,
   132  			0x23, 0x5f, 0xbc, 0x1b, 0x3f, 0xf1, 0x07, 0x94,
   133  			0x59, 0x48, 0xc5, 0x81, 0xb6, 0x92, 0x9b, 0xae,
   134  			0x2c, 0xd6, 0x81, 0x88, 0x9f, 0xf2, 0xd8, 0x24,
   135  		},
   136  	},
   137  	{
   138  		blockHeader: [32]byte{
   139  			0x88, 0x6a, 0x8e, 0x85, 0xb2, 0x75, 0xe7, 0xd6,
   140  			0x5b, 0x56, 0x9b, 0xa5, 0x10, 0x87, 0x5c, 0x0e,
   141  			0x63, 0xde, 0xce, 0x1a, 0x94, 0x56, 0x99, 0x14,
   142  			0xd7, 0x62, 0x4c, 0x0d, 0xac, 0x80, 0x02, 0xf9,
   143  		},
   144  		seed: [32]byte{
   145  			0x9e, 0x62, 0x91, 0x97, 0x0c, 0xb4, 0x4d, 0xd9,
   146  			0x40, 0x08, 0xc7, 0x9b, 0xca, 0xf9, 0xd8, 0x6f,
   147  			0x18, 0xb4, 0xb4, 0x9b, 0xa5, 0xb2, 0xa0, 0x47,
   148  			0x81, 0xdb, 0x71, 0x99, 0xed, 0x3b, 0x9e, 0x4e,
   149  		},
   150  		hash: [32]byte{
   151  			0x6a, 0x1f, 0x27, 0x6c, 0xac, 0x6f, 0x9e, 0x8b,
   152  			0x42, 0x6e, 0xab, 0x46, 0xb5, 0x33, 0xf8, 0x2e,
   153  			0x82, 0xa1, 0x48, 0xc0, 0x3f, 0xb0, 0xa8, 0x69,
   154  			0x34, 0xa8, 0xe5, 0x48, 0x3b, 0x39, 0xda, 0x5e,
   155  		},
   156  	},
   157  	{
   158  		blockHeader: [32]byte{
   159  			0x2f, 0x86, 0xfe, 0x50, 0x8f, 0xaa, 0x7d, 0x68,
   160  			0x69, 0x2a, 0x20, 0x89, 0x27, 0x1b, 0x69, 0x01,
   161  			0x38, 0x5d, 0x90, 0x58, 0xcd, 0x47, 0xe8, 0x4f,
   162  			0xb4, 0x02, 0xb5, 0x08, 0x5f, 0x9a, 0x83, 0x60,
   163  		},
   164  		seed: [32]byte{
   165  			0x9e, 0x62, 0x91, 0x97, 0x0c, 0xb4, 0x4d, 0xd9,
   166  			0x40, 0x08, 0xc7, 0x9b, 0xca, 0xf9, 0xd8, 0x6f,
   167  			0x18, 0xb4, 0xb4, 0x9b, 0xa5, 0xb2, 0xa0, 0x47,
   168  			0x81, 0xdb, 0x71, 0x99, 0xed, 0x3b, 0x9e, 0x4e,
   169  		},
   170  		hash: [32]byte{
   171  			0x81, 0x75, 0x6f, 0xda, 0xb3, 0x9a, 0x17, 0x16,
   172  			0x3b, 0x0c, 0xe5, 0x82, 0xee, 0x4e, 0xe2, 0x56,
   173  			0xfb, 0x4d, 0x1e, 0x15, 0x6c, 0x69, 0x2b, 0x99,
   174  			0x7d, 0x60, 0x8a, 0x42, 0xec, 0xb3, 0x8d, 0x47,
   175  		},
   176  	},
   177  	{
   178  		blockHeader: [32]byte{
   179  			0xfe, 0x97, 0x91, 0xd7, 0x1b, 0x67, 0xee, 0x62,
   180  			0x51, 0x5e, 0x08, 0x72, 0x3c, 0x06, 0x1b, 0x5c,
   181  			0xcb, 0x95, 0x2a, 0x80, 0xd8, 0x04, 0x41, 0x7c,
   182  			0x8a, 0xee, 0xdf, 0x7f, 0x63, 0x3c, 0x52, 0x4a,
   183  		},
   184  		seed: [32]byte{
   185  			0x9e, 0x62, 0x91, 0x97, 0x0c, 0xb4, 0x4d, 0xd9,
   186  			0x40, 0x08, 0xc7, 0x9b, 0xca, 0xf9, 0xd8, 0x6f,
   187  			0x18, 0xb4, 0xb4, 0x9b, 0xa5, 0xb2, 0xa0, 0x47,
   188  			0x81, 0xdb, 0x71, 0x99, 0xed, 0x3b, 0x9e, 0x4e,
   189  		},
   190  		hash: [32]byte{
   191  			0xfa, 0xd5, 0x19, 0x5a, 0x0c, 0x8e, 0x3b, 0x59,
   192  			0x0b, 0x86, 0xa3, 0xc0, 0xa9, 0x5e, 0x75, 0x29,
   193  			0x56, 0x58, 0x88, 0x50, 0x8a, 0xec, 0xca, 0x96,
   194  			0xe9, 0xae, 0xda, 0x63, 0x30, 0x02, 0xf4, 0x09,
   195  		},
   196  	},
   197  	{
   198  		blockHeader: [32]byte{
   199  			0xef, 0x30, 0x67, 0x87, 0xa0, 0x87, 0xc1, 0x18,
   200  			0xfc, 0xb6, 0xd3, 0x51, 0xf0, 0x19, 0x9d, 0xca,
   201  			0x98, 0x05, 0x58, 0x98, 0xe2, 0x08, 0xfb, 0xf1,
   202  			0xa9, 0x34, 0xc9, 0xd7, 0x0b, 0x58, 0xee, 0x4b,
   203  		},
   204  		seed: [32]byte{
   205  			0x9e, 0x62, 0x91, 0x97, 0x0c, 0xb4, 0x4d, 0xd9,
   206  			0x40, 0x08, 0xc7, 0x9b, 0xca, 0xf9, 0xd8, 0x6f,
   207  			0x18, 0xb4, 0xb4, 0x9b, 0xa5, 0xb2, 0xa0, 0x47,
   208  			0x81, 0xdb, 0x71, 0x99, 0xed, 0x3b, 0x9e, 0x4e,
   209  		},
   210  		hash: [32]byte{
   211  			0xff, 0xb0, 0xca, 0xda, 0xb3, 0x14, 0x67, 0x3d,
   212  			0x22, 0x8f, 0x8f, 0xe7, 0x4d, 0x84, 0xa4, 0x65,
   213  			0x2e, 0x01, 0xc8, 0x2c, 0x26, 0x41, 0xd3, 0xe2,
   214  			0xfa, 0x91, 0x48, 0xaf, 0xea, 0xb0, 0xd6, 0x06,
   215  		},
   216  	},
   217  	{
   218  		blockHeader: [32]byte{
   219  			0x10, 0xd2, 0xf1, 0xb2, 0xf4, 0x8e, 0x38, 0x9c,
   220  			0x97, 0xdd, 0xe1, 0xb1, 0xa6, 0x3b, 0xcd, 0x74,
   221  			0x3c, 0x23, 0x40, 0x93, 0x5d, 0x71, 0xe2, 0xc3,
   222  			0x58, 0xba, 0x10, 0xe5, 0x84, 0x69, 0x61, 0x43,
   223  		},
   224  		seed: [32]byte{
   225  			0x9e, 0x62, 0x91, 0x97, 0x0c, 0xb4, 0x4d, 0xd9,
   226  			0x40, 0x08, 0xc7, 0x9b, 0xca, 0xf9, 0xd8, 0x6f,
   227  			0x18, 0xb4, 0xb4, 0x9b, 0xa5, 0xb2, 0xa0, 0x47,
   228  			0x81, 0xdb, 0x71, 0x99, 0xed, 0x3b, 0x9e, 0x4e,
   229  		},
   230  		hash: [32]byte{
   231  			0x6a, 0xdd, 0x5a, 0xf1, 0x82, 0x53, 0xd0, 0x72,
   232  			0x2c, 0x54, 0x2f, 0x7e, 0x71, 0xf4, 0x7d, 0x9c,
   233  			0xb1, 0xa4, 0xd7, 0xfb, 0x7c, 0x7d, 0x26, 0xae,
   234  			0xfe, 0x3c, 0x83, 0x1c, 0xb8, 0x54, 0xf1, 0x7e,
   235  		},
   236  	},
   237  }
   238  
   239  // Tests that tensority hash result is correct.
   240  func TestLegacyAlgorithm(t *testing.T) {
   241  	startT := time.Now()
   242  	for i, tt := range tests {
   243  		sT := time.Now()
   244  		bhhash := bc.NewHash(tt.blockHeader)
   245  		sdhash := bc.NewHash(tt.seed)
   246  		result := LegacyAlgorithm(&bhhash, &sdhash).Bytes()
   247  		var resArr [32]byte
   248  		copy(resArr[:], result)
   249  		eT := time.Now()
   250  
   251  		if !reflect.DeepEqual(resArr, tt.hash) {
   252  			t.Errorf("Test case %d:\n", i+1)
   253  			t.Errorf("Gets\t%x\n", resArr)
   254  			t.Errorf("Expects\t%x\n", tt.hash)
   255  			t.Errorf("FAIL\n\n")
   256  		} else {
   257  			t.Logf("Test case %d:\n", i+1)
   258  			t.Log("Total verification time:", eT.Sub(sT))
   259  			t.Log("PASS\n")
   260  		}
   261  	}
   262  	endT := time.Now()
   263  	t.Log("Avg time:", time.Duration(int(endT.Sub(startT))/len(tests)))
   264  }
   265  
   266  func BenchmarkLegacyAlgorithm(b *testing.B) {
   267  	bhhash := bc.NewHash(tests[0].blockHeader)
   268  	sdhash := bc.NewHash(tests[0].seed)
   269  	b.ResetTimer()
   270  	for i := 0; i < b.N; i++ {
   271  		LegacyAlgorithm(&bhhash, &sdhash)
   272  	}
   273  }
   274  
   275  func BenchmarkLegacyAlgorithmParallel(b *testing.B) {
   276  	bhhash := bc.NewHash(tests[0].blockHeader)
   277  	sdhash := bc.NewHash(tests[0].seed)
   278  	b.SetParallelism(runtime.NumCPU())
   279  	b.RunParallel(func(pb *testing.PB) {
   280  		for pb.Next() {
   281  			LegacyAlgorithm(&bhhash, &sdhash)
   282  		}
   283  	})
   284  }