github.com/vipernet-xyz/tendermint-core@v0.32.0/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 for _, tc := range []struct { 32 desc string 33 got []byte 34 want string 35 }{ 36 // Since creating a merkle tree of no leaves is unsupported here, we skip 37 // the corresponding trillian test vector. 38 39 // Check that the empty hash is not the same as the hash of an empty leaf. 40 // echo -n 00 | xxd -r -p | sha256sum 41 { 42 desc: "RFC6962 Empty Leaf", 43 want: "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d"[:tmhash.Size*2], 44 got: emptyLeafHash, 45 }, 46 // echo -n 004C313233343536 | xxd -r -p | sha256sum 47 { 48 desc: "RFC6962 Leaf", 49 want: "395aa064aa4c29f7010acfe3f25db9485bbd4b91897b6ad7ad547639252b4d56"[:tmhash.Size*2], 50 got: leafHash, 51 }, 52 // echo -n 014E3132334E343536 | xxd -r -p | sha256sum 53 { 54 desc: "RFC6962 Node", 55 want: "aa217fe888e47007fa15edab33c2b492a722cb106c64667fc2b044444de66bbb"[:tmhash.Size*2], 56 got: innerHash([]byte("N123"), []byte("N456")), 57 }, 58 } { 59 tc := tc 60 t.Run(tc.desc, func(t *testing.T) { 61 wantBytes, err := hex.DecodeString(tc.want) 62 if err != nil { 63 t.Fatalf("hex.DecodeString(%x): %v", tc.want, err) 64 } 65 if got, want := tc.got, wantBytes; !bytes.Equal(got, want) { 66 t.Errorf("got %x, want %x", got, want) 67 } 68 }) 69 } 70 } 71 72 func TestRFC6962HasherCollisions(t *testing.T) { 73 // Check that different leaves have different hashes. 74 leaf1, leaf2 := []byte("Hello"), []byte("World") 75 _, leafHashTrail := trailsFromByteSlices([][]byte{leaf1}) 76 hash1 := leafHashTrail.Hash 77 _, leafHashTrail = trailsFromByteSlices([][]byte{leaf2}) 78 hash2 := leafHashTrail.Hash 79 if bytes.Equal(hash1, hash2) { 80 t.Errorf("leaf hashes should differ, but both are %x", hash1) 81 } 82 // Compute an intermediate subtree hash. 83 _, subHash1Trail := trailsFromByteSlices([][]byte{hash1, hash2}) 84 subHash1 := subHash1Trail.Hash 85 // Check that this is not the same as a leaf hash of their concatenation. 86 preimage := append(hash1, hash2...) 87 _, forgedHashTrail := trailsFromByteSlices([][]byte{preimage}) 88 forgedHash := forgedHashTrail.Hash 89 if bytes.Equal(subHash1, forgedHash) { 90 t.Errorf("hasher is not second-preimage resistant") 91 } 92 // Swap the order of nodes and check that the hash is different. 93 _, subHash2Trail := trailsFromByteSlices([][]byte{hash2, hash1}) 94 subHash2 := subHash2Trail.Hash 95 if bytes.Equal(subHash1, subHash2) { 96 t.Errorf("subtree hash does not depend on the order of leaves") 97 } 98 }