github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/merkletree2/encoders_test.go (about)

     1  package merkletree2
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestBlindedSHA512_256v1Encoder(t *testing.T) {
    10  
    11  	tests := []struct {
    12  		desc string
    13  		enc  Encoder
    14  	}{
    15  		{"safe", NewBlindedSHA512_256v1Encoder()},
    16  		{"non thread safe", NewUnsafeBlindedSHA512_256v1Encoder()},
    17  	}
    18  
    19  	for _, test := range tests {
    20  		t.Run(test.desc, func(t *testing.T) {
    21  
    22  			encoder := test.enc
    23  
    24  			hashLength := 32
    25  
    26  			val1 := "pizza"
    27  			val2 := "pasta"
    28  
    29  			enc0, err := encoder.Encode(val1)
    30  			require.NoError(t, err)
    31  			require.NotEmpty(t, enc0)
    32  
    33  			dec := ""
    34  			err = encoder.Decode(&dec, enc0)
    35  			require.NoError(t, err)
    36  			require.Equal(t, val1, dec)
    37  
    38  			enc1, h, err := encoder.EncodeAndHashGeneric(val1)
    39  			require.NoError(t, err)
    40  			require.Len(t, h, hashLength)
    41  			require.NotEmpty(t, enc1)
    42  			require.Equal(t, enc0, enc1)
    43  
    44  			var h2 Hash
    45  			err = encoder.HashGeneric(val1, &h2)
    46  			require.NoError(t, err)
    47  			require.Equal(t, h, h2)
    48  
    49  			enc2, h2, err := encoder.EncodeAndHashGeneric(val2)
    50  			require.NoError(t, err)
    51  			require.Len(t, h, hashLength)
    52  			require.NotEqual(t, h, h2)
    53  			require.NotEmpty(t, enc2)
    54  			require.NotEqual(t, enc1, enc2)
    55  
    56  			ms, err := encoder.GenerateMasterSecret(0)
    57  			require.NoError(t, err)
    58  			require.Len(t, ms, hashLength)
    59  
    60  			ms2, err := encoder.GenerateMasterSecret(0)
    61  			require.NoError(t, err)
    62  			require.Len(t, ms2, hashLength)
    63  			require.NotEqual(t, ms, ms2)
    64  
    65  			ks := encoder.ComputeKeySpecificSecret(ms, Key([]byte{0x00, 0x01}))
    66  			require.Len(t, ks, hashLength)
    67  			ks2 := encoder.ComputeKeySpecificSecret(ms, Key([]byte{0x00, 0x01}))
    68  			require.Len(t, ks, hashLength)
    69  			require.Equal(t, ks, ks2)
    70  			ks3 := encoder.ComputeKeySpecificSecret(ms, Key([]byte{0x00, 0x02}))
    71  			require.Len(t, ks3, hashLength)
    72  			require.NotEqual(t, ks, ks3)
    73  
    74  			var ks4 KeySpecificSecret
    75  			encoder.ComputeKeySpecificSecretTo(ms, Key([]byte{0x00, 0x01}), &ks4)
    76  			require.Equal(t, ks, ks4)
    77  
    78  			h0, err := encoder.HashKeyEncodedValuePairWithKeySpecificSecret(KeyEncodedValuePair{Key: Key([]byte{0x00, 0x01}), Value: enc0}, ks)
    79  			require.NoError(t, err)
    80  			require.Len(t, h, hashLength)
    81  
    82  			err = encoder.HashKeyEncodedValuePairWithKeySpecificSecretTo(KeyEncodedValuePair{Key: Key([]byte{0x00, 0x01}), Value: enc0}, ks, &h)
    83  			require.NoError(t, err)
    84  			require.Equal(t, h0, h)
    85  
    86  			h, err = encoder.HashKeyValuePairWithKeySpecificSecret(KeyValuePair{Key: Key([]byte{0x00, 0x01}), Value: val1}, ks)
    87  			require.NoError(t, err)
    88  			require.Len(t, h, hashLength)
    89  			require.Equal(t, h0, h)
    90  
    91  			h2, err = encoder.HashKeyValuePairWithKeySpecificSecret(KeyValuePair{Key: Key([]byte{0x00, 0x01}), Value: val1}, ks)
    92  			require.NoError(t, err)
    93  			require.Len(t, h2, hashLength)
    94  			require.Equal(t, h, h2)
    95  
    96  			h3, err := encoder.HashKeyValuePairWithKeySpecificSecret(KeyValuePair{Key: Key([]byte{0x00, 0x01}), Value: val1}, ks3)
    97  			require.NoError(t, err)
    98  			require.Len(t, h3, hashLength)
    99  			require.NotEqual(t, h, h3)
   100  
   101  			h4, err := encoder.HashKeyValuePairWithKeySpecificSecret(KeyValuePair{Key: Key([]byte{0x00, 0x02}), Value: val1}, ks)
   102  			require.NoError(t, err)
   103  			require.Len(t, h4, hashLength)
   104  			require.NotEqual(t, h, h3)
   105  		})
   106  	}
   107  }
   108  
   109  func TestBlindedSHA512_256v1EncoderHardcodedValues(t *testing.T) {
   110  	// To prevent changes to go-codec or similar from breaking existing merkle
   111  	// proofs, this test hardcodes some example values for the encoder and
   112  	// hasher.
   113  
   114  	tests := []struct {
   115  		desc string
   116  		enc  Encoder
   117  	}{
   118  		{"safe", NewBlindedSHA512_256v1Encoder()},
   119  		{"non thread safe", NewUnsafeBlindedSHA512_256v1Encoder()},
   120  	}
   121  
   122  	for _, test := range tests {
   123  		t.Run(test.desc, func(t *testing.T) {
   124  
   125  			encoder := test.enc
   126  
   127  			hashLength := 32
   128  
   129  			hashb1 := make([]byte, hashLength)
   130  			hashb1[0] = 0x01
   131  			hash1 := Hash(hashb1)
   132  
   133  			hashb2 := make([]byte, hashLength)
   134  			hashb2[0] = 0x02
   135  			hash2 := Hash(hashb2)
   136  
   137  			enc, err := encoder.Encode(Node{INodes: []Hash{hash1}})
   138  			require.NoError(t, err)
   139  			require.Equal(t, []byte{0x1, 0x91, 0xc4, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, enc)
   140  
   141  			enc, err = encoder.Encode(Node{INodes: []Hash{hash1, hash2}})
   142  			require.NoError(t, err)
   143  			require.Equal(t, []byte{0x1, 0x92, 0xc4, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc4, 0x20, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, enc)
   144  
   145  			enc, err = encoder.Encode(Node{LeafHashes: []KeyHashPair{{Key: []byte{0x01}, Hash: hash1}}})
   146  			require.NoError(t, err)
   147  			require.Equal(t, []byte{0x2, 0x91, 0x92, 0xc4, 0x1, 0x1, 0xc4, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, enc)
   148  
   149  			enc, err = encoder.Encode(Node{LeafHashes: []KeyHashPair{{Key: []byte{0x01}, Hash: hash1}, {Key: []byte{0x03}, Hash: hash2}}})
   150  			require.NoError(t, err)
   151  			require.Equal(t, []byte{0x2, 0x92, 0x92, 0xc4, 0x1, 0x1, 0xc4, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x92, 0xc4, 0x1, 0x3, 0xc4, 0x20, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, enc)
   152  
   153  			_, hash, err := encoder.EncodeAndHashGeneric(Node{INodes: []Hash{hash1}})
   154  			require.NoError(t, err)
   155  			require.EqualValues(t, []byte{0x1d, 0x66, 0xcd, 0x98, 0xa1, 0x36, 0x3a, 0x96, 0x8c, 0xdf, 0x5f, 0xff, 0xb1, 0x8d, 0xff, 0xfa, 0xcf, 0xdb, 0xfe, 0x6, 0xc7, 0xbd, 0xe0, 0xe5, 0x4d, 0x58, 0x5d, 0x79, 0xd, 0x57, 0x50, 0x6d}, hash)
   156  
   157  			_, hash, err = encoder.EncodeAndHashGeneric(Node{INodes: []Hash{hash1, hash2}})
   158  			require.NoError(t, err)
   159  			require.EqualValues(t, []byte{0x85, 0xd5, 0x4f, 0xf7, 0x4c, 0x11, 0x6f, 0x86, 0x46, 0x63, 0x25, 0xf9, 0xc8, 0x69, 0xfb, 0xb8, 0x26, 0x71, 0x50, 0x4f, 0x7d, 0xe9, 0x73, 0x9f, 0x2e, 0x66, 0x76, 0xa5, 0x1e, 0x1f, 0x16, 0x55}, hash)
   160  
   161  			_, hash, err = encoder.EncodeAndHashGeneric(Node{LeafHashes: []KeyHashPair{{Key: []byte{0x01}, Hash: hash1}}})
   162  			require.NoError(t, err)
   163  			require.EqualValues(t, []byte{0x59, 0xfb, 0x86, 0x3b, 0x3a, 0x83, 0xdd, 0x92, 0x22, 0xa3, 0x85, 0x4a, 0x8a, 0x9d, 0x18, 0xb, 0x31, 0x4a, 0x9c, 0xd6, 0xb, 0x5e, 0x20, 0x79, 0xa9, 0xb8, 0xae, 0x41, 0xc9, 0xa1, 0xd8, 0x5d}, hash)
   164  
   165  			_, hash, err = encoder.EncodeAndHashGeneric(Node{LeafHashes: []KeyHashPair{{Key: []byte{0x01}, Hash: hash1}, {Key: []byte{0x03}, Hash: hash2}}})
   166  			require.NoError(t, err)
   167  			require.EqualValues(t, []byte{0xf5, 0x19, 0xdb, 0x70, 0x5c, 0x4f, 0x84, 0x3f, 0xcc, 0x30, 0x62, 0x45, 0x7e, 0xfb, 0x91, 0x31, 0x59, 0x9, 0xca, 0x81, 0xc0, 0x78, 0x13, 0x82, 0xcf, 0x96, 0x37, 0xfd, 0xa7, 0xf3, 0x35, 0x81}, hash)
   168  
   169  			ms := MasterSecret([]byte{0x00, 0x01})
   170  
   171  			ks := encoder.ComputeKeySpecificSecret(ms, Key([]byte{0x00, 0x03}))
   172  			require.Len(t, ks, hashLength)
   173  			require.EqualValues(t, []byte{0xa1, 0x87, 0x3f, 0x2b, 0x2b, 0x1c, 0x76, 0xfe, 0x41, 0x64, 0x83, 0xef, 0x4d, 0xb5, 0x3f, 0x5d, 0xa2, 0x73, 0x84, 0x4d, 0x8d, 0x7e, 0x47, 0xc5, 0xd3, 0x53, 0x7e, 0xc4, 0x3, 0x2d, 0x56, 0xd5}, ks)
   174  
   175  			value := "pasta"
   176  			encValue, err := encoder.Encode(value)
   177  			require.NoError(t, err)
   178  			require.Equal(t, []byte{0xa5, 0x70, 0x61, 0x73, 0x74, 0x61}, encValue)
   179  
   180  			h, err := encoder.HashKeyEncodedValuePairWithKeySpecificSecret(KeyEncodedValuePair{Key: Key([]byte{0x00, 0x01}), Value: encValue}, ks)
   181  			require.NoError(t, err)
   182  			require.EqualValues(t, []byte{0xc, 0x43, 0x55, 0x10, 0x67, 0xc3, 0x0, 0x13, 0x70, 0xfc, 0xab, 0x9e, 0xdd, 0x33, 0x70, 0x9a, 0x65, 0xb9, 0x8c, 0x34, 0xcc, 0xf0, 0x5, 0x23, 0x1d, 0x39, 0x2c, 0x0, 0x5d, 0xd4, 0xe9, 0x1d}, h)
   183  
   184  			h, err = encoder.HashKeyValuePairWithKeySpecificSecret(KeyValuePair{Key: Key([]byte{0x00, 0x01}), Value: value}, ks)
   185  			require.NoError(t, err)
   186  			require.EqualValues(t, []byte{0xc, 0x43, 0x55, 0x10, 0x67, 0xc3, 0x0, 0x13, 0x70, 0xfc, 0xab, 0x9e, 0xdd, 0x33, 0x70, 0x9a, 0x65, 0xb9, 0x8c, 0x34, 0xcc, 0xf0, 0x5, 0x23, 0x1d, 0x39, 0x2c, 0x0, 0x5d, 0xd4, 0xe9, 0x1d}, h)
   187  		})
   188  	}
   189  }