From b5fd359c0ad07afea82d8e8cfcb6e4f64fb86e88 Mon Sep 17 00:00:00 2001 From: Katarzyna Streich Date: Mon, 10 Oct 2016 18:40:05 +0100 Subject: [PATCH] Add Partial Merkle Tree verification. Not tested. --- .../r3corda/core/crypto/PartialMerkleTree.kt | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/core/src/main/kotlin/com/r3corda/core/crypto/PartialMerkleTree.kt b/core/src/main/kotlin/com/r3corda/core/crypto/PartialMerkleTree.kt index a1d993fe69..5a0a13cf69 100644 --- a/core/src/main/kotlin/com/r3corda/core/crypto/PartialMerkleTree.kt +++ b/core/src/main/kotlin/com/r3corda/core/crypto/PartialMerkleTree.kt @@ -120,4 +120,38 @@ class PartialMerkleTree( } + fun verify(leavesHashes: List, merkleRoot: SecureHash): Boolean{ + includeIdx = 0 //todo check that + hashIdx = 0 + val hashesUsed = ArrayList() + val verifyRoot = verifyTree(treeHeight, 0, hashesUsed) + //It means that we obtained more/less hashes than needed. Or different sets of hashes. + //Ordering insensitive. + if(leavesHashes.size != hashesUsed.size || leavesHashes.minus(hashesUsed).isNotEmpty()) + return false + return (verifyRoot == merkleRoot) //Correctness of hashes is checked by folding the tree. + } + + private fun verifyTree(height: Int, position: Int, hashesUsed: MutableList): SecureHash { + if(includeIdx >= includeBranch.size) + throw MerkleTreeException("Included nodes list index overflow.") + val isParent = includeBranch[includeIdx] + includeIdx++ + if (height == 0 || !isParent) { + if(hashIdx >branchHashes.size) + throw MerkleTreeException("Branch hashes index overflow.") + val hash = branchHashes[hashIdx] + hashIdx++ + if(height == 0 && isParent) + hashesUsed.add(hash) //todo or hash into a tree + return hash + } else { + val left: SecureHash = verifyTree(height - 1, position * 2, hashesUsed) + val right: SecureHash = when{ + position * 2 + 1 < treeWidth(height, leavesSize)-1 -> verifyTree(height - 1, position * 2 + 1, hashesUsed) + else -> left + } + return left.hashConcat(right) + } + } }