github.com/zorawar87/trillian@v1.2.1/testonly/compact_merkle_tree.go (about) 1 // Copyright 2016 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package testonly 16 17 // MerkleTreeLeafTestInputs returns a slice of leaf inputs that may be used in 18 // compact Merkle tree test cases. They are intended to be added successively, 19 // so that after each addition the corresponding root from MerkleTreeLeafTestRoots 20 // gives the expected Merkle tree root hash. 21 func MerkleTreeLeafTestInputs() [][]byte { 22 return [][]byte{ 23 []byte(""), []byte("\x00"), []byte("\x10"), []byte("\x20\x21"), []byte("\x30\x31"), 24 []byte("\x40\x41\x42\x43"), []byte("\x50\x51\x52\x53\x54\x55\x56\x57"), 25 []byte("\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f")} 26 } 27 28 // MerkleTreeLeafTestRootHashes returns a slice of Merkle tree root hashes that 29 // correspond to the expected tree state for the leaf additions returned by 30 // MerkleTreeLeafTestInputs(), as described above. 31 func MerkleTreeLeafTestRootHashes() [][]byte { 32 return [][]byte{ 33 // constants from C++ test: https://github.com/google/certificate-transparency/blob/master/cpp/merkletree/merkle_tree_test.cc#L277 34 MustHexDecode("6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d"), 35 MustHexDecode("fac54203e7cc696cf0dfcb42c92a1d9dbaf70ad9e621f4bd8d98662f00e3c125"), 36 MustHexDecode("aeb6bcfe274b70a14fb067a5e5578264db0fa9b51af5e0ba159158f329e06e77"), 37 MustHexDecode("d37ee418976dd95753c1c73862b9398fa2a2cf9b4ff0fdfe8b30cd95209614b7"), 38 MustHexDecode("4e3bbb1f7b478dcfe71fb631631519a3bca12c9aefca1612bfce4c13a86264d4"), 39 MustHexDecode("76e67dadbcdf1e10e1b74ddc608abd2f98dfb16fbce75277b5232a127f2087ef"), 40 MustHexDecode("ddb89be403809e325750d3d263cd78929c2942b7942a34b77e122c9594a74c8c"), 41 MustHexDecode("5dc9da79a70659a9ad559cb701ded9a2ab9d823aad2f4960cfe370eff4604328")} 42 } 43 44 // CompactMerkleTreeLeafTestNodeHashes returns the CompactMerkleTree.node state 45 // that must result after each of the leaf additions returned by 46 // MerkleTreeLeafTestInputs(), as described above. 47 func CompactMerkleTreeLeafTestNodeHashes() [][][]byte { 48 return [][][]byte{ 49 nil, // perfect tree size, 2^0 50 nil, // perfect tree size, 2^1 51 {MustDecodeBase64("ApjRIpBtz8EIkstTpzmS/FufST6kybrbJ7eRtBJ6f+c="), MustDecodeBase64("+sVCA+fMaWzw38tCySodnbr3CtnmIfS9jZhmLwDjwSU=")}, 52 nil, // perfect tree size, 2^2 53 {MustDecodeBase64("vBoGQ7EuTS18d5GPROD095qDi2z57FtcKD4fTYhZnms="), nil, MustDecodeBase64("037kGJdt2VdTwcc4Yrk5j6Kiz5tP8P3+izDNlSCWFLc=")}, 54 {nil, MustDecodeBase64("DrxdNDf74tsVi58Sah0RjjCBgQMdCpSfje3t68VY72o="), MustDecodeBase64("037kGJdt2VdTwcc4Yrk5j6Kiz5tP8P3+izDNlSCWFLc=")}, 55 {MustDecodeBase64("sIaT7C5yFZcTBkHoIR5+7cy0wmQTlj7ubB4u0W/7Gl8="), MustDecodeBase64("DrxdNDf74tsVi58Sah0RjjCBgQMdCpSfje3t68VY72o="), MustDecodeBase64("037kGJdt2VdTwcc4Yrk5j6Kiz5tP8P3+izDNlSCWFLc=")}, 56 nil, // perfect tree size, 2^3 57 } 58 } 59 60 // EmptyMerkleTreeRootHash returns the expected root hash for an empty Merkle Tree 61 // that uses SHA256 hashing. 62 func EmptyMerkleTreeRootHash() []byte { 63 const sha256EmptyTreeHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" 64 return MustHexDecode(sha256EmptyTreeHash) 65 }