github.com/prysmaticlabs/prysm@v1.4.4/shared/hashutil/hash_test.go (about) 1 package hashutil_test 2 3 import ( 4 "encoding/hex" 5 "testing" 6 7 fuzz "github.com/google/gofuzz" 8 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 9 pb "github.com/prysmaticlabs/prysm/proto/testing" 10 "github.com/prysmaticlabs/prysm/shared/bls" 11 "github.com/prysmaticlabs/prysm/shared/bytesutil" 12 "github.com/prysmaticlabs/prysm/shared/hashutil" 13 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 14 "github.com/prysmaticlabs/prysm/shared/testutil/require" 15 ) 16 17 func TestHash(t *testing.T) { 18 hashOf0 := [32]byte{110, 52, 11, 156, 255, 179, 122, 152, 156, 165, 68, 230, 187, 120, 10, 44, 120, 144, 29, 63, 179, 55, 56, 118, 133, 17, 163, 6, 23, 175, 160, 29} 19 hash := hashutil.Hash([]byte{0}) 20 assert.Equal(t, hashOf0, hash) 21 22 hashOf1 := [32]byte{75, 245, 18, 47, 52, 69, 84, 197, 59, 222, 46, 187, 140, 210, 183, 227, 209, 96, 10, 214, 49, 195, 133, 165, 215, 204, 226, 60, 119, 133, 69, 154} 23 hash = hashutil.Hash([]byte{1}) 24 assert.Equal(t, hashOf1, hash) 25 assert.Equal(t, false, hashOf0 == hashOf1) 26 } 27 28 func BenchmarkHash(b *testing.B) { 29 for i := 0; i < b.N; i++ { 30 hashutil.Hash([]byte("abc")) 31 } 32 } 33 34 func TestHashKeccak256(t *testing.T) { 35 hashOf0 := [32]byte{188, 54, 120, 158, 122, 30, 40, 20, 54, 70, 66, 41, 130, 143, 129, 125, 102, 18, 247, 180, 119, 214, 101, 145, 255, 150, 169, 224, 100, 188, 201, 138} 36 hash := hashutil.HashKeccak256([]byte{0}) 37 assert.Equal(t, hashOf0, hash) 38 39 hashOf1 := [32]byte{95, 231, 249, 119, 231, 29, 186, 46, 161, 166, 142, 33, 5, 123, 238, 187, 155, 226, 172, 48, 198, 65, 10, 163, 141, 79, 63, 190, 65, 220, 255, 210} 40 hash = hashutil.HashKeccak256([]byte{1}) 41 assert.Equal(t, hashOf1, hash) 42 assert.Equal(t, false, hashOf0 == hashOf1) 43 44 // Same hashing test from go-ethereum for keccak256 45 hashOfabc, err := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45") 46 require.NoError(t, err) 47 hash = hashutil.HashKeccak256([]byte("abc")) 48 h := bytesutil.ToBytes32(hashOfabc) 49 assert.Equal(t, hash, h) 50 } 51 52 func BenchmarkHashKeccak256(b *testing.B) { 53 for i := 0; i < b.N; i++ { 54 hashutil.HashKeccak256([]byte("abc")) 55 } 56 } 57 58 func TestHashProto(t *testing.T) { 59 msg1 := &pb.Puzzle{ 60 Challenge: "hello", 61 } 62 msg2 := &pb.Puzzle{ 63 Challenge: "hello", 64 } 65 h1, err := hashutil.HashProto(msg1) 66 require.NoError(t, err) 67 h2, err := hashutil.HashProto(msg2) 68 require.NoError(t, err) 69 assert.Equal(t, h1, h2) 70 } 71 72 func TestHashProtoFuzz(t *testing.T) { 73 f := fuzz.New().NilChance(.2) 74 defer func(tt *testing.T) { 75 if r := recover(); r != nil { 76 t.Log("cannot hash nil proto") 77 } 78 }(t) 79 80 for i := 0; i < 1000; i++ { 81 msg := &pb.AddressBook{} 82 f.Fuzz(msg) 83 _, err := hashutil.HashProto(msg) 84 _ = err 85 } 86 } 87 88 func BenchmarkHashProto(b *testing.B) { 89 att := ðpb.Attestation{ 90 AggregationBits: nil, 91 Data: ðpb.AttestationData{ 92 Slot: 5, 93 CommitteeIndex: 3, 94 BeaconBlockRoot: []byte{}, 95 Source: nil, 96 Target: nil, 97 }, 98 Signature: bls.NewAggregateSignature().Marshal(), 99 } 100 101 for i := 0; i < b.N; i++ { 102 if _, err := hashutil.HashProto(att); err != nil { 103 b.Log(err) 104 } 105 } 106 }