-
Notifications
You must be signed in to change notification settings - Fork 24
/
demo.py
144 lines (108 loc) · 4.21 KB
/
demo.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
"""
pymerkle demo
"""
import sys
import argparse
from math import log10
from pymerkle import (
constants,
InmemoryTree,
SqliteTree as _SqliteTree,
verify_inclusion,
verify_consistency
)
# Make init interface identical to that of InmemoryTree
class SqliteTree(_SqliteTree):
def __init__(self, algorithm='sha256', **opts):
super().__init__(':memory:', algorithm, **opts)
def parse_cli_args():
config = {'prog': sys.argv[0], 'usage': 'python %s' % sys.argv[0],
'description': __doc__, 'epilog': '\n',
'formatter_class': argparse.ArgumentDefaultsHelpFormatter}
parser = argparse.ArgumentParser(**config)
parser.add_argument('--backend', choices=['inmemory', 'sqlite'],
default='inmemory', help='Storage backend')
parser.add_argument('--algorithm', choices=constants.ALGORITHMS,
default='sha256', help='Hashing algorithm')
parser.add_argument('--threshold', type=int, metavar='WIDTH',
default=128, help='Subroot cache threshold')
parser.add_argument('--capacity', type=int, metavar='MAXSIZE',
default=1024 ** 3, help='Subroot cache capacity in bytes')
parser.add_argument('--disable-security', action='store_true',
default=False, help='Disable resistance against second-preimage attack')
parser.add_argument('--disable-optimizations', action='store_true',
default=False, help='Use unopmitized versions of core operations')
parser.add_argument('--disable-cache', action='store_true',
default=False, help='Disable subroot caching')
return parser.parse_args()
def order_of_magnitude(num):
return int(log10(num)) if not num == 0 else 0
def strpath(rule, path):
s2 = 3 * ' '
s3 = 3 * ' '
template = '\n{s1}[{index}]{s2}{bit}{s3}{value}'
pairs = []
for index, (bit, value) in enumerate(zip(rule, path)):
s1 = (7 - order_of_magnitude(index)) * ' '
kw = {'s1': s1, 'index': index, 's2': s2, 'bit': bit, 's3': s3,
'value': value}
pairs += [template.format(**kw)]
return ''.join(pairs)
def strtree(tree):
if isinstance(tree, SqliteTree):
entries = [tree.get_entry(index) for index in range(1, tree.get_size()
+ 1)]
tree = InmemoryTree.init_from_entries(entries)
return str(tree)
def strproof(proof):
template = """
algorithm : {algorithm}
security : {security}
size : {size}
rule : {rule}
subset : {subset}
{path}\n\n"""
data = proof.serialize()
metadata = data['metadata']
rule = data['rule']
subset = data['subset']
path = data['path']
path = strpath(rule, path)
kw = {**metadata, 'rule': rule, 'subset': subset, 'path': path}
return template.format(**kw)
if __name__ == '__main__':
args = parse_cli_args()
MerkleTree = { 'inmemory': InmemoryTree, 'sqlite': SqliteTree }[
args.backend]
config = {'algorithm': args.algorithm,
'disable_security': args.disable_security,
'disable_optimizations': args.disable_optimizations,
'disable_cache': args.disable_cache,
'threshold': args.threshold,
'capacity': args.capacity}
tree = MerkleTree(**config)
# Populate tree with some entries
for data in [b'foo', b'bar', b'baz', b'qux', b'quux']:
tree.append_entry(data)
sys.stdout.write('\n nr leaves: %d' % tree.get_size())
sys.stdout.write(strtree(tree))
# Prove and verify inclusion of `bar`
proof = tree.prove_inclusion(2)
sys.stdout.write(strproof(proof))
root = tree.get_state()
base = tree.get_leaf(2)
verify_inclusion(base, root, proof)
# Save current state and append further entries
size1 = tree.get_size()
state1 = tree.get_state()
for data in [b'corge', b'grault', b'garlpy']:
tree.append_entry(data)
sys.stdout.write('\n nr leaves: %d' % tree.get_size())
sys.stdout.write(strtree(tree))
# Prove and verify previous state
size2 = tree.get_size()
proof = tree.prove_consistency(size1, size2)
sys.stdout.write(strproof(proof))
state2 = tree.get_state()
verify_consistency(state1, state2, proof)
print(tree.get_cache_info())