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