Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California |
| 4 | * |
| 5 | * This file is part of NSL (NDN Signature Logger). |
| 6 | * See AUTHORS.md for complete list of NSL authors and contributors. |
| 7 | * |
| 8 | * NSL is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NSL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NSL, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of nsl authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "tree-generator.hpp" |
| 23 | #include <ndn-cxx/util/digest.hpp> |
| 24 | |
| 25 | namespace nsl { |
| 26 | namespace tests { |
| 27 | |
| 28 | const Name TreeGenerator::LOGGER_NAME("/logger/name"); |
| 29 | ndn::ConstBufferPtr TreeGenerator::LEAF_HASH; |
| 30 | |
| 31 | ndn::ConstBufferPtr |
| 32 | TreeGenerator::getHash(const Node::Index& idx, |
| 33 | const NonNegativeInteger& nextLeafSeqNo, |
| 34 | bool useEmpty) |
| 35 | { |
| 36 | if (idx.level == 0) { |
| 37 | if (useEmpty) |
| 38 | return Node::getEmptyHash(); |
| 39 | |
| 40 | return Node::getEmptyHash(); |
| 41 | } |
| 42 | |
| 43 | NonNegativeInteger leftChildSeqNo = idx.seqNo; |
| 44 | NonNegativeInteger rightChildSeqNo = idx.seqNo + (idx.range >> 1); |
| 45 | |
| 46 | if (idx.seqNo == 0 && nextLeafSeqNo <= rightChildSeqNo) { |
| 47 | BOOST_ASSERT(false); |
| 48 | } |
| 49 | |
| 50 | ndn::util::Sha256 sha256; |
| 51 | sha256 << idx.level << idx.seqNo; |
| 52 | |
| 53 | auto hash1 = getHash(Node::Index(leftChildSeqNo, idx.level - 1), |
| 54 | nextLeafSeqNo, |
| 55 | useEmpty); |
| 56 | sha256.update(hash1->buf(), hash1->size()); |
| 57 | |
| 58 | if (nextLeafSeqNo > rightChildSeqNo) { |
| 59 | auto hash2 = getHash(Node::Index(rightChildSeqNo, idx.level - 1), |
| 60 | nextLeafSeqNo, |
| 61 | useEmpty); |
| 62 | sha256.update(hash2->buf(), hash2->size()); |
| 63 | } |
| 64 | else { |
| 65 | auto hash2 = Node::getEmptyHash(); |
| 66 | sha256.update(hash2->buf(), hash2->size()); |
| 67 | } |
| 68 | return sha256.computeDigest(); |
| 69 | } |
| 70 | |
| 71 | shared_ptr<SubTreeBinary> |
| 72 | TreeGenerator::getSubTreeBinary(const Node::Index& index, |
| 73 | const NonNegativeInteger& nextLeafSeqNo, |
| 74 | bool useEmpty) |
| 75 | { |
| 76 | auto subtree = make_shared<SubTreeBinary>(LOGGER_NAME, index, |
| 77 | [&] (const Node::Index&) {}, |
| 78 | [&] (const Node::Index&, |
| 79 | const NonNegativeInteger&, |
| 80 | ndn::ConstBufferPtr) {}); |
| 81 | |
| 82 | size_t leafLevel = index.level + 1 - SubTreeBinary::SUB_TREE_DEPTH; |
| 83 | NonNegativeInteger step = 1 << leafLevel; |
| 84 | |
| 85 | for (NonNegativeInteger i = index.seqNo; i < nextLeafSeqNo - step; i += step) { |
| 86 | auto node = make_shared<Node>(i, leafLevel, i + step, |
| 87 | getHash(Node::Index(i, leafLevel), |
| 88 | i + step, |
| 89 | useEmpty)); |
| 90 | subtree->addLeaf(node); |
| 91 | } |
| 92 | |
| 93 | NonNegativeInteger childSeqNo = ((nextLeafSeqNo - 1) >> leafLevel) << leafLevel; |
| 94 | auto node = make_shared<Node>(childSeqNo, leafLevel, nextLeafSeqNo, |
| 95 | getHash(Node::Index(childSeqNo, leafLevel), |
| 96 | nextLeafSeqNo, |
| 97 | useEmpty)); |
| 98 | subtree->addLeaf(node); |
| 99 | |
| 100 | return subtree; |
| 101 | } |
| 102 | |
| 103 | ndn::ConstBufferPtr |
| 104 | TreeGenerator::getLeafHash() |
| 105 | { |
| 106 | if (LEAF_HASH == nullptr) { |
| 107 | ndn::util::Sha256 sha256; |
| 108 | sha256 << 1; |
| 109 | LEAF_HASH = sha256.computeDigest(); |
| 110 | } |
| 111 | |
| 112 | return LEAF_HASH; |
| 113 | } |
| 114 | |
| 115 | } // namespace tests |
| 116 | } // namespace nsl |