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.

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