-
Notifications
You must be signed in to change notification settings - Fork 56
/
test_merkle_proof.py
executable file
·156 lines (127 loc) · 4.7 KB
/
test_merkle_proof.py
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
import unittest
from merkle_proof import *
from merkle_tree import *
from node import Node
class TestMerkleProof(unittest.TestCase):
def test_one_proof(self):
"""Test that the proof can handle a tree with only one transaction.
No other data is necessary to arrive at the block header
"""
tx1 = 'a'
merkle_tree = MerkleTree([tx1])
self.assertEqual([], merkle_proof(tx1, merkle_tree))
def test_small_proof(self):
"""Test that the proof can handle a tree with only two transactions"""
tx1 = 'a'
tx2 = 'b'
merkle_tree = MerkleTree([tx1, tx2])
self.assertEqual([Node('r', tx2)], merkle_proof(tx1, merkle_tree))
self.assertEqual([Node('l', tx1)], merkle_proof(tx2, merkle_tree))
def test_medium_proof(self):
"""Test that the proof can handle a tree with up to four
transactions
"""
tx1 = 'a'
tx2 = 'b'
tx3 = 'c'
tx4 = 'd'
merkle_tree = MerkleTree([tx1, tx2, tx3, tx4])
data = tx1 + tx2
data = hash_data(data, 'sha256')
self.assertEqual([Node('l', data), Node('l', tx3)], merkle_proof(tx4, merkle_tree))
self.assertEqual([Node('l', data), Node('r', tx4)], merkle_proof(tx3, merkle_tree))
data = tx3 + tx4
data = hash_data(data, 'sha256')
self.assertEqual([Node('r', data), Node('l', tx1)], merkle_proof(tx2, merkle_tree))
self.assertEqual([Node('r', data), Node('r', tx2)], merkle_proof(tx1, merkle_tree))
def test_large_proof(self):
"""Test that the proof can handle a tree with up to eight
transaction"""
tx1 = 'a'
tx2 = 'b'
tx3 = 'c'
tx4 = 'd'
tx5 = 'e'
tx6 = 'f'
tx7 = 'g'
tx8 = 'h'
data1 = tx1 + tx2
data1 = hash_data(data1, 'sha256')
data2 = tx5 + tx6
data2 = hash_data(data2, 'sha256')
data3 = tx7 + tx8
data3 = hash_data(data3, 'sha256')
data4 = data2 + data3
data4 = hash_data(data4, 'sha256')
merkle_tree = MerkleTree([tx1, tx2, tx3, tx4, tx5, tx6, tx7, tx8])
self.assertEqual([Node('r', data4), Node('l', data1), Node('r', tx4)], merkle_proof(tx3, merkle_tree))
def test_extra_large_proof(self):
"""Test that the proof can handle a tree with up to eight
transaction"""
tx1 = 'a'
tx2 = 'b'
tx3 = 'c'
tx4 = 'd'
tx5 = 'e'
tx6 = 'f'
tx7 = 'g'
tx8 = 'h'
tx9 = 'i'
tx10 = 'j'
tx11 = 'k'
tx12 = 'l'
tx13 = 'm'
tx14 = 'n'
tx15 = 'o'
tx16 = 'p'
data1 = hash_data(tx1 + tx2, 'sha256')
data2 = hash_data(tx3 + tx4, 'sha256')
data3 = hash_data(tx5 + tx6, 'sha256')
data4 = hash_data(tx7 + tx8, 'sha256')
data5 = hash_data(hash_data(data1 + data2, 'sha256') + hash_data(data3 + data4, 'sha256'), 'sha256')
data6 = hash_data(tx15 + tx16, 'sha256')
data7 = hash_data(tx13 + tx14, 'sha256')
data8 = hash_data(data7 + data6, 'sha256')
data9 = hash_data(tx11 + tx12, 'sha256')
merkle_tree = MerkleTree([tx1, tx2, tx3, tx4, tx5, tx6, tx7, tx8, tx9, tx10, tx11, tx12, tx13, tx14, tx15, tx16])
self.assertEqual([Node('l', data5), Node('r', data8), Node('r', data9), Node('r', tx10)], merkle_proof(tx9, merkle_tree))
def test_verify_proof_small(self):
"""Test that the proof can be verified; the hash must be reconstructed
exactly right. Issues may come up with the order in which data is
hashed
"""
tx1 = 'a'
tx2 = 'b'
tx3 = 'c'
tx4 = 'd'
merkle_tree = MerkleTree([tx1, tx2, tx3, tx4])
proof = merkle_proof(tx1, merkle_tree)
verified_hash = verify_proof(tx1, proof)
self.assertEqual(verified_hash, merkle_tree.block_header)
def test_verify_proof_big(self):
"""Test that the proof can be verified; the hash must be reconstructed
exactly right. Issues may come up with the order in which data is
hashed
"""
tx1 = 'a'
tx2 = 'b'
tx3 = 'c'
tx4 = 'd'
tx5 = 'e'
tx6 = 'f'
tx7 = 'g'
tx8 = 'h'
tx9 = 'i'
tx10 = 'j'
tx11 = 'k'
tx12 = 'l'
tx13 = 'm'
tx14 = 'n'
tx15 = 'o'
tx16 = 'p'
merkle_tree = MerkleTree([tx1, tx2, tx3, tx4, tx5, tx6, tx7, tx8, tx9, tx10, tx11, tx12, tx13, tx14, tx15, tx16])
proof = merkle_proof(tx2, merkle_tree)
verified_hash = verify_proof(tx2, proof)
self.assertEqual(verified_hash, merkle_tree.block_header)
if __name__ == '__main__':
unittest.main()