-
Notifications
You must be signed in to change notification settings - Fork 41
/
MerkleTree.cs
366 lines (305 loc) · 13 KB
/
MerkleTree.cs
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* Copyright (c) Marc Clifton
* The Code Project Open License (CPOL) 1.02
* http://www.codeproject.com/info/cpol10.aspx
*/
using System;
using System.Collections.Generic;
using System.Linq;
using Clifton.Core.ExtensionMethods;
namespace Clifton.Blockchain
{
public class MerkleTree
{
public MerkleNode RootNode { get; protected set; }
protected List<MerkleNode> nodes;
protected List<MerkleNode> leaves;
public static void Contract(Func<bool> action, string msg)
{
if (!action())
{
throw new MerkleException(msg);
}
}
public MerkleTree()
{
nodes = new List<MerkleNode>();
leaves = new List<MerkleNode>();
}
public MerkleNode AppendLeaf(MerkleNode node)
{
nodes.Add(node);
leaves.Add(node);
return node;
}
public void AppendLeaves(MerkleNode[] nodes)
{
nodes.ForEach(n => AppendLeaf(n));
}
public MerkleNode AppendLeaf(MerkleHash hash)
{
var node = CreateNode(hash);
nodes.Add(node);
leaves.Add(node);
return node;
}
public List<MerkleNode> AppendLeaves(MerkleHash[] hashes)
{
List<MerkleNode> nodes = new List<MerkleNode>();
hashes.ForEach(h => nodes.Add(AppendLeaf(h)));
return nodes;
}
public MerkleHash AddTree(MerkleTree tree)
{
Contract(() => leaves.Count > 0, "Cannot add to a tree with no leaves.");
tree.leaves.ForEach(l => AppendLeaf(l));
return BuildTree();
}
/// <summary>
/// If we have an odd number of leaves, add a leaf that
/// is a duplicate of the last leaf hash so that when we add the leaves of the new tree,
/// we don't change the root hash of the current tree.
/// This method should only be used if you have a specific reason that you need to balance
/// the last node with it's right branch, for example as a pre-step to computing an audit trail
/// on the last leaf of an odd number of leaves in the tree.
/// </summary>
public void FixOddNumberLeaves()
{
if ((leaves.Count & 1) == 1)
{
var lastLeaf = leaves.Last();
var l = AppendLeaf(lastLeaf.Hash);
// l.Text = lastLeaf.Text;
}
}
/// <summary>
/// Builds the tree for leaves and returns the root node.
/// </summary>
public MerkleHash BuildTree()
{
// We do not call FixOddNumberLeaves because we want the ability to append
// leaves and add additional trees without creating unecessary wasted space in the tree.
Contract(() => leaves.Count > 0, "Cannot build a tree with no leaves.");
BuildTree(leaves);
return RootNode.Hash;
}
// Why would we need this?
//public void RegisterRoot(MerkleNode node)
//{
// Contract(() => node.Parent == null, "Node is not a root node.");
// rootNode = node;
//}
/// <summary>
/// Returns the audit proof hashes to reconstruct the root hash.
/// </summary>
/// <param name="leafHash">The leaf hash we want to verify exists in the tree.</param>
/// <returns>The audit trail of hashes needed to create the root, or an empty list if the leaf hash doesn't exist.</returns>
public List<MerkleProofHash> AuditProof(MerkleHash leafHash)
{
List<MerkleProofHash> auditTrail = new List<MerkleProofHash>();
var leafNode = FindLeaf(leafHash);
if (leafNode != null)
{
Contract(() => leafNode.Parent != null, "Expected leaf to have a parent.");
var parent = leafNode.Parent;
BuildAuditTrail(auditTrail, parent, leafNode);
}
return auditTrail;
}
/// <summary>
/// Verifies ordering and consistency of the first n leaves, such that we reach the expected subroot.
/// This verifies that the prior data has not been changed and that leaf order has been preserved.
/// m is the number of leaves for which to do a consistency check.
/// </summary>
public List<MerkleProofHash> ConsistencyProof(int m)
{
// Rule 1:
// Find the leftmost node of the tree from which we can start our consistency proof.
// Set k, the number of leaves for this node.
List<MerkleProofHash> hashNodes = new List<MerkleProofHash>();
int idx = (int)Math.Log(m, 2);
// Get the leftmost node.
MerkleNode node = leaves[0];
// Traverse up the tree until we get to the node specified by idx.
while (idx > 0)
{
node = node.Parent;
--idx;
}
int k = node.Leaves().Count();
hashNodes.Add(new MerkleProofHash(node.Hash, MerkleProofHash.Branch.OldRoot));
if (m == k)
{
// Continue with Rule 3 -- the remainder is the audit proof
}
else
{
// Rule 2:
// Set the initial sibling node (SN) to the sibling of the node acquired by Rule 1.
// if m-k == # of SN's leaves, concatenate the hash of the sibling SN and exit Rule 2, as this represents the hash of the old root.
// if m - k < # of SN's leaves, set SN to SN's left child node and repeat Rule 2.
// sibling node:
MerkleNode sn = node.Parent.RightNode;
bool traverseTree = true;
while (traverseTree)
{
Contract(() => sn != null, "Sibling node must exist because m != k");
int sncount = sn.Leaves().Count();
if (m - k == sncount)
{
hashNodes.Add(new MerkleProofHash(sn.Hash, MerkleProofHash.Branch.OldRoot));
break;
}
if (m - k > sncount)
{
hashNodes.Add(new MerkleProofHash(sn.Hash, MerkleProofHash.Branch.OldRoot));
sn = sn.Parent.RightNode;
k += sncount;
}
else // (m - k < sncount)
{
sn = sn.LeftNode;
}
}
}
// Rule 3: Apply ConsistencyAuditProof below.
return hashNodes;
}
/// <summary>
/// Completes the consistency proof with an audit proof using the last node in the consistency proof.
/// </summary>
public List<MerkleProofHash> ConsistencyAuditProof(MerkleHash nodeHash)
{
List<MerkleProofHash> auditTrail = new List<MerkleProofHash>();
var node = RootNode.Single(n => n.Hash == nodeHash);
var parent = node.Parent;
BuildAuditTrail(auditTrail, parent, node);
return auditTrail;
}
/// <summary>
/// Verify that if we walk up the tree from a particular leaf, we encounter the expected root hash.
/// </summary>
public static bool VerifyAudit(MerkleHash rootHash, MerkleHash leafHash, List<MerkleProofHash> auditTrail)
{
Contract(() => auditTrail.Count > 0, "Audit trail cannot be empty.");
MerkleHash testHash = leafHash;
// TODO: Inefficient - compute hashes directly.
foreach (MerkleProofHash auditHash in auditTrail)
{
testHash = auditHash.Direction == MerkleProofHash.Branch.Left ?
MerkleHash.Create(testHash.Value.Concat(auditHash.Hash.Value).ToArray()) :
MerkleHash.Create(auditHash.Hash.Value.Concat(testHash.Value).ToArray());
}
return rootHash == testHash;
}
/// <summary>
/// For demo / debugging purposes, we return the pairs of hashes used to verify the audit proof.
/// </summary>
public static List<Tuple<MerkleHash, MerkleHash>> AuditHashPairs(MerkleHash leafHash, List<MerkleProofHash> auditTrail)
{
Contract(() => auditTrail.Count > 0, "Audit trail cannot be empty.");
var auditPairs = new List<Tuple<MerkleHash, MerkleHash>>();
MerkleHash testHash = leafHash;
// TODO: Inefficient - compute hashes directly.
foreach (MerkleProofHash auditHash in auditTrail)
{
switch (auditHash.Direction)
{
case MerkleProofHash.Branch.Left:
auditPairs.Add(new Tuple<MerkleHash, MerkleHash>(testHash, auditHash.Hash));
testHash = MerkleHash.Create(testHash.Value.Concat(auditHash.Hash.Value).ToArray());
break;
case MerkleProofHash.Branch.Right:
auditPairs.Add(new Tuple<MerkleHash, MerkleHash>(auditHash.Hash, testHash));
testHash = MerkleHash.Create(auditHash.Hash.Value.Concat(testHash.Value).ToArray());
break;
}
}
return auditPairs;
}
public static bool VerifyConsistency(MerkleHash oldRootHash, List<MerkleProofHash> proof)
{
MerkleHash hash, lhash, rhash;
if (proof.Count > 1)
{
lhash = proof[proof.Count - 2].Hash;
int hidx = proof.Count - 1;
hash = rhash = MerkleTree.ComputeHash(lhash, proof[hidx].Hash);
hidx -= 2;
// foreach (var nextHashNode in proof.Skip(1))
while (hidx >= 0)
{
lhash = proof[hidx].Hash;
hash = rhash = MerkleTree.ComputeHash(lhash, rhash);
--hidx;
}
}
else
{
hash = proof[0].Hash;
}
return hash == oldRootHash;
}
public static MerkleHash ComputeHash(MerkleHash left, MerkleHash right)
{
return MerkleHash.Create(left.Value.Concat(right.Value).ToArray());
}
protected void BuildAuditTrail(List<MerkleProofHash> auditTrail, MerkleNode parent, MerkleNode child)
{
if (parent != null)
{
Contract(() => child.Parent == parent, "Parent of child is not expected parent.");
var nextChild = parent.LeftNode == child ? parent.RightNode : parent.LeftNode;
var direction = parent.LeftNode == child ? MerkleProofHash.Branch.Left : MerkleProofHash.Branch.Right;
// For the last leaf, the right node may not exist. In that case, we ignore it because it's
// the hash we are given to verify.
if (nextChild != null)
{
auditTrail.Add(new MerkleProofHash(nextChild.Hash, direction));
}
BuildAuditTrail(auditTrail, child.Parent.Parent, child.Parent);
}
}
protected MerkleNode FindLeaf(MerkleHash leafHash)
{
// TODO: We can improve the search for the leaf hash by maintaining a sorted list of leaf hashes.
// We use First because a tree with an odd number of leaves will duplicate the last leaf
// and will therefore have the same hash.
return leaves.FirstOrDefault(l => l.Hash == leafHash);
}
/// <summary>
/// Reduce the current list of n nodes to n/2 parents.
/// </summary>
/// <param name="nodes"></param>
protected void BuildTree(List<MerkleNode> nodes)
{
Contract(() => nodes.Count > 0, "node list not expected to be empty.");
if (nodes.Count == 1)
{
RootNode = nodes[0];
}
else
{
List<MerkleNode> parents = new List<MerkleNode>();
for (int i = 0; i < nodes.Count; i += 2)
{
MerkleNode right = (i + 1 < nodes.Count) ? nodes[i + 1] : null;
// Constructing the MerkleNode resolves the right node being null.
MerkleNode parent = CreateNode(nodes[i], right);
parents.Add(parent);
}
BuildTree(parents);
}
}
// Override in derived class to extend the behavior.
// Alternatively, we could implement a factory pattern.
protected virtual MerkleNode CreateNode(MerkleHash hash)
{
return new MerkleNode(hash);
}
protected virtual MerkleNode CreateNode(MerkleNode left, MerkleNode right)
{
return new MerkleNode(left, right);
}
}
}