blob: f74af330c912e62a9ad42fb59c1c079fc4af5145 [file] [log] [blame]
peizhen guo410e0e12014-08-12 13:24:14 -07001/* -*- 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 * @author Peizhen Guo <patrick.guopz@gmail.com>
20 */
21#include "merkle-tree.hpp"
22
23namespace nsl {
24
25MerkleTree::MerkleTree()
26{
27 m_nLevels = 0;
28 m_nLeaves = 0;
29}
30
31
32ConstNodePtr
33MerkleTree::getNode(const Index& index)
34{
35 ConstNodePtr p_leav;
36 if (m_cache.doesNodeExist(index))
37 {
38 p_leav = m_cache.queryNode(index);
39 return p_leav;
40 }
41 else
42 return p_leav;
43}
44
45
46uint64_t
47MerkleTree::getLeafNum() const
48{
49 return m_nLeaves;
50}
51
52
53uint64_t
54MerkleTree::getLevel() const
55{
56 return m_nLevels;
57}
58
59
60
61uint64_t
62MerkleTree::addLeaf(ndn::ConstBufferPtr info)
63{
64 Leaf new_leaf(info, m_nLeaves, 0, 0);
65 new_leaf.computeHash(); // computeHash() has been written.
66 m_nLeaves++;
67 if (m_nLeaves > int(pow(2, int(m_nLevels) - 1)))
68 {
69 m_nLevels++;
70 }
71 m_cache.addLeaf(new_leaf);
72 return m_nLeaves - 1;
73}
74
75
76std::vector<ConstNodePtr>
77MerkleTree::generateProof(uint64_t version1, uint64_t version2)
78{
79 std::vector<ConstNodePtr> proof;
80 if (version1 >= version2)
81 {
82 return proof;
83 }
84
85 //add a memberproof from version2
86 Index this_idx;
87 this_idx.number = version2;
88 this_idx.level = 0;
89 ConstNodePtr p_leav;
90 p_leav = m_cache.queryNode(this_idx);
91 proof.push_back(p_leav);
92 if ((this_idx.number % 2) != 0)
93 {
94 this_idx.number -= 1;
95 p_leav = m_cache.queryNode(this_idx);
96 proof.push_back(p_leav);
97 }
98 this_idx.level += 1;
99 this_idx.number -= this_idx.number % 2;
100 for (int i = 1; i < m_nLevels - 1 ; i++)
101 {
102 if (this_idx.number % int(pow(2, i + 1)) != 0)
103 {
104 this_idx.number -= int(pow(2, i));
105 p_leav = m_cache.queryNode(this_idx);
106 proof.push_back(p_leav);
107 }
108 this_idx.level += 1;
109 this_idx.number -= this_idx.number % int(pow(2, i + 1));
110 }
111
112 //add another path from version1
113 this_idx.number = version1;
114 this_idx.level = 0;
115 p_leav = m_cache.queryNode(this_idx);
116 proof.push_back(p_leav);
117 if ((this_idx.number % 2) != 0)
118 {
119 this_idx.number -= 1;
120 p_leav = m_cache.queryNode(this_idx);
121 proof.push_back(p_leav);
122 }
123 this_idx.level += 1;
124 this_idx.number -= this_idx.number % 2;
125 for (int i = 1; i < m_nLevels - 1 ; i++)
126 {
127 if (this_idx.number % int(pow(2, i + 1)) != 0)
128 {
129 this_idx.number -= int(pow(2, i));
130 p_leav = m_cache.queryNode(this_idx);
131 proof.push_back(p_leav);
132 }
133 this_idx.level += 1;
134 this_idx.number -= this_idx.number % int(pow(2, i + 1));
135 }
136
137 return proof;
138}
139
140} // namespace nsl