-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple merkle rfc compatibility (#2713)
* Begin simple merkle compatibility PR * Fix query_test * Use trillian test vectors * Change the split point per RFC 6962 * update spec * refactor innerhash to match spec * Update changelog * Address @liamsi's comments * Write the comment requested by @liamsi
- Loading branch information
1 parent
1f68318
commit ec53ce3
Showing
15 changed files
with
233 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package merkle | ||
|
||
import ( | ||
"github.com/tendermint/tendermint/crypto/tmhash" | ||
) | ||
|
||
// TODO: make these have a large predefined capacity | ||
var ( | ||
leafPrefix = []byte{0} | ||
innerPrefix = []byte{1} | ||
) | ||
|
||
// returns tmhash(0x00 || leaf) | ||
func leafHash(leaf []byte) []byte { | ||
return tmhash.Sum(append(leafPrefix, leaf...)) | ||
} | ||
|
||
// returns tmhash(0x01 || left || right) | ||
func innerHash(left []byte, right []byte) []byte { | ||
return tmhash.Sum(append(innerPrefix, append(left, right...)...)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package merkle | ||
|
||
// Copyright 2016 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// These tests were taken from https://github.com/google/trillian/blob/master/merkle/rfc6962/rfc6962_test.go, | ||
// and consequently fall under the above license. | ||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"testing" | ||
|
||
"github.com/tendermint/tendermint/crypto/tmhash" | ||
) | ||
|
||
func TestRFC6962Hasher(t *testing.T) { | ||
_, leafHashTrail := trailsFromByteSlices([][]byte{[]byte("L123456")}) | ||
leafHash := leafHashTrail.Hash | ||
_, leafHashTrail = trailsFromByteSlices([][]byte{[]byte{}}) | ||
emptyLeafHash := leafHashTrail.Hash | ||
for _, tc := range []struct { | ||
desc string | ||
got []byte | ||
want string | ||
}{ | ||
// Since creating a merkle tree of no leaves is unsupported here, we skip | ||
// the corresponding trillian test vector. | ||
|
||
// Check that the empty hash is not the same as the hash of an empty leaf. | ||
// echo -n 00 | xxd -r -p | sha256sum | ||
{ | ||
desc: "RFC6962 Empty Leaf", | ||
want: "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d"[:tmhash.Size*2], | ||
got: emptyLeafHash, | ||
}, | ||
// echo -n 004C313233343536 | xxd -r -p | sha256sum | ||
{ | ||
desc: "RFC6962 Leaf", | ||
want: "395aa064aa4c29f7010acfe3f25db9485bbd4b91897b6ad7ad547639252b4d56"[:tmhash.Size*2], | ||
got: leafHash, | ||
}, | ||
// echo -n 014E3132334E343536 | xxd -r -p | sha256sum | ||
{ | ||
desc: "RFC6962 Node", | ||
want: "aa217fe888e47007fa15edab33c2b492a722cb106c64667fc2b044444de66bbb"[:tmhash.Size*2], | ||
got: innerHash([]byte("N123"), []byte("N456")), | ||
}, | ||
} { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
wantBytes, err := hex.DecodeString(tc.want) | ||
if err != nil { | ||
t.Fatalf("hex.DecodeString(%x): %v", tc.want, err) | ||
} | ||
if got, want := tc.got, wantBytes; !bytes.Equal(got, want) { | ||
t.Errorf("got %x, want %x", got, want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRFC6962HasherCollisions(t *testing.T) { | ||
// Check that different leaves have different hashes. | ||
leaf1, leaf2 := []byte("Hello"), []byte("World") | ||
_, leafHashTrail := trailsFromByteSlices([][]byte{leaf1}) | ||
hash1 := leafHashTrail.Hash | ||
_, leafHashTrail = trailsFromByteSlices([][]byte{leaf2}) | ||
hash2 := leafHashTrail.Hash | ||
if bytes.Equal(hash1, hash2) { | ||
t.Errorf("Leaf hashes should differ, but both are %x", hash1) | ||
} | ||
// Compute an intermediate subtree hash. | ||
_, subHash1Trail := trailsFromByteSlices([][]byte{hash1, hash2}) | ||
subHash1 := subHash1Trail.Hash | ||
// Check that this is not the same as a leaf hash of their concatenation. | ||
preimage := append(hash1, hash2...) | ||
_, forgedHashTrail := trailsFromByteSlices([][]byte{preimage}) | ||
forgedHash := forgedHashTrail.Hash | ||
if bytes.Equal(subHash1, forgedHash) { | ||
t.Errorf("Hasher is not second-preimage resistant") | ||
} | ||
// Swap the order of nodes and check that the hash is different. | ||
_, subHash2Trail := trailsFromByteSlices([][]byte{hash2, hash1}) | ||
subHash2 := subHash2Trail.Hash | ||
if bytes.Equal(subHash1, subHash2) { | ||
t.Errorf("Subtree hash does not depend on the order of leaves") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.