github.com/Oyster-zx/tendermint@v0.34.24-fork/crypto/merkle/rfc6962_test.go (about) 1 package merkle 2 3 // Copyright 2016 Google Inc. All Rights Reserved. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // These tests were taken from https://github.com/google/trillian/blob/master/merkle/rfc6962/rfc6962_test.go, 17 // and consequently fall under the above license. 18 import ( 19 "bytes" 20 "encoding/hex" 21 "testing" 22 23 "github.com/tendermint/tendermint/crypto/tmhash" 24 ) 25 26 func TestRFC6962Hasher(t *testing.T) { 27 _, leafHashTrail := trailsFromByteSlices([][]byte{[]byte("L123456")}) 28 leafHash := leafHashTrail.Hash 29 _, leafHashTrail = trailsFromByteSlices([][]byte{{}}) 30 emptyLeafHash := leafHashTrail.Hash 31 _, emptyHashTrail := trailsFromByteSlices([][]byte{}) 32 emptyTreeHash := emptyHashTrail.Hash 33 for _, tc := range []struct { 34 desc string 35 got []byte 36 want string 37 }{ 38 // Check that empty trees return the hash of an empty string. 39 // echo -n '' | sha256sum 40 { 41 desc: "RFC6962 Empty Tree", 42 want: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"[:tmhash.Size*2], 43 got: emptyTreeHash, 44 }, 45 46 // Check that the empty hash is not the same as the hash of an empty leaf. 47 // echo -n 00 | xxd -r -p | sha256sum 48 { 49 desc: "RFC6962 Empty Leaf", 50 want: "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d"[:tmhash.Size*2], 51 got: emptyLeafHash, 52 }, 53 // echo -n 004C313233343536 | xxd -r -p | sha256sum 54 { 55 desc: "RFC6962 Leaf", 56 want: "395aa064aa4c29f7010acfe3f25db9485bbd4b91897b6ad7ad547639252b4d56"[:tmhash.Size*2], 57 got: leafHash, 58 }, 59 // echo -n 014E3132334E343536 | xxd -r -p | sha256sum 60 { 61 desc: "RFC6962 Node", 62 want: "aa217fe888e47007fa15edab33c2b492a722cb106c64667fc2b044444de66bbb"[:tmhash.Size*2], 63 got: innerHash([]byte("N123"), []byte("N456")), 64 }, 65 } { 66 tc := tc 67 t.Run(tc.desc, func(t *testing.T) { 68 wantBytes, err := hex.DecodeString(tc.want) 69 if err != nil { 70 t.Fatalf("hex.DecodeString(%x): %v", tc.want, err) 71 } 72 if got, want := tc.got, wantBytes; !bytes.Equal(got, want) { 73 t.Errorf("got %x, want %x", got, want) 74 } 75 }) 76 } 77 } 78 79 func TestRFC6962HasherCollisions(t *testing.T) { 80 // Check that different leaves have different hashes. 81 leaf1, leaf2 := []byte("Hello"), []byte("World") 82 _, leafHashTrail := trailsFromByteSlices([][]byte{leaf1}) 83 hash1 := leafHashTrail.Hash 84 _, leafHashTrail = trailsFromByteSlices([][]byte{leaf2}) 85 hash2 := leafHashTrail.Hash 86 if bytes.Equal(hash1, hash2) { 87 t.Errorf("leaf hashes should differ, but both are %x", hash1) 88 } 89 // Compute an intermediate subtree hash. 90 _, subHash1Trail := trailsFromByteSlices([][]byte{hash1, hash2}) 91 subHash1 := subHash1Trail.Hash 92 // Check that this is not the same as a leaf hash of their concatenation. 93 preimage := append(hash1, hash2...) 94 _, forgedHashTrail := trailsFromByteSlices([][]byte{preimage}) 95 forgedHash := forgedHashTrail.Hash 96 if bytes.Equal(subHash1, forgedHash) { 97 t.Errorf("hasher is not second-preimage resistant") 98 } 99 // Swap the order of nodes and check that the hash is different. 100 _, subHash2Trail := trailsFromByteSlices([][]byte{hash2, hash1}) 101 subHash2 := subHash2Trail.Hash 102 if bytes.Equal(subHash1, subHash2) { 103 t.Errorf("subtree hash does not depend on the order of leaves") 104 } 105 }