github.com/evdatsion/aphelion-dpos-bft@v0.32.1/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/evdatsion/aphelion-dpos-bft/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 t.Run(tc.desc, func(t *testing.T) { 60 wantBytes, err := hex.DecodeString(tc.want) 61 if err != nil { 62 t.Fatalf("hex.DecodeString(%x): %v", tc.want, err) 63 } 64 if got, want := tc.got, wantBytes; !bytes.Equal(got, want) { 65 t.Errorf("got %x, want %x", got, want) 66 } 67 }) 68 } 69 } 70 71 func TestRFC6962HasherCollisions(t *testing.T) { 72 // Check that different leaves have different hashes. 73 leaf1, leaf2 := []byte("Hello"), []byte("World") 74 _, leafHashTrail := trailsFromByteSlices([][]byte{leaf1}) 75 hash1 := leafHashTrail.Hash 76 _, leafHashTrail = trailsFromByteSlices([][]byte{leaf2}) 77 hash2 := leafHashTrail.Hash 78 if bytes.Equal(hash1, hash2) { 79 t.Errorf("Leaf hashes should differ, but both are %x", hash1) 80 } 81 // Compute an intermediate subtree hash. 82 _, subHash1Trail := trailsFromByteSlices([][]byte{hash1, hash2}) 83 subHash1 := subHash1Trail.Hash 84 // Check that this is not the same as a leaf hash of their concatenation. 85 preimage := append(hash1, hash2...) 86 _, forgedHashTrail := trailsFromByteSlices([][]byte{preimage}) 87 forgedHash := forgedHashTrail.Hash 88 if bytes.Equal(subHash1, forgedHash) { 89 t.Errorf("Hasher is not second-preimage resistant") 90 } 91 // Swap the order of nodes and check that the hash is different. 92 _, subHash2Trail := trailsFromByteSlices([][]byte{hash2, hash1}) 93 subHash2 := subHash2Trail.Hash 94 if bytes.Equal(subHash1, subHash2) { 95 t.Errorf("Subtree hash does not depend on the order of leaves") 96 } 97 }