github.com/prysmaticlabs/prysm@v1.4.4/shared/htrutils/helpers_test.go (about)

     1  package htrutils_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prysmaticlabs/go-bitfield"
     7  	"github.com/prysmaticlabs/prysm/shared/hashutil"
     8  	"github.com/prysmaticlabs/prysm/shared/htrutils"
     9  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    10  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    11  )
    12  
    13  const merkleizingListLimitError = "merkleizing list that is too large, over limit"
    14  
    15  func TestBitlistRoot(t *testing.T) {
    16  	hasher := hashutil.CustomSHA256Hasher()
    17  	capacity := uint64(10)
    18  	bfield := bitfield.NewBitlist(capacity)
    19  	expected := [32]byte{176, 76, 194, 203, 142, 166, 117, 79, 148, 194, 231, 64, 60, 245, 142, 32, 201, 2, 58, 152, 53, 12, 132, 40, 41, 102, 224, 189, 103, 41, 211, 202}
    20  
    21  	result, err := htrutils.BitlistRoot(hasher, bfield, capacity)
    22  	require.NoError(t, err)
    23  	assert.Equal(t, expected, result)
    24  }
    25  
    26  func TestBitwiseMerkleize(t *testing.T) {
    27  	hasher := hashutil.CustomSHA256Hasher()
    28  	chunks := [][]byte{
    29  		{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
    30  		{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
    31  	}
    32  	count := uint64(2)
    33  	limit := uint64(2)
    34  	expected := [32]byte{194, 32, 213, 52, 220, 127, 18, 240, 43, 151, 19, 79, 188, 175, 142, 177, 208, 46, 96, 20, 18, 231, 208, 29, 120, 102, 122, 17, 46, 31, 155, 30}
    35  
    36  	result, err := htrutils.BitwiseMerkleize(hasher, chunks, count, limit)
    37  	require.NoError(t, err)
    38  	assert.Equal(t, expected, result)
    39  }
    40  
    41  func TestBitwiseMerkleizeOverLimit(t *testing.T) {
    42  	hasher := hashutil.CustomSHA256Hasher()
    43  	chunks := [][]byte{
    44  		{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
    45  		{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
    46  	}
    47  	count := uint64(2)
    48  	limit := uint64(1)
    49  
    50  	_, err := htrutils.BitwiseMerkleize(hasher, chunks, count, limit)
    51  	assert.ErrorContains(t, merkleizingListLimitError, err)
    52  }
    53  
    54  func TestBitwiseMerkleizeArrays(t *testing.T) {
    55  	hasher := hashutil.CustomSHA256Hasher()
    56  	chunks := [][32]byte{
    57  		{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
    58  		{33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 62, 62, 63, 64},
    59  	}
    60  	count := uint64(2)
    61  	limit := uint64(2)
    62  	expected := [32]byte{138, 81, 210, 194, 151, 231, 249, 241, 64, 118, 209, 58, 145, 109, 225, 89, 118, 110, 159, 220, 193, 183, 203, 124, 166, 24, 65, 26, 160, 215, 233, 219}
    63  
    64  	result, err := htrutils.BitwiseMerkleizeArrays(hasher, chunks, count, limit)
    65  	require.NoError(t, err)
    66  	assert.Equal(t, expected, result)
    67  }
    68  
    69  func TestBitwiseMerkleizeArraysOverLimit(t *testing.T) {
    70  	hasher := hashutil.CustomSHA256Hasher()
    71  	chunks := [][32]byte{
    72  		{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
    73  		{33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 62, 62, 63, 64},
    74  	}
    75  	count := uint64(2)
    76  	limit := uint64(1)
    77  
    78  	_, err := htrutils.BitwiseMerkleizeArrays(hasher, chunks, count, limit)
    79  	assert.ErrorContains(t, merkleizingListLimitError, err)
    80  }
    81  
    82  func TestPack(t *testing.T) {
    83  	byteSlice2D := [][]byte{
    84  		{1, 2, 3, 4, 5, 6, 7, 8, 9},
    85  		{1, 1, 2, 3, 5, 8, 13, 21, 34},
    86  	}
    87  	expected := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2, 3, 5, 8, 13, 21, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    88  
    89  	result, err := htrutils.Pack(byteSlice2D)
    90  	require.NoError(t, err)
    91  	assert.Equal(t, len(expected), len(result[0]))
    92  	for i, v := range expected {
    93  		assert.DeepEqual(t, v, result[0][i])
    94  	}
    95  }
    96  
    97  func TestMixInLength(t *testing.T) {
    98  	byteSlice := [32]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
    99  	length := []byte{1, 2, 3}
   100  	expected := [32]byte{105, 60, 167, 169, 197, 220, 122, 99, 59, 14, 250, 12, 251, 62, 135, 239, 29, 68, 140, 1, 6, 36, 207, 44, 64, 221, 76, 230, 237, 218, 150, 88}
   101  	result := htrutils.MixInLength(byteSlice, length)
   102  	assert.Equal(t, expected, result)
   103  }