You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.4 KiB

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