-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMerkleBundle.hpp
164 lines (126 loc) · 3.79 KB
/
MerkleBundle.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ifndef _SNARKFRONT_MERKLE_BUNDLE_HPP_
#define _SNARKFRONT_MERKLE_BUNDLE_HPP_
#include <cstdint>
#include <functional>
#include <iostream>
#include <istream>
#include <ostream>
#include <set>
#include <vector>
#include <snarkfront/DSL_utility.hpp>
#include <snarkfront/MerkleAuthPath.hpp>
#include <snarkfront/MerkleTree.hpp>
namespace snarkfront {
////////////////////////////////////////////////////////////////////////////////
// Merkle tree with authentication paths
//
template <typename TREE, typename PATH, typename COUNT>
class MerkleBundle
{
public:
typedef typename TREE::HashType HashType;
typedef typename TREE::DigType DigType;
typedef PATH AuthPath;
typedef COUNT Count;
MerkleBundle()
: m_treeSize(0)
{}
MerkleBundle(const std::size_t depth)
: m_tree(depth),
m_treeSize(0)
{}
bool isFull() const {
return m_tree.isFull();
}
COUNT treeSize() const {
return m_treeSize;
}
const DigType& rootHash() const {
return m_tree.authPath().rootHash();
}
std::size_t addLeaf(const DigType& cm, const bool keepPath = false) {
m_tree.updatePath(cm, m_authPath);
const std::size_t pathIndex = keepPath ? m_authPath.size() : -1;
if (keepPath) {
m_authLeaf.emplace_back(cm);
m_authPath.emplace_back(m_tree.authPath());
}
m_tree.updateSiblings(cm);
++m_treeSize;
return pathIndex;
}
const std::vector<DigType>& authLeaf() const {
return m_authLeaf;
}
const std::vector<PATH>& authPath() const {
return m_authPath;
}
void cleanup(std::function<bool (const DigType&)> func) {
std::vector<DigType> keepLeaf;
std::vector<PATH> keepPath;
for (std::size_t i = 0; i < m_authLeaf.size(); ++i) {
const auto& cm = m_authLeaf[i];
if (func(cm)) {
keepLeaf.emplace_back(cm);
keepPath.emplace_back(m_authPath[i]);
}
}
m_authLeaf = keepLeaf;
m_authPath = keepPath;
}
void marshal_out(std::ostream& os) const {
os << m_tree
<< m_treeSize << std::endl
<< m_authLeaf;
for (const auto& r : m_authPath)
os << r;
}
bool marshal_in(std::istream& is) {
if (!m_tree.marshal_in(is) || !(is >> m_treeSize) || !(is >> m_authLeaf))
return false;
m_authPath.resize(m_authLeaf.size());
for (auto& r : m_authPath) {
if (! r.marshal_in(is)) return false;
}
return true;
}
void clear() {
m_tree.clear();
m_treeSize = 0;
m_authLeaf.clear();
m_authPath.clear();
}
bool empty() const {
return
m_tree.empty() ||
0 == m_treeSize ||
m_authLeaf.empty() ||
m_authPath.empty();
}
private:
TREE m_tree;
COUNT m_treeSize;
std::vector<DigType> m_authLeaf;
std::vector<PATH> m_authPath;
};
template <typename TREE, typename PATH, typename COUNT>
std::ostream& operator<< (std::ostream& os,
const MerkleBundle<TREE, PATH, COUNT>& a) {
a.marshal_out(os);
return os;
}
template <typename TREE, typename PATH, typename COUNT>
std::istream& operator>> (std::istream& is,
MerkleBundle<TREE, PATH, COUNT>& a) {
if (! a.marshal_in(is)) a.clear();
return is;
}
////////////////////////////////////////////////////////////////////////////////
// typedefs
//
template <typename COUNT> using MerkleBundle_SHA256
= MerkleBundle<MerkleTree_SHA256, eval::MerkleAuthPath_SHA256, COUNT>;
template <typename COUNT> using MerkleBundle_SHA512
= MerkleBundle<MerkleTree_SHA512, eval::MerkleAuthPath_SHA512, COUNT>;
} // namespace snarkfront
#endif