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