-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_merkleTree.py
73 lines (56 loc) · 2.54 KB
/
test_merkleTree.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
import common, verifier, merkle as MT
import unittest
class TestMerkle(unittest.TestCase):
def test_prove_leaf(self):
input = [bytes([1]*32),bytes([2]*32),bytes([3]*32),bytes([4]*32),bytes([5]*32)]
merkleTree1 = MT.MerkleTree(input)
#for file_index = 2
file_index = 2
proof = merkleTree1.prove_leaf(2)
leaves = [MT.Node(None, None, None, el) for el in input]
hash12 = MT.combine_hashes(input[0],input[1])
hash34 = MT.combine_hashes(input[2],input[3])
hash1234 = MT.combine_hashes(hash12,hash34)
hash12345 = MT.combine_hashes(hash1234,input[4])
self.assertEqual(proof[0],input[3])
self.assertEqual(proof[1],hash12)
self.assertEqual(proof[2],input[4])
def test_floor_lg(self):
self.assertEqual(MT.floor_lg(1), 0)
self.assertEqual(MT.floor_lg(2), 1)
self.assertEqual(MT.floor_lg(3), 1)
self.assertEqual(MT.floor_lg(4), 2)
self.assertEqual(MT.floor_lg(5), 2)
with self.assertRaises(AssertionError):
MT.floor_lg(-1)
def test_ceil_lg(self):
self.assertEqual(MT.ceil_lg(1),0)
self.assertEqual(MT.ceil_lg(2),1)
self.assertEqual(MT.ceil_lg(3),2)
self.assertEqual(MT.ceil_lg(4),2)
with self.assertRaises(AssertionError):
MT.floor_lg(0)
def test_is_power_of_2(self):
self.assertEqual(MT.is_power_of_2(1),1)
self.assertEqual(MT.is_power_of_2(2),1)
self.assertEqual(MT.is_power_of_2(3),0)
self.assertEqual(MT.is_power_of_2(4),1)
with self.assertRaises(AssertionError):
MT.floor_lg(0)
def test_element_hash(self):
input = bytes([1]*32)
self.assertEqual(MT.element_hash(input),common.sha256(b'\x00' + input))
def test_combine_hashes(self):
input1 = bytes([1]*32)
input2 = bytes([2]*32)
self.assertEqual(MT.combine_hashes(input1,input2),common.sha256(b'\x01' + input1 + input2))
def test_make_tree(self):
input = [bytes([1]*32),bytes([2]*32),bytes([3]*32),bytes([4]*32),bytes([5]*32)]
leaves = [MT.Node(None, None, None, el) for el in input]
hash12 = MT.combine_hashes(input[0],input[1])
hash34 = MT.combine_hashes(input[2],input[3])
hash1234 = MT.combine_hashes(hash12,hash34)
hash12345 = MT.combine_hashes(hash1234,input[4])
self.assertEqual(MT.make_tree(leaves,0,5).value,hash12345)
if __name__ == '__main__':
unittest.main()